My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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