My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

temperature.cpp 51KB

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