My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

temperature.cpp 37KB

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