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

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