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

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