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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504
  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 square it to get an area
  548. if(meas_shift_index<0)
  549. meas_shift_index=0;
  550. else if (meas_shift_index>MAX_MEASUREMENT_DELAY)
  551. meas_shift_index=MAX_MEASUREMENT_DELAY;
  552. volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] = pow((float)(100+measurement_delay[meas_shift_index])/100.0,2);
  553. if (volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] <0.01)
  554. volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM]=0.01;
  555. }
  556. #endif
  557. }
  558. #define PGM_RD_W(x) (short)pgm_read_word(&x)
  559. // Derived from RepRap FiveD extruder::getTemperature()
  560. // For hot end temperature measurement.
  561. static float analog2temp(int raw, uint8_t e) {
  562. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  563. if(e > EXTRUDERS)
  564. #else
  565. if(e >= EXTRUDERS)
  566. #endif
  567. {
  568. SERIAL_ERROR_START;
  569. SERIAL_ERROR((int)e);
  570. SERIAL_ERRORLNPGM(" - Invalid extruder number !");
  571. kill();
  572. return 0.0;
  573. }
  574. #ifdef HEATER_0_USES_MAX6675
  575. if (e == 0)
  576. {
  577. return 0.25 * raw;
  578. }
  579. #endif
  580. if(heater_ttbl_map[e] != NULL)
  581. {
  582. float celsius = 0;
  583. uint8_t i;
  584. short (*tt)[][2] = (short (*)[][2])(heater_ttbl_map[e]);
  585. for (i=1; i<heater_ttbllen_map[e]; i++)
  586. {
  587. if (PGM_RD_W((*tt)[i][0]) > raw)
  588. {
  589. celsius = PGM_RD_W((*tt)[i-1][1]) +
  590. (raw - PGM_RD_W((*tt)[i-1][0])) *
  591. (float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i-1][1])) /
  592. (float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i-1][0]));
  593. break;
  594. }
  595. }
  596. // Overflow: Set to last value in the table
  597. if (i == heater_ttbllen_map[e]) celsius = PGM_RD_W((*tt)[i-1][1]);
  598. return celsius;
  599. }
  600. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  601. }
  602. // Derived from RepRap FiveD extruder::getTemperature()
  603. // For bed temperature measurement.
  604. static float analog2tempBed(int raw) {
  605. #ifdef BED_USES_THERMISTOR
  606. float celsius = 0;
  607. byte i;
  608. for (i=1; i<BEDTEMPTABLE_LEN; i++)
  609. {
  610. if (PGM_RD_W(BEDTEMPTABLE[i][0]) > raw)
  611. {
  612. celsius = PGM_RD_W(BEDTEMPTABLE[i-1][1]) +
  613. (raw - PGM_RD_W(BEDTEMPTABLE[i-1][0])) *
  614. (float)(PGM_RD_W(BEDTEMPTABLE[i][1]) - PGM_RD_W(BEDTEMPTABLE[i-1][1])) /
  615. (float)(PGM_RD_W(BEDTEMPTABLE[i][0]) - PGM_RD_W(BEDTEMPTABLE[i-1][0]));
  616. break;
  617. }
  618. }
  619. // Overflow: Set to last value in the table
  620. if (i == BEDTEMPTABLE_LEN) celsius = PGM_RD_W(BEDTEMPTABLE[i-1][1]);
  621. return celsius;
  622. #elif defined BED_USES_AD595
  623. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  624. #else
  625. return 0;
  626. #endif
  627. }
  628. /* Called to get the raw values into the the actual temperatures. The raw values are created in interrupt context,
  629. and this function is called from normal context as it is too slow to run in interrupts and will block the stepper routine otherwise */
  630. static void updateTemperaturesFromRawValues()
  631. {
  632. for(uint8_t e=0;e<EXTRUDERS;e++)
  633. {
  634. current_temperature[e] = analog2temp(current_temperature_raw[e], e);
  635. }
  636. current_temperature_bed = analog2tempBed(current_temperature_bed_raw);
  637. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  638. redundant_temperature = analog2temp(redundant_temperature_raw, 1);
  639. #endif
  640. #ifdef FILAMENT_SENSOR && (FILWIDTH_PIN > -1) //check if a sensor is supported
  641. filament_width_meas = analog2widthFil();
  642. #endif
  643. //Reset the watchdog after we know we have a temperature measurement.
  644. watchdog_reset();
  645. CRITICAL_SECTION_START;
  646. temp_meas_ready = false;
  647. CRITICAL_SECTION_END;
  648. }
  649. // For converting raw Filament Width to milimeters
  650. #ifdef FILAMENT_SENSOR
  651. float analog2widthFil() {
  652. return current_raw_filwidth/16383.0*5.0;
  653. //return current_raw_filwidth;
  654. }
  655. // For converting raw Filament Width to a ratio
  656. int widthFil_to_size_ratio() {
  657. float temp;
  658. temp=filament_width_meas;
  659. if(filament_width_meas<MEASURED_LOWER_LIMIT)
  660. temp=filament_width_nominal; //assume sensor cut out
  661. else if (filament_width_meas>MEASURED_UPPER_LIMIT)
  662. temp= MEASURED_UPPER_LIMIT;
  663. return(filament_width_nominal/temp*100);
  664. }
  665. #endif
  666. void tp_init()
  667. {
  668. #if (MOTHERBOARD == 80) && ((TEMP_SENSOR_0==-1)||(TEMP_SENSOR_1==-1)||(TEMP_SENSOR_2==-1)||(TEMP_SENSOR_BED==-1))
  669. //disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector
  670. MCUCR=(1<<JTD);
  671. MCUCR=(1<<JTD);
  672. #endif
  673. // Finish init of mult extruder arrays
  674. for(int e = 0; e < EXTRUDERS; e++) {
  675. // populate with the first value
  676. maxttemp[e] = maxttemp[0];
  677. #ifdef PIDTEMP
  678. temp_iState_min[e] = 0.0;
  679. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / Ki;
  680. #endif //PIDTEMP
  681. #ifdef PIDTEMPBED
  682. temp_iState_min_bed = 0.0;
  683. temp_iState_max_bed = PID_INTEGRAL_DRIVE_MAX / bedKi;
  684. #endif //PIDTEMPBED
  685. }
  686. #if defined(HEATER_0_PIN) && (HEATER_0_PIN > -1)
  687. SET_OUTPUT(HEATER_0_PIN);
  688. #endif
  689. #if defined(HEATER_1_PIN) && (HEATER_1_PIN > -1)
  690. SET_OUTPUT(HEATER_1_PIN);
  691. #endif
  692. #if defined(HEATER_2_PIN) && (HEATER_2_PIN > -1)
  693. SET_OUTPUT(HEATER_2_PIN);
  694. #endif
  695. #if defined(HEATER_BED_PIN) && (HEATER_BED_PIN > -1)
  696. SET_OUTPUT(HEATER_BED_PIN);
  697. #endif
  698. #if defined(FAN_PIN) && (FAN_PIN > -1)
  699. SET_OUTPUT(FAN_PIN);
  700. #ifdef FAST_PWM_FAN
  701. setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  702. #endif
  703. #ifdef FAN_SOFT_PWM
  704. soft_pwm_fan = fanSpeedSoftPwm / 2;
  705. #endif
  706. #endif
  707. #ifdef HEATER_0_USES_MAX6675
  708. #ifndef SDSUPPORT
  709. SET_OUTPUT(MAX_SCK_PIN);
  710. WRITE(MAX_SCK_PIN,0);
  711. SET_OUTPUT(MAX_MOSI_PIN);
  712. WRITE(MAX_MOSI_PIN,1);
  713. SET_INPUT(MAX_MISO_PIN);
  714. WRITE(MAX_MISO_PIN,1);
  715. #endif
  716. SET_OUTPUT(MAX6675_SS);
  717. WRITE(MAX6675_SS,1);
  718. #endif
  719. // Set analog inputs
  720. ADCSRA = 1<<ADEN | 1<<ADSC | 1<<ADIF | 0x07;
  721. DIDR0 = 0;
  722. #ifdef DIDR2
  723. DIDR2 = 0;
  724. #endif
  725. #if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
  726. #if TEMP_0_PIN < 8
  727. DIDR0 |= 1 << TEMP_0_PIN;
  728. #else
  729. DIDR2 |= 1<<(TEMP_0_PIN - 8);
  730. #endif
  731. #endif
  732. #if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1)
  733. #if TEMP_1_PIN < 8
  734. DIDR0 |= 1<<TEMP_1_PIN;
  735. #else
  736. DIDR2 |= 1<<(TEMP_1_PIN - 8);
  737. #endif
  738. #endif
  739. #if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
  740. #if TEMP_2_PIN < 8
  741. DIDR0 |= 1 << TEMP_2_PIN;
  742. #else
  743. DIDR2 |= 1<<(TEMP_2_PIN - 8);
  744. #endif
  745. #endif
  746. #if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
  747. #if TEMP_BED_PIN < 8
  748. DIDR0 |= 1<<TEMP_BED_PIN;
  749. #else
  750. DIDR2 |= 1<<(TEMP_BED_PIN - 8);
  751. #endif
  752. #endif
  753. //Added for Filament Sensor
  754. #ifdef FILAMENT_SENSOR
  755. #if defined(FILWIDTH_PIN) && (FILWIDTH_PIN > -1)
  756. #if FILWIDTH_PIN < 8
  757. DIDR0 |= 1<<FILWIDTH_PIN;
  758. #else
  759. DIDR2 |= 1<<(FILWIDTH_PIN - 8);
  760. #endif
  761. #endif
  762. #endif
  763. // Use timer0 for temperature measurement
  764. // Interleave temperature interrupt with millies interrupt
  765. OCR0B = 128;
  766. TIMSK0 |= (1<<OCIE0B);
  767. // Wait for temperature measurement to settle
  768. delay(250);
  769. #ifdef HEATER_0_MINTEMP
  770. minttemp[0] = HEATER_0_MINTEMP;
  771. while(analog2temp(minttemp_raw[0], 0) < HEATER_0_MINTEMP) {
  772. #if HEATER_0_RAW_LO_TEMP < HEATER_0_RAW_HI_TEMP
  773. minttemp_raw[0] += OVERSAMPLENR;
  774. #else
  775. minttemp_raw[0] -= OVERSAMPLENR;
  776. #endif
  777. }
  778. #endif //MINTEMP
  779. #ifdef HEATER_0_MAXTEMP
  780. maxttemp[0] = HEATER_0_MAXTEMP;
  781. while(analog2temp(maxttemp_raw[0], 0) > HEATER_0_MAXTEMP) {
  782. #if HEATER_0_RAW_LO_TEMP < HEATER_0_RAW_HI_TEMP
  783. maxttemp_raw[0] -= OVERSAMPLENR;
  784. #else
  785. maxttemp_raw[0] += OVERSAMPLENR;
  786. #endif
  787. }
  788. #endif //MAXTEMP
  789. #if (EXTRUDERS > 1) && defined(HEATER_1_MINTEMP)
  790. minttemp[1] = HEATER_1_MINTEMP;
  791. while(analog2temp(minttemp_raw[1], 1) < HEATER_1_MINTEMP) {
  792. #if HEATER_1_RAW_LO_TEMP < HEATER_1_RAW_HI_TEMP
  793. minttemp_raw[1] += OVERSAMPLENR;
  794. #else
  795. minttemp_raw[1] -= OVERSAMPLENR;
  796. #endif
  797. }
  798. #endif // MINTEMP 1
  799. #if (EXTRUDERS > 1) && defined(HEATER_1_MAXTEMP)
  800. maxttemp[1] = HEATER_1_MAXTEMP;
  801. while(analog2temp(maxttemp_raw[1], 1) > HEATER_1_MAXTEMP) {
  802. #if HEATER_1_RAW_LO_TEMP < HEATER_1_RAW_HI_TEMP
  803. maxttemp_raw[1] -= OVERSAMPLENR;
  804. #else
  805. maxttemp_raw[1] += OVERSAMPLENR;
  806. #endif
  807. }
  808. #endif //MAXTEMP 1
  809. #if (EXTRUDERS > 2) && defined(HEATER_2_MINTEMP)
  810. minttemp[2] = HEATER_2_MINTEMP;
  811. while(analog2temp(minttemp_raw[2], 2) < HEATER_2_MINTEMP) {
  812. #if HEATER_2_RAW_LO_TEMP < HEATER_2_RAW_HI_TEMP
  813. minttemp_raw[2] += OVERSAMPLENR;
  814. #else
  815. minttemp_raw[2] -= OVERSAMPLENR;
  816. #endif
  817. }
  818. #endif //MINTEMP 2
  819. #if (EXTRUDERS > 2) && defined(HEATER_2_MAXTEMP)
  820. maxttemp[2] = HEATER_2_MAXTEMP;
  821. while(analog2temp(maxttemp_raw[2], 2) > HEATER_2_MAXTEMP) {
  822. #if HEATER_2_RAW_LO_TEMP < HEATER_2_RAW_HI_TEMP
  823. maxttemp_raw[2] -= OVERSAMPLENR;
  824. #else
  825. maxttemp_raw[2] += OVERSAMPLENR;
  826. #endif
  827. }
  828. #endif //MAXTEMP 2
  829. #ifdef BED_MINTEMP
  830. /* No bed MINTEMP error implemented?!? */ /*
  831. while(analog2tempBed(bed_minttemp_raw) < BED_MINTEMP) {
  832. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  833. bed_minttemp_raw += OVERSAMPLENR;
  834. #else
  835. bed_minttemp_raw -= OVERSAMPLENR;
  836. #endif
  837. }
  838. */
  839. #endif //BED_MINTEMP
  840. #ifdef BED_MAXTEMP
  841. while(analog2tempBed(bed_maxttemp_raw) > BED_MAXTEMP) {
  842. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  843. bed_maxttemp_raw -= OVERSAMPLENR;
  844. #else
  845. bed_maxttemp_raw += OVERSAMPLENR;
  846. #endif
  847. }
  848. #endif //BED_MAXTEMP
  849. }
  850. void setWatch()
  851. {
  852. #ifdef WATCH_TEMP_PERIOD
  853. for (int e = 0; e < EXTRUDERS; e++)
  854. {
  855. if(degHotend(e) < degTargetHotend(e) - (WATCH_TEMP_INCREASE * 2))
  856. {
  857. watch_start_temp[e] = degHotend(e);
  858. watchmillis[e] = millis();
  859. }
  860. }
  861. #endif
  862. }
  863. #ifdef THERMAL_RUNAWAY_PROTECTION_PERIOD && THERMAL_RUNAWAY_PROTECTION_PERIOD > 0
  864. void thermal_runaway_protection(int *state, unsigned long *timer, float temperature, float target_temperature, int heater_id, int period_seconds, int hysteresis_degc)
  865. {
  866. /*
  867. SERIAL_ECHO_START;
  868. SERIAL_ECHO("Thermal Thermal Runaway Running. Heater ID:");
  869. SERIAL_ECHO(heater_id);
  870. SERIAL_ECHO(" ; State:");
  871. SERIAL_ECHO(*state);
  872. SERIAL_ECHO(" ; Timer:");
  873. SERIAL_ECHO(*timer);
  874. SERIAL_ECHO(" ; Temperature:");
  875. SERIAL_ECHO(temperature);
  876. SERIAL_ECHO(" ; Target Temp:");
  877. SERIAL_ECHO(target_temperature);
  878. SERIAL_ECHOLN("");
  879. */
  880. if ((target_temperature == 0) || thermal_runaway)
  881. {
  882. *state = 0;
  883. *timer = 0;
  884. return;
  885. }
  886. switch (*state)
  887. {
  888. case 0: // "Heater Inactive" state
  889. if (target_temperature > 0) *state = 1;
  890. break;
  891. case 1: // "First Heating" state
  892. if (temperature >= target_temperature) *state = 2;
  893. break;
  894. case 2: // "Temperature Stable" state
  895. if (temperature >= (target_temperature - hysteresis_degc))
  896. {
  897. *timer = millis();
  898. }
  899. else if ( (millis() - *timer) > period_seconds*1000)
  900. {
  901. SERIAL_ERROR_START;
  902. SERIAL_ERRORLNPGM("Thermal Runaway, system stopped! Heater_ID: ");
  903. SERIAL_ERRORLN((int)heater_id);
  904. LCD_ALERTMESSAGEPGM("THERMAL RUNAWAY");
  905. thermal_runaway = true;
  906. while(1)
  907. {
  908. disable_heater();
  909. disable_x();
  910. disable_y();
  911. disable_z();
  912. disable_e0();
  913. disable_e1();
  914. disable_e2();
  915. manage_heater();
  916. lcd_update();
  917. }
  918. }
  919. break;
  920. }
  921. }
  922. #endif
  923. void disable_heater()
  924. {
  925. for(int i=0;i<EXTRUDERS;i++)
  926. setTargetHotend(0,i);
  927. setTargetBed(0);
  928. #if defined(TEMP_0_PIN) && TEMP_0_PIN > -1
  929. target_temperature[0]=0;
  930. soft_pwm[0]=0;
  931. #if defined(HEATER_0_PIN) && HEATER_0_PIN > -1
  932. WRITE(HEATER_0_PIN,LOW);
  933. #endif
  934. #endif
  935. #if defined(TEMP_1_PIN) && TEMP_1_PIN > -1 && EXTRUDERS > 1
  936. target_temperature[1]=0;
  937. soft_pwm[1]=0;
  938. #if defined(HEATER_1_PIN) && HEATER_1_PIN > -1
  939. WRITE(HEATER_1_PIN,LOW);
  940. #endif
  941. #endif
  942. #if defined(TEMP_2_PIN) && TEMP_2_PIN > -1 && EXTRUDERS > 2
  943. target_temperature[2]=0;
  944. soft_pwm[2]=0;
  945. #if defined(HEATER_2_PIN) && HEATER_2_PIN > -1
  946. WRITE(HEATER_2_PIN,LOW);
  947. #endif
  948. #endif
  949. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  950. target_temperature_bed=0;
  951. soft_pwm_bed=0;
  952. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  953. WRITE(HEATER_BED_PIN,LOW);
  954. #endif
  955. #endif
  956. }
  957. void max_temp_error(uint8_t e) {
  958. disable_heater();
  959. if(IsStopped() == false) {
  960. SERIAL_ERROR_START;
  961. SERIAL_ERRORLN((int)e);
  962. SERIAL_ERRORLNPGM(": Extruder switched off. MAXTEMP triggered !");
  963. LCD_ALERTMESSAGEPGM("Err: MAXTEMP");
  964. }
  965. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  966. Stop();
  967. #endif
  968. }
  969. void min_temp_error(uint8_t e) {
  970. disable_heater();
  971. if(IsStopped() == false) {
  972. SERIAL_ERROR_START;
  973. SERIAL_ERRORLN((int)e);
  974. SERIAL_ERRORLNPGM(": Extruder switched off. MINTEMP triggered !");
  975. LCD_ALERTMESSAGEPGM("Err: MINTEMP");
  976. }
  977. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  978. Stop();
  979. #endif
  980. }
  981. void bed_max_temp_error(void) {
  982. #if HEATER_BED_PIN > -1
  983. WRITE(HEATER_BED_PIN, 0);
  984. #endif
  985. if(IsStopped() == false) {
  986. SERIAL_ERROR_START;
  987. SERIAL_ERRORLNPGM("Temperature heated bed switched off. MAXTEMP triggered !!");
  988. LCD_ALERTMESSAGEPGM("Err: MAXTEMP BED");
  989. }
  990. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  991. Stop();
  992. #endif
  993. }
  994. #ifdef HEATER_0_USES_MAX6675
  995. #define MAX6675_HEAT_INTERVAL 250
  996. long max6675_previous_millis = -HEAT_INTERVAL;
  997. int max6675_temp = 2000;
  998. int read_max6675()
  999. {
  1000. if (millis() - max6675_previous_millis < MAX6675_HEAT_INTERVAL)
  1001. return max6675_temp;
  1002. max6675_previous_millis = millis();
  1003. max6675_temp = 0;
  1004. #ifdef PRR
  1005. PRR &= ~(1<<PRSPI);
  1006. #elif defined PRR0
  1007. PRR0 &= ~(1<<PRSPI);
  1008. #endif
  1009. SPCR = (1<<MSTR) | (1<<SPE) | (1<<SPR0);
  1010. // enable TT_MAX6675
  1011. WRITE(MAX6675_SS, 0);
  1012. // ensure 100ns delay - a bit extra is fine
  1013. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1014. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1015. // read MSB
  1016. SPDR = 0;
  1017. for (;(SPSR & (1<<SPIF)) == 0;);
  1018. max6675_temp = SPDR;
  1019. max6675_temp <<= 8;
  1020. // read LSB
  1021. SPDR = 0;
  1022. for (;(SPSR & (1<<SPIF)) == 0;);
  1023. max6675_temp |= SPDR;
  1024. // disable TT_MAX6675
  1025. WRITE(MAX6675_SS, 1);
  1026. if (max6675_temp & 4)
  1027. {
  1028. // thermocouple open
  1029. max6675_temp = 2000;
  1030. }
  1031. else
  1032. {
  1033. max6675_temp = max6675_temp >> 3;
  1034. }
  1035. return max6675_temp;
  1036. }
  1037. #endif
  1038. // Timer 0 is shared with millies
  1039. ISR(TIMER0_COMPB_vect)
  1040. {
  1041. //these variables are only accesible from the ISR, but static, so they don't lose their value
  1042. static unsigned char temp_count = 0;
  1043. static unsigned long raw_temp_0_value = 0;
  1044. static unsigned long raw_temp_1_value = 0;
  1045. static unsigned long raw_temp_2_value = 0;
  1046. static unsigned long raw_temp_bed_value = 0;
  1047. static unsigned char temp_state = 10;
  1048. static unsigned char pwm_count = (1 << SOFT_PWM_SCALE);
  1049. static unsigned char soft_pwm_0;
  1050. #if (EXTRUDERS > 1) || defined(HEATERS_PARALLEL)
  1051. static unsigned char soft_pwm_1;
  1052. #endif
  1053. #if EXTRUDERS > 2
  1054. static unsigned char soft_pwm_2;
  1055. #endif
  1056. #if HEATER_BED_PIN > -1
  1057. static unsigned char soft_pwm_b;
  1058. #endif
  1059. #if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1)
  1060. static unsigned long raw_filwidth_value = 0; //added for filament width sensor
  1061. #endif
  1062. if(pwm_count == 0){
  1063. soft_pwm_0 = soft_pwm[0];
  1064. if(soft_pwm_0 > 0) {
  1065. WRITE(HEATER_0_PIN,1);
  1066. #ifdef HEATERS_PARALLEL
  1067. WRITE(HEATER_1_PIN,1);
  1068. #endif
  1069. } else WRITE(HEATER_0_PIN,0);
  1070. #if EXTRUDERS > 1
  1071. soft_pwm_1 = soft_pwm[1];
  1072. if(soft_pwm_1 > 0) WRITE(HEATER_1_PIN,1); else WRITE(HEATER_1_PIN,0);
  1073. #endif
  1074. #if EXTRUDERS > 2
  1075. soft_pwm_2 = soft_pwm[2];
  1076. if(soft_pwm_2 > 0) WRITE(HEATER_2_PIN,1); else WRITE(HEATER_2_PIN,0);
  1077. #endif
  1078. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1079. soft_pwm_b = soft_pwm_bed;
  1080. if(soft_pwm_b > 0) WRITE(HEATER_BED_PIN,1); else WRITE(HEATER_BED_PIN,0);
  1081. #endif
  1082. #ifdef FAN_SOFT_PWM
  1083. soft_pwm_fan = fanSpeedSoftPwm / 2;
  1084. if(soft_pwm_fan > 0) WRITE(FAN_PIN,1); else WRITE(FAN_PIN,0);
  1085. #endif
  1086. }
  1087. if(soft_pwm_0 < pwm_count) {
  1088. WRITE(HEATER_0_PIN,0);
  1089. #ifdef HEATERS_PARALLEL
  1090. WRITE(HEATER_1_PIN,0);
  1091. #endif
  1092. }
  1093. #if EXTRUDERS > 1
  1094. if(soft_pwm_1 < pwm_count) WRITE(HEATER_1_PIN,0);
  1095. #endif
  1096. #if EXTRUDERS > 2
  1097. if(soft_pwm_2 < pwm_count) WRITE(HEATER_2_PIN,0);
  1098. #endif
  1099. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1100. if(soft_pwm_b < pwm_count) WRITE(HEATER_BED_PIN,0);
  1101. #endif
  1102. #ifdef FAN_SOFT_PWM
  1103. if(soft_pwm_fan < pwm_count) WRITE(FAN_PIN,0);
  1104. #endif
  1105. pwm_count += (1 << SOFT_PWM_SCALE);
  1106. pwm_count &= 0x7f;
  1107. switch(temp_state) {
  1108. case 0: // Prepare TEMP_0
  1109. #if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
  1110. #if TEMP_0_PIN > 7
  1111. ADCSRB = 1<<MUX5;
  1112. #else
  1113. ADCSRB = 0;
  1114. #endif
  1115. ADMUX = ((1 << REFS0) | (TEMP_0_PIN & 0x07));
  1116. ADCSRA |= 1<<ADSC; // Start conversion
  1117. #endif
  1118. lcd_buttons_update();
  1119. temp_state = 1;
  1120. break;
  1121. case 1: // Measure TEMP_0
  1122. #if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
  1123. raw_temp_0_value += ADC;
  1124. #endif
  1125. #ifdef HEATER_0_USES_MAX6675 // TODO remove the blocking
  1126. raw_temp_0_value = read_max6675();
  1127. #endif
  1128. temp_state = 2;
  1129. break;
  1130. case 2: // Prepare TEMP_BED
  1131. #if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
  1132. #if TEMP_BED_PIN > 7
  1133. ADCSRB = 1<<MUX5;
  1134. #else
  1135. ADCSRB = 0;
  1136. #endif
  1137. ADMUX = ((1 << REFS0) | (TEMP_BED_PIN & 0x07));
  1138. ADCSRA |= 1<<ADSC; // Start conversion
  1139. #endif
  1140. lcd_buttons_update();
  1141. temp_state = 3;
  1142. break;
  1143. case 3: // Measure TEMP_BED
  1144. #if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
  1145. raw_temp_bed_value += ADC;
  1146. #endif
  1147. temp_state = 4;
  1148. break;
  1149. case 4: // Prepare TEMP_1
  1150. #if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1)
  1151. #if TEMP_1_PIN > 7
  1152. ADCSRB = 1<<MUX5;
  1153. #else
  1154. ADCSRB = 0;
  1155. #endif
  1156. ADMUX = ((1 << REFS0) | (TEMP_1_PIN & 0x07));
  1157. ADCSRA |= 1<<ADSC; // Start conversion
  1158. #endif
  1159. lcd_buttons_update();
  1160. temp_state = 5;
  1161. break;
  1162. case 5: // Measure TEMP_1
  1163. #if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1)
  1164. raw_temp_1_value += ADC;
  1165. #endif
  1166. temp_state = 6;
  1167. break;
  1168. case 6: // Prepare TEMP_2
  1169. #if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
  1170. #if TEMP_2_PIN > 7
  1171. ADCSRB = 1<<MUX5;
  1172. #else
  1173. ADCSRB = 0;
  1174. #endif
  1175. ADMUX = ((1 << REFS0) | (TEMP_2_PIN & 0x07));
  1176. ADCSRA |= 1<<ADSC; // Start conversion
  1177. #endif
  1178. lcd_buttons_update();
  1179. temp_state = 7;
  1180. break;
  1181. case 7: // Measure TEMP_2
  1182. #if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
  1183. raw_temp_2_value += ADC;
  1184. #endif
  1185. temp_state = 8;//change so that Filament Width is also measured
  1186. break;
  1187. case 8: //Prepare FILWIDTH
  1188. #if defined(FILWIDTH_PIN) && (FILWIDTH_PIN> -1)
  1189. #if FILWIDTH_PIN>7
  1190. ADCSRB = 1<<MUX5;
  1191. #else
  1192. ADCSRB = 0;
  1193. #endif
  1194. ADMUX = ((1 << REFS0) | (FILWIDTH_PIN & 0x07));
  1195. ADCSRA |= 1<<ADSC; // Start conversion
  1196. #endif
  1197. lcd_buttons_update();
  1198. temp_state = 9;
  1199. break;
  1200. case 9: //Measure FILWIDTH
  1201. #if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1)
  1202. //raw_filwidth_value += ADC; //remove to use an IIR filter approach
  1203. if(ADC>102) //check that ADC is reading a voltage > 0.5 volts, otherwise don't take in the data.
  1204. {
  1205. raw_filwidth_value= raw_filwidth_value-(raw_filwidth_value>>7); //multipliy raw_filwidth_value by 127/128
  1206. raw_filwidth_value= raw_filwidth_value + ((unsigned long)ADC<<7); //add new ADC reading
  1207. }
  1208. #endif
  1209. temp_state = 0;
  1210. temp_count++;
  1211. break;
  1212. case 10: //Startup, delay initial temp reading a tiny bit so the hardware can settle.
  1213. temp_state = 0;
  1214. break;
  1215. // default:
  1216. // SERIAL_ERROR_START;
  1217. // SERIAL_ERRORLNPGM("Temp measurement error!");
  1218. // break;
  1219. }
  1220. if(temp_count >= OVERSAMPLENR) // 10 * 16 * 1/(16000000/64/256) = 164ms.
  1221. {
  1222. if (!temp_meas_ready) //Only update the raw values if they have been read. Else we could be updating them during reading.
  1223. {
  1224. current_temperature_raw[0] = raw_temp_0_value;
  1225. #if EXTRUDERS > 1
  1226. current_temperature_raw[1] = raw_temp_1_value;
  1227. #endif
  1228. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  1229. redundant_temperature_raw = raw_temp_1_value;
  1230. #endif
  1231. #if EXTRUDERS > 2
  1232. current_temperature_raw[2] = raw_temp_2_value;
  1233. #endif
  1234. current_temperature_bed_raw = raw_temp_bed_value;
  1235. }
  1236. //Add similar code for Filament Sensor - can be read any time since IIR filtering is used
  1237. #if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1)
  1238. current_raw_filwidth = raw_filwidth_value>>10; //need to divide to get to 0-16384 range since we used 1/128 IIR filter approach
  1239. #endif
  1240. temp_meas_ready = true;
  1241. temp_count = 0;
  1242. raw_temp_0_value = 0;
  1243. raw_temp_1_value = 0;
  1244. raw_temp_2_value = 0;
  1245. raw_temp_bed_value = 0;
  1246. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1247. if(current_temperature_raw[0] <= maxttemp_raw[0]) {
  1248. #else
  1249. if(current_temperature_raw[0] >= maxttemp_raw[0]) {
  1250. #endif
  1251. max_temp_error(0);
  1252. }
  1253. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1254. if(current_temperature_raw[0] >= minttemp_raw[0]) {
  1255. #else
  1256. if(current_temperature_raw[0] <= minttemp_raw[0]) {
  1257. #endif
  1258. min_temp_error(0);
  1259. }
  1260. #if EXTRUDERS > 1
  1261. #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
  1262. if(current_temperature_raw[1] <= maxttemp_raw[1]) {
  1263. #else
  1264. if(current_temperature_raw[1] >= maxttemp_raw[1]) {
  1265. #endif
  1266. max_temp_error(1);
  1267. }
  1268. #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
  1269. if(current_temperature_raw[1] >= minttemp_raw[1]) {
  1270. #else
  1271. if(current_temperature_raw[1] <= minttemp_raw[1]) {
  1272. #endif
  1273. min_temp_error(1);
  1274. }
  1275. #endif
  1276. #if EXTRUDERS > 2
  1277. #if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
  1278. if(current_temperature_raw[2] <= maxttemp_raw[2]) {
  1279. #else
  1280. if(current_temperature_raw[2] >= maxttemp_raw[2]) {
  1281. #endif
  1282. max_temp_error(2);
  1283. }
  1284. #if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
  1285. if(current_temperature_raw[2] >= minttemp_raw[2]) {
  1286. #else
  1287. if(current_temperature_raw[2] <= minttemp_raw[2]) {
  1288. #endif
  1289. min_temp_error(2);
  1290. }
  1291. #endif
  1292. /* No bed MINTEMP error? */
  1293. #if defined(BED_MAXTEMP) && (TEMP_SENSOR_BED != 0)
  1294. # if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1295. if(current_temperature_bed_raw <= bed_maxttemp_raw) {
  1296. #else
  1297. if(current_temperature_bed_raw >= bed_maxttemp_raw) {
  1298. #endif
  1299. target_temperature_bed = 0;
  1300. bed_max_temp_error();
  1301. }
  1302. #endif
  1303. }
  1304. #ifdef BABYSTEPPING
  1305. for(uint8_t axis=0;axis<3;axis++)
  1306. {
  1307. int curTodo=babystepsTodo[axis]; //get rid of volatile for performance
  1308. if(curTodo>0)
  1309. {
  1310. babystep(axis,/*fwd*/true);
  1311. babystepsTodo[axis]--; //less to do next time
  1312. }
  1313. else
  1314. if(curTodo<0)
  1315. {
  1316. babystep(axis,/*fwd*/false);
  1317. babystepsTodo[axis]++; //less to do next time
  1318. }
  1319. }
  1320. #endif //BABYSTEPPING
  1321. }
  1322. #ifdef PIDTEMP
  1323. // Apply the scale factors to the PID values
  1324. float scalePID_i(float i)
  1325. {
  1326. return i*PID_dT;
  1327. }
  1328. float unscalePID_i(float i)
  1329. {
  1330. return i/PID_dT;
  1331. }
  1332. float scalePID_d(float d)
  1333. {
  1334. return d/PID_dT;
  1335. }
  1336. float unscalePID_d(float d)
  1337. {
  1338. return d*PID_dT;
  1339. }
  1340. #endif //PIDTEMP