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

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