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

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