temperature.cpp 58 KB

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