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 50KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (C) 2016 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (C) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * temperature.cpp - temperature control
  24. */
  25. #include "Marlin.h"
  26. #include "ultralcd.h"
  27. #include "temperature.h"
  28. #include "language.h"
  29. #include "Sd2PinMap.h"
  30. #if ENABLED(USE_WATCHDOG)
  31. #include "watchdog.h"
  32. #endif
  33. #ifdef K1 // Defined in Configuration.h in the PID settings
  34. #define K2 (1.0-K1)
  35. #endif
  36. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  37. static void* heater_ttbl_map[2] = {(void*)HEATER_0_TEMPTABLE, (void*)HEATER_1_TEMPTABLE };
  38. static uint8_t heater_ttbllen_map[2] = { HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN };
  39. #else
  40. static void* heater_ttbl_map[EXTRUDERS] = ARRAY_BY_EXTRUDERS((void*)HEATER_0_TEMPTABLE, (void*)HEATER_1_TEMPTABLE, (void*)HEATER_2_TEMPTABLE, (void*)HEATER_3_TEMPTABLE);
  41. static uint8_t heater_ttbllen_map[EXTRUDERS] = ARRAY_BY_EXTRUDERS(HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN, HEATER_2_TEMPTABLE_LEN, HEATER_3_TEMPTABLE_LEN);
  42. #endif
  43. Temperature thermalManager;
  44. #if HAS_PID_HEATING
  45. void Temperature::PID_autotune(float temp, int extruder, int ncycles, bool set_result/*=false*/) {
  46. float input = 0.0;
  47. int cycles = 0;
  48. bool heating = true;
  49. millis_t temp_ms = millis(), t1 = temp_ms, t2 = temp_ms;
  50. long t_high = 0, t_low = 0;
  51. long bias, d;
  52. float Ku, Tu;
  53. float workKp = 0, workKi = 0, workKd = 0;
  54. float max = 0, min = 10000;
  55. #if HAS_AUTO_FAN
  56. next_auto_fan_check_ms = temp_ms + 2500UL;
  57. #endif
  58. if (false
  59. #if ENABLED(PIDTEMP)
  60. || extruder >= EXTRUDERS
  61. #else
  62. || extruder >= 0
  63. #endif
  64. #if DISABLED(PIDTEMPBED)
  65. || extruder < 0
  66. #endif
  67. ) {
  68. SERIAL_ECHOLN(MSG_PID_BAD_EXTRUDER_NUM);
  69. return;
  70. }
  71. SERIAL_ECHOLN(MSG_PID_AUTOTUNE_START);
  72. disable_all_heaters(); // switch off all heaters.
  73. #if HAS_PID_FOR_BOTH
  74. if (extruder < 0)
  75. soft_pwm_bed = bias = d = (MAX_BED_POWER) / 2;
  76. else
  77. soft_pwm[extruder] = bias = d = (PID_MAX) / 2;
  78. #elif ENABLED(PIDTEMP)
  79. soft_pwm[extruder] = bias = d = (PID_MAX) / 2;
  80. #else
  81. soft_pwm_bed = bias = d = (MAX_BED_POWER) / 2;
  82. #endif
  83. // PID Tuning loop
  84. for (;;) {
  85. millis_t ms = millis();
  86. if (temp_meas_ready) { // temp sample ready
  87. updateTemperaturesFromRawValues();
  88. input =
  89. #if HAS_PID_FOR_BOTH
  90. extruder < 0 ? current_temperature_bed : current_temperature[extruder]
  91. #elif ENABLED(PIDTEMP)
  92. current_temperature[extruder]
  93. #else
  94. current_temperature_bed
  95. #endif
  96. ;
  97. max = max(max, input);
  98. min = min(min, input);
  99. #if HAS_AUTO_FAN
  100. if (ELAPSED(ms, next_auto_fan_check_ms)) {
  101. checkExtruderAutoFans();
  102. next_auto_fan_check_ms = ms + 2500UL;
  103. }
  104. #endif
  105. if (heating && input > temp) {
  106. if (ELAPSED(ms, t2 + 5000UL)) {
  107. heating = false;
  108. #if HAS_PID_FOR_BOTH
  109. if (extruder < 0)
  110. soft_pwm_bed = (bias - d) >> 1;
  111. else
  112. soft_pwm[extruder] = (bias - d) >> 1;
  113. #elif ENABLED(PIDTEMP)
  114. soft_pwm[extruder] = (bias - d) >> 1;
  115. #elif ENABLED(PIDTEMPBED)
  116. soft_pwm_bed = (bias - d) >> 1;
  117. #endif
  118. t1 = ms;
  119. t_high = t1 - t2;
  120. max = temp;
  121. }
  122. }
  123. if (!heating && input < temp) {
  124. if (ELAPSED(ms, t1 + 5000UL)) {
  125. heating = true;
  126. t2 = ms;
  127. t_low = t2 - t1;
  128. if (cycles > 0) {
  129. long max_pow =
  130. #if HAS_PID_FOR_BOTH
  131. extruder < 0 ? MAX_BED_POWER : PID_MAX
  132. #elif ENABLED(PIDTEMP)
  133. PID_MAX
  134. #else
  135. MAX_BED_POWER
  136. #endif
  137. ;
  138. bias += (d * (t_high - t_low)) / (t_low + t_high);
  139. bias = constrain(bias, 20, max_pow - 20);
  140. d = (bias > max_pow / 2) ? max_pow - 1 - bias : bias;
  141. SERIAL_PROTOCOLPGM(MSG_BIAS); SERIAL_PROTOCOL(bias);
  142. SERIAL_PROTOCOLPGM(MSG_D); SERIAL_PROTOCOL(d);
  143. SERIAL_PROTOCOLPGM(MSG_T_MIN); SERIAL_PROTOCOL(min);
  144. SERIAL_PROTOCOLPGM(MSG_T_MAX); SERIAL_PROTOCOLLN(max);
  145. if (cycles > 2) {
  146. Ku = (4.0 * d) / (3.14159265 * (max - min) / 2.0);
  147. Tu = ((float)(t_low + t_high) / 1000.0);
  148. SERIAL_PROTOCOLPGM(MSG_KU); SERIAL_PROTOCOL(Ku);
  149. SERIAL_PROTOCOLPGM(MSG_TU); SERIAL_PROTOCOLLN(Tu);
  150. workKp = 0.6 * Ku;
  151. workKi = 2 * workKp / Tu;
  152. workKd = workKp * Tu / 8;
  153. SERIAL_PROTOCOLLNPGM(MSG_CLASSIC_PID);
  154. SERIAL_PROTOCOLPGM(MSG_KP); SERIAL_PROTOCOLLN(workKp);
  155. SERIAL_PROTOCOLPGM(MSG_KI); SERIAL_PROTOCOLLN(workKi);
  156. SERIAL_PROTOCOLPGM(MSG_KD); SERIAL_PROTOCOLLN(workKd);
  157. /**
  158. workKp = 0.33*Ku;
  159. workKi = workKp/Tu;
  160. workKd = workKp*Tu/3;
  161. SERIAL_PROTOCOLLNPGM(" Some overshoot ");
  162. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(workKp);
  163. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(workKi);
  164. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(workKd);
  165. workKp = 0.2*Ku;
  166. workKi = 2*workKp/Tu;
  167. workKd = workKp*Tu/3;
  168. SERIAL_PROTOCOLLNPGM(" No overshoot ");
  169. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(workKp);
  170. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(workKi);
  171. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(workKd);
  172. */
  173. }
  174. }
  175. #if HAS_PID_FOR_BOTH
  176. if (extruder < 0)
  177. soft_pwm_bed = (bias + d) >> 1;
  178. else
  179. soft_pwm[extruder] = (bias + d) >> 1;
  180. #elif ENABLED(PIDTEMP)
  181. soft_pwm[extruder] = (bias + d) >> 1;
  182. #else
  183. soft_pwm_bed = (bias + d) >> 1;
  184. #endif
  185. cycles++;
  186. min = temp;
  187. }
  188. }
  189. }
  190. #define MAX_OVERSHOOT_PID_AUTOTUNE 20
  191. if (input > temp + MAX_OVERSHOOT_PID_AUTOTUNE) {
  192. SERIAL_PROTOCOLLNPGM(MSG_PID_TEMP_TOO_HIGH);
  193. return;
  194. }
  195. // Every 2 seconds...
  196. if (ELAPSED(ms, temp_ms + 2000UL)) {
  197. #if HAS_TEMP_HOTEND || HAS_TEMP_BED
  198. print_heaterstates();
  199. SERIAL_EOL;
  200. #endif
  201. temp_ms = ms;
  202. } // every 2 seconds
  203. // Over 2 minutes?
  204. if (((ms - t1) + (ms - t2)) > (10L * 60L * 1000L * 2L)) {
  205. SERIAL_PROTOCOLLNPGM(MSG_PID_TIMEOUT);
  206. return;
  207. }
  208. if (cycles > ncycles) {
  209. SERIAL_PROTOCOLLNPGM(MSG_PID_AUTOTUNE_FINISHED);
  210. #if HAS_PID_FOR_BOTH
  211. const char* estring = extruder < 0 ? "bed" : "";
  212. SERIAL_PROTOCOLPGM("#define DEFAULT_"); SERIAL_PROTOCOL(estring); SERIAL_PROTOCOLPGM("Kp "); SERIAL_PROTOCOLLN(workKp);
  213. SERIAL_PROTOCOLPGM("#define DEFAULT_"); SERIAL_PROTOCOL(estring); SERIAL_PROTOCOLPGM("Ki "); SERIAL_PROTOCOLLN(workKi);
  214. SERIAL_PROTOCOLPGM("#define DEFAULT_"); SERIAL_PROTOCOL(estring); SERIAL_PROTOCOLPGM("Kd "); SERIAL_PROTOCOLLN(workKd);
  215. #elif ENABLED(PIDTEMP)
  216. SERIAL_PROTOCOLPGM("#define DEFAULT_Kp "); SERIAL_PROTOCOLLN(workKp);
  217. SERIAL_PROTOCOLPGM("#define DEFAULT_Ki "); SERIAL_PROTOCOLLN(workKi);
  218. SERIAL_PROTOCOLPGM("#define DEFAULT_Kd "); SERIAL_PROTOCOLLN(workKd);
  219. #else
  220. SERIAL_PROTOCOLPGM("#define DEFAULT_bedKp "); SERIAL_PROTOCOLLN(workKp);
  221. SERIAL_PROTOCOLPGM("#define DEFAULT_bedKi "); SERIAL_PROTOCOLLN(workKi);
  222. SERIAL_PROTOCOLPGM("#define DEFAULT_bedKd "); SERIAL_PROTOCOLLN(workKd);
  223. #endif
  224. #define _SET_BED_PID() \
  225. bedKp = workKp; \
  226. bedKi = scalePID_i(workKi); \
  227. bedKd = scalePID_d(workKd); \
  228. updatePID()
  229. #define _SET_EXTRUDER_PID() \
  230. PID_PARAM(Kp, extruder) = workKp; \
  231. PID_PARAM(Ki, extruder) = scalePID_i(workKi); \
  232. PID_PARAM(Kd, extruder) = scalePID_d(workKd); \
  233. updatePID()
  234. // Use the result? (As with "M303 U1")
  235. if (set_result) {
  236. #if HAS_PID_FOR_BOTH
  237. if (extruder < 0) {
  238. _SET_BED_PID();
  239. }
  240. else {
  241. _SET_EXTRUDER_PID();
  242. }
  243. #elif ENABLED(PIDTEMP)
  244. _SET_EXTRUDER_PID();
  245. #else
  246. _SET_BED_PID();
  247. #endif
  248. }
  249. return;
  250. }
  251. lcd_update();
  252. }
  253. }
  254. #endif // HAS_PID_HEATING
  255. #if ENABLED(PIDTEMP)
  256. #if ENABLED(PID_PARAMS_PER_EXTRUDER)
  257. float Temperature::Kp[EXTRUDERS] = ARRAY_BY_EXTRUDERS1(DEFAULT_Kp),
  258. Temperature::Ki[EXTRUDERS] = ARRAY_BY_EXTRUDERS1((DEFAULT_Ki) * (PID_dT)),
  259. Temperature::Kd[EXTRUDERS] = ARRAY_BY_EXTRUDERS1((DEFAULT_Kd) / (PID_dT));
  260. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  261. float Temperature::Kc[EXTRUDERS] = ARRAY_BY_EXTRUDERS1(DEFAULT_Kc);
  262. #endif
  263. #else
  264. float Temperature::Kp = DEFAULT_Kp,
  265. Temperature::Ki = (DEFAULT_Ki) * (PID_dT),
  266. Temperature::Kd = (DEFAULT_Kd) / (PID_dT);
  267. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  268. float Temperature::Kc = DEFAULT_Kc;
  269. #endif
  270. #endif
  271. #endif
  272. Temperature::Temperature() { }
  273. void Temperature::updatePID() {
  274. #if ENABLED(PIDTEMP)
  275. for (int e = 0; e < EXTRUDERS; e++) {
  276. temp_iState_max[e] = (PID_INTEGRAL_DRIVE_MAX) / PID_PARAM(Ki, e);
  277. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  278. last_position[e] = 0;
  279. #endif
  280. }
  281. #endif
  282. #if ENABLED(PIDTEMPBED)
  283. temp_iState_max_bed = (PID_BED_INTEGRAL_DRIVE_MAX) / bedKi;
  284. #endif
  285. }
  286. int Temperature::getHeaterPower(int heater) {
  287. return heater < 0 ? soft_pwm_bed : soft_pwm[heater];
  288. }
  289. #if HAS_AUTO_FAN
  290. void Temperature::checkExtruderAutoFans() {
  291. const int8_t fanPin[] = { EXTRUDER_0_AUTO_FAN_PIN, EXTRUDER_1_AUTO_FAN_PIN, EXTRUDER_2_AUTO_FAN_PIN, EXTRUDER_3_AUTO_FAN_PIN };
  292. const int fanBit[] = { 0,
  293. EXTRUDER_1_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN ? 0 : 1,
  294. EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN ? 0 :
  295. EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_1_AUTO_FAN_PIN ? 1 : 2,
  296. EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN ? 0 :
  297. EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_1_AUTO_FAN_PIN ? 1 :
  298. EXTRUDER_3_AUTO_FAN_PIN == EXTRUDER_2_AUTO_FAN_PIN ? 2 : 3
  299. };
  300. uint8_t fanState = 0;
  301. for (int f = 0; f <= EXTRUDERS; f++) {
  302. if (current_temperature[f] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  303. SBI(fanState, fanBit[f]);
  304. }
  305. uint8_t fanDone = 0;
  306. for (int f = 0; f <= 3; f++) {
  307. int8_t pin = fanPin[f];
  308. if (pin >= 0 && !TEST(fanDone, fanBit[f])) {
  309. unsigned char newFanSpeed = TEST(fanState, fanBit[f]) ? EXTRUDER_AUTO_FAN_SPEED : 0;
  310. // this idiom allows both digital and PWM fan outputs (see M42 handling).
  311. digitalWrite(pin, newFanSpeed);
  312. analogWrite(pin, newFanSpeed);
  313. SBI(fanDone, fanBit[f]);
  314. }
  315. }
  316. }
  317. #endif // HAS_AUTO_FAN
  318. //
  319. // Temperature Error Handlers
  320. //
  321. void Temperature::_temp_error(int e, const char* serial_msg, const char* lcd_msg) {
  322. static bool killed = false;
  323. if (IsRunning()) {
  324. SERIAL_ERROR_START;
  325. serialprintPGM(serial_msg);
  326. SERIAL_ERRORPGM(MSG_STOPPED_HEATER);
  327. if (e >= 0) SERIAL_ERRORLN((int)e); else SERIAL_ERRORLNPGM(MSG_HEATER_BED);
  328. }
  329. #if DISABLED(BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE)
  330. if (!killed) {
  331. Running = false;
  332. killed = true;
  333. kill(lcd_msg);
  334. }
  335. else
  336. disable_all_heaters(); // paranoia
  337. #endif
  338. }
  339. void Temperature::max_temp_error(uint8_t e) {
  340. _temp_error(e, PSTR(MSG_T_MAXTEMP), PSTR(MSG_ERR_MAXTEMP));
  341. }
  342. void Temperature::min_temp_error(uint8_t e) {
  343. _temp_error(e, PSTR(MSG_T_MINTEMP), PSTR(MSG_ERR_MINTEMP));
  344. }
  345. float Temperature::get_pid_output(int e) {
  346. float pid_output;
  347. #if ENABLED(PIDTEMP)
  348. #if DISABLED(PID_OPENLOOP)
  349. pid_error[e] = target_temperature[e] - current_temperature[e];
  350. dTerm[e] = K2 * PID_PARAM(Kd, e) * (current_temperature[e] - temp_dState[e]) + K1 * dTerm[e];
  351. temp_dState[e] = current_temperature[e];
  352. if (pid_error[e] > PID_FUNCTIONAL_RANGE) {
  353. pid_output = BANG_MAX;
  354. pid_reset[e] = true;
  355. }
  356. else if (pid_error[e] < -(PID_FUNCTIONAL_RANGE) || target_temperature[e] == 0) {
  357. pid_output = 0;
  358. pid_reset[e] = true;
  359. }
  360. else {
  361. if (pid_reset[e]) {
  362. temp_iState[e] = 0.0;
  363. pid_reset[e] = false;
  364. }
  365. pTerm[e] = PID_PARAM(Kp, e) * pid_error[e];
  366. temp_iState[e] += pid_error[e];
  367. temp_iState[e] = constrain(temp_iState[e], temp_iState_min[e], temp_iState_max[e]);
  368. iTerm[e] = PID_PARAM(Ki, e) * temp_iState[e];
  369. pid_output = pTerm[e] + iTerm[e] - dTerm[e];
  370. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  371. cTerm[e] = 0;
  372. if (e == active_extruder) {
  373. long e_position = stepper.position(E_AXIS);
  374. if (e_position > last_position[e]) {
  375. lpq[lpq_ptr++] = e_position - last_position[e];
  376. last_position[e] = e_position;
  377. }
  378. else {
  379. lpq[lpq_ptr++] = 0;
  380. }
  381. if (lpq_ptr >= lpq_len) lpq_ptr = 0;
  382. cTerm[e] = (lpq[lpq_ptr] / planner.axis_steps_per_unit[E_AXIS]) * PID_PARAM(Kc, e);
  383. pid_output += cTerm[e];
  384. }
  385. #endif //PID_ADD_EXTRUSION_RATE
  386. if (pid_output > PID_MAX) {
  387. if (pid_error[e] > 0) temp_iState[e] -= pid_error[e]; // conditional un-integration
  388. pid_output = PID_MAX;
  389. }
  390. else if (pid_output < 0) {
  391. if (pid_error[e] < 0) temp_iState[e] -= pid_error[e]; // conditional un-integration
  392. pid_output = 0;
  393. }
  394. }
  395. #else
  396. pid_output = constrain(target_temperature[e], 0, PID_MAX);
  397. #endif //PID_OPENLOOP
  398. #if ENABLED(PID_DEBUG)
  399. SERIAL_ECHO_START;
  400. SERIAL_ECHOPAIR(MSG_PID_DEBUG, e);
  401. SERIAL_ECHOPAIR(MSG_PID_DEBUG_INPUT, current_temperature[e]);
  402. SERIAL_ECHOPAIR(MSG_PID_DEBUG_OUTPUT, pid_output);
  403. SERIAL_ECHOPAIR(MSG_PID_DEBUG_PTERM, pTerm[e]);
  404. SERIAL_ECHOPAIR(MSG_PID_DEBUG_ITERM, iTerm[e]);
  405. SERIAL_ECHOPAIR(MSG_PID_DEBUG_DTERM, dTerm[e]);
  406. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  407. SERIAL_ECHOPAIR(MSG_PID_DEBUG_CTERM, cTerm[e]);
  408. #endif
  409. SERIAL_EOL;
  410. #endif //PID_DEBUG
  411. #else /* PID off */
  412. pid_output = (current_temperature[e] < target_temperature[e]) ? PID_MAX : 0;
  413. #endif
  414. return pid_output;
  415. }
  416. #if ENABLED(PIDTEMPBED)
  417. float Temperature::get_pid_output_bed() {
  418. float pid_output;
  419. #if DISABLED(PID_OPENLOOP)
  420. pid_error_bed = target_temperature_bed - current_temperature_bed;
  421. pTerm_bed = bedKp * pid_error_bed;
  422. temp_iState_bed += pid_error_bed;
  423. temp_iState_bed = constrain(temp_iState_bed, temp_iState_min_bed, temp_iState_max_bed);
  424. iTerm_bed = bedKi * temp_iState_bed;
  425. dTerm_bed = K2 * bedKd * (current_temperature_bed - temp_dState_bed) + K1 * dTerm_bed;
  426. temp_dState_bed = current_temperature_bed;
  427. pid_output = pTerm_bed + iTerm_bed - dTerm_bed;
  428. if (pid_output > MAX_BED_POWER) {
  429. if (pid_error_bed > 0) temp_iState_bed -= pid_error_bed; // conditional un-integration
  430. pid_output = MAX_BED_POWER;
  431. }
  432. else if (pid_output < 0) {
  433. if (pid_error_bed < 0) temp_iState_bed -= pid_error_bed; // conditional un-integration
  434. pid_output = 0;
  435. }
  436. #else
  437. pid_output = constrain(target_temperature_bed, 0, MAX_BED_POWER);
  438. #endif // PID_OPENLOOP
  439. #if ENABLED(PID_BED_DEBUG)
  440. SERIAL_ECHO_START;
  441. SERIAL_ECHO(" PID_BED_DEBUG ");
  442. SERIAL_ECHO(": Input ");
  443. SERIAL_ECHO(current_temperature_bed);
  444. SERIAL_ECHO(" Output ");
  445. SERIAL_ECHO(pid_output);
  446. SERIAL_ECHO(" pTerm ");
  447. SERIAL_ECHO(pTerm_bed);
  448. SERIAL_ECHO(" iTerm ");
  449. SERIAL_ECHO(iTerm_bed);
  450. SERIAL_ECHO(" dTerm ");
  451. SERIAL_ECHOLN(dTerm_bed);
  452. #endif //PID_BED_DEBUG
  453. return pid_output;
  454. }
  455. #endif //PIDTEMPBED
  456. /**
  457. * Manage heating activities for extruder hot-ends and a heated bed
  458. * - Acquire updated temperature readings
  459. * - Invoke thermal runaway protection
  460. * - Manage extruder auto-fan
  461. * - Apply filament width to the extrusion rate (may move)
  462. * - Update the heated bed PID output value
  463. */
  464. void Temperature::manage_heater() {
  465. if (!temp_meas_ready) return;
  466. updateTemperaturesFromRawValues();
  467. #if ENABLED(HEATER_0_USES_MAX6675)
  468. float ct = current_temperature[0];
  469. if (ct > min(HEATER_0_MAXTEMP, 1023)) max_temp_error(0);
  470. if (ct < max(HEATER_0_MINTEMP, 0.01)) min_temp_error(0);
  471. #endif
  472. #if (ENABLED(THERMAL_PROTECTION_HOTENDS) && WATCH_TEMP_PERIOD > 0) || (ENABLED(THERMAL_PROTECTION_BED) && WATCH_BED_TEMP_PERIOD > 0) || DISABLED(PIDTEMPBED) || HAS_AUTO_FAN
  473. millis_t ms = millis();
  474. #endif
  475. // Loop through all extruders
  476. for (int e = 0; e < EXTRUDERS; e++) {
  477. #if ENABLED(THERMAL_PROTECTION_HOTENDS)
  478. thermal_runaway_protection(&thermal_runaway_state_machine[e], &thermal_runaway_timer[e], current_temperature[e], target_temperature[e], e, THERMAL_PROTECTION_PERIOD, THERMAL_PROTECTION_HYSTERESIS);
  479. #endif
  480. float pid_output = get_pid_output(e);
  481. // Check if temperature is within the correct range
  482. soft_pwm[e] = current_temperature[e] > minttemp[e] && current_temperature[e] < maxttemp[e] ? (int)pid_output >> 1 : 0;
  483. // Check if the temperature is failing to increase
  484. #if ENABLED(THERMAL_PROTECTION_HOTENDS) && WATCH_TEMP_PERIOD > 0
  485. // Is it time to check this extruder's heater?
  486. if (watch_heater_next_ms[e] && ELAPSED(ms, watch_heater_next_ms[e])) {
  487. // Has it failed to increase enough?
  488. if (degHotend(e) < watch_target_temp[e]) {
  489. // Stop!
  490. _temp_error(e, PSTR(MSG_T_HEATING_FAILED), PSTR(MSG_HEATING_FAILED_LCD));
  491. }
  492. else {
  493. // Start again if the target is still far off
  494. start_watching_heater(e);
  495. }
  496. }
  497. #endif // THERMAL_PROTECTION_HOTENDS
  498. // Check if the temperature is failing to increase
  499. #if ENABLED(THERMAL_PROTECTION_BED) && WATCH_BED_TEMP_PERIOD > 0
  500. // Is it time to check the bed?
  501. if (watch_bed_next_ms && ELAPSED(ms, watch_bed_next_ms)) {
  502. // Has it failed to increase enough?
  503. if (degBed() < watch_target_bed_temp) {
  504. // Stop!
  505. _temp_error(-1, PSTR(MSG_T_HEATING_FAILED), PSTR(MSG_HEATING_FAILED_LCD));
  506. }
  507. else {
  508. // Start again if the target is still far off
  509. start_watching_bed();
  510. }
  511. }
  512. #endif // THERMAL_PROTECTION_HOTENDS
  513. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  514. if (fabs(current_temperature[0] - redundant_temperature) > MAX_REDUNDANT_TEMP_SENSOR_DIFF) {
  515. _temp_error(0, PSTR(MSG_REDUNDANCY), PSTR(MSG_ERR_REDUNDANT_TEMP));
  516. }
  517. #endif
  518. } // Extruders Loop
  519. #if HAS_AUTO_FAN
  520. if (ELAPSED(ms, next_auto_fan_check_ms)) { // only need to check fan state very infrequently
  521. checkExtruderAutoFans();
  522. next_auto_fan_check_ms = ms + 2500UL;
  523. }
  524. #endif
  525. // Control the extruder rate based on the width sensor
  526. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  527. if (filament_sensor) {
  528. meas_shift_index = filwidth_delay_index1 - meas_delay_cm;
  529. if (meas_shift_index < 0) meas_shift_index += MAX_MEASUREMENT_DELAY + 1; //loop around buffer if needed
  530. // Get the delayed info and add 100 to reconstitute to a percent of
  531. // the nominal filament diameter then square it to get an area
  532. meas_shift_index = constrain(meas_shift_index, 0, MAX_MEASUREMENT_DELAY);
  533. float vm = pow((measurement_delay[meas_shift_index] + 100.0) / 100.0, 2);
  534. NOLESS(vm, 0.01);
  535. volumetric_multiplier[FILAMENT_SENSOR_EXTRUDER_NUM] = vm;
  536. }
  537. #endif //FILAMENT_WIDTH_SENSOR
  538. #if DISABLED(PIDTEMPBED)
  539. if (PENDING(ms, next_bed_check_ms)) return;
  540. next_bed_check_ms = ms + BED_CHECK_INTERVAL;
  541. #endif
  542. #if TEMP_SENSOR_BED != 0
  543. #if HAS_THERMALLY_PROTECTED_BED
  544. thermal_runaway_protection(&thermal_runaway_bed_state_machine, &thermal_runaway_bed_timer, current_temperature_bed, target_temperature_bed, -1, THERMAL_PROTECTION_BED_PERIOD, THERMAL_PROTECTION_BED_HYSTERESIS);
  545. #endif
  546. #if ENABLED(PIDTEMPBED)
  547. float pid_output = get_pid_output_bed();
  548. soft_pwm_bed = current_temperature_bed > BED_MINTEMP && current_temperature_bed < BED_MAXTEMP ? (int)pid_output >> 1 : 0;
  549. #elif ENABLED(BED_LIMIT_SWITCHING)
  550. // Check if temperature is within the correct band
  551. if (current_temperature_bed > BED_MINTEMP && current_temperature_bed < BED_MAXTEMP) {
  552. if (current_temperature_bed >= target_temperature_bed + BED_HYSTERESIS)
  553. soft_pwm_bed = 0;
  554. else if (current_temperature_bed <= target_temperature_bed - (BED_HYSTERESIS))
  555. soft_pwm_bed = MAX_BED_POWER >> 1;
  556. }
  557. else {
  558. soft_pwm_bed = 0;
  559. WRITE_HEATER_BED(LOW);
  560. }
  561. #else // !PIDTEMPBED && !BED_LIMIT_SWITCHING
  562. // Check if temperature is within the correct range
  563. if (current_temperature_bed > BED_MINTEMP && current_temperature_bed < BED_MAXTEMP) {
  564. soft_pwm_bed = current_temperature_bed < target_temperature_bed ? MAX_BED_POWER >> 1 : 0;
  565. }
  566. else {
  567. soft_pwm_bed = 0;
  568. WRITE_HEATER_BED(LOW);
  569. }
  570. #endif
  571. #endif //TEMP_SENSOR_BED != 0
  572. }
  573. #define PGM_RD_W(x) (short)pgm_read_word(&x)
  574. // Derived from RepRap FiveD extruder::getTemperature()
  575. // For hot end temperature measurement.
  576. float Temperature::analog2temp(int raw, uint8_t e) {
  577. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  578. if (e > EXTRUDERS)
  579. #else
  580. if (e >= EXTRUDERS)
  581. #endif
  582. {
  583. SERIAL_ERROR_START;
  584. SERIAL_ERROR((int)e);
  585. SERIAL_ERRORLNPGM(MSG_INVALID_EXTRUDER_NUM);
  586. kill(PSTR(MSG_KILLED));
  587. return 0.0;
  588. }
  589. #if ENABLED(HEATER_0_USES_MAX6675)
  590. if (e == 0) return 0.25 * raw;
  591. #endif
  592. if (heater_ttbl_map[e] != NULL) {
  593. float celsius = 0;
  594. uint8_t i;
  595. short(*tt)[][2] = (short(*)[][2])(heater_ttbl_map[e]);
  596. for (i = 1; i < heater_ttbllen_map[e]; i++) {
  597. if (PGM_RD_W((*tt)[i][0]) > raw) {
  598. celsius = PGM_RD_W((*tt)[i - 1][1]) +
  599. (raw - PGM_RD_W((*tt)[i - 1][0])) *
  600. (float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i - 1][1])) /
  601. (float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i - 1][0]));
  602. break;
  603. }
  604. }
  605. // Overflow: Set to last value in the table
  606. if (i == heater_ttbllen_map[e]) celsius = PGM_RD_W((*tt)[i - 1][1]);
  607. return celsius;
  608. }
  609. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * (TEMP_SENSOR_AD595_GAIN)) + TEMP_SENSOR_AD595_OFFSET;
  610. }
  611. // Derived from RepRap FiveD extruder::getTemperature()
  612. // For bed temperature measurement.
  613. float Temperature::analog2tempBed(int raw) {
  614. #if ENABLED(BED_USES_THERMISTOR)
  615. float celsius = 0;
  616. byte i;
  617. for (i = 1; i < BEDTEMPTABLE_LEN; i++) {
  618. if (PGM_RD_W(BEDTEMPTABLE[i][0]) > raw) {
  619. celsius = PGM_RD_W(BEDTEMPTABLE[i - 1][1]) +
  620. (raw - PGM_RD_W(BEDTEMPTABLE[i - 1][0])) *
  621. (float)(PGM_RD_W(BEDTEMPTABLE[i][1]) - PGM_RD_W(BEDTEMPTABLE[i - 1][1])) /
  622. (float)(PGM_RD_W(BEDTEMPTABLE[i][0]) - PGM_RD_W(BEDTEMPTABLE[i - 1][0]));
  623. break;
  624. }
  625. }
  626. // Overflow: Set to last value in the table
  627. if (i == BEDTEMPTABLE_LEN) celsius = PGM_RD_W(BEDTEMPTABLE[i - 1][1]);
  628. return celsius;
  629. #elif defined(BED_USES_AD595)
  630. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * (TEMP_SENSOR_AD595_GAIN)) + TEMP_SENSOR_AD595_OFFSET;
  631. #else
  632. UNUSED(raw);
  633. return 0;
  634. #endif
  635. }
  636. /**
  637. * Get the raw values into the actual temperatures.
  638. * The raw values are created in interrupt context,
  639. * and this function is called from normal context
  640. * as it would block the stepper routine.
  641. */
  642. void Temperature::updateTemperaturesFromRawValues() {
  643. #if ENABLED(HEATER_0_USES_MAX6675)
  644. current_temperature_raw[0] = read_max6675();
  645. #endif
  646. for (uint8_t e = 0; e < EXTRUDERS; e++) {
  647. current_temperature[e] = Temperature::analog2temp(current_temperature_raw[e], e);
  648. }
  649. current_temperature_bed = Temperature::analog2tempBed(current_temperature_bed_raw);
  650. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  651. redundant_temperature = Temperature::analog2temp(redundant_temperature_raw, 1);
  652. #endif
  653. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  654. filament_width_meas = analog2widthFil();
  655. #endif
  656. #if ENABLED(USE_WATCHDOG)
  657. // Reset the watchdog after we know we have a temperature measurement.
  658. watchdog_reset();
  659. #endif
  660. CRITICAL_SECTION_START;
  661. temp_meas_ready = false;
  662. CRITICAL_SECTION_END;
  663. }
  664. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  665. // Convert raw Filament Width to millimeters
  666. float Temperature::analog2widthFil() {
  667. return current_raw_filwidth / 16383.0 * 5.0;
  668. //return current_raw_filwidth;
  669. }
  670. // Convert raw Filament Width to a ratio
  671. int Temperature::widthFil_to_size_ratio() {
  672. float temp = filament_width_meas;
  673. if (temp < MEASURED_LOWER_LIMIT) temp = filament_width_nominal; //assume sensor cut out
  674. else NOMORE(temp, MEASURED_UPPER_LIMIT);
  675. return filament_width_nominal / temp * 100;
  676. }
  677. #endif
  678. /**
  679. * Initialize the temperature manager
  680. * The manager is implemented by periodic calls to manage_heater()
  681. */
  682. void Temperature::init() {
  683. #if MB(RUMBA) && ((TEMP_SENSOR_0==-1)||(TEMP_SENSOR_1==-1)||(TEMP_SENSOR_2==-1)||(TEMP_SENSOR_BED==-1))
  684. //disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector
  685. MCUCR = _BV(JTD);
  686. MCUCR = _BV(JTD);
  687. #endif
  688. // Finish init of mult extruder arrays
  689. for (int e = 0; e < EXTRUDERS; e++) {
  690. // populate with the first value
  691. maxttemp[e] = maxttemp[0];
  692. #if ENABLED(PIDTEMP)
  693. temp_iState_min[e] = 0.0;
  694. temp_iState_max[e] = (PID_INTEGRAL_DRIVE_MAX) / PID_PARAM(Ki, e);
  695. #if ENABLED(PID_ADD_EXTRUSION_RATE)
  696. last_position[e] = 0;
  697. #endif
  698. #endif //PIDTEMP
  699. #if ENABLED(PIDTEMPBED)
  700. temp_iState_min_bed = 0.0;
  701. temp_iState_max_bed = (PID_BED_INTEGRAL_DRIVE_MAX) / bedKi;
  702. #endif //PIDTEMPBED
  703. }
  704. #if HAS_HEATER_0
  705. SET_OUTPUT(HEATER_0_PIN);
  706. #endif
  707. #if HAS_HEATER_1
  708. SET_OUTPUT(HEATER_1_PIN);
  709. #endif
  710. #if HAS_HEATER_2
  711. SET_OUTPUT(HEATER_2_PIN);
  712. #endif
  713. #if HAS_HEATER_3
  714. SET_OUTPUT(HEATER_3_PIN);
  715. #endif
  716. #if HAS_HEATER_BED
  717. SET_OUTPUT(HEATER_BED_PIN);
  718. #endif
  719. #if ENABLED(FAST_PWM_FAN) || ENABLED(FAN_SOFT_PWM)
  720. #if HAS_FAN0
  721. SET_OUTPUT(FAN_PIN);
  722. #if ENABLED(FAST_PWM_FAN)
  723. setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  724. #endif
  725. #if ENABLED(FAN_SOFT_PWM)
  726. soft_pwm_fan[0] = fanSpeedSoftPwm[0] / 2;
  727. #endif
  728. #endif
  729. #if HAS_FAN1
  730. SET_OUTPUT(FAN1_PIN);
  731. #if ENABLED(FAST_PWM_FAN)
  732. setPwmFrequency(FAN1_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  733. #endif
  734. #if ENABLED(FAN_SOFT_PWM)
  735. soft_pwm_fan[1] = fanSpeedSoftPwm[1] / 2;
  736. #endif
  737. #endif
  738. #if HAS_FAN2
  739. SET_OUTPUT(FAN2_PIN);
  740. #if ENABLED(FAST_PWM_FAN)
  741. setPwmFrequency(FAN2_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  742. #endif
  743. #if ENABLED(FAN_SOFT_PWM)
  744. soft_pwm_fan[2] = fanSpeedSoftPwm[2] / 2;
  745. #endif
  746. #endif
  747. #endif // FAST_PWM_FAN || FAN_SOFT_PWM
  748. #if ENABLED(HEATER_0_USES_MAX6675)
  749. #if DISABLED(SDSUPPORT)
  750. OUT_WRITE(SCK_PIN, LOW);
  751. OUT_WRITE(MOSI_PIN, HIGH);
  752. OUT_WRITE(MISO_PIN, HIGH);
  753. #else
  754. pinMode(SS_PIN, OUTPUT);
  755. digitalWrite(SS_PIN, HIGH);
  756. #endif
  757. OUT_WRITE(MAX6675_SS, HIGH);
  758. #endif //HEATER_0_USES_MAX6675
  759. #ifdef DIDR2
  760. #define ANALOG_SELECT(pin) do{ if (pin < 8) SBI(DIDR0, pin); else SBI(DIDR2, pin - 8); }while(0)
  761. #else
  762. #define ANALOG_SELECT(pin) do{ SBI(DIDR0, pin); }while(0)
  763. #endif
  764. // Set analog inputs
  765. ADCSRA = _BV(ADEN) | _BV(ADSC) | _BV(ADIF) | 0x07;
  766. DIDR0 = 0;
  767. #ifdef DIDR2
  768. DIDR2 = 0;
  769. #endif
  770. #if HAS_TEMP_0
  771. ANALOG_SELECT(TEMP_0_PIN);
  772. #endif
  773. #if HAS_TEMP_1
  774. ANALOG_SELECT(TEMP_1_PIN);
  775. #endif
  776. #if HAS_TEMP_2
  777. ANALOG_SELECT(TEMP_2_PIN);
  778. #endif
  779. #if HAS_TEMP_3
  780. ANALOG_SELECT(TEMP_3_PIN);
  781. #endif
  782. #if HAS_TEMP_BED
  783. ANALOG_SELECT(TEMP_BED_PIN);
  784. #endif
  785. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  786. ANALOG_SELECT(FILWIDTH_PIN);
  787. #endif
  788. #if HAS_AUTO_FAN_0
  789. pinMode(EXTRUDER_0_AUTO_FAN_PIN, OUTPUT);
  790. #endif
  791. #if HAS_AUTO_FAN_1 && (EXTRUDER_1_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN)
  792. pinMode(EXTRUDER_1_AUTO_FAN_PIN, OUTPUT);
  793. #endif
  794. #if HAS_AUTO_FAN_2 && (EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN) && (EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_1_AUTO_FAN_PIN)
  795. pinMode(EXTRUDER_2_AUTO_FAN_PIN, OUTPUT);
  796. #endif
  797. #if HAS_AUTO_FAN_3 && (EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN) && (EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_1_AUTO_FAN_PIN) && (EXTRUDER_3_AUTO_FAN_PIN != EXTRUDER_2_AUTO_FAN_PIN)
  798. pinMode(EXTRUDER_3_AUTO_FAN_PIN, OUTPUT);
  799. #endif
  800. // Use timer0 for temperature measurement
  801. // Interleave temperature interrupt with millies interrupt
  802. OCR0B = 128;
  803. SBI(TIMSK0, OCIE0B);
  804. // Wait for temperature measurement to settle
  805. delay(250);
  806. #define TEMP_MIN_ROUTINE(NR) \
  807. minttemp[NR] = HEATER_ ## NR ## _MINTEMP; \
  808. while(analog2temp(minttemp_raw[NR], NR) < HEATER_ ## NR ## _MINTEMP) { \
  809. if (HEATER_ ## NR ## _RAW_LO_TEMP < HEATER_ ## NR ## _RAW_HI_TEMP) \
  810. minttemp_raw[NR] += OVERSAMPLENR; \
  811. else \
  812. minttemp_raw[NR] -= OVERSAMPLENR; \
  813. }
  814. #define TEMP_MAX_ROUTINE(NR) \
  815. maxttemp[NR] = HEATER_ ## NR ## _MAXTEMP; \
  816. while(analog2temp(maxttemp_raw[NR], NR) > HEATER_ ## NR ## _MAXTEMP) { \
  817. if (HEATER_ ## NR ## _RAW_LO_TEMP < HEATER_ ## NR ## _RAW_HI_TEMP) \
  818. maxttemp_raw[NR] -= OVERSAMPLENR; \
  819. else \
  820. maxttemp_raw[NR] += OVERSAMPLENR; \
  821. }
  822. #ifdef HEATER_0_MINTEMP
  823. TEMP_MIN_ROUTINE(0);
  824. #endif
  825. #ifdef HEATER_0_MAXTEMP
  826. TEMP_MAX_ROUTINE(0);
  827. #endif
  828. #if EXTRUDERS > 1
  829. #ifdef HEATER_1_MINTEMP
  830. TEMP_MIN_ROUTINE(1);
  831. #endif
  832. #ifdef HEATER_1_MAXTEMP
  833. TEMP_MAX_ROUTINE(1);
  834. #endif
  835. #if EXTRUDERS > 2
  836. #ifdef HEATER_2_MINTEMP
  837. TEMP_MIN_ROUTINE(2);
  838. #endif
  839. #ifdef HEATER_2_MAXTEMP
  840. TEMP_MAX_ROUTINE(2);
  841. #endif
  842. #if EXTRUDERS > 3
  843. #ifdef HEATER_3_MINTEMP
  844. TEMP_MIN_ROUTINE(3);
  845. #endif
  846. #ifdef HEATER_3_MAXTEMP
  847. TEMP_MAX_ROUTINE(3);
  848. #endif
  849. #endif // EXTRUDERS > 3
  850. #endif // EXTRUDERS > 2
  851. #endif // EXTRUDERS > 1
  852. #ifdef BED_MINTEMP
  853. while(analog2tempBed(bed_minttemp_raw) < BED_MINTEMP) {
  854. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  855. bed_minttemp_raw += OVERSAMPLENR;
  856. #else
  857. bed_minttemp_raw -= OVERSAMPLENR;
  858. #endif
  859. }
  860. #endif //BED_MINTEMP
  861. #ifdef BED_MAXTEMP
  862. while (analog2tempBed(bed_maxttemp_raw) > BED_MAXTEMP) {
  863. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  864. bed_maxttemp_raw -= OVERSAMPLENR;
  865. #else
  866. bed_maxttemp_raw += OVERSAMPLENR;
  867. #endif
  868. }
  869. #endif //BED_MAXTEMP
  870. }
  871. #if ENABLED(THERMAL_PROTECTION_HOTENDS) && WATCH_TEMP_PERIOD > 0
  872. /**
  873. * Start Heating Sanity Check for hotends that are below
  874. * their target temperature by a configurable margin.
  875. * This is called when the temperature is set. (M104, M109)
  876. */
  877. void Temperature::start_watching_heater(int e) {
  878. if (degHotend(e) < degTargetHotend(e) - (WATCH_TEMP_INCREASE + TEMP_HYSTERESIS + 1)) {
  879. watch_target_temp[e] = degHotend(e) + WATCH_TEMP_INCREASE;
  880. watch_heater_next_ms[e] = millis() + (WATCH_TEMP_PERIOD) * 1000UL;
  881. }
  882. else
  883. watch_heater_next_ms[e] = 0;
  884. }
  885. #endif
  886. #if ENABLED(THERMAL_PROTECTION_BED) && WATCH_BED_TEMP_PERIOD > 0
  887. /**
  888. * Start Heating Sanity Check for hotends that are below
  889. * their target temperature by a configurable margin.
  890. * This is called when the temperature is set. (M140, M190)
  891. */
  892. void Temperature::start_watching_bed() {
  893. if (degBed() < degTargetBed() - (WATCH_BED_TEMP_INCREASE + TEMP_BED_HYSTERESIS + 1)) {
  894. watch_target_bed_temp = degBed() + WATCH_BED_TEMP_INCREASE;
  895. watch_bed_next_ms = millis() + (WATCH_BED_TEMP_PERIOD) * 1000UL;
  896. }
  897. else
  898. watch_bed_next_ms = 0;
  899. }
  900. #endif
  901. #if ENABLED(THERMAL_PROTECTION_HOTENDS) || HAS_THERMALLY_PROTECTED_BED
  902. void Temperature::thermal_runaway_protection(TRState* state, millis_t* timer, float temperature, float target_temperature, int heater_id, int period_seconds, int hysteresis_degc) {
  903. static float tr_target_temperature[EXTRUDERS + 1] = { 0.0 };
  904. /**
  905. SERIAL_ECHO_START;
  906. SERIAL_ECHOPGM("Thermal Thermal Runaway Running. Heater ID: ");
  907. if (heater_id < 0) SERIAL_ECHOPGM("bed"); else SERIAL_ECHO(heater_id);
  908. SERIAL_ECHOPAIR(" ; State:", *state);
  909. SERIAL_ECHOPAIR(" ; Timer:", *timer);
  910. SERIAL_ECHOPAIR(" ; Temperature:", temperature);
  911. SERIAL_ECHOPAIR(" ; Target Temp:", target_temperature);
  912. SERIAL_EOL;
  913. */
  914. int heater_index = heater_id >= 0 ? heater_id : EXTRUDERS;
  915. // If the target temperature changes, restart
  916. if (tr_target_temperature[heater_index] != target_temperature) {
  917. tr_target_temperature[heater_index] = target_temperature;
  918. *state = target_temperature > 0 ? TRFirstHeating : TRInactive;
  919. }
  920. switch (*state) {
  921. // Inactive state waits for a target temperature to be set
  922. case TRInactive: break;
  923. // When first heating, wait for the temperature to be reached then go to Stable state
  924. case TRFirstHeating:
  925. if (temperature < tr_target_temperature[heater_index]) break;
  926. *state = TRStable;
  927. // While the temperature is stable watch for a bad temperature
  928. case TRStable:
  929. if (temperature < tr_target_temperature[heater_index] - hysteresis_degc && ELAPSED(millis(), *timer))
  930. *state = TRRunaway;
  931. else {
  932. *timer = millis() + period_seconds * 1000UL;
  933. break;
  934. }
  935. case TRRunaway:
  936. _temp_error(heater_id, PSTR(MSG_T_THERMAL_RUNAWAY), PSTR(MSG_THERMAL_RUNAWAY));
  937. }
  938. }
  939. #endif // THERMAL_PROTECTION_HOTENDS || THERMAL_PROTECTION_BED
  940. void Temperature::disable_all_heaters() {
  941. for (int i = 0; i < EXTRUDERS; i++) setTargetHotend(0, i);
  942. setTargetBed(0);
  943. // If all heaters go down then for sure our print job has stopped
  944. print_job_timer.stop();
  945. #define DISABLE_HEATER(NR) { \
  946. setTargetHotend(NR, 0); \
  947. soft_pwm[NR] = 0; \
  948. WRITE_HEATER_ ## NR (LOW); \
  949. }
  950. #if HAS_TEMP_HOTEND
  951. setTargetHotend(0, 0);
  952. soft_pwm[0] = 0;
  953. WRITE_HEATER_0P(LOW); // Should HEATERS_PARALLEL apply here? Then change to DISABLE_HEATER(0)
  954. #endif
  955. #if EXTRUDERS > 1 && HAS_TEMP_1
  956. DISABLE_HEATER(1);
  957. #endif
  958. #if EXTRUDERS > 2 && HAS_TEMP_2
  959. DISABLE_HEATER(2);
  960. #endif
  961. #if EXTRUDERS > 3 && HAS_TEMP_3
  962. DISABLE_HEATER(3);
  963. #endif
  964. #if HAS_TEMP_BED
  965. target_temperature_bed = 0;
  966. soft_pwm_bed = 0;
  967. #if HAS_HEATER_BED
  968. WRITE_HEATER_BED(LOW);
  969. #endif
  970. #endif
  971. }
  972. #if ENABLED(HEATER_0_USES_MAX6675)
  973. #define MAX6675_HEAT_INTERVAL 250u
  974. #if ENABLED(MAX6675_IS_MAX31855)
  975. uint32_t max6675_temp = 2000;
  976. #define MAX6675_ERROR_MASK 7
  977. #define MAX6675_DISCARD_BITS 18
  978. #else
  979. uint16_t max6675_temp = 2000;
  980. #define MAX6675_ERROR_MASK 4
  981. #define MAX6675_DISCARD_BITS 3
  982. #endif
  983. int Temperature::read_max6675() {
  984. static millis_t next_max6675_ms = 0;
  985. millis_t ms = millis();
  986. if (PENDING(ms, next_max6675_ms)) return (int)max6675_temp;
  987. next_max6675_ms = ms + MAX6675_HEAT_INTERVAL;
  988. CBI(
  989. #ifdef PRR
  990. PRR
  991. #elif defined(PRR0)
  992. PRR0
  993. #endif
  994. , PRSPI);
  995. SPCR = _BV(MSTR) | _BV(SPE) | _BV(SPR0);
  996. WRITE(MAX6675_SS, 0); // enable TT_MAX6675
  997. // ensure 100ns delay - a bit extra is fine
  998. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  999. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  1000. // Read a big-endian temperature value
  1001. max6675_temp = 0;
  1002. for (uint8_t i = sizeof(max6675_temp); i--;) {
  1003. SPDR = 0;
  1004. for (;!TEST(SPSR, SPIF););
  1005. max6675_temp |= SPDR;
  1006. if (i > 0) max6675_temp <<= 8; // shift left if not the last byte
  1007. }
  1008. WRITE(MAX6675_SS, 1); // disable TT_MAX6675
  1009. if (max6675_temp & MAX6675_ERROR_MASK)
  1010. max6675_temp = 4000; // thermocouple open
  1011. else
  1012. max6675_temp >>= MAX6675_DISCARD_BITS;
  1013. return (int)max6675_temp;
  1014. }
  1015. #endif //HEATER_0_USES_MAX6675
  1016. /**
  1017. * Stages in the ISR loop
  1018. */
  1019. enum TempState {
  1020. PrepareTemp_0,
  1021. MeasureTemp_0,
  1022. PrepareTemp_BED,
  1023. MeasureTemp_BED,
  1024. PrepareTemp_1,
  1025. MeasureTemp_1,
  1026. PrepareTemp_2,
  1027. MeasureTemp_2,
  1028. PrepareTemp_3,
  1029. MeasureTemp_3,
  1030. Prepare_FILWIDTH,
  1031. Measure_FILWIDTH,
  1032. StartupDelay // Startup, delay initial temp reading a tiny bit so the hardware can settle
  1033. };
  1034. /**
  1035. * Get raw temperatures
  1036. */
  1037. void Temperature::set_current_temp_raw() {
  1038. #if HAS_TEMP_0 && DISABLED(HEATER_0_USES_MAX6675)
  1039. current_temperature_raw[0] = raw_temp_value[0];
  1040. #endif
  1041. #if HAS_TEMP_1
  1042. #if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
  1043. redundant_temperature_raw = raw_temp_value[1];
  1044. #else
  1045. current_temperature_raw[1] = raw_temp_value[1];
  1046. #endif
  1047. #if HAS_TEMP_2
  1048. current_temperature_raw[2] = raw_temp_value[2];
  1049. #if HAS_TEMP_3
  1050. current_temperature_raw[3] = raw_temp_value[3];
  1051. #endif
  1052. #endif
  1053. #endif
  1054. current_temperature_bed_raw = raw_temp_bed_value;
  1055. temp_meas_ready = true;
  1056. }
  1057. /**
  1058. * Timer 0 is shared with millies
  1059. * - Manage PWM to all the heaters and fan
  1060. * - Update the raw temperature values
  1061. * - Check new temperature values for MIN/MAX errors
  1062. * - Step the babysteps value for each axis towards 0
  1063. */
  1064. ISR(TIMER0_COMPB_vect) { thermalManager.isr(); }
  1065. void Temperature::isr() {
  1066. static unsigned char temp_count = 0;
  1067. static TempState temp_state = StartupDelay;
  1068. static unsigned char pwm_count = _BV(SOFT_PWM_SCALE);
  1069. // Static members for each heater
  1070. #if ENABLED(SLOW_PWM_HEATERS)
  1071. static unsigned char slow_pwm_count = 0;
  1072. #define ISR_STATICS(n) \
  1073. static unsigned char soft_pwm_ ## n; \
  1074. static unsigned char state_heater_ ## n = 0; \
  1075. static unsigned char state_timer_heater_ ## n = 0
  1076. #else
  1077. #define ISR_STATICS(n) static unsigned char soft_pwm_ ## n
  1078. #endif
  1079. // Statics per heater
  1080. ISR_STATICS(0);
  1081. #if (EXTRUDERS > 1) || ENABLED(HEATERS_PARALLEL)
  1082. ISR_STATICS(1);
  1083. #if EXTRUDERS > 2
  1084. ISR_STATICS(2);
  1085. #if EXTRUDERS > 3
  1086. ISR_STATICS(3);
  1087. #endif
  1088. #endif
  1089. #endif
  1090. #if HAS_HEATER_BED
  1091. ISR_STATICS(BED);
  1092. #endif
  1093. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1094. static unsigned long raw_filwidth_value = 0;
  1095. #endif
  1096. #if DISABLED(SLOW_PWM_HEATERS)
  1097. /**
  1098. * standard PWM modulation
  1099. */
  1100. if (pwm_count == 0) {
  1101. soft_pwm_0 = soft_pwm[0];
  1102. if (soft_pwm_0 > 0) {
  1103. WRITE_HEATER_0(1);
  1104. }
  1105. else WRITE_HEATER_0P(0); // If HEATERS_PARALLEL should apply, change to WRITE_HEATER_0
  1106. #if EXTRUDERS > 1
  1107. soft_pwm_1 = soft_pwm[1];
  1108. WRITE_HEATER_1(soft_pwm_1 > 0 ? 1 : 0);
  1109. #if EXTRUDERS > 2
  1110. soft_pwm_2 = soft_pwm[2];
  1111. WRITE_HEATER_2(soft_pwm_2 > 0 ? 1 : 0);
  1112. #if EXTRUDERS > 3
  1113. soft_pwm_3 = soft_pwm[3];
  1114. WRITE_HEATER_3(soft_pwm_3 > 0 ? 1 : 0);
  1115. #endif
  1116. #endif
  1117. #endif
  1118. #if HAS_HEATER_BED
  1119. soft_pwm_BED = soft_pwm_bed;
  1120. WRITE_HEATER_BED(soft_pwm_BED > 0 ? 1 : 0);
  1121. #endif
  1122. #if ENABLED(FAN_SOFT_PWM)
  1123. #if HAS_FAN0
  1124. soft_pwm_fan[0] = fanSpeedSoftPwm[0] / 2;
  1125. WRITE_FAN(soft_pwm_fan[0] > 0 ? 1 : 0);
  1126. #endif
  1127. #if HAS_FAN1
  1128. soft_pwm_fan[1] = fanSpeedSoftPwm[1] / 2;
  1129. WRITE_FAN1(soft_pwm_fan[1] > 0 ? 1 : 0);
  1130. #endif
  1131. #if HAS_FAN2
  1132. soft_pwm_fan[2] = fanSpeedSoftPwm[2] / 2;
  1133. WRITE_FAN2(soft_pwm_fan[2] > 0 ? 1 : 0);
  1134. #endif
  1135. #endif
  1136. }
  1137. if (soft_pwm_0 < pwm_count) WRITE_HEATER_0(0);
  1138. #if EXTRUDERS > 1
  1139. if (soft_pwm_1 < pwm_count) WRITE_HEATER_1(0);
  1140. #if EXTRUDERS > 2
  1141. if (soft_pwm_2 < pwm_count) WRITE_HEATER_2(0);
  1142. #if EXTRUDERS > 3
  1143. if (soft_pwm_3 < pwm_count) WRITE_HEATER_3(0);
  1144. #endif
  1145. #endif
  1146. #endif
  1147. #if HAS_HEATER_BED
  1148. if (soft_pwm_BED < pwm_count) WRITE_HEATER_BED(0);
  1149. #endif
  1150. #if ENABLED(FAN_SOFT_PWM)
  1151. #if HAS_FAN0
  1152. if (soft_pwm_fan[0] < pwm_count) WRITE_FAN(0);
  1153. #endif
  1154. #if HAS_FAN1
  1155. if (soft_pwm_fan[1] < pwm_count) WRITE_FAN1(0);
  1156. #endif
  1157. #if HAS_FAN2
  1158. if (soft_pwm_fan[2] < pwm_count) WRITE_FAN2(0);
  1159. #endif
  1160. #endif
  1161. pwm_count += _BV(SOFT_PWM_SCALE);
  1162. pwm_count &= 0x7f;
  1163. #else // SLOW_PWM_HEATERS
  1164. /**
  1165. * SLOW PWM HEATERS
  1166. *
  1167. * for heaters drived by relay
  1168. */
  1169. #ifndef MIN_STATE_TIME
  1170. #define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds
  1171. #endif
  1172. // Macros for Slow PWM timer logic - HEATERS_PARALLEL applies
  1173. #define _SLOW_PWM_ROUTINE(NR, src) \
  1174. soft_pwm_ ## NR = src; \
  1175. if (soft_pwm_ ## NR > 0) { \
  1176. if (state_timer_heater_ ## NR == 0) { \
  1177. if (state_heater_ ## NR == 0) state_timer_heater_ ## NR = MIN_STATE_TIME; \
  1178. state_heater_ ## NR = 1; \
  1179. WRITE_HEATER_ ## NR(1); \
  1180. } \
  1181. } \
  1182. else { \
  1183. if (state_timer_heater_ ## NR == 0) { \
  1184. if (state_heater_ ## NR == 1) state_timer_heater_ ## NR = MIN_STATE_TIME; \
  1185. state_heater_ ## NR = 0; \
  1186. WRITE_HEATER_ ## NR(0); \
  1187. } \
  1188. }
  1189. #define SLOW_PWM_ROUTINE(n) _SLOW_PWM_ROUTINE(n, soft_pwm[n])
  1190. #define PWM_OFF_ROUTINE(NR) \
  1191. if (soft_pwm_ ## NR < slow_pwm_count) { \
  1192. if (state_timer_heater_ ## NR == 0) { \
  1193. if (state_heater_ ## NR == 1) state_timer_heater_ ## NR = MIN_STATE_TIME; \
  1194. state_heater_ ## NR = 0; \
  1195. WRITE_HEATER_ ## NR (0); \
  1196. } \
  1197. }
  1198. if (slow_pwm_count == 0) {
  1199. SLOW_PWM_ROUTINE(0); // EXTRUDER 0
  1200. #if EXTRUDERS > 1
  1201. SLOW_PWM_ROUTINE(1); // EXTRUDER 1
  1202. #if EXTRUDERS > 2
  1203. SLOW_PWM_ROUTINE(2); // EXTRUDER 2
  1204. #if EXTRUDERS > 3
  1205. SLOW_PWM_ROUTINE(3); // EXTRUDER 3
  1206. #endif
  1207. #endif
  1208. #endif
  1209. #if HAS_HEATER_BED
  1210. _SLOW_PWM_ROUTINE(BED, soft_pwm_bed); // BED
  1211. #endif
  1212. } // slow_pwm_count == 0
  1213. PWM_OFF_ROUTINE(0); // EXTRUDER 0
  1214. #if EXTRUDERS > 1
  1215. PWM_OFF_ROUTINE(1); // EXTRUDER 1
  1216. #if EXTRUDERS > 2
  1217. PWM_OFF_ROUTINE(2); // EXTRUDER 2
  1218. #if EXTRUDERS > 3
  1219. PWM_OFF_ROUTINE(3); // EXTRUDER 3
  1220. #endif
  1221. #endif
  1222. #endif
  1223. #if HAS_HEATER_BED
  1224. PWM_OFF_ROUTINE(BED); // BED
  1225. #endif
  1226. #if ENABLED(FAN_SOFT_PWM)
  1227. if (pwm_count == 0) {
  1228. #if HAS_FAN0
  1229. soft_pwm_fan[0] = fanSpeedSoftPwm[0] / 2;
  1230. WRITE_FAN(soft_pwm_fan[0] > 0 ? 1 : 0);
  1231. #endif
  1232. #if HAS_FAN1
  1233. soft_pwm_fan[1] = fanSpeedSoftPwm[1] / 2;
  1234. WRITE_FAN1(soft_pwm_fan[1] > 0 ? 1 : 0);
  1235. #endif
  1236. #if HAS_FAN2
  1237. soft_pwm_fan[2] = fanSpeedSoftPwm[2] / 2;
  1238. WRITE_FAN2(soft_pwm_fan[2] > 0 ? 1 : 0);
  1239. #endif
  1240. }
  1241. #if HAS_FAN0
  1242. if (soft_pwm_fan[0] < pwm_count) WRITE_FAN(0);
  1243. #endif
  1244. #if HAS_FAN1
  1245. if (soft_pwm_fan[1] < pwm_count) WRITE_FAN1(0);
  1246. #endif
  1247. #if HAS_FAN2
  1248. if (soft_pwm_fan[2] < pwm_count) WRITE_FAN2(0);
  1249. #endif
  1250. #endif //FAN_SOFT_PWM
  1251. pwm_count += _BV(SOFT_PWM_SCALE);
  1252. pwm_count &= 0x7f;
  1253. // increment slow_pwm_count only every 64 pwm_count circa 65.5ms
  1254. if ((pwm_count % 64) == 0) {
  1255. slow_pwm_count++;
  1256. slow_pwm_count &= 0x7f;
  1257. // EXTRUDER 0
  1258. if (state_timer_heater_0 > 0) state_timer_heater_0--;
  1259. #if EXTRUDERS > 1 // EXTRUDER 1
  1260. if (state_timer_heater_1 > 0) state_timer_heater_1--;
  1261. #if EXTRUDERS > 2 // EXTRUDER 2
  1262. if (state_timer_heater_2 > 0) state_timer_heater_2--;
  1263. #if EXTRUDERS > 3 // EXTRUDER 3
  1264. if (state_timer_heater_3 > 0) state_timer_heater_3--;
  1265. #endif
  1266. #endif
  1267. #endif
  1268. #if HAS_HEATER_BED
  1269. if (state_timer_heater_BED > 0) state_timer_heater_BED--;
  1270. #endif
  1271. } // (pwm_count % 64) == 0
  1272. #endif // SLOW_PWM_HEATERS
  1273. #define SET_ADMUX_ADCSRA(pin) ADMUX = _BV(REFS0) | (pin & 0x07); SBI(ADCSRA, ADSC)
  1274. #ifdef MUX5
  1275. #define START_ADC(pin) if (pin > 7) ADCSRB = _BV(MUX5); else ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
  1276. #else
  1277. #define START_ADC(pin) ADCSRB = 0; SET_ADMUX_ADCSRA(pin)
  1278. #endif
  1279. // Prepare or measure a sensor, each one every 12th frame
  1280. switch (temp_state) {
  1281. case PrepareTemp_0:
  1282. #if HAS_TEMP_0
  1283. START_ADC(TEMP_0_PIN);
  1284. #endif
  1285. lcd_buttons_update();
  1286. temp_state = MeasureTemp_0;
  1287. break;
  1288. case MeasureTemp_0:
  1289. #if HAS_TEMP_0
  1290. raw_temp_value[0] += ADC;
  1291. #endif
  1292. temp_state = PrepareTemp_BED;
  1293. break;
  1294. case PrepareTemp_BED:
  1295. #if HAS_TEMP_BED
  1296. START_ADC(TEMP_BED_PIN);
  1297. #endif
  1298. lcd_buttons_update();
  1299. temp_state = MeasureTemp_BED;
  1300. break;
  1301. case MeasureTemp_BED:
  1302. #if HAS_TEMP_BED
  1303. raw_temp_bed_value += ADC;
  1304. #endif
  1305. temp_state = PrepareTemp_1;
  1306. break;
  1307. case PrepareTemp_1:
  1308. #if HAS_TEMP_1
  1309. START_ADC(TEMP_1_PIN);
  1310. #endif
  1311. lcd_buttons_update();
  1312. temp_state = MeasureTemp_1;
  1313. break;
  1314. case MeasureTemp_1:
  1315. #if HAS_TEMP_1
  1316. raw_temp_value[1] += ADC;
  1317. #endif
  1318. temp_state = PrepareTemp_2;
  1319. break;
  1320. case PrepareTemp_2:
  1321. #if HAS_TEMP_2
  1322. START_ADC(TEMP_2_PIN);
  1323. #endif
  1324. lcd_buttons_update();
  1325. temp_state = MeasureTemp_2;
  1326. break;
  1327. case MeasureTemp_2:
  1328. #if HAS_TEMP_2
  1329. raw_temp_value[2] += ADC;
  1330. #endif
  1331. temp_state = PrepareTemp_3;
  1332. break;
  1333. case PrepareTemp_3:
  1334. #if HAS_TEMP_3
  1335. START_ADC(TEMP_3_PIN);
  1336. #endif
  1337. lcd_buttons_update();
  1338. temp_state = MeasureTemp_3;
  1339. break;
  1340. case MeasureTemp_3:
  1341. #if HAS_TEMP_3
  1342. raw_temp_value[3] += ADC;
  1343. #endif
  1344. temp_state = Prepare_FILWIDTH;
  1345. break;
  1346. case Prepare_FILWIDTH:
  1347. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1348. START_ADC(FILWIDTH_PIN);
  1349. #endif
  1350. lcd_buttons_update();
  1351. temp_state = Measure_FILWIDTH;
  1352. break;
  1353. case Measure_FILWIDTH:
  1354. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1355. // raw_filwidth_value += ADC; //remove to use an IIR filter approach
  1356. if (ADC > 102) { //check that ADC is reading a voltage > 0.5 volts, otherwise don't take in the data.
  1357. raw_filwidth_value -= (raw_filwidth_value >> 7); //multiply raw_filwidth_value by 127/128
  1358. raw_filwidth_value += ((unsigned long)ADC << 7); //add new ADC reading
  1359. }
  1360. #endif
  1361. temp_state = PrepareTemp_0;
  1362. temp_count++;
  1363. break;
  1364. case StartupDelay:
  1365. temp_state = PrepareTemp_0;
  1366. break;
  1367. // default:
  1368. // SERIAL_ERROR_START;
  1369. // SERIAL_ERRORLNPGM("Temp measurement error!");
  1370. // break;
  1371. } // switch(temp_state)
  1372. if (temp_count >= OVERSAMPLENR) { // 10 * 16 * 1/(16000000/64/256) = 164ms.
  1373. // Update the raw values if they've been read. Else we could be updating them during reading.
  1374. if (!temp_meas_ready) set_current_temp_raw();
  1375. // Filament Sensor - can be read any time since IIR filtering is used
  1376. #if ENABLED(FILAMENT_WIDTH_SENSOR)
  1377. current_raw_filwidth = raw_filwidth_value >> 10; // Divide to get to 0-16384 range since we used 1/128 IIR filter approach
  1378. #endif
  1379. temp_count = 0;
  1380. for (int i = 0; i < 4; i++) raw_temp_value[i] = 0;
  1381. raw_temp_bed_value = 0;
  1382. #if HAS_TEMP_0 && DISABLED(HEATER_0_USES_MAX6675)
  1383. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1384. #define GE0 <=
  1385. #else
  1386. #define GE0 >=
  1387. #endif
  1388. if (current_temperature_raw[0] GE0 maxttemp_raw[0]) max_temp_error(0);
  1389. if (minttemp_raw[0] GE0 current_temperature_raw[0]) min_temp_error(0);
  1390. #endif
  1391. #if HAS_TEMP_1 && EXTRUDERS > 1
  1392. #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
  1393. #define GE1 <=
  1394. #else
  1395. #define GE1 >=
  1396. #endif
  1397. if (current_temperature_raw[1] GE1 maxttemp_raw[1]) max_temp_error(1);
  1398. if (minttemp_raw[1] GE1 current_temperature_raw[1]) min_temp_error(1);
  1399. #endif // TEMP_SENSOR_1
  1400. #if HAS_TEMP_2 && EXTRUDERS > 2
  1401. #if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
  1402. #define GE2 <=
  1403. #else
  1404. #define GE2 >=
  1405. #endif
  1406. if (current_temperature_raw[2] GE2 maxttemp_raw[2]) max_temp_error(2);
  1407. if (minttemp_raw[2] GE2 current_temperature_raw[2]) min_temp_error(2);
  1408. #endif // TEMP_SENSOR_2
  1409. #if HAS_TEMP_3 && EXTRUDERS > 3
  1410. #if HEATER_3_RAW_LO_TEMP > HEATER_3_RAW_HI_TEMP
  1411. #define GE3 <=
  1412. #else
  1413. #define GE3 >=
  1414. #endif
  1415. if (current_temperature_raw[3] GE3 maxttemp_raw[3]) max_temp_error(3);
  1416. if (minttemp_raw[3] GE3 current_temperature_raw[3]) min_temp_error(3);
  1417. #endif // TEMP_SENSOR_3
  1418. #if HAS_TEMP_BED
  1419. #if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1420. #define GEBED <=
  1421. #else
  1422. #define GEBED >=
  1423. #endif
  1424. if (current_temperature_bed_raw GEBED bed_maxttemp_raw) _temp_error(-1, PSTR(MSG_T_MAXTEMP), PSTR(MSG_ERR_MAXTEMP_BED));
  1425. if (bed_minttemp_raw GEBED current_temperature_bed_raw) _temp_error(-1, PSTR(MSG_T_MINTEMP), PSTR(MSG_ERR_MINTEMP_BED));
  1426. #endif
  1427. } // temp_count >= OVERSAMPLENR
  1428. #if ENABLED(BABYSTEPPING)
  1429. for (uint8_t axis = X_AXIS; axis <= Z_AXIS; axis++) {
  1430. int curTodo = babystepsTodo[axis]; //get rid of volatile for performance
  1431. if (curTodo > 0) {
  1432. stepper.babystep(axis,/*fwd*/true);
  1433. babystepsTodo[axis]--; //fewer to do next time
  1434. }
  1435. else if (curTodo < 0) {
  1436. stepper.babystep(axis,/*fwd*/false);
  1437. babystepsTodo[axis]++; //fewer to do next time
  1438. }
  1439. }
  1440. #endif //BABYSTEPPING
  1441. }