My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

temperature.cpp 36KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278
  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. //===========================================================================
  28. //=============================public variables============================
  29. //===========================================================================
  30. int target_temperature[EXTRUDERS] = { 0 };
  31. int target_temperature_bed = 0;
  32. int current_temperature_raw[EXTRUDERS] = { 0 };
  33. float current_temperature[EXTRUDERS] = { 0.0 };
  34. int current_temperature_bed_raw = 0;
  35. float current_temperature_bed = 0.0;
  36. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  37. int redundant_temperature_raw = 0;
  38. float redundant_temperature = 0.0;
  39. #endif
  40. #ifdef PIDTEMP
  41. float Kp=DEFAULT_Kp;
  42. float Ki=(DEFAULT_Ki*PID_dT);
  43. float Kd=(DEFAULT_Kd/PID_dT);
  44. #ifdef PID_ADD_EXTRUSION_RATE
  45. float Kc=DEFAULT_Kc;
  46. #endif
  47. #endif //PIDTEMP
  48. #ifdef PIDTEMPBED
  49. float bedKp=DEFAULT_bedKp;
  50. float bedKi=(DEFAULT_bedKi*PID_dT);
  51. float bedKd=(DEFAULT_bedKd/PID_dT);
  52. #endif //PIDTEMPBED
  53. //===========================================================================
  54. //=============================private variables============================
  55. //===========================================================================
  56. static volatile bool temp_meas_ready = false;
  57. #ifdef PIDTEMP
  58. //static cannot be external:
  59. static float temp_iState[EXTRUDERS] = { 0 };
  60. static float temp_dState[EXTRUDERS] = { 0 };
  61. static float pTerm[EXTRUDERS];
  62. static float iTerm[EXTRUDERS];
  63. static float dTerm[EXTRUDERS];
  64. //int output;
  65. static float pid_error[EXTRUDERS];
  66. static float temp_iState_min[EXTRUDERS];
  67. static float temp_iState_max[EXTRUDERS];
  68. // static float pid_input[EXTRUDERS];
  69. // static float pid_output[EXTRUDERS];
  70. static bool pid_reset[EXTRUDERS];
  71. #endif //PIDTEMP
  72. #ifdef PIDTEMPBED
  73. //static cannot be external:
  74. static float temp_iState_bed = { 0 };
  75. static float temp_dState_bed = { 0 };
  76. static float pTerm_bed;
  77. static float iTerm_bed;
  78. static float dTerm_bed;
  79. //int output;
  80. static float pid_error_bed;
  81. static float temp_iState_min_bed;
  82. static float temp_iState_max_bed;
  83. #else //PIDTEMPBED
  84. static unsigned long previous_millis_bed_heater;
  85. #endif //PIDTEMPBED
  86. static unsigned char soft_pwm[EXTRUDERS];
  87. static unsigned char soft_pwm_bed;
  88. #ifdef FAN_SOFT_PWM
  89. static unsigned char soft_pwm_fan;
  90. #endif
  91. #if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
  92. (defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
  93. (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1)
  94. static unsigned long extruder_autofan_last_check;
  95. #endif
  96. #if EXTRUDERS > 3
  97. # error Unsupported number of extruders
  98. #elif EXTRUDERS > 2
  99. # define ARRAY_BY_EXTRUDERS(v1, v2, v3) { v1, v2, v3 }
  100. #elif EXTRUDERS > 1
  101. # define ARRAY_BY_EXTRUDERS(v1, v2, v3) { v1, v2 }
  102. #else
  103. # define ARRAY_BY_EXTRUDERS(v1, v2, v3) { v1 }
  104. #endif
  105. // Init min and max temp with extreme values to prevent false errors during startup
  106. static int minttemp_raw[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_RAW_LO_TEMP , HEATER_1_RAW_LO_TEMP , HEATER_2_RAW_LO_TEMP );
  107. static int maxttemp_raw[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_RAW_HI_TEMP , HEATER_1_RAW_HI_TEMP , HEATER_2_RAW_HI_TEMP );
  108. static int minttemp[EXTRUDERS] = ARRAY_BY_EXTRUDERS( 0, 0, 0 );
  109. static int maxttemp[EXTRUDERS] = ARRAY_BY_EXTRUDERS( 16383, 16383, 16383 );
  110. //static int bed_minttemp_raw = HEATER_BED_RAW_LO_TEMP; /* No bed mintemp error implemented?!? */
  111. #ifdef BED_MAXTEMP
  112. static int bed_maxttemp_raw = HEATER_BED_RAW_HI_TEMP;
  113. #endif
  114. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  115. static void *heater_ttbl_map[2] = {(void *)HEATER_0_TEMPTABLE, (void *)HEATER_1_TEMPTABLE };
  116. static uint8_t heater_ttbllen_map[2] = { HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN };
  117. #else
  118. static void *heater_ttbl_map[EXTRUDERS] = ARRAY_BY_EXTRUDERS( (void *)HEATER_0_TEMPTABLE, (void *)HEATER_1_TEMPTABLE, (void *)HEATER_2_TEMPTABLE );
  119. static uint8_t heater_ttbllen_map[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN, HEATER_2_TEMPTABLE_LEN );
  120. #endif
  121. static float analog2temp(int raw, uint8_t e);
  122. static float analog2tempBed(int raw);
  123. static void updateTemperaturesFromRawValues();
  124. #ifdef WATCH_TEMP_PERIOD
  125. int watch_start_temp[EXTRUDERS] = ARRAY_BY_EXTRUDERS(0,0,0);
  126. unsigned long watchmillis[EXTRUDERS] = ARRAY_BY_EXTRUDERS(0,0,0);
  127. #endif //WATCH_TEMP_PERIOD
  128. //===========================================================================
  129. //============================= functions ============================
  130. //===========================================================================
  131. void PID_autotune(float temp, int extruder, int ncycles)
  132. {
  133. float input = 0.0;
  134. int cycles=0;
  135. bool heating = true;
  136. unsigned long temp_millis = millis();
  137. unsigned long t1=temp_millis;
  138. unsigned long t2=temp_millis;
  139. long t_high = 0;
  140. long t_low = 0;
  141. long bias, d;
  142. float Ku, Tu;
  143. float Kp, Ki, Kd;
  144. float max = 0, min = 10000;
  145. if ((extruder > EXTRUDERS)
  146. #if (TEMP_BED_PIN <= -1)
  147. ||(extruder < 0)
  148. #endif
  149. ){
  150. SERIAL_ECHOLN("PID Autotune failed. Bad extruder number.");
  151. return;
  152. }
  153. SERIAL_ECHOLN("PID Autotune start");
  154. disable_heater(); // switch off all heaters.
  155. if (extruder<0)
  156. {
  157. soft_pwm_bed = (MAX_BED_POWER)/2;
  158. bias = d = (MAX_BED_POWER)/2;
  159. }
  160. else
  161. {
  162. soft_pwm[extruder] = (PID_MAX)/2;
  163. bias = d = (PID_MAX)/2;
  164. }
  165. for(;;) {
  166. if(temp_meas_ready == true) { // temp sample ready
  167. updateTemperaturesFromRawValues();
  168. input = (extruder<0)?current_temperature_bed:current_temperature[extruder];
  169. max=max(max,input);
  170. min=min(min,input);
  171. if(heating == true && input > temp) {
  172. if(millis() - t2 > 5000) {
  173. heating=false;
  174. if (extruder<0)
  175. soft_pwm_bed = (bias - d) >> 1;
  176. else
  177. soft_pwm[extruder] = (bias - d) >> 1;
  178. t1=millis();
  179. t_high=t1 - t2;
  180. max=temp;
  181. }
  182. }
  183. if(heating == false && input < temp) {
  184. if(millis() - t1 > 5000) {
  185. heating=true;
  186. t2=millis();
  187. t_low=t2 - t1;
  188. if(cycles > 0) {
  189. bias += (d*(t_high - t_low))/(t_low + t_high);
  190. bias = constrain(bias, 20 ,(extruder<0?(MAX_BED_POWER):(PID_MAX))-20);
  191. if(bias > (extruder<0?(MAX_BED_POWER):(PID_MAX))/2) d = (extruder<0?(MAX_BED_POWER):(PID_MAX)) - 1 - bias;
  192. else d = bias;
  193. SERIAL_PROTOCOLPGM(" bias: "); SERIAL_PROTOCOL(bias);
  194. SERIAL_PROTOCOLPGM(" d: "); SERIAL_PROTOCOL(d);
  195. SERIAL_PROTOCOLPGM(" min: "); SERIAL_PROTOCOL(min);
  196. SERIAL_PROTOCOLPGM(" max: "); SERIAL_PROTOCOLLN(max);
  197. if(cycles > 2) {
  198. Ku = (4.0*d)/(3.14159*(max-min)/2.0);
  199. Tu = ((float)(t_low + t_high)/1000.0);
  200. SERIAL_PROTOCOLPGM(" Ku: "); SERIAL_PROTOCOL(Ku);
  201. SERIAL_PROTOCOLPGM(" Tu: "); SERIAL_PROTOCOLLN(Tu);
  202. Kp = 0.6*Ku;
  203. Ki = 2*Kp/Tu;
  204. Kd = Kp*Tu/8;
  205. SERIAL_PROTOCOLLNPGM(" Clasic PID ")
  206. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(Kp);
  207. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(Ki);
  208. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(Kd);
  209. /*
  210. Kp = 0.33*Ku;
  211. Ki = Kp/Tu;
  212. Kd = Kp*Tu/3;
  213. SERIAL_PROTOCOLLNPGM(" Some overshoot ")
  214. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(Kp);
  215. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(Ki);
  216. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(Kd);
  217. Kp = 0.2*Ku;
  218. Ki = 2*Kp/Tu;
  219. Kd = Kp*Tu/3;
  220. SERIAL_PROTOCOLLNPGM(" No overshoot ")
  221. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(Kp);
  222. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(Ki);
  223. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(Kd);
  224. */
  225. }
  226. }
  227. if (extruder<0)
  228. soft_pwm_bed = (bias + d) >> 1;
  229. else
  230. soft_pwm[extruder] = (bias + d) >> 1;
  231. cycles++;
  232. min=temp;
  233. }
  234. }
  235. }
  236. if(input > (temp + 20)) {
  237. SERIAL_PROTOCOLLNPGM("PID Autotune failed! Temperature too high");
  238. return;
  239. }
  240. if(millis() - temp_millis > 2000) {
  241. int p;
  242. if (extruder<0){
  243. p=soft_pwm_bed;
  244. SERIAL_PROTOCOLPGM("ok B:");
  245. }else{
  246. p=soft_pwm[extruder];
  247. SERIAL_PROTOCOLPGM("ok T:");
  248. }
  249. SERIAL_PROTOCOL(input);
  250. SERIAL_PROTOCOLPGM(" @:");
  251. SERIAL_PROTOCOLLN(p);
  252. temp_millis = millis();
  253. }
  254. if(((millis() - t1) + (millis() - t2)) > (10L*60L*1000L*2L)) {
  255. SERIAL_PROTOCOLLNPGM("PID Autotune failed! timeout");
  256. return;
  257. }
  258. if(cycles > ncycles) {
  259. SERIAL_PROTOCOLLNPGM("PID Autotune finished! Put the Kp, Ki and Kd constants into Configuration.h");
  260. return;
  261. }
  262. lcd_update();
  263. }
  264. }
  265. void updatePID()
  266. {
  267. #ifdef PIDTEMP
  268. for(int e = 0; e < EXTRUDERS; e++) {
  269. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / Ki;
  270. }
  271. #endif
  272. #ifdef PIDTEMPBED
  273. temp_iState_max_bed = PID_INTEGRAL_DRIVE_MAX / bedKi;
  274. #endif
  275. }
  276. int getHeaterPower(int heater) {
  277. if (heater<0)
  278. return soft_pwm_bed;
  279. return soft_pwm[heater];
  280. }
  281. #if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
  282. (defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
  283. (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1)
  284. #if defined(FAN_PIN) && FAN_PIN > -1
  285. #if EXTRUDER_0_AUTO_FAN_PIN == FAN_PIN
  286. #error "You cannot set EXTRUDER_0_AUTO_FAN_PIN equal to FAN_PIN"
  287. #endif
  288. #if EXTRUDER_1_AUTO_FAN_PIN == FAN_PIN
  289. #error "You cannot set EXTRUDER_1_AUTO_FAN_PIN equal to FAN_PIN"
  290. #endif
  291. #if EXTRUDER_2_AUTO_FAN_PIN == FAN_PIN
  292. #error "You cannot set EXTRUDER_2_AUTO_FAN_PIN equal to FAN_PIN"
  293. #endif
  294. #endif
  295. void setExtruderAutoFanState(int pin, bool state)
  296. {
  297. unsigned char newFanSpeed = (state != 0) ? EXTRUDER_AUTO_FAN_SPEED : 0;
  298. // this idiom allows both digital and PWM fan outputs (see M42 handling).
  299. pinMode(pin, OUTPUT);
  300. digitalWrite(pin, newFanSpeed);
  301. analogWrite(pin, newFanSpeed);
  302. }
  303. void checkExtruderAutoFans()
  304. {
  305. uint8_t fanState = 0;
  306. // which fan pins need to be turned on?
  307. #if defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1
  308. if (current_temperature[0] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  309. fanState |= 1;
  310. #endif
  311. #if defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1
  312. if (current_temperature[1] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  313. {
  314. if (EXTRUDER_1_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN)
  315. fanState |= 1;
  316. else
  317. fanState |= 2;
  318. }
  319. #endif
  320. #if defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1
  321. if (current_temperature[2] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  322. {
  323. if (EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN)
  324. fanState |= 1;
  325. else if (EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_1_AUTO_FAN_PIN)
  326. fanState |= 2;
  327. else
  328. fanState |= 4;
  329. }
  330. #endif
  331. // update extruder auto fan states
  332. #if defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1
  333. setExtruderAutoFanState(EXTRUDER_0_AUTO_FAN_PIN, (fanState & 1) != 0);
  334. #endif
  335. #if defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1
  336. if (EXTRUDER_1_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN)
  337. setExtruderAutoFanState(EXTRUDER_1_AUTO_FAN_PIN, (fanState & 2) != 0);
  338. #endif
  339. #if defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1
  340. if (EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN
  341. && EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_1_AUTO_FAN_PIN)
  342. setExtruderAutoFanState(EXTRUDER_2_AUTO_FAN_PIN, (fanState & 4) != 0);
  343. #endif
  344. }
  345. #endif // any extruder auto fan pins set
  346. void manage_heater()
  347. {
  348. float pid_input;
  349. float pid_output;
  350. if(temp_meas_ready != true) //better readability
  351. return;
  352. updateTemperaturesFromRawValues();
  353. for(int e = 0; e < EXTRUDERS; e++)
  354. {
  355. #ifdef PIDTEMP
  356. pid_input = current_temperature[e];
  357. #ifndef PID_OPENLOOP
  358. pid_error[e] = target_temperature[e] - pid_input;
  359. if(pid_error[e] > PID_FUNCTIONAL_RANGE) {
  360. pid_output = BANG_MAX;
  361. pid_reset[e] = true;
  362. }
  363. else if(pid_error[e] < -PID_FUNCTIONAL_RANGE || target_temperature[e] == 0) {
  364. pid_output = 0;
  365. pid_reset[e] = true;
  366. }
  367. else {
  368. if(pid_reset[e] == true) {
  369. temp_iState[e] = 0.0;
  370. pid_reset[e] = false;
  371. }
  372. pTerm[e] = Kp * pid_error[e];
  373. temp_iState[e] += pid_error[e];
  374. temp_iState[e] = constrain(temp_iState[e], temp_iState_min[e], temp_iState_max[e]);
  375. iTerm[e] = Ki * temp_iState[e];
  376. //K1 defined in Configuration.h in the PID settings
  377. #define K2 (1.0-K1)
  378. dTerm[e] = (Kd * (pid_input - temp_dState[e]))*K2 + (K1 * dTerm[e]);
  379. temp_dState[e] = pid_input;
  380. pid_output = constrain(pTerm[e] + iTerm[e] - dTerm[e], 0, PID_MAX);
  381. }
  382. #else
  383. pid_output = constrain(target_temperature[e], 0, PID_MAX);
  384. #endif //PID_OPENLOOP
  385. #ifdef PID_DEBUG
  386. SERIAL_ECHO_START(" PIDDEBUG ");
  387. SERIAL_ECHO(e);
  388. SERIAL_ECHO(": Input ");
  389. SERIAL_ECHO(pid_input);
  390. SERIAL_ECHO(" Output ");
  391. SERIAL_ECHO(pid_output);
  392. SERIAL_ECHO(" pTerm ");
  393. SERIAL_ECHO(pTerm[e]);
  394. SERIAL_ECHO(" iTerm ");
  395. SERIAL_ECHO(iTerm[e]);
  396. SERIAL_ECHO(" dTerm ");
  397. SERIAL_ECHOLN(dTerm[e]);
  398. #endif //PID_DEBUG
  399. #else /* PID off */
  400. pid_output = 0;
  401. if(current_temperature[e] < target_temperature[e]) {
  402. pid_output = PID_MAX;
  403. }
  404. #endif
  405. // Check if temperature is within the correct range
  406. if((current_temperature[e] > minttemp[e]) && (current_temperature[e] < maxttemp[e]))
  407. {
  408. soft_pwm[e] = (int)pid_output >> 1;
  409. }
  410. else {
  411. soft_pwm[e] = 0;
  412. }
  413. #ifdef WATCH_TEMP_PERIOD
  414. if(watchmillis[e] && millis() - watchmillis[e] > WATCH_TEMP_PERIOD)
  415. {
  416. if(degHotend(e) < watch_start_temp[e] + WATCH_TEMP_INCREASE)
  417. {
  418. setTargetHotend(0, e);
  419. LCD_MESSAGEPGM("Heating failed");
  420. SERIAL_ECHO_START;
  421. SERIAL_ECHOLN("Heating failed");
  422. }else{
  423. watchmillis[e] = 0;
  424. }
  425. }
  426. #endif
  427. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  428. if(fabs(current_temperature[0] - redundant_temperature) > MAX_REDUNDANT_TEMP_SENSOR_DIFF) {
  429. disable_heater();
  430. if(IsStopped() == false) {
  431. SERIAL_ERROR_START;
  432. SERIAL_ERRORLNPGM("Extruder switched off. Temperature difference between temp sensors is too high !");
  433. LCD_ALERTMESSAGEPGM("Err: REDUNDANT TEMP ERROR");
  434. }
  435. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  436. Stop();
  437. #endif
  438. }
  439. #endif
  440. } // End extruder for loop
  441. #if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
  442. (defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
  443. (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1)
  444. if(millis() - extruder_autofan_last_check > 2500) // only need to check fan state very infrequently
  445. {
  446. checkExtruderAutoFans();
  447. extruder_autofan_last_check = millis();
  448. }
  449. #endif
  450. #ifndef PIDTEMPBED
  451. if(millis() - previous_millis_bed_heater < BED_CHECK_INTERVAL)
  452. return;
  453. previous_millis_bed_heater = millis();
  454. #endif
  455. #if TEMP_SENSOR_BED != 0
  456. #ifdef PIDTEMPBED
  457. pid_input = current_temperature_bed;
  458. #ifndef PID_OPENLOOP
  459. pid_error_bed = target_temperature_bed - pid_input;
  460. pTerm_bed = bedKp * pid_error_bed;
  461. temp_iState_bed += pid_error_bed;
  462. temp_iState_bed = constrain(temp_iState_bed, temp_iState_min_bed, temp_iState_max_bed);
  463. iTerm_bed = bedKi * temp_iState_bed;
  464. //K1 defined in Configuration.h in the PID settings
  465. #define K2 (1.0-K1)
  466. dTerm_bed= (bedKd * (pid_input - temp_dState_bed))*K2 + (K1 * dTerm_bed);
  467. temp_dState_bed = pid_input;
  468. pid_output = constrain(pTerm_bed + iTerm_bed - dTerm_bed, 0, MAX_BED_POWER);
  469. #else
  470. pid_output = constrain(target_temperature_bed, 0, MAX_BED_POWER);
  471. #endif //PID_OPENLOOP
  472. if((current_temperature_bed > BED_MINTEMP) && (current_temperature_bed < BED_MAXTEMP))
  473. {
  474. soft_pwm_bed = (int)pid_output >> 1;
  475. }
  476. else {
  477. soft_pwm_bed = 0;
  478. }
  479. #elif !defined(BED_LIMIT_SWITCHING)
  480. // Check if temperature is within the correct range
  481. if((current_temperature_bed > BED_MINTEMP) && (current_temperature_bed < BED_MAXTEMP))
  482. {
  483. if(current_temperature_bed >= target_temperature_bed)
  484. {
  485. soft_pwm_bed = 0;
  486. }
  487. else
  488. {
  489. soft_pwm_bed = MAX_BED_POWER>>1;
  490. }
  491. }
  492. else
  493. {
  494. soft_pwm_bed = 0;
  495. WRITE(HEATER_BED_PIN,LOW);
  496. }
  497. #else //#ifdef BED_LIMIT_SWITCHING
  498. // Check if temperature is within the correct band
  499. if((current_temperature_bed > BED_MINTEMP) && (current_temperature_bed < BED_MAXTEMP))
  500. {
  501. if(current_temperature_bed > target_temperature_bed + BED_HYSTERESIS)
  502. {
  503. soft_pwm_bed = 0;
  504. }
  505. else if(current_temperature_bed <= target_temperature_bed - BED_HYSTERESIS)
  506. {
  507. soft_pwm_bed = MAX_BED_POWER>>1;
  508. }
  509. }
  510. else
  511. {
  512. soft_pwm_bed = 0;
  513. WRITE(HEATER_BED_PIN,LOW);
  514. }
  515. #endif
  516. #endif
  517. }
  518. #define PGM_RD_W(x) (short)pgm_read_word(&x)
  519. // Derived from RepRap FiveD extruder::getTemperature()
  520. // For hot end temperature measurement.
  521. static float analog2temp(int raw, uint8_t e) {
  522. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  523. if(e > EXTRUDERS)
  524. #else
  525. if(e >= EXTRUDERS)
  526. #endif
  527. {
  528. SERIAL_ERROR_START;
  529. SERIAL_ERROR((int)e);
  530. SERIAL_ERRORLNPGM(" - Invalid extruder number !");
  531. kill();
  532. }
  533. #ifdef HEATER_0_USES_MAX6675
  534. if (e == 0)
  535. {
  536. return 0.25 * raw;
  537. }
  538. #endif
  539. if(heater_ttbl_map[e] != NULL)
  540. {
  541. float celsius = 0;
  542. uint8_t i;
  543. short (*tt)[][2] = (short (*)[][2])(heater_ttbl_map[e]);
  544. for (i=1; i<heater_ttbllen_map[e]; i++)
  545. {
  546. if (PGM_RD_W((*tt)[i][0]) > raw)
  547. {
  548. celsius = PGM_RD_W((*tt)[i-1][1]) +
  549. (raw - PGM_RD_W((*tt)[i-1][0])) *
  550. (float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i-1][1])) /
  551. (float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i-1][0]));
  552. break;
  553. }
  554. }
  555. // Overflow: Set to last value in the table
  556. if (i == heater_ttbllen_map[e]) celsius = PGM_RD_W((*tt)[i-1][1]);
  557. return celsius;
  558. }
  559. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  560. }
  561. // Derived from RepRap FiveD extruder::getTemperature()
  562. // For bed temperature measurement.
  563. static float analog2tempBed(int raw) {
  564. #ifdef BED_USES_THERMISTOR
  565. float celsius = 0;
  566. byte i;
  567. for (i=1; i<BEDTEMPTABLE_LEN; i++)
  568. {
  569. if (PGM_RD_W(BEDTEMPTABLE[i][0]) > raw)
  570. {
  571. celsius = PGM_RD_W(BEDTEMPTABLE[i-1][1]) +
  572. (raw - PGM_RD_W(BEDTEMPTABLE[i-1][0])) *
  573. (float)(PGM_RD_W(BEDTEMPTABLE[i][1]) - PGM_RD_W(BEDTEMPTABLE[i-1][1])) /
  574. (float)(PGM_RD_W(BEDTEMPTABLE[i][0]) - PGM_RD_W(BEDTEMPTABLE[i-1][0]));
  575. break;
  576. }
  577. }
  578. // Overflow: Set to last value in the table
  579. if (i == BEDTEMPTABLE_LEN) celsius = PGM_RD_W(BEDTEMPTABLE[i-1][1]);
  580. return celsius;
  581. #elif defined BED_USES_AD595
  582. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  583. #else
  584. return 0;
  585. #endif
  586. }
  587. /* Called to get the raw values into the the actual temperatures. The raw values are created in interrupt context,
  588. and this function is called from normal context as it is too slow to run in interrupts and will block the stepper routine otherwise */
  589. static void updateTemperaturesFromRawValues()
  590. {
  591. for(uint8_t e=0;e<EXTRUDERS;e++)
  592. {
  593. current_temperature[e] = analog2temp(current_temperature_raw[e], e);
  594. }
  595. current_temperature_bed = analog2tempBed(current_temperature_bed_raw);
  596. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  597. redundant_temperature = analog2temp(redundant_temperature_raw, 1);
  598. #endif
  599. //Reset the watchdog after we know we have a temperature measurement.
  600. watchdog_reset();
  601. CRITICAL_SECTION_START;
  602. temp_meas_ready = false;
  603. CRITICAL_SECTION_END;
  604. }
  605. void tp_init()
  606. {
  607. #if (MOTHERBOARD == 80) && ((TEMP_SENSOR_0==-1)||(TEMP_SENSOR_1==-1)||(TEMP_SENSOR_2==-1)||(TEMP_SENSOR_BED==-1))
  608. //disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector
  609. MCUCR=(1<<JTD);
  610. MCUCR=(1<<JTD);
  611. #endif
  612. // Finish init of mult extruder arrays
  613. for(int e = 0; e < EXTRUDERS; e++) {
  614. // populate with the first value
  615. maxttemp[e] = maxttemp[0];
  616. #ifdef PIDTEMP
  617. temp_iState_min[e] = 0.0;
  618. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / Ki;
  619. #endif //PIDTEMP
  620. #ifdef PIDTEMPBED
  621. temp_iState_min_bed = 0.0;
  622. temp_iState_max_bed = PID_INTEGRAL_DRIVE_MAX / bedKi;
  623. #endif //PIDTEMPBED
  624. }
  625. #if defined(HEATER_0_PIN) && (HEATER_0_PIN > -1)
  626. SET_OUTPUT(HEATER_0_PIN);
  627. #endif
  628. #if defined(HEATER_1_PIN) && (HEATER_1_PIN > -1)
  629. SET_OUTPUT(HEATER_1_PIN);
  630. #endif
  631. #if defined(HEATER_2_PIN) && (HEATER_2_PIN > -1)
  632. SET_OUTPUT(HEATER_2_PIN);
  633. #endif
  634. #if defined(HEATER_BED_PIN) && (HEATER_BED_PIN > -1)
  635. SET_OUTPUT(HEATER_BED_PIN);
  636. #endif
  637. #if defined(FAN_PIN) && (FAN_PIN > -1)
  638. SET_OUTPUT(FAN_PIN);
  639. #ifdef FAST_PWM_FAN
  640. setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  641. #endif
  642. #ifdef FAN_SOFT_PWM
  643. soft_pwm_fan=(unsigned char)fanSpeed;
  644. #endif
  645. #endif
  646. #ifdef HEATER_0_USES_MAX6675
  647. #ifndef SDSUPPORT
  648. SET_OUTPUT(MAX_SCK_PIN);
  649. WRITE(MAX_SCK_PIN,0);
  650. SET_OUTPUT(MAX_MOSI_PIN);
  651. WRITE(MAX_MOSI_PIN,1);
  652. SET_INPUT(MAX_MISO_PIN);
  653. WRITE(MAX_MISO_PIN,1);
  654. #endif
  655. SET_OUTPUT(MAX6675_SS);
  656. WRITE(MAX6675_SS,1);
  657. #endif
  658. // Set analog inputs
  659. ADCSRA = 1<<ADEN | 1<<ADSC | 1<<ADIF | 0x07;
  660. DIDR0 = 0;
  661. #ifdef DIDR2
  662. DIDR2 = 0;
  663. #endif
  664. #if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
  665. #if TEMP_0_PIN < 8
  666. DIDR0 |= 1 << TEMP_0_PIN;
  667. #else
  668. DIDR2 |= 1<<(TEMP_0_PIN - 8);
  669. #endif
  670. #endif
  671. #if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1)
  672. #if TEMP_1_PIN < 8
  673. DIDR0 |= 1<<TEMP_1_PIN;
  674. #else
  675. DIDR2 |= 1<<(TEMP_1_PIN - 8);
  676. #endif
  677. #endif
  678. #if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
  679. #if TEMP_2_PIN < 8
  680. DIDR0 |= 1 << TEMP_2_PIN;
  681. #else
  682. DIDR2 |= 1<<(TEMP_2_PIN - 8);
  683. #endif
  684. #endif
  685. #if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
  686. #if TEMP_BED_PIN < 8
  687. DIDR0 |= 1<<TEMP_BED_PIN;
  688. #else
  689. DIDR2 |= 1<<(TEMP_BED_PIN - 8);
  690. #endif
  691. #endif
  692. // Use timer0 for temperature measurement
  693. // Interleave temperature interrupt with millies interrupt
  694. OCR0B = 128;
  695. TIMSK0 |= (1<<OCIE0B);
  696. // Wait for temperature measurement to settle
  697. delay(250);
  698. #ifdef HEATER_0_MINTEMP
  699. minttemp[0] = HEATER_0_MINTEMP;
  700. while(analog2temp(minttemp_raw[0], 0) < HEATER_0_MINTEMP) {
  701. #if HEATER_0_RAW_LO_TEMP < HEATER_0_RAW_HI_TEMP
  702. minttemp_raw[0] += OVERSAMPLENR;
  703. #else
  704. minttemp_raw[0] -= OVERSAMPLENR;
  705. #endif
  706. }
  707. #endif //MINTEMP
  708. #ifdef HEATER_0_MAXTEMP
  709. maxttemp[0] = HEATER_0_MAXTEMP;
  710. while(analog2temp(maxttemp_raw[0], 0) > HEATER_0_MAXTEMP) {
  711. #if HEATER_0_RAW_LO_TEMP < HEATER_0_RAW_HI_TEMP
  712. maxttemp_raw[0] -= OVERSAMPLENR;
  713. #else
  714. maxttemp_raw[0] += OVERSAMPLENR;
  715. #endif
  716. }
  717. #endif //MAXTEMP
  718. #if (EXTRUDERS > 1) && defined(HEATER_1_MINTEMP)
  719. minttemp[1] = HEATER_1_MINTEMP;
  720. while(analog2temp(minttemp_raw[1], 1) < HEATER_1_MINTEMP) {
  721. #if HEATER_1_RAW_LO_TEMP < HEATER_1_RAW_HI_TEMP
  722. minttemp_raw[1] += OVERSAMPLENR;
  723. #else
  724. minttemp_raw[1] -= OVERSAMPLENR;
  725. #endif
  726. }
  727. #endif // MINTEMP 1
  728. #if (EXTRUDERS > 1) && defined(HEATER_1_MAXTEMP)
  729. maxttemp[1] = HEATER_1_MAXTEMP;
  730. while(analog2temp(maxttemp_raw[1], 1) > HEATER_1_MAXTEMP) {
  731. #if HEATER_1_RAW_LO_TEMP < HEATER_1_RAW_HI_TEMP
  732. maxttemp_raw[1] -= OVERSAMPLENR;
  733. #else
  734. maxttemp_raw[1] += OVERSAMPLENR;
  735. #endif
  736. }
  737. #endif //MAXTEMP 1
  738. #if (EXTRUDERS > 2) && defined(HEATER_2_MINTEMP)
  739. minttemp[2] = HEATER_2_MINTEMP;
  740. while(analog2temp(minttemp_raw[2], 2) < HEATER_2_MINTEMP) {
  741. #if HEATER_2_RAW_LO_TEMP < HEATER_2_RAW_HI_TEMP
  742. minttemp_raw[2] += OVERSAMPLENR;
  743. #else
  744. minttemp_raw[2] -= OVERSAMPLENR;
  745. #endif
  746. }
  747. #endif //MINTEMP 2
  748. #if (EXTRUDERS > 2) && defined(HEATER_2_MAXTEMP)
  749. maxttemp[2] = HEATER_2_MAXTEMP;
  750. while(analog2temp(maxttemp_raw[2], 2) > HEATER_2_MAXTEMP) {
  751. #if HEATER_2_RAW_LO_TEMP < HEATER_2_RAW_HI_TEMP
  752. maxttemp_raw[2] -= OVERSAMPLENR;
  753. #else
  754. maxttemp_raw[2] += OVERSAMPLENR;
  755. #endif
  756. }
  757. #endif //MAXTEMP 2
  758. #ifdef BED_MINTEMP
  759. /* No bed MINTEMP error implemented?!? */ /*
  760. while(analog2tempBed(bed_minttemp_raw) < BED_MINTEMP) {
  761. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  762. bed_minttemp_raw += OVERSAMPLENR;
  763. #else
  764. bed_minttemp_raw -= OVERSAMPLENR;
  765. #endif
  766. }
  767. */
  768. #endif //BED_MINTEMP
  769. #ifdef BED_MAXTEMP
  770. while(analog2tempBed(bed_maxttemp_raw) > BED_MAXTEMP) {
  771. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  772. bed_maxttemp_raw -= OVERSAMPLENR;
  773. #else
  774. bed_maxttemp_raw += OVERSAMPLENR;
  775. #endif
  776. }
  777. #endif //BED_MAXTEMP
  778. }
  779. void setWatch()
  780. {
  781. #ifdef WATCH_TEMP_PERIOD
  782. for (int e = 0; e < EXTRUDERS; e++)
  783. {
  784. if(degHotend(e) < degTargetHotend(e) - (WATCH_TEMP_INCREASE * 2))
  785. {
  786. watch_start_temp[e] = degHotend(e);
  787. watchmillis[e] = millis();
  788. }
  789. }
  790. #endif
  791. }
  792. void disable_heater()
  793. {
  794. for(int i=0;i<EXTRUDERS;i++)
  795. setTargetHotend(0,i);
  796. setTargetBed(0);
  797. #if defined(TEMP_0_PIN) && TEMP_0_PIN > -1
  798. target_temperature[0]=0;
  799. soft_pwm[0]=0;
  800. #if defined(HEATER_0_PIN) && HEATER_0_PIN > -1
  801. WRITE(HEATER_0_PIN,LOW);
  802. #endif
  803. #endif
  804. #if defined(TEMP_1_PIN) && TEMP_1_PIN > -1
  805. target_temperature[1]=0;
  806. soft_pwm[1]=0;
  807. #if defined(HEATER_1_PIN) && HEATER_1_PIN > -1
  808. WRITE(HEATER_1_PIN,LOW);
  809. #endif
  810. #endif
  811. #if defined(TEMP_2_PIN) && TEMP_2_PIN > -1
  812. target_temperature[2]=0;
  813. soft_pwm[2]=0;
  814. #if defined(HEATER_2_PIN) && HEATER_2_PIN > -1
  815. WRITE(HEATER_2_PIN,LOW);
  816. #endif
  817. #endif
  818. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  819. target_temperature_bed=0;
  820. soft_pwm_bed=0;
  821. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  822. WRITE(HEATER_BED_PIN,LOW);
  823. #endif
  824. #endif
  825. }
  826. void max_temp_error(uint8_t e) {
  827. disable_heater();
  828. if(IsStopped() == false) {
  829. SERIAL_ERROR_START;
  830. SERIAL_ERRORLN((int)e);
  831. SERIAL_ERRORLNPGM(": Extruder switched off. MAXTEMP triggered !");
  832. LCD_ALERTMESSAGEPGM("Err: MAXTEMP");
  833. }
  834. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  835. Stop();
  836. #endif
  837. }
  838. void min_temp_error(uint8_t e) {
  839. disable_heater();
  840. if(IsStopped() == false) {
  841. SERIAL_ERROR_START;
  842. SERIAL_ERRORLN((int)e);
  843. SERIAL_ERRORLNPGM(": Extruder switched off. MINTEMP triggered !");
  844. LCD_ALERTMESSAGEPGM("Err: MINTEMP");
  845. }
  846. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  847. Stop();
  848. #endif
  849. }
  850. void bed_max_temp_error(void) {
  851. #if HEATER_BED_PIN > -1
  852. WRITE(HEATER_BED_PIN, 0);
  853. #endif
  854. if(IsStopped() == false) {
  855. SERIAL_ERROR_START;
  856. SERIAL_ERRORLNPGM("Temperature heated bed switched off. MAXTEMP triggered !!");
  857. LCD_ALERTMESSAGEPGM("Err: MAXTEMP BED");
  858. }
  859. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  860. Stop();
  861. #endif
  862. }
  863. #ifdef HEATER_0_USES_MAX6675
  864. #define MAX6675_HEAT_INTERVAL 250
  865. long max6675_previous_millis = -HEAT_INTERVAL;
  866. int max6675_temp = 2000;
  867. int read_max6675()
  868. {
  869. if (millis() - max6675_previous_millis < MAX6675_HEAT_INTERVAL)
  870. return max6675_temp;
  871. max6675_previous_millis = millis();
  872. max6675_temp = 0;
  873. #ifdef PRR
  874. PRR &= ~(1<<PRSPI);
  875. #elif defined PRR0
  876. PRR0 &= ~(1<<PRSPI);
  877. #endif
  878. SPCR = (1<<MSTR) | (1<<SPE) | (1<<SPR0);
  879. // enable TT_MAX6675
  880. WRITE(MAX6675_SS, 0);
  881. // ensure 100ns delay - a bit extra is fine
  882. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  883. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  884. // read MSB
  885. SPDR = 0;
  886. for (;(SPSR & (1<<SPIF)) == 0;);
  887. max6675_temp = SPDR;
  888. max6675_temp <<= 8;
  889. // read LSB
  890. SPDR = 0;
  891. for (;(SPSR & (1<<SPIF)) == 0;);
  892. max6675_temp |= SPDR;
  893. // disable TT_MAX6675
  894. WRITE(MAX6675_SS, 1);
  895. if (max6675_temp & 4)
  896. {
  897. // thermocouple open
  898. max6675_temp = 2000;
  899. }
  900. else
  901. {
  902. max6675_temp = max6675_temp >> 3;
  903. }
  904. return max6675_temp;
  905. }
  906. #endif
  907. // Timer 0 is shared with millies
  908. ISR(TIMER0_COMPB_vect)
  909. {
  910. //these variables are only accesible from the ISR, but static, so they don't lose their value
  911. static unsigned char temp_count = 0;
  912. static unsigned long raw_temp_0_value = 0;
  913. static unsigned long raw_temp_1_value = 0;
  914. static unsigned long raw_temp_2_value = 0;
  915. static unsigned long raw_temp_bed_value = 0;
  916. static unsigned char temp_state = 0;
  917. static unsigned char pwm_count = 1;
  918. static unsigned char soft_pwm_0;
  919. #if EXTRUDERS > 1
  920. static unsigned char soft_pwm_1;
  921. #endif
  922. #if EXTRUDERS > 2
  923. static unsigned char soft_pwm_2;
  924. #endif
  925. #if HEATER_BED_PIN > -1
  926. static unsigned char soft_pwm_b;
  927. #endif
  928. if(pwm_count == 0){
  929. soft_pwm_0 = soft_pwm[0];
  930. if(soft_pwm_0 > 0) WRITE(HEATER_0_PIN,1);
  931. #if EXTRUDERS > 1
  932. soft_pwm_1 = soft_pwm[1];
  933. if(soft_pwm_1 > 0) WRITE(HEATER_1_PIN,1);
  934. #endif
  935. #if EXTRUDERS > 2
  936. soft_pwm_2 = soft_pwm[2];
  937. if(soft_pwm_2 > 0) WRITE(HEATER_2_PIN,1);
  938. #endif
  939. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  940. soft_pwm_b = soft_pwm_bed;
  941. if(soft_pwm_b > 0) WRITE(HEATER_BED_PIN,1);
  942. #endif
  943. #ifdef FAN_SOFT_PWM
  944. soft_pwm_fan =(unsigned char) fanSpeed;
  945. if(soft_pwm_fan > 0) WRITE(FAN_PIN,1);
  946. #endif
  947. }
  948. if(soft_pwm_0 <= pwm_count) WRITE(HEATER_0_PIN,0);
  949. #if EXTRUDERS > 1
  950. if(soft_pwm_1 <= pwm_count) WRITE(HEATER_1_PIN,0);
  951. #endif
  952. #if EXTRUDERS > 2
  953. if(soft_pwm_2 <= pwm_count) WRITE(HEATER_2_PIN,0);
  954. #endif
  955. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  956. if(soft_pwm_b <= pwm_count) WRITE(HEATER_BED_PIN,0);
  957. #endif
  958. #ifdef FAN_SOFT_PWM
  959. if(soft_pwm_fan <= pwm_count) WRITE(FAN_PIN,0);
  960. #endif
  961. pwm_count++;
  962. pwm_count &= 0x7f;
  963. switch(temp_state) {
  964. case 0: // Prepare TEMP_0
  965. #if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
  966. #if TEMP_0_PIN > 7
  967. ADCSRB = 1<<MUX5;
  968. #else
  969. ADCSRB = 0;
  970. #endif
  971. ADMUX = ((1 << REFS0) | (TEMP_0_PIN & 0x07));
  972. ADCSRA |= 1<<ADSC; // Start conversion
  973. #endif
  974. lcd_buttons_update();
  975. temp_state = 1;
  976. break;
  977. case 1: // Measure TEMP_0
  978. #if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
  979. raw_temp_0_value += ADC;
  980. #endif
  981. #ifdef HEATER_0_USES_MAX6675 // TODO remove the blocking
  982. raw_temp_0_value = read_max6675();
  983. #endif
  984. temp_state = 2;
  985. break;
  986. case 2: // Prepare TEMP_BED
  987. #if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
  988. #if TEMP_BED_PIN > 7
  989. ADCSRB = 1<<MUX5;
  990. #else
  991. ADCSRB = 0;
  992. #endif
  993. ADMUX = ((1 << REFS0) | (TEMP_BED_PIN & 0x07));
  994. ADCSRA |= 1<<ADSC; // Start conversion
  995. #endif
  996. lcd_buttons_update();
  997. temp_state = 3;
  998. break;
  999. case 3: // Measure TEMP_BED
  1000. #if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
  1001. raw_temp_bed_value += ADC;
  1002. #endif
  1003. temp_state = 4;
  1004. break;
  1005. case 4: // Prepare TEMP_1
  1006. #if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1)
  1007. #if TEMP_1_PIN > 7
  1008. ADCSRB = 1<<MUX5;
  1009. #else
  1010. ADCSRB = 0;
  1011. #endif
  1012. ADMUX = ((1 << REFS0) | (TEMP_1_PIN & 0x07));
  1013. ADCSRA |= 1<<ADSC; // Start conversion
  1014. #endif
  1015. lcd_buttons_update();
  1016. temp_state = 5;
  1017. break;
  1018. case 5: // Measure TEMP_1
  1019. #if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1)
  1020. raw_temp_1_value += ADC;
  1021. #endif
  1022. temp_state = 6;
  1023. break;
  1024. case 6: // Prepare TEMP_2
  1025. #if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
  1026. #if TEMP_2_PIN > 7
  1027. ADCSRB = 1<<MUX5;
  1028. #else
  1029. ADCSRB = 0;
  1030. #endif
  1031. ADMUX = ((1 << REFS0) | (TEMP_2_PIN & 0x07));
  1032. ADCSRA |= 1<<ADSC; // Start conversion
  1033. #endif
  1034. lcd_buttons_update();
  1035. temp_state = 7;
  1036. break;
  1037. case 7: // Measure TEMP_2
  1038. #if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
  1039. raw_temp_2_value += ADC;
  1040. #endif
  1041. temp_state = 0;
  1042. temp_count++;
  1043. break;
  1044. // default:
  1045. // SERIAL_ERROR_START;
  1046. // SERIAL_ERRORLNPGM("Temp measurement error!");
  1047. // break;
  1048. }
  1049. if(temp_count >= 16) // 8 ms * 16 = 128ms.
  1050. {
  1051. if (!temp_meas_ready) //Only update the raw values if they have been read. Else we could be updating them during reading.
  1052. {
  1053. current_temperature_raw[0] = raw_temp_0_value;
  1054. #if EXTRUDERS > 1
  1055. current_temperature_raw[1] = raw_temp_1_value;
  1056. #endif
  1057. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  1058. redundant_temperature_raw = raw_temp_1_value;
  1059. #endif
  1060. #if EXTRUDERS > 2
  1061. current_temperature_raw[2] = raw_temp_2_value;
  1062. #endif
  1063. current_temperature_bed_raw = raw_temp_bed_value;
  1064. }
  1065. temp_meas_ready = true;
  1066. temp_count = 0;
  1067. raw_temp_0_value = 0;
  1068. raw_temp_1_value = 0;
  1069. raw_temp_2_value = 0;
  1070. raw_temp_bed_value = 0;
  1071. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1072. if(current_temperature_raw[0] <= maxttemp_raw[0]) {
  1073. #else
  1074. if(current_temperature_raw[0] >= maxttemp_raw[0]) {
  1075. #endif
  1076. max_temp_error(0);
  1077. }
  1078. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1079. if(current_temperature_raw[0] >= minttemp_raw[0]) {
  1080. #else
  1081. if(current_temperature_raw[0] <= minttemp_raw[0]) {
  1082. #endif
  1083. min_temp_error(0);
  1084. }
  1085. #if EXTRUDERS > 1
  1086. #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
  1087. if(current_temperature_raw[1] <= maxttemp_raw[1]) {
  1088. #else
  1089. if(current_temperature_raw[1] >= maxttemp_raw[1]) {
  1090. #endif
  1091. max_temp_error(1);
  1092. }
  1093. #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
  1094. if(current_temperature_raw[1] >= minttemp_raw[1]) {
  1095. #else
  1096. if(current_temperature_raw[1] <= minttemp_raw[1]) {
  1097. #endif
  1098. min_temp_error(1);
  1099. }
  1100. #endif
  1101. #if EXTRUDERS > 2
  1102. #if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
  1103. if(current_temperature_raw[2] <= maxttemp_raw[2]) {
  1104. #else
  1105. if(current_temperature_raw[2] >= maxttemp_raw[2]) {
  1106. #endif
  1107. max_temp_error(2);
  1108. }
  1109. #if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
  1110. if(current_temperature_raw[2] >= minttemp_raw[2]) {
  1111. #else
  1112. if(current_temperature_raw[2] <= minttemp_raw[2]) {
  1113. #endif
  1114. min_temp_error(2);
  1115. }
  1116. #endif
  1117. /* No bed MINTEMP error? */
  1118. #if defined(BED_MAXTEMP) && (TEMP_SENSOR_BED != 0)
  1119. # if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1120. if(current_temperature_bed_raw <= bed_maxttemp_raw) {
  1121. #else
  1122. if(current_temperature_bed_raw >= bed_maxttemp_raw) {
  1123. #endif
  1124. target_temperature_bed = 0;
  1125. bed_max_temp_error();
  1126. }
  1127. #endif
  1128. }
  1129. }
  1130. #ifdef PIDTEMP
  1131. // Apply the scale factors to the PID values
  1132. float scalePID_i(float i)
  1133. {
  1134. return i*PID_dT;
  1135. }
  1136. float unscalePID_i(float i)
  1137. {
  1138. return i/PID_dT;
  1139. }
  1140. float scalePID_d(float d)
  1141. {
  1142. return d/PID_dT;
  1143. }
  1144. float unscalePID_d(float d)
  1145. {
  1146. return d*PID_dT;
  1147. }
  1148. #endif //PIDTEMP