My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

temperature.cpp 36KB

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