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.

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