temperature.cpp 55 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 "temperature.h"
  26. #include "watchdog.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. #ifdef FILAMENT_SENSOR
  82. int current_raw_filwidth = 0; //Holds measured filament diameter - one extruder only
  83. #endif
  84. //===========================================================================
  85. //=============================private variables============================
  86. //===========================================================================
  87. static volatile bool temp_meas_ready = false;
  88. #ifdef PIDTEMP
  89. //static cannot be external:
  90. static float temp_iState[EXTRUDERS] = { 0 };
  91. static float temp_dState[EXTRUDERS] = { 0 };
  92. static float pTerm[EXTRUDERS];
  93. static float iTerm[EXTRUDERS];
  94. static float dTerm[EXTRUDERS];
  95. //int output;
  96. static float pid_error[EXTRUDERS];
  97. static float temp_iState_min[EXTRUDERS];
  98. static float temp_iState_max[EXTRUDERS];
  99. // static float pid_input[EXTRUDERS];
  100. // static float pid_output[EXTRUDERS];
  101. static bool pid_reset[EXTRUDERS];
  102. #endif //PIDTEMP
  103. #ifdef PIDTEMPBED
  104. //static cannot be external:
  105. static float temp_iState_bed = { 0 };
  106. static float temp_dState_bed = { 0 };
  107. static float pTerm_bed;
  108. static float iTerm_bed;
  109. static float dTerm_bed;
  110. //int output;
  111. static float pid_error_bed;
  112. static float temp_iState_min_bed;
  113. static float temp_iState_max_bed;
  114. #else //PIDTEMPBED
  115. static unsigned long previous_millis_bed_heater;
  116. #endif //PIDTEMPBED
  117. static unsigned char soft_pwm[EXTRUDERS];
  118. #ifdef FAN_SOFT_PWM
  119. static unsigned char soft_pwm_fan;
  120. #endif
  121. #if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
  122. (defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
  123. (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1)
  124. static unsigned long extruder_autofan_last_check;
  125. #endif
  126. #if EXTRUDERS > 3
  127. # error Unsupported number of extruders
  128. #elif EXTRUDERS > 2
  129. # define ARRAY_BY_EXTRUDERS(v1, v2, v3) { v1, v2, v3 }
  130. #elif EXTRUDERS > 1
  131. # define ARRAY_BY_EXTRUDERS(v1, v2, v3) { v1, v2 }
  132. #else
  133. # define ARRAY_BY_EXTRUDERS(v1, v2, v3) { v1 }
  134. #endif
  135. // Init min and max temp with extreme values to prevent false errors during startup
  136. static int minttemp_raw[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_RAW_LO_TEMP , HEATER_1_RAW_LO_TEMP , HEATER_2_RAW_LO_TEMP );
  137. static int maxttemp_raw[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_RAW_HI_TEMP , HEATER_1_RAW_HI_TEMP , HEATER_2_RAW_HI_TEMP );
  138. static int minttemp[EXTRUDERS] = ARRAY_BY_EXTRUDERS( 0, 0, 0 );
  139. static int maxttemp[EXTRUDERS] = ARRAY_BY_EXTRUDERS( 16383, 16383, 16383 );
  140. #ifdef BED_MINTEMP
  141. static int bed_minttemp_raw = HEATER_BED_RAW_LO_TEMP;
  142. #endif
  143. #ifdef BED_MAXTEMP
  144. static int bed_maxttemp_raw = HEATER_BED_RAW_HI_TEMP;
  145. #endif
  146. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  147. static void *heater_ttbl_map[2] = {(void *)HEATER_0_TEMPTABLE, (void *)HEATER_1_TEMPTABLE };
  148. static uint8_t heater_ttbllen_map[2] = { HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN };
  149. #else
  150. static void *heater_ttbl_map[EXTRUDERS] = ARRAY_BY_EXTRUDERS( (void *)HEATER_0_TEMPTABLE, (void *)HEATER_1_TEMPTABLE, (void *)HEATER_2_TEMPTABLE );
  151. static uint8_t heater_ttbllen_map[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN, HEATER_2_TEMPTABLE_LEN );
  152. #endif
  153. static float analog2temp(int raw, uint8_t e);
  154. static float analog2tempBed(int raw);
  155. static float analog2tempAmbient(int raw);
  156. static void updateTemperaturesFromRawValues();
  157. enum TempRunawayStates
  158. {
  159. TempRunaway_INACTIVE = 0,
  160. TempRunaway_PREHEAT = 1,
  161. TempRunaway_ACTIVE = 2,
  162. };
  163. #ifdef WATCH_TEMP_PERIOD
  164. int watch_start_temp[EXTRUDERS] = ARRAY_BY_EXTRUDERS(0,0,0);
  165. unsigned long watchmillis[EXTRUDERS] = ARRAY_BY_EXTRUDERS(0,0,0);
  166. #endif //WATCH_TEMP_PERIOD
  167. #ifndef SOFT_PWM_SCALE
  168. #define SOFT_PWM_SCALE 0
  169. #endif
  170. #ifdef FILAMENT_SENSOR
  171. static int meas_shift_index; //used to point to a delayed sample in buffer for filament width sensor
  172. #endif
  173. //===========================================================================
  174. //============================= functions ============================
  175. //===========================================================================
  176. void PID_autotune(float temp, int extruder, int ncycles)
  177. {
  178. pid_number_of_cycles = ncycles;
  179. pid_tuning_finished = false;
  180. float input = 0.0;
  181. pid_cycle=0;
  182. bool heating = true;
  183. unsigned long temp_millis = millis();
  184. unsigned long t1=temp_millis;
  185. unsigned long t2=temp_millis;
  186. long t_high = 0;
  187. long t_low = 0;
  188. long bias, d;
  189. float Ku, Tu;
  190. float max = 0, min = 10000;
  191. uint8_t safety_check_cycles = 0;
  192. const uint8_t safety_check_cycles_count = (extruder < 0) ? 45 : 10; //10 cycles / 20s delay for extruder and 45 cycles / 90s for heatbed
  193. float temp_ambient;
  194. #if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
  195. (defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
  196. (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1)
  197. unsigned long extruder_autofan_last_check = millis();
  198. #endif
  199. if ((extruder >= EXTRUDERS)
  200. #if (TEMP_BED_PIN <= -1)
  201. ||(extruder < 0)
  202. #endif
  203. ){
  204. SERIAL_ECHOLN("PID Autotune failed. Bad extruder number.");
  205. pid_tuning_finished = true;
  206. pid_cycle = 0;
  207. return;
  208. }
  209. SERIAL_ECHOLN("PID Autotune start");
  210. disable_heater(); // switch off all heaters.
  211. if (extruder<0)
  212. {
  213. soft_pwm_bed = (MAX_BED_POWER)/2;
  214. bias = d = (MAX_BED_POWER)/2;
  215. }
  216. else
  217. {
  218. soft_pwm[extruder] = (PID_MAX)/2;
  219. bias = d = (PID_MAX)/2;
  220. }
  221. for(;;) {
  222. wdt_reset();
  223. if(temp_meas_ready == true) { // temp sample ready
  224. updateTemperaturesFromRawValues();
  225. input = (extruder<0)?current_temperature_bed:current_temperature[extruder];
  226. max=max(max,input);
  227. min=min(min,input);
  228. #if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
  229. (defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
  230. (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1)
  231. if(millis() - extruder_autofan_last_check > 2500) {
  232. checkExtruderAutoFans();
  233. extruder_autofan_last_check = millis();
  234. }
  235. #endif
  236. if(heating == true && input > temp) {
  237. if(millis() - t2 > 5000) {
  238. heating=false;
  239. if (extruder<0)
  240. soft_pwm_bed = (bias - d) >> 1;
  241. else
  242. soft_pwm[extruder] = (bias - d) >> 1;
  243. t1=millis();
  244. t_high=t1 - t2;
  245. max=temp;
  246. }
  247. }
  248. if(heating == false && input < temp) {
  249. if(millis() - t1 > 5000) {
  250. heating=true;
  251. t2=millis();
  252. t_low=t2 - t1;
  253. if(pid_cycle > 0) {
  254. bias += (d*(t_high - t_low))/(t_low + t_high);
  255. bias = constrain(bias, 20 ,(extruder<0?(MAX_BED_POWER):(PID_MAX))-20);
  256. if(bias > (extruder<0?(MAX_BED_POWER):(PID_MAX))/2) d = (extruder<0?(MAX_BED_POWER):(PID_MAX)) - 1 - bias;
  257. else d = bias;
  258. SERIAL_PROTOCOLPGM(" bias: "); SERIAL_PROTOCOL(bias);
  259. SERIAL_PROTOCOLPGM(" d: "); SERIAL_PROTOCOL(d);
  260. SERIAL_PROTOCOLPGM(" min: "); SERIAL_PROTOCOL(min);
  261. SERIAL_PROTOCOLPGM(" max: "); SERIAL_PROTOCOLLN(max);
  262. if(pid_cycle > 2) {
  263. Ku = (4.0*d)/(3.14159*(max-min)/2.0);
  264. Tu = ((float)(t_low + t_high)/1000.0);
  265. SERIAL_PROTOCOLPGM(" Ku: "); SERIAL_PROTOCOL(Ku);
  266. SERIAL_PROTOCOLPGM(" Tu: "); SERIAL_PROTOCOLLN(Tu);
  267. _Kp = 0.6*Ku;
  268. _Ki = 2*_Kp/Tu;
  269. _Kd = _Kp*Tu/8;
  270. SERIAL_PROTOCOLLNPGM(" Classic PID ");
  271. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(_Kp);
  272. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(_Ki);
  273. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(_Kd);
  274. /*
  275. _Kp = 0.33*Ku;
  276. _Ki = _Kp/Tu;
  277. _Kd = _Kp*Tu/3;
  278. SERIAL_PROTOCOLLNPGM(" Some overshoot ");
  279. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(_Kp);
  280. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(_Ki);
  281. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(_Kd);
  282. _Kp = 0.2*Ku;
  283. _Ki = 2*_Kp/Tu;
  284. _Kd = _Kp*Tu/3;
  285. SERIAL_PROTOCOLLNPGM(" No overshoot ");
  286. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(_Kp);
  287. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(_Ki);
  288. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(_Kd);
  289. */
  290. }
  291. }
  292. if (extruder<0)
  293. soft_pwm_bed = (bias + d) >> 1;
  294. else
  295. soft_pwm[extruder] = (bias + d) >> 1;
  296. pid_cycle++;
  297. min=temp;
  298. }
  299. }
  300. }
  301. if(input > (temp + 20)) {
  302. SERIAL_PROTOCOLLNPGM("PID Autotune failed! Temperature too high");
  303. pid_tuning_finished = true;
  304. pid_cycle = 0;
  305. return;
  306. }
  307. if(millis() - temp_millis > 2000) {
  308. int p;
  309. if (extruder<0){
  310. p=soft_pwm_bed;
  311. SERIAL_PROTOCOLPGM("ok B:");
  312. }else{
  313. p=soft_pwm[extruder];
  314. SERIAL_PROTOCOLPGM("ok T:");
  315. }
  316. SERIAL_PROTOCOL(input);
  317. SERIAL_PROTOCOLPGM(" @:");
  318. SERIAL_PROTOCOLLN(p);
  319. if (safety_check_cycles == 0) { //save ambient temp
  320. temp_ambient = input;
  321. //SERIAL_ECHOPGM("Ambient T: ");
  322. //MYSERIAL.println(temp_ambient);
  323. safety_check_cycles++;
  324. }
  325. else if (safety_check_cycles < safety_check_cycles_count) { //delay
  326. safety_check_cycles++;
  327. }
  328. else if (safety_check_cycles == safety_check_cycles_count){ //check that temperature is rising
  329. safety_check_cycles++;
  330. //SERIAL_ECHOPGM("Time from beginning: ");
  331. //MYSERIAL.print(safety_check_cycles_count * 2);
  332. //SERIAL_ECHOPGM("s. Difference between current and ambient T: ");
  333. //MYSERIAL.println(input - temp_ambient);
  334. if (abs(input - temp_ambient) < 5.0) {
  335. temp_runaway_stop(false, (extruder<0));
  336. pid_tuning_finished = true;
  337. return;
  338. }
  339. }
  340. temp_millis = millis();
  341. }
  342. if(((millis() - t1) + (millis() - t2)) > (10L*60L*1000L*2L)) {
  343. SERIAL_PROTOCOLLNPGM("PID Autotune failed! timeout");
  344. pid_tuning_finished = true;
  345. pid_cycle = 0;
  346. return;
  347. }
  348. if(pid_cycle > ncycles) {
  349. SERIAL_PROTOCOLLNPGM("PID Autotune finished! Put the last Kp, Ki and Kd constants from above into Configuration.h");
  350. pid_tuning_finished = true;
  351. pid_cycle = 0;
  352. return;
  353. }
  354. lcd_update();
  355. }
  356. }
  357. void updatePID()
  358. {
  359. #ifdef PIDTEMP
  360. for(int e = 0; e < EXTRUDERS; e++) {
  361. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / Ki;
  362. }
  363. #endif
  364. #ifdef PIDTEMPBED
  365. temp_iState_max_bed = PID_INTEGRAL_DRIVE_MAX / bedKi;
  366. #endif
  367. }
  368. int getHeaterPower(int heater) {
  369. if (heater<0)
  370. return soft_pwm_bed;
  371. return soft_pwm[heater];
  372. }
  373. #if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
  374. (defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
  375. (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1)
  376. #if defined(FAN_PIN) && FAN_PIN > -1
  377. #if EXTRUDER_0_AUTO_FAN_PIN == FAN_PIN
  378. #error "You cannot set EXTRUDER_0_AUTO_FAN_PIN equal to FAN_PIN"
  379. #endif
  380. #if EXTRUDER_1_AUTO_FAN_PIN == FAN_PIN
  381. #error "You cannot set EXTRUDER_1_AUTO_FAN_PIN equal to FAN_PIN"
  382. #endif
  383. #if EXTRUDER_2_AUTO_FAN_PIN == FAN_PIN
  384. #error "You cannot set EXTRUDER_2_AUTO_FAN_PIN equal to FAN_PIN"
  385. #endif
  386. #endif
  387. void setExtruderAutoFanState(int pin, bool state)
  388. {
  389. unsigned char newFanSpeed = (state != 0) ? EXTRUDER_AUTO_FAN_SPEED : 0;
  390. // this idiom allows both digital and PWM fan outputs (see M42 handling).
  391. pinMode(pin, OUTPUT);
  392. digitalWrite(pin, newFanSpeed);
  393. analogWrite(pin, newFanSpeed);
  394. }
  395. void countFanSpeed()
  396. {
  397. //SERIAL_ECHOPGM("edge counter 1:"); MYSERIAL.println(fan_edge_counter[1]);
  398. fan_speed[0] = (fan_edge_counter[0] * (float(250) / (millis() - extruder_autofan_last_check)));
  399. fan_speed[1] = (fan_edge_counter[1] * (float(250) / (millis() - extruder_autofan_last_check)));
  400. /*SERIAL_ECHOPGM("time interval: "); MYSERIAL.println(millis() - extruder_autofan_last_check);
  401. SERIAL_ECHOPGM("extruder fan speed:"); MYSERIAL.print(fan_speed[0]); SERIAL_ECHOPGM("; edge counter:"); MYSERIAL.println(fan_edge_counter[0]);
  402. SERIAL_ECHOPGM("print fan speed:"); MYSERIAL.print(fan_speed[1]); SERIAL_ECHOPGM("; edge counter:"); MYSERIAL.println(fan_edge_counter[1]);
  403. SERIAL_ECHOLNPGM(" ");*/
  404. fan_edge_counter[0] = 0;
  405. fan_edge_counter[1] = 0;
  406. }
  407. extern bool fans_check_enabled;
  408. void checkFanSpeed()
  409. {
  410. fans_check_enabled = (eeprom_read_byte((uint8_t*)EEPROM_FAN_CHECK_ENABLED) > 0);
  411. static unsigned char fan_speed_errors[2] = { 0,0 };
  412. if (fan_speed[0] == 0 && (current_temperature[0] > EXTRUDER_AUTO_FAN_TEMPERATURE)) fan_speed_errors[0]++;
  413. else fan_speed_errors[0] = 0;
  414. if ((fan_speed[1] == 0)&& (fanSpeed > MIN_PRINT_FAN_SPEED)) fan_speed_errors[1]++;
  415. else fan_speed_errors[1] = 0;
  416. if ((fan_speed_errors[0] > 5) && fans_check_enabled) fanSpeedError(0); //extruder fan
  417. if ((fan_speed_errors[1] > 15) && fans_check_enabled) fanSpeedError(1); //print fan
  418. }
  419. extern void stop_and_save_print_to_ram(float z_move, float e_move);
  420. extern void restore_print_from_ram_and_continue(float e_move);
  421. void fanSpeedError(unsigned char _fan) {
  422. if (get_message_level() != 0 && isPrintPaused) return;
  423. //to ensure that target temp. is not set to zero in case taht we are resuming print
  424. if (card.sdprinting) {
  425. if (heating_status != 0) {
  426. lcd_print_stop();
  427. }
  428. else {
  429. isPrintPaused = true;
  430. lcd_sdcard_pause();
  431. }
  432. }
  433. else {
  434. setTargetHotend0(0);
  435. Stop();
  436. }
  437. SERIAL_ERROR_START;
  438. switch (_fan) {
  439. case 0:
  440. SERIAL_ERRORLNPGM("ERROR: Extruder fan speed is lower then expected");
  441. if (get_message_level() == 0) {
  442. WRITE(BEEPER, HIGH);
  443. delayMicroseconds(200);
  444. WRITE(BEEPER, LOW);
  445. delayMicroseconds(100);
  446. LCD_ALERTMESSAGEPGM("Err: EXTR. FAN ERROR");
  447. }
  448. break;
  449. case 1:
  450. SERIAL_ERRORLNPGM("ERROR: Print fan speed is lower then expected");
  451. if (get_message_level() == 0) {
  452. WRITE(BEEPER, HIGH);
  453. delayMicroseconds(200);
  454. WRITE(BEEPER, LOW);
  455. delayMicroseconds(100);
  456. LCD_ALERTMESSAGEPGM("Err: PRINT FAN ERROR");
  457. }
  458. break;
  459. }
  460. }
  461. void checkExtruderAutoFans()
  462. {
  463. uint8_t fanState = 0;
  464. // which fan pins need to be turned on?
  465. #if defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1
  466. if (current_temperature[0] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  467. fanState |= 1;
  468. #endif
  469. #if defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1
  470. if (current_temperature[1] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  471. {
  472. if (EXTRUDER_1_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN)
  473. fanState |= 1;
  474. else
  475. fanState |= 2;
  476. }
  477. #endif
  478. #if defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1
  479. if (current_temperature[2] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  480. {
  481. if (EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN)
  482. fanState |= 1;
  483. else if (EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_1_AUTO_FAN_PIN)
  484. fanState |= 2;
  485. else
  486. fanState |= 4;
  487. }
  488. #endif
  489. // update extruder auto fan states
  490. #if defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1
  491. setExtruderAutoFanState(EXTRUDER_0_AUTO_FAN_PIN, (fanState & 1) != 0);
  492. #endif
  493. #if defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1
  494. if (EXTRUDER_1_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN)
  495. setExtruderAutoFanState(EXTRUDER_1_AUTO_FAN_PIN, (fanState & 2) != 0);
  496. #endif
  497. #if defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1
  498. if (EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN
  499. && EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_1_AUTO_FAN_PIN)
  500. setExtruderAutoFanState(EXTRUDER_2_AUTO_FAN_PIN, (fanState & 4) != 0);
  501. #endif
  502. }
  503. #endif // any extruder auto fan pins set
  504. void manage_heater()
  505. {
  506. wdt_reset();
  507. float pid_input;
  508. float pid_output;
  509. if(temp_meas_ready != true) //better readability
  510. return;
  511. updateTemperaturesFromRawValues();
  512. #ifdef TEMP_RUNAWAY_BED_HYSTERESIS
  513. temp_runaway_check(0, target_temperature_bed, current_temperature_bed, (int)soft_pwm_bed, true);
  514. #endif
  515. for(int e = 0; e < EXTRUDERS; e++)
  516. {
  517. #ifdef TEMP_RUNAWAY_EXTRUDER_HYSTERESIS
  518. temp_runaway_check(e+1, target_temperature[e], current_temperature[e], (int)soft_pwm[e], false);
  519. #endif
  520. #ifdef PIDTEMP
  521. pid_input = current_temperature[e];
  522. #ifndef PID_OPENLOOP
  523. pid_error[e] = target_temperature[e] - pid_input;
  524. if(pid_error[e] > PID_FUNCTIONAL_RANGE) {
  525. pid_output = BANG_MAX;
  526. pid_reset[e] = true;
  527. }
  528. else if(pid_error[e] < -PID_FUNCTIONAL_RANGE || target_temperature[e] == 0) {
  529. pid_output = 0;
  530. pid_reset[e] = true;
  531. }
  532. else {
  533. if(pid_reset[e] == true) {
  534. temp_iState[e] = 0.0;
  535. pid_reset[e] = false;
  536. }
  537. pTerm[e] = Kp * pid_error[e];
  538. temp_iState[e] += pid_error[e];
  539. temp_iState[e] = constrain(temp_iState[e], temp_iState_min[e], temp_iState_max[e]);
  540. iTerm[e] = Ki * temp_iState[e];
  541. //K1 defined in Configuration.h in the PID settings
  542. #define K2 (1.0-K1)
  543. dTerm[e] = (Kd * (pid_input - temp_dState[e]))*K2 + (K1 * dTerm[e]);
  544. pid_output = pTerm[e] + iTerm[e] - dTerm[e];
  545. if (pid_output > PID_MAX) {
  546. if (pid_error[e] > 0 ) temp_iState[e] -= pid_error[e]; // conditional un-integration
  547. pid_output=PID_MAX;
  548. } else if (pid_output < 0){
  549. if (pid_error[e] < 0 ) temp_iState[e] -= pid_error[e]; // conditional un-integration
  550. pid_output=0;
  551. }
  552. }
  553. temp_dState[e] = pid_input;
  554. #else
  555. pid_output = constrain(target_temperature[e], 0, PID_MAX);
  556. #endif //PID_OPENLOOP
  557. #ifdef PID_DEBUG
  558. SERIAL_ECHO_START;
  559. SERIAL_ECHO(" PID_DEBUG ");
  560. SERIAL_ECHO(e);
  561. SERIAL_ECHO(": Input ");
  562. SERIAL_ECHO(pid_input);
  563. SERIAL_ECHO(" Output ");
  564. SERIAL_ECHO(pid_output);
  565. SERIAL_ECHO(" pTerm ");
  566. SERIAL_ECHO(pTerm[e]);
  567. SERIAL_ECHO(" iTerm ");
  568. SERIAL_ECHO(iTerm[e]);
  569. SERIAL_ECHO(" dTerm ");
  570. SERIAL_ECHOLN(dTerm[e]);
  571. #endif //PID_DEBUG
  572. #else /* PID off */
  573. pid_output = 0;
  574. if(current_temperature[e] < target_temperature[e]) {
  575. pid_output = PID_MAX;
  576. }
  577. #endif
  578. // Check if temperature is within the correct range
  579. if(((current_temperature_ambient < MINTEMP_MINAMBIENT) || (current_temperature[e] > minttemp[e])) && (current_temperature[e] < maxttemp[e]))
  580. {
  581. soft_pwm[e] = (int)pid_output >> 1;
  582. }
  583. else {
  584. soft_pwm[e] = 0;
  585. }
  586. #ifdef WATCH_TEMP_PERIOD
  587. if(watchmillis[e] && millis() - watchmillis[e] > WATCH_TEMP_PERIOD)
  588. {
  589. if(degHotend(e) < watch_start_temp[e] + WATCH_TEMP_INCREASE)
  590. {
  591. setTargetHotend(0, e);
  592. LCD_MESSAGEPGM("Heating failed");
  593. SERIAL_ECHO_START;
  594. SERIAL_ECHOLN("Heating failed");
  595. }else{
  596. watchmillis[e] = 0;
  597. }
  598. }
  599. #endif
  600. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  601. if(fabs(current_temperature[0] - redundant_temperature) > MAX_REDUNDANT_TEMP_SENSOR_DIFF) {
  602. disable_heater();
  603. if(IsStopped() == false) {
  604. SERIAL_ERROR_START;
  605. SERIAL_ERRORLNPGM("Extruder switched off. Temperature difference between temp sensors is too high !");
  606. LCD_ALERTMESSAGEPGM("Err: REDUNDANT TEMP ERROR");
  607. }
  608. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  609. Stop();
  610. #endif
  611. }
  612. #endif
  613. } // End extruder for loop
  614. #ifndef DEBUG_DISABLE_FANCHECK
  615. #if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
  616. (defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
  617. (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1)
  618. if(millis() - extruder_autofan_last_check > 1000) // only need to check fan state very infrequently
  619. {
  620. countFanSpeed();
  621. checkFanSpeed();
  622. checkExtruderAutoFans();
  623. extruder_autofan_last_check = millis();
  624. }
  625. #endif
  626. #endif //DEBUG_DISABLE_FANCHECK
  627. #ifndef PIDTEMPBED
  628. if(millis() - previous_millis_bed_heater < BED_CHECK_INTERVAL)
  629. return;
  630. previous_millis_bed_heater = millis();
  631. #endif
  632. #if TEMP_SENSOR_BED != 0
  633. #ifdef PIDTEMPBED
  634. pid_input = current_temperature_bed;
  635. #ifndef PID_OPENLOOP
  636. pid_error_bed = target_temperature_bed - pid_input;
  637. pTerm_bed = bedKp * pid_error_bed;
  638. temp_iState_bed += pid_error_bed;
  639. temp_iState_bed = constrain(temp_iState_bed, temp_iState_min_bed, temp_iState_max_bed);
  640. iTerm_bed = bedKi * temp_iState_bed;
  641. //K1 defined in Configuration.h in the PID settings
  642. #define K2 (1.0-K1)
  643. dTerm_bed= (bedKd * (pid_input - temp_dState_bed))*K2 + (K1 * dTerm_bed);
  644. temp_dState_bed = pid_input;
  645. pid_output = pTerm_bed + iTerm_bed - dTerm_bed;
  646. if (pid_output > MAX_BED_POWER) {
  647. if (pid_error_bed > 0 ) temp_iState_bed -= pid_error_bed; // conditional un-integration
  648. pid_output=MAX_BED_POWER;
  649. } else if (pid_output < 0){
  650. if (pid_error_bed < 0 ) temp_iState_bed -= pid_error_bed; // conditional un-integration
  651. pid_output=0;
  652. }
  653. #else
  654. pid_output = constrain(target_temperature_bed, 0, MAX_BED_POWER);
  655. #endif //PID_OPENLOOP
  656. if(((current_temperature_bed > BED_MINTEMP) || (current_temperature_ambient < MINTEMP_MINAMBIENT)) && (current_temperature_bed < BED_MAXTEMP))
  657. {
  658. soft_pwm_bed = (int)pid_output >> 1;
  659. }
  660. else {
  661. soft_pwm_bed = 0;
  662. }
  663. #elif !defined(BED_LIMIT_SWITCHING)
  664. // Check if temperature is within the correct range
  665. if((current_temperature_bed > BED_MINTEMP) && (current_temperature_bed < BED_MAXTEMP))
  666. {
  667. if(current_temperature_bed >= target_temperature_bed)
  668. {
  669. soft_pwm_bed = 0;
  670. }
  671. else
  672. {
  673. soft_pwm_bed = MAX_BED_POWER>>1;
  674. }
  675. }
  676. else
  677. {
  678. soft_pwm_bed = 0;
  679. WRITE(HEATER_BED_PIN,LOW);
  680. }
  681. #else //#ifdef BED_LIMIT_SWITCHING
  682. // Check if temperature is within the correct band
  683. if((current_temperature_bed > BED_MINTEMP) && (current_temperature_bed < BED_MAXTEMP))
  684. {
  685. if(current_temperature_bed > target_temperature_bed + BED_HYSTERESIS)
  686. {
  687. soft_pwm_bed = 0;
  688. }
  689. else if(current_temperature_bed <= target_temperature_bed - BED_HYSTERESIS)
  690. {
  691. soft_pwm_bed = MAX_BED_POWER>>1;
  692. }
  693. }
  694. else
  695. {
  696. soft_pwm_bed = 0;
  697. WRITE(HEATER_BED_PIN,LOW);
  698. }
  699. #endif
  700. #endif
  701. //code for controlling the extruder rate based on the width sensor
  702. #ifdef FILAMENT_SENSOR
  703. if(filament_sensor)
  704. {
  705. meas_shift_index=delay_index1-meas_delay_cm;
  706. if(meas_shift_index<0)
  707. meas_shift_index = meas_shift_index + (MAX_MEASUREMENT_DELAY+1); //loop around buffer if needed
  708. //get the delayed info and add 100 to reconstitute to a percent of the nominal filament diameter
  709. //then square it to get an area
  710. if(meas_shift_index<0)
  711. meas_shift_index=0;
  712. else if (meas_shift_index>MAX_MEASUREMENT_DELAY)
  713. meas_shift_index=MAX_MEASUREMENT_DELAY;
  714. volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] = pow((float)(100+measurement_delay[meas_shift_index])/100.0,2);
  715. if (volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] <0.01)
  716. volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM]=0.01;
  717. }
  718. #endif
  719. #ifdef HOST_KEEPALIVE_FEATURE
  720. host_keepalive();
  721. #endif
  722. }
  723. #define PGM_RD_W(x) (short)pgm_read_word(&x)
  724. // Derived from RepRap FiveD extruder::getTemperature()
  725. // For hot end temperature measurement.
  726. static float analog2temp(int raw, uint8_t e) {
  727. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  728. if(e > EXTRUDERS)
  729. #else
  730. if(e >= EXTRUDERS)
  731. #endif
  732. {
  733. SERIAL_ERROR_START;
  734. SERIAL_ERROR((int)e);
  735. SERIAL_ERRORLNPGM(" - Invalid extruder number !");
  736. kill("", 6);
  737. return 0.0;
  738. }
  739. #ifdef HEATER_0_USES_MAX6675
  740. if (e == 0)
  741. {
  742. return 0.25 * raw;
  743. }
  744. #endif
  745. if(heater_ttbl_map[e] != NULL)
  746. {
  747. float celsius = 0;
  748. uint8_t i;
  749. short (*tt)[][2] = (short (*)[][2])(heater_ttbl_map[e]);
  750. for (i=1; i<heater_ttbllen_map[e]; i++)
  751. {
  752. if (PGM_RD_W((*tt)[i][0]) > raw)
  753. {
  754. celsius = PGM_RD_W((*tt)[i-1][1]) +
  755. (raw - PGM_RD_W((*tt)[i-1][0])) *
  756. (float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i-1][1])) /
  757. (float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i-1][0]));
  758. break;
  759. }
  760. }
  761. // Overflow: Set to last value in the table
  762. if (i == heater_ttbllen_map[e]) celsius = PGM_RD_W((*tt)[i-1][1]);
  763. return celsius;
  764. }
  765. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  766. }
  767. // Derived from RepRap FiveD extruder::getTemperature()
  768. // For bed temperature measurement.
  769. static float analog2tempBed(int raw) {
  770. #ifdef BED_USES_THERMISTOR
  771. float celsius = 0;
  772. byte i;
  773. for (i=1; i<BEDTEMPTABLE_LEN; i++)
  774. {
  775. if (PGM_RD_W(BEDTEMPTABLE[i][0]) > raw)
  776. {
  777. celsius = PGM_RD_W(BEDTEMPTABLE[i-1][1]) +
  778. (raw - PGM_RD_W(BEDTEMPTABLE[i-1][0])) *
  779. (float)(PGM_RD_W(BEDTEMPTABLE[i][1]) - PGM_RD_W(BEDTEMPTABLE[i-1][1])) /
  780. (float)(PGM_RD_W(BEDTEMPTABLE[i][0]) - PGM_RD_W(BEDTEMPTABLE[i-1][0]));
  781. break;
  782. }
  783. }
  784. // Overflow: Set to last value in the table
  785. if (i == BEDTEMPTABLE_LEN) celsius = PGM_RD_W(BEDTEMPTABLE[i-1][1]);
  786. // temperature offset adjustment
  787. #ifdef BED_OFFSET
  788. float _offset = BED_OFFSET;
  789. float _offset_center = BED_OFFSET_CENTER;
  790. float _offset_start = BED_OFFSET_START;
  791. float _first_koef = (_offset / 2) / (_offset_center - _offset_start);
  792. float _second_koef = (_offset / 2) / (100 - _offset_center);
  793. if (celsius >= _offset_start && celsius <= _offset_center)
  794. {
  795. celsius = celsius + (_first_koef * (celsius - _offset_start));
  796. }
  797. else if (celsius > _offset_center && celsius <= 100)
  798. {
  799. celsius = celsius + (_first_koef * (_offset_center - _offset_start)) + ( _second_koef * ( celsius - ( 100 - _offset_center ) )) ;
  800. }
  801. else if (celsius > 100)
  802. {
  803. celsius = celsius + _offset;
  804. }
  805. #endif
  806. return celsius;
  807. #elif defined BED_USES_AD595
  808. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  809. #else
  810. return 0;
  811. #endif
  812. }
  813. static float analog2tempAmbient(int raw)
  814. {
  815. float celsius = 0;
  816. byte i;
  817. for (i=1; i<AMBIENTTEMPTABLE_LEN; i++)
  818. {
  819. if (PGM_RD_W(AMBIENTTEMPTABLE[i][0]) > raw)
  820. {
  821. celsius = PGM_RD_W(AMBIENTTEMPTABLE[i-1][1]) +
  822. (raw - PGM_RD_W(AMBIENTTEMPTABLE[i-1][0])) *
  823. (float)(PGM_RD_W(AMBIENTTEMPTABLE[i][1]) - PGM_RD_W(AMBIENTTEMPTABLE[i-1][1])) /
  824. (float)(PGM_RD_W(AMBIENTTEMPTABLE[i][0]) - PGM_RD_W(AMBIENTTEMPTABLE[i-1][0]));
  825. break;
  826. }
  827. }
  828. // Overflow: Set to last value in the table
  829. if (i == AMBIENTTEMPTABLE_LEN) celsius = PGM_RD_W(AMBIENTTEMPTABLE[i-1][1]);
  830. return celsius;
  831. }
  832. /* Called to get the raw values into the the actual temperatures. The raw values are created in interrupt context,
  833. and this function is called from normal context as it is too slow to run in interrupts and will block the stepper routine otherwise */
  834. static void updateTemperaturesFromRawValues()
  835. {
  836. for(uint8_t e=0;e<EXTRUDERS;e++)
  837. {
  838. current_temperature[e] = analog2temp(current_temperature_raw[e], e);
  839. }
  840. #ifdef PINDA_THERMISTOR
  841. current_temperature_pinda = analog2tempBed(current_temperature_raw_pinda); //thermistor for pinda is the same as for bed
  842. #endif
  843. #ifdef AMBIENT_THERMISTOR
  844. current_temperature_ambient = analog2tempAmbient(current_temperature_raw_ambient); //thermistor for ambient is NTCG104LH104JT1 (2000)
  845. #endif
  846. current_temperature_bed = analog2tempBed(current_temperature_bed_raw);
  847. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  848. redundant_temperature = analog2temp(redundant_temperature_raw, 1);
  849. #endif
  850. #if defined (FILAMENT_SENSOR) && (FILWIDTH_PIN > -1) //check if a sensor is supported
  851. filament_width_meas = analog2widthFil();
  852. #endif
  853. //Reset the watchdog after we know we have a temperature measurement.
  854. watchdog_reset();
  855. CRITICAL_SECTION_START;
  856. temp_meas_ready = false;
  857. CRITICAL_SECTION_END;
  858. }
  859. // For converting raw Filament Width to milimeters
  860. #ifdef FILAMENT_SENSOR
  861. float analog2widthFil() {
  862. return current_raw_filwidth/16383.0*5.0;
  863. //return current_raw_filwidth;
  864. }
  865. // For converting raw Filament Width to a ratio
  866. int widthFil_to_size_ratio() {
  867. float temp;
  868. temp=filament_width_meas;
  869. if(filament_width_meas<MEASURED_LOWER_LIMIT)
  870. temp=filament_width_nominal; //assume sensor cut out
  871. else if (filament_width_meas>MEASURED_UPPER_LIMIT)
  872. temp= MEASURED_UPPER_LIMIT;
  873. return(filament_width_nominal/temp*100);
  874. }
  875. #endif
  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();
  1165. WRITE(BEEPER, HIGH);
  1166. delayMicroseconds(500);
  1167. WRITE(BEEPER, LOW);
  1168. delayMicroseconds(100);
  1169. if (isPreheat)
  1170. {
  1171. Stop();
  1172. isBed ? LCD_ALERTMESSAGEPGM("BED PREHEAT ERROR") : LCD_ALERTMESSAGEPGM("PREHEAT ERROR");
  1173. SERIAL_ERROR_START;
  1174. isBed ? SERIAL_ERRORLNPGM(" THERMAL RUNAWAY ( PREHEAT HEATBED)") : SERIAL_ERRORLNPGM(" THERMAL RUNAWAY ( PREHEAT HOTEND)");
  1175. SET_OUTPUT(EXTRUDER_0_AUTO_FAN_PIN);
  1176. SET_OUTPUT(FAN_PIN);
  1177. WRITE(EXTRUDER_0_AUTO_FAN_PIN, 1);
  1178. analogWrite(FAN_PIN, 255);
  1179. fanSpeed = 255;
  1180. delayMicroseconds(2000);
  1181. }
  1182. else
  1183. {
  1184. isBed ? LCD_ALERTMESSAGEPGM("BED THERMAL RUNAWAY") : LCD_ALERTMESSAGEPGM("THERMAL RUNAWAY");
  1185. SERIAL_ERROR_START;
  1186. isBed ? SERIAL_ERRORLNPGM(" HEATBED THERMAL RUNAWAY") : SERIAL_ERRORLNPGM(" HOTEND THERMAL RUNAWAY");
  1187. }
  1188. }
  1189. #endif
  1190. void disable_heater()
  1191. {
  1192. for(int i=0;i<EXTRUDERS;i++)
  1193. setTargetHotend(0,i);
  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. WRITE(BEEPER, 1);
  1241. // fanSpeed will consumed by the check_axes_activity() routine.
  1242. fanSpeed=255;
  1243. if (farm_mode) { prusa_statistics(93); }
  1244. }
  1245. void min_temp_error(uint8_t e) {
  1246. #ifdef DEBUG_DISABLE_MINTEMP
  1247. return;
  1248. #endif
  1249. //if (current_temperature_ambient < MINTEMP_MINAMBIENT) return;
  1250. disable_heater();
  1251. if(IsStopped() == false) {
  1252. SERIAL_ERROR_START;
  1253. SERIAL_ERRORLN((int)e);
  1254. SERIAL_ERRORLNPGM(": Extruder switched off. MINTEMP triggered !");
  1255. LCD_ALERTMESSAGEPGM("Err: MINTEMP");
  1256. }
  1257. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  1258. Stop();
  1259. #endif
  1260. if (farm_mode) { prusa_statistics(92); }
  1261. }
  1262. void bed_max_temp_error(void) {
  1263. #if HEATER_BED_PIN > -1
  1264. WRITE(HEATER_BED_PIN, 0);
  1265. #endif
  1266. if(IsStopped() == false) {
  1267. SERIAL_ERROR_START;
  1268. SERIAL_ERRORLNPGM("Temperature heated bed switched off. MAXTEMP triggered !");
  1269. LCD_ALERTMESSAGEPGM("Err: MAXTEMP BED");
  1270. }
  1271. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  1272. Stop();
  1273. #endif
  1274. }
  1275. void bed_min_temp_error(void) {
  1276. #ifdef DEBUG_DISABLE_MINTEMP
  1277. return;
  1278. #endif
  1279. //if (current_temperature_ambient < MINTEMP_MINAMBIENT) return;
  1280. #if HEATER_BED_PIN > -1
  1281. WRITE(HEATER_BED_PIN, 0);
  1282. #endif
  1283. if(IsStopped() == false) {
  1284. SERIAL_ERROR_START;
  1285. SERIAL_ERRORLNPGM("Temperature heated bed switched off. MINTEMP triggered !");
  1286. LCD_ALERTMESSAGEPGM("Err: MINTEMP BED");
  1287. }
  1288. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  1289. Stop();
  1290. #endif*/
  1291. }
  1292. #ifdef HEATER_0_USES_MAX6675
  1293. #define MAX6675_HEAT_INTERVAL 250
  1294. long max6675_previous_millis = MAX6675_HEAT_INTERVAL;
  1295. int max6675_temp = 2000;
  1296. int read_max6675()
  1297. {
  1298. if (millis() - max6675_previous_millis < MAX6675_HEAT_INTERVAL)
  1299. return max6675_temp;
  1300. max6675_previous_millis = millis();
  1301. max6675_temp = 0;
  1302. #ifdef PRR
  1303. PRR &= ~(1<<PRSPI);
  1304. #elif defined PRR0
  1305. PRR0 &= ~(1<<PRSPI);
  1306. #endif
  1307. SPCR = (1<<MSTR) | (1<<SPE) | (1<<SPR0);
  1308. // enable TT_MAX6675
  1309. WRITE(MAX6675_SS, 0);
  1310. // ensure 100ns delay - a bit extra is fine
  1311. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1312. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1313. // read MSB
  1314. SPDR = 0;
  1315. for (;(SPSR & (1<<SPIF)) == 0;);
  1316. max6675_temp = SPDR;
  1317. max6675_temp <<= 8;
  1318. // read LSB
  1319. SPDR = 0;
  1320. for (;(SPSR & (1<<SPIF)) == 0;);
  1321. max6675_temp |= SPDR;
  1322. // disable TT_MAX6675
  1323. WRITE(MAX6675_SS, 1);
  1324. if (max6675_temp & 4)
  1325. {
  1326. // thermocouple open
  1327. max6675_temp = 2000;
  1328. }
  1329. else
  1330. {
  1331. max6675_temp = max6675_temp >> 3;
  1332. }
  1333. return max6675_temp;
  1334. }
  1335. #endif
  1336. extern "C" {
  1337. void adc_ready(void) //callback from adc when sampling finished
  1338. {
  1339. current_temperature_raw[0] = adc_values[0];
  1340. current_temperature_bed_raw = adc_values[2];
  1341. current_temperature_raw_pinda = adc_values[3];
  1342. current_voltage_raw_pwr = adc_values[4];
  1343. current_temperature_raw_ambient = adc_values[5];
  1344. current_voltage_raw_bed = adc_values[6];
  1345. temp_meas_ready = true;
  1346. }
  1347. } // extern "C"
  1348. // Timer 0 is shared with millies
  1349. ISR(TIMER0_COMPB_vect)
  1350. {
  1351. static bool _lock = false;
  1352. if (_lock) return;
  1353. _lock = true;
  1354. asm("sei");
  1355. if (!temp_meas_ready) adc_cycle();
  1356. else
  1357. {
  1358. check_max_temp();
  1359. check_min_temp();
  1360. }
  1361. lcd_buttons_update();
  1362. static unsigned char pwm_count = (1 << SOFT_PWM_SCALE);
  1363. static unsigned char soft_pwm_0;
  1364. #ifdef SLOW_PWM_HEATERS
  1365. static unsigned char slow_pwm_count = 0;
  1366. static unsigned char state_heater_0 = 0;
  1367. static unsigned char state_timer_heater_0 = 0;
  1368. #endif
  1369. #if (EXTRUDERS > 1) || defined(HEATERS_PARALLEL)
  1370. static unsigned char soft_pwm_1;
  1371. #ifdef SLOW_PWM_HEATERS
  1372. static unsigned char state_heater_1 = 0;
  1373. static unsigned char state_timer_heater_1 = 0;
  1374. #endif
  1375. #endif
  1376. #if EXTRUDERS > 2
  1377. static unsigned char soft_pwm_2;
  1378. #ifdef SLOW_PWM_HEATERS
  1379. static unsigned char state_heater_2 = 0;
  1380. static unsigned char state_timer_heater_2 = 0;
  1381. #endif
  1382. #endif
  1383. #if HEATER_BED_PIN > -1
  1384. static unsigned char soft_pwm_b;
  1385. #ifdef SLOW_PWM_HEATERS
  1386. static unsigned char state_heater_b = 0;
  1387. static unsigned char state_timer_heater_b = 0;
  1388. #endif
  1389. #endif
  1390. #if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1)
  1391. static unsigned long raw_filwidth_value = 0; //added for filament width sensor
  1392. #endif
  1393. #ifndef SLOW_PWM_HEATERS
  1394. /*
  1395. * standard PWM modulation
  1396. */
  1397. if (pwm_count == 0)
  1398. {
  1399. soft_pwm_0 = soft_pwm[0];
  1400. if(soft_pwm_0 > 0)
  1401. {
  1402. WRITE(HEATER_0_PIN,1);
  1403. #ifdef HEATERS_PARALLEL
  1404. WRITE(HEATER_1_PIN,1);
  1405. #endif
  1406. } else WRITE(HEATER_0_PIN,0);
  1407. #if EXTRUDERS > 1
  1408. soft_pwm_1 = soft_pwm[1];
  1409. if(soft_pwm_1 > 0) WRITE(HEATER_1_PIN,1); else WRITE(HEATER_1_PIN,0);
  1410. #endif
  1411. #if EXTRUDERS > 2
  1412. soft_pwm_2 = soft_pwm[2];
  1413. if(soft_pwm_2 > 0) WRITE(HEATER_2_PIN,1); else WRITE(HEATER_2_PIN,0);
  1414. #endif
  1415. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1416. soft_pwm_b = soft_pwm_bed;
  1417. if(soft_pwm_b > 0) WRITE(HEATER_BED_PIN,1); else WRITE(HEATER_BED_PIN,0);
  1418. #endif
  1419. #ifdef FAN_SOFT_PWM
  1420. soft_pwm_fan = fanSpeedSoftPwm / 2;
  1421. if(soft_pwm_fan > 0) WRITE(FAN_PIN,1); else WRITE(FAN_PIN,0);
  1422. #endif
  1423. }
  1424. if(soft_pwm_0 < pwm_count)
  1425. {
  1426. WRITE(HEATER_0_PIN,0);
  1427. #ifdef HEATERS_PARALLEL
  1428. WRITE(HEATER_1_PIN,0);
  1429. #endif
  1430. }
  1431. #if EXTRUDERS > 1
  1432. if(soft_pwm_1 < pwm_count) WRITE(HEATER_1_PIN,0);
  1433. #endif
  1434. #if EXTRUDERS > 2
  1435. if(soft_pwm_2 < pwm_count) WRITE(HEATER_2_PIN,0);
  1436. #endif
  1437. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1438. if(soft_pwm_b < pwm_count) WRITE(HEATER_BED_PIN,0);
  1439. #endif
  1440. #ifdef FAN_SOFT_PWM
  1441. if(soft_pwm_fan < pwm_count) WRITE(FAN_PIN,0);
  1442. #endif
  1443. pwm_count += (1 << SOFT_PWM_SCALE);
  1444. pwm_count &= 0x7f;
  1445. #else //ifndef SLOW_PWM_HEATERS
  1446. /*
  1447. * SLOW PWM HEATERS
  1448. *
  1449. * for heaters drived by relay
  1450. */
  1451. #ifndef MIN_STATE_TIME
  1452. #define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds
  1453. #endif
  1454. if (slow_pwm_count == 0) {
  1455. // EXTRUDER 0
  1456. soft_pwm_0 = soft_pwm[0];
  1457. if (soft_pwm_0 > 0) {
  1458. // turn ON heather only if the minimum time is up
  1459. if (state_timer_heater_0 == 0) {
  1460. // if change state set timer
  1461. if (state_heater_0 == 0) {
  1462. state_timer_heater_0 = MIN_STATE_TIME;
  1463. }
  1464. state_heater_0 = 1;
  1465. WRITE(HEATER_0_PIN, 1);
  1466. #ifdef HEATERS_PARALLEL
  1467. WRITE(HEATER_1_PIN, 1);
  1468. #endif
  1469. }
  1470. } else {
  1471. // turn OFF heather only if the minimum time is up
  1472. if (state_timer_heater_0 == 0) {
  1473. // if change state set timer
  1474. if (state_heater_0 == 1) {
  1475. state_timer_heater_0 = MIN_STATE_TIME;
  1476. }
  1477. state_heater_0 = 0;
  1478. WRITE(HEATER_0_PIN, 0);
  1479. #ifdef HEATERS_PARALLEL
  1480. WRITE(HEATER_1_PIN, 0);
  1481. #endif
  1482. }
  1483. }
  1484. #if EXTRUDERS > 1
  1485. // EXTRUDER 1
  1486. soft_pwm_1 = soft_pwm[1];
  1487. if (soft_pwm_1 > 0) {
  1488. // turn ON heather only if the minimum time is up
  1489. if (state_timer_heater_1 == 0) {
  1490. // if change state set timer
  1491. if (state_heater_1 == 0) {
  1492. state_timer_heater_1 = MIN_STATE_TIME;
  1493. }
  1494. state_heater_1 = 1;
  1495. WRITE(HEATER_1_PIN, 1);
  1496. }
  1497. } else {
  1498. // turn OFF heather only if the minimum time is up
  1499. if (state_timer_heater_1 == 0) {
  1500. // if change state set timer
  1501. if (state_heater_1 == 1) {
  1502. state_timer_heater_1 = MIN_STATE_TIME;
  1503. }
  1504. state_heater_1 = 0;
  1505. WRITE(HEATER_1_PIN, 0);
  1506. }
  1507. }
  1508. #endif
  1509. #if EXTRUDERS > 2
  1510. // EXTRUDER 2
  1511. soft_pwm_2 = soft_pwm[2];
  1512. if (soft_pwm_2 > 0) {
  1513. // turn ON heather only if the minimum time is up
  1514. if (state_timer_heater_2 == 0) {
  1515. // if change state set timer
  1516. if (state_heater_2 == 0) {
  1517. state_timer_heater_2 = MIN_STATE_TIME;
  1518. }
  1519. state_heater_2 = 1;
  1520. WRITE(HEATER_2_PIN, 1);
  1521. }
  1522. } else {
  1523. // turn OFF heather only if the minimum time is up
  1524. if (state_timer_heater_2 == 0) {
  1525. // if change state set timer
  1526. if (state_heater_2 == 1) {
  1527. state_timer_heater_2 = MIN_STATE_TIME;
  1528. }
  1529. state_heater_2 = 0;
  1530. WRITE(HEATER_2_PIN, 0);
  1531. }
  1532. }
  1533. #endif
  1534. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1535. // BED
  1536. soft_pwm_b = soft_pwm_bed;
  1537. if (soft_pwm_b > 0) {
  1538. // turn ON heather only if the minimum time is up
  1539. if (state_timer_heater_b == 0) {
  1540. // if change state set timer
  1541. if (state_heater_b == 0) {
  1542. state_timer_heater_b = MIN_STATE_TIME;
  1543. }
  1544. state_heater_b = 1;
  1545. WRITE(HEATER_BED_PIN, 1);
  1546. }
  1547. } else {
  1548. // turn OFF heather only if the minimum time is up
  1549. if (state_timer_heater_b == 0) {
  1550. // if change state set timer
  1551. if (state_heater_b == 1) {
  1552. state_timer_heater_b = MIN_STATE_TIME;
  1553. }
  1554. state_heater_b = 0;
  1555. WRITE(HEATER_BED_PIN, 0);
  1556. }
  1557. }
  1558. #endif
  1559. } // if (slow_pwm_count == 0)
  1560. // EXTRUDER 0
  1561. if (soft_pwm_0 < slow_pwm_count) {
  1562. // turn OFF heather only if the minimum time is up
  1563. if (state_timer_heater_0 == 0) {
  1564. // if change state set timer
  1565. if (state_heater_0 == 1) {
  1566. state_timer_heater_0 = MIN_STATE_TIME;
  1567. }
  1568. state_heater_0 = 0;
  1569. WRITE(HEATER_0_PIN, 0);
  1570. #ifdef HEATERS_PARALLEL
  1571. WRITE(HEATER_1_PIN, 0);
  1572. #endif
  1573. }
  1574. }
  1575. #if EXTRUDERS > 1
  1576. // EXTRUDER 1
  1577. if (soft_pwm_1 < slow_pwm_count) {
  1578. // turn OFF heather only if the minimum time is up
  1579. if (state_timer_heater_1 == 0) {
  1580. // if change state set timer
  1581. if (state_heater_1 == 1) {
  1582. state_timer_heater_1 = MIN_STATE_TIME;
  1583. }
  1584. state_heater_1 = 0;
  1585. WRITE(HEATER_1_PIN, 0);
  1586. }
  1587. }
  1588. #endif
  1589. #if EXTRUDERS > 2
  1590. // EXTRUDER 2
  1591. if (soft_pwm_2 < slow_pwm_count) {
  1592. // turn OFF heather only if the minimum time is up
  1593. if (state_timer_heater_2 == 0) {
  1594. // if change state set timer
  1595. if (state_heater_2 == 1) {
  1596. state_timer_heater_2 = MIN_STATE_TIME;
  1597. }
  1598. state_heater_2 = 0;
  1599. WRITE(HEATER_2_PIN, 0);
  1600. }
  1601. }
  1602. #endif
  1603. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1604. // BED
  1605. if (soft_pwm_b < slow_pwm_count) {
  1606. // turn OFF heather only if the minimum time is up
  1607. if (state_timer_heater_b == 0) {
  1608. // if change state set timer
  1609. if (state_heater_b == 1) {
  1610. state_timer_heater_b = MIN_STATE_TIME;
  1611. }
  1612. state_heater_b = 0;
  1613. WRITE(HEATER_BED_PIN, 0);
  1614. }
  1615. }
  1616. #endif
  1617. #ifdef FAN_SOFT_PWM
  1618. if (pwm_count == 0){
  1619. soft_pwm_fan = fanSpeedSoftPwm / 2;
  1620. if (soft_pwm_fan > 0) WRITE(FAN_PIN,1); else WRITE(FAN_PIN,0);
  1621. }
  1622. if (soft_pwm_fan < pwm_count) WRITE(FAN_PIN,0);
  1623. #endif
  1624. pwm_count += (1 << SOFT_PWM_SCALE);
  1625. pwm_count &= 0x7f;
  1626. // increment slow_pwm_count only every 64 pwm_count circa 65.5ms
  1627. if ((pwm_count % 64) == 0) {
  1628. slow_pwm_count++;
  1629. slow_pwm_count &= 0x7f;
  1630. // Extruder 0
  1631. if (state_timer_heater_0 > 0) {
  1632. state_timer_heater_0--;
  1633. }
  1634. #if EXTRUDERS > 1
  1635. // Extruder 1
  1636. if (state_timer_heater_1 > 0)
  1637. state_timer_heater_1--;
  1638. #endif
  1639. #if EXTRUDERS > 2
  1640. // Extruder 2
  1641. if (state_timer_heater_2 > 0)
  1642. state_timer_heater_2--;
  1643. #endif
  1644. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1645. // Bed
  1646. if (state_timer_heater_b > 0)
  1647. state_timer_heater_b--;
  1648. #endif
  1649. } //if ((pwm_count % 64) == 0) {
  1650. #endif //ifndef SLOW_PWM_HEATERS
  1651. #ifdef BABYSTEPPING
  1652. for(uint8_t axis=0;axis<3;axis++)
  1653. {
  1654. int curTodo=babystepsTodo[axis]; //get rid of volatile for performance
  1655. if(curTodo>0)
  1656. {
  1657. asm("cli");
  1658. babystep(axis,/*fwd*/true);
  1659. babystepsTodo[axis]--; //less to do next time
  1660. asm("sei");
  1661. }
  1662. else
  1663. if(curTodo<0)
  1664. {
  1665. asm("cli");
  1666. babystep(axis,/*fwd*/false);
  1667. babystepsTodo[axis]++; //less to do next time
  1668. asm("sei");
  1669. }
  1670. }
  1671. #endif //BABYSTEPPING
  1672. check_fans();
  1673. _lock = false;
  1674. }
  1675. void check_max_temp()
  1676. {
  1677. //heater
  1678. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1679. if (current_temperature_raw[0] <= maxttemp_raw[0]) {
  1680. #else
  1681. if (current_temperature_raw[0] >= maxttemp_raw[0]) {
  1682. #endif
  1683. max_temp_error(0);
  1684. }
  1685. //bed
  1686. #if defined(BED_MAXTEMP) && (TEMP_SENSOR_BED != 0)
  1687. #if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1688. if (current_temperature_bed_raw <= bed_maxttemp_raw) {
  1689. #else
  1690. if (current_temperature_bed_raw >= bed_maxttemp_raw) {
  1691. #endif
  1692. target_temperature_bed = 0;
  1693. bed_max_temp_error();
  1694. }
  1695. #endif
  1696. }
  1697. void check_min_temp_heater0()
  1698. {
  1699. //heater
  1700. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1701. if (current_temperature_raw[0] >= minttemp_raw[0]) {
  1702. #else
  1703. if (current_temperature_raw[0] <= minttemp_raw[0]) {
  1704. #endif
  1705. min_temp_error(0);
  1706. }
  1707. }
  1708. void check_min_temp_bed()
  1709. {
  1710. #if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1711. if (current_temperature_bed_raw >= bed_minttemp_raw) {
  1712. #else
  1713. if (current_temperature_bed_raw <= bed_minttemp_raw) {
  1714. #endif
  1715. bed_min_temp_error();
  1716. }
  1717. }
  1718. void check_min_temp()
  1719. {
  1720. static uint8_t heat_cycles = 0;
  1721. if (current_temperature_raw_ambient > OVERSAMPLENR*MINTEMP_MINAMBIENT_RAW)
  1722. {
  1723. if (READ(HEATER_0_PIN) == HIGH)
  1724. {
  1725. // if ((heat_cycles % 10) == 0)
  1726. // printf_P(PSTR("X%d\n"), heat_cycles);
  1727. if (heat_cycles > 50) //reaction time 5-10s
  1728. check_min_temp_heater0();
  1729. else
  1730. heat_cycles++;
  1731. }
  1732. else
  1733. heat_cycles = 0;
  1734. return;
  1735. }
  1736. check_min_temp_heater0();
  1737. check_min_temp_bed();
  1738. }
  1739. void check_fans() {
  1740. if (READ(TACH_0) != fan_state[0]) {
  1741. fan_edge_counter[0] ++;
  1742. fan_state[0] = !fan_state[0];
  1743. }
  1744. //if (READ(TACH_1) != fan_state[1]) {
  1745. // fan_edge_counter[1] ++;
  1746. // fan_state[1] = !fan_state[1];
  1747. //}
  1748. }
  1749. #ifdef PIDTEMP
  1750. // Apply the scale factors to the PID values
  1751. float scalePID_i(float i)
  1752. {
  1753. return i*PID_dT;
  1754. }
  1755. float unscalePID_i(float i)
  1756. {
  1757. return i/PID_dT;
  1758. }
  1759. float scalePID_d(float d)
  1760. {
  1761. return d/PID_dT;
  1762. }
  1763. float unscalePID_d(float d)
  1764. {
  1765. return d*PID_dT;
  1766. }
  1767. #endif //PIDTEMP