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

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