temperature.cpp 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986
  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 "cardreader.h"
  28. #include "Sd2PinMap.h"
  29. //===========================================================================
  30. //=============================public variables============================
  31. //===========================================================================
  32. int target_temperature[EXTRUDERS] = { 0 };
  33. int target_temperature_bed = 0;
  34. int current_temperature_raw[EXTRUDERS] = { 0 };
  35. float current_temperature[EXTRUDERS] = { 0.0 };
  36. int current_temperature_bed_raw = 0;
  37. float current_temperature_bed = 0.0;
  38. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  39. int redundant_temperature_raw = 0;
  40. float redundant_temperature = 0.0;
  41. #endif
  42. #ifdef PIDTEMP
  43. float _Kp, _Ki, _Kd;
  44. int pid_cycle, pid_number_of_cycles;
  45. bool pid_tuning_finished = false;
  46. float Kp=DEFAULT_Kp;
  47. float Ki=(DEFAULT_Ki*PID_dT);
  48. float Kd=(DEFAULT_Kd/PID_dT);
  49. #ifdef PID_ADD_EXTRUSION_RATE
  50. float Kc=DEFAULT_Kc;
  51. #endif
  52. #endif //PIDTEMP
  53. #ifdef PIDTEMPBED
  54. float bedKp=DEFAULT_bedKp;
  55. float bedKi=(DEFAULT_bedKi*PID_dT);
  56. float bedKd=(DEFAULT_bedKd/PID_dT);
  57. #endif //PIDTEMPBED
  58. #ifdef FAN_SOFT_PWM
  59. unsigned char fanSpeedSoftPwm;
  60. #endif
  61. unsigned char soft_pwm_bed;
  62. #ifdef BABYSTEPPING
  63. volatile int babystepsTodo[3]={0,0,0};
  64. #endif
  65. #ifdef FILAMENT_SENSOR
  66. int current_raw_filwidth = 0; //Holds measured filament diameter - one extruder only
  67. #endif
  68. //===========================================================================
  69. //=============================private variables============================
  70. //===========================================================================
  71. static volatile bool temp_meas_ready = false;
  72. #ifdef PIDTEMP
  73. //static cannot be external:
  74. static float temp_iState[EXTRUDERS] = { 0 };
  75. static float temp_dState[EXTRUDERS] = { 0 };
  76. static float pTerm[EXTRUDERS];
  77. static float iTerm[EXTRUDERS];
  78. static float dTerm[EXTRUDERS];
  79. //int output;
  80. static float pid_error[EXTRUDERS];
  81. static float temp_iState_min[EXTRUDERS];
  82. static float temp_iState_max[EXTRUDERS];
  83. // static float pid_input[EXTRUDERS];
  84. // static float pid_output[EXTRUDERS];
  85. static bool pid_reset[EXTRUDERS];
  86. #endif //PIDTEMP
  87. #ifdef PIDTEMPBED
  88. //static cannot be external:
  89. static float temp_iState_bed = { 0 };
  90. static float temp_dState_bed = { 0 };
  91. static float pTerm_bed;
  92. static float iTerm_bed;
  93. static float dTerm_bed;
  94. //int output;
  95. static float pid_error_bed;
  96. static float temp_iState_min_bed;
  97. static float temp_iState_max_bed;
  98. #else //PIDTEMPBED
  99. static unsigned long previous_millis_bed_heater;
  100. #endif //PIDTEMPBED
  101. static unsigned char soft_pwm[EXTRUDERS];
  102. #ifdef FAN_SOFT_PWM
  103. static unsigned char soft_pwm_fan;
  104. #endif
  105. #if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
  106. (defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
  107. (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1)
  108. static unsigned long extruder_autofan_last_check;
  109. #endif
  110. #if EXTRUDERS > 3
  111. # error Unsupported number of extruders
  112. #elif EXTRUDERS > 2
  113. # define ARRAY_BY_EXTRUDERS(v1, v2, v3) { v1, v2, v3 }
  114. #elif EXTRUDERS > 1
  115. # define ARRAY_BY_EXTRUDERS(v1, v2, v3) { v1, v2 }
  116. #else
  117. # define ARRAY_BY_EXTRUDERS(v1, v2, v3) { v1 }
  118. #endif
  119. // Init min and max temp with extreme values to prevent false errors during startup
  120. static int minttemp_raw[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_RAW_LO_TEMP , HEATER_1_RAW_LO_TEMP , HEATER_2_RAW_LO_TEMP );
  121. static int maxttemp_raw[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_RAW_HI_TEMP , HEATER_1_RAW_HI_TEMP , HEATER_2_RAW_HI_TEMP );
  122. static int minttemp[EXTRUDERS] = ARRAY_BY_EXTRUDERS( 0, 0, 0 );
  123. static int maxttemp[EXTRUDERS] = ARRAY_BY_EXTRUDERS( 16383, 16383, 16383 );
  124. #ifdef BED_MINTEMP
  125. static int bed_minttemp_raw = HEATER_BED_RAW_LO_TEMP;
  126. #endif
  127. #ifdef BED_MAXTEMP
  128. static int bed_maxttemp_raw = HEATER_BED_RAW_HI_TEMP;
  129. #endif
  130. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  131. static void *heater_ttbl_map[2] = {(void *)HEATER_0_TEMPTABLE, (void *)HEATER_1_TEMPTABLE };
  132. static uint8_t heater_ttbllen_map[2] = { HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN };
  133. #else
  134. static void *heater_ttbl_map[EXTRUDERS] = ARRAY_BY_EXTRUDERS( (void *)HEATER_0_TEMPTABLE, (void *)HEATER_1_TEMPTABLE, (void *)HEATER_2_TEMPTABLE );
  135. static uint8_t heater_ttbllen_map[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN, HEATER_2_TEMPTABLE_LEN );
  136. #endif
  137. static float analog2temp(int raw, uint8_t e);
  138. static float analog2tempBed(int raw);
  139. static void updateTemperaturesFromRawValues();
  140. enum TempRunawayStates
  141. {
  142. TempRunaway_INACTIVE = 0,
  143. TempRunaway_PREHEAT = 1,
  144. TempRunaway_ACTIVE = 2,
  145. };
  146. #ifdef WATCH_TEMP_PERIOD
  147. int watch_start_temp[EXTRUDERS] = ARRAY_BY_EXTRUDERS(0,0,0);
  148. unsigned long watchmillis[EXTRUDERS] = ARRAY_BY_EXTRUDERS(0,0,0);
  149. #endif //WATCH_TEMP_PERIOD
  150. #ifndef SOFT_PWM_SCALE
  151. #define SOFT_PWM_SCALE 0
  152. #endif
  153. #ifdef FILAMENT_SENSOR
  154. static int meas_shift_index; //used to point to a delayed sample in buffer for filament width sensor
  155. #endif
  156. //===========================================================================
  157. //============================= functions ============================
  158. //===========================================================================
  159. void PID_autotune(float temp, int extruder, int ncycles)
  160. {
  161. pid_number_of_cycles = ncycles;
  162. pid_tuning_finished = false;
  163. float input = 0.0;
  164. pid_cycle=0;
  165. bool heating = true;
  166. unsigned long temp_millis = millis();
  167. unsigned long t1=temp_millis;
  168. unsigned long t2=temp_millis;
  169. long t_high = 0;
  170. long t_low = 0;
  171. long bias, d;
  172. float Ku, Tu;
  173. float max = 0, min = 10000;
  174. #if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
  175. (defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
  176. (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1)
  177. unsigned long extruder_autofan_last_check = millis();
  178. #endif
  179. if ((extruder >= EXTRUDERS)
  180. #if (TEMP_BED_PIN <= -1)
  181. ||(extruder < 0)
  182. #endif
  183. ){
  184. SERIAL_ECHOLN("PID Autotune failed. Bad extruder number.");
  185. pid_tuning_finished = true;
  186. pid_cycle = 0;
  187. return;
  188. }
  189. SERIAL_ECHOLN("PID Autotune start");
  190. disable_heater(); // switch off all heaters.
  191. if (extruder<0)
  192. {
  193. soft_pwm_bed = (MAX_BED_POWER)/2;
  194. bias = d = (MAX_BED_POWER)/2;
  195. }
  196. else
  197. {
  198. soft_pwm[extruder] = (PID_MAX)/2;
  199. bias = d = (PID_MAX)/2;
  200. }
  201. for(;;) {
  202. if(temp_meas_ready == true) { // temp sample ready
  203. updateTemperaturesFromRawValues();
  204. input = (extruder<0)?current_temperature_bed:current_temperature[extruder];
  205. max=max(max,input);
  206. min=min(min,input);
  207. #if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
  208. (defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
  209. (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1)
  210. if(millis() - extruder_autofan_last_check > 2500) {
  211. checkExtruderAutoFans();
  212. extruder_autofan_last_check = millis();
  213. }
  214. #endif
  215. if(heating == true && input > temp) {
  216. if(millis() - t2 > 5000) {
  217. heating=false;
  218. if (extruder<0)
  219. soft_pwm_bed = (bias - d) >> 1;
  220. else
  221. soft_pwm[extruder] = (bias - d) >> 1;
  222. t1=millis();
  223. t_high=t1 - t2;
  224. max=temp;
  225. }
  226. }
  227. if(heating == false && input < temp) {
  228. if(millis() - t1 > 5000) {
  229. heating=true;
  230. t2=millis();
  231. t_low=t2 - t1;
  232. if(pid_cycle > 0) {
  233. bias += (d*(t_high - t_low))/(t_low + t_high);
  234. bias = constrain(bias, 20 ,(extruder<0?(MAX_BED_POWER):(PID_MAX))-20);
  235. if(bias > (extruder<0?(MAX_BED_POWER):(PID_MAX))/2) d = (extruder<0?(MAX_BED_POWER):(PID_MAX)) - 1 - bias;
  236. else d = bias;
  237. SERIAL_PROTOCOLPGM(" bias: "); SERIAL_PROTOCOL(bias);
  238. SERIAL_PROTOCOLPGM(" d: "); SERIAL_PROTOCOL(d);
  239. SERIAL_PROTOCOLPGM(" min: "); SERIAL_PROTOCOL(min);
  240. SERIAL_PROTOCOLPGM(" max: "); SERIAL_PROTOCOLLN(max);
  241. if(pid_cycle > 2) {
  242. Ku = (4.0*d)/(3.14159*(max-min)/2.0);
  243. Tu = ((float)(t_low + t_high)/1000.0);
  244. SERIAL_PROTOCOLPGM(" Ku: "); SERIAL_PROTOCOL(Ku);
  245. SERIAL_PROTOCOLPGM(" Tu: "); SERIAL_PROTOCOLLN(Tu);
  246. _Kp = 0.6*Ku;
  247. _Ki = 2*_Kp/Tu;
  248. _Kd = _Kp*Tu/8;
  249. SERIAL_PROTOCOLLNPGM(" Classic PID ");
  250. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(_Kp);
  251. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(_Ki);
  252. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(_Kd);
  253. /*
  254. _Kp = 0.33*Ku;
  255. _Ki = _Kp/Tu;
  256. _Kd = _Kp*Tu/3;
  257. SERIAL_PROTOCOLLNPGM(" Some overshoot ");
  258. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(_Kp);
  259. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(_Ki);
  260. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(_Kd);
  261. _Kp = 0.2*Ku;
  262. _Ki = 2*_Kp/Tu;
  263. _Kd = _Kp*Tu/3;
  264. SERIAL_PROTOCOLLNPGM(" No overshoot ");
  265. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(_Kp);
  266. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(_Ki);
  267. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(_Kd);
  268. */
  269. }
  270. }
  271. if (extruder<0)
  272. soft_pwm_bed = (bias + d) >> 1;
  273. else
  274. soft_pwm[extruder] = (bias + d) >> 1;
  275. pid_cycle++;
  276. min=temp;
  277. }
  278. }
  279. }
  280. if(input > (temp + 20)) {
  281. SERIAL_PROTOCOLLNPGM("PID Autotune failed! Temperature too high");
  282. pid_tuning_finished = true;
  283. pid_cycle = 0;
  284. return;
  285. }
  286. if(millis() - temp_millis > 2000) {
  287. int p;
  288. if (extruder<0){
  289. p=soft_pwm_bed;
  290. SERIAL_PROTOCOLPGM("ok B:");
  291. }else{
  292. p=soft_pwm[extruder];
  293. SERIAL_PROTOCOLPGM("ok T:");
  294. }
  295. SERIAL_PROTOCOL(input);
  296. SERIAL_PROTOCOLPGM(" @:");
  297. SERIAL_PROTOCOLLN(p);
  298. temp_millis = millis();
  299. }
  300. if(((millis() - t1) + (millis() - t2)) > (10L*60L*1000L*2L)) {
  301. SERIAL_PROTOCOLLNPGM("PID Autotune failed! timeout");
  302. pid_tuning_finished = true;
  303. pid_cycle = 0;
  304. return;
  305. }
  306. if(pid_cycle > ncycles) {
  307. SERIAL_PROTOCOLLNPGM("PID Autotune finished! Put the last Kp, Ki and Kd constants from above into Configuration.h");
  308. pid_tuning_finished = true;
  309. pid_cycle = 0;
  310. return;
  311. }
  312. lcd_update();
  313. }
  314. }
  315. void updatePID()
  316. {
  317. #ifdef PIDTEMP
  318. for(int e = 0; e < EXTRUDERS; e++) {
  319. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / Ki;
  320. }
  321. #endif
  322. #ifdef PIDTEMPBED
  323. temp_iState_max_bed = PID_INTEGRAL_DRIVE_MAX / bedKi;
  324. #endif
  325. }
  326. int getHeaterPower(int heater) {
  327. if (heater<0)
  328. return soft_pwm_bed;
  329. return soft_pwm[heater];
  330. }
  331. #if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
  332. (defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
  333. (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1)
  334. #if defined(FAN_PIN) && FAN_PIN > -1
  335. #if EXTRUDER_0_AUTO_FAN_PIN == FAN_PIN
  336. #error "You cannot set EXTRUDER_0_AUTO_FAN_PIN equal to FAN_PIN"
  337. #endif
  338. #if EXTRUDER_1_AUTO_FAN_PIN == FAN_PIN
  339. #error "You cannot set EXTRUDER_1_AUTO_FAN_PIN equal to FAN_PIN"
  340. #endif
  341. #if EXTRUDER_2_AUTO_FAN_PIN == FAN_PIN
  342. #error "You cannot set EXTRUDER_2_AUTO_FAN_PIN equal to FAN_PIN"
  343. #endif
  344. #endif
  345. void setExtruderAutoFanState(int pin, bool state)
  346. {
  347. unsigned char newFanSpeed = (state != 0) ? EXTRUDER_AUTO_FAN_SPEED : 0;
  348. // this idiom allows both digital and PWM fan outputs (see M42 handling).
  349. pinMode(pin, OUTPUT);
  350. digitalWrite(pin, newFanSpeed);
  351. analogWrite(pin, newFanSpeed);
  352. }
  353. void checkExtruderAutoFans()
  354. {
  355. uint8_t fanState = 0;
  356. // which fan pins need to be turned on?
  357. #if defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1
  358. if (current_temperature[0] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  359. fanState |= 1;
  360. #endif
  361. #if defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1
  362. if (current_temperature[1] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  363. {
  364. if (EXTRUDER_1_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN)
  365. fanState |= 1;
  366. else
  367. fanState |= 2;
  368. }
  369. #endif
  370. #if defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1
  371. if (current_temperature[2] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  372. {
  373. if (EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN)
  374. fanState |= 1;
  375. else if (EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_1_AUTO_FAN_PIN)
  376. fanState |= 2;
  377. else
  378. fanState |= 4;
  379. }
  380. #endif
  381. // update extruder auto fan states
  382. #if defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1
  383. setExtruderAutoFanState(EXTRUDER_0_AUTO_FAN_PIN, (fanState & 1) != 0);
  384. #endif
  385. #if defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1
  386. if (EXTRUDER_1_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN)
  387. setExtruderAutoFanState(EXTRUDER_1_AUTO_FAN_PIN, (fanState & 2) != 0);
  388. #endif
  389. #if defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1
  390. if (EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN
  391. && EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_1_AUTO_FAN_PIN)
  392. setExtruderAutoFanState(EXTRUDER_2_AUTO_FAN_PIN, (fanState & 4) != 0);
  393. #endif
  394. }
  395. #endif // any extruder auto fan pins set
  396. void manage_heater()
  397. {
  398. float pid_input;
  399. float pid_output;
  400. if(temp_meas_ready != true) //better readability
  401. return;
  402. updateTemperaturesFromRawValues();
  403. #ifdef TEMP_RUNAWAY_BED_HYSTERESIS
  404. temp_runaway_check(0, target_temperature_bed, current_temperature_bed, (int)soft_pwm_bed, true);
  405. #endif
  406. for(int e = 0; e < EXTRUDERS; e++)
  407. {
  408. #ifdef TEMP_RUNAWAY_EXTRUDER_HYSTERESIS
  409. temp_runaway_check(e+1, target_temperature[e], current_temperature[e], (int)soft_pwm[e], false);
  410. #endif
  411. #ifdef PIDTEMP
  412. pid_input = current_temperature[e];
  413. #ifndef PID_OPENLOOP
  414. pid_error[e] = target_temperature[e] - pid_input;
  415. if(pid_error[e] > PID_FUNCTIONAL_RANGE) {
  416. pid_output = BANG_MAX;
  417. pid_reset[e] = true;
  418. }
  419. else if(pid_error[e] < -PID_FUNCTIONAL_RANGE || target_temperature[e] == 0) {
  420. pid_output = 0;
  421. pid_reset[e] = true;
  422. }
  423. else {
  424. if(pid_reset[e] == true) {
  425. temp_iState[e] = 0.0;
  426. pid_reset[e] = false;
  427. }
  428. pTerm[e] = Kp * pid_error[e];
  429. temp_iState[e] += pid_error[e];
  430. temp_iState[e] = constrain(temp_iState[e], temp_iState_min[e], temp_iState_max[e]);
  431. iTerm[e] = Ki * temp_iState[e];
  432. //K1 defined in Configuration.h in the PID settings
  433. #define K2 (1.0-K1)
  434. dTerm[e] = (Kd * (pid_input - temp_dState[e]))*K2 + (K1 * dTerm[e]);
  435. pid_output = pTerm[e] + iTerm[e] - dTerm[e];
  436. if (pid_output > PID_MAX) {
  437. if (pid_error[e] > 0 ) temp_iState[e] -= pid_error[e]; // conditional un-integration
  438. pid_output=PID_MAX;
  439. } else if (pid_output < 0){
  440. if (pid_error[e] < 0 ) temp_iState[e] -= pid_error[e]; // conditional un-integration
  441. pid_output=0;
  442. }
  443. }
  444. temp_dState[e] = pid_input;
  445. #else
  446. pid_output = constrain(target_temperature[e], 0, PID_MAX);
  447. #endif //PID_OPENLOOP
  448. #ifdef PID_DEBUG
  449. SERIAL_ECHO_START;
  450. SERIAL_ECHO(" PID_DEBUG ");
  451. SERIAL_ECHO(e);
  452. SERIAL_ECHO(": Input ");
  453. SERIAL_ECHO(pid_input);
  454. SERIAL_ECHO(" Output ");
  455. SERIAL_ECHO(pid_output);
  456. SERIAL_ECHO(" pTerm ");
  457. SERIAL_ECHO(pTerm[e]);
  458. SERIAL_ECHO(" iTerm ");
  459. SERIAL_ECHO(iTerm[e]);
  460. SERIAL_ECHO(" dTerm ");
  461. SERIAL_ECHOLN(dTerm[e]);
  462. #endif //PID_DEBUG
  463. #else /* PID off */
  464. pid_output = 0;
  465. if(current_temperature[e] < target_temperature[e]) {
  466. pid_output = PID_MAX;
  467. }
  468. #endif
  469. // Check if temperature is within the correct range
  470. if((current_temperature[e] > minttemp[e]) && (current_temperature[e] < maxttemp[e]))
  471. {
  472. soft_pwm[e] = (int)pid_output >> 1;
  473. }
  474. else {
  475. soft_pwm[e] = 0;
  476. }
  477. #ifdef WATCH_TEMP_PERIOD
  478. if(watchmillis[e] && millis() - watchmillis[e] > WATCH_TEMP_PERIOD)
  479. {
  480. if(degHotend(e) < watch_start_temp[e] + WATCH_TEMP_INCREASE)
  481. {
  482. setTargetHotend(0, e);
  483. LCD_MESSAGEPGM("Heating failed");
  484. SERIAL_ECHO_START;
  485. SERIAL_ECHOLN("Heating failed");
  486. }else{
  487. watchmillis[e] = 0;
  488. }
  489. }
  490. #endif
  491. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  492. if(fabs(current_temperature[0] - redundant_temperature) > MAX_REDUNDANT_TEMP_SENSOR_DIFF) {
  493. disable_heater();
  494. if(IsStopped() == false) {
  495. SERIAL_ERROR_START;
  496. SERIAL_ERRORLNPGM("Extruder switched off. Temperature difference between temp sensors is too high !");
  497. LCD_ALERTMESSAGEPGM("Err: REDUNDANT TEMP ERROR");
  498. }
  499. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  500. Stop();
  501. #endif
  502. }
  503. #endif
  504. } // End extruder for loop
  505. #if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
  506. (defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
  507. (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1)
  508. if(millis() - extruder_autofan_last_check > 2500) // only need to check fan state very infrequently
  509. {
  510. checkExtruderAutoFans();
  511. extruder_autofan_last_check = millis();
  512. }
  513. #endif
  514. #ifndef PIDTEMPBED
  515. if(millis() - previous_millis_bed_heater < BED_CHECK_INTERVAL)
  516. return;
  517. previous_millis_bed_heater = millis();
  518. #endif
  519. #if TEMP_SENSOR_BED != 0
  520. #ifdef PIDTEMPBED
  521. pid_input = current_temperature_bed;
  522. #ifndef PID_OPENLOOP
  523. pid_error_bed = target_temperature_bed - pid_input;
  524. pTerm_bed = bedKp * pid_error_bed;
  525. temp_iState_bed += pid_error_bed;
  526. temp_iState_bed = constrain(temp_iState_bed, temp_iState_min_bed, temp_iState_max_bed);
  527. iTerm_bed = bedKi * temp_iState_bed;
  528. //K1 defined in Configuration.h in the PID settings
  529. #define K2 (1.0-K1)
  530. dTerm_bed= (bedKd * (pid_input - temp_dState_bed))*K2 + (K1 * dTerm_bed);
  531. temp_dState_bed = pid_input;
  532. pid_output = pTerm_bed + iTerm_bed - dTerm_bed;
  533. if (pid_output > MAX_BED_POWER) {
  534. if (pid_error_bed > 0 ) temp_iState_bed -= pid_error_bed; // conditional un-integration
  535. pid_output=MAX_BED_POWER;
  536. } else if (pid_output < 0){
  537. if (pid_error_bed < 0 ) temp_iState_bed -= pid_error_bed; // conditional un-integration
  538. pid_output=0;
  539. }
  540. #else
  541. pid_output = constrain(target_temperature_bed, 0, MAX_BED_POWER);
  542. #endif //PID_OPENLOOP
  543. if((current_temperature_bed > BED_MINTEMP) && (current_temperature_bed < BED_MAXTEMP))
  544. {
  545. soft_pwm_bed = (int)pid_output >> 1;
  546. }
  547. else {
  548. soft_pwm_bed = 0;
  549. }
  550. #elif !defined(BED_LIMIT_SWITCHING)
  551. // Check if temperature is within the correct range
  552. if((current_temperature_bed > BED_MINTEMP) && (current_temperature_bed < BED_MAXTEMP))
  553. {
  554. if(current_temperature_bed >= target_temperature_bed)
  555. {
  556. soft_pwm_bed = 0;
  557. }
  558. else
  559. {
  560. soft_pwm_bed = MAX_BED_POWER>>1;
  561. }
  562. }
  563. else
  564. {
  565. soft_pwm_bed = 0;
  566. WRITE(HEATER_BED_PIN,LOW);
  567. }
  568. #else //#ifdef BED_LIMIT_SWITCHING
  569. // Check if temperature is within the correct band
  570. if((current_temperature_bed > BED_MINTEMP) && (current_temperature_bed < BED_MAXTEMP))
  571. {
  572. if(current_temperature_bed > target_temperature_bed + BED_HYSTERESIS)
  573. {
  574. soft_pwm_bed = 0;
  575. }
  576. else if(current_temperature_bed <= target_temperature_bed - BED_HYSTERESIS)
  577. {
  578. soft_pwm_bed = MAX_BED_POWER>>1;
  579. }
  580. }
  581. else
  582. {
  583. soft_pwm_bed = 0;
  584. WRITE(HEATER_BED_PIN,LOW);
  585. }
  586. #endif
  587. #endif
  588. //code for controlling the extruder rate based on the width sensor
  589. #ifdef FILAMENT_SENSOR
  590. if(filament_sensor)
  591. {
  592. meas_shift_index=delay_index1-meas_delay_cm;
  593. if(meas_shift_index<0)
  594. meas_shift_index = meas_shift_index + (MAX_MEASUREMENT_DELAY+1); //loop around buffer if needed
  595. //get the delayed info and add 100 to reconstitute to a percent of the nominal filament diameter
  596. //then square it to get an area
  597. if(meas_shift_index<0)
  598. meas_shift_index=0;
  599. else if (meas_shift_index>MAX_MEASUREMENT_DELAY)
  600. meas_shift_index=MAX_MEASUREMENT_DELAY;
  601. volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] = pow((float)(100+measurement_delay[meas_shift_index])/100.0,2);
  602. if (volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] <0.01)
  603. volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM]=0.01;
  604. }
  605. #endif
  606. }
  607. #define PGM_RD_W(x) (short)pgm_read_word(&x)
  608. // Derived from RepRap FiveD extruder::getTemperature()
  609. // For hot end temperature measurement.
  610. static float analog2temp(int raw, uint8_t e) {
  611. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  612. if(e > EXTRUDERS)
  613. #else
  614. if(e >= EXTRUDERS)
  615. #endif
  616. {
  617. SERIAL_ERROR_START;
  618. SERIAL_ERROR((int)e);
  619. SERIAL_ERRORLNPGM(" - Invalid extruder number !");
  620. kill();
  621. return 0.0;
  622. }
  623. #ifdef HEATER_0_USES_MAX6675
  624. if (e == 0)
  625. {
  626. return 0.25 * raw;
  627. }
  628. #endif
  629. if(heater_ttbl_map[e] != NULL)
  630. {
  631. float celsius = 0;
  632. uint8_t i;
  633. short (*tt)[][2] = (short (*)[][2])(heater_ttbl_map[e]);
  634. for (i=1; i<heater_ttbllen_map[e]; i++)
  635. {
  636. if (PGM_RD_W((*tt)[i][0]) > raw)
  637. {
  638. celsius = PGM_RD_W((*tt)[i-1][1]) +
  639. (raw - PGM_RD_W((*tt)[i-1][0])) *
  640. (float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i-1][1])) /
  641. (float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i-1][0]));
  642. break;
  643. }
  644. }
  645. // Overflow: Set to last value in the table
  646. if (i == heater_ttbllen_map[e]) celsius = PGM_RD_W((*tt)[i-1][1]);
  647. return celsius;
  648. }
  649. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  650. }
  651. // Derived from RepRap FiveD extruder::getTemperature()
  652. // For bed temperature measurement.
  653. static float analog2tempBed(int raw) {
  654. #ifdef BED_USES_THERMISTOR
  655. float celsius = 0;
  656. byte i;
  657. for (i=1; i<BEDTEMPTABLE_LEN; i++)
  658. {
  659. if (PGM_RD_W(BEDTEMPTABLE[i][0]) > raw)
  660. {
  661. celsius = PGM_RD_W(BEDTEMPTABLE[i-1][1]) +
  662. (raw - PGM_RD_W(BEDTEMPTABLE[i-1][0])) *
  663. (float)(PGM_RD_W(BEDTEMPTABLE[i][1]) - PGM_RD_W(BEDTEMPTABLE[i-1][1])) /
  664. (float)(PGM_RD_W(BEDTEMPTABLE[i][0]) - PGM_RD_W(BEDTEMPTABLE[i-1][0]));
  665. break;
  666. }
  667. }
  668. // Overflow: Set to last value in the table
  669. if (i == BEDTEMPTABLE_LEN) celsius = PGM_RD_W(BEDTEMPTABLE[i-1][1]);
  670. // temperature offset adjustment
  671. #ifdef BED_OFFSET
  672. float _offset = BED_OFFSET;
  673. float _offset_center = BED_OFFSET_CENTER;
  674. float _offset_start = BED_OFFSET_START;
  675. float _first_koef = (_offset / 2) / (_offset_center - _offset_start);
  676. float _second_koef = (_offset / 2) / (100 - _offset_center);
  677. if (celsius >= _offset_start && celsius <= _offset_center)
  678. {
  679. celsius = celsius + (_first_koef * (celsius - _offset_start));
  680. }
  681. else if (celsius > _offset_center && celsius <= 100)
  682. {
  683. celsius = celsius + (_first_koef * (_offset_center - _offset_start)) + ( _second_koef * ( celsius - ( 100 - _offset_center ) )) ;
  684. }
  685. else if (celsius > 100)
  686. {
  687. celsius = celsius + _offset;
  688. }
  689. #endif
  690. return celsius;
  691. #elif defined BED_USES_AD595
  692. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  693. #else
  694. return 0;
  695. #endif
  696. }
  697. /* Called to get the raw values into the the actual temperatures. The raw values are created in interrupt context,
  698. and this function is called from normal context as it is too slow to run in interrupts and will block the stepper routine otherwise */
  699. static void updateTemperaturesFromRawValues()
  700. {
  701. for(uint8_t e=0;e<EXTRUDERS;e++)
  702. {
  703. current_temperature[e] = analog2temp(current_temperature_raw[e], e);
  704. }
  705. current_temperature_bed = analog2tempBed(current_temperature_bed_raw);
  706. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  707. redundant_temperature = analog2temp(redundant_temperature_raw, 1);
  708. #endif
  709. #if defined (FILAMENT_SENSOR) && (FILWIDTH_PIN > -1) //check if a sensor is supported
  710. filament_width_meas = analog2widthFil();
  711. #endif
  712. //Reset the watchdog after we know we have a temperature measurement.
  713. watchdog_reset();
  714. CRITICAL_SECTION_START;
  715. temp_meas_ready = false;
  716. CRITICAL_SECTION_END;
  717. }
  718. // For converting raw Filament Width to milimeters
  719. #ifdef FILAMENT_SENSOR
  720. float analog2widthFil() {
  721. return current_raw_filwidth/16383.0*5.0;
  722. //return current_raw_filwidth;
  723. }
  724. // For converting raw Filament Width to a ratio
  725. int widthFil_to_size_ratio() {
  726. float temp;
  727. temp=filament_width_meas;
  728. if(filament_width_meas<MEASURED_LOWER_LIMIT)
  729. temp=filament_width_nominal; //assume sensor cut out
  730. else if (filament_width_meas>MEASURED_UPPER_LIMIT)
  731. temp= MEASURED_UPPER_LIMIT;
  732. return(filament_width_nominal/temp*100);
  733. }
  734. #endif
  735. void tp_init()
  736. {
  737. #if MB(RUMBA) && ((TEMP_SENSOR_0==-1)||(TEMP_SENSOR_1==-1)||(TEMP_SENSOR_2==-1)||(TEMP_SENSOR_BED==-1))
  738. //disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector
  739. MCUCR=(1<<JTD);
  740. MCUCR=(1<<JTD);
  741. #endif
  742. // Finish init of mult extruder arrays
  743. for(int e = 0; e < EXTRUDERS; e++) {
  744. // populate with the first value
  745. maxttemp[e] = maxttemp[0];
  746. #ifdef PIDTEMP
  747. temp_iState_min[e] = 0.0;
  748. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / Ki;
  749. #endif //PIDTEMP
  750. #ifdef PIDTEMPBED
  751. temp_iState_min_bed = 0.0;
  752. temp_iState_max_bed = PID_INTEGRAL_DRIVE_MAX / bedKi;
  753. #endif //PIDTEMPBED
  754. }
  755. #if defined(HEATER_0_PIN) && (HEATER_0_PIN > -1)
  756. SET_OUTPUT(HEATER_0_PIN);
  757. #endif
  758. #if defined(HEATER_1_PIN) && (HEATER_1_PIN > -1)
  759. SET_OUTPUT(HEATER_1_PIN);
  760. #endif
  761. #if defined(HEATER_2_PIN) && (HEATER_2_PIN > -1)
  762. SET_OUTPUT(HEATER_2_PIN);
  763. #endif
  764. #if defined(HEATER_BED_PIN) && (HEATER_BED_PIN > -1)
  765. SET_OUTPUT(HEATER_BED_PIN);
  766. #endif
  767. #if defined(FAN_PIN) && (FAN_PIN > -1)
  768. SET_OUTPUT(FAN_PIN);
  769. #ifdef FAST_PWM_FAN
  770. setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  771. #endif
  772. #ifdef FAN_SOFT_PWM
  773. soft_pwm_fan = fanSpeedSoftPwm / 2;
  774. #endif
  775. #endif
  776. #ifdef HEATER_0_USES_MAX6675
  777. #ifndef SDSUPPORT
  778. SET_OUTPUT(SCK_PIN);
  779. WRITE(SCK_PIN,0);
  780. SET_OUTPUT(MOSI_PIN);
  781. WRITE(MOSI_PIN,1);
  782. SET_INPUT(MISO_PIN);
  783. WRITE(MISO_PIN,1);
  784. #endif
  785. /* Using pinMode and digitalWrite, as that was the only way I could get it to compile */
  786. //Have to toggle SD card CS pin to low first, to enable firmware to talk with SD card
  787. pinMode(SS_PIN, OUTPUT);
  788. digitalWrite(SS_PIN,0);
  789. pinMode(MAX6675_SS, OUTPUT);
  790. digitalWrite(MAX6675_SS,1);
  791. #endif
  792. // Set analog inputs
  793. ADCSRA = 1<<ADEN | 1<<ADSC | 1<<ADIF | 0x07;
  794. DIDR0 = 0;
  795. #ifdef DIDR2
  796. DIDR2 = 0;
  797. #endif
  798. #if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
  799. #if TEMP_0_PIN < 8
  800. DIDR0 |= 1 << TEMP_0_PIN;
  801. #else
  802. DIDR2 |= 1<<(TEMP_0_PIN - 8);
  803. #endif
  804. #endif
  805. #if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1)
  806. #if TEMP_1_PIN < 8
  807. DIDR0 |= 1<<TEMP_1_PIN;
  808. #else
  809. DIDR2 |= 1<<(TEMP_1_PIN - 8);
  810. #endif
  811. #endif
  812. #if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
  813. #if TEMP_2_PIN < 8
  814. DIDR0 |= 1 << TEMP_2_PIN;
  815. #else
  816. DIDR2 |= 1<<(TEMP_2_PIN - 8);
  817. #endif
  818. #endif
  819. #if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
  820. #if TEMP_BED_PIN < 8
  821. DIDR0 |= 1<<TEMP_BED_PIN;
  822. #else
  823. DIDR2 |= 1<<(TEMP_BED_PIN - 8);
  824. #endif
  825. #endif
  826. //Added for Filament Sensor
  827. #ifdef FILAMENT_SENSOR
  828. #if defined(FILWIDTH_PIN) && (FILWIDTH_PIN > -1)
  829. #if FILWIDTH_PIN < 8
  830. DIDR0 |= 1<<FILWIDTH_PIN;
  831. #else
  832. DIDR2 |= 1<<(FILWIDTH_PIN - 8);
  833. #endif
  834. #endif
  835. #endif
  836. // Use timer0 for temperature measurement
  837. // Interleave temperature interrupt with millies interrupt
  838. OCR0B = 128;
  839. TIMSK0 |= (1<<OCIE0B);
  840. // Wait for temperature measurement to settle
  841. delay(250);
  842. #ifdef HEATER_0_MINTEMP
  843. minttemp[0] = HEATER_0_MINTEMP;
  844. while(analog2temp(minttemp_raw[0], 0) < HEATER_0_MINTEMP) {
  845. #if HEATER_0_RAW_LO_TEMP < HEATER_0_RAW_HI_TEMP
  846. minttemp_raw[0] += OVERSAMPLENR;
  847. #else
  848. minttemp_raw[0] -= OVERSAMPLENR;
  849. #endif
  850. }
  851. #endif //MINTEMP
  852. #ifdef HEATER_0_MAXTEMP
  853. maxttemp[0] = HEATER_0_MAXTEMP;
  854. while(analog2temp(maxttemp_raw[0], 0) > HEATER_0_MAXTEMP) {
  855. #if HEATER_0_RAW_LO_TEMP < HEATER_0_RAW_HI_TEMP
  856. maxttemp_raw[0] -= OVERSAMPLENR;
  857. #else
  858. maxttemp_raw[0] += OVERSAMPLENR;
  859. #endif
  860. }
  861. #endif //MAXTEMP
  862. #if (EXTRUDERS > 1) && defined(HEATER_1_MINTEMP)
  863. minttemp[1] = HEATER_1_MINTEMP;
  864. while(analog2temp(minttemp_raw[1], 1) < HEATER_1_MINTEMP) {
  865. #if HEATER_1_RAW_LO_TEMP < HEATER_1_RAW_HI_TEMP
  866. minttemp_raw[1] += OVERSAMPLENR;
  867. #else
  868. minttemp_raw[1] -= OVERSAMPLENR;
  869. #endif
  870. }
  871. #endif // MINTEMP 1
  872. #if (EXTRUDERS > 1) && defined(HEATER_1_MAXTEMP)
  873. maxttemp[1] = HEATER_1_MAXTEMP;
  874. while(analog2temp(maxttemp_raw[1], 1) > HEATER_1_MAXTEMP) {
  875. #if HEATER_1_RAW_LO_TEMP < HEATER_1_RAW_HI_TEMP
  876. maxttemp_raw[1] -= OVERSAMPLENR;
  877. #else
  878. maxttemp_raw[1] += OVERSAMPLENR;
  879. #endif
  880. }
  881. #endif //MAXTEMP 1
  882. #if (EXTRUDERS > 2) && defined(HEATER_2_MINTEMP)
  883. minttemp[2] = HEATER_2_MINTEMP;
  884. while(analog2temp(minttemp_raw[2], 2) < HEATER_2_MINTEMP) {
  885. #if HEATER_2_RAW_LO_TEMP < HEATER_2_RAW_HI_TEMP
  886. minttemp_raw[2] += OVERSAMPLENR;
  887. #else
  888. minttemp_raw[2] -= OVERSAMPLENR;
  889. #endif
  890. }
  891. #endif //MINTEMP 2
  892. #if (EXTRUDERS > 2) && defined(HEATER_2_MAXTEMP)
  893. maxttemp[2] = HEATER_2_MAXTEMP;
  894. while(analog2temp(maxttemp_raw[2], 2) > HEATER_2_MAXTEMP) {
  895. #if HEATER_2_RAW_LO_TEMP < HEATER_2_RAW_HI_TEMP
  896. maxttemp_raw[2] -= OVERSAMPLENR;
  897. #else
  898. maxttemp_raw[2] += OVERSAMPLENR;
  899. #endif
  900. }
  901. #endif //MAXTEMP 2
  902. #ifdef BED_MINTEMP
  903. /* No bed MINTEMP error implemented?!? */
  904. while(analog2tempBed(bed_minttemp_raw) < BED_MINTEMP) {
  905. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  906. bed_minttemp_raw += OVERSAMPLENR;
  907. #else
  908. bed_minttemp_raw -= OVERSAMPLENR;
  909. #endif
  910. }
  911. #endif //BED_MINTEMP
  912. #ifdef BED_MAXTEMP
  913. while(analog2tempBed(bed_maxttemp_raw) > BED_MAXTEMP) {
  914. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  915. bed_maxttemp_raw -= OVERSAMPLENR;
  916. #else
  917. bed_maxttemp_raw += OVERSAMPLENR;
  918. #endif
  919. }
  920. #endif //BED_MAXTEMP
  921. }
  922. void setWatch()
  923. {
  924. #ifdef WATCH_TEMP_PERIOD
  925. for (int e = 0; e < EXTRUDERS; e++)
  926. {
  927. if(degHotend(e) < degTargetHotend(e) - (WATCH_TEMP_INCREASE * 2))
  928. {
  929. watch_start_temp[e] = degHotend(e);
  930. watchmillis[e] = millis();
  931. }
  932. }
  933. #endif
  934. }
  935. #if (defined (TEMP_RUNAWAY_BED_HYSTERESIS) && TEMP_RUNAWAY_BED_TIMEOUT > 0) || (defined (TEMP_RUNAWAY_EXTRUDER_HYSTERESIS) && TEMP_RUNAWAY_EXTRUDER_TIMEOUT > 0)
  936. void temp_runaway_check(int _heater_id, float _target_temperature, float _current_temperature, float _output, bool _isbed)
  937. {
  938. float __hysteresis = 0;
  939. int __timeout = 0;
  940. bool temp_runaway_check_active = false;
  941. static float __preheat_start[2] = { 0,0}; //currently just bed and one extruder
  942. static int __preheat_counter[2] = { 0,0};
  943. static int __preheat_errors[2] = { 0,0};
  944. #ifdef TEMP_RUNAWAY_BED_TIMEOUT
  945. if (_isbed)
  946. {
  947. __hysteresis = TEMP_RUNAWAY_BED_HYSTERESIS;
  948. __timeout = TEMP_RUNAWAY_BED_TIMEOUT;
  949. }
  950. #endif
  951. #ifdef TEMP_RUNAWAY_EXTRUDER_TIMEOUT
  952. if (!_isbed)
  953. {
  954. __hysteresis = TEMP_RUNAWAY_EXTRUDER_HYSTERESIS;
  955. __timeout = TEMP_RUNAWAY_EXTRUDER_TIMEOUT;
  956. }
  957. #endif
  958. if (millis() - temp_runaway_timer[_heater_id] > 2000)
  959. {
  960. temp_runaway_timer[_heater_id] = millis();
  961. if (_output == 0)
  962. {
  963. temp_runaway_check_active = false;
  964. temp_runaway_error_counter[_heater_id] = 0;
  965. }
  966. if (temp_runaway_target[_heater_id] != _target_temperature)
  967. {
  968. if (_target_temperature > 0)
  969. {
  970. temp_runaway_status[_heater_id] = TempRunaway_PREHEAT;
  971. temp_runaway_target[_heater_id] = _target_temperature;
  972. __preheat_start[_heater_id] = _current_temperature;
  973. __preheat_counter[_heater_id] = 0;
  974. }
  975. else
  976. {
  977. temp_runaway_status[_heater_id] = TempRunaway_INACTIVE;
  978. temp_runaway_target[_heater_id] = _target_temperature;
  979. }
  980. }
  981. if (temp_runaway_status[_heater_id] == TempRunaway_PREHEAT)
  982. {
  983. if (_current_temperature < ((_isbed) ? (0.8 * _target_temperature) : 150)) //check only in area where temperature is changing fastly for heater, check to 0.8 x target temperature for bed
  984. {
  985. __preheat_counter[_heater_id]++;
  986. if (__preheat_counter[_heater_id] > ((_isbed) ? 16 : 8)) // periodicaly check if current temperature changes
  987. {
  988. /*SERIAL_ECHOPGM("Heater:");
  989. MYSERIAL.print(_heater_id);
  990. SERIAL_ECHOPGM(" T:");
  991. MYSERIAL.print(_current_temperature);
  992. SERIAL_ECHOPGM(" Tstart:");
  993. MYSERIAL.print(__preheat_start[_heater_id]);*/
  994. if (_current_temperature - __preheat_start[_heater_id] < 2) {
  995. __preheat_errors[_heater_id]++;
  996. /*SERIAL_ECHOPGM(" Preheat errors:");
  997. MYSERIAL.println(__preheat_errors[_heater_id]);*/
  998. }
  999. else {
  1000. //SERIAL_ECHOLNPGM("");
  1001. __preheat_errors[_heater_id] = 0;
  1002. }
  1003. if (__preheat_errors[_heater_id] > ((_isbed) ? 2 : 5))
  1004. {
  1005. if (farm_mode) { prusa_statistics(0); }
  1006. temp_runaway_stop(true, _isbed);
  1007. if (farm_mode) { prusa_statistics(91); }
  1008. }
  1009. __preheat_start[_heater_id] = _current_temperature;
  1010. __preheat_counter[_heater_id] = 0;
  1011. }
  1012. }
  1013. }
  1014. if (_current_temperature >= _target_temperature && temp_runaway_status[_heater_id] == TempRunaway_PREHEAT)
  1015. {
  1016. temp_runaway_status[_heater_id] = TempRunaway_ACTIVE;
  1017. temp_runaway_check_active = false;
  1018. }
  1019. if (!temp_runaway_check_active && _output > 0)
  1020. {
  1021. temp_runaway_check_active = true;
  1022. }
  1023. if (temp_runaway_check_active)
  1024. {
  1025. // we are in range
  1026. if (_target_temperature - __hysteresis < _current_temperature && _current_temperature < _target_temperature + __hysteresis)
  1027. {
  1028. temp_runaway_check_active = false;
  1029. temp_runaway_error_counter[_heater_id] = 0;
  1030. }
  1031. else
  1032. {
  1033. if (temp_runaway_status[_heater_id] > TempRunaway_PREHEAT)
  1034. {
  1035. temp_runaway_error_counter[_heater_id]++;
  1036. if (temp_runaway_error_counter[_heater_id] * 2 > __timeout)
  1037. {
  1038. if (farm_mode) { prusa_statistics(0); }
  1039. temp_runaway_stop(false, _isbed);
  1040. if (farm_mode) { prusa_statistics(90); }
  1041. }
  1042. }
  1043. }
  1044. }
  1045. }
  1046. }
  1047. void temp_runaway_stop(bool isPreheat, bool isBed)
  1048. {
  1049. cancel_heatup = true;
  1050. quickStop();
  1051. if (card.sdprinting)
  1052. {
  1053. card.sdprinting = false;
  1054. card.closefile();
  1055. }
  1056. disable_heater();
  1057. disable_x();
  1058. disable_y();
  1059. disable_e0();
  1060. disable_e1();
  1061. disable_e2();
  1062. manage_heater();
  1063. lcd_update();
  1064. WRITE(BEEPER, HIGH);
  1065. delayMicroseconds(500);
  1066. WRITE(BEEPER, LOW);
  1067. delayMicroseconds(100);
  1068. if (isPreheat)
  1069. {
  1070. Stop();
  1071. isBed ? LCD_ALERTMESSAGEPGM("BED PREHEAT ERROR") : LCD_ALERTMESSAGEPGM("PREHEAT ERROR");
  1072. SERIAL_ERROR_START;
  1073. isBed ? SERIAL_ERRORLNPGM(" THERMAL RUNAWAY ( PREHEAT HEATBED)") : SERIAL_ERRORLNPGM(" THERMAL RUNAWAY ( PREHEAT HOTEND)");
  1074. SET_OUTPUT(EXTRUDER_0_AUTO_FAN_PIN);
  1075. SET_OUTPUT(FAN_PIN);
  1076. WRITE(EXTRUDER_0_AUTO_FAN_PIN, 1);
  1077. analogWrite(FAN_PIN, 255);
  1078. fanSpeed = 255;
  1079. delayMicroseconds(2000);
  1080. }
  1081. else
  1082. {
  1083. isBed ? LCD_ALERTMESSAGEPGM("BED THERMAL RUNAWAY") : LCD_ALERTMESSAGEPGM("THERMAL RUNAWAY");
  1084. SERIAL_ERROR_START;
  1085. isBed ? SERIAL_ERRORLNPGM(" HEATBED THERMAL RUNAWAY") : SERIAL_ERRORLNPGM(" HOTEND THERMAL RUNAWAY");
  1086. }
  1087. }
  1088. #endif
  1089. void disable_heater()
  1090. {
  1091. for(int i=0;i<EXTRUDERS;i++)
  1092. setTargetHotend(0,i);
  1093. setTargetBed(0);
  1094. #if defined(TEMP_0_PIN) && TEMP_0_PIN > -1
  1095. target_temperature[0]=0;
  1096. soft_pwm[0]=0;
  1097. #if defined(HEATER_0_PIN) && HEATER_0_PIN > -1
  1098. WRITE(HEATER_0_PIN,LOW);
  1099. #endif
  1100. #endif
  1101. #if defined(TEMP_1_PIN) && TEMP_1_PIN > -1 && EXTRUDERS > 1
  1102. target_temperature[1]=0;
  1103. soft_pwm[1]=0;
  1104. #if defined(HEATER_1_PIN) && HEATER_1_PIN > -1
  1105. WRITE(HEATER_1_PIN,LOW);
  1106. #endif
  1107. #endif
  1108. #if defined(TEMP_2_PIN) && TEMP_2_PIN > -1 && EXTRUDERS > 2
  1109. target_temperature[2]=0;
  1110. soft_pwm[2]=0;
  1111. #if defined(HEATER_2_PIN) && HEATER_2_PIN > -1
  1112. WRITE(HEATER_2_PIN,LOW);
  1113. #endif
  1114. #endif
  1115. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  1116. target_temperature_bed=0;
  1117. soft_pwm_bed=0;
  1118. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1119. WRITE(HEATER_BED_PIN,LOW);
  1120. #endif
  1121. #endif
  1122. }
  1123. void max_temp_error(uint8_t e) {
  1124. disable_heater();
  1125. if(IsStopped() == false) {
  1126. SERIAL_ERROR_START;
  1127. SERIAL_ERRORLN((int)e);
  1128. SERIAL_ERRORLNPGM(": Extruder switched off. MAXTEMP triggered !");
  1129. LCD_ALERTMESSAGEPGM("Err: MAXTEMP");
  1130. }
  1131. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  1132. Stop();
  1133. #endif
  1134. SET_OUTPUT(EXTRUDER_0_AUTO_FAN_PIN);
  1135. SET_OUTPUT(FAN_PIN);
  1136. SET_OUTPUT(BEEPER);
  1137. WRITE(FAN_PIN, 1);
  1138. WRITE(EXTRUDER_0_AUTO_FAN_PIN, 1);
  1139. WRITE(BEEPER, 1);
  1140. // fanSpeed will consumed by the check_axes_activity() routine.
  1141. fanSpeed=255;
  1142. if (farm_mode) { prusa_statistics(93); }
  1143. }
  1144. void min_temp_error(uint8_t e) {
  1145. disable_heater();
  1146. if(IsStopped() == false) {
  1147. SERIAL_ERROR_START;
  1148. SERIAL_ERRORLN((int)e);
  1149. SERIAL_ERRORLNPGM(": Extruder switched off. MINTEMP triggered !");
  1150. LCD_ALERTMESSAGEPGM("Err: MINTEMP");
  1151. }
  1152. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  1153. Stop();
  1154. #endif
  1155. if (farm_mode) { prusa_statistics(92); }
  1156. }
  1157. void bed_max_temp_error(void) {
  1158. #if HEATER_BED_PIN > -1
  1159. WRITE(HEATER_BED_PIN, 0);
  1160. #endif
  1161. if(IsStopped() == false) {
  1162. SERIAL_ERROR_START;
  1163. SERIAL_ERRORLNPGM("Temperature heated bed switched off. MAXTEMP triggered !");
  1164. LCD_ALERTMESSAGEPGM("Err: MAXTEMP BED");
  1165. }
  1166. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  1167. Stop();
  1168. #endif
  1169. }
  1170. void bed_min_temp_error(void) {
  1171. #if HEATER_BED_PIN > -1
  1172. WRITE(HEATER_BED_PIN, 0);
  1173. #endif
  1174. if(IsStopped() == false) {
  1175. SERIAL_ERROR_START;
  1176. SERIAL_ERRORLNPGM("Temperature heated bed switched off. MINTEMP triggered !");
  1177. LCD_ALERTMESSAGEPGM("Err: MINTEMP BED");
  1178. }
  1179. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  1180. Stop();
  1181. #endif
  1182. }
  1183. #ifdef HEATER_0_USES_MAX6675
  1184. #define MAX6675_HEAT_INTERVAL 250
  1185. long max6675_previous_millis = MAX6675_HEAT_INTERVAL;
  1186. int max6675_temp = 2000;
  1187. int read_max6675()
  1188. {
  1189. if (millis() - max6675_previous_millis < MAX6675_HEAT_INTERVAL)
  1190. return max6675_temp;
  1191. max6675_previous_millis = millis();
  1192. max6675_temp = 0;
  1193. #ifdef PRR
  1194. PRR &= ~(1<<PRSPI);
  1195. #elif defined PRR0
  1196. PRR0 &= ~(1<<PRSPI);
  1197. #endif
  1198. SPCR = (1<<MSTR) | (1<<SPE) | (1<<SPR0);
  1199. // enable TT_MAX6675
  1200. WRITE(MAX6675_SS, 0);
  1201. // ensure 100ns delay - a bit extra is fine
  1202. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1203. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1204. // read MSB
  1205. SPDR = 0;
  1206. for (;(SPSR & (1<<SPIF)) == 0;);
  1207. max6675_temp = SPDR;
  1208. max6675_temp <<= 8;
  1209. // read LSB
  1210. SPDR = 0;
  1211. for (;(SPSR & (1<<SPIF)) == 0;);
  1212. max6675_temp |= SPDR;
  1213. // disable TT_MAX6675
  1214. WRITE(MAX6675_SS, 1);
  1215. if (max6675_temp & 4)
  1216. {
  1217. // thermocouple open
  1218. max6675_temp = 2000;
  1219. }
  1220. else
  1221. {
  1222. max6675_temp = max6675_temp >> 3;
  1223. }
  1224. return max6675_temp;
  1225. }
  1226. #endif
  1227. // Timer 0 is shared with millies
  1228. ISR(TIMER0_COMPB_vect)
  1229. {
  1230. //these variables are only accesible from the ISR, but static, so they don't lose their value
  1231. static unsigned char temp_count = 0;
  1232. static unsigned long raw_temp_0_value = 0;
  1233. static unsigned long raw_temp_1_value = 0;
  1234. static unsigned long raw_temp_2_value = 0;
  1235. static unsigned long raw_temp_bed_value = 0;
  1236. static unsigned char temp_state = 10;
  1237. static unsigned char pwm_count = (1 << SOFT_PWM_SCALE);
  1238. static unsigned char soft_pwm_0;
  1239. #ifdef SLOW_PWM_HEATERS
  1240. static unsigned char slow_pwm_count = 0;
  1241. static unsigned char state_heater_0 = 0;
  1242. static unsigned char state_timer_heater_0 = 0;
  1243. #endif
  1244. #if (EXTRUDERS > 1) || defined(HEATERS_PARALLEL)
  1245. static unsigned char soft_pwm_1;
  1246. #ifdef SLOW_PWM_HEATERS
  1247. static unsigned char state_heater_1 = 0;
  1248. static unsigned char state_timer_heater_1 = 0;
  1249. #endif
  1250. #endif
  1251. #if EXTRUDERS > 2
  1252. static unsigned char soft_pwm_2;
  1253. #ifdef SLOW_PWM_HEATERS
  1254. static unsigned char state_heater_2 = 0;
  1255. static unsigned char state_timer_heater_2 = 0;
  1256. #endif
  1257. #endif
  1258. #if HEATER_BED_PIN > -1
  1259. static unsigned char soft_pwm_b;
  1260. #ifdef SLOW_PWM_HEATERS
  1261. static unsigned char state_heater_b = 0;
  1262. static unsigned char state_timer_heater_b = 0;
  1263. #endif
  1264. #endif
  1265. #if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1)
  1266. static unsigned long raw_filwidth_value = 0; //added for filament width sensor
  1267. #endif
  1268. #ifndef SLOW_PWM_HEATERS
  1269. /*
  1270. * standard PWM modulation
  1271. */
  1272. if(pwm_count == 0){
  1273. soft_pwm_0 = soft_pwm[0];
  1274. if(soft_pwm_0 > 0) {
  1275. WRITE(HEATER_0_PIN,1);
  1276. #ifdef HEATERS_PARALLEL
  1277. WRITE(HEATER_1_PIN,1);
  1278. #endif
  1279. } else WRITE(HEATER_0_PIN,0);
  1280. #if EXTRUDERS > 1
  1281. soft_pwm_1 = soft_pwm[1];
  1282. if(soft_pwm_1 > 0) WRITE(HEATER_1_PIN,1); else WRITE(HEATER_1_PIN,0);
  1283. #endif
  1284. #if EXTRUDERS > 2
  1285. soft_pwm_2 = soft_pwm[2];
  1286. if(soft_pwm_2 > 0) WRITE(HEATER_2_PIN,1); else WRITE(HEATER_2_PIN,0);
  1287. #endif
  1288. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1289. soft_pwm_b = soft_pwm_bed;
  1290. if(soft_pwm_b > 0) WRITE(HEATER_BED_PIN,1); else WRITE(HEATER_BED_PIN,0);
  1291. #endif
  1292. #ifdef FAN_SOFT_PWM
  1293. soft_pwm_fan = fanSpeedSoftPwm / 2;
  1294. if(soft_pwm_fan > 0) WRITE(FAN_PIN,1); else WRITE(FAN_PIN,0);
  1295. #endif
  1296. }
  1297. if(soft_pwm_0 < pwm_count) {
  1298. WRITE(HEATER_0_PIN,0);
  1299. #ifdef HEATERS_PARALLEL
  1300. WRITE(HEATER_1_PIN,0);
  1301. #endif
  1302. }
  1303. #if EXTRUDERS > 1
  1304. if(soft_pwm_1 < pwm_count) WRITE(HEATER_1_PIN,0);
  1305. #endif
  1306. #if EXTRUDERS > 2
  1307. if(soft_pwm_2 < pwm_count) WRITE(HEATER_2_PIN,0);
  1308. #endif
  1309. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1310. if(soft_pwm_b < pwm_count) WRITE(HEATER_BED_PIN,0);
  1311. #endif
  1312. #ifdef FAN_SOFT_PWM
  1313. if(soft_pwm_fan < pwm_count) WRITE(FAN_PIN,0);
  1314. #endif
  1315. pwm_count += (1 << SOFT_PWM_SCALE);
  1316. pwm_count &= 0x7f;
  1317. #else //ifndef SLOW_PWM_HEATERS
  1318. /*
  1319. * SLOW PWM HEATERS
  1320. *
  1321. * for heaters drived by relay
  1322. */
  1323. #ifndef MIN_STATE_TIME
  1324. #define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds
  1325. #endif
  1326. if (slow_pwm_count == 0) {
  1327. // EXTRUDER 0
  1328. soft_pwm_0 = soft_pwm[0];
  1329. if (soft_pwm_0 > 0) {
  1330. // turn ON heather only if the minimum time is up
  1331. if (state_timer_heater_0 == 0) {
  1332. // if change state set timer
  1333. if (state_heater_0 == 0) {
  1334. state_timer_heater_0 = MIN_STATE_TIME;
  1335. }
  1336. state_heater_0 = 1;
  1337. WRITE(HEATER_0_PIN, 1);
  1338. #ifdef HEATERS_PARALLEL
  1339. WRITE(HEATER_1_PIN, 1);
  1340. #endif
  1341. }
  1342. } else {
  1343. // turn OFF heather only if the minimum time is up
  1344. if (state_timer_heater_0 == 0) {
  1345. // if change state set timer
  1346. if (state_heater_0 == 1) {
  1347. state_timer_heater_0 = MIN_STATE_TIME;
  1348. }
  1349. state_heater_0 = 0;
  1350. WRITE(HEATER_0_PIN, 0);
  1351. #ifdef HEATERS_PARALLEL
  1352. WRITE(HEATER_1_PIN, 0);
  1353. #endif
  1354. }
  1355. }
  1356. #if EXTRUDERS > 1
  1357. // EXTRUDER 1
  1358. soft_pwm_1 = soft_pwm[1];
  1359. if (soft_pwm_1 > 0) {
  1360. // turn ON heather only if the minimum time is up
  1361. if (state_timer_heater_1 == 0) {
  1362. // if change state set timer
  1363. if (state_heater_1 == 0) {
  1364. state_timer_heater_1 = MIN_STATE_TIME;
  1365. }
  1366. state_heater_1 = 1;
  1367. WRITE(HEATER_1_PIN, 1);
  1368. }
  1369. } else {
  1370. // turn OFF heather only if the minimum time is up
  1371. if (state_timer_heater_1 == 0) {
  1372. // if change state set timer
  1373. if (state_heater_1 == 1) {
  1374. state_timer_heater_1 = MIN_STATE_TIME;
  1375. }
  1376. state_heater_1 = 0;
  1377. WRITE(HEATER_1_PIN, 0);
  1378. }
  1379. }
  1380. #endif
  1381. #if EXTRUDERS > 2
  1382. // EXTRUDER 2
  1383. soft_pwm_2 = soft_pwm[2];
  1384. if (soft_pwm_2 > 0) {
  1385. // turn ON heather only if the minimum time is up
  1386. if (state_timer_heater_2 == 0) {
  1387. // if change state set timer
  1388. if (state_heater_2 == 0) {
  1389. state_timer_heater_2 = MIN_STATE_TIME;
  1390. }
  1391. state_heater_2 = 1;
  1392. WRITE(HEATER_2_PIN, 1);
  1393. }
  1394. } else {
  1395. // turn OFF heather only if the minimum time is up
  1396. if (state_timer_heater_2 == 0) {
  1397. // if change state set timer
  1398. if (state_heater_2 == 1) {
  1399. state_timer_heater_2 = MIN_STATE_TIME;
  1400. }
  1401. state_heater_2 = 0;
  1402. WRITE(HEATER_2_PIN, 0);
  1403. }
  1404. }
  1405. #endif
  1406. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1407. // BED
  1408. soft_pwm_b = soft_pwm_bed;
  1409. if (soft_pwm_b > 0) {
  1410. // turn ON heather only if the minimum time is up
  1411. if (state_timer_heater_b == 0) {
  1412. // if change state set timer
  1413. if (state_heater_b == 0) {
  1414. state_timer_heater_b = MIN_STATE_TIME;
  1415. }
  1416. state_heater_b = 1;
  1417. WRITE(HEATER_BED_PIN, 1);
  1418. }
  1419. } else {
  1420. // turn OFF heather only if the minimum time is up
  1421. if (state_timer_heater_b == 0) {
  1422. // if change state set timer
  1423. if (state_heater_b == 1) {
  1424. state_timer_heater_b = MIN_STATE_TIME;
  1425. }
  1426. state_heater_b = 0;
  1427. WRITE(HEATER_BED_PIN, 0);
  1428. }
  1429. }
  1430. #endif
  1431. } // if (slow_pwm_count == 0)
  1432. // EXTRUDER 0
  1433. if (soft_pwm_0 < slow_pwm_count) {
  1434. // turn OFF heather only if the minimum time is up
  1435. if (state_timer_heater_0 == 0) {
  1436. // if change state set timer
  1437. if (state_heater_0 == 1) {
  1438. state_timer_heater_0 = MIN_STATE_TIME;
  1439. }
  1440. state_heater_0 = 0;
  1441. WRITE(HEATER_0_PIN, 0);
  1442. #ifdef HEATERS_PARALLEL
  1443. WRITE(HEATER_1_PIN, 0);
  1444. #endif
  1445. }
  1446. }
  1447. #if EXTRUDERS > 1
  1448. // EXTRUDER 1
  1449. if (soft_pwm_1 < slow_pwm_count) {
  1450. // turn OFF heather only if the minimum time is up
  1451. if (state_timer_heater_1 == 0) {
  1452. // if change state set timer
  1453. if (state_heater_1 == 1) {
  1454. state_timer_heater_1 = MIN_STATE_TIME;
  1455. }
  1456. state_heater_1 = 0;
  1457. WRITE(HEATER_1_PIN, 0);
  1458. }
  1459. }
  1460. #endif
  1461. #if EXTRUDERS > 2
  1462. // EXTRUDER 2
  1463. if (soft_pwm_2 < slow_pwm_count) {
  1464. // turn OFF heather only if the minimum time is up
  1465. if (state_timer_heater_2 == 0) {
  1466. // if change state set timer
  1467. if (state_heater_2 == 1) {
  1468. state_timer_heater_2 = MIN_STATE_TIME;
  1469. }
  1470. state_heater_2 = 0;
  1471. WRITE(HEATER_2_PIN, 0);
  1472. }
  1473. }
  1474. #endif
  1475. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1476. // BED
  1477. if (soft_pwm_b < slow_pwm_count) {
  1478. // turn OFF heather only if the minimum time is up
  1479. if (state_timer_heater_b == 0) {
  1480. // if change state set timer
  1481. if (state_heater_b == 1) {
  1482. state_timer_heater_b = MIN_STATE_TIME;
  1483. }
  1484. state_heater_b = 0;
  1485. WRITE(HEATER_BED_PIN, 0);
  1486. }
  1487. }
  1488. #endif
  1489. #ifdef FAN_SOFT_PWM
  1490. if (pwm_count == 0){
  1491. soft_pwm_fan = fanSpeedSoftPwm / 2;
  1492. if (soft_pwm_fan > 0) WRITE(FAN_PIN,1); else WRITE(FAN_PIN,0);
  1493. }
  1494. if (soft_pwm_fan < pwm_count) WRITE(FAN_PIN,0);
  1495. #endif
  1496. pwm_count += (1 << SOFT_PWM_SCALE);
  1497. pwm_count &= 0x7f;
  1498. // increment slow_pwm_count only every 64 pwm_count circa 65.5ms
  1499. if ((pwm_count % 64) == 0) {
  1500. slow_pwm_count++;
  1501. slow_pwm_count &= 0x7f;
  1502. // Extruder 0
  1503. if (state_timer_heater_0 > 0) {
  1504. state_timer_heater_0--;
  1505. }
  1506. #if EXTRUDERS > 1
  1507. // Extruder 1
  1508. if (state_timer_heater_1 > 0)
  1509. state_timer_heater_1--;
  1510. #endif
  1511. #if EXTRUDERS > 2
  1512. // Extruder 2
  1513. if (state_timer_heater_2 > 0)
  1514. state_timer_heater_2--;
  1515. #endif
  1516. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1517. // Bed
  1518. if (state_timer_heater_b > 0)
  1519. state_timer_heater_b--;
  1520. #endif
  1521. } //if ((pwm_count % 64) == 0) {
  1522. #endif //ifndef SLOW_PWM_HEATERS
  1523. switch(temp_state) {
  1524. case 0: // Prepare TEMP_0
  1525. #if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
  1526. #if TEMP_0_PIN > 7
  1527. ADCSRB = 1<<MUX5;
  1528. #else
  1529. ADCSRB = 0;
  1530. #endif
  1531. ADMUX = ((1 << REFS0) | (TEMP_0_PIN & 0x07));
  1532. ADCSRA |= 1<<ADSC; // Start conversion
  1533. #endif
  1534. lcd_buttons_update();
  1535. temp_state = 1;
  1536. break;
  1537. case 1: // Measure TEMP_0
  1538. #if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
  1539. raw_temp_0_value += ADC;
  1540. #endif
  1541. #ifdef HEATER_0_USES_MAX6675 // TODO remove the blocking
  1542. raw_temp_0_value = read_max6675();
  1543. #endif
  1544. temp_state = 2;
  1545. break;
  1546. case 2: // Prepare TEMP_BED
  1547. #if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
  1548. #if TEMP_BED_PIN > 7
  1549. ADCSRB = 1<<MUX5;
  1550. #else
  1551. ADCSRB = 0;
  1552. #endif
  1553. ADMUX = ((1 << REFS0) | (TEMP_BED_PIN & 0x07));
  1554. ADCSRA |= 1<<ADSC; // Start conversion
  1555. #endif
  1556. lcd_buttons_update();
  1557. temp_state = 3;
  1558. break;
  1559. case 3: // Measure TEMP_BED
  1560. #if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
  1561. raw_temp_bed_value += ADC;
  1562. #endif
  1563. temp_state = 4;
  1564. break;
  1565. case 4: // Prepare TEMP_1
  1566. #if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1)
  1567. #if TEMP_1_PIN > 7
  1568. ADCSRB = 1<<MUX5;
  1569. #else
  1570. ADCSRB = 0;
  1571. #endif
  1572. ADMUX = ((1 << REFS0) | (TEMP_1_PIN & 0x07));
  1573. ADCSRA |= 1<<ADSC; // Start conversion
  1574. #endif
  1575. lcd_buttons_update();
  1576. temp_state = 5;
  1577. break;
  1578. case 5: // Measure TEMP_1
  1579. #if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1)
  1580. raw_temp_1_value += ADC;
  1581. #endif
  1582. temp_state = 6;
  1583. break;
  1584. case 6: // Prepare TEMP_2
  1585. #if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
  1586. #if TEMP_2_PIN > 7
  1587. ADCSRB = 1<<MUX5;
  1588. #else
  1589. ADCSRB = 0;
  1590. #endif
  1591. ADMUX = ((1 << REFS0) | (TEMP_2_PIN & 0x07));
  1592. ADCSRA |= 1<<ADSC; // Start conversion
  1593. #endif
  1594. lcd_buttons_update();
  1595. temp_state = 7;
  1596. break;
  1597. case 7: // Measure TEMP_2
  1598. #if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
  1599. raw_temp_2_value += ADC;
  1600. #endif
  1601. temp_state = 8;//change so that Filament Width is also measured
  1602. break;
  1603. case 8: //Prepare FILWIDTH
  1604. #if defined(FILWIDTH_PIN) && (FILWIDTH_PIN> -1)
  1605. #if FILWIDTH_PIN>7
  1606. ADCSRB = 1<<MUX5;
  1607. #else
  1608. ADCSRB = 0;
  1609. #endif
  1610. ADMUX = ((1 << REFS0) | (FILWIDTH_PIN & 0x07));
  1611. ADCSRA |= 1<<ADSC; // Start conversion
  1612. #endif
  1613. lcd_buttons_update();
  1614. temp_state = 9;
  1615. break;
  1616. case 9: //Measure FILWIDTH
  1617. #if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1)
  1618. //raw_filwidth_value += ADC; //remove to use an IIR filter approach
  1619. if(ADC>102) //check that ADC is reading a voltage > 0.5 volts, otherwise don't take in the data.
  1620. {
  1621. raw_filwidth_value= raw_filwidth_value-(raw_filwidth_value>>7); //multipliy raw_filwidth_value by 127/128
  1622. raw_filwidth_value= raw_filwidth_value + ((unsigned long)ADC<<7); //add new ADC reading
  1623. }
  1624. #endif
  1625. temp_state = 0;
  1626. temp_count++;
  1627. break;
  1628. case 10: //Startup, delay initial temp reading a tiny bit so the hardware can settle.
  1629. temp_state = 0;
  1630. break;
  1631. // default:
  1632. // SERIAL_ERROR_START;
  1633. // SERIAL_ERRORLNPGM("Temp measurement error!");
  1634. // break;
  1635. }
  1636. if(temp_count >= OVERSAMPLENR) // 10 * 16 * 1/(16000000/64/256) = 164ms.
  1637. {
  1638. if (!temp_meas_ready) //Only update the raw values if they have been read. Else we could be updating them during reading.
  1639. {
  1640. current_temperature_raw[0] = raw_temp_0_value;
  1641. #if EXTRUDERS > 1
  1642. current_temperature_raw[1] = raw_temp_1_value;
  1643. #endif
  1644. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  1645. redundant_temperature_raw = raw_temp_1_value;
  1646. #endif
  1647. #if EXTRUDERS > 2
  1648. current_temperature_raw[2] = raw_temp_2_value;
  1649. #endif
  1650. current_temperature_bed_raw = raw_temp_bed_value;
  1651. }
  1652. //Add similar code for Filament Sensor - can be read any time since IIR filtering is used
  1653. #if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1)
  1654. current_raw_filwidth = raw_filwidth_value>>10; //need to divide to get to 0-16384 range since we used 1/128 IIR filter approach
  1655. #endif
  1656. temp_meas_ready = true;
  1657. temp_count = 0;
  1658. raw_temp_0_value = 0;
  1659. raw_temp_1_value = 0;
  1660. raw_temp_2_value = 0;
  1661. raw_temp_bed_value = 0;
  1662. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1663. if(current_temperature_raw[0] <= maxttemp_raw[0]) {
  1664. #else
  1665. if(current_temperature_raw[0] >= maxttemp_raw[0]) {
  1666. #endif
  1667. max_temp_error(0);
  1668. }
  1669. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1670. if(current_temperature_raw[0] >= minttemp_raw[0]) {
  1671. #else
  1672. if(current_temperature_raw[0] <= minttemp_raw[0]) {
  1673. #endif
  1674. min_temp_error(0);
  1675. }
  1676. #if EXTRUDERS > 1
  1677. #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
  1678. if(current_temperature_raw[1] <= maxttemp_raw[1]) {
  1679. #else
  1680. if(current_temperature_raw[1] >= maxttemp_raw[1]) {
  1681. #endif
  1682. max_temp_error(1);
  1683. }
  1684. #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
  1685. if(current_temperature_raw[1] >= minttemp_raw[1]) {
  1686. #else
  1687. if(current_temperature_raw[1] <= minttemp_raw[1]) {
  1688. #endif
  1689. min_temp_error(1);
  1690. }
  1691. #endif
  1692. #if EXTRUDERS > 2
  1693. #if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
  1694. if(current_temperature_raw[2] <= maxttemp_raw[2]) {
  1695. #else
  1696. if(current_temperature_raw[2] >= maxttemp_raw[2]) {
  1697. #endif
  1698. max_temp_error(2);
  1699. }
  1700. #if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
  1701. if(current_temperature_raw[2] >= minttemp_raw[2]) {
  1702. #else
  1703. if(current_temperature_raw[2] <= minttemp_raw[2]) {
  1704. #endif
  1705. min_temp_error(2);
  1706. }
  1707. #endif
  1708. /* No bed MINTEMP error? */
  1709. #if defined(BED_MAXTEMP) && (TEMP_SENSOR_BED != 0)
  1710. # if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1711. if(current_temperature_bed_raw <= bed_maxttemp_raw) {
  1712. #else
  1713. if(current_temperature_bed_raw >= bed_maxttemp_raw) {
  1714. #endif
  1715. target_temperature_bed = 0;
  1716. bed_max_temp_error();
  1717. }
  1718. }
  1719. # if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1720. if(current_temperature_bed_raw >= bed_minttemp_raw) {
  1721. #else
  1722. if(current_temperature_bed_raw <= bed_minttemp_raw) {
  1723. #endif
  1724. bed_min_temp_error();
  1725. }
  1726. #endif
  1727. #ifdef BABYSTEPPING
  1728. for(uint8_t axis=0;axis<3;axis++)
  1729. {
  1730. int curTodo=babystepsTodo[axis]; //get rid of volatile for performance
  1731. if(curTodo>0)
  1732. {
  1733. babystep(axis,/*fwd*/true);
  1734. babystepsTodo[axis]--; //less to do next time
  1735. }
  1736. else
  1737. if(curTodo<0)
  1738. {
  1739. babystep(axis,/*fwd*/false);
  1740. babystepsTodo[axis]++; //less to do next time
  1741. }
  1742. }
  1743. #endif //BABYSTEPPING
  1744. }
  1745. #ifdef PIDTEMP
  1746. // Apply the scale factors to the PID values
  1747. float scalePID_i(float i)
  1748. {
  1749. return i*PID_dT;
  1750. }
  1751. float unscalePID_i(float i)
  1752. {
  1753. return i/PID_dT;
  1754. }
  1755. float scalePID_d(float d)
  1756. {
  1757. return d/PID_dT;
  1758. }
  1759. float unscalePID_d(float d)
  1760. {
  1761. return d*PID_dT;
  1762. }
  1763. #endif //PIDTEMP