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

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