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

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