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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793
  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 "language.h"
  29. #include "Sd2PinMap.h"
  30. #if ENABLED(USE_WATCHDOG)
  31. #include "watchdog.h"
  32. #endif
  33. #ifdef K1 // Defined in Configuration.h in the PID settings
  34. #define K2 (1.0-K1)
  35. #endif
  36. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  37. static void* heater_ttbl_map[2] = {(void*)HEATER_0_TEMPTABLE, (void*)HEATER_1_TEMPTABLE };
  38. static uint8_t heater_ttbllen_map[2] = { HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN };
  39. #else
  40. static void* heater_ttbl_map[EXTRUDERS] = ARRAY_BY_EXTRUDERS((void*)HEATER_0_TEMPTABLE, (void*)HEATER_1_TEMPTABLE, (void*)HEATER_2_TEMPTABLE, (void*)HEATER_3_TEMPTABLE);
  41. static uint8_t heater_ttbllen_map[EXTRUDERS] = ARRAY_BY_EXTRUDERS(HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN, HEATER_2_TEMPTABLE_LEN, HEATER_3_TEMPTABLE_LEN);
  42. #endif
  43. Temperature thermalManager;
  44. // public:
  45. int Temperature::current_temperature_raw[EXTRUDERS] = { 0 };
  46. float Temperature::current_temperature[EXTRUDERS] = { 0.0 };
  47. int Temperature::target_temperature[EXTRUDERS] = { 0 };
  48. int Temperature::current_temperature_bed_raw = 0;
  49. float Temperature::current_temperature_bed = 0.0;
  50. int Temperature::target_temperature_bed = 0;
  51. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  52. float Temperature::redundant_temperature = 0.0;
  53. #endif
  54. unsigned char Temperature::soft_pwm_bed;
  55. #if ENABLED(FAN_SOFT_PWM)
  56. unsigned char Temperature::fanSpeedSoftPwm[FAN_COUNT];
  57. #endif
  58. #if ENABLED(PIDTEMP)
  59. #if ENABLED(PID_PARAMS_PER_EXTRUDER)
  60. float Temperature::Kp[EXTRUDERS] = ARRAY_BY_EXTRUDERS1(DEFAULT_Kp),
  61. Temperature::Ki[EXTRUDERS] = ARRAY_BY_EXTRUDERS1((DEFAULT_Ki) * (PID_dT)),
  62. Temperature::Kd[EXTRUDERS] = ARRAY_BY_EXTRUDERS1((DEFAULT_Kd) / (PID_dT));
  63. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  64. float Temperature::Kc[EXTRUDERS] = ARRAY_BY_EXTRUDERS1(DEFAULT_Kc);
  65. #endif
  66. #else
  67. float Temperature::Kp = DEFAULT_Kp,
  68. Temperature::Ki = (DEFAULT_Ki) * (PID_dT),
  69. Temperature::Kd = (DEFAULT_Kd) / (PID_dT);
  70. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  71. float Temperature::Kc = DEFAULT_Kc;
  72. #endif
  73. #endif
  74. #endif
  75. #if ENABLED(PIDTEMPBED)
  76. float Temperature::bedKp = DEFAULT_bedKp,
  77. Temperature::bedKi = ((DEFAULT_bedKi) * PID_dT),
  78. Temperature::bedKd = ((DEFAULT_bedKd) / PID_dT);
  79. #endif
  80. #if ENABLED(BABYSTEPPING)
  81. volatile int Temperature::babystepsTodo[3] = { 0 };
  82. #endif
  83. #if ENABLED(THERMAL_PROTECTION_HOTENDS) && WATCH_TEMP_PERIOD > 0
  84. int Temperature::watch_target_temp[EXTRUDERS] = { 0 };
  85. millis_t Temperature::watch_heater_next_ms[EXTRUDERS] = { 0 };
  86. #endif
  87. #if ENABLED(THERMAL_PROTECTION_HOTENDS) && WATCH_BED_TEMP_PERIOD > 0
  88. int Temperature::watch_target_bed_temp = 0;
  89. millis_t Temperature::watch_bed_next_ms = 0;
  90. #endif
  91. #if ENABLED(PREVENT_DANGEROUS_EXTRUDE)
  92. float Temperature::extrude_min_temp = EXTRUDE_MINTEMP;
  93. #endif
  94. // private:
  95. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  96. int Temperature::redundant_temperature_raw = 0;
  97. float Temperature::redundant_temperature = 0.0;
  98. #endif
  99. volatile bool Temperature::temp_meas_ready = false;
  100. #if ENABLED(PIDTEMP)
  101. float Temperature::temp_iState[EXTRUDERS] = { 0 };
  102. float Temperature::temp_dState[EXTRUDERS] = { 0 };
  103. float Temperature::pTerm[EXTRUDERS];
  104. float Temperature::iTerm[EXTRUDERS];
  105. float Temperature::dTerm[EXTRUDERS];
  106. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  107. float Temperature::cTerm[EXTRUDERS];
  108. long Temperature::last_position[EXTRUDERS];
  109. long Temperature::lpq[LPQ_MAX_LEN];
  110. int Temperature::lpq_ptr = 0;
  111. #endif
  112. float Temperature::pid_error[EXTRUDERS];
  113. float Temperature::temp_iState_min[EXTRUDERS];
  114. float Temperature::temp_iState_max[EXTRUDERS];
  115. bool Temperature::pid_reset[EXTRUDERS];
  116. #endif
  117. #if ENABLED(PIDTEMPBED)
  118. float Temperature::temp_iState_bed = { 0 };
  119. float Temperature::temp_dState_bed = { 0 };
  120. float Temperature::pTerm_bed;
  121. float Temperature::iTerm_bed;
  122. float Temperature::dTerm_bed;
  123. float Temperature::pid_error_bed;
  124. float Temperature::temp_iState_min_bed;
  125. float Temperature::temp_iState_max_bed;
  126. #else
  127. millis_t Temperature::next_bed_check_ms;
  128. #endif
  129. unsigned long Temperature::raw_temp_value[4] = { 0 };
  130. unsigned long Temperature::raw_temp_bed_value = 0;
  131. // Init min and max temp with extreme values to prevent false errors during startup
  132. int Temperature::minttemp_raw[EXTRUDERS] = ARRAY_BY_EXTRUDERS(HEATER_0_RAW_LO_TEMP , HEATER_1_RAW_LO_TEMP , HEATER_2_RAW_LO_TEMP, HEATER_3_RAW_LO_TEMP);
  133. int Temperature::maxttemp_raw[EXTRUDERS] = ARRAY_BY_EXTRUDERS(HEATER_0_RAW_HI_TEMP , HEATER_1_RAW_HI_TEMP , HEATER_2_RAW_HI_TEMP, HEATER_3_RAW_HI_TEMP);
  134. int Temperature::minttemp[EXTRUDERS] = { 0 };
  135. int Temperature::maxttemp[EXTRUDERS] = ARRAY_BY_EXTRUDERS1(16383);
  136. #ifdef BED_MINTEMP
  137. int Temperature::bed_minttemp_raw = HEATER_BED_RAW_LO_TEMP;
  138. #endif
  139. #ifdef BED_MAXTEMP
  140. int Temperature::bed_maxttemp_raw = HEATER_BED_RAW_HI_TEMP;
  141. #endif
  142. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  143. int Temperature::meas_shift_index; // Index of a delayed sample in buffer
  144. #endif
  145. #if HAS_AUTO_FAN
  146. millis_t Temperature::next_auto_fan_check_ms;
  147. #endif
  148. unsigned char Temperature::soft_pwm[EXTRUDERS];
  149. #if ENABLED(FAN_SOFT_PWM)
  150. unsigned char Temperature::soft_pwm_fan[FAN_COUNT];
  151. #endif
  152. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  153. int Temperature::current_raw_filwidth = 0; //Holds measured filament diameter - one extruder only
  154. #endif
  155. #if HAS_PID_HEATING
  156. void Temperature::PID_autotune(float temp, int extruder, int ncycles, bool set_result/*=false*/) {
  157. float input = 0.0;
  158. int cycles = 0;
  159. bool heating = true;
  160. millis_t temp_ms = millis(), t1 = temp_ms, t2 = temp_ms;
  161. long t_high = 0, t_low = 0;
  162. long bias, d;
  163. float Ku, Tu;
  164. float workKp = 0, workKi = 0, workKd = 0;
  165. float max = 0, min = 10000;
  166. #if HAS_AUTO_FAN
  167. next_auto_fan_check_ms = temp_ms + 2500UL;
  168. #endif
  169. if (false
  170. #if ENABLED(PIDTEMP)
  171. || extruder >= EXTRUDERS
  172. #else
  173. || extruder >= 0
  174. #endif
  175. #if DISABLED(PIDTEMPBED)
  176. || extruder < 0
  177. #endif
  178. ) {
  179. SERIAL_ECHOLN(MSG_PID_BAD_EXTRUDER_NUM);
  180. return;
  181. }
  182. SERIAL_ECHOLN(MSG_PID_AUTOTUNE_START);
  183. disable_all_heaters(); // switch off all heaters.
  184. #if HAS_PID_FOR_BOTH
  185. if (extruder < 0)
  186. soft_pwm_bed = bias = d = (MAX_BED_POWER) / 2;
  187. else
  188. soft_pwm[extruder] = bias = d = (PID_MAX) / 2;
  189. #elif ENABLED(PIDTEMP)
  190. soft_pwm[extruder] = bias = d = (PID_MAX) / 2;
  191. #else
  192. soft_pwm_bed = bias = d = (MAX_BED_POWER) / 2;
  193. #endif
  194. // PID Tuning loop
  195. for (;;) {
  196. millis_t ms = millis();
  197. if (temp_meas_ready) { // temp sample ready
  198. updateTemperaturesFromRawValues();
  199. input =
  200. #if HAS_PID_FOR_BOTH
  201. extruder < 0 ? current_temperature_bed : current_temperature[extruder]
  202. #elif ENABLED(PIDTEMP)
  203. current_temperature[extruder]
  204. #else
  205. current_temperature_bed
  206. #endif
  207. ;
  208. max = max(max, input);
  209. min = min(min, input);
  210. #if HAS_AUTO_FAN
  211. if (ELAPSED(ms, next_auto_fan_check_ms)) {
  212. checkExtruderAutoFans();
  213. next_auto_fan_check_ms = ms + 2500UL;
  214. }
  215. #endif
  216. if (heating && input > temp) {
  217. if (ELAPSED(ms, t2 + 5000UL)) {
  218. heating = false;
  219. #if HAS_PID_FOR_BOTH
  220. if (extruder < 0)
  221. soft_pwm_bed = (bias - d) >> 1;
  222. else
  223. soft_pwm[extruder] = (bias - d) >> 1;
  224. #elif ENABLED(PIDTEMP)
  225. soft_pwm[extruder] = (bias - d) >> 1;
  226. #elif ENABLED(PIDTEMPBED)
  227. soft_pwm_bed = (bias - d) >> 1;
  228. #endif
  229. t1 = ms;
  230. t_high = t1 - t2;
  231. max = temp;
  232. }
  233. }
  234. if (!heating && input < temp) {
  235. if (ELAPSED(ms, t1 + 5000UL)) {
  236. heating = true;
  237. t2 = ms;
  238. t_low = t2 - t1;
  239. if (cycles > 0) {
  240. long max_pow =
  241. #if HAS_PID_FOR_BOTH
  242. extruder < 0 ? MAX_BED_POWER : PID_MAX
  243. #elif ENABLED(PIDTEMP)
  244. PID_MAX
  245. #else
  246. MAX_BED_POWER
  247. #endif
  248. ;
  249. bias += (d * (t_high - t_low)) / (t_low + t_high);
  250. bias = constrain(bias, 20, max_pow - 20);
  251. d = (bias > max_pow / 2) ? max_pow - 1 - bias : bias;
  252. SERIAL_PROTOCOLPGM(MSG_BIAS); SERIAL_PROTOCOL(bias);
  253. SERIAL_PROTOCOLPGM(MSG_D); SERIAL_PROTOCOL(d);
  254. SERIAL_PROTOCOLPGM(MSG_T_MIN); SERIAL_PROTOCOL(min);
  255. SERIAL_PROTOCOLPGM(MSG_T_MAX); SERIAL_PROTOCOLLN(max);
  256. if (cycles > 2) {
  257. Ku = (4.0 * d) / (3.14159265 * (max - min) / 2.0);
  258. Tu = ((float)(t_low + t_high) / 1000.0);
  259. SERIAL_PROTOCOLPGM(MSG_KU); SERIAL_PROTOCOL(Ku);
  260. SERIAL_PROTOCOLPGM(MSG_TU); SERIAL_PROTOCOLLN(Tu);
  261. workKp = 0.6 * Ku;
  262. workKi = 2 * workKp / Tu;
  263. workKd = workKp * Tu / 8;
  264. SERIAL_PROTOCOLLNPGM(MSG_CLASSIC_PID);
  265. SERIAL_PROTOCOLPGM(MSG_KP); SERIAL_PROTOCOLLN(workKp);
  266. SERIAL_PROTOCOLPGM(MSG_KI); SERIAL_PROTOCOLLN(workKi);
  267. SERIAL_PROTOCOLPGM(MSG_KD); SERIAL_PROTOCOLLN(workKd);
  268. /**
  269. workKp = 0.33*Ku;
  270. workKi = workKp/Tu;
  271. workKd = workKp*Tu/3;
  272. SERIAL_PROTOCOLLNPGM(" Some overshoot ");
  273. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(workKp);
  274. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(workKi);
  275. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(workKd);
  276. workKp = 0.2*Ku;
  277. workKi = 2*workKp/Tu;
  278. workKd = workKp*Tu/3;
  279. SERIAL_PROTOCOLLNPGM(" No overshoot ");
  280. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(workKp);
  281. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(workKi);
  282. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(workKd);
  283. */
  284. }
  285. }
  286. #if HAS_PID_FOR_BOTH
  287. if (extruder < 0)
  288. soft_pwm_bed = (bias + d) >> 1;
  289. else
  290. soft_pwm[extruder] = (bias + d) >> 1;
  291. #elif ENABLED(PIDTEMP)
  292. soft_pwm[extruder] = (bias + d) >> 1;
  293. #else
  294. soft_pwm_bed = (bias + d) >> 1;
  295. #endif
  296. cycles++;
  297. min = temp;
  298. }
  299. }
  300. }
  301. #define MAX_OVERSHOOT_PID_AUTOTUNE 20
  302. if (input > temp + MAX_OVERSHOOT_PID_AUTOTUNE) {
  303. SERIAL_PROTOCOLLNPGM(MSG_PID_TEMP_TOO_HIGH);
  304. return;
  305. }
  306. // Every 2 seconds...
  307. if (ELAPSED(ms, temp_ms + 2000UL)) {
  308. #if HAS_TEMP_HOTEND || HAS_TEMP_BED
  309. print_heaterstates();
  310. SERIAL_EOL;
  311. #endif
  312. temp_ms = ms;
  313. } // every 2 seconds
  314. // Over 2 minutes?
  315. if (((ms - t1) + (ms - t2)) > (10L * 60L * 1000L * 2L)) {
  316. SERIAL_PROTOCOLLNPGM(MSG_PID_TIMEOUT);
  317. return;
  318. }
  319. if (cycles > ncycles) {
  320. SERIAL_PROTOCOLLNPGM(MSG_PID_AUTOTUNE_FINISHED);
  321. #if HAS_PID_FOR_BOTH
  322. const char* estring = extruder < 0 ? "bed" : "";
  323. SERIAL_PROTOCOLPGM("#define DEFAULT_"); SERIAL_PROTOCOL(estring); SERIAL_PROTOCOLPGM("Kp "); SERIAL_PROTOCOLLN(workKp);
  324. SERIAL_PROTOCOLPGM("#define DEFAULT_"); SERIAL_PROTOCOL(estring); SERIAL_PROTOCOLPGM("Ki "); SERIAL_PROTOCOLLN(workKi);
  325. SERIAL_PROTOCOLPGM("#define DEFAULT_"); SERIAL_PROTOCOL(estring); SERIAL_PROTOCOLPGM("Kd "); SERIAL_PROTOCOLLN(workKd);
  326. #elif ENABLED(PIDTEMP)
  327. SERIAL_PROTOCOLPGM("#define DEFAULT_Kp "); SERIAL_PROTOCOLLN(workKp);
  328. SERIAL_PROTOCOLPGM("#define DEFAULT_Ki "); SERIAL_PROTOCOLLN(workKi);
  329. SERIAL_PROTOCOLPGM("#define DEFAULT_Kd "); SERIAL_PROTOCOLLN(workKd);
  330. #else
  331. SERIAL_PROTOCOLPGM("#define DEFAULT_bedKp "); SERIAL_PROTOCOLLN(workKp);
  332. SERIAL_PROTOCOLPGM("#define DEFAULT_bedKi "); SERIAL_PROTOCOLLN(workKi);
  333. SERIAL_PROTOCOLPGM("#define DEFAULT_bedKd "); SERIAL_PROTOCOLLN(workKd);
  334. #endif
  335. #define _SET_BED_PID() \
  336. bedKp = workKp; \
  337. bedKi = scalePID_i(workKi); \
  338. bedKd = scalePID_d(workKd); \
  339. updatePID()
  340. #define _SET_EXTRUDER_PID() \
  341. PID_PARAM(Kp, extruder) = workKp; \
  342. PID_PARAM(Ki, extruder) = scalePID_i(workKi); \
  343. PID_PARAM(Kd, extruder) = scalePID_d(workKd); \
  344. updatePID()
  345. // Use the result? (As with "M303 U1")
  346. if (set_result) {
  347. #if HAS_PID_FOR_BOTH
  348. if (extruder < 0) {
  349. _SET_BED_PID();
  350. }
  351. else {
  352. _SET_EXTRUDER_PID();
  353. }
  354. #elif ENABLED(PIDTEMP)
  355. _SET_EXTRUDER_PID();
  356. #else
  357. _SET_BED_PID();
  358. #endif
  359. }
  360. return;
  361. }
  362. lcd_update();
  363. }
  364. }
  365. #endif // HAS_PID_HEATING
  366. /**
  367. * Class and Instance Methods
  368. */
  369. Temperature::Temperature() { }
  370. void Temperature::updatePID() {
  371. #if ENABLED(PIDTEMP)
  372. for (int e = 0; e < EXTRUDERS; e++) {
  373. temp_iState_max[e] = (PID_INTEGRAL_DRIVE_MAX) / PID_PARAM(Ki, e);
  374. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  375. last_position[e] = 0;
  376. #endif
  377. }
  378. #endif
  379. #if ENABLED(PIDTEMPBED)
  380. temp_iState_max_bed = (PID_BED_INTEGRAL_DRIVE_MAX) / bedKi;
  381. #endif
  382. }
  383. int Temperature::getHeaterPower(int heater) {
  384. return heater < 0 ? soft_pwm_bed : soft_pwm[heater];
  385. }
  386. #if HAS_AUTO_FAN
  387. void Temperature::checkExtruderAutoFans() {
  388. const int8_t fanPin[] = { EXTRUDER_0_AUTO_FAN_PIN, EXTRUDER_1_AUTO_FAN_PIN, EXTRUDER_2_AUTO_FAN_PIN, EXTRUDER_3_AUTO_FAN_PIN };
  389. const int fanBit[] = { 0,
  390. EXTRUDER_1_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN ? 0 : 1,
  391. EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN ? 0 :
  392. EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_1_AUTO_FAN_PIN ? 1 : 2,
  393. EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN ? 0 :
  394. EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_1_AUTO_FAN_PIN ? 1 :
  395. EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_2_AUTO_FAN_PIN ? 2 : 3
  396. };
  397. uint8_t fanState = 0;
  398. for (int f = 0; f <= EXTRUDERS; f++) {
  399. if (current_temperature[f] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  400. SBI(fanState, fanBit[f]);
  401. }
  402. uint8_t fanDone = 0;
  403. for (int f = 0; f <= 3; f++) {
  404. int8_t pin = fanPin[f];
  405. if (pin >= 0 && !TEST(fanDone, fanBit[f])) {
  406. unsigned char 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(uint8_t e) {
  437. _temp_error(e, PSTR(MSG_T_MAXTEMP), PSTR(MSG_ERR_MAXTEMP));
  438. }
  439. void Temperature::min_temp_error(uint8_t e) {
  440. _temp_error(e, PSTR(MSG_T_MINTEMP), PSTR(MSG_ERR_MINTEMP));
  441. }
  442. float Temperature::get_pid_output(int e) {
  443. float pid_output;
  444. #if ENABLED(PIDTEMP)
  445. #if DISABLED(PID_OPENLOOP)
  446. pid_error[e] = target_temperature[e] - current_temperature[e];
  447. dTerm[e] = K2 * PID_PARAM(Kd, e) * (current_temperature[e] - temp_dState[e]) + K1 * dTerm[e];
  448. temp_dState[e] = current_temperature[e];
  449. if (pid_error[e] > PID_FUNCTIONAL_RANGE) {
  450. pid_output = BANG_MAX;
  451. pid_reset[e] = true;
  452. }
  453. else if (pid_error[e] < -(PID_FUNCTIONAL_RANGE) || target_temperature[e] == 0) {
  454. pid_output = 0;
  455. pid_reset[e] = true;
  456. }
  457. else {
  458. if (pid_reset[e]) {
  459. temp_iState[e] = 0.0;
  460. pid_reset[e] = false;
  461. }
  462. pTerm[e] = PID_PARAM(Kp, e) * pid_error[e];
  463. temp_iState[e] += pid_error[e];
  464. temp_iState[e] = constrain(temp_iState[e], temp_iState_min[e], temp_iState_max[e]);
  465. iTerm[e] = PID_PARAM(Ki, e) * temp_iState[e];
  466. pid_output = pTerm[e] + iTerm[e] - dTerm[e];
  467. #if ENABLED(SINGLENOZZLE)
  468. #define _NOZZLE_TEST true
  469. #define _NOZZLE_EXTRUDER active_extruder
  470. #define _CTERM_INDEX 0
  471. #else
  472. #define _NOZZLE_TEST e == active_extruder
  473. #define _NOZZLE_EXTRUDER e
  474. #define _CTERM_INDEX e
  475. #endif
  476. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  477. cTerm[_CTERM_INDEX] = 0;
  478. if (_NOZZLE_TEST) {
  479. long e_position = stepper.position(E_AXIS);
  480. if (e_position > last_position[_NOZZLE_EXTRUDER]) {
  481. lpq[lpq_ptr++] = e_position - last_position[_NOZZLE_EXTRUDER];
  482. last_position[_NOZZLE_EXTRUDER] = e_position;
  483. }
  484. else {
  485. lpq[lpq_ptr++] = 0;
  486. }
  487. if (lpq_ptr >= lpq_len) lpq_ptr = 0;
  488. cTerm[_CTERM_INDEX] = (lpq[lpq_ptr] / planner.axis_steps_per_unit[E_AXIS]) * PID_PARAM(Kc, e);
  489. pid_output += cTerm[e];
  490. }
  491. #endif //PID_ADD_EXTRUSION_RATE
  492. if (pid_output > PID_MAX) {
  493. if (pid_error[e] > 0) temp_iState[e] -= pid_error[e]; // conditional un-integration
  494. pid_output = PID_MAX;
  495. }
  496. else if (pid_output < 0) {
  497. if (pid_error[e] < 0) temp_iState[e] -= pid_error[e]; // conditional un-integration
  498. pid_output = 0;
  499. }
  500. }
  501. #else
  502. pid_output = constrain(target_temperature[e], 0, PID_MAX);
  503. #endif //PID_OPENLOOP
  504. #if ENABLED(PID_DEBUG)
  505. SERIAL_ECHO_START;
  506. SERIAL_ECHOPAIR(MSG_PID_DEBUG, e);
  507. SERIAL_ECHOPAIR(MSG_PID_DEBUG_INPUT, current_temperature[e]);
  508. SERIAL_ECHOPAIR(MSG_PID_DEBUG_OUTPUT, pid_output);
  509. SERIAL_ECHOPAIR(MSG_PID_DEBUG_PTERM, pTerm[e]);
  510. SERIAL_ECHOPAIR(MSG_PID_DEBUG_ITERM, iTerm[e]);
  511. SERIAL_ECHOPAIR(MSG_PID_DEBUG_DTERM, dTerm[e]);
  512. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  513. SERIAL_ECHOPAIR(MSG_PID_DEBUG_CTERM, cTerm[e]);
  514. #endif
  515. SERIAL_EOL;
  516. #endif //PID_DEBUG
  517. #else /* PID off */
  518. pid_output = (current_temperature[e] < target_temperature[e]) ? PID_MAX : 0;
  519. #endif
  520. return pid_output;
  521. }
  522. #if ENABLED(PIDTEMPBED)
  523. float Temperature::get_pid_output_bed() {
  524. float pid_output;
  525. #if DISABLED(PID_OPENLOOP)
  526. pid_error_bed = target_temperature_bed - current_temperature_bed;
  527. pTerm_bed = bedKp * pid_error_bed;
  528. temp_iState_bed += pid_error_bed;
  529. temp_iState_bed = constrain(temp_iState_bed, temp_iState_min_bed, temp_iState_max_bed);
  530. iTerm_bed = bedKi * temp_iState_bed;
  531. dTerm_bed = K2 * bedKd * (current_temperature_bed - temp_dState_bed) + K1 * dTerm_bed;
  532. temp_dState_bed = current_temperature_bed;
  533. pid_output = pTerm_bed + iTerm_bed - dTerm_bed;
  534. if (pid_output > MAX_BED_POWER) {
  535. if (pid_error_bed > 0) temp_iState_bed -= pid_error_bed; // conditional un-integration
  536. pid_output = MAX_BED_POWER;
  537. }
  538. else if (pid_output < 0) {
  539. if (pid_error_bed < 0) temp_iState_bed -= pid_error_bed; // conditional un-integration
  540. pid_output = 0;
  541. }
  542. #else
  543. pid_output = constrain(target_temperature_bed, 0, MAX_BED_POWER);
  544. #endif // PID_OPENLOOP
  545. #if ENABLED(PID_BED_DEBUG)
  546. SERIAL_ECHO_START;
  547. SERIAL_ECHO(" PID_BED_DEBUG ");
  548. SERIAL_ECHO(": Input ");
  549. SERIAL_ECHO(current_temperature_bed);
  550. SERIAL_ECHO(" Output ");
  551. SERIAL_ECHO(pid_output);
  552. SERIAL_ECHO(" pTerm ");
  553. SERIAL_ECHO(pTerm_bed);
  554. SERIAL_ECHO(" iTerm ");
  555. SERIAL_ECHO(iTerm_bed);
  556. SERIAL_ECHO(" dTerm ");
  557. SERIAL_ECHOLN(dTerm_bed);
  558. #endif //PID_BED_DEBUG
  559. return pid_output;
  560. }
  561. #endif //PIDTEMPBED
  562. /**
  563. * Manage heating activities for extruder hot-ends and a heated bed
  564. * - Acquire updated temperature readings
  565. * - Also resets the watchdog timer
  566. * - Invoke thermal runaway protection
  567. * - Manage extruder auto-fan
  568. * - Apply filament width to the extrusion rate (may move)
  569. * - Update the heated bed PID output value
  570. */
  571. void Temperature::manage_heater() {
  572. if (!temp_meas_ready) return;
  573. updateTemperaturesFromRawValues(); // also resets the watchdog
  574. #if ENABLED(HEATER_0_USES_MAX6675)
  575. float ct = current_temperature[0];
  576. if (ct > min(HEATER_0_MAXTEMP, 1023)) max_temp_error(0);
  577. if (ct < 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 extruders
  583. for (int e = 0; e < EXTRUDERS; e++) {
  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] && 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. } // Extruders 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_index1 - 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) / 100.0, 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 > EXTRUDERS)
  686. #else
  687. if (e >= EXTRUDERS)
  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. for (uint8_t e = 0; e < EXTRUDERS; e++) {
  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 extruder arrays
  796. for (int e = 0; e < EXTRUDERS; e++) {
  797. // populate with the first value
  798. maxttemp[e] = maxttemp[0];
  799. #if ENABLED(PIDTEMP)
  800. temp_iState_min[e] = 0.0;
  801. temp_iState_max[e] = (PID_INTEGRAL_DRIVE_MAX) / PID_PARAM(Ki, e);
  802. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  803. last_position[e] = 0;
  804. #endif
  805. #endif //PIDTEMP
  806. #if ENABLED(PIDTEMPBED)
  807. temp_iState_min_bed = 0.0;
  808. temp_iState_max_bed = (PID_BED_INTEGRAL_DRIVE_MAX) / bedKi;
  809. #endif //PIDTEMPBED
  810. }
  811. #if HAS_HEATER_0
  812. SET_OUTPUT(HEATER_0_PIN);
  813. #endif
  814. #if HAS_HEATER_1
  815. SET_OUTPUT(HEATER_1_PIN);
  816. #endif
  817. #if HAS_HEATER_2
  818. SET_OUTPUT(HEATER_2_PIN);
  819. #endif
  820. #if HAS_HEATER_3
  821. SET_OUTPUT(HEATER_3_PIN);
  822. #endif
  823. #if HAS_HEATER_BED
  824. SET_OUTPUT(HEATER_BED_PIN);
  825. #endif
  826. #if ENABLED(FAST_PWM_FAN) || ENABLED(FAN_SOFT_PWM)
  827. #if HAS_FAN0
  828. SET_OUTPUT(FAN_PIN);
  829. #if ENABLED(FAST_PWM_FAN)
  830. setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  831. #endif
  832. #if ENABLED(FAN_SOFT_PWM)
  833. soft_pwm_fan[0] = fanSpeedSoftPwm[0] / 2;
  834. #endif
  835. #endif
  836. #if HAS_FAN1
  837. SET_OUTPUT(FAN1_PIN);
  838. #if ENABLED(FAST_PWM_FAN)
  839. setPwmFrequency(FAN1_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  840. #endif
  841. #if ENABLED(FAN_SOFT_PWM)
  842. soft_pwm_fan[1] = fanSpeedSoftPwm[1] / 2;
  843. #endif
  844. #endif
  845. #if HAS_FAN2
  846. SET_OUTPUT(FAN2_PIN);
  847. #if ENABLED(FAST_PWM_FAN)
  848. setPwmFrequency(FAN2_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  849. #endif
  850. #if ENABLED(FAN_SOFT_PWM)
  851. soft_pwm_fan[2] = fanSpeedSoftPwm[2] / 2;
  852. #endif
  853. #endif
  854. #endif // FAST_PWM_FAN || FAN_SOFT_PWM
  855. #if ENABLED(HEATER_0_USES_MAX6675)
  856. #if DISABLED(SDSUPPORT)
  857. OUT_WRITE(SCK_PIN, LOW);
  858. OUT_WRITE(MOSI_PIN, HIGH);
  859. OUT_WRITE(MISO_PIN, HIGH);
  860. #else
  861. pinMode(SS_PIN, OUTPUT);
  862. digitalWrite(SS_PIN, HIGH);
  863. #endif
  864. OUT_WRITE(MAX6675_SS, HIGH);
  865. #endif //HEATER_0_USES_MAX6675
  866. #ifdef DIDR2
  867. #define ANALOG_SELECT(pin) do{ if (pin < 8) SBI(DIDR0, pin); else SBI(DIDR2, pin - 8); }while(0)
  868. #else
  869. #define ANALOG_SELECT(pin) do{ SBI(DIDR0, pin); }while(0)
  870. #endif
  871. // Set analog inputs
  872. ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADIF) | 0x07;
  873. DIDR0 = 0;
  874. #ifdef DIDR2
  875. DIDR2 = 0;
  876. #endif
  877. #if HAS_TEMP_0
  878. ANALOG_SELECT(TEMP_0_PIN);
  879. #endif
  880. #if HAS_TEMP_1
  881. ANALOG_SELECT(TEMP_1_PIN);
  882. #endif
  883. #if HAS_TEMP_2
  884. ANALOG_SELECT(TEMP_2_PIN);
  885. #endif
  886. #if HAS_TEMP_3
  887. ANALOG_SELECT(TEMP_3_PIN);
  888. #endif
  889. #if HAS_TEMP_BED
  890. ANALOG_SELECT(TEMP_BED_PIN);
  891. #endif
  892. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  893. ANALOG_SELECT(FILWIDTH_PIN);
  894. #endif
  895. #if HAS_AUTO_FAN_0
  896. pinMode(EXTRUDER_0_AUTO_FAN_PIN, OUTPUT);
  897. #endif
  898. #if HAS_AUTO_FAN_1 && (EXTRUDER_1_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN)
  899. pinMode(EXTRUDER_1_AUTO_FAN_PIN, OUTPUT);
  900. #endif
  901. #if HAS_AUTO_FAN_2 && (EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN) && (EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_1_AUTO_FAN_PIN)
  902. pinMode(EXTRUDER_2_AUTO_FAN_PIN, OUTPUT);
  903. #endif
  904. #if HAS_AUTO_FAN_3 && (EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN) && (EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_1_AUTO_FAN_PIN) && (EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_2_AUTO_FAN_PIN)
  905. pinMode(EXTRUDER_3_AUTO_FAN_PIN, OUTPUT);
  906. #endif
  907. // Use timer0 for temperature measurement
  908. // Interleave temperature interrupt with millies interrupt
  909. OCR0B = 128;
  910. SBI(TIMSK0, OCIE0B);
  911. // Wait for temperature measurement to settle
  912. delay(250);
  913. #define TEMP_MIN_ROUTINE(NR) \
  914. minttemp[NR] = HEATER_ ## NR ## _MINTEMP; \
  915. while(analog2temp(minttemp_raw[NR], NR) < HEATER_ ## NR ## _MINTEMP) { \
  916. if (HEATER_ ## NR ## _RAW_LO_TEMP < HEATER_ ## NR ## _RAW_HI_TEMP) \
  917. minttemp_raw[NR] += OVERSAMPLENR; \
  918. else \
  919. minttemp_raw[NR] -= OVERSAMPLENR; \
  920. }
  921. #define TEMP_MAX_ROUTINE(NR) \
  922. maxttemp[NR] = HEATER_ ## NR ## _MAXTEMP; \
  923. while(analog2temp(maxttemp_raw[NR], NR) > HEATER_ ## NR ## _MAXTEMP) { \
  924. if (HEATER_ ## NR ## _RAW_LO_TEMP < HEATER_ ## NR ## _RAW_HI_TEMP) \
  925. maxttemp_raw[NR] -= OVERSAMPLENR; \
  926. else \
  927. maxttemp_raw[NR] += OVERSAMPLENR; \
  928. }
  929. #ifdef HEATER_0_MINTEMP
  930. TEMP_MIN_ROUTINE(0);
  931. #endif
  932. #ifdef HEATER_0_MAXTEMP
  933. TEMP_MAX_ROUTINE(0);
  934. #endif
  935. #if EXTRUDERS > 1
  936. #ifdef HEATER_1_MINTEMP
  937. TEMP_MIN_ROUTINE(1);
  938. #endif
  939. #ifdef HEATER_1_MAXTEMP
  940. TEMP_MAX_ROUTINE(1);
  941. #endif
  942. #if EXTRUDERS > 2
  943. #ifdef HEATER_2_MINTEMP
  944. TEMP_MIN_ROUTINE(2);
  945. #endif
  946. #ifdef HEATER_2_MAXTEMP
  947. TEMP_MAX_ROUTINE(2);
  948. #endif
  949. #if EXTRUDERS > 3
  950. #ifdef HEATER_3_MINTEMP
  951. TEMP_MIN_ROUTINE(3);
  952. #endif
  953. #ifdef HEATER_3_MAXTEMP
  954. TEMP_MAX_ROUTINE(3);
  955. #endif
  956. #endif // EXTRUDERS > 3
  957. #endif // EXTRUDERS > 2
  958. #endif // EXTRUDERS > 1
  959. #ifdef BED_MINTEMP
  960. while(analog2tempBed(bed_minttemp_raw) < BED_MINTEMP) {
  961. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  962. bed_minttemp_raw += OVERSAMPLENR;
  963. #else
  964. bed_minttemp_raw -= OVERSAMPLENR;
  965. #endif
  966. }
  967. #endif //BED_MINTEMP
  968. #ifdef BED_MAXTEMP
  969. while (analog2tempBed(bed_maxttemp_raw) > BED_MAXTEMP) {
  970. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  971. bed_maxttemp_raw -= OVERSAMPLENR;
  972. #else
  973. bed_maxttemp_raw += OVERSAMPLENR;
  974. #endif
  975. }
  976. #endif //BED_MAXTEMP
  977. }
  978. #if ENABLED(THERMAL_PROTECTION_HOTENDS) && WATCH_TEMP_PERIOD > 0
  979. /**
  980. * Start Heating Sanity Check for hotends that are below
  981. * their target temperature by a configurable margin.
  982. * This is called when the temperature is set. (M104, M109)
  983. */
  984. void Temperature::start_watching_heater(int e) {
  985. if (degHotend(e) < degTargetHotend(e) - (WATCH_TEMP_INCREASE + TEMP_HYSTERESIS + 1)) {
  986. watch_target_temp[e] = degHotend(e) + WATCH_TEMP_INCREASE;
  987. watch_heater_next_ms[e] = millis() + (WATCH_TEMP_PERIOD) * 1000UL;
  988. }
  989. else
  990. watch_heater_next_ms[e] = 0;
  991. }
  992. #endif
  993. #if ENABLED(THERMAL_PROTECTION_BED) && WATCH_BED_TEMP_PERIOD > 0
  994. /**
  995. * Start Heating Sanity Check for hotends that are below
  996. * their target temperature by a configurable margin.
  997. * This is called when the temperature is set. (M140, M190)
  998. */
  999. void Temperature::start_watching_bed() {
  1000. if (degBed() < degTargetBed() - (WATCH_BED_TEMP_INCREASE + TEMP_BED_HYSTERESIS + 1)) {
  1001. watch_target_bed_temp = degBed() + WATCH_BED_TEMP_INCREASE;
  1002. watch_bed_next_ms = millis() + (WATCH_BED_TEMP_PERIOD) * 1000UL;
  1003. }
  1004. else
  1005. watch_bed_next_ms = 0;
  1006. }
  1007. #endif
  1008. #if ENABLED(THERMAL_PROTECTION_HOTENDS) || HAS_THERMALLY_PROTECTED_BED
  1009. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  1010. Temperature::TRState Temperature::thermal_runaway_state_machine[EXTRUDERS] = { TRInactive };
  1011. millis_t Temperature::thermal_runaway_timer[EXTRUDERS] = { 0 };
  1012. #endif
  1013. #if HAS_THERMALLY_PROTECTED_BED
  1014. Temperature::TRState Temperature::thermal_runaway_bed_state_machine = TRInactive;
  1015. millis_t Temperature::thermal_runaway_bed_timer;
  1016. #endif
  1017. 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) {
  1018. static float tr_target_temperature[EXTRUDERS + 1] = { 0.0 };
  1019. /**
  1020. SERIAL_ECHO_START;
  1021. SERIAL_ECHOPGM("Thermal Thermal Runaway Running. Heater ID: ");
  1022. if (heater_id < 0) SERIAL_ECHOPGM("bed"); else SERIAL_ECHO(heater_id);
  1023. SERIAL_ECHOPAIR(" ; State:", *state);
  1024. SERIAL_ECHOPAIR(" ; Timer:", *timer);
  1025. SERIAL_ECHOPAIR(" ; Temperature:", temperature);
  1026. SERIAL_ECHOPAIR(" ; Target Temp:", target_temperature);
  1027. SERIAL_EOL;
  1028. */
  1029. int heater_index = heater_id >= 0 ? heater_id : EXTRUDERS;
  1030. // If the target temperature changes, restart
  1031. if (tr_target_temperature[heater_index] != target_temperature) {
  1032. tr_target_temperature[heater_index] = target_temperature;
  1033. *state = target_temperature > 0 ? TRFirstHeating : TRInactive;
  1034. }
  1035. switch (*state) {
  1036. // Inactive state waits for a target temperature to be set
  1037. case TRInactive: break;
  1038. // When first heating, wait for the temperature to be reached then go to Stable state
  1039. case TRFirstHeating:
  1040. if (temperature < tr_target_temperature[heater_index]) break;
  1041. *state = TRStable;
  1042. // While the temperature is stable watch for a bad temperature
  1043. case TRStable:
  1044. if (temperature < tr_target_temperature[heater_index] - hysteresis_degc && ELAPSED(millis(), *timer))
  1045. *state = TRRunaway;
  1046. else {
  1047. *timer = millis() + period_seconds * 1000UL;
  1048. break;
  1049. }
  1050. case TRRunaway:
  1051. _temp_error(heater_id, PSTR(MSG_T_THERMAL_RUNAWAY), PSTR(MSG_THERMAL_RUNAWAY));
  1052. }
  1053. }
  1054. #endif // THERMAL_PROTECTION_HOTENDS || THERMAL_PROTECTION_BED
  1055. void Temperature::disable_all_heaters() {
  1056. for (int i = 0; i < EXTRUDERS; i++) setTargetHotend(0, i);
  1057. setTargetBed(0);
  1058. // If all heaters go down then for sure our print job has stopped
  1059. print_job_timer.stop();
  1060. #define DISABLE_HEATER(NR) { \
  1061. setTargetHotend(NR, 0); \
  1062. soft_pwm[NR] = 0; \
  1063. WRITE_HEATER_ ## NR (LOW); \
  1064. }
  1065. #if HAS_TEMP_HOTEND
  1066. setTargetHotend(0, 0);
  1067. soft_pwm[0] = 0;
  1068. WRITE_HEATER_0P(LOW); // Should HEATERS_PARALLEL apply here? Then change to DISABLE_HEATER(0)
  1069. #endif
  1070. #if EXTRUDERS > 1 && HAS_TEMP_1
  1071. DISABLE_HEATER(1);
  1072. #endif
  1073. #if EXTRUDERS > 2 && HAS_TEMP_2
  1074. DISABLE_HEATER(2);
  1075. #endif
  1076. #if EXTRUDERS > 3 && HAS_TEMP_3
  1077. DISABLE_HEATER(3);
  1078. #endif
  1079. #if HAS_TEMP_BED
  1080. target_temperature_bed = 0;
  1081. soft_pwm_bed = 0;
  1082. #if HAS_HEATER_BED
  1083. WRITE_HEATER_BED(LOW);
  1084. #endif
  1085. #endif
  1086. }
  1087. #if ENABLED(HEATER_0_USES_MAX6675)
  1088. #define MAX6675_HEAT_INTERVAL 250u
  1089. #if ENABLED(MAX6675_IS_MAX31855)
  1090. uint32_t max6675_temp = 2000;
  1091. #define MAX6675_ERROR_MASK 7
  1092. #define MAX6675_DISCARD_BITS 18
  1093. #else
  1094. uint16_t max6675_temp = 2000;
  1095. #define MAX6675_ERROR_MASK 4
  1096. #define MAX6675_DISCARD_BITS 3
  1097. #endif
  1098. int Temperature::read_max6675() {
  1099. static millis_t next_max6675_ms = 0;
  1100. millis_t ms = millis();
  1101. if (PENDING(ms, next_max6675_ms)) return (int)max6675_temp;
  1102. next_max6675_ms = ms + MAX6675_HEAT_INTERVAL;
  1103. CBI(
  1104. #ifdef PRR
  1105. PRR
  1106. #elif defined(PRR0)
  1107. PRR0
  1108. #endif
  1109. , PRSPI);
  1110. SPCR = _BV(MSTR) | _BV(SPE) | _BV(SPR0);
  1111. WRITE(MAX6675_SS, 0); // enable TT_MAX6675
  1112. // ensure 100ns delay - a bit extra is fine
  1113. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1114. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1115. // Read a big-endian temperature value
  1116. max6675_temp = 0;
  1117. for (uint8_t i = sizeof(max6675_temp); i--;) {
  1118. SPDR = 0;
  1119. for (;!TEST(SPSR, SPIF););
  1120. max6675_temp |= SPDR;
  1121. if (i > 0) max6675_temp <<= 8; // shift left if not the last byte
  1122. }
  1123. WRITE(MAX6675_SS, 1); // disable TT_MAX6675
  1124. if (max6675_temp & MAX6675_ERROR_MASK)
  1125. max6675_temp = 4000; // thermocouple open
  1126. else
  1127. max6675_temp >>= MAX6675_DISCARD_BITS;
  1128. return (int)max6675_temp;
  1129. }
  1130. #endif //HEATER_0_USES_MAX6675
  1131. /**
  1132. * Stages in the ISR loop
  1133. */
  1134. enum TempState {
  1135. PrepareTemp_0,
  1136. MeasureTemp_0,
  1137. PrepareTemp_BED,
  1138. MeasureTemp_BED,
  1139. PrepareTemp_1,
  1140. MeasureTemp_1,
  1141. PrepareTemp_2,
  1142. MeasureTemp_2,
  1143. PrepareTemp_3,
  1144. MeasureTemp_3,
  1145. Prepare_FILWIDTH,
  1146. Measure_FILWIDTH,
  1147. StartupDelay // Startup, delay initial temp reading a tiny bit so the hardware can settle
  1148. };
  1149. /**
  1150. * Get raw temperatures
  1151. */
  1152. void Temperature::set_current_temp_raw() {
  1153. #if HAS_TEMP_0 && DISABLED(HEATER_0_USES_MAX6675)
  1154. current_temperature_raw[0] = raw_temp_value[0];
  1155. #endif
  1156. #if HAS_TEMP_1
  1157. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  1158. redundant_temperature_raw = raw_temp_value[1];
  1159. #else
  1160. current_temperature_raw[1] = raw_temp_value[1];
  1161. #endif
  1162. #if HAS_TEMP_2
  1163. current_temperature_raw[2] = raw_temp_value[2];
  1164. #if HAS_TEMP_3
  1165. current_temperature_raw[3] = raw_temp_value[3];
  1166. #endif
  1167. #endif
  1168. #endif
  1169. current_temperature_bed_raw = raw_temp_bed_value;
  1170. temp_meas_ready = true;
  1171. }
  1172. /**
  1173. * Timer 0 is shared with millies
  1174. * - Manage PWM to all the heaters and fan
  1175. * - Update the raw temperature values
  1176. * - Check new temperature values for MIN/MAX errors
  1177. * - Step the babysteps value for each axis towards 0
  1178. */
  1179. ISR(TIMER0_COMPB_vect) { Temperature::isr(); }
  1180. void Temperature::isr() {
  1181. static unsigned char temp_count = 0;
  1182. static TempState temp_state = StartupDelay;
  1183. static unsigned char pwm_count = _BV(SOFT_PWM_SCALE);
  1184. // Static members for each heater
  1185. #if ENABLED(SLOW_PWM_HEATERS)
  1186. static unsigned char slow_pwm_count = 0;
  1187. #define ISR_STATICS(n) \
  1188. static unsigned char soft_pwm_ ## n; \
  1189. static unsigned char state_heater_ ## n = 0; \
  1190. static unsigned char state_timer_heater_ ## n = 0
  1191. #else
  1192. #define ISR_STATICS(n) static unsigned char soft_pwm_ ## n
  1193. #endif
  1194. // Statics per heater
  1195. ISR_STATICS(0);
  1196. #if (EXTRUDERS > 1) || ENABLED(HEATERS_PARALLEL)
  1197. ISR_STATICS(1);
  1198. #if EXTRUDERS > 2
  1199. ISR_STATICS(2);
  1200. #if EXTRUDERS > 3
  1201. ISR_STATICS(3);
  1202. #endif
  1203. #endif
  1204. #endif
  1205. #if HAS_HEATER_BED
  1206. ISR_STATICS(BED);
  1207. #endif
  1208. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1209. static unsigned long raw_filwidth_value = 0;
  1210. #endif
  1211. #if DISABLED(SLOW_PWM_HEATERS)
  1212. /**
  1213. * standard PWM modulation
  1214. */
  1215. if (pwm_count == 0) {
  1216. soft_pwm_0 = soft_pwm[0];
  1217. if (soft_pwm_0 > 0) {
  1218. WRITE_HEATER_0(1);
  1219. }
  1220. else WRITE_HEATER_0P(0); // If HEATERS_PARALLEL should apply, change to WRITE_HEATER_0
  1221. #if EXTRUDERS > 1
  1222. soft_pwm_1 = soft_pwm[1];
  1223. WRITE_HEATER_1(soft_pwm_1 > 0 ? 1 : 0);
  1224. #if EXTRUDERS > 2
  1225. soft_pwm_2 = soft_pwm[2];
  1226. WRITE_HEATER_2(soft_pwm_2 > 0 ? 1 : 0);
  1227. #if EXTRUDERS > 3
  1228. soft_pwm_3 = soft_pwm[3];
  1229. WRITE_HEATER_3(soft_pwm_3 > 0 ? 1 : 0);
  1230. #endif
  1231. #endif
  1232. #endif
  1233. #if HAS_HEATER_BED
  1234. soft_pwm_BED = soft_pwm_bed;
  1235. WRITE_HEATER_BED(soft_pwm_BED > 0 ? 1 : 0);
  1236. #endif
  1237. #if ENABLED(FAN_SOFT_PWM)
  1238. #if HAS_FAN0
  1239. soft_pwm_fan[0] = fanSpeedSoftPwm[0] / 2;
  1240. WRITE_FAN(soft_pwm_fan[0] > 0 ? 1 : 0);
  1241. #endif
  1242. #if HAS_FAN1
  1243. soft_pwm_fan[1] = fanSpeedSoftPwm[1] / 2;
  1244. WRITE_FAN1(soft_pwm_fan[1] > 0 ? 1 : 0);
  1245. #endif
  1246. #if HAS_FAN2
  1247. soft_pwm_fan[2] = fanSpeedSoftPwm[2] / 2;
  1248. WRITE_FAN2(soft_pwm_fan[2] > 0 ? 1 : 0);
  1249. #endif
  1250. #endif
  1251. }
  1252. if (soft_pwm_0 < pwm_count) WRITE_HEATER_0(0);
  1253. #if EXTRUDERS > 1
  1254. if (soft_pwm_1 < pwm_count) WRITE_HEATER_1(0);
  1255. #if EXTRUDERS > 2
  1256. if (soft_pwm_2 < pwm_count) WRITE_HEATER_2(0);
  1257. #if EXTRUDERS > 3
  1258. if (soft_pwm_3 < pwm_count) WRITE_HEATER_3(0);
  1259. #endif
  1260. #endif
  1261. #endif
  1262. #if HAS_HEATER_BED
  1263. if (soft_pwm_BED < pwm_count) WRITE_HEATER_BED(0);
  1264. #endif
  1265. #if ENABLED(FAN_SOFT_PWM)
  1266. #if HAS_FAN0
  1267. if (soft_pwm_fan[0] < pwm_count) WRITE_FAN(0);
  1268. #endif
  1269. #if HAS_FAN1
  1270. if (soft_pwm_fan[1] < pwm_count) WRITE_FAN1(0);
  1271. #endif
  1272. #if HAS_FAN2
  1273. if (soft_pwm_fan[2] < pwm_count) WRITE_FAN2(0);
  1274. #endif
  1275. #endif
  1276. pwm_count += _BV(SOFT_PWM_SCALE);
  1277. pwm_count &= 0x7f;
  1278. #else // SLOW_PWM_HEATERS
  1279. /**
  1280. * SLOW PWM HEATERS
  1281. *
  1282. * for heaters drived by relay
  1283. */
  1284. #ifndef MIN_STATE_TIME
  1285. #define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds
  1286. #endif
  1287. // Macros for Slow PWM timer logic - HEATERS_PARALLEL applies
  1288. #define _SLOW_PWM_ROUTINE(NR, src) \
  1289. soft_pwm_ ## NR = src; \
  1290. if (soft_pwm_ ## NR > 0) { \
  1291. if (state_timer_heater_ ## NR == 0) { \
  1292. if (state_heater_ ## NR == 0) state_timer_heater_ ## NR = MIN_STATE_TIME; \
  1293. state_heater_ ## NR = 1; \
  1294. WRITE_HEATER_ ## NR(1); \
  1295. } \
  1296. } \
  1297. else { \
  1298. if (state_timer_heater_ ## NR == 0) { \
  1299. if (state_heater_ ## NR == 1) state_timer_heater_ ## NR = MIN_STATE_TIME; \
  1300. state_heater_ ## NR = 0; \
  1301. WRITE_HEATER_ ## NR(0); \
  1302. } \
  1303. }
  1304. #define SLOW_PWM_ROUTINE(n) _SLOW_PWM_ROUTINE(n, soft_pwm[n])
  1305. #define PWM_OFF_ROUTINE(NR) \
  1306. if (soft_pwm_ ## NR < slow_pwm_count) { \
  1307. if (state_timer_heater_ ## NR == 0) { \
  1308. if (state_heater_ ## NR == 1) state_timer_heater_ ## NR = MIN_STATE_TIME; \
  1309. state_heater_ ## NR = 0; \
  1310. WRITE_HEATER_ ## NR (0); \
  1311. } \
  1312. }
  1313. if (slow_pwm_count == 0) {
  1314. SLOW_PWM_ROUTINE(0); // EXTRUDER 0
  1315. #if EXTRUDERS > 1
  1316. SLOW_PWM_ROUTINE(1); // EXTRUDER 1
  1317. #if EXTRUDERS > 2
  1318. SLOW_PWM_ROUTINE(2); // EXTRUDER 2
  1319. #if EXTRUDERS > 3
  1320. SLOW_PWM_ROUTINE(3); // EXTRUDER 3
  1321. #endif
  1322. #endif
  1323. #endif
  1324. #if HAS_HEATER_BED
  1325. _SLOW_PWM_ROUTINE(BED, soft_pwm_bed); // BED
  1326. #endif
  1327. } // slow_pwm_count == 0
  1328. PWM_OFF_ROUTINE(0); // EXTRUDER 0
  1329. #if EXTRUDERS > 1
  1330. PWM_OFF_ROUTINE(1); // EXTRUDER 1
  1331. #if EXTRUDERS > 2
  1332. PWM_OFF_ROUTINE(2); // EXTRUDER 2
  1333. #if EXTRUDERS > 3
  1334. PWM_OFF_ROUTINE(3); // EXTRUDER 3
  1335. #endif
  1336. #endif
  1337. #endif
  1338. #if HAS_HEATER_BED
  1339. PWM_OFF_ROUTINE(BED); // BED
  1340. #endif
  1341. #if ENABLED(FAN_SOFT_PWM)
  1342. if (pwm_count == 0) {
  1343. #if HAS_FAN0
  1344. soft_pwm_fan[0] = fanSpeedSoftPwm[0] / 2;
  1345. WRITE_FAN(soft_pwm_fan[0] > 0 ? 1 : 0);
  1346. #endif
  1347. #if HAS_FAN1
  1348. soft_pwm_fan[1] = fanSpeedSoftPwm[1] / 2;
  1349. WRITE_FAN1(soft_pwm_fan[1] > 0 ? 1 : 0);
  1350. #endif
  1351. #if HAS_FAN2
  1352. soft_pwm_fan[2] = fanSpeedSoftPwm[2] / 2;
  1353. WRITE_FAN2(soft_pwm_fan[2] > 0 ? 1 : 0);
  1354. #endif
  1355. }
  1356. #if HAS_FAN0
  1357. if (soft_pwm_fan[0] < pwm_count) WRITE_FAN(0);
  1358. #endif
  1359. #if HAS_FAN1
  1360. if (soft_pwm_fan[1] < pwm_count) WRITE_FAN1(0);
  1361. #endif
  1362. #if HAS_FAN2
  1363. if (soft_pwm_fan[2] < pwm_count) WRITE_FAN2(0);
  1364. #endif
  1365. #endif //FAN_SOFT_PWM
  1366. pwm_count += _BV(SOFT_PWM_SCALE);
  1367. pwm_count &= 0x7f;
  1368. // increment slow_pwm_count only every 64 pwm_count circa 65.5ms
  1369. if ((pwm_count % 64) == 0) {
  1370. slow_pwm_count++;
  1371. slow_pwm_count &= 0x7f;
  1372. // EXTRUDER 0
  1373. if (state_timer_heater_0 > 0) state_timer_heater_0--;
  1374. #if EXTRUDERS > 1 // EXTRUDER 1
  1375. if (state_timer_heater_1 > 0) state_timer_heater_1--;
  1376. #if EXTRUDERS > 2 // EXTRUDER 2
  1377. if (state_timer_heater_2 > 0) state_timer_heater_2--;
  1378. #if EXTRUDERS > 3 // EXTRUDER 3
  1379. if (state_timer_heater_3 > 0) state_timer_heater_3--;
  1380. #endif
  1381. #endif
  1382. #endif
  1383. #if HAS_HEATER_BED
  1384. if (state_timer_heater_BED > 0) state_timer_heater_BED--;
  1385. #endif
  1386. } // (pwm_count % 64) == 0
  1387. #endif // SLOW_PWM_HEATERS
  1388. #define SET_ADMUX_ADCSRA(pin) ADMUX = _BV(REFS0) | (pin & 0x07); SBI(ADCSRA, ADSC)
  1389. #ifdef MUX5
  1390. #define START_ADC(pin) if (pin > 7) ADCSRB = _BV(MUX5); else ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
  1391. #else
  1392. #define START_ADC(pin) ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
  1393. #endif
  1394. // Prepare or measure a sensor, each one every 12th frame
  1395. switch (temp_state) {
  1396. case PrepareTemp_0:
  1397. #if HAS_TEMP_0
  1398. START_ADC(TEMP_0_PIN);
  1399. #endif
  1400. lcd_buttons_update();
  1401. temp_state = MeasureTemp_0;
  1402. break;
  1403. case MeasureTemp_0:
  1404. #if HAS_TEMP_0
  1405. raw_temp_value[0] += ADC;
  1406. #endif
  1407. temp_state = PrepareTemp_BED;
  1408. break;
  1409. case PrepareTemp_BED:
  1410. #if HAS_TEMP_BED
  1411. START_ADC(TEMP_BED_PIN);
  1412. #endif
  1413. lcd_buttons_update();
  1414. temp_state = MeasureTemp_BED;
  1415. break;
  1416. case MeasureTemp_BED:
  1417. #if HAS_TEMP_BED
  1418. raw_temp_bed_value += ADC;
  1419. #endif
  1420. temp_state = PrepareTemp_1;
  1421. break;
  1422. case PrepareTemp_1:
  1423. #if HAS_TEMP_1
  1424. START_ADC(TEMP_1_PIN);
  1425. #endif
  1426. lcd_buttons_update();
  1427. temp_state = MeasureTemp_1;
  1428. break;
  1429. case MeasureTemp_1:
  1430. #if HAS_TEMP_1
  1431. raw_temp_value[1] += ADC;
  1432. #endif
  1433. temp_state = PrepareTemp_2;
  1434. break;
  1435. case PrepareTemp_2:
  1436. #if HAS_TEMP_2
  1437. START_ADC(TEMP_2_PIN);
  1438. #endif
  1439. lcd_buttons_update();
  1440. temp_state = MeasureTemp_2;
  1441. break;
  1442. case MeasureTemp_2:
  1443. #if HAS_TEMP_2
  1444. raw_temp_value[2] += ADC;
  1445. #endif
  1446. temp_state = PrepareTemp_3;
  1447. break;
  1448. case PrepareTemp_3:
  1449. #if HAS_TEMP_3
  1450. START_ADC(TEMP_3_PIN);
  1451. #endif
  1452. lcd_buttons_update();
  1453. temp_state = MeasureTemp_3;
  1454. break;
  1455. case MeasureTemp_3:
  1456. #if HAS_TEMP_3
  1457. raw_temp_value[3] += ADC;
  1458. #endif
  1459. temp_state = Prepare_FILWIDTH;
  1460. break;
  1461. case Prepare_FILWIDTH:
  1462. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1463. START_ADC(FILWIDTH_PIN);
  1464. #endif
  1465. lcd_buttons_update();
  1466. temp_state = Measure_FILWIDTH;
  1467. break;
  1468. case Measure_FILWIDTH:
  1469. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1470. // raw_filwidth_value += ADC; //remove to use an IIR filter approach
  1471. if (ADC > 102) { //check that ADC is reading a voltage > 0.5 volts, otherwise don't take in the data.
  1472. raw_filwidth_value -= (raw_filwidth_value >> 7); //multiply raw_filwidth_value by 127/128
  1473. raw_filwidth_value += ((unsigned long)ADC << 7); //add new ADC reading
  1474. }
  1475. #endif
  1476. temp_state = PrepareTemp_0;
  1477. temp_count++;
  1478. break;
  1479. case StartupDelay:
  1480. temp_state = PrepareTemp_0;
  1481. break;
  1482. // default:
  1483. // SERIAL_ERROR_START;
  1484. // SERIAL_ERRORLNPGM("Temp measurement error!");
  1485. // break;
  1486. } // switch(temp_state)
  1487. if (temp_count >= OVERSAMPLENR) { // 10 * 16 * 1/(16000000/64/256) = 164ms.
  1488. // Update the raw values if they've been read. Else we could be updating them during reading.
  1489. if (!temp_meas_ready) set_current_temp_raw();
  1490. // Filament Sensor - can be read any time since IIR filtering is used
  1491. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1492. current_raw_filwidth = raw_filwidth_value >> 10; // Divide to get to 0-16384 range since we used 1/128 IIR filter approach
  1493. #endif
  1494. temp_count = 0;
  1495. for (int i = 0; i < 4; i++) raw_temp_value[i] = 0;
  1496. raw_temp_bed_value = 0;
  1497. #if HAS_TEMP_0 && DISABLED(HEATER_0_USES_MAX6675)
  1498. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1499. #define GE0 <=
  1500. #else
  1501. #define GE0 >=
  1502. #endif
  1503. if (current_temperature_raw[0] GE0 maxttemp_raw[0]) max_temp_error(0);
  1504. if (minttemp_raw[0] GE0 current_temperature_raw[0]) min_temp_error(0);
  1505. #endif
  1506. #if HAS_TEMP_1 && EXTRUDERS > 1
  1507. #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
  1508. #define GE1 <=
  1509. #else
  1510. #define GE1 >=
  1511. #endif
  1512. if (current_temperature_raw[1] GE1 maxttemp_raw[1]) max_temp_error(1);
  1513. if (minttemp_raw[1] GE1 current_temperature_raw[1]) min_temp_error(1);
  1514. #endif // TEMP_SENSOR_1
  1515. #if HAS_TEMP_2 && EXTRUDERS > 2
  1516. #if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
  1517. #define GE2 <=
  1518. #else
  1519. #define GE2 >=
  1520. #endif
  1521. if (current_temperature_raw[2] GE2 maxttemp_raw[2]) max_temp_error(2);
  1522. if (minttemp_raw[2] GE2 current_temperature_raw[2]) min_temp_error(2);
  1523. #endif // TEMP_SENSOR_2
  1524. #if HAS_TEMP_3 && EXTRUDERS > 3
  1525. #if HEATER_3_RAW_LO_TEMP > HEATER_3_RAW_HI_TEMP
  1526. #define GE3 <=
  1527. #else
  1528. #define GE3 >=
  1529. #endif
  1530. if (current_temperature_raw[3] GE3 maxttemp_raw[3]) max_temp_error(3);
  1531. if (minttemp_raw[3] GE3 current_temperature_raw[3]) min_temp_error(3);
  1532. #endif // TEMP_SENSOR_3
  1533. #if HAS_TEMP_BED
  1534. #if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1535. #define GEBED <=
  1536. #else
  1537. #define GEBED >=
  1538. #endif
  1539. if (current_temperature_bed_raw GEBED bed_maxttemp_raw) _temp_error(-1, PSTR(MSG_T_MAXTEMP), PSTR(MSG_ERR_MAXTEMP_BED));
  1540. if (bed_minttemp_raw GEBED current_temperature_bed_raw) _temp_error(-1, PSTR(MSG_T_MINTEMP), PSTR(MSG_ERR_MINTEMP_BED));
  1541. #endif
  1542. } // temp_count >= OVERSAMPLENR
  1543. #if ENABLED(BABYSTEPPING)
  1544. for (uint8_t axis = X_AXIS; axis <= Z_AXIS; axis++) {
  1545. int curTodo = babystepsTodo[axis]; //get rid of volatile for performance
  1546. if (curTodo > 0) {
  1547. stepper.babystep(axis,/*fwd*/true);
  1548. babystepsTodo[axis]--; //fewer to do next time
  1549. }
  1550. else if (curTodo < 0) {
  1551. stepper.babystep(axis,/*fwd*/false);
  1552. babystepsTodo[axis]++; //fewer to do next time
  1553. }
  1554. }
  1555. #endif //BABYSTEPPING
  1556. }