My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

temperature.cpp 49KB

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