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