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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626
  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. if (e >= 0) SERIAL_ERRORLN((int)e);
  395. serialprintPGM(serial_msg);
  396. MYSERIAL.write('\n');
  397. #ifdef ULTRA_LCD
  398. lcd_setalertstatuspgm(lcd_msg);
  399. #endif
  400. }
  401. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  402. Stop();
  403. #endif
  404. }
  405. void max_temp_error(uint8_t e) {
  406. disable_all_heaters();
  407. _temp_error(e, PSTR(MSG_MAXTEMP_EXTRUDER_OFF), PSTR(MSG_ERR_MAXTEMP));
  408. }
  409. void min_temp_error(uint8_t e) {
  410. disable_all_heaters();
  411. _temp_error(e, PSTR(MSG_MINTEMP_EXTRUDER_OFF), PSTR(MSG_ERR_MINTEMP));
  412. }
  413. void bed_max_temp_error(void) {
  414. #if HAS_HEATER_BED
  415. WRITE_HEATER_BED(0);
  416. #endif
  417. _temp_error(-1, PSTR(MSG_MAXTEMP_BED_OFF), PSTR(MSG_ERR_MAXTEMP_BED));
  418. }
  419. float get_pid_output(int e) {
  420. float pid_output;
  421. #ifdef PIDTEMP
  422. #ifndef PID_OPENLOOP
  423. pid_error[e] = target_temperature[e] - current_temperature[e];
  424. if (pid_error[e] > PID_FUNCTIONAL_RANGE) {
  425. pid_output = BANG_MAX;
  426. pid_reset[e] = true;
  427. }
  428. else if (pid_error[e] < -PID_FUNCTIONAL_RANGE || target_temperature[e] == 0) {
  429. pid_output = 0;
  430. pid_reset[e] = true;
  431. }
  432. else {
  433. if (pid_reset[e]) {
  434. temp_iState[e] = 0.0;
  435. pid_reset[e] = false;
  436. }
  437. pTerm[e] = PID_PARAM(Kp,e) * pid_error[e];
  438. temp_iState[e] += pid_error[e];
  439. temp_iState[e] = constrain(temp_iState[e], temp_iState_min[e], temp_iState_max[e]);
  440. iTerm[e] = PID_PARAM(Ki,e) * temp_iState[e];
  441. dTerm[e] = K2 * PID_PARAM(Kd,e) * (current_temperature[e] - temp_dState[e]) + K1 * dTerm[e];
  442. pid_output = pTerm[e] + iTerm[e] - dTerm[e];
  443. if (pid_output > PID_MAX) {
  444. if (pid_error[e] > 0) temp_iState[e] -= pid_error[e]; // conditional un-integration
  445. pid_output = PID_MAX;
  446. }
  447. else if (pid_output < 0) {
  448. if (pid_error[e] < 0) temp_iState[e] -= pid_error[e]; // conditional un-integration
  449. pid_output = 0;
  450. }
  451. }
  452. temp_dState[e] = current_temperature[e];
  453. #else
  454. pid_output = constrain(target_temperature[e], 0, PID_MAX);
  455. #endif //PID_OPENLOOP
  456. #ifdef PID_DEBUG
  457. SERIAL_ECHO_START;
  458. SERIAL_ECHO(MSG_PID_DEBUG);
  459. SERIAL_ECHO(e);
  460. SERIAL_ECHO(MSG_PID_DEBUG_INPUT);
  461. SERIAL_ECHO(current_temperature[e]);
  462. SERIAL_ECHO(MSG_PID_DEBUG_OUTPUT);
  463. SERIAL_ECHO(pid_output);
  464. SERIAL_ECHO(MSG_PID_DEBUG_PTERM);
  465. SERIAL_ECHO(pTerm[e]);
  466. SERIAL_ECHO(MSG_PID_DEBUG_ITERM);
  467. SERIAL_ECHO(iTerm[e]);
  468. SERIAL_ECHO(MSG_PID_DEBUG_DTERM);
  469. SERIAL_ECHOLN(dTerm[e]);
  470. #endif //PID_DEBUG
  471. #else /* PID off */
  472. pid_output = (current_temperature[e] < target_temperature[e]) ? PID_MAX : 0;
  473. #endif
  474. return pid_output;
  475. }
  476. #ifdef PIDTEMPBED
  477. float get_pid_output_bed() {
  478. float pid_output;
  479. #ifndef PID_OPENLOOP
  480. pid_error_bed = target_temperature_bed - current_temperature_bed;
  481. pTerm_bed = bedKp * pid_error_bed;
  482. temp_iState_bed += pid_error_bed;
  483. temp_iState_bed = constrain(temp_iState_bed, temp_iState_min_bed, temp_iState_max_bed);
  484. iTerm_bed = bedKi * temp_iState_bed;
  485. dTerm_bed = K2 * bedKd * (current_temperature_bed - temp_dState_bed) + K1 * dTerm_bed;
  486. temp_dState_bed = current_temperature_bed;
  487. pid_output = pTerm_bed + iTerm_bed - dTerm_bed;
  488. if (pid_output > MAX_BED_POWER) {
  489. if (pid_error_bed > 0) temp_iState_bed -= pid_error_bed; // conditional un-integration
  490. pid_output = MAX_BED_POWER;
  491. }
  492. else if (pid_output < 0) {
  493. if (pid_error_bed < 0) temp_iState_bed -= pid_error_bed; // conditional un-integration
  494. pid_output = 0;
  495. }
  496. #else
  497. pid_output = constrain(target_temperature_bed, 0, MAX_BED_POWER);
  498. #endif // PID_OPENLOOP
  499. #ifdef PID_BED_DEBUG
  500. SERIAL_ECHO_START;
  501. SERIAL_ECHO(" PID_BED_DEBUG ");
  502. SERIAL_ECHO(": Input ");
  503. SERIAL_ECHO(current_temperature_bed);
  504. SERIAL_ECHO(" Output ");
  505. SERIAL_ECHO(pid_output);
  506. SERIAL_ECHO(" pTerm ");
  507. SERIAL_ECHO(pTerm_bed);
  508. SERIAL_ECHO(" iTerm ");
  509. SERIAL_ECHO(iTerm_bed);
  510. SERIAL_ECHO(" dTerm ");
  511. SERIAL_ECHOLN(dTerm_bed);
  512. #endif //PID_BED_DEBUG
  513. return pid_output;
  514. }
  515. #endif
  516. /**
  517. * Manage heating activities for extruder hot-ends and a heated bed
  518. * - Acquire updated temperature readings
  519. * - Invoke thermal runaway protection
  520. * - Manage extruder auto-fan
  521. * - Apply filament width to the extrusion rate (may move)
  522. * - Update the heated bed PID output value
  523. */
  524. void manage_heater() {
  525. if (!temp_meas_ready) return;
  526. updateTemperaturesFromRawValues();
  527. #ifdef HEATER_0_USES_MAX6675
  528. float ct = current_temperature[0];
  529. if (ct > min(HEATER_0_MAXTEMP, 1023)) max_temp_error(0);
  530. if (ct < max(HEATER_0_MINTEMP, 0.01)) min_temp_error(0);
  531. #endif
  532. #if defined(THERMAL_PROTECTION_HOTENDS) || !defined(PIDTEMPBED) || HAS_AUTO_FAN
  533. millis_t ms = millis();
  534. #endif
  535. // Loop through all extruders
  536. for (int e = 0; e < EXTRUDERS; e++) {
  537. #ifdef THERMAL_PROTECTION_HOTENDS
  538. 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);
  539. #endif
  540. float pid_output = get_pid_output(e);
  541. // Check if temperature is within the correct range
  542. soft_pwm[e] = current_temperature[e] > minttemp[e] && current_temperature[e] < maxttemp[e] ? (int)pid_output >> 1 : 0;
  543. // Check if the temperature is failing to increase
  544. #ifdef THERMAL_PROTECTION_HOTENDS
  545. // Is it time to check this extruder's heater?
  546. if (watch_heater_next_ms[e] && ms > watch_heater_next_ms[e]) {
  547. // Has it failed to increase enough?
  548. if (degHotend(e) < watch_target_temp[e]) {
  549. // Stop!
  550. disable_all_heaters();
  551. _temp_error(e, PSTR(MSG_HEATING_FAILED), PSTR(MSG_HEATING_FAILED_LCD));
  552. }
  553. else {
  554. // Start again if the target is still far off
  555. start_watching_heater(e);
  556. }
  557. }
  558. #endif // THERMAL_PROTECTION_HOTENDS
  559. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  560. if (fabs(current_temperature[0] - redundant_temperature) > MAX_REDUNDANT_TEMP_SENSOR_DIFF) {
  561. disable_all_heaters();
  562. _temp_error(0, PSTR(MSG_EXTRUDER_SWITCHED_OFF), PSTR(MSG_ERR_REDUNDANT_TEMP));
  563. }
  564. #endif
  565. } // Extruders Loop
  566. #if HAS_AUTO_FAN
  567. if (ms > next_auto_fan_check_ms) { // only need to check fan state very infrequently
  568. checkExtruderAutoFans();
  569. next_auto_fan_check_ms = ms + 2500;
  570. }
  571. #endif
  572. // Control the extruder rate based on the width sensor
  573. #ifdef FILAMENT_SENSOR
  574. if (filament_sensor) {
  575. meas_shift_index = delay_index1 - meas_delay_cm;
  576. if (meas_shift_index < 0) meas_shift_index += MAX_MEASUREMENT_DELAY + 1; //loop around buffer if needed
  577. // Get the delayed info and add 100 to reconstitute to a percent of
  578. // the nominal filament diameter then square it to get an area
  579. meas_shift_index = constrain(meas_shift_index, 0, MAX_MEASUREMENT_DELAY);
  580. float vm = pow((measurement_delay[meas_shift_index] + 100.0) / 100.0, 2);
  581. if (vm < 0.01) vm = 0.01;
  582. volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] = vm;
  583. }
  584. #endif //FILAMENT_SENSOR
  585. #ifndef PIDTEMPBED
  586. if (ms < next_bed_check_ms) return;
  587. next_bed_check_ms = ms + BED_CHECK_INTERVAL;
  588. #endif
  589. #if TEMP_SENSOR_BED != 0
  590. #ifdef THERMAL_PROTECTION_BED
  591. 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);
  592. #endif
  593. #ifdef PIDTEMPBED
  594. float pid_output = get_pid_output_bed();
  595. soft_pwm_bed = current_temperature_bed > BED_MINTEMP && current_temperature_bed < BED_MAXTEMP ? (int)pid_output >> 1 : 0;
  596. #elif defined(BED_LIMIT_SWITCHING)
  597. // Check if temperature is within the correct band
  598. if (current_temperature_bed > BED_MINTEMP && current_temperature_bed < BED_MAXTEMP) {
  599. if (current_temperature_bed >= target_temperature_bed + BED_HYSTERESIS)
  600. soft_pwm_bed = 0;
  601. else if (current_temperature_bed <= target_temperature_bed - BED_HYSTERESIS)
  602. soft_pwm_bed = MAX_BED_POWER >> 1;
  603. }
  604. else {
  605. soft_pwm_bed = 0;
  606. WRITE_HEATER_BED(LOW);
  607. }
  608. #else // BED_LIMIT_SWITCHING
  609. // Check if temperature is within the correct range
  610. if (current_temperature_bed > BED_MINTEMP && current_temperature_bed < BED_MAXTEMP) {
  611. soft_pwm_bed = current_temperature_bed < target_temperature_bed ? MAX_BED_POWER >> 1 : 0;
  612. }
  613. else {
  614. soft_pwm_bed = 0;
  615. WRITE_HEATER_BED(LOW);
  616. }
  617. #endif
  618. #endif //TEMP_SENSOR_BED != 0
  619. }
  620. #define PGM_RD_W(x) (short)pgm_read_word(&x)
  621. // Derived from RepRap FiveD extruder::getTemperature()
  622. // For hot end temperature measurement.
  623. static float analog2temp(int raw, uint8_t e) {
  624. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  625. if (e > EXTRUDERS)
  626. #else
  627. if (e >= EXTRUDERS)
  628. #endif
  629. {
  630. SERIAL_ERROR_START;
  631. SERIAL_ERROR((int)e);
  632. SERIAL_ERRORLNPGM(MSG_INVALID_EXTRUDER_NUM);
  633. kill();
  634. return 0.0;
  635. }
  636. #ifdef HEATER_0_USES_MAX6675
  637. if (e == 0) return 0.25 * raw;
  638. #endif
  639. if (heater_ttbl_map[e] != NULL) {
  640. float celsius = 0;
  641. uint8_t i;
  642. short (*tt)[][2] = (short (*)[][2])(heater_ttbl_map[e]);
  643. for (i = 1; i < heater_ttbllen_map[e]; i++) {
  644. if (PGM_RD_W((*tt)[i][0]) > raw) {
  645. celsius = PGM_RD_W((*tt)[i-1][1]) +
  646. (raw - PGM_RD_W((*tt)[i-1][0])) *
  647. (float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i-1][1])) /
  648. (float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i-1][0]));
  649. break;
  650. }
  651. }
  652. // Overflow: Set to last value in the table
  653. if (i == heater_ttbllen_map[e]) celsius = PGM_RD_W((*tt)[i-1][1]);
  654. return celsius;
  655. }
  656. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  657. }
  658. // Derived from RepRap FiveD extruder::getTemperature()
  659. // For bed temperature measurement.
  660. static float analog2tempBed(int raw) {
  661. #ifdef BED_USES_THERMISTOR
  662. float celsius = 0;
  663. byte i;
  664. for (i = 1; i < BEDTEMPTABLE_LEN; i++) {
  665. if (PGM_RD_W(BEDTEMPTABLE[i][0]) > raw) {
  666. celsius = PGM_RD_W(BEDTEMPTABLE[i-1][1]) +
  667. (raw - PGM_RD_W(BEDTEMPTABLE[i-1][0])) *
  668. (float)(PGM_RD_W(BEDTEMPTABLE[i][1]) - PGM_RD_W(BEDTEMPTABLE[i-1][1])) /
  669. (float)(PGM_RD_W(BEDTEMPTABLE[i][0]) - PGM_RD_W(BEDTEMPTABLE[i-1][0]));
  670. break;
  671. }
  672. }
  673. // Overflow: Set to last value in the table
  674. if (i == BEDTEMPTABLE_LEN) celsius = PGM_RD_W(BEDTEMPTABLE[i-1][1]);
  675. return celsius;
  676. #elif defined BED_USES_AD595
  677. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  678. #else
  679. return 0;
  680. #endif
  681. }
  682. /* Called to get the raw values into the the actual temperatures. The raw values are created in interrupt context,
  683. and this function is called from normal context as it is too slow to run in interrupts and will block the stepper routine otherwise */
  684. static void updateTemperaturesFromRawValues() {
  685. #ifdef HEATER_0_USES_MAX6675
  686. current_temperature_raw[0] = read_max6675();
  687. #endif
  688. for (uint8_t e = 0; e < EXTRUDERS; e++) {
  689. current_temperature[e] = analog2temp(current_temperature_raw[e], e);
  690. }
  691. current_temperature_bed = analog2tempBed(current_temperature_bed_raw);
  692. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  693. redundant_temperature = analog2temp(redundant_temperature_raw, 1);
  694. #endif
  695. #if HAS_FILAMENT_SENSOR
  696. filament_width_meas = analog2widthFil();
  697. #endif
  698. //Reset the watchdog after we know we have a temperature measurement.
  699. watchdog_reset();
  700. CRITICAL_SECTION_START;
  701. temp_meas_ready = false;
  702. CRITICAL_SECTION_END;
  703. }
  704. #ifdef FILAMENT_SENSOR
  705. // Convert raw Filament Width to millimeters
  706. float analog2widthFil() {
  707. return current_raw_filwidth / 16383.0 * 5.0;
  708. //return current_raw_filwidth;
  709. }
  710. // Convert raw Filament Width to a ratio
  711. int widthFil_to_size_ratio() {
  712. float temp = filament_width_meas;
  713. if (temp < MEASURED_LOWER_LIMIT) temp = filament_width_nominal; //assume sensor cut out
  714. else if (temp > MEASURED_UPPER_LIMIT) temp = MEASURED_UPPER_LIMIT;
  715. return filament_width_nominal / temp * 100;
  716. }
  717. #endif
  718. /**
  719. * Initialize the temperature manager
  720. * The manager is implemented by periodic calls to manage_heater()
  721. */
  722. void tp_init() {
  723. #if MB(RUMBA) && ((TEMP_SENSOR_0==-1)||(TEMP_SENSOR_1==-1)||(TEMP_SENSOR_2==-1)||(TEMP_SENSOR_BED==-1))
  724. //disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector
  725. MCUCR=BIT(JTD);
  726. MCUCR=BIT(JTD);
  727. #endif
  728. // Finish init of mult extruder arrays
  729. for (int e = 0; e < EXTRUDERS; e++) {
  730. // populate with the first value
  731. maxttemp[e] = maxttemp[0];
  732. #ifdef PIDTEMP
  733. temp_iState_min[e] = 0.0;
  734. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / PID_PARAM(Ki,e);
  735. #endif //PIDTEMP
  736. #ifdef PIDTEMPBED
  737. temp_iState_min_bed = 0.0;
  738. temp_iState_max_bed = PID_INTEGRAL_DRIVE_MAX / bedKi;
  739. #endif //PIDTEMPBED
  740. }
  741. #if HAS_HEATER_0
  742. SET_OUTPUT(HEATER_0_PIN);
  743. #endif
  744. #if HAS_HEATER_1
  745. SET_OUTPUT(HEATER_1_PIN);
  746. #endif
  747. #if HAS_HEATER_2
  748. SET_OUTPUT(HEATER_2_PIN);
  749. #endif
  750. #if HAS_HEATER_3
  751. SET_OUTPUT(HEATER_3_PIN);
  752. #endif
  753. #if HAS_HEATER_BED
  754. SET_OUTPUT(HEATER_BED_PIN);
  755. #endif
  756. #if HAS_FAN
  757. SET_OUTPUT(FAN_PIN);
  758. #ifdef FAST_PWM_FAN
  759. setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  760. #endif
  761. #ifdef FAN_SOFT_PWM
  762. soft_pwm_fan = fanSpeedSoftPwm / 2;
  763. #endif
  764. #endif
  765. #ifdef HEATER_0_USES_MAX6675
  766. #ifndef SDSUPPORT
  767. OUT_WRITE(SCK_PIN, LOW);
  768. OUT_WRITE(MOSI_PIN, HIGH);
  769. OUT_WRITE(MISO_PIN, HIGH);
  770. #else
  771. pinMode(SS_PIN, OUTPUT);
  772. digitalWrite(SS_PIN, HIGH);
  773. #endif
  774. OUT_WRITE(MAX6675_SS,HIGH);
  775. #endif //HEATER_0_USES_MAX6675
  776. #ifdef DIDR2
  777. #define ANALOG_SELECT(pin) do{ if (pin < 8) DIDR0 |= BIT(pin); else DIDR2 |= BIT(pin - 8); }while(0)
  778. #else
  779. #define ANALOG_SELECT(pin) do{ DIDR0 |= BIT(pin); }while(0)
  780. #endif
  781. // Set analog inputs
  782. ADCSRA = BIT(ADEN) | BIT(ADSC) | BIT(ADIF) | 0x07;
  783. DIDR0 = 0;
  784. #ifdef DIDR2
  785. DIDR2 = 0;
  786. #endif
  787. #if HAS_TEMP_0
  788. ANALOG_SELECT(TEMP_0_PIN);
  789. #endif
  790. #if HAS_TEMP_1
  791. ANALOG_SELECT(TEMP_1_PIN);
  792. #endif
  793. #if HAS_TEMP_2
  794. ANALOG_SELECT(TEMP_2_PIN);
  795. #endif
  796. #if HAS_TEMP_3
  797. ANALOG_SELECT(TEMP_3_PIN);
  798. #endif
  799. #if HAS_TEMP_BED
  800. ANALOG_SELECT(TEMP_BED_PIN);
  801. #endif
  802. #if HAS_FILAMENT_SENSOR
  803. ANALOG_SELECT(FILWIDTH_PIN);
  804. #endif
  805. // Use timer0 for temperature measurement
  806. // Interleave temperature interrupt with millies interrupt
  807. OCR0B = 128;
  808. TIMSK0 |= BIT(OCIE0B);
  809. // Wait for temperature measurement to settle
  810. delay(250);
  811. #define TEMP_MIN_ROUTINE(NR) \
  812. minttemp[NR] = HEATER_ ## NR ## _MINTEMP; \
  813. while(analog2temp(minttemp_raw[NR], NR) < HEATER_ ## NR ## _MINTEMP) { \
  814. if (HEATER_ ## NR ## _RAW_LO_TEMP < HEATER_ ## NR ## _RAW_HI_TEMP) \
  815. minttemp_raw[NR] += OVERSAMPLENR; \
  816. else \
  817. minttemp_raw[NR] -= OVERSAMPLENR; \
  818. }
  819. #define TEMP_MAX_ROUTINE(NR) \
  820. maxttemp[NR] = HEATER_ ## NR ## _MAXTEMP; \
  821. while(analog2temp(maxttemp_raw[NR], NR) > HEATER_ ## NR ## _MAXTEMP) { \
  822. if (HEATER_ ## NR ## _RAW_LO_TEMP < HEATER_ ## NR ## _RAW_HI_TEMP) \
  823. maxttemp_raw[NR] -= OVERSAMPLENR; \
  824. else \
  825. maxttemp_raw[NR] += OVERSAMPLENR; \
  826. }
  827. #ifdef HEATER_0_MINTEMP
  828. TEMP_MIN_ROUTINE(0);
  829. #endif
  830. #ifdef HEATER_0_MAXTEMP
  831. TEMP_MAX_ROUTINE(0);
  832. #endif
  833. #if EXTRUDERS > 1
  834. #ifdef HEATER_1_MINTEMP
  835. TEMP_MIN_ROUTINE(1);
  836. #endif
  837. #ifdef HEATER_1_MAXTEMP
  838. TEMP_MAX_ROUTINE(1);
  839. #endif
  840. #if EXTRUDERS > 2
  841. #ifdef HEATER_2_MINTEMP
  842. TEMP_MIN_ROUTINE(2);
  843. #endif
  844. #ifdef HEATER_2_MAXTEMP
  845. TEMP_MAX_ROUTINE(2);
  846. #endif
  847. #if EXTRUDERS > 3
  848. #ifdef HEATER_3_MINTEMP
  849. TEMP_MIN_ROUTINE(3);
  850. #endif
  851. #ifdef HEATER_3_MAXTEMP
  852. TEMP_MAX_ROUTINE(3);
  853. #endif
  854. #endif // EXTRUDERS > 3
  855. #endif // EXTRUDERS > 2
  856. #endif // EXTRUDERS > 1
  857. #ifdef BED_MINTEMP
  858. /* No bed MINTEMP error implemented?!? */ /*
  859. while(analog2tempBed(bed_minttemp_raw) < BED_MINTEMP) {
  860. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  861. bed_minttemp_raw += OVERSAMPLENR;
  862. #else
  863. bed_minttemp_raw -= OVERSAMPLENR;
  864. #endif
  865. }
  866. */
  867. #endif //BED_MINTEMP
  868. #ifdef BED_MAXTEMP
  869. while(analog2tempBed(bed_maxttemp_raw) > BED_MAXTEMP) {
  870. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  871. bed_maxttemp_raw -= OVERSAMPLENR;
  872. #else
  873. bed_maxttemp_raw += OVERSAMPLENR;
  874. #endif
  875. }
  876. #endif //BED_MAXTEMP
  877. }
  878. #ifdef THERMAL_PROTECTION_HOTENDS
  879. /**
  880. * Start Heating Sanity Check for hotends that are below
  881. * their target temperature by a configurable margin.
  882. * This is called when the temperature is set. (M104, M109)
  883. */
  884. void start_watching_heater(int e) {
  885. millis_t ms = millis() + WATCH_TEMP_PERIOD * 1000;
  886. if (degHotend(e) < degTargetHotend(e) - (WATCH_TEMP_INCREASE + TEMP_HYSTERESIS + 1)) {
  887. watch_target_temp[e] = degHotend(e) + WATCH_TEMP_INCREASE;
  888. watch_heater_next_ms[e] = ms;
  889. }
  890. else
  891. watch_heater_next_ms[e] = 0;
  892. }
  893. #endif
  894. #if defined(THERMAL_PROTECTION_HOTENDS) || defined(THERMAL_PROTECTION_BED)
  895. void thermal_runaway_protection(TRState *state, millis_t *timer, float temperature, float target_temperature, int heater_id, int period_seconds, int hysteresis_degc) {
  896. static float tr_target_temperature[EXTRUDERS+1] = { 0.0 };
  897. /*
  898. SERIAL_ECHO_START;
  899. SERIAL_ECHOPGM("Thermal Thermal Runaway Running. Heater ID: ");
  900. if (heater_id < 0) SERIAL_ECHOPGM("bed"); else SERIAL_ECHOPGM(heater_id);
  901. SERIAL_ECHOPGM(" ; State:");
  902. SERIAL_ECHOPGM(*state);
  903. SERIAL_ECHOPGM(" ; Timer:");
  904. SERIAL_ECHOPGM(*timer);
  905. SERIAL_ECHOPGM(" ; Temperature:");
  906. SERIAL_ECHOPGM(temperature);
  907. SERIAL_ECHOPGM(" ; Target Temp:");
  908. SERIAL_ECHOPGM(target_temperature);
  909. SERIAL_EOL;
  910. */
  911. int heater_index = heater_id >= 0 ? heater_id : EXTRUDERS;
  912. // If the target temperature changes, restart
  913. if (tr_target_temperature[heater_index] != target_temperature)
  914. *state = TRReset;
  915. switch (*state) {
  916. case TRReset:
  917. *timer = 0;
  918. *state = TRInactive;
  919. break;
  920. // Inactive state waits for a target temperature to be set
  921. case TRInactive:
  922. if (target_temperature > 0) {
  923. tr_target_temperature[heater_index] = target_temperature;
  924. *state = TRFirstHeating;
  925. }
  926. break;
  927. // When first heating, wait for the temperature to be reached then go to Stable state
  928. case TRFirstHeating:
  929. if (temperature >= tr_target_temperature[heater_index]) *state = TRStable;
  930. break;
  931. // While the temperature is stable watch for a bad temperature
  932. case TRStable:
  933. // If the temperature is over the target (-hysteresis) restart the timer
  934. if (temperature >= tr_target_temperature[heater_index] - hysteresis_degc)
  935. *timer = millis();
  936. // If the timer goes too long without a reset, trigger shutdown
  937. else if (millis() > *timer + period_seconds * 1000UL)
  938. *state = TRRunaway;
  939. break;
  940. case TRRunaway:
  941. SERIAL_ERROR_START;
  942. SERIAL_ERRORLNPGM(MSG_THERMAL_RUNAWAY_STOP);
  943. if (heater_id < 0) SERIAL_ERRORLNPGM("bed"); else SERIAL_ERRORLN(heater_id);
  944. LCD_ALERTMESSAGEPGM(MSG_THERMAL_RUNAWAY);
  945. disable_all_heaters();
  946. disable_all_steppers();
  947. for (;;) {
  948. manage_heater();
  949. lcd_update();
  950. }
  951. }
  952. }
  953. #endif // THERMAL_PROTECTION_HOTENDS || THERMAL_PROTECTION_BED
  954. void disable_all_heaters() {
  955. for (int i=0; i<EXTRUDERS; i++) setTargetHotend(0, i);
  956. setTargetBed(0);
  957. #define DISABLE_HEATER(NR) { \
  958. target_temperature[NR] = 0; \
  959. soft_pwm[NR] = 0; \
  960. WRITE_HEATER_ ## NR (LOW); \
  961. }
  962. #if HAS_TEMP_0
  963. target_temperature[0] = 0;
  964. soft_pwm[0] = 0;
  965. WRITE_HEATER_0P(LOW); // Should HEATERS_PARALLEL apply here? Then change to DISABLE_HEATER(0)
  966. #endif
  967. #if EXTRUDERS > 1 && HAS_TEMP_1
  968. DISABLE_HEATER(1);
  969. #endif
  970. #if EXTRUDERS > 2 && HAS_TEMP_2
  971. DISABLE_HEATER(2);
  972. #endif
  973. #if EXTRUDERS > 3 && HAS_TEMP_3
  974. DISABLE_HEATER(3);
  975. #endif
  976. #if HAS_TEMP_BED
  977. target_temperature_bed = 0;
  978. soft_pwm_bed = 0;
  979. #if HAS_HEATER_BED
  980. WRITE_HEATER_BED(LOW);
  981. #endif
  982. #endif
  983. }
  984. #ifdef HEATER_0_USES_MAX6675
  985. #define MAX6675_HEAT_INTERVAL 250u
  986. static millis_t next_max6675_ms = 0;
  987. int max6675_temp = 2000;
  988. static int read_max6675() {
  989. millis_t ms = millis();
  990. if (ms < next_max6675_ms)
  991. return max6675_temp;
  992. next_max6675_ms = ms + MAX6675_HEAT_INTERVAL;
  993. max6675_temp = 0;
  994. #ifdef PRR
  995. PRR &= ~BIT(PRSPI);
  996. #elif defined(PRR0)
  997. PRR0 &= ~BIT(PRSPI);
  998. #endif
  999. SPCR = BIT(MSTR) | BIT(SPE) | BIT(SPR0);
  1000. // enable TT_MAX6675
  1001. WRITE(MAX6675_SS, 0);
  1002. // ensure 100ns delay - a bit extra is fine
  1003. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1004. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1005. // read MSB
  1006. SPDR = 0;
  1007. for (;(SPSR & BIT(SPIF)) == 0;);
  1008. max6675_temp = SPDR;
  1009. max6675_temp <<= 8;
  1010. // read LSB
  1011. SPDR = 0;
  1012. for (;(SPSR & BIT(SPIF)) == 0;);
  1013. max6675_temp |= SPDR;
  1014. // disable TT_MAX6675
  1015. WRITE(MAX6675_SS, 1);
  1016. if (max6675_temp & 4) {
  1017. // thermocouple open
  1018. max6675_temp = 4000;
  1019. }
  1020. else {
  1021. max6675_temp = max6675_temp >> 3;
  1022. }
  1023. return max6675_temp;
  1024. }
  1025. #endif //HEATER_0_USES_MAX6675
  1026. /**
  1027. * Stages in the ISR loop
  1028. */
  1029. enum TempState {
  1030. PrepareTemp_0,
  1031. MeasureTemp_0,
  1032. PrepareTemp_BED,
  1033. MeasureTemp_BED,
  1034. PrepareTemp_1,
  1035. MeasureTemp_1,
  1036. PrepareTemp_2,
  1037. MeasureTemp_2,
  1038. PrepareTemp_3,
  1039. MeasureTemp_3,
  1040. Prepare_FILWIDTH,
  1041. Measure_FILWIDTH,
  1042. StartupDelay // Startup, delay initial temp reading a tiny bit so the hardware can settle
  1043. };
  1044. static unsigned long raw_temp_value[4] = { 0 };
  1045. static unsigned long raw_temp_bed_value = 0;
  1046. static void set_current_temp_raw() {
  1047. #if HAS_TEMP_0 && !defined(HEATER_0_USES_MAX6675)
  1048. current_temperature_raw[0] = raw_temp_value[0];
  1049. #endif
  1050. #if HAS_TEMP_1
  1051. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  1052. redundant_temperature_raw = raw_temp_value[1];
  1053. #else
  1054. current_temperature_raw[1] = raw_temp_value[1];
  1055. #endif
  1056. #if HAS_TEMP_2
  1057. current_temperature_raw[2] = raw_temp_value[2];
  1058. #if HAS_TEMP_3
  1059. current_temperature_raw[3] = raw_temp_value[3];
  1060. #endif
  1061. #endif
  1062. #endif
  1063. current_temperature_bed_raw = raw_temp_bed_value;
  1064. temp_meas_ready = true;
  1065. }
  1066. /**
  1067. * Timer 0 is shared with millies
  1068. * - Manage PWM to all the heaters and fan
  1069. * - Update the raw temperature values
  1070. * - Check new temperature values for MIN/MAX errors
  1071. * - Step the babysteps value for each axis towards 0
  1072. */
  1073. ISR(TIMER0_COMPB_vect) {
  1074. static unsigned char temp_count = 0;
  1075. static TempState temp_state = StartupDelay;
  1076. static unsigned char pwm_count = BIT(SOFT_PWM_SCALE);
  1077. // Static members for each heater
  1078. #ifdef SLOW_PWM_HEATERS
  1079. static unsigned char slow_pwm_count = 0;
  1080. #define ISR_STATICS(n) \
  1081. static unsigned char soft_pwm_ ## n; \
  1082. static unsigned char state_heater_ ## n = 0; \
  1083. static unsigned char state_timer_heater_ ## n = 0
  1084. #else
  1085. #define ISR_STATICS(n) static unsigned char soft_pwm_ ## n
  1086. #endif
  1087. // Statics per heater
  1088. ISR_STATICS(0);
  1089. #if (EXTRUDERS > 1) || defined(HEATERS_PARALLEL)
  1090. ISR_STATICS(1);
  1091. #if EXTRUDERS > 2
  1092. ISR_STATICS(2);
  1093. #if EXTRUDERS > 3
  1094. ISR_STATICS(3);
  1095. #endif
  1096. #endif
  1097. #endif
  1098. #if HAS_HEATER_BED
  1099. ISR_STATICS(BED);
  1100. #endif
  1101. #if HAS_FILAMENT_SENSOR
  1102. static unsigned long raw_filwidth_value = 0;
  1103. #endif
  1104. #ifndef SLOW_PWM_HEATERS
  1105. /**
  1106. * standard PWM modulation
  1107. */
  1108. if (pwm_count == 0) {
  1109. soft_pwm_0 = soft_pwm[0];
  1110. if (soft_pwm_0 > 0) {
  1111. WRITE_HEATER_0(1);
  1112. }
  1113. else WRITE_HEATER_0P(0); // If HEATERS_PARALLEL should apply, change to WRITE_HEATER_0
  1114. #if EXTRUDERS > 1
  1115. soft_pwm_1 = soft_pwm[1];
  1116. WRITE_HEATER_1(soft_pwm_1 > 0 ? 1 : 0);
  1117. #if EXTRUDERS > 2
  1118. soft_pwm_2 = soft_pwm[2];
  1119. WRITE_HEATER_2(soft_pwm_2 > 0 ? 1 : 0);
  1120. #if EXTRUDERS > 3
  1121. soft_pwm_3 = soft_pwm[3];
  1122. WRITE_HEATER_3(soft_pwm_3 > 0 ? 1 : 0);
  1123. #endif
  1124. #endif
  1125. #endif
  1126. #if HAS_HEATER_BED
  1127. soft_pwm_BED = soft_pwm_bed;
  1128. WRITE_HEATER_BED(soft_pwm_BED > 0 ? 1 : 0);
  1129. #endif
  1130. #ifdef FAN_SOFT_PWM
  1131. soft_pwm_fan = fanSpeedSoftPwm / 2;
  1132. WRITE_FAN(soft_pwm_fan > 0 ? 1 : 0);
  1133. #endif
  1134. }
  1135. if (soft_pwm_0 < pwm_count) { WRITE_HEATER_0(0); }
  1136. #if EXTRUDERS > 1
  1137. if (soft_pwm_1 < pwm_count) WRITE_HEATER_1(0);
  1138. #if EXTRUDERS > 2
  1139. if (soft_pwm_2 < pwm_count) WRITE_HEATER_2(0);
  1140. #if EXTRUDERS > 3
  1141. if (soft_pwm_3 < pwm_count) WRITE_HEATER_3(0);
  1142. #endif
  1143. #endif
  1144. #endif
  1145. #if HAS_HEATER_BED
  1146. if (soft_pwm_BED < pwm_count) WRITE_HEATER_BED(0);
  1147. #endif
  1148. #ifdef FAN_SOFT_PWM
  1149. if (soft_pwm_fan < pwm_count) WRITE_FAN(0);
  1150. #endif
  1151. pwm_count += BIT(SOFT_PWM_SCALE);
  1152. pwm_count &= 0x7f;
  1153. #else // SLOW_PWM_HEATERS
  1154. /*
  1155. * SLOW PWM HEATERS
  1156. *
  1157. * for heaters drived by relay
  1158. */
  1159. #ifndef MIN_STATE_TIME
  1160. #define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds
  1161. #endif
  1162. // Macros for Slow PWM timer logic - HEATERS_PARALLEL applies
  1163. #define _SLOW_PWM_ROUTINE(NR, src) \
  1164. soft_pwm_ ## NR = src; \
  1165. if (soft_pwm_ ## NR > 0) { \
  1166. if (state_timer_heater_ ## NR == 0) { \
  1167. if (state_heater_ ## NR == 0) state_timer_heater_ ## NR = MIN_STATE_TIME; \
  1168. state_heater_ ## NR = 1; \
  1169. WRITE_HEATER_ ## NR(1); \
  1170. } \
  1171. } \
  1172. else { \
  1173. if (state_timer_heater_ ## NR == 0) { \
  1174. if (state_heater_ ## NR == 1) state_timer_heater_ ## NR = MIN_STATE_TIME; \
  1175. state_heater_ ## NR = 0; \
  1176. WRITE_HEATER_ ## NR(0); \
  1177. } \
  1178. }
  1179. #define SLOW_PWM_ROUTINE(n) _SLOW_PWM_ROUTINE(n, soft_pwm[n])
  1180. #define PWM_OFF_ROUTINE(NR) \
  1181. if (soft_pwm_ ## NR < slow_pwm_count) { \
  1182. if (state_timer_heater_ ## NR == 0) { \
  1183. if (state_heater_ ## NR == 1) state_timer_heater_ ## NR = MIN_STATE_TIME; \
  1184. state_heater_ ## NR = 0; \
  1185. WRITE_HEATER_ ## NR (0); \
  1186. } \
  1187. }
  1188. if (slow_pwm_count == 0) {
  1189. SLOW_PWM_ROUTINE(0); // EXTRUDER 0
  1190. #if EXTRUDERS > 1
  1191. SLOW_PWM_ROUTINE(1); // EXTRUDER 1
  1192. #if EXTRUDERS > 2
  1193. SLOW_PWM_ROUTINE(2); // EXTRUDER 2
  1194. #if EXTRUDERS > 3
  1195. SLOW_PWM_ROUTINE(3); // EXTRUDER 3
  1196. #endif
  1197. #endif
  1198. #endif
  1199. #if HAS_HEATER_BED
  1200. _SLOW_PWM_ROUTINE(BED, soft_pwm_bed); // BED
  1201. #endif
  1202. } // slow_pwm_count == 0
  1203. PWM_OFF_ROUTINE(0); // EXTRUDER 0
  1204. #if EXTRUDERS > 1
  1205. PWM_OFF_ROUTINE(1); // EXTRUDER 1
  1206. #if EXTRUDERS > 2
  1207. PWM_OFF_ROUTINE(2); // EXTRUDER 2
  1208. #if EXTRUDERS > 3
  1209. PWM_OFF_ROUTINE(3); // EXTRUDER 3
  1210. #endif
  1211. #endif
  1212. #endif
  1213. #if HAS_HEATER_BED
  1214. PWM_OFF_ROUTINE(BED); // BED
  1215. #endif
  1216. #ifdef FAN_SOFT_PWM
  1217. if (pwm_count == 0) {
  1218. soft_pwm_fan = fanSpeedSoftPwm / 2;
  1219. WRITE_FAN(soft_pwm_fan > 0 ? 1 : 0);
  1220. }
  1221. if (soft_pwm_fan < pwm_count) WRITE_FAN(0);
  1222. #endif //FAN_SOFT_PWM
  1223. pwm_count += BIT(SOFT_PWM_SCALE);
  1224. pwm_count &= 0x7f;
  1225. // increment slow_pwm_count only every 64 pwm_count circa 65.5ms
  1226. if ((pwm_count % 64) == 0) {
  1227. slow_pwm_count++;
  1228. slow_pwm_count &= 0x7f;
  1229. // EXTRUDER 0
  1230. if (state_timer_heater_0 > 0) state_timer_heater_0--;
  1231. #if EXTRUDERS > 1 // EXTRUDER 1
  1232. if (state_timer_heater_1 > 0) state_timer_heater_1--;
  1233. #if EXTRUDERS > 2 // EXTRUDER 2
  1234. if (state_timer_heater_2 > 0) state_timer_heater_2--;
  1235. #if EXTRUDERS > 3 // EXTRUDER 3
  1236. if (state_timer_heater_3 > 0) state_timer_heater_3--;
  1237. #endif
  1238. #endif
  1239. #endif
  1240. #if HAS_HEATER_BED
  1241. if (state_timer_heater_BED > 0) state_timer_heater_BED--;
  1242. #endif
  1243. } // (pwm_count % 64) == 0
  1244. #endif // SLOW_PWM_HEATERS
  1245. #define SET_ADMUX_ADCSRA(pin) ADMUX = BIT(REFS0) | (pin & 0x07); ADCSRA |= BIT(ADSC)
  1246. #ifdef MUX5
  1247. #define START_ADC(pin) if (pin > 7) ADCSRB = BIT(MUX5); else ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
  1248. #else
  1249. #define START_ADC(pin) ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
  1250. #endif
  1251. // Prepare or measure a sensor, each one every 12th frame
  1252. switch(temp_state) {
  1253. case PrepareTemp_0:
  1254. #if HAS_TEMP_0
  1255. START_ADC(TEMP_0_PIN);
  1256. #endif
  1257. lcd_buttons_update();
  1258. temp_state = MeasureTemp_0;
  1259. break;
  1260. case MeasureTemp_0:
  1261. #if HAS_TEMP_0
  1262. raw_temp_value[0] += ADC;
  1263. #endif
  1264. temp_state = PrepareTemp_BED;
  1265. break;
  1266. case PrepareTemp_BED:
  1267. #if HAS_TEMP_BED
  1268. START_ADC(TEMP_BED_PIN);
  1269. #endif
  1270. lcd_buttons_update();
  1271. temp_state = MeasureTemp_BED;
  1272. break;
  1273. case MeasureTemp_BED:
  1274. #if HAS_TEMP_BED
  1275. raw_temp_bed_value += ADC;
  1276. #endif
  1277. temp_state = PrepareTemp_1;
  1278. break;
  1279. case PrepareTemp_1:
  1280. #if HAS_TEMP_1
  1281. START_ADC(TEMP_1_PIN);
  1282. #endif
  1283. lcd_buttons_update();
  1284. temp_state = MeasureTemp_1;
  1285. break;
  1286. case MeasureTemp_1:
  1287. #if HAS_TEMP_1
  1288. raw_temp_value[1] += ADC;
  1289. #endif
  1290. temp_state = PrepareTemp_2;
  1291. break;
  1292. case PrepareTemp_2:
  1293. #if HAS_TEMP_2
  1294. START_ADC(TEMP_2_PIN);
  1295. #endif
  1296. lcd_buttons_update();
  1297. temp_state = MeasureTemp_2;
  1298. break;
  1299. case MeasureTemp_2:
  1300. #if HAS_TEMP_2
  1301. raw_temp_value[2] += ADC;
  1302. #endif
  1303. temp_state = PrepareTemp_3;
  1304. break;
  1305. case PrepareTemp_3:
  1306. #if HAS_TEMP_3
  1307. START_ADC(TEMP_3_PIN);
  1308. #endif
  1309. lcd_buttons_update();
  1310. temp_state = MeasureTemp_3;
  1311. break;
  1312. case MeasureTemp_3:
  1313. #if HAS_TEMP_3
  1314. raw_temp_value[3] += ADC;
  1315. #endif
  1316. temp_state = Prepare_FILWIDTH;
  1317. break;
  1318. case Prepare_FILWIDTH:
  1319. #if HAS_FILAMENT_SENSOR
  1320. START_ADC(FILWIDTH_PIN);
  1321. #endif
  1322. lcd_buttons_update();
  1323. temp_state = Measure_FILWIDTH;
  1324. break;
  1325. case Measure_FILWIDTH:
  1326. #if HAS_FILAMENT_SENSOR
  1327. // raw_filwidth_value += ADC; //remove to use an IIR filter approach
  1328. if (ADC > 102) { //check that ADC is reading a voltage > 0.5 volts, otherwise don't take in the data.
  1329. raw_filwidth_value -= (raw_filwidth_value>>7); //multiply raw_filwidth_value by 127/128
  1330. raw_filwidth_value += ((unsigned long)ADC<<7); //add new ADC reading
  1331. }
  1332. #endif
  1333. temp_state = PrepareTemp_0;
  1334. temp_count++;
  1335. break;
  1336. case StartupDelay:
  1337. temp_state = PrepareTemp_0;
  1338. break;
  1339. // default:
  1340. // SERIAL_ERROR_START;
  1341. // SERIAL_ERRORLNPGM("Temp measurement error!");
  1342. // break;
  1343. } // switch(temp_state)
  1344. if (temp_count >= OVERSAMPLENR) { // 10 * 16 * 1/(16000000/64/256) = 164ms.
  1345. // Update the raw values if they've been read. Else we could be updating them during reading.
  1346. if (!temp_meas_ready) set_current_temp_raw();
  1347. // Filament Sensor - can be read any time since IIR filtering is used
  1348. #if HAS_FILAMENT_SENSOR
  1349. current_raw_filwidth = raw_filwidth_value >> 10; // Divide to get to 0-16384 range since we used 1/128 IIR filter approach
  1350. #endif
  1351. temp_count = 0;
  1352. for (int i = 0; i < 4; i++) raw_temp_value[i] = 0;
  1353. raw_temp_bed_value = 0;
  1354. #if HAS_TEMP_0 && !defined(HEATER_0_USES_MAX6675)
  1355. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1356. #define GE0 <=
  1357. #else
  1358. #define GE0 >=
  1359. #endif
  1360. if (current_temperature_raw[0] GE0 maxttemp_raw[0]) max_temp_error(0);
  1361. if (minttemp_raw[0] GE0 current_temperature_raw[0]) min_temp_error(0);
  1362. #endif
  1363. #if HAS_TEMP_1
  1364. #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
  1365. #define GE1 <=
  1366. #else
  1367. #define GE1 >=
  1368. #endif
  1369. if (current_temperature_raw[1] GE1 maxttemp_raw[1]) max_temp_error(1);
  1370. if (minttemp_raw[1] GE1 current_temperature_raw[1]) min_temp_error(1);
  1371. #endif // TEMP_SENSOR_1
  1372. #if HAS_TEMP_2
  1373. #if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
  1374. #define GE2 <=
  1375. #else
  1376. #define GE2 >=
  1377. #endif
  1378. if (current_temperature_raw[2] GE2 maxttemp_raw[2]) max_temp_error(2);
  1379. if (minttemp_raw[2] GE2 current_temperature_raw[2]) min_temp_error(2);
  1380. #endif // TEMP_SENSOR_2
  1381. #if HAS_TEMP_3
  1382. #if HEATER_3_RAW_LO_TEMP > HEATER_3_RAW_HI_TEMP
  1383. #define GE3 <=
  1384. #else
  1385. #define GE3 >=
  1386. #endif
  1387. if (current_temperature_raw[3] GE3 maxttemp_raw[3]) max_temp_error(3);
  1388. if (minttemp_raw[3] GE3 current_temperature_raw[3]) min_temp_error(3);
  1389. #endif // TEMP_SENSOR_3
  1390. #if HAS_TEMP_BED
  1391. #if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1392. #define GEBED <=
  1393. #else
  1394. #define GEBED >=
  1395. #endif
  1396. if (current_temperature_bed_raw GEBED bed_maxttemp_raw) {
  1397. target_temperature_bed = 0;
  1398. bed_max_temp_error();
  1399. }
  1400. #endif
  1401. } // temp_count >= OVERSAMPLENR
  1402. #ifdef BABYSTEPPING
  1403. for (uint8_t axis = X_AXIS; axis <= Z_AXIS; axis++) {
  1404. int curTodo = babystepsTodo[axis]; //get rid of volatile for performance
  1405. if (curTodo > 0) {
  1406. babystep(axis,/*fwd*/true);
  1407. babystepsTodo[axis]--; //fewer to do next time
  1408. }
  1409. else if (curTodo < 0) {
  1410. babystep(axis,/*fwd*/false);
  1411. babystepsTodo[axis]++; //fewer to do next time
  1412. }
  1413. }
  1414. #endif //BABYSTEPPING
  1415. }
  1416. #ifdef PIDTEMP
  1417. // Apply the scale factors to the PID values
  1418. float scalePID_i(float i) { return i * PID_dT; }
  1419. float unscalePID_i(float i) { return i / PID_dT; }
  1420. float scalePID_d(float d) { return d / PID_dT; }
  1421. float unscalePID_d(float d) { return d * PID_dT; }
  1422. #endif //PIDTEMP