temperature.cpp 58 KB

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