My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

temperature.cpp 57KB

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