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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243
  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. }
  714. #endif // FILAMENT_WIDTH_SENSOR
  715. #if WATCH_THE_BED
  716. // Make sure temperature is increasing
  717. if (watch_bed_next_ms && ELAPSED(ms, watch_bed_next_ms)) { // Time to check the bed?
  718. if (degBed() < watch_target_bed_temp) // Failed to increase enough?
  719. _temp_error(-1, PSTR(MSG_T_HEATING_FAILED), PSTR(MSG_HEATING_FAILED_LCD));
  720. else // Start again if the target is still far off
  721. start_watching_bed();
  722. }
  723. #endif // WATCH_THE_BED
  724. #if DISABLED(PIDTEMPBED)
  725. if (PENDING(ms, next_bed_check_ms)) return;
  726. next_bed_check_ms = ms + BED_CHECK_INTERVAL;
  727. #endif
  728. #if HAS_TEMP_BED
  729. #if HEATER_IDLE_HANDLER
  730. if (!bed_idle_timeout_exceeded && bed_idle_timeout_ms && ELAPSED(ms, bed_idle_timeout_ms))
  731. bed_idle_timeout_exceeded = true;
  732. #endif
  733. #if HAS_THERMALLY_PROTECTED_BED
  734. 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);
  735. #endif
  736. #if HEATER_IDLE_HANDLER
  737. if (bed_idle_timeout_exceeded)
  738. {
  739. soft_pwm_amount_bed = 0;
  740. #if DISABLED(PIDTEMPBED)
  741. WRITE_HEATER_BED(LOW);
  742. #endif
  743. }
  744. else
  745. #endif
  746. {
  747. #if ENABLED(PIDTEMPBED)
  748. soft_pwm_amount_bed = WITHIN(current_temperature_bed, BED_MINTEMP, BED_MAXTEMP) ? (int)get_pid_output_bed() >> 1 : 0;
  749. #elif ENABLED(BED_LIMIT_SWITCHING)
  750. // Check if temperature is within the correct band
  751. if (WITHIN(current_temperature_bed, BED_MINTEMP, BED_MAXTEMP)) {
  752. if (current_temperature_bed >= target_temperature_bed + BED_HYSTERESIS)
  753. soft_pwm_amount_bed = 0;
  754. else if (current_temperature_bed <= target_temperature_bed - (BED_HYSTERESIS))
  755. soft_pwm_amount_bed = MAX_BED_POWER >> 1;
  756. }
  757. else {
  758. soft_pwm_amount_bed = 0;
  759. WRITE_HEATER_BED(LOW);
  760. }
  761. #else // !PIDTEMPBED && !BED_LIMIT_SWITCHING
  762. // Check if temperature is within the correct range
  763. if (WITHIN(current_temperature_bed, BED_MINTEMP, BED_MAXTEMP)) {
  764. soft_pwm_amount_bed = current_temperature_bed < target_temperature_bed ? MAX_BED_POWER >> 1 : 0;
  765. }
  766. else {
  767. soft_pwm_amount_bed = 0;
  768. WRITE_HEATER_BED(LOW);
  769. }
  770. #endif
  771. }
  772. #endif // HAS_TEMP_BED
  773. }
  774. #define PGM_RD_W(x) (short)pgm_read_word(&x)
  775. // Derived from RepRap FiveD extruder::getTemperature()
  776. // For hot end temperature measurement.
  777. float Temperature::analog2temp(int raw, uint8_t e) {
  778. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  779. if (e > HOTENDS)
  780. #else
  781. if (e >= HOTENDS)
  782. #endif
  783. {
  784. SERIAL_ERROR_START();
  785. SERIAL_ERROR((int)e);
  786. SERIAL_ERRORLNPGM(MSG_INVALID_EXTRUDER_NUM);
  787. kill(PSTR(MSG_KILLED));
  788. return 0.0;
  789. }
  790. #if ENABLED(HEATER_0_USES_MAX6675)
  791. if (e == 0) return 0.25 * raw;
  792. #endif
  793. if (heater_ttbl_map[e] != NULL) {
  794. float celsius = 0;
  795. uint8_t i;
  796. short(*tt)[][2] = (short(*)[][2])(heater_ttbl_map[e]);
  797. for (i = 1; i < heater_ttbllen_map[e]; i++) {
  798. if (PGM_RD_W((*tt)[i][0]) > raw) {
  799. celsius = PGM_RD_W((*tt)[i - 1][1]) +
  800. (raw - PGM_RD_W((*tt)[i - 1][0])) *
  801. (float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i - 1][1])) /
  802. (float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i - 1][0]));
  803. break;
  804. }
  805. }
  806. // Overflow: Set to last value in the table
  807. if (i == heater_ttbllen_map[e]) celsius = PGM_RD_W((*tt)[i - 1][1]);
  808. return celsius;
  809. }
  810. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * (TEMP_SENSOR_AD595_GAIN)) + TEMP_SENSOR_AD595_OFFSET;
  811. }
  812. // Derived from RepRap FiveD extruder::getTemperature()
  813. // For bed temperature measurement.
  814. float Temperature::analog2tempBed(const int raw) {
  815. #if ENABLED(BED_USES_THERMISTOR)
  816. float celsius = 0;
  817. byte i;
  818. for (i = 1; i < BEDTEMPTABLE_LEN; i++) {
  819. if (PGM_RD_W(BEDTEMPTABLE[i][0]) > raw) {
  820. celsius = PGM_RD_W(BEDTEMPTABLE[i - 1][1]) +
  821. (raw - PGM_RD_W(BEDTEMPTABLE[i - 1][0])) *
  822. (float)(PGM_RD_W(BEDTEMPTABLE[i][1]) - PGM_RD_W(BEDTEMPTABLE[i - 1][1])) /
  823. (float)(PGM_RD_W(BEDTEMPTABLE[i][0]) - PGM_RD_W(BEDTEMPTABLE[i - 1][0]));
  824. break;
  825. }
  826. }
  827. // Overflow: Set to last value in the table
  828. if (i == BEDTEMPTABLE_LEN) celsius = PGM_RD_W(BEDTEMPTABLE[i - 1][1]);
  829. return celsius;
  830. #elif defined(BED_USES_AD595)
  831. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * (TEMP_SENSOR_AD595_GAIN)) + TEMP_SENSOR_AD595_OFFSET;
  832. #else
  833. UNUSED(raw);
  834. return 0;
  835. #endif
  836. }
  837. /**
  838. * Get the raw values into the actual temperatures.
  839. * The raw values are created in interrupt context,
  840. * and this function is called from normal context
  841. * as it would block the stepper routine.
  842. */
  843. void Temperature::updateTemperaturesFromRawValues() {
  844. #if ENABLED(HEATER_0_USES_MAX6675)
  845. current_temperature_raw[0] = read_max6675();
  846. #endif
  847. HOTEND_LOOP()
  848. current_temperature[e] = Temperature::analog2temp(current_temperature_raw[e], e);
  849. current_temperature_bed = Temperature::analog2tempBed(current_temperature_bed_raw);
  850. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  851. redundant_temperature = Temperature::analog2temp(redundant_temperature_raw, 1);
  852. #endif
  853. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  854. filament_width_meas = analog2widthFil();
  855. #endif
  856. #if ENABLED(USE_WATCHDOG)
  857. // Reset the watchdog after we know we have a temperature measurement.
  858. watchdog_reset();
  859. #endif
  860. CRITICAL_SECTION_START;
  861. temp_meas_ready = false;
  862. CRITICAL_SECTION_END;
  863. }
  864. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  865. // Convert raw Filament Width to millimeters
  866. float Temperature::analog2widthFil() {
  867. return current_raw_filwidth * 5.0 * (1.0 / 16383.0);
  868. //return current_raw_filwidth;
  869. }
  870. // Convert raw Filament Width to a ratio
  871. int Temperature::widthFil_to_size_ratio() {
  872. float temp = filament_width_meas;
  873. if (temp < MEASURED_LOWER_LIMIT) temp = filament_width_nominal; //assume sensor cut out
  874. else NOMORE(temp, MEASURED_UPPER_LIMIT);
  875. return filament_width_nominal / temp * 100;
  876. }
  877. #endif
  878. #if ENABLED(HEATER_0_USES_MAX6675)
  879. #ifndef MAX6675_SCK_PIN
  880. #define MAX6675_SCK_PIN SCK_PIN
  881. #endif
  882. #ifndef MAX6675_DO_PIN
  883. #define MAX6675_DO_PIN MISO_PIN
  884. #endif
  885. SPIclass<MAX6675_DO_PIN, MOSI_PIN, MAX6675_SCK_PIN> max6675_spi;
  886. #endif
  887. /**
  888. * Initialize the temperature manager
  889. * The manager is implemented by periodic calls to manage_heater()
  890. */
  891. void Temperature::init() {
  892. #if MB(RUMBA) && (TEMP_SENSOR_0 == -1 || TEMP_SENSOR_1 == -1 || TEMP_SENSOR_2 == -1 || TEMP_SENSOR_BED == -1)
  893. // Disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector
  894. MCUCR = _BV(JTD);
  895. MCUCR = _BV(JTD);
  896. #endif
  897. // Finish init of mult hotend arrays
  898. HOTEND_LOOP() maxttemp[e] = maxttemp[0];
  899. #if ENABLED(PIDTEMP) && ENABLED(PID_EXTRUSION_SCALING)
  900. last_e_position = 0;
  901. #endif
  902. #if HAS_HEATER_0
  903. SET_OUTPUT(HEATER_0_PIN);
  904. #endif
  905. #if HAS_HEATER_1
  906. SET_OUTPUT(HEATER_1_PIN);
  907. #endif
  908. #if HAS_HEATER_2
  909. SET_OUTPUT(HEATER_2_PIN);
  910. #endif
  911. #if HAS_HEATER_3
  912. SET_OUTPUT(HEATER_3_PIN);
  913. #endif
  914. #if HAS_HEATER_4
  915. SET_OUTPUT(HEATER_3_PIN);
  916. #endif
  917. #if HAS_HEATER_BED
  918. SET_OUTPUT(HEATER_BED_PIN);
  919. #endif
  920. #if HAS_FAN0
  921. SET_OUTPUT(FAN_PIN);
  922. #if ENABLED(FAST_PWM_FAN)
  923. setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  924. #endif
  925. #endif
  926. #if HAS_FAN1
  927. SET_OUTPUT(FAN1_PIN);
  928. #if ENABLED(FAST_PWM_FAN)
  929. setPwmFrequency(FAN1_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  930. #endif
  931. #endif
  932. #if HAS_FAN2
  933. SET_OUTPUT(FAN2_PIN);
  934. #if ENABLED(FAST_PWM_FAN)
  935. setPwmFrequency(FAN2_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  936. #endif
  937. #endif
  938. #if ENABLED(HEATER_0_USES_MAX6675)
  939. OUT_WRITE(SCK_PIN, LOW);
  940. OUT_WRITE(MOSI_PIN, HIGH);
  941. SET_INPUT_PULLUP(MISO_PIN);
  942. max6675_spi.init();
  943. OUT_WRITE(SS_PIN, HIGH);
  944. OUT_WRITE(MAX6675_SS, HIGH);
  945. #endif // HEATER_0_USES_MAX6675
  946. HAL_adc_init();
  947. #if HAS_TEMP_0
  948. HAL_ANALOG_SELECT(TEMP_0_PIN);
  949. #endif
  950. #if HAS_TEMP_1
  951. HAL_ANALOG_SELECT(TEMP_1_PIN);
  952. #endif
  953. #if HAS_TEMP_2
  954. HAL_ANALOG_SELECT(TEMP_2_PIN);
  955. #endif
  956. #if HAS_TEMP_3
  957. HAL_ANALOG_SELECT(TEMP_3_PIN);
  958. #endif
  959. #if HAS_TEMP_4
  960. HAL_ANALOG_SELECT(TEMP_4_PIN);
  961. #endif
  962. #if HAS_TEMP_BED
  963. HAL_ANALOG_SELECT(TEMP_BED_PIN);
  964. #endif
  965. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  966. HAL_ANALOG_SELECT(FILWIDTH_PIN);
  967. #endif
  968. // todo: HAL: fix abstraction
  969. #ifdef __AVR__
  970. // Use timer0 for temperature measurement
  971. // Interleave temperature interrupt with millies interrupt
  972. OCR0B = 128;
  973. SBI(TIMSK0, OCIE0B);
  974. #else
  975. HAL_timer_start(TEMP_TIMER_NUM, TEMP_TIMER_FREQUENCY);
  976. HAL_timer_enable_interrupt(TEMP_TIMER_NUM);
  977. #endif
  978. #if HAS_AUTO_FAN_0
  979. #if E0_AUTO_FAN_PIN == FAN1_PIN
  980. SET_OUTPUT(E0_AUTO_FAN_PIN);
  981. #if ENABLED(FAST_PWM_FAN)
  982. setPwmFrequency(E0_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  983. #endif
  984. #else
  985. SET_OUTPUT(E0_AUTO_FAN_PIN);
  986. #endif
  987. #endif
  988. #if HAS_AUTO_FAN_1 && !AUTO_1_IS_0
  989. #if E1_AUTO_FAN_PIN == FAN1_PIN
  990. SET_OUTPUT(E1_AUTO_FAN_PIN);
  991. #if ENABLED(FAST_PWM_FAN)
  992. setPwmFrequency(E1_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  993. #endif
  994. #else
  995. SET_OUTPUT(E1_AUTO_FAN_PIN);
  996. #endif
  997. #endif
  998. #if HAS_AUTO_FAN_2 && !AUTO_2_IS_0 && !AUTO_2_IS_1
  999. #if E2_AUTO_FAN_PIN == FAN1_PIN
  1000. SET_OUTPUT(E2_AUTO_FAN_PIN);
  1001. #if ENABLED(FAST_PWM_FAN)
  1002. setPwmFrequency(E2_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  1003. #endif
  1004. #else
  1005. SET_OUTPUT(E2_AUTO_FAN_PIN);
  1006. #endif
  1007. #endif
  1008. #if HAS_AUTO_FAN_3 && !AUTO_3_IS_0 && !AUTO_3_IS_1 && !AUTO_3_IS_2
  1009. #if E3_AUTO_FAN_PIN == FAN1_PIN
  1010. SET_OUTPUT(E3_AUTO_FAN_PIN);
  1011. #if ENABLED(FAST_PWM_FAN)
  1012. setPwmFrequency(E3_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  1013. #endif
  1014. #else
  1015. SET_OUTPUT(E3_AUTO_FAN_PIN);
  1016. #endif
  1017. #endif
  1018. #if HAS_AUTO_FAN_4 && !AUTO_4_IS_0 && !AUTO_4_IS_1 && !AUTO_4_IS_2 && !AUTO_4_IS_3
  1019. #if E4_AUTO_FAN_PIN == FAN1_PIN
  1020. SET_OUTPUT(E4_AUTO_FAN_PIN);
  1021. #if ENABLED(FAST_PWM_FAN)
  1022. setPwmFrequency(E4_AUTO_FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  1023. #endif
  1024. #else
  1025. SET_OUTPUT(E4_AUTO_FAN_PIN);
  1026. #endif
  1027. #endif
  1028. // Wait for temperature measurement to settle
  1029. delay(250);
  1030. #define TEMP_MIN_ROUTINE(NR) \
  1031. minttemp[NR] = HEATER_ ##NR## _MINTEMP; \
  1032. while (analog2temp(minttemp_raw[NR], NR) < HEATER_ ##NR## _MINTEMP) { \
  1033. if (HEATER_ ##NR## _RAW_LO_TEMP < HEATER_ ##NR## _RAW_HI_TEMP) \
  1034. minttemp_raw[NR] += OVERSAMPLENR; \
  1035. else \
  1036. minttemp_raw[NR] -= OVERSAMPLENR; \
  1037. }
  1038. #define TEMP_MAX_ROUTINE(NR) \
  1039. maxttemp[NR] = HEATER_ ##NR## _MAXTEMP; \
  1040. while (analog2temp(maxttemp_raw[NR], NR) > HEATER_ ##NR## _MAXTEMP) { \
  1041. if (HEATER_ ##NR## _RAW_LO_TEMP < HEATER_ ##NR## _RAW_HI_TEMP) \
  1042. maxttemp_raw[NR] -= OVERSAMPLENR; \
  1043. else \
  1044. maxttemp_raw[NR] += OVERSAMPLENR; \
  1045. }
  1046. #ifdef HEATER_0_MINTEMP
  1047. TEMP_MIN_ROUTINE(0);
  1048. #endif
  1049. #ifdef HEATER_0_MAXTEMP
  1050. TEMP_MAX_ROUTINE(0);
  1051. #endif
  1052. #if HOTENDS > 1
  1053. #ifdef HEATER_1_MINTEMP
  1054. TEMP_MIN_ROUTINE(1);
  1055. #endif
  1056. #ifdef HEATER_1_MAXTEMP
  1057. TEMP_MAX_ROUTINE(1);
  1058. #endif
  1059. #if HOTENDS > 2
  1060. #ifdef HEATER_2_MINTEMP
  1061. TEMP_MIN_ROUTINE(2);
  1062. #endif
  1063. #ifdef HEATER_2_MAXTEMP
  1064. TEMP_MAX_ROUTINE(2);
  1065. #endif
  1066. #if HOTENDS > 3
  1067. #ifdef HEATER_3_MINTEMP
  1068. TEMP_MIN_ROUTINE(3);
  1069. #endif
  1070. #ifdef HEATER_3_MAXTEMP
  1071. TEMP_MAX_ROUTINE(3);
  1072. #endif
  1073. #if HOTENDS > 4
  1074. #ifdef HEATER_4_MINTEMP
  1075. TEMP_MIN_ROUTINE(4);
  1076. #endif
  1077. #ifdef HEATER_4_MAXTEMP
  1078. TEMP_MAX_ROUTINE(4);
  1079. #endif
  1080. #endif // HOTENDS > 4
  1081. #endif // HOTENDS > 3
  1082. #endif // HOTENDS > 2
  1083. #endif // HOTENDS > 1
  1084. #ifdef BED_MINTEMP
  1085. while (analog2tempBed(bed_minttemp_raw) < BED_MINTEMP) {
  1086. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  1087. bed_minttemp_raw += OVERSAMPLENR;
  1088. #else
  1089. bed_minttemp_raw -= OVERSAMPLENR;
  1090. #endif
  1091. }
  1092. #endif // BED_MINTEMP
  1093. #ifdef BED_MAXTEMP
  1094. while (analog2tempBed(bed_maxttemp_raw) > BED_MAXTEMP) {
  1095. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  1096. bed_maxttemp_raw -= OVERSAMPLENR;
  1097. #else
  1098. bed_maxttemp_raw += OVERSAMPLENR;
  1099. #endif
  1100. }
  1101. #endif // BED_MAXTEMP
  1102. #if ENABLED(PROBING_HEATERS_OFF)
  1103. paused = false;
  1104. #endif
  1105. }
  1106. #if ENABLED(FAST_PWM_FAN)
  1107. void Temperature::setPwmFrequency(const uint8_t pin, int val) {
  1108. val &= 0x07;
  1109. switch (digitalPinToTimer(pin)) {
  1110. #ifdef TCCR0A
  1111. #if !AVR_AT90USB1286_FAMILY
  1112. case TIMER0A:
  1113. #endif
  1114. case TIMER0B:
  1115. //_SET_CS(0, val);
  1116. break;
  1117. #endif
  1118. #ifdef TCCR1A
  1119. case TIMER1A:
  1120. case TIMER1B:
  1121. //_SET_CS(1, val);
  1122. break;
  1123. #endif
  1124. #ifdef TCCR2
  1125. case TIMER2:
  1126. case TIMER2:
  1127. _SET_CS(2, val);
  1128. break;
  1129. #endif
  1130. #ifdef TCCR2A
  1131. case TIMER2A:
  1132. case TIMER2B:
  1133. _SET_CS(2, val);
  1134. break;
  1135. #endif
  1136. #ifdef TCCR3A
  1137. case TIMER3A:
  1138. case TIMER3B:
  1139. case TIMER3C:
  1140. _SET_CS(3, val);
  1141. break;
  1142. #endif
  1143. #ifdef TCCR4A
  1144. case TIMER4A:
  1145. case TIMER4B:
  1146. case TIMER4C:
  1147. _SET_CS(4, val);
  1148. break;
  1149. #endif
  1150. #ifdef TCCR5A
  1151. case TIMER5A:
  1152. case TIMER5B:
  1153. case TIMER5C:
  1154. _SET_CS(5, val);
  1155. break;
  1156. #endif
  1157. }
  1158. }
  1159. #endif // FAST_PWM_FAN
  1160. #if WATCH_HOTENDS
  1161. /**
  1162. * Start Heating Sanity Check for hotends that are below
  1163. * their target temperature by a configurable margin.
  1164. * This is called when the temperature is set. (M104, M109)
  1165. */
  1166. void Temperature::start_watching_heater(uint8_t e) {
  1167. #if HOTENDS == 1
  1168. UNUSED(e);
  1169. #endif
  1170. if (degHotend(HOTEND_INDEX) < degTargetHotend(HOTEND_INDEX) - (WATCH_TEMP_INCREASE + TEMP_HYSTERESIS + 1)) {
  1171. watch_target_temp[HOTEND_INDEX] = degHotend(HOTEND_INDEX) + WATCH_TEMP_INCREASE;
  1172. watch_heater_next_ms[HOTEND_INDEX] = millis() + (WATCH_TEMP_PERIOD) * 1000UL;
  1173. }
  1174. else
  1175. watch_heater_next_ms[HOTEND_INDEX] = 0;
  1176. }
  1177. #endif
  1178. #if WATCH_THE_BED
  1179. /**
  1180. * Start Heating Sanity Check for hotends that are below
  1181. * their target temperature by a configurable margin.
  1182. * This is called when the temperature is set. (M140, M190)
  1183. */
  1184. void Temperature::start_watching_bed() {
  1185. if (degBed() < degTargetBed() - (WATCH_BED_TEMP_INCREASE + TEMP_BED_HYSTERESIS + 1)) {
  1186. watch_target_bed_temp = degBed() + WATCH_BED_TEMP_INCREASE;
  1187. watch_bed_next_ms = millis() + (WATCH_BED_TEMP_PERIOD) * 1000UL;
  1188. }
  1189. else
  1190. watch_bed_next_ms = 0;
  1191. }
  1192. #endif
  1193. #if ENABLED(THERMAL_PROTECTION_HOTENDS) || HAS_THERMALLY_PROTECTED_BED
  1194. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  1195. Temperature::TRState Temperature::thermal_runaway_state_machine[HOTENDS] = { TRInactive };
  1196. millis_t Temperature::thermal_runaway_timer[HOTENDS] = { 0 };
  1197. #endif
  1198. #if HAS_THERMALLY_PROTECTED_BED
  1199. Temperature::TRState Temperature::thermal_runaway_bed_state_machine = TRInactive;
  1200. millis_t Temperature::thermal_runaway_bed_timer;
  1201. #endif
  1202. void Temperature::thermal_runaway_protection(Temperature::TRState* state, millis_t* timer, float current, float target, int heater_id, int period_seconds, int hysteresis_degc) {
  1203. static float tr_target_temperature[HOTENDS + 1] = { 0.0 };
  1204. /**
  1205. SERIAL_ECHO_START();
  1206. SERIAL_ECHOPGM("Thermal Thermal Runaway Running. Heater ID: ");
  1207. if (heater_id < 0) SERIAL_ECHOPGM("bed"); else SERIAL_ECHO(heater_id);
  1208. SERIAL_ECHOPAIR(" ; State:", *state);
  1209. SERIAL_ECHOPAIR(" ; Timer:", *timer);
  1210. SERIAL_ECHOPAIR(" ; Temperature:", current);
  1211. SERIAL_ECHOPAIR(" ; Target Temp:", target);
  1212. if (heater_id >= 0)
  1213. SERIAL_ECHOPAIR(" ; Idle Timeout:", heater_idle_timeout_exceeded[heater_id]);
  1214. else
  1215. SERIAL_ECHOPAIR(" ; Idle Timeout:", bed_idle_timeout_exceeded);
  1216. SERIAL_EOL();
  1217. */
  1218. const int heater_index = heater_id >= 0 ? heater_id : HOTENDS;
  1219. #if HEATER_IDLE_HANDLER
  1220. // If the heater idle timeout expires, restart
  1221. if (heater_id >= 0 && heater_idle_timeout_exceeded[heater_id]) {
  1222. *state = TRInactive;
  1223. tr_target_temperature[heater_index] = 0;
  1224. }
  1225. #if HAS_TEMP_BED
  1226. else if (heater_id < 0 && bed_idle_timeout_exceeded) {
  1227. *state = TRInactive;
  1228. tr_target_temperature[heater_index] = 0;
  1229. }
  1230. #endif
  1231. else
  1232. #endif
  1233. // If the target temperature changes, restart
  1234. if (tr_target_temperature[heater_index] != target) {
  1235. tr_target_temperature[heater_index] = target;
  1236. *state = target > 0 ? TRFirstHeating : TRInactive;
  1237. }
  1238. switch (*state) {
  1239. // Inactive state waits for a target temperature to be set
  1240. case TRInactive: break;
  1241. // When first heating, wait for the temperature to be reached then go to Stable state
  1242. case TRFirstHeating:
  1243. if (current < tr_target_temperature[heater_index]) break;
  1244. *state = TRStable;
  1245. // While the temperature is stable watch for a bad temperature
  1246. case TRStable:
  1247. if (current >= tr_target_temperature[heater_index] - hysteresis_degc) {
  1248. *timer = millis() + period_seconds * 1000UL;
  1249. break;
  1250. }
  1251. else if (PENDING(millis(), *timer)) break;
  1252. *state = TRRunaway;
  1253. case TRRunaway:
  1254. _temp_error(heater_id, PSTR(MSG_T_THERMAL_RUNAWAY), PSTR(MSG_THERMAL_RUNAWAY));
  1255. }
  1256. }
  1257. #endif // THERMAL_PROTECTION_HOTENDS || THERMAL_PROTECTION_BED
  1258. void Temperature::disable_all_heaters() {
  1259. #if ENABLED(AUTOTEMP)
  1260. planner.autotemp_enabled = false;
  1261. #endif
  1262. HOTEND_LOOP() setTargetHotend(0, e);
  1263. setTargetBed(0);
  1264. // Unpause and reset everything
  1265. #if ENABLED(PROBING_HEATERS_OFF)
  1266. pause(false);
  1267. #endif
  1268. // If all heaters go down then for sure our print job has stopped
  1269. print_job_timer.stop();
  1270. #define DISABLE_HEATER(NR) { \
  1271. setTargetHotend(0, NR); \
  1272. soft_pwm_amount[NR] = 0; \
  1273. WRITE_HEATER_ ##NR (LOW); \
  1274. }
  1275. #if HAS_TEMP_HOTEND
  1276. DISABLE_HEATER(0);
  1277. #if HOTENDS > 1
  1278. DISABLE_HEATER(1);
  1279. #if HOTENDS > 2
  1280. DISABLE_HEATER(2);
  1281. #if HOTENDS > 3
  1282. DISABLE_HEATER(3);
  1283. #if HOTENDS > 4
  1284. DISABLE_HEATER(4);
  1285. #endif // HOTENDS > 4
  1286. #endif // HOTENDS > 3
  1287. #endif // HOTENDS > 2
  1288. #endif // HOTENDS > 1
  1289. #endif
  1290. #if HAS_TEMP_BED
  1291. target_temperature_bed = 0;
  1292. soft_pwm_amount_bed = 0;
  1293. #if HAS_HEATER_BED
  1294. WRITE_HEATER_BED(LOW);
  1295. #endif
  1296. #endif
  1297. }
  1298. #if ENABLED(PROBING_HEATERS_OFF)
  1299. void Temperature::pause(const bool p) {
  1300. if (p != paused) {
  1301. paused = p;
  1302. if (p) {
  1303. HOTEND_LOOP() start_heater_idle_timer(e, 0); // timeout immediately
  1304. #if HAS_TEMP_BED
  1305. start_bed_idle_timer(0); // timeout immediately
  1306. #endif
  1307. }
  1308. else {
  1309. HOTEND_LOOP() reset_heater_idle_timer(e);
  1310. #if HAS_TEMP_BED
  1311. reset_bed_idle_timer();
  1312. #endif
  1313. }
  1314. }
  1315. }
  1316. #endif // PROBING_HEATERS_OFF
  1317. #if ENABLED(HEATER_0_USES_MAX6675)
  1318. #define MAX6675_HEAT_INTERVAL 250u
  1319. #if ENABLED(MAX6675_IS_MAX31855)
  1320. uint32_t max6675_temp = 2000;
  1321. #define MAX6675_ERROR_MASK 7
  1322. #define MAX6675_DISCARD_BITS 18
  1323. #define MAX6675_SPEED_BITS 3 // (_BV(SPR1)) // clock ÷ 64
  1324. #else
  1325. uint16_t max6675_temp = 2000;
  1326. #define MAX6675_ERROR_MASK 4
  1327. #define MAX6675_DISCARD_BITS 3
  1328. #define MAX6675_SPEED_BITS 2 // (_BV(SPR0)) // clock ÷ 16
  1329. #endif
  1330. int Temperature::read_max6675() {
  1331. static millis_t next_max6675_ms = 0;
  1332. millis_t ms = millis();
  1333. if (PENDING(ms, next_max6675_ms)) return (int)max6675_temp;
  1334. next_max6675_ms = ms + MAX6675_HEAT_INTERVAL;
  1335. spiBegin();
  1336. spiInit(MAX6675_SPEED_BITS);
  1337. WRITE(MAX6675_SS, 0); // enable TT_MAX6675
  1338. // ensure 100ns delay - a bit extra is fine
  1339. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1340. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1341. // Read a big-endian temperature value
  1342. max6675_temp = 0;
  1343. for (uint8_t i = sizeof(max6675_temp); i--;) {
  1344. max6675_temp |= spiRec();
  1345. if (i > 0) max6675_temp <<= 8; // shift left if not the last byte
  1346. }
  1347. WRITE(MAX6675_SS, 1); // disable TT_MAX6675
  1348. if (max6675_temp & MAX6675_ERROR_MASK) {
  1349. SERIAL_ERROR_START();
  1350. SERIAL_ERRORPGM("Temp measurement error! ");
  1351. #if MAX6675_ERROR_MASK == 7
  1352. SERIAL_ERRORPGM("MAX31855 ");
  1353. if (max6675_temp & 1)
  1354. SERIAL_ERRORLNPGM("Open Circuit");
  1355. else if (max6675_temp & 2)
  1356. SERIAL_ERRORLNPGM("Short to GND");
  1357. else if (max6675_temp & 4)
  1358. SERIAL_ERRORLNPGM("Short to VCC");
  1359. #else
  1360. SERIAL_ERRORLNPGM("MAX6675");
  1361. #endif
  1362. max6675_temp = MAX6675_TMAX * 4; // thermocouple open
  1363. }
  1364. else
  1365. max6675_temp >>= MAX6675_DISCARD_BITS;
  1366. #if ENABLED(MAX6675_IS_MAX31855)
  1367. // Support negative temperature
  1368. if (max6675_temp & 0x00002000) max6675_temp |= 0xFFFFC000;
  1369. #endif
  1370. return (int)max6675_temp;
  1371. }
  1372. #endif // HEATER_0_USES_MAX6675
  1373. /**
  1374. * Get raw temperatures
  1375. */
  1376. void Temperature::set_current_temp_raw() {
  1377. #if HAS_TEMP_0 && DISABLED(HEATER_0_USES_MAX6675)
  1378. current_temperature_raw[0] = raw_temp_value[0];
  1379. #endif
  1380. #if HAS_TEMP_1
  1381. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  1382. redundant_temperature_raw = raw_temp_value[1];
  1383. #else
  1384. current_temperature_raw[1] = raw_temp_value[1];
  1385. #endif
  1386. #if HAS_TEMP_2
  1387. current_temperature_raw[2] = raw_temp_value[2];
  1388. #if HAS_TEMP_3
  1389. current_temperature_raw[3] = raw_temp_value[3];
  1390. #if HAS_TEMP_4
  1391. current_temperature_raw[4] = raw_temp_value[4];
  1392. #endif
  1393. #endif
  1394. #endif
  1395. #endif
  1396. current_temperature_bed_raw = raw_temp_bed_value;
  1397. temp_meas_ready = true;
  1398. }
  1399. /**
  1400. * Timer 0 is shared with millies so don't change the prescaler.
  1401. *
  1402. * This ISR uses the compare method so it runs at the base
  1403. * frequency (16 MHz / 64 / 256 = 976.5625 Hz), but at the TCNT0 set
  1404. * in OCR0B above (128 or halfway between OVFs).
  1405. *
  1406. * - Manage PWM to all the heaters and fan
  1407. * - Prepare or Measure one of the raw ADC sensor values
  1408. * - Check new temperature values for MIN/MAX errors (kill on error)
  1409. * - Step the babysteps value for each axis towards 0
  1410. * - For PINS_DEBUGGING, monitor and report endstop pins
  1411. * - For ENDSTOP_INTERRUPTS_FEATURE check endstops if flagged
  1412. */
  1413. HAL_TEMP_TIMER_ISR {
  1414. HAL_timer_isr_prologue(TEMP_TIMER_NUM);
  1415. Temperature::isr();
  1416. }
  1417. volatile bool Temperature::in_temp_isr = false;
  1418. void Temperature::isr() {
  1419. // The stepper ISR can interrupt this ISR. When it does it re-enables this ISR
  1420. // at the end of its run, potentially causing re-entry. This flag prevents it.
  1421. if (in_temp_isr) return;
  1422. in_temp_isr = true;
  1423. // Allow UART and stepper ISRs
  1424. DISABLE_TEMPERATURE_INTERRUPT(); //Disable Temperature ISR
  1425. #ifndef CPU_32_BIT
  1426. sei();
  1427. #endif
  1428. static int8_t temp_count = -1;
  1429. static ADCSensorState adc_sensor_state = StartupDelay;
  1430. static uint8_t pwm_count = _BV(SOFT_PWM_SCALE);
  1431. // avoid multiple loads of pwm_count
  1432. uint8_t pwm_count_tmp = pwm_count;
  1433. #if ENABLED(ADC_KEYPAD)
  1434. static unsigned int raw_ADCKey_value = 0;
  1435. #endif
  1436. // Static members for each heater
  1437. #if ENABLED(SLOW_PWM_HEATERS)
  1438. static uint8_t slow_pwm_count = 0;
  1439. #define ISR_STATICS(n) \
  1440. static uint8_t soft_pwm_count_ ## n, \
  1441. state_heater_ ## n = 0, \
  1442. state_timer_heater_ ## n = 0
  1443. #else
  1444. #define ISR_STATICS(n) static uint8_t soft_pwm_count_ ## n = 0
  1445. #endif
  1446. // Statics per heater
  1447. ISR_STATICS(0);
  1448. #if HOTENDS > 1
  1449. ISR_STATICS(1);
  1450. #if HOTENDS > 2
  1451. ISR_STATICS(2);
  1452. #if HOTENDS > 3
  1453. ISR_STATICS(3);
  1454. #if HOTENDS > 4
  1455. ISR_STATICS(4);
  1456. #endif // HOTENDS > 4
  1457. #endif // HOTENDS > 3
  1458. #endif // HOTENDS > 2
  1459. #endif // HOTENDS > 1
  1460. #if HAS_HEATER_BED
  1461. ISR_STATICS(BED);
  1462. #endif
  1463. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1464. static unsigned long raw_filwidth_value = 0;
  1465. #endif
  1466. #if DISABLED(SLOW_PWM_HEATERS)
  1467. constexpr uint8_t pwm_mask =
  1468. #if ENABLED(SOFT_PWM_DITHER)
  1469. _BV(SOFT_PWM_SCALE) - 1
  1470. #else
  1471. 0
  1472. #endif
  1473. ;
  1474. /**
  1475. * Standard PWM modulation
  1476. */
  1477. if (pwm_count_tmp >= 127) {
  1478. pwm_count_tmp -= 127;
  1479. soft_pwm_count_0 = (soft_pwm_count_0 & pwm_mask) + soft_pwm_amount[0];
  1480. WRITE_HEATER_0(soft_pwm_count_0 > pwm_mask ? HIGH : LOW);
  1481. #if HOTENDS > 1
  1482. soft_pwm_count_1 = (soft_pwm_count_1 & pwm_mask) + soft_pwm_amount[1];
  1483. WRITE_HEATER_1(soft_pwm_count_1 > pwm_mask ? HIGH : LOW);
  1484. #if HOTENDS > 2
  1485. soft_pwm_count_2 = (soft_pwm_count_2 & pwm_mask) + soft_pwm_amount[2];
  1486. WRITE_HEATER_2(soft_pwm_count_2 > pwm_mask ? HIGH : LOW);
  1487. #if HOTENDS > 3
  1488. soft_pwm_count_3 = (soft_pwm_count_3 & pwm_mask) + soft_pwm_amount[3];
  1489. WRITE_HEATER_3(soft_pwm_count_3 > pwm_mask ? HIGH : LOW);
  1490. #if HOTENDS > 4
  1491. soft_pwm_count_4 = (soft_pwm_count_4 & pwm_mask) + soft_pwm_amount[4];
  1492. WRITE_HEATER_4(soft_pwm_count_4 > pwm_mask ? HIGH : LOW);
  1493. #endif // HOTENDS > 4
  1494. #endif // HOTENDS > 3
  1495. #endif // HOTENDS > 2
  1496. #endif // HOTENDS > 1
  1497. #if HAS_HEATER_BED
  1498. soft_pwm_count_BED = (soft_pwm_count_BED & pwm_mask) + soft_pwm_amount_bed;
  1499. WRITE_HEATER_BED(soft_pwm_count_BED > pwm_mask ? HIGH : LOW);
  1500. #endif
  1501. #if ENABLED(FAN_SOFT_PWM)
  1502. #if HAS_FAN0
  1503. soft_pwm_count_fan[0] = (soft_pwm_count_fan[0] & pwm_mask) + soft_pwm_amount_fan[0] >> 1;
  1504. WRITE_FAN(soft_pwm_count_fan[0] > pwm_mask ? HIGH : LOW);
  1505. #endif
  1506. #if HAS_FAN1
  1507. soft_pwm_count_fan[1] = (soft_pwm_count_fan[1] & pwm_mask) + soft_pwm_amount_fan[1] >> 1;
  1508. WRITE_FAN1(soft_pwm_count_fan[1] > pwm_mask ? HIGH : LOW);
  1509. #endif
  1510. #if HAS_FAN2
  1511. soft_pwm_count_fan[2] = (soft_pwm_count_fan[2] & pwm_mask) + soft_pwm_amount_fan[2] >> 1;
  1512. WRITE_FAN2(soft_pwm_count_fan[2] > pwm_mask ? HIGH : LOW);
  1513. #endif
  1514. #endif
  1515. }
  1516. else {
  1517. if (soft_pwm_count_0 <= pwm_count_tmp) WRITE_HEATER_0(LOW);
  1518. #if HOTENDS > 1
  1519. if (soft_pwm_count_1 <= pwm_count_tmp) WRITE_HEATER_1(LOW);
  1520. #if HOTENDS > 2
  1521. if (soft_pwm_count_2 <= pwm_count_tmp) WRITE_HEATER_2(LOW);
  1522. #if HOTENDS > 3
  1523. if (soft_pwm_count_3 <= pwm_count_tmp) WRITE_HEATER_3(LOW);
  1524. #if HOTENDS > 4
  1525. if (soft_pwm_count_4 <= pwm_count_tmp) WRITE_HEATER_4(LOW);
  1526. #endif // HOTENDS > 4
  1527. #endif // HOTENDS > 3
  1528. #endif // HOTENDS > 2
  1529. #endif // HOTENDS > 1
  1530. #if HAS_HEATER_BED
  1531. if (soft_pwm_count_BED <= pwm_count_tmp) WRITE_HEATER_BED(LOW);
  1532. #endif
  1533. #if ENABLED(FAN_SOFT_PWM)
  1534. #if HAS_FAN0
  1535. if (soft_pwm_count_fan[0] <= pwm_count_tmp) WRITE_FAN(LOW);
  1536. #endif
  1537. #if HAS_FAN1
  1538. if (soft_pwm_count_fan[1] <= pwm_count_tmp) WRITE_FAN1(LOW);
  1539. #endif
  1540. #if HAS_FAN2
  1541. if (soft_pwm_count_fan[2] <= pwm_count_tmp) WRITE_FAN2(LOW);
  1542. #endif
  1543. #endif
  1544. }
  1545. // SOFT_PWM_SCALE to frequency:
  1546. //
  1547. // 0: 16000000/64/256/128 = 7.6294 Hz
  1548. // 1: / 64 = 15.2588 Hz
  1549. // 2: / 32 = 30.5176 Hz
  1550. // 3: / 16 = 61.0352 Hz
  1551. // 4: / 8 = 122.0703 Hz
  1552. // 5: / 4 = 244.1406 Hz
  1553. pwm_count = pwm_count_tmp + _BV(SOFT_PWM_SCALE);
  1554. #else // SLOW_PWM_HEATERS
  1555. /**
  1556. * SLOW PWM HEATERS
  1557. *
  1558. * For relay-driven heaters
  1559. */
  1560. #ifndef MIN_STATE_TIME
  1561. #define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds
  1562. #endif
  1563. // Macros for Slow PWM timer logic
  1564. #define _SLOW_PWM_ROUTINE(NR, src) \
  1565. soft_pwm_count_ ##NR = src; \
  1566. if (soft_pwm_count_ ##NR > 0) { \
  1567. if (state_timer_heater_ ##NR == 0) { \
  1568. if (state_heater_ ##NR == 0) state_timer_heater_ ##NR = MIN_STATE_TIME; \
  1569. state_heater_ ##NR = 1; \
  1570. WRITE_HEATER_ ##NR(1); \
  1571. } \
  1572. } \
  1573. else { \
  1574. if (state_timer_heater_ ##NR == 0) { \
  1575. if (state_heater_ ##NR == 1) state_timer_heater_ ##NR = MIN_STATE_TIME; \
  1576. state_heater_ ##NR = 0; \
  1577. WRITE_HEATER_ ##NR(0); \
  1578. } \
  1579. }
  1580. #define SLOW_PWM_ROUTINE(n) _SLOW_PWM_ROUTINE(n, soft_pwm_amount[n])
  1581. #define PWM_OFF_ROUTINE(NR) \
  1582. if (soft_pwm_count_ ##NR < slow_pwm_count) { \
  1583. if (state_timer_heater_ ##NR == 0) { \
  1584. if (state_heater_ ##NR == 1) state_timer_heater_ ##NR = MIN_STATE_TIME; \
  1585. state_heater_ ##NR = 0; \
  1586. WRITE_HEATER_ ##NR (0); \
  1587. } \
  1588. }
  1589. if (slow_pwm_count == 0) {
  1590. SLOW_PWM_ROUTINE(0);
  1591. #if HOTENDS > 1
  1592. SLOW_PWM_ROUTINE(1);
  1593. #if HOTENDS > 2
  1594. SLOW_PWM_ROUTINE(2);
  1595. #if HOTENDS > 3
  1596. SLOW_PWM_ROUTINE(3);
  1597. #if HOTENDS > 4
  1598. SLOW_PWM_ROUTINE(4);
  1599. #endif // HOTENDS > 4
  1600. #endif // HOTENDS > 3
  1601. #endif // HOTENDS > 2
  1602. #endif // HOTENDS > 1
  1603. #if HAS_HEATER_BED
  1604. _SLOW_PWM_ROUTINE(BED, soft_pwm_amount_bed); // BED
  1605. #endif
  1606. } // slow_pwm_count == 0
  1607. PWM_OFF_ROUTINE(0);
  1608. #if HOTENDS > 1
  1609. PWM_OFF_ROUTINE(1);
  1610. #if HOTENDS > 2
  1611. PWM_OFF_ROUTINE(2);
  1612. #if HOTENDS > 3
  1613. PWM_OFF_ROUTINE(3);
  1614. #if HOTENDS > 4
  1615. PWM_OFF_ROUTINE(4);
  1616. #endif // HOTENDS > 4
  1617. #endif // HOTENDS > 3
  1618. #endif // HOTENDS > 2
  1619. #endif // HOTENDS > 1
  1620. #if HAS_HEATER_BED
  1621. PWM_OFF_ROUTINE(BED); // BED
  1622. #endif
  1623. #if ENABLED(FAN_SOFT_PWM)
  1624. if (pwm_count_tmp >= 127) {
  1625. pwm_count_tmp = 0;
  1626. #if HAS_FAN0
  1627. soft_pwm_count_fan[0] = soft_pwm_amount_fan[0] >> 1;
  1628. WRITE_FAN(soft_pwm_count_fan[0] > 0 ? HIGH : LOW);
  1629. #endif
  1630. #if HAS_FAN1
  1631. soft_pwm_count_fan[1] = soft_pwm_amount_fan[1] >> 1;
  1632. WRITE_FAN1(soft_pwm_count_fan[1] > 0 ? HIGH : LOW);
  1633. #endif
  1634. #if HAS_FAN2
  1635. soft_pwm_count_fan[2] = soft_pwm_amount_fan[2] >> 1;
  1636. WRITE_FAN2(soft_pwm_count_fan[2] > 0 ? HIGH : LOW);
  1637. #endif
  1638. }
  1639. #if HAS_FAN0
  1640. if (soft_pwm_count_fan[0] <= pwm_count_tmp) WRITE_FAN(LOW);
  1641. #endif
  1642. #if HAS_FAN1
  1643. if (soft_pwm_count_fan[1] <= pwm_count_tmp) WRITE_FAN1(LOW);
  1644. #endif
  1645. #if HAS_FAN2
  1646. if (soft_pwm_count_fan[2] <= pwm_count_tmp) WRITE_FAN2(LOW);
  1647. #endif
  1648. #endif // FAN_SOFT_PWM
  1649. // SOFT_PWM_SCALE to frequency:
  1650. //
  1651. // 0: 16000000/64/256/128 = 7.6294 Hz
  1652. // 1: / 64 = 15.2588 Hz
  1653. // 2: / 32 = 30.5176 Hz
  1654. // 3: / 16 = 61.0352 Hz
  1655. // 4: / 8 = 122.0703 Hz
  1656. // 5: / 4 = 244.1406 Hz
  1657. pwm_count = pwm_count_tmp + _BV(SOFT_PWM_SCALE);
  1658. // increment slow_pwm_count only every 64th pwm_count,
  1659. // i.e. yielding a PWM frequency of 16/128 Hz (8s).
  1660. if (((pwm_count >> SOFT_PWM_SCALE) & 0x3F) == 0) {
  1661. slow_pwm_count++;
  1662. slow_pwm_count &= 0x7F;
  1663. if (state_timer_heater_0 > 0) state_timer_heater_0--;
  1664. #if HOTENDS > 1
  1665. if (state_timer_heater_1 > 0) state_timer_heater_1--;
  1666. #if HOTENDS > 2
  1667. if (state_timer_heater_2 > 0) state_timer_heater_2--;
  1668. #if HOTENDS > 3
  1669. if (state_timer_heater_3 > 0) state_timer_heater_3--;
  1670. #if HOTENDS > 4
  1671. if (state_timer_heater_4 > 0) state_timer_heater_4--;
  1672. #endif // HOTENDS > 4
  1673. #endif // HOTENDS > 3
  1674. #endif // HOTENDS > 2
  1675. #endif // HOTENDS > 1
  1676. #if HAS_HEATER_BED
  1677. if (state_timer_heater_BED > 0) state_timer_heater_BED--;
  1678. #endif
  1679. } // ((pwm_count >> SOFT_PWM_SCALE) & 0x3F) == 0
  1680. #endif // SLOW_PWM_HEATERS
  1681. //
  1682. // Update lcd buttons 488 times per second
  1683. //
  1684. static bool do_buttons;
  1685. if ((do_buttons ^= true)) lcd_buttons_update();
  1686. /**
  1687. * One sensor is sampled on every other call of the ISR.
  1688. * Each sensor is read 16 (OVERSAMPLENR) times, taking the average.
  1689. *
  1690. * On each Prepare pass, ADC is started for a sensor pin.
  1691. * On the next pass, the ADC value is read and accumulated.
  1692. *
  1693. * This gives each ADC 0.9765ms to charge up.
  1694. */
  1695. switch (adc_sensor_state) {
  1696. case SensorsReady: {
  1697. // All sensors have been read. Stay in this state for a few
  1698. // ISRs to save on calls to temp update/checking code below.
  1699. constexpr int8_t extra_loops = MIN_ADC_ISR_LOOPS - (int8_t)SensorsReady;
  1700. static uint8_t delay_count = 0;
  1701. if (extra_loops > 0) {
  1702. if (delay_count == 0) delay_count = extra_loops; // Init this delay
  1703. if (--delay_count) // While delaying...
  1704. adc_sensor_state = (ADCSensorState)(int(SensorsReady) - 1); // retain this state (else, next state will be 0)
  1705. break;
  1706. }
  1707. else
  1708. adc_sensor_state = (ADCSensorState)0; // Fall-through to start first sensor now
  1709. }
  1710. #if HAS_TEMP_0
  1711. case PrepareTemp_0:
  1712. HAL_START_ADC(TEMP_0_PIN);
  1713. break;
  1714. case MeasureTemp_0:
  1715. raw_temp_value[0] += HAL_READ_ADC;
  1716. break;
  1717. #endif
  1718. #if HAS_TEMP_BED
  1719. case PrepareTemp_BED:
  1720. HAL_START_ADC(TEMP_BED_PIN);
  1721. break;
  1722. case MeasureTemp_BED:
  1723. raw_temp_bed_value += HAL_READ_ADC;
  1724. break;
  1725. #endif
  1726. #if HAS_TEMP_1
  1727. case PrepareTemp_1:
  1728. HAL_START_ADC(TEMP_1_PIN);
  1729. break;
  1730. case MeasureTemp_1:
  1731. raw_temp_value[1] += HAL_READ_ADC;
  1732. break;
  1733. #endif
  1734. #if HAS_TEMP_2
  1735. case PrepareTemp_2:
  1736. HAL_START_ADC(TEMP_2_PIN);
  1737. break;
  1738. case MeasureTemp_2:
  1739. raw_temp_value[2] += HAL_READ_ADC;
  1740. break;
  1741. #endif
  1742. #if HAS_TEMP_3
  1743. case PrepareTemp_3:
  1744. HAL_START_ADC(TEMP_3_PIN);
  1745. break;
  1746. case MeasureTemp_3:
  1747. raw_temp_value[3] += HAL_READ_ADC;
  1748. break;
  1749. #endif
  1750. #if HAS_TEMP_4
  1751. case PrepareTemp_4:
  1752. HAL_START_ADC(TEMP_4_PIN);
  1753. break;
  1754. case MeasureTemp_4:
  1755. raw_temp_value[4] += HAL_READ_ADC;
  1756. break;
  1757. #endif
  1758. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1759. case Prepare_FILWIDTH:
  1760. HAL_START_ADC(FILWIDTH_PIN);
  1761. break;
  1762. case Measure_FILWIDTH:
  1763. if (HAL_READ_ADC > 102) { // Make sure ADC is reading > 0.5 volts, otherwise don't read.
  1764. raw_filwidth_value -= (raw_filwidth_value >> 7); // Subtract 1/128th of the raw_filwidth_value
  1765. raw_filwidth_value += ((unsigned long)HAL_READ_ADC << 7); // Add new ADC reading, scaled by 128
  1766. }
  1767. break;
  1768. #endif
  1769. #if ENABLED(ADC_KEYPAD)
  1770. case Prepare_ADC_KEY:
  1771. HAL_START_ADC(ADC_KEYPAD_PIN);
  1772. break;
  1773. case Measure_ADC_KEY:
  1774. if (ADCKey_count < 16) {
  1775. raw_ADCKey_value = ADC;
  1776. if (raw_ADCKey_value > 900) {
  1777. //ADC Key release
  1778. ADCKey_count = 0;
  1779. current_ADCKey_raw = 0;
  1780. }
  1781. else {
  1782. current_ADCKey_raw += raw_ADCKey_value;
  1783. ADCKey_count++;
  1784. }
  1785. }
  1786. break;
  1787. #endif // ADC_KEYPAD
  1788. case StartupDelay: break;
  1789. } // switch(adc_sensor_state)
  1790. if (!adc_sensor_state && ++temp_count >= OVERSAMPLENR) { // 10 * 16 * 1/(16000000/64/256) = 164ms.
  1791. temp_count = 0;
  1792. // Update the raw values if they've been read. Else we could be updating them during reading.
  1793. if (!temp_meas_ready) set_current_temp_raw();
  1794. // Filament Sensor - can be read any time since IIR filtering is used
  1795. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1796. current_raw_filwidth = raw_filwidth_value >> 10; // Divide to get to 0-16384 range since we used 1/128 IIR filter approach
  1797. #endif
  1798. ZERO(raw_temp_value);
  1799. raw_temp_bed_value = 0;
  1800. #define TEMPDIR(N) ((HEATER_##N##_RAW_LO_TEMP) > (HEATER_##N##_RAW_HI_TEMP) ? -1 : 1)
  1801. int constexpr temp_dir[] = {
  1802. #if ENABLED(HEATER_0_USES_MAX6675)
  1803. 0
  1804. #else
  1805. TEMPDIR(0)
  1806. #endif
  1807. #if HOTENDS > 1
  1808. , TEMPDIR(1)
  1809. #if HOTENDS > 2
  1810. , TEMPDIR(2)
  1811. #if HOTENDS > 3
  1812. , TEMPDIR(3)
  1813. #if HOTENDS > 4
  1814. , TEMPDIR(4)
  1815. #endif // HOTENDS > 4
  1816. #endif // HOTENDS > 3
  1817. #endif // HOTENDS > 2
  1818. #endif // HOTENDS > 1
  1819. };
  1820. for (uint8_t e = 0; e < COUNT(temp_dir); e++) {
  1821. const int16_t tdir = temp_dir[e], rawtemp = current_temperature_raw[e] * tdir;
  1822. const bool heater_on = 0 <
  1823. #if ENABLED(PIDTEMP)
  1824. soft_pwm_amount[e]
  1825. #else
  1826. target_temperature[e]
  1827. #endif
  1828. ;
  1829. if (rawtemp > maxttemp_raw[e] * tdir && heater_on) max_temp_error(e);
  1830. if (rawtemp < minttemp_raw[e] * tdir && !is_preheating(e) && heater_on) {
  1831. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  1832. if (++consecutive_low_temperature_error[e] >= MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED)
  1833. #endif
  1834. min_temp_error(e);
  1835. }
  1836. #ifdef MAX_CONSECUTIVE_LOW_TEMPERATURE_ERROR_ALLOWED
  1837. else
  1838. consecutive_low_temperature_error[e] = 0;
  1839. #endif
  1840. }
  1841. #if HAS_TEMP_BED
  1842. #if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1843. #define GEBED <=
  1844. #else
  1845. #define GEBED >=
  1846. #endif
  1847. const bool bed_on = 0 <
  1848. #if ENABLED(PIDTEMPBED)
  1849. soft_pwm_amount_bed
  1850. #else
  1851. target_temperature_bed
  1852. #endif
  1853. ;
  1854. if (current_temperature_bed_raw GEBED bed_maxttemp_raw && bed_on) max_temp_error(-1);
  1855. if (bed_minttemp_raw GEBED current_temperature_bed_raw && bed_on) min_temp_error(-1);
  1856. #endif
  1857. } // temp_count >= OVERSAMPLENR
  1858. // Go to the next state, up to SensorsReady
  1859. adc_sensor_state = (ADCSensorState)(int(adc_sensor_state) + 1);
  1860. if (adc_sensor_state > SensorsReady) adc_sensor_state = (ADCSensorState)0;
  1861. #if ENABLED(BABYSTEPPING)
  1862. LOOP_XYZ(axis) {
  1863. const int curTodo = babystepsTodo[axis]; // get rid of volatile for performance
  1864. if (curTodo) {
  1865. stepper.babystep((AxisEnum)axis, curTodo > 0);
  1866. if (curTodo > 0) babystepsTodo[axis]--;
  1867. else babystepsTodo[axis]++;
  1868. }
  1869. }
  1870. #endif // BABYSTEPPING
  1871. #if ENABLED(PINS_DEBUGGING)
  1872. endstops.run_monitor(); // report changes in endstop status
  1873. #endif
  1874. #if ENABLED(ENDSTOP_INTERRUPTS_FEATURE)
  1875. extern volatile uint8_t e_hit;
  1876. if (e_hit && ENDSTOPS_ENABLED) {
  1877. endstops.update(); // call endstop update routine
  1878. e_hit--;
  1879. }
  1880. #endif
  1881. #ifndef CPU_32_BIT
  1882. cli();
  1883. #endif
  1884. in_temp_isr = false;
  1885. ENABLE_TEMPERATURE_INTERRUPT(); //re-enable Temperature ISR
  1886. }
  1887. #if HAS_TEMP_HOTEND || HAS_TEMP_BED
  1888. #include "../gcode/gcode.h"
  1889. void print_heater_state(const float &c, const float &t,
  1890. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  1891. const float r,
  1892. #endif
  1893. const int8_t e=-2
  1894. ) {
  1895. #if !(HAS_TEMP_BED && HAS_TEMP_HOTEND) && HOTENDS <= 1
  1896. UNUSED(e);
  1897. #endif
  1898. SERIAL_PROTOCOLCHAR(' ');
  1899. SERIAL_PROTOCOLCHAR(
  1900. #if HAS_TEMP_BED && HAS_TEMP_HOTEND
  1901. e == -1 ? 'B' : 'T'
  1902. #elif HAS_TEMP_HOTEND
  1903. 'T'
  1904. #else
  1905. 'B'
  1906. #endif
  1907. );
  1908. #if HOTENDS > 1
  1909. if (e >= 0) SERIAL_PROTOCOLCHAR('0' + e);
  1910. #endif
  1911. SERIAL_PROTOCOLCHAR(':');
  1912. SERIAL_PROTOCOL(c);
  1913. SERIAL_PROTOCOLPAIR(" /" , t);
  1914. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  1915. SERIAL_PROTOCOLPAIR(" (", r / OVERSAMPLENR);
  1916. SERIAL_PROTOCOLCHAR(')');
  1917. #endif
  1918. }
  1919. void Temperature::print_heaterstates() {
  1920. #if HAS_TEMP_HOTEND
  1921. print_heater_state(degHotend(gcode.target_extruder), degTargetHotend(gcode.target_extruder)
  1922. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  1923. , rawHotendTemp(gcode.target_extruder)
  1924. #endif
  1925. );
  1926. #endif
  1927. #if HAS_TEMP_BED
  1928. print_heater_state(degBed(), degTargetBed(),
  1929. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  1930. rawBedTemp(),
  1931. #endif
  1932. -1 // BED
  1933. );
  1934. #endif
  1935. #if HOTENDS > 1
  1936. HOTEND_LOOP() print_heater_state(degHotend(e), degTargetHotend(e),
  1937. #if ENABLED(SHOW_TEMP_ADC_VALUES)
  1938. rawHotendTemp(e),
  1939. #endif
  1940. e
  1941. );
  1942. #endif
  1943. SERIAL_PROTOCOLPGM(" @:");
  1944. SERIAL_PROTOCOL(getHeaterPower(gcode.target_extruder));
  1945. #if HAS_TEMP_BED
  1946. SERIAL_PROTOCOLPGM(" B@:");
  1947. SERIAL_PROTOCOL(getHeaterPower(-1));
  1948. #endif
  1949. #if HOTENDS > 1
  1950. HOTEND_LOOP() {
  1951. SERIAL_PROTOCOLPAIR(" @", e);
  1952. SERIAL_PROTOCOLCHAR(':');
  1953. SERIAL_PROTOCOL(getHeaterPower(e));
  1954. }
  1955. #endif
  1956. }
  1957. #if ENABLED(AUTO_REPORT_TEMPERATURES)
  1958. uint8_t Temperature::auto_report_temp_interval;
  1959. millis_t Temperature::next_temp_report_ms;
  1960. void Temperature::auto_report_temperatures() {
  1961. if (auto_report_temp_interval && ELAPSED(millis(), next_temp_report_ms)) {
  1962. next_temp_report_ms = millis() + 1000UL * auto_report_temp_interval;
  1963. print_heaterstates();
  1964. SERIAL_EOL();
  1965. }
  1966. }
  1967. #endif // AUTO_REPORT_TEMPERATURES
  1968. #endif // HAS_TEMP_HOTEND || HAS_TEMP_BED