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 69KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * temperature.cpp - temperature control
  24. */
  25. #include "temperature.h"
  26. #include "../Marlin.h"
  27. #include "../lcd/ultralcd.h"
  28. #include "planner.h"
  29. #include "../core/language.h"
  30. #if ENABLED(HEATER_0_USES_MAX6675)
  31. #include "../libs/private_spi.h"
  32. #endif
  33. #if ENABLED(BABYSTEPPING)
  34. #include "stepper.h"
  35. #endif
  36. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE) || ENABLED(PINS_DEBUGGING)
  37. #include "endstops.h"
  38. #endif
  39. #include "printcounter.h"
  40. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  41. #include "../feature/filwidth.h"
  42. #endif
  43. #ifdef K1 // Defined in Configuration.h in the PID settings
  44. #define K2 (1.0-K1)
  45. #endif
  46. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  47. static void* heater_ttbl_map[2] = { (void*)HEATER_0_TEMPTABLE, (void*)HEATER_1_TEMPTABLE };
  48. static uint8_t heater_ttbllen_map[2] = { HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN };
  49. #else
  50. static void* heater_ttbl_map[HOTENDS] = ARRAY_BY_HOTENDS((void*)HEATER_0_TEMPTABLE, (void*)HEATER_1_TEMPTABLE, (void*)HEATER_2_TEMPTABLE, (void*)HEATER_3_TEMPTABLE, (void*)HEATER_4_TEMPTABLE);
  51. static uint8_t heater_ttbllen_map[HOTENDS] = ARRAY_BY_HOTENDS(HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN, HEATER_2_TEMPTABLE_LEN, HEATER_3_TEMPTABLE_LEN, HEATER_4_TEMPTABLE_LEN);
  52. #endif
  53. Temperature thermalManager;
  54. // public:
  55. float Temperature::current_temperature[HOTENDS] = { 0.0 },
  56. Temperature::current_temperature_bed = 0.0;
  57. int16_t Temperature::current_temperature_raw[HOTENDS] = { 0 },
  58. Temperature::target_temperature[HOTENDS] = { 0 },
  59. Temperature::current_temperature_bed_raw = 0;
  60. #if HAS_HEATER_BED
  61. int16_t Temperature::target_temperature_bed = 0;
  62. #endif
  63. // Initialized by settings.load()
  64. #if ENABLED(PIDTEMP)
  65. #if ENABLED(PID_PARAMS_PER_HOTEND) && HOTENDS > 1
  66. float Temperature::Kp[HOTENDS], Temperature::Ki[HOTENDS], Temperature::Kd[HOTENDS];
  67. #if ENABLED(PID_EXTRUSION_SCALING)
  68. float Temperature::Kc[HOTENDS];
  69. #endif
  70. #else
  71. float Temperature::Kp, Temperature::Ki, Temperature::Kd;
  72. #if ENABLED(PID_EXTRUSION_SCALING)
  73. float Temperature::Kc;
  74. #endif
  75. #endif
  76. #endif
  77. // Initialized by settings.load()
  78. #if ENABLED(PIDTEMPBED)
  79. float Temperature::bedKp, Temperature::bedKi, Temperature::bedKd;
  80. #endif
  81. #if ENABLED(BABYSTEPPING)
  82. volatile int Temperature::babystepsTodo[XYZ] = { 0 };
  83. #endif
  84. #if WATCH_HOTENDS
  85. uint16_t Temperature::watch_target_temp[HOTENDS] = { 0 };
  86. millis_t Temperature::watch_heater_next_ms[HOTENDS] = { 0 };
  87. #endif
  88. #if WATCH_THE_BED
  89. uint16_t Temperature::watch_target_bed_temp = 0;
  90. millis_t Temperature::watch_bed_next_ms = 0;
  91. #endif
  92. #if ENABLED(PREVENT_COLD_EXTRUSION)
  93. bool Temperature::allow_cold_extrude = false;
  94. int16_t Temperature::extrude_min_temp = EXTRUDE_MINTEMP;
  95. #endif
  96. // private:
  97. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  98. uint16_t Temperature::redundant_temperature_raw = 0;
  99. float Temperature::redundant_temperature = 0.0;
  100. #endif
  101. volatile bool Temperature::temp_meas_ready = false;
  102. #if ENABLED(PIDTEMP)
  103. float Temperature::temp_iState[HOTENDS] = { 0 },
  104. Temperature::temp_dState[HOTENDS] = { 0 },
  105. Temperature::pTerm[HOTENDS],
  106. Temperature::iTerm[HOTENDS],
  107. Temperature::dTerm[HOTENDS];
  108. #if ENABLED(PID_EXTRUSION_SCALING)
  109. float Temperature::cTerm[HOTENDS];
  110. long Temperature::last_e_position;
  111. long Temperature::lpq[LPQ_MAX_LEN];
  112. int Temperature::lpq_ptr = 0;
  113. #endif
  114. float Temperature::pid_error[HOTENDS];
  115. bool Temperature::pid_reset[HOTENDS];
  116. #endif
  117. #if ENABLED(PIDTEMPBED)
  118. float Temperature::temp_iState_bed = { 0 },
  119. Temperature::temp_dState_bed = { 0 },
  120. Temperature::pTerm_bed,
  121. Temperature::iTerm_bed,
  122. Temperature::dTerm_bed,
  123. Temperature::pid_error_bed;
  124. #else
  125. millis_t Temperature::next_bed_check_ms;
  126. #endif
  127. uint16_t Temperature::raw_temp_value[MAX_EXTRUDERS] = { 0 },
  128. Temperature::raw_temp_bed_value = 0;
  129. // Init min and max temp with extreme values to prevent false errors during startup
  130. int16_t Temperature::minttemp_raw[HOTENDS] = ARRAY_BY_HOTENDS(HEATER_0_RAW_LO_TEMP , HEATER_1_RAW_LO_TEMP , HEATER_2_RAW_LO_TEMP, HEATER_3_RAW_LO_TEMP, HEATER_4_RAW_LO_TEMP),
  131. Temperature::maxttemp_raw[HOTENDS] = ARRAY_BY_HOTENDS(HEATER_0_RAW_HI_TEMP , HEATER_1_RAW_HI_TEMP , HEATER_2_RAW_HI_TEMP, HEATER_3_RAW_HI_TEMP, HEATER_4_RAW_HI_TEMP),
  132. Temperature::minttemp[HOTENDS] = { 0 },
  133. Temperature::maxttemp[HOTENDS] = ARRAY_BY_HOTENDS1(16383);
  134. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  135. uint8_t Temperature::consecutive_low_temperature_error[HOTENDS] = { 0 };
  136. #endif
  137. #ifdef MILLISECONDS_PREHEAT_TIME
  138. millis_t Temperature::preheat_end_time[HOTENDS] = { 0 };
  139. #endif
  140. #ifdef BED_MINTEMP
  141. int16_t Temperature::bed_minttemp_raw = HEATER_BED_RAW_LO_TEMP;
  142. #endif
  143. #ifdef BED_MAXTEMP
  144. int16_t Temperature::bed_maxttemp_raw = HEATER_BED_RAW_HI_TEMP;
  145. #endif
  146. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  147. int8_t Temperature::meas_shift_index; // Index of a delayed sample in buffer
  148. #endif
  149. #if HAS_AUTO_FAN
  150. millis_t Temperature::next_auto_fan_check_ms = 0;
  151. #endif
  152. uint8_t Temperature::soft_pwm_amount[HOTENDS],
  153. Temperature::soft_pwm_amount_bed;
  154. #if ENABLED(FAN_SOFT_PWM)
  155. uint8_t Temperature::soft_pwm_amount_fan[FAN_COUNT],
  156. Temperature::soft_pwm_count_fan[FAN_COUNT];
  157. #endif
  158. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  159. uint16_t Temperature::current_raw_filwidth = 0; // Measured filament diameter - one extruder only
  160. #endif
  161. #if ENABLED(PROBING_HEATERS_OFF)
  162. bool Temperature::paused;
  163. #endif
  164. #if HEATER_IDLE_HANDLER
  165. millis_t Temperature::heater_idle_timeout_ms[HOTENDS] = { 0 };
  166. bool Temperature::heater_idle_timeout_exceeded[HOTENDS] = { false };
  167. #if HAS_TEMP_BED
  168. millis_t Temperature::bed_idle_timeout_ms = 0;
  169. bool Temperature::bed_idle_timeout_exceeded = false;
  170. #endif
  171. #endif
  172. #if ENABLED(ADC_KEYPAD)
  173. uint32_t Temperature::current_ADCKey_raw = 0;
  174. uint8_t Temperature::ADCKey_count = 0;
  175. #endif
  176. #if HAS_PID_HEATING
  177. void Temperature::PID_autotune(const float temp, const int8_t hotend, const int8_t ncycles, const bool set_result/*=false*/) {
  178. float input = 0.0;
  179. int cycles = 0;
  180. bool heating = true;
  181. millis_t next_temp_ms = millis(), t1 = next_temp_ms, t2 = next_temp_ms;
  182. long t_high = 0, t_low = 0;
  183. long bias, d;
  184. float Ku, Tu,
  185. workKp = 0, workKi = 0, workKd = 0,
  186. max = 0, min = 10000;
  187. #if WATCH_THE_BED || WATCH_HOTENDS
  188. const float watch_temp_target = temp -
  189. #if ENABLED(THERMAL_PROTECTION_BED) && ENABLED(PIDTEMPBED) && ENABLED(THERMAL_PROTECTION_HOTENDS) && ENABLED(PIDTEMP)
  190. (hotend < 0 ? (WATCH_BED_TEMP_INCREASE + TEMP_BED_HYSTERESIS + 1) : (WATCH_TEMP_INCREASE + TEMP_HYSTERESIS + 1))
  191. #elif ENABLED(THERMAL_PROTECTION_BED) && ENABLED(PIDTEMPBED)
  192. (WATCH_BED_TEMP_INCREASE + TEMP_BED_HYSTERESIS + 1)
  193. #else
  194. (WATCH_TEMP_INCREASE + TEMP_HYSTERESIS + 1)
  195. #endif
  196. ;
  197. const int8_t watch_temp_period =
  198. #if ENABLED(THERMAL_PROTECTION_BED) && ENABLED(PIDTEMPBED) && ENABLED(THERMAL_PROTECTION_HOTENDS) && ENABLED(PIDTEMP)
  199. hotend < 0 ? temp - THERMAL_PROTECTION_BED_PERIOD : THERMAL_PROTECTION_PERIOD
  200. #elif ENABLED(THERMAL_PROTECTION_BED) && ENABLED(PIDTEMPBED)
  201. THERMAL_PROTECTION_BED_PERIOD
  202. #else
  203. THERMAL_PROTECTION_PERIOD
  204. #endif
  205. ;
  206. const int8_t watch_temp_increase =
  207. #if ENABLED(THERMAL_PROTECTION_BED) && ENABLED(PIDTEMPBED) && ENABLED(THERMAL_PROTECTION_HOTENDS) && ENABLED(PIDTEMP)
  208. hotend < 0 ? WATCH_BED_TEMP_INCREASE : WATCH_TEMP_INCREASE
  209. #elif ENABLED(THERMAL_PROTECTION_BED) && ENABLED(PIDTEMPBED)
  210. WATCH_BED_TEMP_INCREASE
  211. #else
  212. WATCH_TEMP_INCREASE
  213. #endif
  214. ;
  215. millis_t temp_change_ms = next_temp_ms + watch_temp_period * 1000UL;
  216. float next_watch_temp = 0.0;
  217. bool heated = false;
  218. #endif
  219. #if HAS_AUTO_FAN
  220. next_auto_fan_check_ms = next_temp_ms + 2500UL;
  221. #endif
  222. #if ENABLED(PIDTEMP)
  223. #define _TOP_HOTEND HOTENDS - 1
  224. #else
  225. #define _TOP_HOTEND -1
  226. #endif
  227. #if ENABLED(PIDTEMPBED)
  228. #define _BOT_HOTEND -1
  229. #else
  230. #define _BOT_HOTEND 0
  231. #endif
  232. if (!WITHIN(hotend, _BOT_HOTEND, _TOP_HOTEND)) {
  233. SERIAL_ECHOLN(MSG_PID_BAD_EXTRUDER_NUM);
  234. return;
  235. }
  236. SERIAL_ECHOLN(MSG_PID_AUTOTUNE_START);
  237. disable_all_heaters(); // switch off all heaters.
  238. #if HAS_PID_FOR_BOTH
  239. if (hotend < 0)
  240. soft_pwm_amount_bed = bias = d = (MAX_BED_POWER) >> 1;
  241. else
  242. soft_pwm_amount[hotend] = bias = d = (PID_MAX) >> 1;
  243. #elif ENABLED(PIDTEMP)
  244. soft_pwm_amount[hotend] = bias = d = (PID_MAX) >> 1;
  245. #else
  246. soft_pwm_amount_bed = bias = d = (MAX_BED_POWER) >> 1;
  247. #endif
  248. wait_for_heatup = true;
  249. // PID Tuning loop
  250. while (wait_for_heatup) {
  251. const millis_t ms = millis();
  252. if (temp_meas_ready) { // temp sample ready
  253. updateTemperaturesFromRawValues();
  254. input =
  255. #if HAS_PID_FOR_BOTH
  256. hotend < 0 ? current_temperature_bed : current_temperature[hotend]
  257. #elif ENABLED(PIDTEMP)
  258. current_temperature[hotend]
  259. #else
  260. current_temperature_bed
  261. #endif
  262. ;
  263. NOLESS(max, input);
  264. NOMORE(min, input);
  265. #if HAS_AUTO_FAN
  266. if (ELAPSED(ms, next_auto_fan_check_ms)) {
  267. checkExtruderAutoFans();
  268. next_auto_fan_check_ms = ms + 2500UL;
  269. }
  270. #endif
  271. if (heating && input > temp) {
  272. if (ELAPSED(ms, t2 + 5000UL)) {
  273. heating = false;
  274. #if HAS_PID_FOR_BOTH
  275. if (hotend < 0)
  276. soft_pwm_amount_bed = (bias - d) >> 1;
  277. else
  278. soft_pwm_amount[hotend] = (bias - d) >> 1;
  279. #elif ENABLED(PIDTEMP)
  280. soft_pwm_amount[hotend] = (bias - d) >> 1;
  281. #elif ENABLED(PIDTEMPBED)
  282. soft_pwm_amount_bed = (bias - d) >> 1;
  283. #endif
  284. t1 = ms;
  285. t_high = t1 - t2;
  286. max = temp;
  287. }
  288. }
  289. if (!heating && input < temp) {
  290. if (ELAPSED(ms, t1 + 5000UL)) {
  291. heating = true;
  292. t2 = ms;
  293. t_low = t2 - t1;
  294. if (cycles > 0) {
  295. long max_pow =
  296. #if HAS_PID_FOR_BOTH
  297. hotend < 0 ? MAX_BED_POWER : PID_MAX
  298. #elif ENABLED(PIDTEMP)
  299. PID_MAX
  300. #else
  301. MAX_BED_POWER
  302. #endif
  303. ;
  304. bias += (d * (t_high - t_low)) / (t_low + t_high);
  305. bias = constrain(bias, 20, max_pow - 20);
  306. d = (bias > max_pow >> 1) ? max_pow - 1 - bias : bias;
  307. SERIAL_PROTOCOLPAIR(MSG_BIAS, bias);
  308. SERIAL_PROTOCOLPAIR(MSG_D, d);
  309. SERIAL_PROTOCOLPAIR(MSG_T_MIN, min);
  310. SERIAL_PROTOCOLPAIR(MSG_T_MAX, max);
  311. if (cycles > 2) {
  312. Ku = (4.0 * d) / (M_PI * (max - min) * 0.5); // i.e., CIRCLE_CIRC((max - min) * 0.25)
  313. Tu = ((float)(t_low + t_high) * 0.001);
  314. SERIAL_PROTOCOLPAIR(MSG_KU, Ku);
  315. SERIAL_PROTOCOLPAIR(MSG_TU, Tu);
  316. workKp = 0.6 * Ku;
  317. workKi = 2 * workKp / Tu;
  318. workKd = workKp * Tu * 0.125;
  319. SERIAL_PROTOCOLLNPGM("\n" MSG_CLASSIC_PID);
  320. SERIAL_PROTOCOLPAIR(MSG_KP, workKp);
  321. SERIAL_PROTOCOLPAIR(MSG_KI, workKi);
  322. SERIAL_PROTOCOLLNPAIR(MSG_KD, workKd);
  323. /**
  324. workKp = 0.33*Ku;
  325. workKi = workKp/Tu;
  326. workKd = workKp*Tu/3;
  327. SERIAL_PROTOCOLLNPGM(" Some overshoot");
  328. SERIAL_PROTOCOLPAIR(" Kp: ", workKp);
  329. SERIAL_PROTOCOLPAIR(" Ki: ", workKi);
  330. SERIAL_PROTOCOLPAIR(" Kd: ", workKd);
  331. workKp = 0.2*Ku;
  332. workKi = 2*workKp/Tu;
  333. workKd = workKp*Tu/3;
  334. SERIAL_PROTOCOLLNPGM(" No overshoot");
  335. SERIAL_PROTOCOLPAIR(" Kp: ", workKp);
  336. SERIAL_PROTOCOLPAIR(" Ki: ", workKi);
  337. SERIAL_PROTOCOLPAIR(" Kd: ", workKd);
  338. */
  339. }
  340. }
  341. #if HAS_PID_FOR_BOTH
  342. if (hotend < 0)
  343. soft_pwm_amount_bed = (bias + d) >> 1;
  344. else
  345. soft_pwm_amount[hotend] = (bias + d) >> 1;
  346. #elif ENABLED(PIDTEMP)
  347. soft_pwm_amount[hotend] = (bias + d) >> 1;
  348. #else
  349. soft_pwm_amount_bed = (bias + d) >> 1;
  350. #endif
  351. cycles++;
  352. min = temp;
  353. }
  354. }
  355. }
  356. #define MAX_OVERSHOOT_PID_AUTOTUNE 20
  357. if (input > temp + MAX_OVERSHOOT_PID_AUTOTUNE) {
  358. SERIAL_PROTOCOLLNPGM(MSG_PID_TEMP_TOO_HIGH);
  359. break;
  360. }
  361. // Every 2 seconds...
  362. if (ELAPSED(ms, next_temp_ms)) {
  363. #if HAS_TEMP_HOTEND || HAS_TEMP_BED
  364. print_heaterstates();
  365. SERIAL_EOL();
  366. #endif
  367. next_temp_ms = ms + 2000UL;
  368. #if WATCH_THE_BED || WATCH_HOTENDS
  369. if (!heated && input > next_watch_temp) {
  370. if (input > watch_temp_target) heated = true;
  371. next_watch_temp = input + watch_temp_increase;
  372. temp_change_ms = ms + watch_temp_period * 1000UL;
  373. }
  374. else if ((!heated && ELAPSED(ms, temp_change_ms)) || (heated && input < temp - MAX_OVERSHOOT_PID_AUTOTUNE))
  375. _temp_error(hotend, PSTR(MSG_T_THERMAL_RUNAWAY), PSTR(MSG_THERMAL_RUNAWAY));
  376. #endif
  377. } // every 2 seconds
  378. // Timeout after 20 minutes since the last undershoot/overshoot cycle
  379. if (((ms - t1) + (ms - t2)) > (20L * 60L * 1000L)) {
  380. SERIAL_PROTOCOLLNPGM(MSG_PID_TIMEOUT);
  381. break;
  382. }
  383. if (cycles > ncycles) {
  384. SERIAL_PROTOCOLLNPGM(MSG_PID_AUTOTUNE_FINISHED);
  385. #if HAS_PID_FOR_BOTH
  386. const char* estring = hotend < 0 ? "bed" : "";
  387. SERIAL_PROTOCOLPAIR("#define DEFAULT_", estring); SERIAL_PROTOCOLPAIR("Kp ", workKp); SERIAL_EOL();
  388. SERIAL_PROTOCOLPAIR("#define DEFAULT_", estring); SERIAL_PROTOCOLPAIR("Ki ", workKi); SERIAL_EOL();
  389. SERIAL_PROTOCOLPAIR("#define DEFAULT_", estring); SERIAL_PROTOCOLPAIR("Kd ", workKd); SERIAL_EOL();
  390. #elif ENABLED(PIDTEMP)
  391. SERIAL_PROTOCOLPAIR("#define DEFAULT_Kp ", workKp); SERIAL_EOL();
  392. SERIAL_PROTOCOLPAIR("#define DEFAULT_Ki ", workKi); SERIAL_EOL();
  393. SERIAL_PROTOCOLPAIR("#define DEFAULT_Kd ", workKd); SERIAL_EOL();
  394. #else
  395. SERIAL_PROTOCOLPAIR("#define DEFAULT_bedKp ", workKp); SERIAL_EOL();
  396. SERIAL_PROTOCOLPAIR("#define DEFAULT_bedKi ", workKi); SERIAL_EOL();
  397. SERIAL_PROTOCOLPAIR("#define DEFAULT_bedKd ", workKd); SERIAL_EOL();
  398. #endif
  399. #define _SET_BED_PID() do { \
  400. bedKp = workKp; \
  401. bedKi = scalePID_i(workKi); \
  402. bedKd = scalePID_d(workKd); \
  403. updatePID(); }while(0)
  404. #define _SET_EXTRUDER_PID() do { \
  405. PID_PARAM(Kp, hotend) = workKp; \
  406. PID_PARAM(Ki, hotend) = scalePID_i(workKi); \
  407. PID_PARAM(Kd, hotend) = scalePID_d(workKd); \
  408. updatePID(); }while(0)
  409. // Use the result? (As with "M303 U1")
  410. if (set_result) {
  411. #if HAS_PID_FOR_BOTH
  412. if (hotend < 0)
  413. _SET_BED_PID();
  414. else
  415. _SET_EXTRUDER_PID();
  416. #elif ENABLED(PIDTEMP)
  417. _SET_EXTRUDER_PID();
  418. #else
  419. _SET_BED_PID();
  420. #endif
  421. }
  422. return;
  423. }
  424. lcd_update();
  425. }
  426. disable_all_heaters();
  427. }
  428. #endif // HAS_PID_HEATING
  429. /**
  430. * Class and Instance Methods
  431. */
  432. Temperature::Temperature() { }
  433. void Temperature::updatePID() {
  434. #if ENABLED(PIDTEMP)
  435. #if ENABLED(PID_EXTRUSION_SCALING)
  436. last_e_position = 0;
  437. #endif
  438. #endif
  439. }
  440. int Temperature::getHeaterPower(int heater) {
  441. return heater < 0 ? soft_pwm_amount_bed : soft_pwm_amount[heater];
  442. }
  443. #if HAS_AUTO_FAN
  444. void Temperature::checkExtruderAutoFans() {
  445. static const int8_t fanPin[] PROGMEM = { E0_AUTO_FAN_PIN, E1_AUTO_FAN_PIN, E2_AUTO_FAN_PIN, E3_AUTO_FAN_PIN, E4_AUTO_FAN_PIN };
  446. static const uint8_t fanBit[] PROGMEM = {
  447. 0,
  448. AUTO_1_IS_0 ? 0 : 1,
  449. AUTO_2_IS_0 ? 0 : AUTO_2_IS_1 ? 1 : 2,
  450. AUTO_3_IS_0 ? 0 : AUTO_3_IS_1 ? 1 : AUTO_3_IS_2 ? 2 : 3,
  451. AUTO_4_IS_0 ? 0 : AUTO_4_IS_1 ? 1 : AUTO_4_IS_2 ? 2 : AUTO_4_IS_3 ? 3 : 4
  452. };
  453. uint8_t fanState = 0;
  454. HOTEND_LOOP()
  455. if (current_temperature[e] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  456. SBI(fanState, pgm_read_byte(&fanBit[e]));
  457. uint8_t fanDone = 0;
  458. for (uint8_t f = 0; f < COUNT(fanPin); f++) {
  459. int8_t pin = pgm_read_byte(&fanPin[f]);
  460. const uint8_t bit = pgm_read_byte(&fanBit[f]);
  461. if (pin >= 0 && !TEST(fanDone, bit)) {
  462. uint8_t newFanSpeed = TEST(fanState, bit) ? EXTRUDER_AUTO_FAN_SPEED : 0;
  463. // this idiom allows both digital and PWM fan outputs (see M42 handling).
  464. digitalWrite(pin, newFanSpeed);
  465. analogWrite(pin, newFanSpeed);
  466. SBI(fanDone, bit);
  467. }
  468. }
  469. }
  470. #endif // HAS_AUTO_FAN
  471. //
  472. // Temperature Error Handlers
  473. //
  474. void Temperature::_temp_error(const int8_t e, const char * const serial_msg, const char * const lcd_msg) {
  475. static bool killed = false;
  476. if (IsRunning()) {
  477. SERIAL_ERROR_START();
  478. serialprintPGM(serial_msg);
  479. SERIAL_ERRORPGM(MSG_STOPPED_HEATER);
  480. if (e >= 0) SERIAL_ERRORLN((int)e); else SERIAL_ERRORLNPGM(MSG_HEATER_BED);
  481. }
  482. #if DISABLED(BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE)
  483. if (!killed) {
  484. Running = false;
  485. killed = true;
  486. kill(lcd_msg);
  487. }
  488. else
  489. disable_all_heaters(); // paranoia
  490. #endif
  491. }
  492. void Temperature::max_temp_error(const int8_t e) {
  493. #if HAS_TEMP_BED
  494. _temp_error(e, PSTR(MSG_T_MAXTEMP), e >= 0 ? PSTR(MSG_ERR_MAXTEMP) : PSTR(MSG_ERR_MAXTEMP_BED));
  495. #else
  496. _temp_error(HOTEND_INDEX, PSTR(MSG_T_MAXTEMP), PSTR(MSG_ERR_MAXTEMP));
  497. #if HOTENDS == 1
  498. UNUSED(e);
  499. #endif
  500. #endif
  501. }
  502. void Temperature::min_temp_error(const int8_t e) {
  503. #if HAS_TEMP_BED
  504. _temp_error(e, PSTR(MSG_T_MINTEMP), e >= 0 ? PSTR(MSG_ERR_MINTEMP) : PSTR(MSG_ERR_MINTEMP_BED));
  505. #else
  506. _temp_error(HOTEND_INDEX, PSTR(MSG_T_MINTEMP), PSTR(MSG_ERR_MINTEMP));
  507. #if HOTENDS == 1
  508. UNUSED(e);
  509. #endif
  510. #endif
  511. }
  512. float Temperature::get_pid_output(const int8_t e) {
  513. #if HOTENDS == 1
  514. UNUSED(e);
  515. #define _HOTEND_TEST true
  516. #else
  517. #define _HOTEND_TEST e == active_extruder
  518. #endif
  519. float pid_output;
  520. #if ENABLED(PIDTEMP)
  521. #if DISABLED(PID_OPENLOOP)
  522. pid_error[HOTEND_INDEX] = target_temperature[HOTEND_INDEX] - current_temperature[HOTEND_INDEX];
  523. dTerm[HOTEND_INDEX] = K2 * PID_PARAM(Kd, HOTEND_INDEX) * (current_temperature[HOTEND_INDEX] - temp_dState[HOTEND_INDEX]) + K1 * dTerm[HOTEND_INDEX];
  524. temp_dState[HOTEND_INDEX] = current_temperature[HOTEND_INDEX];
  525. #if HEATER_IDLE_HANDLER
  526. if (heater_idle_timeout_exceeded[HOTEND_INDEX]) {
  527. pid_output = 0;
  528. pid_reset[HOTEND_INDEX] = true;
  529. }
  530. else
  531. #endif
  532. if (pid_error[HOTEND_INDEX] > PID_FUNCTIONAL_RANGE) {
  533. pid_output = BANG_MAX;
  534. pid_reset[HOTEND_INDEX] = true;
  535. }
  536. else if (pid_error[HOTEND_INDEX] < -(PID_FUNCTIONAL_RANGE) || target_temperature[HOTEND_INDEX] == 0
  537. #if HEATER_IDLE_HANDLER
  538. || heater_idle_timeout_exceeded[HOTEND_INDEX]
  539. #endif
  540. ) {
  541. pid_output = 0;
  542. pid_reset[HOTEND_INDEX] = true;
  543. }
  544. else {
  545. if (pid_reset[HOTEND_INDEX]) {
  546. temp_iState[HOTEND_INDEX] = 0.0;
  547. pid_reset[HOTEND_INDEX] = false;
  548. }
  549. pTerm[HOTEND_INDEX] = PID_PARAM(Kp, HOTEND_INDEX) * pid_error[HOTEND_INDEX];
  550. temp_iState[HOTEND_INDEX] += pid_error[HOTEND_INDEX];
  551. iTerm[HOTEND_INDEX] = PID_PARAM(Ki, HOTEND_INDEX) * temp_iState[HOTEND_INDEX];
  552. pid_output = pTerm[HOTEND_INDEX] + iTerm[HOTEND_INDEX] - dTerm[HOTEND_INDEX];
  553. #if ENABLED(PID_EXTRUSION_SCALING)
  554. cTerm[HOTEND_INDEX] = 0;
  555. if (_HOTEND_TEST) {
  556. long e_position = stepper.position(E_AXIS);
  557. if (e_position > last_e_position) {
  558. lpq[lpq_ptr] = e_position - last_e_position;
  559. last_e_position = e_position;
  560. }
  561. else {
  562. lpq[lpq_ptr] = 0;
  563. }
  564. if (++lpq_ptr >= lpq_len) lpq_ptr = 0;
  565. cTerm[HOTEND_INDEX] = (lpq[lpq_ptr] * planner.steps_to_mm[E_AXIS]) * PID_PARAM(Kc, HOTEND_INDEX);
  566. pid_output += cTerm[HOTEND_INDEX];
  567. }
  568. #endif // PID_EXTRUSION_SCALING
  569. if (pid_output > PID_MAX) {
  570. if (pid_error[HOTEND_INDEX] > 0) temp_iState[HOTEND_INDEX] -= pid_error[HOTEND_INDEX]; // conditional un-integration
  571. pid_output = PID_MAX;
  572. }
  573. else if (pid_output < 0) {
  574. if (pid_error[HOTEND_INDEX] < 0) temp_iState[HOTEND_INDEX] -= pid_error[HOTEND_INDEX]; // conditional un-integration
  575. pid_output = 0;
  576. }
  577. }
  578. #else
  579. pid_output = constrain(target_temperature[HOTEND_INDEX], 0, PID_MAX);
  580. #endif // PID_OPENLOOP
  581. #if ENABLED(PID_DEBUG)
  582. SERIAL_ECHO_START();
  583. SERIAL_ECHOPAIR(MSG_PID_DEBUG, HOTEND_INDEX);
  584. SERIAL_ECHOPAIR(MSG_PID_DEBUG_INPUT, current_temperature[HOTEND_INDEX]);
  585. SERIAL_ECHOPAIR(MSG_PID_DEBUG_OUTPUT, pid_output);
  586. SERIAL_ECHOPAIR(MSG_PID_DEBUG_PTERM, pTerm[HOTEND_INDEX]);
  587. SERIAL_ECHOPAIR(MSG_PID_DEBUG_ITERM, iTerm[HOTEND_INDEX]);
  588. SERIAL_ECHOPAIR(MSG_PID_DEBUG_DTERM, dTerm[HOTEND_INDEX]);
  589. #if ENABLED(PID_EXTRUSION_SCALING)
  590. SERIAL_ECHOPAIR(MSG_PID_DEBUG_CTERM, cTerm[HOTEND_INDEX]);
  591. #endif
  592. SERIAL_EOL();
  593. #endif // PID_DEBUG
  594. #else /* PID off */
  595. #if HEATER_IDLE_HANDLER
  596. if (heater_idle_timeout_exceeded[HOTEND_INDEX])
  597. pid_output = 0;
  598. else
  599. #endif
  600. pid_output = (current_temperature[HOTEND_INDEX] < target_temperature[HOTEND_INDEX]) ? PID_MAX : 0;
  601. #endif
  602. return pid_output;
  603. }
  604. #if ENABLED(PIDTEMPBED)
  605. float Temperature::get_pid_output_bed() {
  606. float pid_output;
  607. #if DISABLED(PID_OPENLOOP)
  608. pid_error_bed = target_temperature_bed - current_temperature_bed;
  609. pTerm_bed = bedKp * pid_error_bed;
  610. temp_iState_bed += pid_error_bed;
  611. iTerm_bed = bedKi * temp_iState_bed;
  612. dTerm_bed = K2 * bedKd * (current_temperature_bed - temp_dState_bed) + K1 * dTerm_bed;
  613. temp_dState_bed = current_temperature_bed;
  614. pid_output = pTerm_bed + iTerm_bed - dTerm_bed;
  615. if (pid_output > MAX_BED_POWER) {
  616. if (pid_error_bed > 0) temp_iState_bed -= pid_error_bed; // conditional un-integration
  617. pid_output = MAX_BED_POWER;
  618. }
  619. else if (pid_output < 0) {
  620. if (pid_error_bed < 0) temp_iState_bed -= pid_error_bed; // conditional un-integration
  621. pid_output = 0;
  622. }
  623. #else
  624. pid_output = constrain(target_temperature_bed, 0, MAX_BED_POWER);
  625. #endif // PID_OPENLOOP
  626. #if ENABLED(PID_BED_DEBUG)
  627. SERIAL_ECHO_START();
  628. SERIAL_ECHOPGM(" PID_BED_DEBUG ");
  629. SERIAL_ECHOPGM(": Input ");
  630. SERIAL_ECHO(current_temperature_bed);
  631. SERIAL_ECHOPGM(" Output ");
  632. SERIAL_ECHO(pid_output);
  633. SERIAL_ECHOPGM(" pTerm ");
  634. SERIAL_ECHO(pTerm_bed);
  635. SERIAL_ECHOPGM(" iTerm ");
  636. SERIAL_ECHO(iTerm_bed);
  637. SERIAL_ECHOPGM(" dTerm ");
  638. SERIAL_ECHOLN(dTerm_bed);
  639. #endif // PID_BED_DEBUG
  640. return pid_output;
  641. }
  642. #endif // PIDTEMPBED
  643. /**
  644. * Manage heating activities for extruder hot-ends and a heated bed
  645. * - Acquire updated temperature readings
  646. * - Also resets the watchdog timer
  647. * - Invoke thermal runaway protection
  648. * - Manage extruder auto-fan
  649. * - Apply filament width to the extrusion rate (may move)
  650. * - Update the heated bed PID output value
  651. */
  652. /**
  653. * The following line SOMETIMES results in the dreaded "unable to find a register to spill in class 'POINTER_REGS'"
  654. * compile error.
  655. * thermal_runaway_protection(&thermal_runaway_state_machine[e], &thermal_runaway_timer[e], current_temperature[e], target_temperature[e], e, THERMAL_PROTECTION_PERIOD, THERMAL_PROTECTION_HYSTERESIS);
  656. *
  657. * This is due to a bug in the C++ compiler used by the Arduino IDE from 1.6.10 to at least 1.8.1.
  658. *
  659. * The work around is to add the compiler flag "__attribute__((__optimize__("O2")))" to the declaration for manage_heater()
  660. */
  661. //void Temperature::manage_heater() __attribute__((__optimize__("O2")));
  662. void Temperature::manage_heater() {
  663. if (!temp_meas_ready) return;
  664. updateTemperaturesFromRawValues(); // also resets the watchdog
  665. #if ENABLED(HEATER_0_USES_MAX6675)
  666. if (current_temperature[0] > min(HEATER_0_MAXTEMP, MAX6675_TMAX - 1.0)) max_temp_error(0);
  667. if (current_temperature[0] < max(HEATER_0_MINTEMP, MAX6675_TMIN + .01)) min_temp_error(0);
  668. #endif
  669. #if WATCH_HOTENDS || WATCH_THE_BED || DISABLED(PIDTEMPBED) || HAS_AUTO_FAN || HEATER_IDLE_HANDLER
  670. millis_t ms = millis();
  671. #endif
  672. HOTEND_LOOP() {
  673. #if HEATER_IDLE_HANDLER
  674. if (!heater_idle_timeout_exceeded[e] && heater_idle_timeout_ms[e] && ELAPSED(ms, heater_idle_timeout_ms[e]))
  675. heater_idle_timeout_exceeded[e] = true;
  676. #endif
  677. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  678. // Check for thermal runaway
  679. thermal_runaway_protection(&thermal_runaway_state_machine[e], &thermal_runaway_timer[e], current_temperature[e], target_temperature[e], e, THERMAL_PROTECTION_PERIOD, THERMAL_PROTECTION_HYSTERESIS);
  680. #endif
  681. soft_pwm_amount[e] = (current_temperature[e] > minttemp[e] || is_preheating(e)) && current_temperature[e] < maxttemp[e] ? (int)get_pid_output(e) >> 1 : 0;
  682. #if WATCH_HOTENDS
  683. // Make sure temperature is increasing
  684. if (watch_heater_next_ms[e] && ELAPSED(ms, watch_heater_next_ms[e])) { // Time to check this extruder?
  685. if (degHotend(e) < watch_target_temp[e]) // Failed to increase enough?
  686. _temp_error(e, PSTR(MSG_T_HEATING_FAILED), PSTR(MSG_HEATING_FAILED_LCD));
  687. else // Start again if the target is still far off
  688. start_watching_heater(e);
  689. }
  690. #endif
  691. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  692. // Make sure measured temperatures are close together
  693. if (FABS(current_temperature[0] - redundant_temperature) > MAX_REDUNDANT_TEMP_SENSOR_DIFF)
  694. _temp_error(0, PSTR(MSG_REDUNDANCY), PSTR(MSG_ERR_REDUNDANT_TEMP));
  695. #endif
  696. } // HOTEND_LOOP
  697. #if HAS_AUTO_FAN
  698. if (ELAPSED(ms, next_auto_fan_check_ms)) { // only need to check fan state very infrequently
  699. checkExtruderAutoFans();
  700. next_auto_fan_check_ms = ms + 2500UL;
  701. }
  702. #endif
  703. // Control the extruder rate based on the width sensor
  704. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  705. if (filament_sensor) {
  706. meas_shift_index = filwidth_delay_index[0] - meas_delay_cm;
  707. if (meas_shift_index < 0) meas_shift_index += MAX_MEASUREMENT_DELAY + 1; //loop around buffer if needed
  708. meas_shift_index = constrain(meas_shift_index, 0, MAX_MEASUREMENT_DELAY);
  709. // Get the delayed info and add 100 to reconstitute to a percent of
  710. // the nominal filament diameter then square it to get an area
  711. const float vmroot = measurement_delay[meas_shift_index] * 0.01 + 1.0;
  712. planner.volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] = vmroot <= 0.1 ? 0.01 : sq(vmroot);
  713. planner.refresh_e_factor(FILAMENT_SENSOR_EXTRUDER_NUM);
  714. }
  715. #endif // FILAMENT_WIDTH_SENSOR
  716. #if WATCH_THE_BED
  717. // Make sure temperature is increasing
  718. if (watch_bed_next_ms && ELAPSED(ms, watch_bed_next_ms)) { // Time to check the bed?
  719. if (degBed() < watch_target_bed_temp) // Failed to increase enough?
  720. _temp_error(-1, PSTR(MSG_T_HEATING_FAILED), PSTR(MSG_HEATING_FAILED_LCD));
  721. else // Start again if the target is still far off
  722. start_watching_bed();
  723. }
  724. #endif // WATCH_THE_BED
  725. #if DISABLED(PIDTEMPBED)
  726. if (PENDING(ms, next_bed_check_ms)) return;
  727. next_bed_check_ms = ms + BED_CHECK_INTERVAL;
  728. #endif
  729. #if HAS_TEMP_BED
  730. #if HEATER_IDLE_HANDLER
  731. if (!bed_idle_timeout_exceeded && bed_idle_timeout_ms && ELAPSED(ms, bed_idle_timeout_ms))
  732. bed_idle_timeout_exceeded = true;
  733. #endif
  734. #if HAS_THERMALLY_PROTECTED_BED
  735. thermal_runaway_protection(&thermal_runaway_bed_state_machine, &thermal_runaway_bed_timer, current_temperature_bed, target_temperature_bed, -1, THERMAL_PROTECTION_BED_PERIOD, THERMAL_PROTECTION_BED_HYSTERESIS);
  736. #endif
  737. #if HEATER_IDLE_HANDLER
  738. if (bed_idle_timeout_exceeded)
  739. {
  740. soft_pwm_amount_bed = 0;
  741. #if DISABLED(PIDTEMPBED)
  742. WRITE_HEATER_BED(LOW);
  743. #endif
  744. }
  745. else
  746. #endif
  747. {
  748. #if ENABLED(PIDTEMPBED)
  749. soft_pwm_amount_bed = WITHIN(current_temperature_bed, BED_MINTEMP, BED_MAXTEMP) ? (int)get_pid_output_bed() >> 1 : 0;
  750. #elif ENABLED(BED_LIMIT_SWITCHING)
  751. // Check if temperature is within the correct band
  752. if (WITHIN(current_temperature_bed, BED_MINTEMP, BED_MAXTEMP)) {
  753. if (current_temperature_bed >= target_temperature_bed + BED_HYSTERESIS)
  754. soft_pwm_amount_bed = 0;
  755. else if (current_temperature_bed <= target_temperature_bed - (BED_HYSTERESIS))
  756. soft_pwm_amount_bed = MAX_BED_POWER >> 1;
  757. }
  758. else {
  759. soft_pwm_amount_bed = 0;
  760. WRITE_HEATER_BED(LOW);
  761. }
  762. #else // !PIDTEMPBED && !BED_LIMIT_SWITCHING
  763. // Check if temperature is within the correct range
  764. if (WITHIN(current_temperature_bed, BED_MINTEMP, BED_MAXTEMP)) {
  765. soft_pwm_amount_bed = current_temperature_bed < target_temperature_bed ? MAX_BED_POWER >> 1 : 0;
  766. }
  767. else {
  768. soft_pwm_amount_bed = 0;
  769. WRITE_HEATER_BED(LOW);
  770. }
  771. #endif
  772. }
  773. #endif // HAS_TEMP_BED
  774. }
  775. #define PGM_RD_W(x) (short)pgm_read_word(&x)
  776. // Derived from RepRap FiveD extruder::getTemperature()
  777. // For hot end temperature measurement.
  778. float Temperature::analog2temp(int raw, uint8_t e) {
  779. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  780. if (e > HOTENDS)
  781. #else
  782. if (e >= HOTENDS)
  783. #endif
  784. {
  785. SERIAL_ERROR_START();
  786. SERIAL_ERROR((int)e);
  787. SERIAL_ERRORLNPGM(MSG_INVALID_EXTRUDER_NUM);
  788. kill(PSTR(MSG_KILLED));
  789. return 0.0;
  790. }
  791. #if ENABLED(HEATER_0_USES_MAX6675)
  792. if (e == 0) return 0.25 * raw;
  793. #endif
  794. if (heater_ttbl_map[e] != NULL) {
  795. float celsius = 0;
  796. uint8_t i;
  797. short(*tt)[][2] = (short(*)[][2])(heater_ttbl_map[e]);
  798. for (i = 1; i < heater_ttbllen_map[e]; i++) {
  799. if (PGM_RD_W((*tt)[i][0]) > raw) {
  800. celsius = PGM_RD_W((*tt)[i - 1][1]) +
  801. (raw - PGM_RD_W((*tt)[i - 1][0])) *
  802. (float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i - 1][1])) /
  803. (float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i - 1][0]));
  804. break;
  805. }
  806. }
  807. // Overflow: Set to last value in the table
  808. if (i == heater_ttbllen_map[e]) celsius = PGM_RD_W((*tt)[i - 1][1]);
  809. return celsius;
  810. }
  811. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * (TEMP_SENSOR_AD595_GAIN)) + TEMP_SENSOR_AD595_OFFSET;
  812. }
  813. // Derived from RepRap FiveD extruder::getTemperature()
  814. // For bed temperature measurement.
  815. float Temperature::analog2tempBed(const int raw) {
  816. #if ENABLED(BED_USES_THERMISTOR)
  817. float celsius = 0;
  818. byte i;
  819. for (i = 1; i < BEDTEMPTABLE_LEN; i++) {
  820. if (PGM_RD_W(BEDTEMPTABLE[i][0]) > raw) {
  821. celsius = PGM_RD_W(BEDTEMPTABLE[i - 1][1]) +
  822. (raw - PGM_RD_W(BEDTEMPTABLE[i - 1][0])) *
  823. (float)(PGM_RD_W(BEDTEMPTABLE[i][1]) - PGM_RD_W(BEDTEMPTABLE[i - 1][1])) /
  824. (float)(PGM_RD_W(BEDTEMPTABLE[i][0]) - PGM_RD_W(BEDTEMPTABLE[i - 1][0]));
  825. break;
  826. }
  827. }
  828. // Overflow: Set to last value in the table
  829. if (i == BEDTEMPTABLE_LEN) celsius = PGM_RD_W(BEDTEMPTABLE[i - 1][1]);
  830. return celsius;
  831. #elif defined(BED_USES_AD595)
  832. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * (TEMP_SENSOR_AD595_GAIN)) + TEMP_SENSOR_AD595_OFFSET;
  833. #else
  834. UNUSED(raw);
  835. return 0;
  836. #endif
  837. }
  838. /**
  839. * Get the raw values into the actual temperatures.
  840. * The raw values are created in interrupt context,
  841. * and this function is called from normal context
  842. * as it would block the stepper routine.
  843. */
  844. void Temperature::updateTemperaturesFromRawValues() {
  845. #if ENABLED(HEATER_0_USES_MAX6675)
  846. current_temperature_raw[0] = read_max6675();
  847. #endif
  848. HOTEND_LOOP()
  849. current_temperature[e] = Temperature::analog2temp(current_temperature_raw[e], e);
  850. current_temperature_bed = Temperature::analog2tempBed(current_temperature_bed_raw);
  851. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  852. redundant_temperature = Temperature::analog2temp(redundant_temperature_raw, 1);
  853. #endif
  854. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  855. filament_width_meas = analog2widthFil();
  856. #endif
  857. #if ENABLED(USE_WATCHDOG)
  858. // Reset the watchdog after we know we have a temperature measurement.
  859. watchdog_reset();
  860. #endif
  861. CRITICAL_SECTION_START;
  862. temp_meas_ready = false;
  863. CRITICAL_SECTION_END;
  864. }
  865. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  866. // Convert raw Filament Width to millimeters
  867. float Temperature::analog2widthFil() {
  868. return current_raw_filwidth * 5.0 * (1.0 / 16383.0);
  869. //return current_raw_filwidth;
  870. }
  871. // Convert raw Filament Width to a ratio
  872. int Temperature::widthFil_to_size_ratio() {
  873. float temp = filament_width_meas;
  874. if (temp < MEASURED_LOWER_LIMIT) temp = filament_width_nominal; //assume sensor cut out
  875. else NOMORE(temp, MEASURED_UPPER_LIMIT);
  876. return filament_width_nominal / temp * 100;
  877. }
  878. #endif
  879. #if ENABLED(HEATER_0_USES_MAX6675)
  880. #ifndef MAX6675_SCK_PIN
  881. #define MAX6675_SCK_PIN SCK_PIN
  882. #endif
  883. #ifndef MAX6675_DO_PIN
  884. #define MAX6675_DO_PIN MISO_PIN
  885. #endif
  886. SPIclass<MAX6675_DO_PIN, MOSI_PIN, MAX6675_SCK_PIN> max6675_spi;
  887. #endif
  888. /**
  889. * Initialize the temperature manager
  890. * The manager is implemented by periodic calls to manage_heater()
  891. */
  892. void Temperature::init() {
  893. #if MB(RUMBA) && (TEMP_SENSOR_0 == -1 || TEMP_SENSOR_1 == -1 || TEMP_SENSOR_2 == -1 || TEMP_SENSOR_BED == -1)
  894. // Disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector
  895. MCUCR = _BV(JTD);
  896. MCUCR = _BV(JTD);
  897. #endif
  898. // Finish init of mult hotend arrays
  899. HOTEND_LOOP() maxttemp[e] = maxttemp[0];
  900. #if ENABLED(PIDTEMP) && ENABLED(PID_EXTRUSION_SCALING)
  901. last_e_position = 0;
  902. #endif
  903. #if HAS_HEATER_0
  904. SET_OUTPUT(HEATER_0_PIN);
  905. #endif
  906. #if HAS_HEATER_1
  907. SET_OUTPUT(HEATER_1_PIN);
  908. #endif
  909. #if HAS_HEATER_2
  910. SET_OUTPUT(HEATER_2_PIN);
  911. #endif
  912. #if HAS_HEATER_3
  913. SET_OUTPUT(HEATER_3_PIN);
  914. #endif
  915. #if HAS_HEATER_4
  916. SET_OUTPUT(HEATER_3_PIN);
  917. #endif
  918. #if HAS_HEATER_BED
  919. SET_OUTPUT(HEATER_BED_PIN);
  920. #endif
  921. #if HAS_FAN0
  922. SET_OUTPUT(FAN_PIN);
  923. #if ENABLED(FAST_PWM_FAN)
  924. setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  925. #endif
  926. #endif
  927. #if HAS_FAN1
  928. SET_OUTPUT(FAN1_PIN);
  929. #if ENABLED(FAST_PWM_FAN)
  930. setPwmFrequency(FAN1_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  931. #endif
  932. #endif
  933. #if HAS_FAN2
  934. SET_OUTPUT(FAN2_PIN);
  935. #if ENABLED(FAST_PWM_FAN)
  936. setPwmFrequency(FAN2_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  937. #endif
  938. #endif
  939. #if ENABLED(HEATER_0_USES_MAX6675)
  940. OUT_WRITE(SCK_PIN, LOW);
  941. OUT_WRITE(MOSI_PIN, HIGH);
  942. SET_INPUT_PULLUP(MISO_PIN);
  943. max6675_spi.init();
  944. OUT_WRITE(SS_PIN, HIGH);
  945. OUT_WRITE(MAX6675_SS, HIGH);
  946. #endif // HEATER_0_USES_MAX6675
  947. HAL_adc_init();
  948. #if HAS_TEMP_0
  949. HAL_ANALOG_SELECT(TEMP_0_PIN);
  950. #endif
  951. #if HAS_TEMP_1
  952. HAL_ANALOG_SELECT(TEMP_1_PIN);
  953. #endif
  954. #if HAS_TEMP_2
  955. HAL_ANALOG_SELECT(TEMP_2_PIN);
  956. #endif
  957. #if HAS_TEMP_3
  958. HAL_ANALOG_SELECT(TEMP_3_PIN);
  959. #endif
  960. #if HAS_TEMP_4
  961. HAL_ANALOG_SELECT(TEMP_4_PIN);
  962. #endif
  963. #if HAS_TEMP_BED
  964. HAL_ANALOG_SELECT(TEMP_BED_PIN);
  965. #endif
  966. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  967. HAL_ANALOG_SELECT(FILWIDTH_PIN);
  968. #endif
  969. // todo: HAL: fix abstraction
  970. #ifdef __AVR__
  971. // Use timer0 for temperature measurement
  972. // Interleave temperature interrupt with millies interrupt
  973. OCR0B = 128;
  974. SBI(TIMSK0, OCIE0B);
  975. #else
  976. HAL_timer_start(TEMP_TIMER_NUM, TEMP_TIMER_FREQUENCY);
  977. HAL_timer_enable_interrupt(TEMP_TIMER_NUM);
  978. #endif
  979. #if HAS_AUTO_FAN_0
  980. #if E0_AUTO_FAN_PIN == FAN1_PIN
  981. SET_OUTPUT(E0_AUTO_FAN_PIN);
  982. #if ENABLED(FAST_PWM_FAN)
  983. setPwmFrequency(E0_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  984. #endif
  985. #else
  986. SET_OUTPUT(E0_AUTO_FAN_PIN);
  987. #endif
  988. #endif
  989. #if HAS_AUTO_FAN_1 && !AUTO_1_IS_0
  990. #if E1_AUTO_FAN_PIN == FAN1_PIN
  991. SET_OUTPUT(E1_AUTO_FAN_PIN);
  992. #if ENABLED(FAST_PWM_FAN)
  993. setPwmFrequency(E1_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  994. #endif
  995. #else
  996. SET_OUTPUT(E1_AUTO_FAN_PIN);
  997. #endif
  998. #endif
  999. #if HAS_AUTO_FAN_2 && !AUTO_2_IS_0 && !AUTO_2_IS_1
  1000. #if E2_AUTO_FAN_PIN == FAN1_PIN
  1001. SET_OUTPUT(E2_AUTO_FAN_PIN);
  1002. #if ENABLED(FAST_PWM_FAN)
  1003. setPwmFrequency(E2_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  1004. #endif
  1005. #else
  1006. SET_OUTPUT(E2_AUTO_FAN_PIN);
  1007. #endif
  1008. #endif
  1009. #if HAS_AUTO_FAN_3 && !AUTO_3_IS_0 && !AUTO_3_IS_1 && !AUTO_3_IS_2
  1010. #if E3_AUTO_FAN_PIN == FAN1_PIN
  1011. SET_OUTPUT(E3_AUTO_FAN_PIN);
  1012. #if ENABLED(FAST_PWM_FAN)
  1013. setPwmFrequency(E3_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  1014. #endif
  1015. #else
  1016. SET_OUTPUT(E3_AUTO_FAN_PIN);
  1017. #endif
  1018. #endif
  1019. #if HAS_AUTO_FAN_4 && !AUTO_4_IS_0 && !AUTO_4_IS_1 && !AUTO_4_IS_2 && !AUTO_4_IS_3
  1020. #if E4_AUTO_FAN_PIN == FAN1_PIN
  1021. SET_OUTPUT(E4_AUTO_FAN_PIN);
  1022. #if ENABLED(FAST_PWM_FAN)
  1023. setPwmFrequency(E4_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  1024. #endif
  1025. #else
  1026. SET_OUTPUT(E4_AUTO_FAN_PIN);
  1027. #endif
  1028. #endif
  1029. // Wait for temperature measurement to settle
  1030. delay(250);
  1031. #define TEMP_MIN_ROUTINE(NR) \
  1032. minttemp[NR] = HEATER_ ##NR## _MINTEMP; \
  1033. while (analog2temp(minttemp_raw[NR], NR) < HEATER_ ##NR## _MINTEMP) { \
  1034. if (HEATER_ ##NR## _RAW_LO_TEMP < HEATER_ ##NR## _RAW_HI_TEMP) \
  1035. minttemp_raw[NR] += OVERSAMPLENR; \
  1036. else \
  1037. minttemp_raw[NR] -= OVERSAMPLENR; \
  1038. }
  1039. #define TEMP_MAX_ROUTINE(NR) \
  1040. maxttemp[NR] = HEATER_ ##NR## _MAXTEMP; \
  1041. while (analog2temp(maxttemp_raw[NR], NR) > HEATER_ ##NR## _MAXTEMP) { \
  1042. if (HEATER_ ##NR## _RAW_LO_TEMP < HEATER_ ##NR## _RAW_HI_TEMP) \
  1043. maxttemp_raw[NR] -= OVERSAMPLENR; \
  1044. else \
  1045. maxttemp_raw[NR] += OVERSAMPLENR; \
  1046. }
  1047. #ifdef HEATER_0_MINTEMP
  1048. TEMP_MIN_ROUTINE(0);
  1049. #endif
  1050. #ifdef HEATER_0_MAXTEMP
  1051. TEMP_MAX_ROUTINE(0);
  1052. #endif
  1053. #if HOTENDS > 1
  1054. #ifdef HEATER_1_MINTEMP
  1055. TEMP_MIN_ROUTINE(1);
  1056. #endif
  1057. #ifdef HEATER_1_MAXTEMP
  1058. TEMP_MAX_ROUTINE(1);
  1059. #endif
  1060. #if HOTENDS > 2
  1061. #ifdef HEATER_2_MINTEMP
  1062. TEMP_MIN_ROUTINE(2);
  1063. #endif
  1064. #ifdef HEATER_2_MAXTEMP
  1065. TEMP_MAX_ROUTINE(2);
  1066. #endif
  1067. #if HOTENDS > 3
  1068. #ifdef HEATER_3_MINTEMP
  1069. TEMP_MIN_ROUTINE(3);
  1070. #endif
  1071. #ifdef HEATER_3_MAXTEMP
  1072. TEMP_MAX_ROUTINE(3);
  1073. #endif
  1074. #if HOTENDS > 4
  1075. #ifdef HEATER_4_MINTEMP
  1076. TEMP_MIN_ROUTINE(4);
  1077. #endif
  1078. #ifdef HEATER_4_MAXTEMP
  1079. TEMP_MAX_ROUTINE(4);
  1080. #endif
  1081. #endif // HOTENDS > 4
  1082. #endif // HOTENDS > 3
  1083. #endif // HOTENDS > 2
  1084. #endif // HOTENDS > 1
  1085. #ifdef BED_MINTEMP
  1086. while (analog2tempBed(bed_minttemp_raw) < BED_MINTEMP) {
  1087. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  1088. bed_minttemp_raw += OVERSAMPLENR;
  1089. #else
  1090. bed_minttemp_raw -= OVERSAMPLENR;
  1091. #endif
  1092. }
  1093. #endif // BED_MINTEMP
  1094. #ifdef BED_MAXTEMP
  1095. while (analog2tempBed(bed_maxttemp_raw) > BED_MAXTEMP) {
  1096. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  1097. bed_maxttemp_raw -= OVERSAMPLENR;
  1098. #else
  1099. bed_maxttemp_raw += OVERSAMPLENR;
  1100. #endif
  1101. }
  1102. #endif // BED_MAXTEMP
  1103. #if ENABLED(PROBING_HEATERS_OFF)
  1104. paused = false;
  1105. #endif
  1106. }
  1107. #if ENABLED(FAST_PWM_FAN)
  1108. void Temperature::setPwmFrequency(const uint8_t pin, int val) {
  1109. val &= 0x07;
  1110. switch (digitalPinToTimer(pin)) {
  1111. #ifdef TCCR0A
  1112. #if !AVR_AT90USB1286_FAMILY
  1113. case TIMER0A:
  1114. #endif
  1115. case TIMER0B:
  1116. //_SET_CS(0, val);
  1117. break;
  1118. #endif
  1119. #ifdef TCCR1A
  1120. case TIMER1A:
  1121. case TIMER1B:
  1122. //_SET_CS(1, val);
  1123. break;
  1124. #endif
  1125. #ifdef TCCR2
  1126. case TIMER2:
  1127. case TIMER2:
  1128. _SET_CS(2, val);
  1129. break;
  1130. #endif
  1131. #ifdef TCCR2A
  1132. case TIMER2A:
  1133. case TIMER2B:
  1134. _SET_CS(2, val);
  1135. break;
  1136. #endif
  1137. #ifdef TCCR3A
  1138. case TIMER3A:
  1139. case TIMER3B:
  1140. case TIMER3C:
  1141. _SET_CS(3, val);
  1142. break;
  1143. #endif
  1144. #ifdef TCCR4A
  1145. case TIMER4A:
  1146. case TIMER4B:
  1147. case TIMER4C:
  1148. _SET_CS(4, val);
  1149. break;
  1150. #endif
  1151. #ifdef TCCR5A
  1152. case TIMER5A:
  1153. case TIMER5B:
  1154. case TIMER5C:
  1155. _SET_CS(5, val);
  1156. break;
  1157. #endif
  1158. }
  1159. }
  1160. #endif // FAST_PWM_FAN
  1161. #if WATCH_HOTENDS
  1162. /**
  1163. * Start Heating Sanity Check for hotends that are below
  1164. * their target temperature by a configurable margin.
  1165. * This is called when the temperature is set. (M104, M109)
  1166. */
  1167. void Temperature::start_watching_heater(uint8_t e) {
  1168. #if HOTENDS == 1
  1169. UNUSED(e);
  1170. #endif
  1171. if (degHotend(HOTEND_INDEX) < degTargetHotend(HOTEND_INDEX) - (WATCH_TEMP_INCREASE + TEMP_HYSTERESIS + 1)) {
  1172. watch_target_temp[HOTEND_INDEX] = degHotend(HOTEND_INDEX) + WATCH_TEMP_INCREASE;
  1173. watch_heater_next_ms[HOTEND_INDEX] = millis() + (WATCH_TEMP_PERIOD) * 1000UL;
  1174. }
  1175. else
  1176. watch_heater_next_ms[HOTEND_INDEX] = 0;
  1177. }
  1178. #endif
  1179. #if WATCH_THE_BED
  1180. /**
  1181. * Start Heating Sanity Check for hotends that are below
  1182. * their target temperature by a configurable margin.
  1183. * This is called when the temperature is set. (M140, M190)
  1184. */
  1185. void Temperature::start_watching_bed() {
  1186. if (degBed() < degTargetBed() - (WATCH_BED_TEMP_INCREASE + TEMP_BED_HYSTERESIS + 1)) {
  1187. watch_target_bed_temp = degBed() + WATCH_BED_TEMP_INCREASE;
  1188. watch_bed_next_ms = millis() + (WATCH_BED_TEMP_PERIOD) * 1000UL;
  1189. }
  1190. else
  1191. watch_bed_next_ms = 0;
  1192. }
  1193. #endif
  1194. #if ENABLED(THERMAL_PROTECTION_HOTENDS) || HAS_THERMALLY_PROTECTED_BED
  1195. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  1196. Temperature::TRState Temperature::thermal_runaway_state_machine[HOTENDS] = { TRInactive };
  1197. millis_t Temperature::thermal_runaway_timer[HOTENDS] = { 0 };
  1198. #endif
  1199. #if HAS_THERMALLY_PROTECTED_BED
  1200. Temperature::TRState Temperature::thermal_runaway_bed_state_machine = TRInactive;
  1201. millis_t Temperature::thermal_runaway_bed_timer;
  1202. #endif
  1203. void Temperature::thermal_runaway_protection(Temperature::TRState* state, millis_t* timer, float current, float target, int heater_id, int period_seconds, int hysteresis_degc) {
  1204. static float tr_target_temperature[HOTENDS + 1] = { 0.0 };
  1205. /**
  1206. SERIAL_ECHO_START();
  1207. SERIAL_ECHOPGM("Thermal Thermal Runaway Running. Heater ID: ");
  1208. if (heater_id < 0) SERIAL_ECHOPGM("bed"); else SERIAL_ECHO(heater_id);
  1209. SERIAL_ECHOPAIR(" ; State:", *state);
  1210. SERIAL_ECHOPAIR(" ; Timer:", *timer);
  1211. SERIAL_ECHOPAIR(" ; Temperature:", current);
  1212. SERIAL_ECHOPAIR(" ; Target Temp:", target);
  1213. if (heater_id >= 0)
  1214. SERIAL_ECHOPAIR(" ; Idle Timeout:", heater_idle_timeout_exceeded[heater_id]);
  1215. else
  1216. SERIAL_ECHOPAIR(" ; Idle Timeout:", bed_idle_timeout_exceeded);
  1217. SERIAL_EOL();
  1218. */
  1219. const int heater_index = heater_id >= 0 ? heater_id : HOTENDS;
  1220. #if HEATER_IDLE_HANDLER
  1221. // If the heater idle timeout expires, restart
  1222. if (heater_id >= 0 && heater_idle_timeout_exceeded[heater_id]) {
  1223. *state = TRInactive;
  1224. tr_target_temperature[heater_index] = 0;
  1225. }
  1226. #if HAS_TEMP_BED
  1227. else if (heater_id < 0 && bed_idle_timeout_exceeded) {
  1228. *state = TRInactive;
  1229. tr_target_temperature[heater_index] = 0;
  1230. }
  1231. #endif
  1232. else
  1233. #endif
  1234. // If the target temperature changes, restart
  1235. if (tr_target_temperature[heater_index] != target) {
  1236. tr_target_temperature[heater_index] = target;
  1237. *state = target > 0 ? TRFirstHeating : TRInactive;
  1238. }
  1239. switch (*state) {
  1240. // Inactive state waits for a target temperature to be set
  1241. case TRInactive: break;
  1242. // When first heating, wait for the temperature to be reached then go to Stable state
  1243. case TRFirstHeating:
  1244. if (current < tr_target_temperature[heater_index]) break;
  1245. *state = TRStable;
  1246. // While the temperature is stable watch for a bad temperature
  1247. case TRStable:
  1248. if (current >= tr_target_temperature[heater_index] - hysteresis_degc) {
  1249. *timer = millis() + period_seconds * 1000UL;
  1250. break;
  1251. }
  1252. else if (PENDING(millis(), *timer)) break;
  1253. *state = TRRunaway;
  1254. case TRRunaway:
  1255. _temp_error(heater_id, PSTR(MSG_T_THERMAL_RUNAWAY), PSTR(MSG_THERMAL_RUNAWAY));
  1256. }
  1257. }
  1258. #endif // THERMAL_PROTECTION_HOTENDS || THERMAL_PROTECTION_BED
  1259. void Temperature::disable_all_heaters() {
  1260. #if ENABLED(AUTOTEMP)
  1261. planner.autotemp_enabled = false;
  1262. #endif
  1263. HOTEND_LOOP() setTargetHotend(0, e);
  1264. setTargetBed(0);
  1265. // Unpause and reset everything
  1266. #if ENABLED(PROBING_HEATERS_OFF)
  1267. pause(false);
  1268. #endif
  1269. // If all heaters go down then for sure our print job has stopped
  1270. print_job_timer.stop();
  1271. #define DISABLE_HEATER(NR) { \
  1272. setTargetHotend(0, NR); \
  1273. soft_pwm_amount[NR] = 0; \
  1274. WRITE_HEATER_ ##NR (LOW); \
  1275. }
  1276. #if HAS_TEMP_HOTEND
  1277. DISABLE_HEATER(0);
  1278. #if HOTENDS > 1
  1279. DISABLE_HEATER(1);
  1280. #if HOTENDS > 2
  1281. DISABLE_HEATER(2);
  1282. #if HOTENDS > 3
  1283. DISABLE_HEATER(3);
  1284. #if HOTENDS > 4
  1285. DISABLE_HEATER(4);
  1286. #endif // HOTENDS > 4
  1287. #endif // HOTENDS > 3
  1288. #endif // HOTENDS > 2
  1289. #endif // HOTENDS > 1
  1290. #endif
  1291. #if HAS_TEMP_BED
  1292. target_temperature_bed = 0;
  1293. soft_pwm_amount_bed = 0;
  1294. #if HAS_HEATER_BED
  1295. WRITE_HEATER_BED(LOW);
  1296. #endif
  1297. #endif
  1298. }
  1299. #if ENABLED(PROBING_HEATERS_OFF)
  1300. void Temperature::pause(const bool p) {
  1301. if (p != paused) {
  1302. paused = p;
  1303. if (p) {
  1304. HOTEND_LOOP() start_heater_idle_timer(e, 0); // timeout immediately
  1305. #if HAS_TEMP_BED
  1306. start_bed_idle_timer(0); // timeout immediately
  1307. #endif
  1308. }
  1309. else {
  1310. HOTEND_LOOP() reset_heater_idle_timer(e);
  1311. #if HAS_TEMP_BED
  1312. reset_bed_idle_timer();
  1313. #endif
  1314. }
  1315. }
  1316. }
  1317. #endif // PROBING_HEATERS_OFF
  1318. #if ENABLED(HEATER_0_USES_MAX6675)
  1319. #define MAX6675_HEAT_INTERVAL 250u
  1320. #if ENABLED(MAX6675_IS_MAX31855)
  1321. uint32_t max6675_temp = 2000;
  1322. #define MAX6675_ERROR_MASK 7
  1323. #define MAX6675_DISCARD_BITS 18
  1324. #define MAX6675_SPEED_BITS 3 // (_BV(SPR1)) // clock ÷ 64
  1325. #else
  1326. uint16_t max6675_temp = 2000;
  1327. #define MAX6675_ERROR_MASK 4
  1328. #define MAX6675_DISCARD_BITS 3
  1329. #define MAX6675_SPEED_BITS 2 // (_BV(SPR0)) // clock ÷ 16
  1330. #endif
  1331. int Temperature::read_max6675() {
  1332. static millis_t next_max6675_ms = 0;
  1333. millis_t ms = millis();
  1334. if (PENDING(ms, next_max6675_ms)) return (int)max6675_temp;
  1335. next_max6675_ms = ms + MAX6675_HEAT_INTERVAL;
  1336. spiBegin();
  1337. spiInit(MAX6675_SPEED_BITS);
  1338. WRITE(MAX6675_SS, 0); // enable TT_MAX6675
  1339. // ensure 100ns delay - a bit extra is fine
  1340. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1341. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1342. // Read a big-endian temperature value
  1343. max6675_temp = 0;
  1344. for (uint8_t i = sizeof(max6675_temp); i--;) {
  1345. max6675_temp |= spiRec();
  1346. if (i > 0) max6675_temp <<= 8; // shift left if not the last byte
  1347. }
  1348. WRITE(MAX6675_SS, 1); // disable TT_MAX6675
  1349. if (max6675_temp & MAX6675_ERROR_MASK) {
  1350. SERIAL_ERROR_START();
  1351. SERIAL_ERRORPGM("Temp measurement error! ");
  1352. #if MAX6675_ERROR_MASK == 7
  1353. SERIAL_ERRORPGM("MAX31855 ");
  1354. if (max6675_temp & 1)
  1355. SERIAL_ERRORLNPGM("Open Circuit");
  1356. else if (max6675_temp & 2)
  1357. SERIAL_ERRORLNPGM("Short to GND");
  1358. else if (max6675_temp & 4)
  1359. SERIAL_ERRORLNPGM("Short to VCC");
  1360. #else
  1361. SERIAL_ERRORLNPGM("MAX6675");
  1362. #endif
  1363. max6675_temp = MAX6675_TMAX * 4; // thermocouple open
  1364. }
  1365. else
  1366. max6675_temp >>= MAX6675_DISCARD_BITS;
  1367. #if ENABLED(MAX6675_IS_MAX31855)
  1368. // Support negative temperature
  1369. if (max6675_temp & 0x00002000) max6675_temp |= 0xFFFFC000;
  1370. #endif
  1371. return (int)max6675_temp;
  1372. }
  1373. #endif // HEATER_0_USES_MAX6675
  1374. /**
  1375. * Get raw temperatures
  1376. */
  1377. void Temperature::set_current_temp_raw() {
  1378. #if HAS_TEMP_0 && DISABLED(HEATER_0_USES_MAX6675)
  1379. current_temperature_raw[0] = raw_temp_value[0];
  1380. #endif
  1381. #if HAS_TEMP_1
  1382. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  1383. redundant_temperature_raw = raw_temp_value[1];
  1384. #else
  1385. current_temperature_raw[1] = raw_temp_value[1];
  1386. #endif
  1387. #if HAS_TEMP_2
  1388. current_temperature_raw[2] = raw_temp_value[2];
  1389. #if HAS_TEMP_3
  1390. current_temperature_raw[3] = raw_temp_value[3];
  1391. #if HAS_TEMP_4
  1392. current_temperature_raw[4] = raw_temp_value[4];
  1393. #endif
  1394. #endif
  1395. #endif
  1396. #endif
  1397. current_temperature_bed_raw = raw_temp_bed_value;
  1398. temp_meas_ready = true;
  1399. }
  1400. /**
  1401. * Timer 0 is shared with millies so don't change the prescaler.
  1402. *
  1403. * This ISR uses the compare method so it runs at the base
  1404. * frequency (16 MHz / 64 / 256 = 976.5625 Hz), but at the TCNT0 set
  1405. * in OCR0B above (128 or halfway between OVFs).
  1406. *
  1407. * - Manage PWM to all the heaters and fan
  1408. * - Prepare or Measure one of the raw ADC sensor values
  1409. * - Check new temperature values for MIN/MAX errors (kill on error)
  1410. * - Step the babysteps value for each axis towards 0
  1411. * - For PINS_DEBUGGING, monitor and report endstop pins
  1412. * - For ENDSTOP_INTERRUPTS_FEATURE check endstops if flagged
  1413. */
  1414. HAL_TEMP_TIMER_ISR {
  1415. HAL_timer_isr_prologue(TEMP_TIMER_NUM);
  1416. Temperature::isr();
  1417. }
  1418. volatile bool Temperature::in_temp_isr = false;
  1419. void Temperature::isr() {
  1420. // The stepper ISR can interrupt this ISR. When it does it re-enables this ISR
  1421. // at the end of its run, potentially causing re-entry. This flag prevents it.
  1422. if (in_temp_isr) return;
  1423. in_temp_isr = true;
  1424. // Allow UART and stepper ISRs
  1425. DISABLE_TEMPERATURE_INTERRUPT(); //Disable Temperature ISR
  1426. #ifndef CPU_32_BIT
  1427. sei();
  1428. #endif
  1429. static int8_t temp_count = -1;
  1430. static ADCSensorState adc_sensor_state = StartupDelay;
  1431. static uint8_t pwm_count = _BV(SOFT_PWM_SCALE);
  1432. // avoid multiple loads of pwm_count
  1433. uint8_t pwm_count_tmp = pwm_count;
  1434. #if ENABLED(ADC_KEYPAD)
  1435. static unsigned int raw_ADCKey_value = 0;
  1436. #endif
  1437. // Static members for each heater
  1438. #if ENABLED(SLOW_PWM_HEATERS)
  1439. static uint8_t slow_pwm_count = 0;
  1440. #define ISR_STATICS(n) \
  1441. static uint8_t soft_pwm_count_ ## n, \
  1442. state_heater_ ## n = 0, \
  1443. state_timer_heater_ ## n = 0
  1444. #else
  1445. #define ISR_STATICS(n) static uint8_t soft_pwm_count_ ## n = 0
  1446. #endif
  1447. // Statics per heater
  1448. ISR_STATICS(0);
  1449. #if HOTENDS > 1
  1450. ISR_STATICS(1);
  1451. #if HOTENDS > 2
  1452. ISR_STATICS(2);
  1453. #if HOTENDS > 3
  1454. ISR_STATICS(3);
  1455. #if HOTENDS > 4
  1456. ISR_STATICS(4);
  1457. #endif // HOTENDS > 4
  1458. #endif // HOTENDS > 3
  1459. #endif // HOTENDS > 2
  1460. #endif // HOTENDS > 1
  1461. #if HAS_HEATER_BED
  1462. ISR_STATICS(BED);
  1463. #endif
  1464. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1465. static unsigned long raw_filwidth_value = 0;
  1466. #endif
  1467. #if DISABLED(SLOW_PWM_HEATERS)
  1468. constexpr uint8_t pwm_mask =
  1469. #if ENABLED(SOFT_PWM_DITHER)
  1470. _BV(SOFT_PWM_SCALE) - 1
  1471. #else
  1472. 0
  1473. #endif
  1474. ;
  1475. /**
  1476. * Standard PWM modulation
  1477. */
  1478. if (pwm_count_tmp >= 127) {
  1479. pwm_count_tmp -= 127;
  1480. soft_pwm_count_0 = (soft_pwm_count_0 & pwm_mask) + soft_pwm_amount[0];
  1481. WRITE_HEATER_0(soft_pwm_count_0 > pwm_mask ? HIGH : LOW);
  1482. #if HOTENDS > 1
  1483. soft_pwm_count_1 = (soft_pwm_count_1 & pwm_mask) + soft_pwm_amount[1];
  1484. WRITE_HEATER_1(soft_pwm_count_1 > pwm_mask ? HIGH : LOW);
  1485. #if HOTENDS > 2
  1486. soft_pwm_count_2 = (soft_pwm_count_2 & pwm_mask) + soft_pwm_amount[2];
  1487. WRITE_HEATER_2(soft_pwm_count_2 > pwm_mask ? HIGH : LOW);
  1488. #if HOTENDS > 3
  1489. soft_pwm_count_3 = (soft_pwm_count_3 & pwm_mask) + soft_pwm_amount[3];
  1490. WRITE_HEATER_3(soft_pwm_count_3 > pwm_mask ? HIGH : LOW);
  1491. #if HOTENDS > 4
  1492. soft_pwm_count_4 = (soft_pwm_count_4 & pwm_mask) + soft_pwm_amount[4];
  1493. WRITE_HEATER_4(soft_pwm_count_4 > pwm_mask ? HIGH : LOW);
  1494. #endif // HOTENDS > 4
  1495. #endif // HOTENDS > 3
  1496. #endif // HOTENDS > 2
  1497. #endif // HOTENDS > 1
  1498. #if HAS_HEATER_BED
  1499. soft_pwm_count_BED = (soft_pwm_count_BED & pwm_mask) + soft_pwm_amount_bed;
  1500. WRITE_HEATER_BED(soft_pwm_count_BED > pwm_mask ? HIGH : LOW);
  1501. #endif
  1502. #if ENABLED(FAN_SOFT_PWM)
  1503. #if HAS_FAN0
  1504. soft_pwm_count_fan[0] = (soft_pwm_count_fan[0] & pwm_mask) + soft_pwm_amount_fan[0] >> 1;
  1505. WRITE_FAN(soft_pwm_count_fan[0] > pwm_mask ? HIGH : LOW);
  1506. #endif
  1507. #if HAS_FAN1
  1508. soft_pwm_count_fan[1] = (soft_pwm_count_fan[1] & pwm_mask) + soft_pwm_amount_fan[1] >> 1;
  1509. WRITE_FAN1(soft_pwm_count_fan[1] > pwm_mask ? HIGH : LOW);
  1510. #endif
  1511. #if HAS_FAN2
  1512. soft_pwm_count_fan[2] = (soft_pwm_count_fan[2] & pwm_mask) + soft_pwm_amount_fan[2] >> 1;
  1513. WRITE_FAN2(soft_pwm_count_fan[2] > pwm_mask ? HIGH : LOW);
  1514. #endif
  1515. #endif
  1516. }
  1517. else {
  1518. if (soft_pwm_count_0 <= pwm_count_tmp) WRITE_HEATER_0(LOW);
  1519. #if HOTENDS > 1
  1520. if (soft_pwm_count_1 <= pwm_count_tmp) WRITE_HEATER_1(LOW);
  1521. #if HOTENDS > 2
  1522. if (soft_pwm_count_2 <= pwm_count_tmp) WRITE_HEATER_2(LOW);
  1523. #if HOTENDS > 3
  1524. if (soft_pwm_count_3 <= pwm_count_tmp) WRITE_HEATER_3(LOW);
  1525. #if HOTENDS > 4
  1526. if (soft_pwm_count_4 <= pwm_count_tmp) WRITE_HEATER_4(LOW);
  1527. #endif // HOTENDS > 4
  1528. #endif // HOTENDS > 3
  1529. #endif // HOTENDS > 2
  1530. #endif // HOTENDS > 1
  1531. #if HAS_HEATER_BED
  1532. if (soft_pwm_count_BED <= pwm_count_tmp) WRITE_HEATER_BED(LOW);
  1533. #endif
  1534. #if ENABLED(FAN_SOFT_PWM)
  1535. #if HAS_FAN0
  1536. if (soft_pwm_count_fan[0] <= pwm_count_tmp) WRITE_FAN(LOW);
  1537. #endif
  1538. #if HAS_FAN1
  1539. if (soft_pwm_count_fan[1] <= pwm_count_tmp) WRITE_FAN1(LOW);
  1540. #endif
  1541. #if HAS_FAN2
  1542. if (soft_pwm_count_fan[2] <= pwm_count_tmp) WRITE_FAN2(LOW);
  1543. #endif
  1544. #endif
  1545. }
  1546. // SOFT_PWM_SCALE to frequency:
  1547. //
  1548. // 0: 16000000/64/256/128 = 7.6294 Hz
  1549. // 1: / 64 = 15.2588 Hz
  1550. // 2: / 32 = 30.5176 Hz
  1551. // 3: / 16 = 61.0352 Hz
  1552. // 4: / 8 = 122.0703 Hz
  1553. // 5: / 4 = 244.1406 Hz
  1554. pwm_count = pwm_count_tmp + _BV(SOFT_PWM_SCALE);
  1555. #else // SLOW_PWM_HEATERS
  1556. /**
  1557. * SLOW PWM HEATERS
  1558. *
  1559. * For relay-driven heaters
  1560. */
  1561. #ifndef MIN_STATE_TIME
  1562. #define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds
  1563. #endif
  1564. // Macros for Slow PWM timer logic
  1565. #define _SLOW_PWM_ROUTINE(NR, src) \
  1566. soft_pwm_count_ ##NR = src; \
  1567. if (soft_pwm_count_ ##NR > 0) { \
  1568. if (state_timer_heater_ ##NR == 0) { \
  1569. if (state_heater_ ##NR == 0) state_timer_heater_ ##NR = MIN_STATE_TIME; \
  1570. state_heater_ ##NR = 1; \
  1571. WRITE_HEATER_ ##NR(1); \
  1572. } \
  1573. } \
  1574. else { \
  1575. if (state_timer_heater_ ##NR == 0) { \
  1576. if (state_heater_ ##NR == 1) state_timer_heater_ ##NR = MIN_STATE_TIME; \
  1577. state_heater_ ##NR = 0; \
  1578. WRITE_HEATER_ ##NR(0); \
  1579. } \
  1580. }
  1581. #define SLOW_PWM_ROUTINE(n) _SLOW_PWM_ROUTINE(n, soft_pwm_amount[n])
  1582. #define PWM_OFF_ROUTINE(NR) \
  1583. if (soft_pwm_count_ ##NR < slow_pwm_count) { \
  1584. if (state_timer_heater_ ##NR == 0) { \
  1585. if (state_heater_ ##NR == 1) state_timer_heater_ ##NR = MIN_STATE_TIME; \
  1586. state_heater_ ##NR = 0; \
  1587. WRITE_HEATER_ ##NR (0); \
  1588. } \
  1589. }
  1590. if (slow_pwm_count == 0) {
  1591. SLOW_PWM_ROUTINE(0);
  1592. #if HOTENDS > 1
  1593. SLOW_PWM_ROUTINE(1);
  1594. #if HOTENDS > 2
  1595. SLOW_PWM_ROUTINE(2);
  1596. #if HOTENDS > 3
  1597. SLOW_PWM_ROUTINE(3);
  1598. #if HOTENDS > 4
  1599. SLOW_PWM_ROUTINE(4);
  1600. #endif // HOTENDS > 4
  1601. #endif // HOTENDS > 3
  1602. #endif // HOTENDS > 2
  1603. #endif // HOTENDS > 1
  1604. #if HAS_HEATER_BED
  1605. _SLOW_PWM_ROUTINE(BED, soft_pwm_amount_bed); // BED
  1606. #endif
  1607. } // slow_pwm_count == 0
  1608. PWM_OFF_ROUTINE(0);
  1609. #if HOTENDS > 1
  1610. PWM_OFF_ROUTINE(1);
  1611. #if HOTENDS > 2
  1612. PWM_OFF_ROUTINE(2);
  1613. #if HOTENDS > 3
  1614. PWM_OFF_ROUTINE(3);
  1615. #if HOTENDS > 4
  1616. PWM_OFF_ROUTINE(4);
  1617. #endif // HOTENDS > 4
  1618. #endif // HOTENDS > 3
  1619. #endif // HOTENDS > 2
  1620. #endif // HOTENDS > 1
  1621. #if HAS_HEATER_BED
  1622. PWM_OFF_ROUTINE(BED); // BED
  1623. #endif
  1624. #if ENABLED(FAN_SOFT_PWM)
  1625. if (pwm_count_tmp >= 127) {
  1626. pwm_count_tmp = 0;
  1627. #if HAS_FAN0
  1628. soft_pwm_count_fan[0] = soft_pwm_amount_fan[0] >> 1;
  1629. WRITE_FAN(soft_pwm_count_fan[0] > 0 ? HIGH : LOW);
  1630. #endif
  1631. #if HAS_FAN1
  1632. soft_pwm_count_fan[1] = soft_pwm_amount_fan[1] >> 1;
  1633. WRITE_FAN1(soft_pwm_count_fan[1] > 0 ? HIGH : LOW);
  1634. #endif
  1635. #if HAS_FAN2
  1636. soft_pwm_count_fan[2] = soft_pwm_amount_fan[2] >> 1;
  1637. WRITE_FAN2(soft_pwm_count_fan[2] > 0 ? HIGH : LOW);
  1638. #endif
  1639. }
  1640. #if HAS_FAN0
  1641. if (soft_pwm_count_fan[0] <= pwm_count_tmp) WRITE_FAN(LOW);
  1642. #endif
  1643. #if HAS_FAN1
  1644. if (soft_pwm_count_fan[1] <= pwm_count_tmp) WRITE_FAN1(LOW);
  1645. #endif
  1646. #if HAS_FAN2
  1647. if (soft_pwm_count_fan[2] <= pwm_count_tmp) WRITE_FAN2(LOW);
  1648. #endif
  1649. #endif // FAN_SOFT_PWM
  1650. // SOFT_PWM_SCALE to frequency:
  1651. //
  1652. // 0: 16000000/64/256/128 = 7.6294 Hz
  1653. // 1: / 64 = 15.2588 Hz
  1654. // 2: / 32 = 30.5176 Hz
  1655. // 3: / 16 = 61.0352 Hz
  1656. // 4: / 8 = 122.0703 Hz
  1657. // 5: / 4 = 244.1406 Hz
  1658. pwm_count = pwm_count_tmp + _BV(SOFT_PWM_SCALE);
  1659. // increment slow_pwm_count only every 64th pwm_count,
  1660. // i.e. yielding a PWM frequency of 16/128 Hz (8s).
  1661. if (((pwm_count >> SOFT_PWM_SCALE) & 0x3F) == 0) {
  1662. slow_pwm_count++;
  1663. slow_pwm_count &= 0x7F;
  1664. if (state_timer_heater_0 > 0) state_timer_heater_0--;
  1665. #if HOTENDS > 1
  1666. if (state_timer_heater_1 > 0) state_timer_heater_1--;
  1667. #if HOTENDS > 2
  1668. if (state_timer_heater_2 > 0) state_timer_heater_2--;
  1669. #if HOTENDS > 3
  1670. if (state_timer_heater_3 > 0) state_timer_heater_3--;
  1671. #if HOTENDS > 4
  1672. if (state_timer_heater_4 > 0) state_timer_heater_4--;
  1673. #endif // HOTENDS > 4
  1674. #endif // HOTENDS > 3
  1675. #endif // HOTENDS > 2
  1676. #endif // HOTENDS > 1
  1677. #if HAS_HEATER_BED
  1678. if (state_timer_heater_BED > 0) state_timer_heater_BED--;
  1679. #endif
  1680. } // ((pwm_count >> SOFT_PWM_SCALE) & 0x3F) == 0
  1681. #endif // SLOW_PWM_HEATERS
  1682. //
  1683. // Update lcd buttons 488 times per second
  1684. //
  1685. static bool do_buttons;
  1686. if ((do_buttons ^= true)) lcd_buttons_update();
  1687. /**
  1688. * One sensor is sampled on every other call of the ISR.
  1689. * Each sensor is read 16 (OVERSAMPLENR) times, taking the average.
  1690. *
  1691. * On each Prepare pass, ADC is started for a sensor pin.
  1692. * On the next pass, the ADC value is read and accumulated.
  1693. *
  1694. * This gives each ADC 0.9765ms to charge up.
  1695. */
  1696. switch (adc_sensor_state) {
  1697. case SensorsReady: {
  1698. // All sensors have been read. Stay in this state for a few
  1699. // ISRs to save on calls to temp update/checking code below.
  1700. constexpr int8_t extra_loops = MIN_ADC_ISR_LOOPS - (int8_t)SensorsReady;
  1701. static uint8_t delay_count = 0;
  1702. if (extra_loops > 0) {
  1703. if (delay_count == 0) delay_count = extra_loops; // Init this delay
  1704. if (--delay_count) // While delaying...
  1705. adc_sensor_state = (ADCSensorState)(int(SensorsReady) - 1); // retain this state (else, next state will be 0)
  1706. break;
  1707. }
  1708. else
  1709. adc_sensor_state = (ADCSensorState)0; // Fall-through to start first sensor now
  1710. }
  1711. #if HAS_TEMP_0
  1712. case PrepareTemp_0:
  1713. HAL_START_ADC(TEMP_0_PIN);
  1714. break;
  1715. case MeasureTemp_0:
  1716. raw_temp_value[0] += HAL_READ_ADC;
  1717. break;
  1718. #endif
  1719. #if HAS_TEMP_BED
  1720. case PrepareTemp_BED:
  1721. HAL_START_ADC(TEMP_BED_PIN);
  1722. break;
  1723. case MeasureTemp_BED:
  1724. raw_temp_bed_value += HAL_READ_ADC;
  1725. break;
  1726. #endif
  1727. #if HAS_TEMP_1
  1728. case PrepareTemp_1:
  1729. HAL_START_ADC(TEMP_1_PIN);
  1730. break;
  1731. case MeasureTemp_1:
  1732. raw_temp_value[1] += HAL_READ_ADC;
  1733. break;
  1734. #endif
  1735. #if HAS_TEMP_2
  1736. case PrepareTemp_2:
  1737. HAL_START_ADC(TEMP_2_PIN);
  1738. break;
  1739. case MeasureTemp_2:
  1740. raw_temp_value[2] += HAL_READ_ADC;
  1741. break;
  1742. #endif
  1743. #if HAS_TEMP_3
  1744. case PrepareTemp_3:
  1745. HAL_START_ADC(TEMP_3_PIN);
  1746. break;
  1747. case MeasureTemp_3:
  1748. raw_temp_value[3] += HAL_READ_ADC;
  1749. break;
  1750. #endif
  1751. #if HAS_TEMP_4
  1752. case PrepareTemp_4:
  1753. HAL_START_ADC(TEMP_4_PIN);
  1754. break;
  1755. case MeasureTemp_4:
  1756. raw_temp_value[4] += HAL_READ_ADC;
  1757. break;
  1758. #endif
  1759. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1760. case Prepare_FILWIDTH:
  1761. HAL_START_ADC(FILWIDTH_PIN);
  1762. break;
  1763. case Measure_FILWIDTH:
  1764. if (HAL_READ_ADC > 102) { // Make sure ADC is reading > 0.5 volts, otherwise don't read.
  1765. raw_filwidth_value -= (raw_filwidth_value >> 7); // Subtract 1/128th of the raw_filwidth_value
  1766. raw_filwidth_value += ((unsigned long)HAL_READ_ADC << 7); // Add new ADC reading, scaled by 128
  1767. }
  1768. break;
  1769. #endif
  1770. #if ENABLED(ADC_KEYPAD)
  1771. case Prepare_ADC_KEY:
  1772. HAL_START_ADC(ADC_KEYPAD_PIN);
  1773. break;
  1774. case Measure_ADC_KEY:
  1775. if (ADCKey_count < 16) {
  1776. raw_ADCKey_value = ADC;
  1777. if (raw_ADCKey_value > 900) {
  1778. //ADC Key release
  1779. ADCKey_count = 0;
  1780. current_ADCKey_raw = 0;
  1781. }
  1782. else {
  1783. current_ADCKey_raw += raw_ADCKey_value;
  1784. ADCKey_count++;
  1785. }
  1786. }
  1787. break;
  1788. #endif // ADC_KEYPAD
  1789. case StartupDelay: break;
  1790. } // switch(adc_sensor_state)
  1791. if (!adc_sensor_state && ++temp_count >= OVERSAMPLENR) { // 10 * 16 * 1/(16000000/64/256) = 164ms.
  1792. temp_count = 0;
  1793. // Update the raw values if they've been read. Else we could be updating them during reading.
  1794. if (!temp_meas_ready) set_current_temp_raw();
  1795. // Filament Sensor - can be read any time since IIR filtering is used
  1796. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1797. current_raw_filwidth = raw_filwidth_value >> 10; // Divide to get to 0-16384 range since we used 1/128 IIR filter approach
  1798. #endif
  1799. ZERO(raw_temp_value);
  1800. raw_temp_bed_value = 0;
  1801. #define TEMPDIR(N) ((HEATER_##N##_RAW_LO_TEMP) > (HEATER_##N##_RAW_HI_TEMP) ? -1 : 1)
  1802. int constexpr temp_dir[] = {
  1803. #if ENABLED(HEATER_0_USES_MAX6675)
  1804. 0
  1805. #else
  1806. TEMPDIR(0)
  1807. #endif
  1808. #if HOTENDS > 1
  1809. , TEMPDIR(1)
  1810. #if HOTENDS > 2
  1811. , TEMPDIR(2)
  1812. #if HOTENDS > 3
  1813. , TEMPDIR(3)
  1814. #if HOTENDS > 4
  1815. , TEMPDIR(4)
  1816. #endif // HOTENDS > 4
  1817. #endif // HOTENDS > 3
  1818. #endif // HOTENDS > 2
  1819. #endif // HOTENDS > 1
  1820. };
  1821. for (uint8_t e = 0; e < COUNT(temp_dir); e++) {
  1822. const int16_t tdir = temp_dir[e], rawtemp = current_temperature_raw[e] * tdir;
  1823. const bool heater_on = 0 <
  1824. #if ENABLED(PIDTEMP)
  1825. soft_pwm_amount[e]
  1826. #else
  1827. target_temperature[e]
  1828. #endif
  1829. ;
  1830. if (rawtemp > maxttemp_raw[e] * tdir && heater_on) max_temp_error(e);
  1831. if (rawtemp < minttemp_raw[e] * tdir && !is_preheating(e) && heater_on) {
  1832. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  1833. if (++consecutive_low_temperature_error[e] >= MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED)
  1834. #endif
  1835. min_temp_error(e);
  1836. }
  1837. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  1838. else
  1839. consecutive_low_temperature_error[e] = 0;
  1840. #endif
  1841. }
  1842. #if HAS_TEMP_BED
  1843. #if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1844. #define GEBED <=
  1845. #else
  1846. #define GEBED >=
  1847. #endif
  1848. const bool bed_on = 0 <
  1849. #if ENABLED(PIDTEMPBED)
  1850. soft_pwm_amount_bed
  1851. #else
  1852. target_temperature_bed
  1853. #endif
  1854. ;
  1855. if (current_temperature_bed_raw GEBED bed_maxttemp_raw && bed_on) max_temp_error(-1);
  1856. if (bed_minttemp_raw GEBED current_temperature_bed_raw && bed_on) min_temp_error(-1);
  1857. #endif
  1858. } // temp_count >= OVERSAMPLENR
  1859. // Go to the next state, up to SensorsReady
  1860. adc_sensor_state = (ADCSensorState)(int(adc_sensor_state) + 1);
  1861. if (adc_sensor_state > SensorsReady) adc_sensor_state = (ADCSensorState)0;
  1862. #if ENABLED(BABYSTEPPING)
  1863. LOOP_XYZ(axis) {
  1864. const int curTodo = babystepsTodo[axis]; // get rid of volatile for performance
  1865. if (curTodo) {
  1866. stepper.babystep((AxisEnum)axis, curTodo > 0);
  1867. if (curTodo > 0) babystepsTodo[axis]--;
  1868. else babystepsTodo[axis]++;
  1869. }
  1870. }
  1871. #endif // BABYSTEPPING
  1872. #if ENABLED(PINS_DEBUGGING)
  1873. endstops.run_monitor(); // report changes in endstop status
  1874. #endif
  1875. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  1876. extern volatile uint8_t e_hit;
  1877. if (e_hit && ENDSTOPS_ENABLED) {
  1878. endstops.update(); // call endstop update routine
  1879. e_hit--;
  1880. }
  1881. #endif
  1882. #ifndef CPU_32_BIT
  1883. cli();
  1884. #endif
  1885. in_temp_isr = false;
  1886. ENABLE_TEMPERATURE_INTERRUPT(); //re-enable Temperature ISR
  1887. }
  1888. #if HAS_TEMP_HOTEND || HAS_TEMP_BED
  1889. #include "../gcode/gcode.h"
  1890. void print_heater_state(const float &c, const float &t,
  1891. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  1892. const float r,
  1893. #endif
  1894. const int8_t e=-2
  1895. ) {
  1896. #if !(HAS_TEMP_BED && HAS_TEMP_HOTEND) && HOTENDS <= 1
  1897. UNUSED(e);
  1898. #endif
  1899. SERIAL_PROTOCOLCHAR(' ');
  1900. SERIAL_PROTOCOLCHAR(
  1901. #if HAS_TEMP_BED && HAS_TEMP_HOTEND
  1902. e == -1 ? 'B' : 'T'
  1903. #elif HAS_TEMP_HOTEND
  1904. 'T'
  1905. #else
  1906. 'B'
  1907. #endif
  1908. );
  1909. #if HOTENDS > 1
  1910. if (e >= 0) SERIAL_PROTOCOLCHAR('0' + e);
  1911. #endif
  1912. SERIAL_PROTOCOLCHAR(':');
  1913. SERIAL_PROTOCOL(c);
  1914. SERIAL_PROTOCOLPAIR(" /" , t);
  1915. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  1916. SERIAL_PROTOCOLPAIR(" (", r / OVERSAMPLENR);
  1917. SERIAL_PROTOCOLCHAR(')');
  1918. #endif
  1919. }
  1920. void Temperature::print_heaterstates() {
  1921. #if HAS_TEMP_HOTEND
  1922. print_heater_state(degHotend(gcode.target_extruder), degTargetHotend(gcode.target_extruder)
  1923. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  1924. , rawHotendTemp(gcode.target_extruder)
  1925. #endif
  1926. );
  1927. #endif
  1928. #if HAS_TEMP_BED
  1929. print_heater_state(degBed(), degTargetBed(),
  1930. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  1931. rawBedTemp(),
  1932. #endif
  1933. -1 // BED
  1934. );
  1935. #endif
  1936. #if HOTENDS > 1
  1937. HOTEND_LOOP() print_heater_state(degHotend(e), degTargetHotend(e),
  1938. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  1939. rawHotendTemp(e),
  1940. #endif
  1941. e
  1942. );
  1943. #endif
  1944. SERIAL_PROTOCOLPGM(" @:");
  1945. SERIAL_PROTOCOL(getHeaterPower(gcode.target_extruder));
  1946. #if HAS_TEMP_BED
  1947. SERIAL_PROTOCOLPGM(" B@:");
  1948. SERIAL_PROTOCOL(getHeaterPower(-1));
  1949. #endif
  1950. #if HOTENDS > 1
  1951. HOTEND_LOOP() {
  1952. SERIAL_PROTOCOLPAIR(" @", e);
  1953. SERIAL_PROTOCOLCHAR(':');
  1954. SERIAL_PROTOCOL(getHeaterPower(e));
  1955. }
  1956. #endif
  1957. }
  1958. #if ENABLED(AUTO_REPORT_TEMPERATURES)
  1959. uint8_t Temperature::auto_report_temp_interval;
  1960. millis_t Temperature::next_temp_report_ms;
  1961. void Temperature::auto_report_temperatures() {
  1962. if (auto_report_temp_interval && ELAPSED(millis(), next_temp_report_ms)) {
  1963. next_temp_report_ms = millis() + 1000UL * auto_report_temp_interval;
  1964. print_heaterstates();
  1965. SERIAL_EOL();
  1966. }
  1967. }
  1968. #endif // AUTO_REPORT_TEMPERATURES
  1969. #endif // HAS_TEMP_HOTEND || HAS_TEMP_BED