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

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