My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

temperature.cpp 74KB

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