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

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