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

temperature.cpp 48KB

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