temperature.cpp 54 KB

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