temperature.cpp 57 KB

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