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

temperature.cpp 49KB

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