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

temperature.cpp 55KB

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