My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

temperature.cpp 49KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635
  1. /*
  2. temperature.c - temperature control
  3. Part of Marlin
  4. Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. /*
  17. This firmware is a mashup between Sprinter and grbl.
  18. (https://github.com/kliment/Sprinter)
  19. (https://github.com/simen/grbl/tree)
  20. It has preliminary support for Matthew Roberts advance algorithm
  21. http://reprap.org/pipermail/reprap-dev/2011-May/003323.html
  22. */
  23. #include "Marlin.h"
  24. #include "ultralcd.h"
  25. #include "temperature.h"
  26. #include "watchdog.h"
  27. #include "language.h"
  28. #include "Sd2PinMap.h"
  29. //===========================================================================
  30. //================================== macros =================================
  31. //===========================================================================
  32. #if EXTRUDERS > 4
  33. #error Unsupported number of extruders
  34. #elif EXTRUDERS > 3
  35. #define ARRAY_BY_EXTRUDERS(v1, v2, v3, v4) { v1, v2, v3, v4 }
  36. #elif EXTRUDERS > 2
  37. #define ARRAY_BY_EXTRUDERS(v1, v2, v3, v4) { v1, v2, v3 }
  38. #elif EXTRUDERS > 1
  39. #define ARRAY_BY_EXTRUDERS(v1, v2, v3, v4) { v1, v2 }
  40. #else
  41. #define ARRAY_BY_EXTRUDERS(v1, v2, v3, v4) { v1 }
  42. #endif
  43. #define HAS_TEMP_0 (defined(TEMP_0_PIN) && TEMP_0_PIN >= 0)
  44. #define HAS_TEMP_1 (defined(TEMP_1_PIN) && TEMP_1_PIN >= 0)
  45. #define HAS_TEMP_2 (defined(TEMP_2_PIN) && TEMP_2_PIN >= 0)
  46. #define HAS_TEMP_3 (defined(TEMP_3_PIN) && TEMP_3_PIN >= 0)
  47. #define HAS_TEMP_BED (defined(TEMP_BED_PIN) && TEMP_BED_PIN >= 0)
  48. #define HAS_FILAMENT_SENSOR (defined(FILAMENT_SENSOR) && defined(FILWIDTH_PIN) && FILWIDTH_PIN >= 0)
  49. #define HAS_HEATER_0 (defined(HEATER_0_PIN) && HEATER_0_PIN >= 0)
  50. #define HAS_HEATER_1 (defined(HEATER_1_PIN) && HEATER_1_PIN >= 0)
  51. #define HAS_HEATER_2 (defined(HEATER_2_PIN) && HEATER_2_PIN >= 0)
  52. #define HAS_HEATER_3 (defined(HEATER_3_PIN) && HEATER_3_PIN >= 0)
  53. #define HAS_HEATER_BED (defined(HEATER_BED_PIN) && HEATER_BED_PIN >= 0)
  54. #define HAS_AUTO_FAN_0 (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN >= 0)
  55. #define HAS_AUTO_FAN_1 (defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN >= 0)
  56. #define HAS_AUTO_FAN_2 (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN >= 0)
  57. #define HAS_AUTO_FAN_3 (defined(EXTRUDER_3_AUTO_FAN_PIN) && EXTRUDER_3_AUTO_FAN_PIN >= 0)
  58. #define HAS_AUTO_FAN HAS_AUTO_FAN_0 || HAS_AUTO_FAN_1 || HAS_AUTO_FAN_2 || HAS_AUTO_FAN_3
  59. #define HAS_FAN (defined(FAN_PIN) && FAN_PIN >= 0)
  60. //===========================================================================
  61. //============================= public variables ============================
  62. //===========================================================================
  63. // Sampling period of the temperature routine
  64. #ifdef PID_dT
  65. #undef PID_dT
  66. #endif
  67. #define PID_dT ((OVERSAMPLENR * 12.0)/(F_CPU / 64.0 / 256.0))
  68. int target_temperature[EXTRUDERS] = { 0 };
  69. int target_temperature_bed = 0;
  70. int current_temperature_raw[EXTRUDERS] = { 0 };
  71. float current_temperature[EXTRUDERS] = { 0.0 };
  72. int current_temperature_bed_raw = 0;
  73. float current_temperature_bed = 0.0;
  74. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  75. int redundant_temperature_raw = 0;
  76. float redundant_temperature = 0.0;
  77. #endif
  78. #ifdef PIDTEMPBED
  79. float bedKp=DEFAULT_bedKp;
  80. float bedKi=(DEFAULT_bedKi*PID_dT);
  81. float bedKd=(DEFAULT_bedKd/PID_dT);
  82. #endif //PIDTEMPBED
  83. #ifdef FAN_SOFT_PWM
  84. unsigned char fanSpeedSoftPwm;
  85. #endif
  86. unsigned char soft_pwm_bed;
  87. #ifdef BABYSTEPPING
  88. volatile int babystepsTodo[3] = { 0 };
  89. #endif
  90. #ifdef FILAMENT_SENSOR
  91. int current_raw_filwidth = 0; //Holds measured filament diameter - one extruder only
  92. #endif
  93. //===========================================================================
  94. //=============================private variables============================
  95. //===========================================================================
  96. static volatile bool temp_meas_ready = false;
  97. #ifdef PIDTEMP
  98. //static cannot be external:
  99. static float temp_iState[EXTRUDERS] = { 0 };
  100. static float temp_dState[EXTRUDERS] = { 0 };
  101. static float pTerm[EXTRUDERS];
  102. static float iTerm[EXTRUDERS];
  103. static float dTerm[EXTRUDERS];
  104. //int output;
  105. static float pid_error[EXTRUDERS];
  106. static float temp_iState_min[EXTRUDERS];
  107. static float temp_iState_max[EXTRUDERS];
  108. // static float pid_input[EXTRUDERS];
  109. // static float pid_output[EXTRUDERS];
  110. static bool pid_reset[EXTRUDERS];
  111. #endif //PIDTEMP
  112. #ifdef PIDTEMPBED
  113. //static cannot be external:
  114. static float temp_iState_bed = { 0 };
  115. static float temp_dState_bed = { 0 };
  116. static float pTerm_bed;
  117. static float iTerm_bed;
  118. static float dTerm_bed;
  119. //int output;
  120. static float pid_error_bed;
  121. static float temp_iState_min_bed;
  122. static float temp_iState_max_bed;
  123. #else //PIDTEMPBED
  124. static unsigned long previous_millis_bed_heater;
  125. #endif //PIDTEMPBED
  126. static unsigned char soft_pwm[EXTRUDERS];
  127. #ifdef FAN_SOFT_PWM
  128. static unsigned char soft_pwm_fan;
  129. #endif
  130. #if HAS_AUTO_FAN
  131. static unsigned long extruder_autofan_last_check;
  132. #endif
  133. #ifdef PIDTEMP
  134. #ifdef PID_PARAMS_PER_EXTRUDER
  135. float Kp[EXTRUDERS] = ARRAY_BY_EXTRUDERS(DEFAULT_Kp, DEFAULT_Kp, DEFAULT_Kp, DEFAULT_Kp);
  136. float Ki[EXTRUDERS] = ARRAY_BY_EXTRUDERS(DEFAULT_Ki*PID_dT, DEFAULT_Ki*PID_dT, DEFAULT_Ki*PID_dT, DEFAULT_Ki*PID_dT);
  137. float Kd[EXTRUDERS] = ARRAY_BY_EXTRUDERS(DEFAULT_Kd / PID_dT, DEFAULT_Kd / PID_dT, DEFAULT_Kd / PID_dT, DEFAULT_Kd / PID_dT);
  138. #ifdef PID_ADD_EXTRUSION_RATE
  139. float Kc[EXTRUDERS] = ARRAY_BY_EXTRUDERS(DEFAULT_Kc, DEFAULT_Kc, DEFAULT_Kc, DEFAULT_Kc);
  140. #endif // PID_ADD_EXTRUSION_RATE
  141. #else //PID_PARAMS_PER_EXTRUDER
  142. float Kp = DEFAULT_Kp;
  143. float Ki = DEFAULT_Ki * PID_dT;
  144. float Kd = DEFAULT_Kd / PID_dT;
  145. #ifdef PID_ADD_EXTRUSION_RATE
  146. float Kc = DEFAULT_Kc;
  147. #endif // PID_ADD_EXTRUSION_RATE
  148. #endif // PID_PARAMS_PER_EXTRUDER
  149. #endif //PIDTEMP
  150. // Init min and max temp with extreme values to prevent false errors during startup
  151. static int minttemp_raw[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_RAW_LO_TEMP , HEATER_1_RAW_LO_TEMP , HEATER_2_RAW_LO_TEMP, HEATER_3_RAW_LO_TEMP);
  152. static int maxttemp_raw[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_RAW_HI_TEMP , HEATER_1_RAW_HI_TEMP , HEATER_2_RAW_HI_TEMP, HEATER_3_RAW_HI_TEMP);
  153. static int minttemp[EXTRUDERS] = ARRAY_BY_EXTRUDERS( 0, 0, 0, 0 );
  154. static int maxttemp[EXTRUDERS] = ARRAY_BY_EXTRUDERS( 16383, 16383, 16383, 16383 );
  155. //static int bed_minttemp_raw = HEATER_BED_RAW_LO_TEMP; /* No bed mintemp error implemented?!? */
  156. #ifdef BED_MAXTEMP
  157. static int bed_maxttemp_raw = HEATER_BED_RAW_HI_TEMP;
  158. #endif
  159. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  160. static void *heater_ttbl_map[2] = {(void *)HEATER_0_TEMPTABLE, (void *)HEATER_1_TEMPTABLE };
  161. static uint8_t heater_ttbllen_map[2] = { HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN };
  162. #else
  163. static void *heater_ttbl_map[EXTRUDERS] = ARRAY_BY_EXTRUDERS( (void *)HEATER_0_TEMPTABLE, (void *)HEATER_1_TEMPTABLE, (void *)HEATER_2_TEMPTABLE, (void *)HEATER_3_TEMPTABLE );
  164. static uint8_t heater_ttbllen_map[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN, HEATER_2_TEMPTABLE_LEN, HEATER_3_TEMPTABLE_LEN );
  165. #endif
  166. static float analog2temp(int raw, uint8_t e);
  167. static float analog2tempBed(int raw);
  168. static void updateTemperaturesFromRawValues();
  169. #ifdef WATCH_TEMP_PERIOD
  170. int watch_start_temp[EXTRUDERS] = ARRAY_BY_EXTRUDERS(0,0,0,0);
  171. unsigned long watchmillis[EXTRUDERS] = ARRAY_BY_EXTRUDERS(0,0,0,0);
  172. #endif //WATCH_TEMP_PERIOD
  173. #ifndef SOFT_PWM_SCALE
  174. #define SOFT_PWM_SCALE 0
  175. #endif
  176. #ifdef FILAMENT_SENSOR
  177. static int meas_shift_index; //used to point to a delayed sample in buffer for filament width sensor
  178. #endif
  179. #ifdef HEATER_0_USES_MAX6675
  180. static int read_max6675();
  181. #endif
  182. //===========================================================================
  183. //============================= functions ============================
  184. //===========================================================================
  185. void PID_autotune(float temp, int extruder, int ncycles)
  186. {
  187. float input = 0.0;
  188. int cycles = 0;
  189. bool heating = true;
  190. unsigned long temp_millis = millis(), t1 = temp_millis, t2 = temp_millis;
  191. long t_high = 0, t_low = 0;
  192. long bias, d;
  193. float Ku, Tu;
  194. float Kp, Ki, Kd;
  195. float max = 0, min = 10000;
  196. #if HAS_AUTO_FAN
  197. unsigned long extruder_autofan_last_check = temp_millis;
  198. #endif
  199. if (extruder >= EXTRUDERS
  200. #if !HAS_TEMP_BED
  201. || extruder < 0
  202. #endif
  203. ) {
  204. SERIAL_ECHOLN(MSG_PID_BAD_EXTRUDER_NUM);
  205. return;
  206. }
  207. SERIAL_ECHOLN(MSG_PID_AUTOTUNE_START);
  208. disable_heater(); // switch off all heaters.
  209. if (extruder < 0)
  210. soft_pwm_bed = bias = d = MAX_BED_POWER / 2;
  211. else
  212. soft_pwm[extruder] = bias = d = PID_MAX / 2;
  213. // PID Tuning loop
  214. for(;;) {
  215. unsigned long ms = millis();
  216. if (temp_meas_ready == true) { // temp sample ready
  217. updateTemperaturesFromRawValues();
  218. input = (extruder<0)?current_temperature_bed:current_temperature[extruder];
  219. max = max(max, input);
  220. min = min(min, input);
  221. #if HAS_AUTO_FAN
  222. if (ms > extruder_autofan_last_check + 2500) {
  223. checkExtruderAutoFans();
  224. extruder_autofan_last_check = ms;
  225. }
  226. #endif
  227. if (heating == true && input > temp) {
  228. if (ms - t2 > 5000) {
  229. heating = false;
  230. if (extruder < 0)
  231. soft_pwm_bed = (bias - d) >> 1;
  232. else
  233. soft_pwm[extruder] = (bias - d) >> 1;
  234. t1 = ms;
  235. t_high = t1 - t2;
  236. max = temp;
  237. }
  238. }
  239. if (heating == false && input < temp) {
  240. if (ms - t1 > 5000) {
  241. heating = true;
  242. t2 = ms;
  243. t_low = t2 - t1;
  244. if (cycles > 0) {
  245. long max_pow = extruder < 0 ? MAX_BED_POWER : PID_MAX;
  246. bias += (d*(t_high - t_low))/(t_low + t_high);
  247. bias = constrain(bias, 20, max_pow - 20);
  248. d = (bias > max_pow / 2) ? max_pow - 1 - bias : bias;
  249. SERIAL_PROTOCOLPGM(MSG_BIAS); SERIAL_PROTOCOL(bias);
  250. SERIAL_PROTOCOLPGM(MSG_D); SERIAL_PROTOCOL(d);
  251. SERIAL_PROTOCOLPGM(MSG_T_MIN); SERIAL_PROTOCOL(min);
  252. SERIAL_PROTOCOLPGM(MSG_T_MAX); SERIAL_PROTOCOLLN(max);
  253. if (cycles > 2) {
  254. Ku = (4.0 * d) / (3.14159265 * (max - min) / 2.0);
  255. Tu = ((float)(t_low + t_high) / 1000.0);
  256. SERIAL_PROTOCOLPGM(MSG_KU); SERIAL_PROTOCOL(Ku);
  257. SERIAL_PROTOCOLPGM(MSG_TU); SERIAL_PROTOCOLLN(Tu);
  258. Kp = 0.6 * Ku;
  259. Ki = 2 * Kp / Tu;
  260. Kd = Kp * Tu / 8;
  261. SERIAL_PROTOCOLLNPGM(MSG_CLASSIC_PID);
  262. SERIAL_PROTOCOLPGM(MSG_KP); SERIAL_PROTOCOLLN(Kp);
  263. SERIAL_PROTOCOLPGM(MSG_KI); SERIAL_PROTOCOLLN(Ki);
  264. SERIAL_PROTOCOLPGM(MSG_KD); SERIAL_PROTOCOLLN(Kd);
  265. /*
  266. Kp = 0.33*Ku;
  267. Ki = Kp/Tu;
  268. Kd = Kp*Tu/3;
  269. SERIAL_PROTOCOLLNPGM(" Some overshoot ");
  270. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(Kp);
  271. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(Ki);
  272. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(Kd);
  273. Kp = 0.2*Ku;
  274. Ki = 2*Kp/Tu;
  275. Kd = Kp*Tu/3;
  276. SERIAL_PROTOCOLLNPGM(" No overshoot ");
  277. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(Kp);
  278. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(Ki);
  279. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(Kd);
  280. */
  281. }
  282. }
  283. if (extruder < 0)
  284. soft_pwm_bed = (bias + d) >> 1;
  285. else
  286. soft_pwm[extruder] = (bias + d) >> 1;
  287. cycles++;
  288. min = temp;
  289. }
  290. }
  291. }
  292. if (input > temp + 20) {
  293. SERIAL_PROTOCOLLNPGM(MSG_PID_TEMP_TOO_HIGH);
  294. return;
  295. }
  296. // Every 2 seconds...
  297. if (ms > temp_millis + 2000) {
  298. int p;
  299. if (extruder < 0) {
  300. p = soft_pwm_bed;
  301. SERIAL_PROTOCOLPGM(MSG_OK_B);
  302. }
  303. else {
  304. p = soft_pwm[extruder];
  305. SERIAL_PROTOCOLPGM(MSG_OK_T);
  306. }
  307. SERIAL_PROTOCOL(input);
  308. SERIAL_PROTOCOLPGM(MSG_AT);
  309. SERIAL_PROTOCOLLN(p);
  310. temp_millis = ms;
  311. } // every 2 seconds
  312. // Over 2 minutes?
  313. if (((ms - t1) + (ms - t2)) > (10L*60L*1000L*2L)) {
  314. SERIAL_PROTOCOLLNPGM(MSG_PID_TIMEOUT);
  315. return;
  316. }
  317. if (cycles > ncycles) {
  318. SERIAL_PROTOCOLLNPGM(MSG_PID_AUTOTUNE_FINISHED);
  319. return;
  320. }
  321. lcd_update();
  322. }
  323. }
  324. void updatePID() {
  325. #ifdef PIDTEMP
  326. for (int e = 0; e < EXTRUDERS; e++) {
  327. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / PID_PARAM(Ki,e);
  328. }
  329. #endif
  330. #ifdef PIDTEMPBED
  331. temp_iState_max_bed = PID_INTEGRAL_DRIVE_MAX / bedKi;
  332. #endif
  333. }
  334. int getHeaterPower(int heater) {
  335. return heater < 0 ? soft_pwm_bed : soft_pwm[heater];
  336. }
  337. #if HAS_AUTO_FAN
  338. #if HAS_FAN
  339. #if EXTRUDER_0_AUTO_FAN_PIN == FAN_PIN
  340. #error "You cannot set EXTRUDER_0_AUTO_FAN_PIN equal to FAN_PIN"
  341. #endif
  342. #if EXTRUDER_1_AUTO_FAN_PIN == FAN_PIN
  343. #error "You cannot set EXTRUDER_1_AUTO_FAN_PIN equal to FAN_PIN"
  344. #endif
  345. #if EXTRUDER_2_AUTO_FAN_PIN == FAN_PIN
  346. #error "You cannot set EXTRUDER_2_AUTO_FAN_PIN equal to FAN_PIN"
  347. #endif
  348. #if EXTRUDER_3_AUTO_FAN_PIN == FAN_PIN
  349. #error "You cannot set EXTRUDER_3_AUTO_FAN_PIN equal to FAN_PIN"
  350. #endif
  351. #endif
  352. void setExtruderAutoFanState(int pin, bool state)
  353. {
  354. unsigned char newFanSpeed = (state != 0) ? EXTRUDER_AUTO_FAN_SPEED : 0;
  355. // this idiom allows both digital and PWM fan outputs (see M42 handling).
  356. pinMode(pin, OUTPUT);
  357. digitalWrite(pin, newFanSpeed);
  358. analogWrite(pin, newFanSpeed);
  359. }
  360. void checkExtruderAutoFans()
  361. {
  362. uint8_t fanState = 0;
  363. // which fan pins need to be turned on?
  364. #if HAS_AUTO_FAN_0
  365. if (current_temperature[0] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  366. fanState |= 1;
  367. #endif
  368. #if HAS_AUTO_FAN_1
  369. if (current_temperature[1] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  370. {
  371. if (EXTRUDER_1_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN)
  372. fanState |= 1;
  373. else
  374. fanState |= 2;
  375. }
  376. #endif
  377. #if HAS_AUTO_FAN_2
  378. if (current_temperature[2] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  379. {
  380. if (EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN)
  381. fanState |= 1;
  382. else if (EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_1_AUTO_FAN_PIN)
  383. fanState |= 2;
  384. else
  385. fanState |= 4;
  386. }
  387. #endif
  388. #if HAS_AUTO_FAN_3
  389. if (current_temperature[3] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  390. {
  391. if (EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN)
  392. fanState |= 1;
  393. else if (EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_1_AUTO_FAN_PIN)
  394. fanState |= 2;
  395. else if (EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_2_AUTO_FAN_PIN)
  396. fanState |= 4;
  397. else
  398. fanState |= 8;
  399. }
  400. #endif
  401. // update extruder auto fan states
  402. #if HAS_AUTO_FAN_0
  403. setExtruderAutoFanState(EXTRUDER_0_AUTO_FAN_PIN, (fanState & 1) != 0);
  404. #endif
  405. #if HAS_AUTO_FAN_1
  406. if (EXTRUDER_1_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN)
  407. setExtruderAutoFanState(EXTRUDER_1_AUTO_FAN_PIN, (fanState & 2) != 0);
  408. #endif
  409. #if HAS_AUTO_FAN_2
  410. if (EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN
  411. && EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_1_AUTO_FAN_PIN)
  412. setExtruderAutoFanState(EXTRUDER_2_AUTO_FAN_PIN, (fanState & 4) != 0);
  413. #endif
  414. #if HAS_AUTO_FAN_3
  415. if (EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN
  416. && EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_1_AUTO_FAN_PIN
  417. && EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_2_AUTO_FAN_PIN)
  418. setExtruderAutoFanState(EXTRUDER_3_AUTO_FAN_PIN, (fanState & 8) != 0);
  419. #endif
  420. }
  421. #endif // any extruder auto fan pins set
  422. //
  423. // Error checking and Write Routines
  424. //
  425. #if !HAS_HEATER_0
  426. #error HEATER_0_PIN not defined for this board
  427. #endif
  428. #define WRITE_HEATER_0P(v) WRITE(HEATER_0_PIN, v)
  429. #if EXTRUDERS > 1 || defined(HEATERS_PARALLEL)
  430. #if !HAS_HEATER_1
  431. #error HEATER_1_PIN not defined for this board
  432. #endif
  433. #define WRITE_HEATER_1(v) WRITE(HEATER_1_PIN, v)
  434. #if EXTRUDERS > 2
  435. #if !HAS_HEATER_2
  436. #error HEATER_2_PIN not defined for this board
  437. #endif
  438. #define WRITE_HEATER_2(v) WRITE(HEATER_2_PIN, v)
  439. #if EXTRUDERS > 3
  440. #if !HAS_HEATER_3
  441. #error HEATER_3_PIN not defined for this board
  442. #endif
  443. #define WRITE_HEATER_3(v) WRITE(HEATER_3_PIN, v)
  444. #endif
  445. #endif
  446. #endif
  447. #ifdef HEATERS_PARALLEL
  448. #define WRITE_HEATER_0(v) { WRITE_HEATER_0P(v); WRITE_HEATER_1(v); }
  449. #else
  450. #define WRITE_HEATER_0(v) WRITE_HEATER_0P(v)
  451. #endif
  452. #if HAS_HEATER_BED
  453. #define WRITE_HEATER_BED(v) WRITE(HEATER_BED_PIN, v)
  454. #endif
  455. #if HAS_FAN
  456. #define WRITE_FAN(v) WRITE(FAN_PIN, v)
  457. #endif
  458. void manage_heater() {
  459. if (!temp_meas_ready) return;
  460. float pid_input, pid_output;
  461. updateTemperaturesFromRawValues();
  462. #ifdef HEATER_0_USES_MAX6675
  463. float ct = current_temperature[0];
  464. if (ct > min(HEATER_0_MAXTEMP, 1023)) max_temp_error(0);
  465. if (ct < max(HEATER_0_MINTEMP, 0.01)) min_temp_error(0);
  466. #endif //HEATER_0_USES_MAX6675
  467. unsigned long ms = millis();
  468. // Loop through all extruders
  469. for (int e = 0; e < EXTRUDERS; e++) {
  470. #if defined (THERMAL_RUNAWAY_PROTECTION_PERIOD) && THERMAL_RUNAWAY_PROTECTION_PERIOD > 0
  471. thermal_runaway_protection(&thermal_runaway_state_machine[e], &thermal_runaway_timer[e], current_temperature[e], target_temperature[e], e, THERMAL_RUNAWAY_PROTECTION_PERIOD, THERMAL_RUNAWAY_PROTECTION_HYSTERESIS);
  472. #endif
  473. #ifdef PIDTEMP
  474. pid_input = current_temperature[e];
  475. #ifndef PID_OPENLOOP
  476. pid_error[e] = target_temperature[e] - pid_input;
  477. if (pid_error[e] > PID_FUNCTIONAL_RANGE) {
  478. pid_output = BANG_MAX;
  479. pid_reset[e] = true;
  480. }
  481. else if (pid_error[e] < -PID_FUNCTIONAL_RANGE || target_temperature[e] == 0) {
  482. pid_output = 0;
  483. pid_reset[e] = true;
  484. }
  485. else {
  486. if (pid_reset[e] == true) {
  487. temp_iState[e] = 0.0;
  488. pid_reset[e] = false;
  489. }
  490. pTerm[e] = PID_PARAM(Kp,e) * pid_error[e];
  491. temp_iState[e] += pid_error[e];
  492. temp_iState[e] = constrain(temp_iState[e], temp_iState_min[e], temp_iState_max[e]);
  493. iTerm[e] = PID_PARAM(Ki,e) * temp_iState[e];
  494. //K1 defined in Configuration.h in the PID settings
  495. #define K2 (1.0-K1)
  496. dTerm[e] = (PID_PARAM(Kd,e) * (pid_input - temp_dState[e])) * K2 + (K1 * dTerm[e]);
  497. pid_output = pTerm[e] + iTerm[e] - dTerm[e];
  498. if (pid_output > PID_MAX) {
  499. if (pid_error[e] > 0) temp_iState[e] -= pid_error[e]; // conditional un-integration
  500. pid_output = PID_MAX;
  501. }
  502. else if (pid_output < 0) {
  503. if (pid_error[e] < 0) temp_iState[e] -= pid_error[e]; // conditional un-integration
  504. pid_output = 0;
  505. }
  506. }
  507. temp_dState[e] = pid_input;
  508. #else
  509. pid_output = constrain(target_temperature[e], 0, PID_MAX);
  510. #endif //PID_OPENLOOP
  511. #ifdef PID_DEBUG
  512. SERIAL_ECHO_START;
  513. SERIAL_ECHO(MSG_PID_DEBUG);
  514. SERIAL_ECHO(e);
  515. SERIAL_ECHO(MSG_PID_DEBUG_INPUT);
  516. SERIAL_ECHO(pid_input);
  517. SERIAL_ECHO(MSG_PID_DEBUG_OUTPUT);
  518. SERIAL_ECHO(pid_output);
  519. SERIAL_ECHO(MSG_PID_DEBUG_PTERM);
  520. SERIAL_ECHO(pTerm[e]);
  521. SERIAL_ECHO(MSG_PID_DEBUG_ITERM);
  522. SERIAL_ECHO(iTerm[e]);
  523. SERIAL_ECHO(MSG_PID_DEBUG_DTERM);
  524. SERIAL_ECHOLN(dTerm[e]);
  525. #endif //PID_DEBUG
  526. #else /* PID off */
  527. pid_output = 0;
  528. if (current_temperature[e] < target_temperature[e]) pid_output = PID_MAX;
  529. #endif
  530. // Check if temperature is within the correct range
  531. soft_pwm[e] = current_temperature[e] > minttemp[e] && current_temperature[e] < maxttemp[e] ? (int)pid_output >> 1 : 0;
  532. #ifdef WATCH_TEMP_PERIOD
  533. if (watchmillis[e] && ms > watchmillis[e] + WATCH_TEMP_PERIOD) {
  534. if (degHotend(e) < watch_start_temp[e] + WATCH_TEMP_INCREASE) {
  535. setTargetHotend(0, e);
  536. LCD_MESSAGEPGM(MSG_HEATING_FAILED_LCD); // translatable
  537. SERIAL_ECHO_START;
  538. SERIAL_ECHOLNPGM(MSG_HEATING_FAILED);
  539. }
  540. else {
  541. watchmillis[e] = 0;
  542. }
  543. }
  544. #endif //WATCH_TEMP_PERIOD
  545. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  546. if (fabs(current_temperature[0] - redundant_temperature) > MAX_REDUNDANT_TEMP_SENSOR_DIFF) {
  547. disable_heater();
  548. if (IsStopped() == false) {
  549. SERIAL_ERROR_START;
  550. SERIAL_ERRORLNPGM(MSG_EXTRUDER_SWITCHED_OFF);
  551. LCD_ALERTMESSAGEPGM(MSG_ERR_REDUNDANT_TEMP); // translatable
  552. }
  553. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  554. Stop();
  555. #endif
  556. }
  557. #endif //TEMP_SENSOR_1_AS_REDUNDANT
  558. } // Extruders Loop
  559. #if HAS_AUTO_FAN
  560. if (ms > extruder_autofan_last_check + 2500) { // only need to check fan state very infrequently
  561. checkExtruderAutoFans();
  562. extruder_autofan_last_check = ms;
  563. }
  564. #endif
  565. #ifndef PIDTEMPBED
  566. if (ms < previous_millis_bed_heater + BED_CHECK_INTERVAL) return;
  567. previous_millis_bed_heater = ms;
  568. #endif //PIDTEMPBED
  569. #if TEMP_SENSOR_BED != 0
  570. #if defined(THERMAL_RUNAWAY_PROTECTION_BED_PERIOD) && THERMAL_RUNAWAY_PROTECTION_BED_PERIOD > 0
  571. thermal_runaway_protection(&thermal_runaway_bed_state_machine, &thermal_runaway_bed_timer, current_temperature_bed, target_temperature_bed, 9, THERMAL_RUNAWAY_PROTECTION_BED_PERIOD, THERMAL_RUNAWAY_PROTECTION_BED_HYSTERESIS);
  572. #endif
  573. #ifdef PIDTEMPBED
  574. pid_input = current_temperature_bed;
  575. #ifndef PID_OPENLOOP
  576. pid_error_bed = target_temperature_bed - pid_input;
  577. pTerm_bed = bedKp * pid_error_bed;
  578. temp_iState_bed += pid_error_bed;
  579. temp_iState_bed = constrain(temp_iState_bed, temp_iState_min_bed, temp_iState_max_bed);
  580. iTerm_bed = bedKi * temp_iState_bed;
  581. //K1 defined in Configuration.h in the PID settings
  582. #define K2 (1.0-K1)
  583. dTerm_bed = (bedKd * (pid_input - temp_dState_bed))*K2 + (K1 * dTerm_bed);
  584. temp_dState_bed = pid_input;
  585. pid_output = pTerm_bed + iTerm_bed - dTerm_bed;
  586. if (pid_output > MAX_BED_POWER) {
  587. if (pid_error_bed > 0) temp_iState_bed -= pid_error_bed; // conditional un-integration
  588. pid_output = MAX_BED_POWER;
  589. }
  590. else if (pid_output < 0) {
  591. if (pid_error_bed < 0) temp_iState_bed -= pid_error_bed; // conditional un-integration
  592. pid_output = 0;
  593. }
  594. #else
  595. pid_output = constrain(target_temperature_bed, 0, MAX_BED_POWER);
  596. #endif //PID_OPENLOOP
  597. soft_pwm_bed = current_temperature_bed > BED_MINTEMP && current_temperature_bed < BED_MAXTEMP ? (int)pid_output >> 1 : 0;
  598. #elif !defined(BED_LIMIT_SWITCHING)
  599. // Check if temperature is within the correct range
  600. if (current_temperature_bed > BED_MINTEMP && current_temperature_bed < BED_MAXTEMP) {
  601. soft_pwm_bed = current_temperature_bed < target_temperature_bed ? MAX_BED_POWER >> 1 : 0;
  602. }
  603. else {
  604. soft_pwm_bed = 0;
  605. WRITE_HEATER_BED(LOW);
  606. }
  607. #else //#ifdef BED_LIMIT_SWITCHING
  608. // Check if temperature is within the correct band
  609. if (current_temperature_bed > BED_MINTEMP && current_temperature_bed < BED_MAXTEMP) {
  610. if (current_temperature_bed >= target_temperature_bed + BED_HYSTERESIS)
  611. soft_pwm_bed = 0;
  612. else if (current_temperature_bed <= target_temperature_bed - BED_HYSTERESIS)
  613. soft_pwm_bed = MAX_BED_POWER >> 1;
  614. }
  615. else {
  616. soft_pwm_bed = 0;
  617. WRITE_HEATER_BED(LOW);
  618. }
  619. #endif
  620. #endif //TEMP_SENSOR_BED != 0
  621. // Control the extruder rate based on the width sensor
  622. #ifdef FILAMENT_SENSOR
  623. if (filament_sensor) {
  624. meas_shift_index = delay_index1 - meas_delay_cm;
  625. if (meas_shift_index < 0) meas_shift_index += MAX_MEASUREMENT_DELAY + 1; //loop around buffer if needed
  626. // Get the delayed info and add 100 to reconstitute to a percent of
  627. // the nominal filament diameter then square it to get an area
  628. meas_shift_index = constrain(meas_shift_index, 0, MAX_MEASUREMENT_DELAY);
  629. float vm = pow((measurement_delay[meas_shift_index] + 100.0) / 100.0, 2);
  630. if (vm < 0.01) vm = 0.01;
  631. volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] = vm;
  632. }
  633. #endif //FILAMENT_SENSOR
  634. }
  635. #define PGM_RD_W(x) (short)pgm_read_word(&x)
  636. // Derived from RepRap FiveD extruder::getTemperature()
  637. // For hot end temperature measurement.
  638. static float analog2temp(int raw, uint8_t e) {
  639. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  640. if (e > EXTRUDERS)
  641. #else
  642. if (e >= EXTRUDERS)
  643. #endif
  644. {
  645. SERIAL_ERROR_START;
  646. SERIAL_ERROR((int)e);
  647. SERIAL_ERRORLNPGM(MSG_INVALID_EXTRUDER_NUM);
  648. kill();
  649. return 0.0;
  650. }
  651. #ifdef HEATER_0_USES_MAX6675
  652. if (e == 0)
  653. {
  654. return 0.25 * raw;
  655. }
  656. #endif
  657. if(heater_ttbl_map[e] != NULL)
  658. {
  659. float celsius = 0;
  660. uint8_t i;
  661. short (*tt)[][2] = (short (*)[][2])(heater_ttbl_map[e]);
  662. for (i=1; i<heater_ttbllen_map[e]; i++)
  663. {
  664. if (PGM_RD_W((*tt)[i][0]) > raw)
  665. {
  666. celsius = PGM_RD_W((*tt)[i-1][1]) +
  667. (raw - PGM_RD_W((*tt)[i-1][0])) *
  668. (float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i-1][1])) /
  669. (float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i-1][0]));
  670. break;
  671. }
  672. }
  673. // Overflow: Set to last value in the table
  674. if (i == heater_ttbllen_map[e]) celsius = PGM_RD_W((*tt)[i-1][1]);
  675. return celsius;
  676. }
  677. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  678. }
  679. // Derived from RepRap FiveD extruder::getTemperature()
  680. // For bed temperature measurement.
  681. static float analog2tempBed(int raw) {
  682. #ifdef BED_USES_THERMISTOR
  683. float celsius = 0;
  684. byte i;
  685. for (i=1; i<BEDTEMPTABLE_LEN; i++)
  686. {
  687. if (PGM_RD_W(BEDTEMPTABLE[i][0]) > raw)
  688. {
  689. celsius = PGM_RD_W(BEDTEMPTABLE[i-1][1]) +
  690. (raw - PGM_RD_W(BEDTEMPTABLE[i-1][0])) *
  691. (float)(PGM_RD_W(BEDTEMPTABLE[i][1]) - PGM_RD_W(BEDTEMPTABLE[i-1][1])) /
  692. (float)(PGM_RD_W(BEDTEMPTABLE[i][0]) - PGM_RD_W(BEDTEMPTABLE[i-1][0]));
  693. break;
  694. }
  695. }
  696. // Overflow: Set to last value in the table
  697. if (i == BEDTEMPTABLE_LEN) celsius = PGM_RD_W(BEDTEMPTABLE[i-1][1]);
  698. return celsius;
  699. #elif defined BED_USES_AD595
  700. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  701. #else
  702. return 0;
  703. #endif
  704. }
  705. /* Called to get the raw values into the the actual temperatures. The raw values are created in interrupt context,
  706. and this function is called from normal context as it is too slow to run in interrupts and will block the stepper routine otherwise */
  707. static void updateTemperaturesFromRawValues() {
  708. #ifdef HEATER_0_USES_MAX6675
  709. current_temperature_raw[0] = read_max6675();
  710. #endif
  711. for(uint8_t e = 0; e < EXTRUDERS; e++) {
  712. current_temperature[e] = analog2temp(current_temperature_raw[e], e);
  713. }
  714. current_temperature_bed = analog2tempBed(current_temperature_bed_raw);
  715. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  716. redundant_temperature = analog2temp(redundant_temperature_raw, 1);
  717. #endif
  718. #if HAS_FILAMENT_SENSOR
  719. filament_width_meas = analog2widthFil();
  720. #endif
  721. //Reset the watchdog after we know we have a temperature measurement.
  722. watchdog_reset();
  723. CRITICAL_SECTION_START;
  724. temp_meas_ready = false;
  725. CRITICAL_SECTION_END;
  726. }
  727. #ifdef FILAMENT_SENSOR
  728. // Convert raw Filament Width to millimeters
  729. float analog2widthFil() {
  730. return current_raw_filwidth / 16383.0 * 5.0;
  731. //return current_raw_filwidth;
  732. }
  733. // Convert raw Filament Width to a ratio
  734. int widthFil_to_size_ratio() {
  735. float temp = filament_width_meas;
  736. if (temp < MEASURED_LOWER_LIMIT) temp = filament_width_nominal; //assume sensor cut out
  737. else if (temp > MEASURED_UPPER_LIMIT) temp = MEASURED_UPPER_LIMIT;
  738. return filament_width_nominal / temp * 100;
  739. }
  740. #endif
  741. void tp_init()
  742. {
  743. #if MB(RUMBA) && ((TEMP_SENSOR_0==-1)||(TEMP_SENSOR_1==-1)||(TEMP_SENSOR_2==-1)||(TEMP_SENSOR_BED==-1))
  744. //disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector
  745. MCUCR=(1<<JTD);
  746. MCUCR=(1<<JTD);
  747. #endif
  748. // Finish init of mult extruder arrays
  749. for (int e = 0; e < EXTRUDERS; e++) {
  750. // populate with the first value
  751. maxttemp[e] = maxttemp[0];
  752. #ifdef PIDTEMP
  753. temp_iState_min[e] = 0.0;
  754. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / PID_PARAM(Ki,e);
  755. #endif //PIDTEMP
  756. #ifdef PIDTEMPBED
  757. temp_iState_min_bed = 0.0;
  758. temp_iState_max_bed = PID_INTEGRAL_DRIVE_MAX / bedKi;
  759. #endif //PIDTEMPBED
  760. }
  761. #if HAS_HEATER_0
  762. SET_OUTPUT(HEATER_0_PIN);
  763. #endif
  764. #if HAS_HEATER_1
  765. SET_OUTPUT(HEATER_1_PIN);
  766. #endif
  767. #if HAS_HEATER_2
  768. SET_OUTPUT(HEATER_2_PIN);
  769. #endif
  770. #if HAS_HEATER_3
  771. SET_OUTPUT(HEATER_3_PIN);
  772. #endif
  773. #if HAS_HEATER_BED
  774. SET_OUTPUT(HEATER_BED_PIN);
  775. #endif
  776. #if HAS_FAN
  777. SET_OUTPUT(FAN_PIN);
  778. #ifdef FAST_PWM_FAN
  779. setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  780. #endif
  781. #ifdef FAN_SOFT_PWM
  782. soft_pwm_fan = fanSpeedSoftPwm / 2;
  783. #endif
  784. #endif
  785. #ifdef HEATER_0_USES_MAX6675
  786. #ifndef SDSUPPORT
  787. OUT_WRITE(SCK_PIN, LOW);
  788. OUT_WRITE(MOSI_PIN, HIGH);
  789. OUT_WRITE(MISO_PIN, HIGH);
  790. #else
  791. pinMode(SS_PIN, OUTPUT);
  792. digitalWrite(SS_PIN, HIGH);
  793. #endif
  794. OUT_WRITE(MAX6675_SS,HIGH);
  795. #endif //HEATER_0_USES_MAX6675
  796. #ifdef DIDR2
  797. #define ANALOG_SELECT(pin) do{ if (pin < 8) DIDR0 |= 1 << pin; else DIDR2 |= 1 << (pin - 8); }while(0)
  798. #else
  799. #define ANALOG_SELECT(pin) do{ DIDR0 |= 1 << pin; }while(0)
  800. #endif
  801. // Set analog inputs
  802. ADCSRA = 1<<ADEN | 1<<ADSC | 1<<ADIF | 0x07;
  803. DIDR0 = 0;
  804. #ifdef DIDR2
  805. DIDR2 = 0;
  806. #endif
  807. #if HAS_TEMP_0
  808. ANALOG_SELECT(TEMP_0_PIN);
  809. #endif
  810. #if HAS_TEMP_1
  811. ANALOG_SELECT(TEMP_1_PIN);
  812. #endif
  813. #if HAS_TEMP_2
  814. ANALOG_SELECT(TEMP_2_PIN);
  815. #endif
  816. #if HAS_TEMP_3
  817. ANALOG_SELECT(TEMP_3_PIN);
  818. #endif
  819. #if HAS_TEMP_BED
  820. ANALOG_SELECT(TEMP_BED_PIN);
  821. #endif
  822. #if HAS_FILAMENT_SENSOR
  823. ANALOG_SELECT(FILWIDTH_PIN);
  824. #endif
  825. // Use timer0 for temperature measurement
  826. // Interleave temperature interrupt with millies interrupt
  827. OCR0B = 128;
  828. TIMSK0 |= (1<<OCIE0B);
  829. // Wait for temperature measurement to settle
  830. delay(250);
  831. #define TEMP_MIN_ROUTINE(NR) \
  832. minttemp[NR] = HEATER_ ## NR ## _MINTEMP; \
  833. while(analog2temp(minttemp_raw[NR], NR) < HEATER_ ## NR ## _MINTEMP) { \
  834. if (HEATER_ ## NR ## _RAW_LO_TEMP < HEATER_ ## NR ## _RAW_HI_TEMP) \
  835. minttemp_raw[NR] += OVERSAMPLENR; \
  836. else \
  837. minttemp_raw[NR] -= OVERSAMPLENR; \
  838. }
  839. #define TEMP_MAX_ROUTINE(NR) \
  840. maxttemp[NR] = HEATER_ ## NR ## _MAXTEMP; \
  841. while(analog2temp(maxttemp_raw[NR], NR) > HEATER_ ## NR ## _MAXTEMP) { \
  842. if (HEATER_ ## NR ## _RAW_LO_TEMP < HEATER_ ## NR ## _RAW_HI_TEMP) \
  843. maxttemp_raw[NR] -= OVERSAMPLENR; \
  844. else \
  845. maxttemp_raw[NR] += OVERSAMPLENR; \
  846. }
  847. #ifdef HEATER_0_MINTEMP
  848. TEMP_MIN_ROUTINE(0);
  849. #endif
  850. #ifdef HEATER_0_MAXTEMP
  851. TEMP_MAX_ROUTINE(0);
  852. #endif
  853. #if EXTRUDERS > 1
  854. #ifdef HEATER_1_MINTEMP
  855. TEMP_MIN_ROUTINE(1);
  856. #endif
  857. #ifdef HEATER_1_MAXTEMP
  858. TEMP_MAX_ROUTINE(1);
  859. #endif
  860. #if EXTRUDERS > 2
  861. #ifdef HEATER_2_MINTEMP
  862. TEMP_MIN_ROUTINE(2);
  863. #endif
  864. #ifdef HEATER_2_MAXTEMP
  865. TEMP_MAX_ROUTINE(2);
  866. #endif
  867. #if EXTRUDERS > 3
  868. #ifdef HEATER_3_MINTEMP
  869. TEMP_MIN_ROUTINE(3);
  870. #endif
  871. #ifdef HEATER_3_MAXTEMP
  872. TEMP_MAX_ROUTINE(3);
  873. #endif
  874. #endif // EXTRUDERS > 3
  875. #endif // EXTRUDERS > 2
  876. #endif // EXTRUDERS > 1
  877. #ifdef BED_MINTEMP
  878. /* No bed MINTEMP error implemented?!? */ /*
  879. while(analog2tempBed(bed_minttemp_raw) < BED_MINTEMP) {
  880. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  881. bed_minttemp_raw += OVERSAMPLENR;
  882. #else
  883. bed_minttemp_raw -= OVERSAMPLENR;
  884. #endif
  885. }
  886. */
  887. #endif //BED_MINTEMP
  888. #ifdef BED_MAXTEMP
  889. while(analog2tempBed(bed_maxttemp_raw) > BED_MAXTEMP) {
  890. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  891. bed_maxttemp_raw -= OVERSAMPLENR;
  892. #else
  893. bed_maxttemp_raw += OVERSAMPLENR;
  894. #endif
  895. }
  896. #endif //BED_MAXTEMP
  897. }
  898. void setWatch() {
  899. #ifdef WATCH_TEMP_PERIOD
  900. unsigned long ms = millis();
  901. for (int e = 0; e < EXTRUDERS; e++) {
  902. if (degHotend(e) < degTargetHotend(e) - (WATCH_TEMP_INCREASE * 2)) {
  903. watch_start_temp[e] = degHotend(e);
  904. watchmillis[e] = ms;
  905. }
  906. }
  907. #endif
  908. }
  909. #if defined(THERMAL_RUNAWAY_PROTECTION_PERIOD) && THERMAL_RUNAWAY_PROTECTION_PERIOD > 0
  910. void thermal_runaway_protection(int *state, unsigned long *timer, float temperature, float target_temperature, int heater_id, int period_seconds, int hysteresis_degc)
  911. {
  912. /*
  913. SERIAL_ECHO_START;
  914. SERIAL_ECHO("Thermal Thermal Runaway Running. Heater ID:");
  915. SERIAL_ECHO(heater_id);
  916. SERIAL_ECHO(" ; State:");
  917. SERIAL_ECHO(*state);
  918. SERIAL_ECHO(" ; Timer:");
  919. SERIAL_ECHO(*timer);
  920. SERIAL_ECHO(" ; Temperature:");
  921. SERIAL_ECHO(temperature);
  922. SERIAL_ECHO(" ; Target Temp:");
  923. SERIAL_ECHO(target_temperature);
  924. SERIAL_ECHOLN("");
  925. */
  926. if ((target_temperature == 0) || thermal_runaway)
  927. {
  928. *state = 0;
  929. *timer = 0;
  930. return;
  931. }
  932. switch (*state)
  933. {
  934. case 0: // "Heater Inactive" state
  935. if (target_temperature > 0) *state = 1;
  936. break;
  937. case 1: // "First Heating" state
  938. if (temperature >= target_temperature) *state = 2;
  939. break;
  940. case 2: // "Temperature Stable" state
  941. {
  942. unsigned long ms = millis();
  943. if (temperature >= (target_temperature - hysteresis_degc))
  944. {
  945. *timer = ms;
  946. }
  947. else if ( (ms - *timer) > ((unsigned long) period_seconds) * 1000)
  948. {
  949. SERIAL_ERROR_START;
  950. SERIAL_ERRORLNPGM(MSG_THERMAL_RUNAWAY_STOP);
  951. SERIAL_ERRORLN((int)heater_id);
  952. LCD_ALERTMESSAGEPGM(MSG_THERMAL_RUNAWAY); // translatable
  953. thermal_runaway = true;
  954. while(1)
  955. {
  956. disable_heater();
  957. disable_x();
  958. disable_y();
  959. disable_z();
  960. disable_e0();
  961. disable_e1();
  962. disable_e2();
  963. disable_e3();
  964. manage_heater();
  965. lcd_update();
  966. }
  967. }
  968. } break;
  969. }
  970. }
  971. #endif //THERMAL_RUNAWAY_PROTECTION_PERIOD
  972. void disable_heater() {
  973. for (int i=0; i<EXTRUDERS; i++) setTargetHotend(0, i);
  974. setTargetBed(0);
  975. #if HAS_TEMP_0
  976. target_temperature[0] = 0;
  977. soft_pwm[0] = 0;
  978. WRITE_HEATER_0P(LOW); // If HEATERS_PARALLEL should apply, change to WRITE_HEATER_0
  979. #endif
  980. #if EXTRUDERS > 1 && HAS_TEMP_1
  981. target_temperature[1] = 0;
  982. soft_pwm[1] = 0;
  983. WRITE_HEATER_1(LOW);
  984. #endif
  985. #if EXTRUDERS > 2 && HAS_TEMP_2
  986. target_temperature[2] = 0;
  987. soft_pwm[2] = 0;
  988. WRITE_HEATER_2(LOW);
  989. #endif
  990. #if EXTRUDERS > 3 && HAS_TEMP_3
  991. target_temperature[3] = 0;
  992. soft_pwm[3] = 0;
  993. WRITE_HEATER_3(LOW);
  994. #endif
  995. #if HAS_TEMP_BED
  996. target_temperature_bed = 0;
  997. soft_pwm_bed = 0;
  998. #if HAS_HEATER_BED
  999. WRITE_HEATER_BED(LOW);
  1000. #endif
  1001. #endif
  1002. }
  1003. void max_temp_error(uint8_t e) {
  1004. disable_heater();
  1005. if(IsStopped() == false) {
  1006. SERIAL_ERROR_START;
  1007. SERIAL_ERRORLN((int)e);
  1008. SERIAL_ERRORLNPGM(MSG_MAXTEMP_EXTRUDER_OFF);
  1009. LCD_ALERTMESSAGEPGM(MSG_ERR_MAXTEMP); // translatable
  1010. }
  1011. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  1012. Stop();
  1013. #endif
  1014. }
  1015. void min_temp_error(uint8_t e) {
  1016. disable_heater();
  1017. if(IsStopped() == false) {
  1018. SERIAL_ERROR_START;
  1019. SERIAL_ERRORLN((int)e);
  1020. SERIAL_ERRORLNPGM(MSG_MINTEMP_EXTRUDER_OFF);
  1021. LCD_ALERTMESSAGEPGM(MSG_ERR_MINTEMP); // translatable
  1022. }
  1023. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  1024. Stop();
  1025. #endif
  1026. }
  1027. void bed_max_temp_error(void) {
  1028. #if HAS_HEATER_BED
  1029. WRITE_HEATER_BED(0);
  1030. #endif
  1031. if (IsStopped() == false) {
  1032. SERIAL_ERROR_START;
  1033. SERIAL_ERRORLNPGM(MSG_MAXTEMP_BED_OFF);
  1034. LCD_ALERTMESSAGEPGM(MSG_ERR_MAXTEMP_BED); // translatable
  1035. }
  1036. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  1037. Stop();
  1038. #endif
  1039. }
  1040. #ifdef HEATER_0_USES_MAX6675
  1041. #define MAX6675_HEAT_INTERVAL 250
  1042. long max6675_previous_millis = MAX6675_HEAT_INTERVAL;
  1043. int max6675_temp = 2000;
  1044. static int read_max6675() {
  1045. unsigned long ms = millis();
  1046. if (ms < max6675_previous_millis + MAX6675_HEAT_INTERVAL)
  1047. return max6675_temp;
  1048. max6675_previous_millis = ms;
  1049. max6675_temp = 0;
  1050. #ifdef PRR
  1051. PRR &= ~(1<<PRSPI);
  1052. #elif defined(PRR0)
  1053. PRR0 &= ~(1<<PRSPI);
  1054. #endif
  1055. SPCR = (1<<MSTR) | (1<<SPE) | (1<<SPR0);
  1056. // enable TT_MAX6675
  1057. WRITE(MAX6675_SS, 0);
  1058. // ensure 100ns delay - a bit extra is fine
  1059. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1060. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1061. // read MSB
  1062. SPDR = 0;
  1063. for (;(SPSR & (1<<SPIF)) == 0;);
  1064. max6675_temp = SPDR;
  1065. max6675_temp <<= 8;
  1066. // read LSB
  1067. SPDR = 0;
  1068. for (;(SPSR & (1<<SPIF)) == 0;);
  1069. max6675_temp |= SPDR;
  1070. // disable TT_MAX6675
  1071. WRITE(MAX6675_SS, 1);
  1072. if (max6675_temp & 4) {
  1073. // thermocouple open
  1074. max6675_temp = 4000;
  1075. }
  1076. else {
  1077. max6675_temp = max6675_temp >> 3;
  1078. }
  1079. return max6675_temp;
  1080. }
  1081. #endif //HEATER_0_USES_MAX6675
  1082. /**
  1083. * Stages in the ISR loop
  1084. */
  1085. enum TempState {
  1086. PrepareTemp_0,
  1087. MeasureTemp_0,
  1088. PrepareTemp_BED,
  1089. MeasureTemp_BED,
  1090. PrepareTemp_1,
  1091. MeasureTemp_1,
  1092. PrepareTemp_2,
  1093. MeasureTemp_2,
  1094. PrepareTemp_3,
  1095. MeasureTemp_3,
  1096. Prepare_FILWIDTH,
  1097. Measure_FILWIDTH,
  1098. StartupDelay // Startup, delay initial temp reading a tiny bit so the hardware can settle
  1099. };
  1100. //
  1101. // Timer 0 is shared with millies
  1102. //
  1103. ISR(TIMER0_COMPB_vect) {
  1104. //these variables are only accesible from the ISR, but static, so they don't lose their value
  1105. static unsigned char temp_count = 0;
  1106. static unsigned long raw_temp_0_value = 0;
  1107. static unsigned long raw_temp_1_value = 0;
  1108. static unsigned long raw_temp_2_value = 0;
  1109. static unsigned long raw_temp_3_value = 0;
  1110. static unsigned long raw_temp_bed_value = 0;
  1111. static TempState temp_state = StartupDelay;
  1112. static unsigned char pwm_count = (1 << SOFT_PWM_SCALE);
  1113. // Static members for each heater
  1114. #ifdef SLOW_PWM_HEATERS
  1115. static unsigned char slow_pwm_count = 0;
  1116. #define ISR_STATICS(n) \
  1117. static unsigned char soft_pwm_ ## n; \
  1118. static unsigned char state_heater_ ## n = 0; \
  1119. static unsigned char state_timer_heater_ ## n = 0
  1120. #else
  1121. #define ISR_STATICS(n) static unsigned char soft_pwm_ ## n
  1122. #endif
  1123. // Statics per heater
  1124. ISR_STATICS(0);
  1125. #if (EXTRUDERS > 1) || defined(HEATERS_PARALLEL)
  1126. ISR_STATICS(1);
  1127. #if EXTRUDERS > 2
  1128. ISR_STATICS(2);
  1129. #if EXTRUDERS > 3
  1130. ISR_STATICS(3);
  1131. #endif
  1132. #endif
  1133. #endif
  1134. #if HAS_HEATER_BED
  1135. ISR_STATICS(BED);
  1136. #endif
  1137. #if HAS_FILAMENT_SENSOR
  1138. static unsigned long raw_filwidth_value = 0;
  1139. #endif
  1140. #ifndef SLOW_PWM_HEATERS
  1141. /**
  1142. * standard PWM modulation
  1143. */
  1144. if (pwm_count == 0) {
  1145. soft_pwm_0 = soft_pwm[0];
  1146. if (soft_pwm_0 > 0) {
  1147. WRITE_HEATER_0(1);
  1148. }
  1149. else WRITE_HEATER_0P(0); // If HEATERS_PARALLEL should apply, change to WRITE_HEATER_0
  1150. #if EXTRUDERS > 1
  1151. soft_pwm_1 = soft_pwm[1];
  1152. WRITE_HEATER_1(soft_pwm_1 > 0 ? 1 : 0);
  1153. #if EXTRUDERS > 2
  1154. soft_pwm_2 = soft_pwm[2];
  1155. WRITE_HEATER_2(soft_pwm_2 > 0 ? 1 : 0);
  1156. #if EXTRUDERS > 3
  1157. soft_pwm_3 = soft_pwm[3];
  1158. WRITE_HEATER_3(soft_pwm_3 > 0 ? 1 : 0);
  1159. #endif
  1160. #endif
  1161. #endif
  1162. #if HAS_HEATER_BED
  1163. soft_pwm_BED = soft_pwm_bed;
  1164. WRITE_HEATER_BED(soft_pwm_BED > 0 ? 1 : 0);
  1165. #endif
  1166. #ifdef FAN_SOFT_PWM
  1167. soft_pwm_fan = fanSpeedSoftPwm / 2;
  1168. WRITE_FAN(soft_pwm_fan > 0 ? 1 : 0);
  1169. #endif
  1170. }
  1171. if (soft_pwm_0 < pwm_count) { WRITE_HEATER_0(0); }
  1172. #if EXTRUDERS > 1
  1173. if (soft_pwm_1 < pwm_count) WRITE_HEATER_1(0);
  1174. #if EXTRUDERS > 2
  1175. if (soft_pwm_2 < pwm_count) WRITE_HEATER_2(0);
  1176. #if EXTRUDERS > 3
  1177. if (soft_pwm_3 < pwm_count) WRITE_HEATER_3(0);
  1178. #endif
  1179. #endif
  1180. #endif
  1181. #if HAS_HEATER_BED
  1182. if (soft_pwm_BED < pwm_count) WRITE_HEATER_BED(0);
  1183. #endif
  1184. #ifdef FAN_SOFT_PWM
  1185. if (soft_pwm_fan < pwm_count) WRITE_FAN(0);
  1186. #endif
  1187. pwm_count += (1 << SOFT_PWM_SCALE);
  1188. pwm_count &= 0x7f;
  1189. #else // SLOW_PWM_HEATERS
  1190. /*
  1191. * SLOW PWM HEATERS
  1192. *
  1193. * for heaters drived by relay
  1194. */
  1195. #ifndef MIN_STATE_TIME
  1196. #define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds
  1197. #endif
  1198. // Macros for Slow PWM timer logic - HEATERS_PARALLEL applies
  1199. #define _SLOW_PWM_ROUTINE(NR, src) \
  1200. soft_pwm_ ## NR = src; \
  1201. if (soft_pwm_ ## NR > 0) { \
  1202. if (state_timer_heater_ ## NR == 0) { \
  1203. if (state_heater_ ## NR == 0) state_timer_heater_ ## NR = MIN_STATE_TIME; \
  1204. state_heater_ ## NR = 1; \
  1205. WRITE_HEATER_ ## NR(1); \
  1206. } \
  1207. } \
  1208. else { \
  1209. if (state_timer_heater_ ## NR == 0) { \
  1210. if (state_heater_ ## NR == 1) state_timer_heater_ ## NR = MIN_STATE_TIME; \
  1211. state_heater_ ## NR = 0; \
  1212. WRITE_HEATER_ ## NR(0); \
  1213. } \
  1214. }
  1215. #define SLOW_PWM_ROUTINE(n) _SLOW_PWM_ROUTINE(n, soft_pwm[n])
  1216. #define PWM_OFF_ROUTINE(NR) \
  1217. if (soft_pwm_ ## NR < slow_pwm_count) { \
  1218. if (state_timer_heater_ ## NR == 0) { \
  1219. if (state_heater_ ## NR == 1) state_timer_heater_ ## NR = MIN_STATE_TIME; \
  1220. state_heater_ ## NR = 0; \
  1221. WRITE_HEATER_ ## NR (0); \
  1222. } \
  1223. }
  1224. if (slow_pwm_count == 0) {
  1225. SLOW_PWM_ROUTINE(0); // EXTRUDER 0
  1226. #if EXTRUDERS > 1
  1227. SLOW_PWM_ROUTINE(1); // EXTRUDER 1
  1228. #if EXTRUDERS > 2
  1229. SLOW_PWM_ROUTINE(2); // EXTRUDER 2
  1230. #if EXTRUDERS > 3
  1231. SLOW_PWM_ROUTINE(3); // EXTRUDER 3
  1232. #endif
  1233. #endif
  1234. #endif
  1235. #if HAS_HEATER_BED
  1236. _SLOW_PWM_ROUTINE(BED, soft_pwm_bed); // BED
  1237. #endif
  1238. } // slow_pwm_count == 0
  1239. PWM_OFF_ROUTINE(0); // EXTRUDER 0
  1240. #if EXTRUDERS > 1
  1241. PWM_OFF_ROUTINE(1); // EXTRUDER 1
  1242. #if EXTRUDERS > 2
  1243. PWM_OFF_ROUTINE(2); // EXTRUDER 2
  1244. #if EXTRUDERS > 3
  1245. PWM_OFF_ROUTINE(3); // EXTRUDER 3
  1246. #endif
  1247. #endif
  1248. #endif
  1249. #if HAS_HEATER_BED
  1250. PWM_OFF_ROUTINE(BED); // BED
  1251. #endif
  1252. #ifdef FAN_SOFT_PWM
  1253. if (pwm_count == 0) {
  1254. soft_pwm_fan = fanSpeedSoftPwm / 2;
  1255. WRITE_FAN(soft_pwm_fan > 0 ? 1 : 0);
  1256. }
  1257. if (soft_pwm_fan < pwm_count) WRITE_FAN(0);
  1258. #endif //FAN_SOFT_PWM
  1259. pwm_count += (1 << SOFT_PWM_SCALE);
  1260. pwm_count &= 0x7f;
  1261. // increment slow_pwm_count only every 64 pwm_count circa 65.5ms
  1262. if ((pwm_count % 64) == 0) {
  1263. slow_pwm_count++;
  1264. slow_pwm_count &= 0x7f;
  1265. // EXTRUDER 0
  1266. if (state_timer_heater_0 > 0) state_timer_heater_0--;
  1267. #if EXTRUDERS > 1 // EXTRUDER 1
  1268. if (state_timer_heater_1 > 0) state_timer_heater_1--;
  1269. #if EXTRUDERS > 2 // EXTRUDER 2
  1270. if (state_timer_heater_2 > 0) state_timer_heater_2--;
  1271. #if EXTRUDERS > 3 // EXTRUDER 3
  1272. if (state_timer_heater_3 > 0) state_timer_heater_3--;
  1273. #endif
  1274. #endif
  1275. #endif
  1276. #if HAS_HEATER_BED
  1277. if (state_timer_heater_BED > 0) state_timer_heater_BED--;
  1278. #endif
  1279. } // (pwm_count % 64) == 0
  1280. #endif // SLOW_PWM_HEATERS
  1281. #define SET_ADMUX_ADCSRA(pin) ADMUX = (1 << REFS0) | (pin & 0x07); ADCSRA |= 1<<ADSC
  1282. #ifdef MUX5
  1283. #define START_ADC(pin) if (pin > 7) ADCSRB = 1 << MUX5; else ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
  1284. #else
  1285. #define START_ADC(pin) ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
  1286. #endif
  1287. switch(temp_state) {
  1288. case PrepareTemp_0:
  1289. #if HAS_TEMP_0
  1290. START_ADC(TEMP_0_PIN);
  1291. #endif
  1292. lcd_buttons_update();
  1293. temp_state = MeasureTemp_0;
  1294. break;
  1295. case MeasureTemp_0:
  1296. #if HAS_TEMP_0
  1297. raw_temp_0_value += ADC;
  1298. #endif
  1299. temp_state = PrepareTemp_BED;
  1300. break;
  1301. case PrepareTemp_BED:
  1302. #if HAS_TEMP_BED
  1303. START_ADC(TEMP_BED_PIN);
  1304. #endif
  1305. lcd_buttons_update();
  1306. temp_state = MeasureTemp_BED;
  1307. break;
  1308. case MeasureTemp_BED:
  1309. #if HAS_TEMP_BED
  1310. raw_temp_bed_value += ADC;
  1311. #endif
  1312. temp_state = PrepareTemp_1;
  1313. break;
  1314. case PrepareTemp_1:
  1315. #if HAS_TEMP_1
  1316. START_ADC(TEMP_1_PIN);
  1317. #endif
  1318. lcd_buttons_update();
  1319. temp_state = MeasureTemp_1;
  1320. break;
  1321. case MeasureTemp_1:
  1322. #if HAS_TEMP_1
  1323. raw_temp_1_value += ADC;
  1324. #endif
  1325. temp_state = PrepareTemp_2;
  1326. break;
  1327. case PrepareTemp_2:
  1328. #if HAS_TEMP_2
  1329. START_ADC(TEMP_2_PIN);
  1330. #endif
  1331. lcd_buttons_update();
  1332. temp_state = MeasureTemp_2;
  1333. break;
  1334. case MeasureTemp_2:
  1335. #if HAS_TEMP_2
  1336. raw_temp_2_value += ADC;
  1337. #endif
  1338. temp_state = PrepareTemp_3;
  1339. break;
  1340. case PrepareTemp_3:
  1341. #if HAS_TEMP_3
  1342. START_ADC(TEMP_3_PIN);
  1343. #endif
  1344. lcd_buttons_update();
  1345. temp_state = MeasureTemp_3;
  1346. break;
  1347. case MeasureTemp_3:
  1348. #if HAS_TEMP_3
  1349. raw_temp_3_value += ADC;
  1350. #endif
  1351. temp_state = Prepare_FILWIDTH;
  1352. break;
  1353. case Prepare_FILWIDTH:
  1354. #if HAS_FILAMENT_SENSOR
  1355. START_ADC(FILWIDTH_PIN);
  1356. #endif
  1357. lcd_buttons_update();
  1358. temp_state = Measure_FILWIDTH;
  1359. break;
  1360. case Measure_FILWIDTH:
  1361. #if HAS_FILAMENT_SENSOR
  1362. // raw_filwidth_value += ADC; //remove to use an IIR filter approach
  1363. if (ADC > 102) { //check that ADC is reading a voltage > 0.5 volts, otherwise don't take in the data.
  1364. raw_filwidth_value -= (raw_filwidth_value>>7); //multiply raw_filwidth_value by 127/128
  1365. raw_filwidth_value += ((unsigned long)ADC<<7); //add new ADC reading
  1366. }
  1367. #endif
  1368. temp_state = PrepareTemp_0;
  1369. temp_count++;
  1370. break;
  1371. case StartupDelay:
  1372. temp_state = PrepareTemp_0;
  1373. break;
  1374. // default:
  1375. // SERIAL_ERROR_START;
  1376. // SERIAL_ERRORLNPGM("Temp measurement error!");
  1377. // break;
  1378. } // switch(temp_state)
  1379. if (temp_count >= OVERSAMPLENR) { // 10 * 16 * 1/(16000000/64/256) = 164ms.
  1380. if (!temp_meas_ready) { //Only update the raw values if they have been read. Else we could be updating them during reading.
  1381. #ifndef HEATER_0_USES_MAX6675
  1382. current_temperature_raw[0] = raw_temp_0_value;
  1383. #endif
  1384. #if EXTRUDERS > 1
  1385. current_temperature_raw[1] = raw_temp_1_value;
  1386. #if EXTRUDERS > 2
  1387. current_temperature_raw[2] = raw_temp_2_value;
  1388. #if EXTRUDERS > 3
  1389. current_temperature_raw[3] = raw_temp_3_value;
  1390. #endif
  1391. #endif
  1392. #endif
  1393. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  1394. redundant_temperature_raw = raw_temp_1_value;
  1395. #endif
  1396. current_temperature_bed_raw = raw_temp_bed_value;
  1397. } //!temp_meas_ready
  1398. // Filament Sensor - can be read any time since IIR filtering is used
  1399. #if HAS_FILAMENT_SENSOR
  1400. current_raw_filwidth = raw_filwidth_value >> 10; // Divide to get to 0-16384 range since we used 1/128 IIR filter approach
  1401. #endif
  1402. temp_meas_ready = true;
  1403. temp_count = 0;
  1404. raw_temp_0_value = 0;
  1405. raw_temp_1_value = 0;
  1406. raw_temp_2_value = 0;
  1407. raw_temp_3_value = 0;
  1408. raw_temp_bed_value = 0;
  1409. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1410. #define MAXTEST <=
  1411. #define MINTEST >=
  1412. #else
  1413. #define MAXTEST >=
  1414. #define MINTEST <=
  1415. #endif
  1416. for (int i=0; i<EXTRUDERS; i++) {
  1417. if (current_temperature_raw[i] MAXTEST maxttemp_raw[i]) max_temp_error(i);
  1418. else if (current_temperature_raw[i] MINTEST minttemp_raw[i]) min_temp_error(i);
  1419. }
  1420. /* No bed MINTEMP error? */
  1421. #if defined(BED_MAXTEMP) && (TEMP_SENSOR_BED != 0)
  1422. if (current_temperature_bed_raw MAXTEST bed_maxttemp_raw) {
  1423. target_temperature_bed = 0;
  1424. bed_max_temp_error();
  1425. }
  1426. #endif
  1427. } // temp_count >= OVERSAMPLENR
  1428. #ifdef BABYSTEPPING
  1429. for (uint8_t axis=X_AXIS; axis<=Z_AXIS; axis++) {
  1430. int curTodo=babystepsTodo[axis]; //get rid of volatile for performance
  1431. if (curTodo > 0) {
  1432. babystep(axis,/*fwd*/true);
  1433. babystepsTodo[axis]--; //less to do next time
  1434. }
  1435. else if(curTodo < 0) {
  1436. babystep(axis,/*fwd*/false);
  1437. babystepsTodo[axis]++; //less to do next time
  1438. }
  1439. }
  1440. #endif //BABYSTEPPING
  1441. }
  1442. #ifdef PIDTEMP
  1443. // Apply the scale factors to the PID values
  1444. float scalePID_i(float i) { return i * PID_dT; }
  1445. float unscalePID_i(float i) { return i / PID_dT; }
  1446. float scalePID_d(float d) { return d / PID_dT; }
  1447. float unscalePID_d(float d) { return d * PID_dT; }
  1448. #endif //PIDTEMP