My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

temperature.cpp 64KB

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