temperature.cpp 56 KB

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