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

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