My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

temperature.cpp 44KB

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