temperature.cpp 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270
  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 "temperature.h"
  24. #include "stepper.h"
  25. #include "ultralcd.h"
  26. #include "menu.h"
  27. #include "sound.h"
  28. #include "fancheck.h"
  29. #include "SdFatUtil.h"
  30. #include <avr/wdt.h>
  31. #include <util/atomic.h>
  32. #include "adc.h"
  33. #include "ConfigurationStore.h"
  34. #include "Timer.h"
  35. #include "Configuration_prusa.h"
  36. #if (ADC_OVRSAMPL != OVERSAMPLENR)
  37. #error "ADC_OVRSAMPL oversampling must match OVERSAMPLENR"
  38. #endif
  39. //===========================================================================
  40. //=============================public variables============================
  41. //===========================================================================
  42. int target_temperature[EXTRUDERS] = { 0 };
  43. int target_temperature_bed = 0;
  44. int current_temperature_raw[EXTRUDERS] = { 0 };
  45. float current_temperature[EXTRUDERS] = { 0.0 };
  46. #ifdef PINDA_THERMISTOR
  47. uint16_t current_temperature_raw_pinda = 0;
  48. float current_temperature_pinda = 0.0;
  49. #endif //PINDA_THERMISTOR
  50. #ifdef AMBIENT_THERMISTOR
  51. int current_temperature_raw_ambient = 0;
  52. float current_temperature_ambient = 0.0;
  53. #endif //AMBIENT_THERMISTOR
  54. #ifdef VOLT_PWR_PIN
  55. int current_voltage_raw_pwr = 0;
  56. #endif
  57. #ifdef VOLT_BED_PIN
  58. int current_voltage_raw_bed = 0;
  59. #endif
  60. #ifdef IR_SENSOR_ANALOG
  61. uint16_t current_voltage_raw_IR = 0;
  62. #endif //IR_SENSOR_ANALOG
  63. int current_temperature_bed_raw = 0;
  64. float current_temperature_bed = 0.0;
  65. #ifdef PIDTEMP
  66. float _Kp, _Ki, _Kd;
  67. int pid_cycle, pid_number_of_cycles;
  68. bool pid_tuning_finished = false;
  69. #endif //PIDTEMP
  70. unsigned char soft_pwm_bed;
  71. #ifdef BABYSTEPPING
  72. volatile int babystepsTodo[3]={0,0,0};
  73. #endif
  74. //===========================================================================
  75. //=============================private variables============================
  76. //===========================================================================
  77. static volatile bool temp_meas_ready = false;
  78. #ifdef PIDTEMP
  79. //static cannot be external:
  80. static float iState_sum[EXTRUDERS] = { 0 };
  81. static float dState_last[EXTRUDERS] = { 0 };
  82. static float pTerm[EXTRUDERS];
  83. static float iTerm[EXTRUDERS];
  84. static float dTerm[EXTRUDERS];
  85. static float pid_error[EXTRUDERS];
  86. static float iState_sum_min[EXTRUDERS];
  87. static float iState_sum_max[EXTRUDERS];
  88. static bool pid_reset[EXTRUDERS];
  89. #endif //PIDTEMP
  90. #ifdef PIDTEMPBED
  91. //static cannot be external:
  92. static float temp_iState_bed = { 0 };
  93. static float temp_dState_bed = { 0 };
  94. static float pTerm_bed;
  95. static float iTerm_bed;
  96. static float dTerm_bed;
  97. static float pid_error_bed;
  98. static float temp_iState_min_bed;
  99. static float temp_iState_max_bed;
  100. #else //PIDTEMPBED
  101. static unsigned long previous_millis_bed_heater;
  102. #endif //PIDTEMPBED
  103. static unsigned char soft_pwm[EXTRUDERS];
  104. #ifdef FAN_SOFT_PWM
  105. unsigned char fanSpeedSoftPwm;
  106. static unsigned char soft_pwm_fan;
  107. #endif
  108. uint8_t fanSpeedBckp = 255;
  109. #if EXTRUDERS > 3
  110. # error Unsupported number of extruders
  111. #elif EXTRUDERS > 2
  112. # define ARRAY_BY_EXTRUDERS(v1, v2, v3) { v1, v2, v3 }
  113. #elif EXTRUDERS > 1
  114. # define ARRAY_BY_EXTRUDERS(v1, v2, v3) { v1, v2 }
  115. #else
  116. # define ARRAY_BY_EXTRUDERS(v1, v2, v3) { v1 }
  117. #endif
  118. // Init min and max temp with extreme values to prevent false errors during startup
  119. static int minttemp_raw[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_RAW_LO_TEMP , HEATER_1_RAW_LO_TEMP , HEATER_2_RAW_LO_TEMP );
  120. static int maxttemp_raw[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_RAW_HI_TEMP , HEATER_1_RAW_HI_TEMP , HEATER_2_RAW_HI_TEMP );
  121. static int minttemp[EXTRUDERS] = ARRAY_BY_EXTRUDERS( 0, 0, 0 );
  122. static int maxttemp[EXTRUDERS] = ARRAY_BY_EXTRUDERS( 16383, 16383, 16383 );
  123. #ifdef BED_MINTEMP
  124. static int bed_minttemp_raw = HEATER_BED_RAW_LO_TEMP;
  125. #endif
  126. #ifdef BED_MAXTEMP
  127. static int bed_maxttemp_raw = HEATER_BED_RAW_HI_TEMP;
  128. #endif
  129. #ifdef AMBIENT_MINTEMP
  130. static int ambient_minttemp_raw = AMBIENT_RAW_LO_TEMP;
  131. #endif
  132. #ifdef AMBIENT_MAXTEMP
  133. static int ambient_maxttemp_raw = AMBIENT_RAW_HI_TEMP;
  134. #endif
  135. static void *heater_ttbl_map[EXTRUDERS] = ARRAY_BY_EXTRUDERS( (void *)HEATER_0_TEMPTABLE, (void *)HEATER_1_TEMPTABLE, (void *)HEATER_2_TEMPTABLE );
  136. static uint8_t heater_ttbllen_map[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN, HEATER_2_TEMPTABLE_LEN );
  137. static float analog2temp(int raw, uint8_t e);
  138. static float analog2tempBed(int raw);
  139. #ifdef AMBIENT_MAXTEMP
  140. static float analog2tempAmbient(int raw);
  141. #endif
  142. static void updateTemperatures();
  143. enum TempRunawayStates : uint8_t
  144. {
  145. TempRunaway_INACTIVE = 0,
  146. TempRunaway_PREHEAT = 1,
  147. TempRunaway_ACTIVE = 2,
  148. };
  149. #ifndef SOFT_PWM_SCALE
  150. #define SOFT_PWM_SCALE 0
  151. #endif
  152. //===========================================================================
  153. //============================= functions ============================
  154. //===========================================================================
  155. #if (defined (TEMP_RUNAWAY_BED_HYSTERESIS) && TEMP_RUNAWAY_BED_TIMEOUT > 0) || (defined (TEMP_RUNAWAY_EXTRUDER_HYSTERESIS) && TEMP_RUNAWAY_EXTRUDER_TIMEOUT > 0)
  156. static uint8_t temp_runaway_status[1 + EXTRUDERS];
  157. static float temp_runaway_target[1 + EXTRUDERS];
  158. static uint32_t temp_runaway_timer[1 + EXTRUDERS];
  159. static uint16_t temp_runaway_error_counter[1 + EXTRUDERS];
  160. static void temp_runaway_check(uint8_t _heater_id, float _target_temperature, float _current_temperature, float _output, bool _isbed);
  161. static void temp_runaway_stop(bool isPreheat, bool isBed);
  162. #endif
  163. // return "false", if all extruder-heaters are 'off' (ie. "true", if any heater is 'on')
  164. bool checkAllHotends(void)
  165. {
  166. bool result=false;
  167. for(int i=0;i<EXTRUDERS;i++) result=(result||(target_temperature[i]!=0));
  168. return(result);
  169. }
  170. // WARNING: the following function has been marked noinline to avoid a GCC 4.9.2 LTO
  171. // codegen bug causing a stack overwrite issue in process_commands()
  172. void __attribute__((noinline)) PID_autotune(float temp, int extruder, int ncycles)
  173. {
  174. pid_number_of_cycles = ncycles;
  175. pid_tuning_finished = false;
  176. float input = 0.0;
  177. pid_cycle=0;
  178. bool heating = true;
  179. unsigned long temp_millis = _millis();
  180. unsigned long t1=temp_millis;
  181. unsigned long t2=temp_millis;
  182. long t_high = 0;
  183. long t_low = 0;
  184. long bias, d;
  185. float Ku, Tu;
  186. float max = 0, min = 10000;
  187. uint8_t safety_check_cycles = 0;
  188. const uint8_t safety_check_cycles_count = (extruder < 0) ? 45 : 10; //10 cycles / 20s delay for extruder and 45 cycles / 90s for heatbed
  189. float temp_ambient;
  190. #if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1)
  191. unsigned long extruder_autofan_last_check = _millis();
  192. #endif
  193. if ((extruder >= EXTRUDERS)
  194. #if (TEMP_BED_PIN <= -1)
  195. ||(extruder < 0)
  196. #endif
  197. ){
  198. SERIAL_ECHOLN("PID Autotune failed. Bad extruder number.");
  199. pid_tuning_finished = true;
  200. pid_cycle = 0;
  201. return;
  202. }
  203. SERIAL_ECHOLN("PID Autotune start");
  204. disable_heater(); // switch off all heaters.
  205. if (extruder<0)
  206. {
  207. soft_pwm_bed = (MAX_BED_POWER)/2;
  208. timer02_set_pwm0(soft_pwm_bed << 1);
  209. bias = d = (MAX_BED_POWER)/2;
  210. target_temperature_bed = (int)temp; // to display the requested target bed temperature properly on the main screen
  211. }
  212. else
  213. {
  214. soft_pwm[extruder] = (PID_MAX)/2;
  215. bias = d = (PID_MAX)/2;
  216. target_temperature[extruder] = (int)temp; // to display the requested target extruder temperature properly on the main screen
  217. }
  218. for(;;) {
  219. #ifdef WATCHDOG
  220. wdt_reset();
  221. #endif //WATCHDOG
  222. if(temp_meas_ready == true) { // temp sample ready
  223. updateTemperatures();
  224. input = (extruder<0)?current_temperature_bed:current_temperature[extruder];
  225. max=max(max,input);
  226. min=min(min,input);
  227. #if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1)
  228. if(_millis() - extruder_autofan_last_check > 2500) {
  229. checkExtruderAutoFans();
  230. extruder_autofan_last_check = _millis();
  231. }
  232. #endif
  233. if(heating == true && input > temp) {
  234. if(_millis() - t2 > 5000) {
  235. heating=false;
  236. if (extruder<0)
  237. {
  238. soft_pwm_bed = (bias - d) >> 1;
  239. timer02_set_pwm0(soft_pwm_bed << 1);
  240. }
  241. else
  242. soft_pwm[extruder] = (bias - d) >> 1;
  243. t1=_millis();
  244. t_high=t1 - t2;
  245. max=temp;
  246. }
  247. }
  248. if(heating == false && input < temp) {
  249. if(_millis() - t1 > 5000) {
  250. heating=true;
  251. t2=_millis();
  252. t_low=t2 - t1;
  253. if(pid_cycle > 0) {
  254. bias += (d*(t_high - t_low))/(t_low + t_high);
  255. bias = constrain(bias, 20 ,(extruder<0?(MAX_BED_POWER):(PID_MAX))-20);
  256. if(bias > (extruder<0?(MAX_BED_POWER):(PID_MAX))/2) d = (extruder<0?(MAX_BED_POWER):(PID_MAX)) - 1 - bias;
  257. else d = bias;
  258. SERIAL_PROTOCOLPGM(" bias: "); SERIAL_PROTOCOL(bias);
  259. SERIAL_PROTOCOLPGM(" d: "); SERIAL_PROTOCOL(d);
  260. SERIAL_PROTOCOLPGM(" min: "); SERIAL_PROTOCOL(min);
  261. SERIAL_PROTOCOLPGM(" max: "); SERIAL_PROTOCOLLN(max);
  262. if(pid_cycle > 2) {
  263. Ku = (4.0*d)/(3.14159*(max-min)/2.0);
  264. Tu = ((float)(t_low + t_high)/1000.0);
  265. SERIAL_PROTOCOLPGM(" Ku: "); SERIAL_PROTOCOL(Ku);
  266. SERIAL_PROTOCOLPGM(" Tu: "); SERIAL_PROTOCOLLN(Tu);
  267. _Kp = 0.6*Ku;
  268. _Ki = 2*_Kp/Tu;
  269. _Kd = _Kp*Tu/8;
  270. SERIAL_PROTOCOLLNPGM(" Classic PID ");
  271. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(_Kp);
  272. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(_Ki);
  273. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(_Kd);
  274. /*
  275. _Kp = 0.33*Ku;
  276. _Ki = _Kp/Tu;
  277. _Kd = _Kp*Tu/3;
  278. SERIAL_PROTOCOLLNPGM(" Some overshoot ");
  279. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(_Kp);
  280. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(_Ki);
  281. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(_Kd);
  282. _Kp = 0.2*Ku;
  283. _Ki = 2*_Kp/Tu;
  284. _Kd = _Kp*Tu/3;
  285. SERIAL_PROTOCOLLNPGM(" No overshoot ");
  286. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(_Kp);
  287. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(_Ki);
  288. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(_Kd);
  289. */
  290. }
  291. }
  292. if (extruder<0)
  293. {
  294. soft_pwm_bed = (bias + d) >> 1;
  295. timer02_set_pwm0(soft_pwm_bed << 1);
  296. }
  297. else
  298. soft_pwm[extruder] = (bias + d) >> 1;
  299. pid_cycle++;
  300. min=temp;
  301. }
  302. }
  303. }
  304. if(input > (temp + 20)) {
  305. SERIAL_PROTOCOLLNPGM("PID Autotune failed! Temperature too high");
  306. pid_tuning_finished = true;
  307. pid_cycle = 0;
  308. return;
  309. }
  310. if(_millis() - temp_millis > 2000) {
  311. int p;
  312. if (extruder<0){
  313. p=soft_pwm_bed;
  314. SERIAL_PROTOCOLPGM("B:");
  315. }else{
  316. p=soft_pwm[extruder];
  317. SERIAL_PROTOCOLPGM("T:");
  318. }
  319. SERIAL_PROTOCOL(input);
  320. SERIAL_PROTOCOLPGM(" @:");
  321. SERIAL_PROTOCOLLN(p);
  322. if (safety_check_cycles == 0) { //save ambient temp
  323. temp_ambient = input;
  324. //SERIAL_ECHOPGM("Ambient T: ");
  325. //MYSERIAL.println(temp_ambient);
  326. safety_check_cycles++;
  327. }
  328. else if (safety_check_cycles < safety_check_cycles_count) { //delay
  329. safety_check_cycles++;
  330. }
  331. else if (safety_check_cycles == safety_check_cycles_count){ //check that temperature is rising
  332. safety_check_cycles++;
  333. //SERIAL_ECHOPGM("Time from beginning: ");
  334. //MYSERIAL.print(safety_check_cycles_count * 2);
  335. //SERIAL_ECHOPGM("s. Difference between current and ambient T: ");
  336. //MYSERIAL.println(input - temp_ambient);
  337. if (fabs(input - temp_ambient) < 5.0) {
  338. temp_runaway_stop(false, (extruder<0));
  339. pid_tuning_finished = true;
  340. return;
  341. }
  342. }
  343. temp_millis = _millis();
  344. }
  345. if(((_millis() - t1) + (_millis() - t2)) > (10L*60L*1000L*2L)) {
  346. SERIAL_PROTOCOLLNPGM("PID Autotune failed! timeout");
  347. pid_tuning_finished = true;
  348. pid_cycle = 0;
  349. return;
  350. }
  351. if(pid_cycle > ncycles) {
  352. SERIAL_PROTOCOLLNPGM("PID Autotune finished! Put the last Kp, Ki and Kd constants from above into Configuration.h");
  353. pid_tuning_finished = true;
  354. pid_cycle = 0;
  355. return;
  356. }
  357. lcd_update(0);
  358. }
  359. }
  360. void updatePID()
  361. {
  362. // TODO: iState_sum_max and PID values should be synchronized for temp_mgr_isr
  363. #ifdef PIDTEMP
  364. for(uint_least8_t e = 0; e < EXTRUDERS; e++) {
  365. iState_sum_max[e] = PID_INTEGRAL_DRIVE_MAX / cs.Ki;
  366. }
  367. #endif
  368. #ifdef PIDTEMPBED
  369. temp_iState_max_bed = PID_INTEGRAL_DRIVE_MAX / cs.bedKi;
  370. #endif
  371. }
  372. int getHeaterPower(int heater) {
  373. if (heater<0)
  374. return soft_pwm_bed;
  375. return soft_pwm[heater];
  376. }
  377. // reset PID state after changing target_temperature
  378. void resetPID(uint8_t extruder _UNUSED) {}
  379. enum class TempErrorSource : uint8_t
  380. {
  381. hotend,
  382. bed,
  383. ambient,
  384. };
  385. enum class TempErrorType : uint8_t
  386. {
  387. min,
  388. max,
  389. preheat,
  390. runaway,
  391. };
  392. // error state (updated via set_temp_error from isr context)
  393. volatile static union
  394. {
  395. uint8_t v;
  396. struct
  397. {
  398. uint8_t error: 1; // error condition
  399. uint8_t assert: 1; // error is still asserted
  400. uint8_t source: 2; // source
  401. uint8_t index: 1; // source index
  402. uint8_t type: 3; // error type
  403. };
  404. } temp_error_state;
  405. // set the error type from within the temp_mgr isr to be handled in manager_heater
  406. // - immediately disable all heaters and turn on all fans at full speed
  407. // - prevent the user to set temperatures until all errors are cleared
  408. void set_temp_error(TempErrorSource source, uint8_t index, TempErrorType type)
  409. {
  410. // keep disabling heaters and keep fans on as long as the condition is asserted
  411. disable_heater();
  412. hotendFanSetFullSpeed();
  413. // set the error state
  414. temp_error_state.error = true;
  415. temp_error_state.assert = true;
  416. temp_error_state.source = (uint8_t)source;
  417. temp_error_state.index = index;
  418. temp_error_state.type = (uint8_t)type;
  419. }
  420. void handle_temp_error();
  421. void manage_heater()
  422. {
  423. #ifdef WATCHDOG
  424. wdt_reset();
  425. #endif //WATCHDOG
  426. // limit execution to the same rate as temp_mgr (low-level fault handling is already handled -
  427. // any remaining error handling is just user-facing and can wait one extra cycle)
  428. if(!temp_meas_ready)
  429. return;
  430. // syncronize temperatures with isr
  431. updateTemperatures();
  432. // handle temperature errors
  433. if(temp_error_state.v)
  434. handle_temp_error();
  435. // periodically check fans
  436. checkFans();
  437. }
  438. #define PGM_RD_W(x) (short)pgm_read_word(&x)
  439. // Derived from RepRap FiveD extruder::getTemperature()
  440. // For hot end temperature measurement.
  441. static float analog2temp(int raw, uint8_t e) {
  442. if(e >= EXTRUDERS)
  443. {
  444. SERIAL_ERROR_START;
  445. SERIAL_ERROR((int)e);
  446. SERIAL_ERRORLNPGM(" - Invalid extruder number !");
  447. kill(NULL, 6);
  448. return 0.0;
  449. }
  450. #ifdef HEATER_0_USES_MAX6675
  451. if (e == 0)
  452. {
  453. return 0.25 * raw;
  454. }
  455. #endif
  456. if(heater_ttbl_map[e] != NULL)
  457. {
  458. float celsius = 0;
  459. uint8_t i;
  460. short (*tt)[][2] = (short (*)[][2])(heater_ttbl_map[e]);
  461. for (i=1; i<heater_ttbllen_map[e]; i++)
  462. {
  463. if (PGM_RD_W((*tt)[i][0]) > raw)
  464. {
  465. celsius = PGM_RD_W((*tt)[i-1][1]) +
  466. (raw - PGM_RD_W((*tt)[i-1][0])) *
  467. (float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i-1][1])) /
  468. (float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i-1][0]));
  469. break;
  470. }
  471. }
  472. // Overflow: Set to last value in the table
  473. if (i == heater_ttbllen_map[e]) celsius = PGM_RD_W((*tt)[i-1][1]);
  474. return celsius;
  475. }
  476. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  477. }
  478. // Derived from RepRap FiveD extruder::getTemperature()
  479. // For bed temperature measurement.
  480. static float analog2tempBed(int raw) {
  481. #ifdef BED_USES_THERMISTOR
  482. float celsius = 0;
  483. byte i;
  484. for (i=1; i<BEDTEMPTABLE_LEN; i++)
  485. {
  486. if (PGM_RD_W(BEDTEMPTABLE[i][0]) > raw)
  487. {
  488. celsius = PGM_RD_W(BEDTEMPTABLE[i-1][1]) +
  489. (raw - PGM_RD_W(BEDTEMPTABLE[i-1][0])) *
  490. (float)(PGM_RD_W(BEDTEMPTABLE[i][1]) - PGM_RD_W(BEDTEMPTABLE[i-1][1])) /
  491. (float)(PGM_RD_W(BEDTEMPTABLE[i][0]) - PGM_RD_W(BEDTEMPTABLE[i-1][0]));
  492. break;
  493. }
  494. }
  495. // Overflow: Set to last value in the table
  496. if (i == BEDTEMPTABLE_LEN) celsius = PGM_RD_W(BEDTEMPTABLE[i-1][1]);
  497. // temperature offset adjustment
  498. #ifdef BED_OFFSET
  499. float _offset = BED_OFFSET;
  500. float _offset_center = BED_OFFSET_CENTER;
  501. float _offset_start = BED_OFFSET_START;
  502. float _first_koef = (_offset / 2) / (_offset_center - _offset_start);
  503. float _second_koef = (_offset / 2) / (100 - _offset_center);
  504. if (celsius >= _offset_start && celsius <= _offset_center)
  505. {
  506. celsius = celsius + (_first_koef * (celsius - _offset_start));
  507. }
  508. else if (celsius > _offset_center && celsius <= 100)
  509. {
  510. celsius = celsius + (_first_koef * (_offset_center - _offset_start)) + ( _second_koef * ( celsius - ( 100 - _offset_center ) )) ;
  511. }
  512. else if (celsius > 100)
  513. {
  514. celsius = celsius + _offset;
  515. }
  516. #endif
  517. return celsius;
  518. #elif defined BED_USES_AD595
  519. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  520. #else
  521. return 0;
  522. #endif
  523. }
  524. #ifdef AMBIENT_THERMISTOR
  525. static float analog2tempAmbient(int raw)
  526. {
  527. float celsius = 0;
  528. byte i;
  529. for (i=1; i<AMBIENTTEMPTABLE_LEN; i++)
  530. {
  531. if (PGM_RD_W(AMBIENTTEMPTABLE[i][0]) > raw)
  532. {
  533. celsius = PGM_RD_W(AMBIENTTEMPTABLE[i-1][1]) +
  534. (raw - PGM_RD_W(AMBIENTTEMPTABLE[i-1][0])) *
  535. (float)(PGM_RD_W(AMBIENTTEMPTABLE[i][1]) - PGM_RD_W(AMBIENTTEMPTABLE[i-1][1])) /
  536. (float)(PGM_RD_W(AMBIENTTEMPTABLE[i][0]) - PGM_RD_W(AMBIENTTEMPTABLE[i-1][0]));
  537. break;
  538. }
  539. }
  540. // Overflow: Set to last value in the table
  541. if (i == AMBIENTTEMPTABLE_LEN) celsius = PGM_RD_W(AMBIENTTEMPTABLE[i-1][1]);
  542. return celsius;
  543. }
  544. #endif //AMBIENT_THERMISTOR
  545. void soft_pwm_init()
  546. {
  547. #if MB(RUMBA) && ((TEMP_SENSOR_0==-1)||(TEMP_SENSOR_1==-1)||(TEMP_SENSOR_2==-1)||(TEMP_SENSOR_BED==-1))
  548. //disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector
  549. MCUCR=(1<<JTD);
  550. MCUCR=(1<<JTD);
  551. #endif
  552. // Finish init of mult extruder arrays
  553. for(int e = 0; e < EXTRUDERS; e++) {
  554. // populate with the first value
  555. maxttemp[e] = maxttemp[0];
  556. #ifdef PIDTEMP
  557. iState_sum_min[e] = 0.0;
  558. iState_sum_max[e] = PID_INTEGRAL_DRIVE_MAX / cs.Ki;
  559. #endif //PIDTEMP
  560. #ifdef PIDTEMPBED
  561. temp_iState_min_bed = 0.0;
  562. temp_iState_max_bed = PID_INTEGRAL_DRIVE_MAX / cs.bedKi;
  563. #endif //PIDTEMPBED
  564. }
  565. #if defined(HEATER_0_PIN) && (HEATER_0_PIN > -1)
  566. SET_OUTPUT(HEATER_0_PIN);
  567. #endif
  568. #if defined(HEATER_1_PIN) && (HEATER_1_PIN > -1)
  569. SET_OUTPUT(HEATER_1_PIN);
  570. #endif
  571. #if defined(HEATER_2_PIN) && (HEATER_2_PIN > -1)
  572. SET_OUTPUT(HEATER_2_PIN);
  573. #endif
  574. #if defined(HEATER_BED_PIN) && (HEATER_BED_PIN > -1)
  575. SET_OUTPUT(HEATER_BED_PIN);
  576. #endif
  577. #if defined(FAN_PIN) && (FAN_PIN > -1)
  578. SET_OUTPUT(FAN_PIN);
  579. #ifdef FAST_PWM_FAN
  580. setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  581. #endif
  582. #ifdef FAN_SOFT_PWM
  583. soft_pwm_fan = fanSpeedSoftPwm / (1 << (8 - FAN_SOFT_PWM_BITS));
  584. #endif
  585. #endif
  586. #ifdef HEATER_0_USES_MAX6675
  587. #ifndef SDSUPPORT
  588. SET_OUTPUT(SCK_PIN);
  589. WRITE(SCK_PIN,0);
  590. SET_OUTPUT(MOSI_PIN);
  591. WRITE(MOSI_PIN,1);
  592. SET_INPUT(MISO_PIN);
  593. WRITE(MISO_PIN,1);
  594. #endif
  595. /* Using pinMode and digitalWrite, as that was the only way I could get it to compile */
  596. //Have to toggle SD card CS pin to low first, to enable firmware to talk with SD card
  597. pinMode(SS_PIN, OUTPUT);
  598. digitalWrite(SS_PIN,0);
  599. pinMode(MAX6675_SS, OUTPUT);
  600. digitalWrite(MAX6675_SS,1);
  601. #endif
  602. timer0_init(); //enables the heatbed timer.
  603. // timer2 already enabled earlier in the code
  604. // now enable the COMPB temperature interrupt
  605. OCR2B = 128;
  606. ENABLE_SOFT_PWM_INTERRUPT();
  607. timer4_init(); //for tone and Extruder fan PWM
  608. #ifdef HEATER_0_MINTEMP
  609. minttemp[0] = HEATER_0_MINTEMP;
  610. while(analog2temp(minttemp_raw[0], 0) < HEATER_0_MINTEMP) {
  611. #if HEATER_0_RAW_LO_TEMP < HEATER_0_RAW_HI_TEMP
  612. minttemp_raw[0] += OVERSAMPLENR;
  613. #else
  614. minttemp_raw[0] -= OVERSAMPLENR;
  615. #endif
  616. }
  617. #endif //MINTEMP
  618. #ifdef HEATER_0_MAXTEMP
  619. maxttemp[0] = HEATER_0_MAXTEMP;
  620. while(analog2temp(maxttemp_raw[0], 0) > HEATER_0_MAXTEMP) {
  621. #if HEATER_0_RAW_LO_TEMP < HEATER_0_RAW_HI_TEMP
  622. maxttemp_raw[0] -= OVERSAMPLENR;
  623. #else
  624. maxttemp_raw[0] += OVERSAMPLENR;
  625. #endif
  626. }
  627. #endif //MAXTEMP
  628. #if (EXTRUDERS > 1) && defined(HEATER_1_MINTEMP)
  629. minttemp[1] = HEATER_1_MINTEMP;
  630. while(analog2temp(minttemp_raw[1], 1) < HEATER_1_MINTEMP) {
  631. #if HEATER_1_RAW_LO_TEMP < HEATER_1_RAW_HI_TEMP
  632. minttemp_raw[1] += OVERSAMPLENR;
  633. #else
  634. minttemp_raw[1] -= OVERSAMPLENR;
  635. #endif
  636. }
  637. #endif // MINTEMP 1
  638. #if (EXTRUDERS > 1) && defined(HEATER_1_MAXTEMP)
  639. maxttemp[1] = HEATER_1_MAXTEMP;
  640. while(analog2temp(maxttemp_raw[1], 1) > HEATER_1_MAXTEMP) {
  641. #if HEATER_1_RAW_LO_TEMP < HEATER_1_RAW_HI_TEMP
  642. maxttemp_raw[1] -= OVERSAMPLENR;
  643. #else
  644. maxttemp_raw[1] += OVERSAMPLENR;
  645. #endif
  646. }
  647. #endif //MAXTEMP 1
  648. #if (EXTRUDERS > 2) && defined(HEATER_2_MINTEMP)
  649. minttemp[2] = HEATER_2_MINTEMP;
  650. while(analog2temp(minttemp_raw[2], 2) < HEATER_2_MINTEMP) {
  651. #if HEATER_2_RAW_LO_TEMP < HEATER_2_RAW_HI_TEMP
  652. minttemp_raw[2] += OVERSAMPLENR;
  653. #else
  654. minttemp_raw[2] -= OVERSAMPLENR;
  655. #endif
  656. }
  657. #endif //MINTEMP 2
  658. #if (EXTRUDERS > 2) && defined(HEATER_2_MAXTEMP)
  659. maxttemp[2] = HEATER_2_MAXTEMP;
  660. while(analog2temp(maxttemp_raw[2], 2) > HEATER_2_MAXTEMP) {
  661. #if HEATER_2_RAW_LO_TEMP < HEATER_2_RAW_HI_TEMP
  662. maxttemp_raw[2] -= OVERSAMPLENR;
  663. #else
  664. maxttemp_raw[2] += OVERSAMPLENR;
  665. #endif
  666. }
  667. #endif //MAXTEMP 2
  668. #ifdef BED_MINTEMP
  669. while(analog2tempBed(bed_minttemp_raw) < BED_MINTEMP) {
  670. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  671. bed_minttemp_raw += OVERSAMPLENR;
  672. #else
  673. bed_minttemp_raw -= OVERSAMPLENR;
  674. #endif
  675. }
  676. #endif //BED_MINTEMP
  677. #ifdef BED_MAXTEMP
  678. while(analog2tempBed(bed_maxttemp_raw) > BED_MAXTEMP) {
  679. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  680. bed_maxttemp_raw -= OVERSAMPLENR;
  681. #else
  682. bed_maxttemp_raw += OVERSAMPLENR;
  683. #endif
  684. }
  685. #endif //BED_MAXTEMP
  686. #ifdef AMBIENT_MINTEMP
  687. while(analog2tempAmbient(ambient_minttemp_raw) < AMBIENT_MINTEMP) {
  688. #if AMBIENT_RAW_LO_TEMP < AMBIENT_RAW_HI_TEMP
  689. ambient_minttemp_raw += OVERSAMPLENR;
  690. #else
  691. ambient_minttemp_raw -= OVERSAMPLENR;
  692. #endif
  693. }
  694. #endif //AMBIENT_MINTEMP
  695. #ifdef AMBIENT_MAXTEMP
  696. while(analog2tempAmbient(ambient_maxttemp_raw) > AMBIENT_MAXTEMP) {
  697. #if AMBIENT_RAW_LO_TEMP < AMBIENT_RAW_HI_TEMP
  698. ambient_maxttemp_raw -= OVERSAMPLENR;
  699. #else
  700. ambient_maxttemp_raw += OVERSAMPLENR;
  701. #endif
  702. }
  703. #endif //AMBIENT_MAXTEMP
  704. }
  705. #if (defined (TEMP_RUNAWAY_BED_HYSTERESIS) && TEMP_RUNAWAY_BED_TIMEOUT > 0) || (defined (TEMP_RUNAWAY_EXTRUDER_HYSTERESIS) && TEMP_RUNAWAY_EXTRUDER_TIMEOUT > 0)
  706. void temp_runaway_check(uint8_t _heater_id, float _target_temperature, float _current_temperature, float _output, bool _isbed)
  707. {
  708. float __delta;
  709. float __hysteresis = 0;
  710. uint16_t __timeout = 0;
  711. bool temp_runaway_check_active = false;
  712. static float __preheat_start[2] = { 0,0}; //currently just bed and one extruder
  713. static uint8_t __preheat_counter[2] = { 0,0};
  714. static uint8_t __preheat_errors[2] = { 0,0};
  715. if (_millis() - temp_runaway_timer[_heater_id] > 2000)
  716. {
  717. #ifdef TEMP_RUNAWAY_BED_TIMEOUT
  718. if (_isbed)
  719. {
  720. __hysteresis = TEMP_RUNAWAY_BED_HYSTERESIS;
  721. __timeout = TEMP_RUNAWAY_BED_TIMEOUT;
  722. }
  723. #endif
  724. #ifdef TEMP_RUNAWAY_EXTRUDER_TIMEOUT
  725. if (!_isbed)
  726. {
  727. __hysteresis = TEMP_RUNAWAY_EXTRUDER_HYSTERESIS;
  728. __timeout = TEMP_RUNAWAY_EXTRUDER_TIMEOUT;
  729. }
  730. #endif
  731. temp_runaway_timer[_heater_id] = _millis();
  732. if (_output == 0)
  733. {
  734. temp_runaway_check_active = false;
  735. temp_runaway_error_counter[_heater_id] = 0;
  736. }
  737. if (temp_runaway_target[_heater_id] != _target_temperature)
  738. {
  739. if (_target_temperature > 0)
  740. {
  741. temp_runaway_status[_heater_id] = TempRunaway_PREHEAT;
  742. temp_runaway_target[_heater_id] = _target_temperature;
  743. __preheat_start[_heater_id] = _current_temperature;
  744. __preheat_counter[_heater_id] = 0;
  745. }
  746. else
  747. {
  748. temp_runaway_status[_heater_id] = TempRunaway_INACTIVE;
  749. temp_runaway_target[_heater_id] = _target_temperature;
  750. }
  751. }
  752. if ((_current_temperature < _target_temperature) && (temp_runaway_status[_heater_id] == TempRunaway_PREHEAT))
  753. {
  754. __preheat_counter[_heater_id]++;
  755. if (__preheat_counter[_heater_id] > ((_isbed) ? 16 : 8)) // periodicaly check if current temperature changes
  756. {
  757. /*SERIAL_ECHOPGM("Heater:");
  758. MYSERIAL.print(_heater_id);
  759. SERIAL_ECHOPGM(" T:");
  760. MYSERIAL.print(_current_temperature);
  761. SERIAL_ECHOPGM(" Tstart:");
  762. MYSERIAL.print(__preheat_start[_heater_id]);
  763. SERIAL_ECHOPGM(" delta:");
  764. MYSERIAL.print(_current_temperature-__preheat_start[_heater_id]);*/
  765. //-// if (_current_temperature - __preheat_start[_heater_id] < 2) {
  766. //-// if (_current_temperature - __preheat_start[_heater_id] < ((_isbed && (_current_temperature>105.0))?0.6:2.0)) {
  767. __delta=2.0;
  768. if(_isbed)
  769. {
  770. __delta=3.0;
  771. if(_current_temperature>90.0) __delta=2.0;
  772. if(_current_temperature>105.0) __delta=0.6;
  773. }
  774. if (_current_temperature - __preheat_start[_heater_id] < __delta) {
  775. __preheat_errors[_heater_id]++;
  776. /*SERIAL_ECHOPGM(" Preheat errors:");
  777. MYSERIAL.println(__preheat_errors[_heater_id]);*/
  778. }
  779. else {
  780. //SERIAL_ECHOLNPGM("");
  781. __preheat_errors[_heater_id] = 0;
  782. }
  783. if (__preheat_errors[_heater_id] > ((_isbed) ? 3 : 5))
  784. set_temp_error((_isbed?TempErrorSource::bed:TempErrorSource::hotend), _heater_id, TempErrorType::preheat);
  785. __preheat_start[_heater_id] = _current_temperature;
  786. __preheat_counter[_heater_id] = 0;
  787. }
  788. }
  789. //-// if (_current_temperature >= _target_temperature && temp_runaway_status[_heater_id] == TempRunaway_PREHEAT)
  790. if ((_current_temperature > (_target_temperature - __hysteresis)) && temp_runaway_status[_heater_id] == TempRunaway_PREHEAT)
  791. {
  792. /*SERIAL_ECHOPGM("Heater:");
  793. MYSERIAL.print(_heater_id);
  794. MYSERIAL.println(" ->tempRunaway");*/
  795. temp_runaway_status[_heater_id] = TempRunaway_ACTIVE;
  796. temp_runaway_check_active = false;
  797. temp_runaway_error_counter[_heater_id] = 0;
  798. }
  799. if (_output > 0)
  800. {
  801. temp_runaway_check_active = true;
  802. }
  803. if (temp_runaway_check_active)
  804. {
  805. // we are in range
  806. if ((_current_temperature > (_target_temperature - __hysteresis)) && (_current_temperature < (_target_temperature + __hysteresis)))
  807. {
  808. temp_runaway_check_active = false;
  809. temp_runaway_error_counter[_heater_id] = 0;
  810. }
  811. else
  812. {
  813. if (temp_runaway_status[_heater_id] > TempRunaway_PREHEAT)
  814. {
  815. temp_runaway_error_counter[_heater_id]++;
  816. if (temp_runaway_error_counter[_heater_id] * 2 > __timeout)
  817. set_temp_error((_isbed?TempErrorSource::bed:TempErrorSource::hotend), _heater_id, TempErrorType::runaway);
  818. }
  819. }
  820. }
  821. }
  822. }
  823. void temp_runaway_stop(bool isPreheat, bool isBed)
  824. {
  825. disable_heater();
  826. Sound_MakeCustom(200,0,true);
  827. if (isPreheat)
  828. {
  829. lcd_setalertstatuspgm(isBed? PSTR("BED PREHEAT ERROR") : PSTR("PREHEAT ERROR"), LCD_STATUS_CRITICAL);
  830. SERIAL_ERROR_START;
  831. isBed ? SERIAL_ERRORLNPGM(" THERMAL RUNAWAY (PREHEAT HEATBED)") : SERIAL_ERRORLNPGM(" THERMAL RUNAWAY (PREHEAT HOTEND)");
  832. hotendFanSetFullSpeed();
  833. }
  834. else
  835. {
  836. lcd_setalertstatuspgm(isBed? PSTR("BED THERMAL RUNAWAY") : PSTR("THERMAL RUNAWAY"), LCD_STATUS_CRITICAL);
  837. SERIAL_ERROR_START;
  838. isBed ? SERIAL_ERRORLNPGM(" HEATBED THERMAL RUNAWAY") : SERIAL_ERRORLNPGM(" HOTEND THERMAL RUNAWAY");
  839. }
  840. Stop();
  841. if (farm_mode) {
  842. prusa_statistics(0);
  843. prusa_statistics(isPreheat? 91 : 90);
  844. }
  845. }
  846. #endif
  847. //! codes of alert messages for the LCD - it is shorter to compare an uin8_t
  848. //! than raw const char * of the messages themselves.
  849. //! Could be used for MAXTEMP situations too - after reaching MAXTEMP and turning off the heater automagically
  850. //! the heater/bed may cool down and a similar alert message like "MAXTERM fixed..." may be displayed.
  851. enum { LCDALERT_NONE = 0, LCDALERT_HEATERMINTEMP, LCDALERT_BEDMINTEMP, LCDALERT_MINTEMPFIXED, LCDALERT_PLEASERESTART };
  852. //! remember the last alert message sent to the LCD
  853. //! to prevent flicker and improve speed
  854. uint8_t last_alert_sent_to_lcd = LCDALERT_NONE;
  855. //! update the current temperature error message
  856. //! @param type short error abbreviation (PROGMEM)
  857. void temp_update_messagepgm(const char* PROGMEM type)
  858. {
  859. char msg[LCD_WIDTH];
  860. strcpy_P(msg, PSTR("Err: "));
  861. strcat_P(msg, type);
  862. lcd_setalertstatus(msg, LCD_STATUS_CRITICAL);
  863. }
  864. //! signal a temperature error on both the lcd and serial
  865. //! @param type short error abbreviation (PROGMEM)
  866. //! @param e optional extruder index for hotend errors
  867. void temp_error_messagepgm(const char* PROGMEM type, uint8_t e = EXTRUDERS)
  868. {
  869. temp_update_messagepgm(type);
  870. SERIAL_ERROR_START;
  871. if(e != EXTRUDERS) {
  872. SERIAL_ERROR((int)e);
  873. SERIAL_ERRORPGM(": ");
  874. }
  875. SERIAL_ERRORPGM("Heaters switched off. ");
  876. SERIAL_ERRORRPGM(type);
  877. SERIAL_ERRORLNPGM(" triggered!");
  878. }
  879. void max_temp_error(uint8_t e) {
  880. disable_heater();
  881. if(IsStopped() == false) {
  882. temp_error_messagepgm(PSTR("MAXTEMP"), e);
  883. }
  884. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  885. Stop();
  886. #endif
  887. hotendFanSetFullSpeed();
  888. if (farm_mode) { prusa_statistics(93); }
  889. }
  890. void min_temp_error(uint8_t e) {
  891. #ifdef DEBUG_DISABLE_MINTEMP
  892. return;
  893. #endif
  894. disable_heater();
  895. //if (current_temperature_ambient < MINTEMP_MINAMBIENT) return;
  896. static const char err[] PROGMEM = "MINTEMP";
  897. if(IsStopped() == false) {
  898. temp_error_messagepgm(err, e);
  899. last_alert_sent_to_lcd = LCDALERT_HEATERMINTEMP;
  900. } else if( last_alert_sent_to_lcd != LCDALERT_HEATERMINTEMP ){ // only update, if the lcd message is to be changed (i.e. not the same as last time)
  901. // we are already stopped due to some error, only update the status message without flickering
  902. temp_update_messagepgm(err);
  903. last_alert_sent_to_lcd = LCDALERT_HEATERMINTEMP;
  904. }
  905. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  906. // if( last_alert_sent_to_lcd != LCDALERT_HEATERMINTEMP ){
  907. // last_alert_sent_to_lcd = LCDALERT_HEATERMINTEMP;
  908. // lcd_print_stop();
  909. // }
  910. Stop();
  911. #endif
  912. if (farm_mode) { prusa_statistics(92); }
  913. }
  914. void bed_max_temp_error(void) {
  915. disable_heater();
  916. if(IsStopped() == false) {
  917. temp_error_messagepgm(PSTR("MAXTEMP BED"));
  918. }
  919. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  920. Stop();
  921. #endif
  922. }
  923. void bed_min_temp_error(void) {
  924. #ifdef DEBUG_DISABLE_MINTEMP
  925. return;
  926. #endif
  927. disable_heater();
  928. static const char err[] PROGMEM = "MINTEMP BED";
  929. if(IsStopped() == false) {
  930. temp_error_messagepgm(err);
  931. last_alert_sent_to_lcd = LCDALERT_BEDMINTEMP;
  932. } else if( last_alert_sent_to_lcd != LCDALERT_BEDMINTEMP ){ // only update, if the lcd message is to be changed (i.e. not the same as last time)
  933. // we are already stopped due to some error, only update the status message without flickering
  934. temp_update_messagepgm(err);
  935. last_alert_sent_to_lcd = LCDALERT_BEDMINTEMP;
  936. }
  937. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  938. Stop();
  939. #endif
  940. }
  941. #ifdef AMBIENT_THERMISTOR
  942. void ambient_max_temp_error(void) {
  943. disable_heater();
  944. if(IsStopped() == false) {
  945. temp_error_messagepgm(PSTR("MAXTEMP AMB"));
  946. }
  947. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  948. Stop();
  949. #endif
  950. }
  951. void ambient_min_temp_error(void) {
  952. #ifdef DEBUG_DISABLE_MINTEMP
  953. return;
  954. #endif
  955. disable_heater();
  956. if(IsStopped() == false) {
  957. temp_error_messagepgm(PSTR("MINTEMP AMB"));
  958. }
  959. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  960. Stop();
  961. #endif
  962. }
  963. #endif
  964. #ifdef HEATER_0_USES_MAX6675
  965. #define MAX6675_HEAT_INTERVAL 250
  966. long max6675_previous_millis = MAX6675_HEAT_INTERVAL;
  967. int max6675_temp = 2000;
  968. int read_max6675()
  969. {
  970. if (_millis() - max6675_previous_millis < MAX6675_HEAT_INTERVAL)
  971. return max6675_temp;
  972. max6675_previous_millis = _millis();
  973. max6675_temp = 0;
  974. #ifdef PRR
  975. PRR &= ~(1<<PRSPI);
  976. #elif defined PRR0
  977. PRR0 &= ~(1<<PRSPI);
  978. #endif
  979. SPCR = (1<<MSTR) | (1<<SPE) | (1<<SPR0);
  980. // enable TT_MAX6675
  981. WRITE(MAX6675_SS, 0);
  982. // ensure 100ns delay - a bit extra is fine
  983. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  984. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  985. // read MSB
  986. SPDR = 0;
  987. for (;(SPSR & (1<<SPIF)) == 0;);
  988. max6675_temp = SPDR;
  989. max6675_temp <<= 8;
  990. // read LSB
  991. SPDR = 0;
  992. for (;(SPSR & (1<<SPIF)) == 0;);
  993. max6675_temp |= SPDR;
  994. // disable TT_MAX6675
  995. WRITE(MAX6675_SS, 1);
  996. if (max6675_temp & 4)
  997. {
  998. // thermocouple open
  999. max6675_temp = 2000;
  1000. }
  1001. else
  1002. {
  1003. max6675_temp = max6675_temp >> 3;
  1004. }
  1005. return max6675_temp;
  1006. }
  1007. #endif
  1008. #ifdef BABYSTEPPING
  1009. FORCE_INLINE static void applyBabysteps() {
  1010. for(uint8_t axis=0;axis<3;axis++)
  1011. {
  1012. int curTodo=babystepsTodo[axis]; //get rid of volatile for performance
  1013. if(curTodo>0)
  1014. {
  1015. CRITICAL_SECTION_START;
  1016. babystep(axis,/*fwd*/true);
  1017. babystepsTodo[axis]--; //less to do next time
  1018. CRITICAL_SECTION_END;
  1019. }
  1020. else
  1021. if(curTodo<0)
  1022. {
  1023. CRITICAL_SECTION_START;
  1024. babystep(axis,/*fwd*/false);
  1025. babystepsTodo[axis]++; //less to do next time
  1026. CRITICAL_SECTION_END;
  1027. }
  1028. }
  1029. }
  1030. #endif //BABYSTEPPING
  1031. FORCE_INLINE static void soft_pwm_core()
  1032. {
  1033. static uint8_t pwm_count = (1 << SOFT_PWM_SCALE);
  1034. static uint8_t soft_pwm_0;
  1035. #ifdef SLOW_PWM_HEATERS
  1036. static unsigned char slow_pwm_count = 0;
  1037. static unsigned char state_heater_0 = 0;
  1038. static unsigned char state_timer_heater_0 = 0;
  1039. #endif
  1040. #if (EXTRUDERS > 1) || defined(HEATERS_PARALLEL)
  1041. static unsigned char soft_pwm_1;
  1042. #ifdef SLOW_PWM_HEATERS
  1043. static unsigned char state_heater_1 = 0;
  1044. static unsigned char state_timer_heater_1 = 0;
  1045. #endif
  1046. #endif
  1047. #if EXTRUDERS > 2
  1048. static unsigned char soft_pwm_2;
  1049. #ifdef SLOW_PWM_HEATERS
  1050. static unsigned char state_heater_2 = 0;
  1051. static unsigned char state_timer_heater_2 = 0;
  1052. #endif
  1053. #endif
  1054. #if HEATER_BED_PIN > -1
  1055. // @@DR static unsigned char soft_pwm_b;
  1056. #ifdef SLOW_PWM_HEATERS
  1057. static unsigned char state_heater_b = 0;
  1058. static unsigned char state_timer_heater_b = 0;
  1059. #endif
  1060. #endif
  1061. #if defined(FILWIDTH_PIN) &&(FILWIDTH_PIN > -1)
  1062. static unsigned long raw_filwidth_value = 0; //added for filament width sensor
  1063. #endif
  1064. #ifndef SLOW_PWM_HEATERS
  1065. /*
  1066. * standard PWM modulation
  1067. */
  1068. if (pwm_count == 0)
  1069. {
  1070. soft_pwm_0 = soft_pwm[0];
  1071. if(soft_pwm_0 > 0)
  1072. {
  1073. WRITE(HEATER_0_PIN,1);
  1074. #ifdef HEATERS_PARALLEL
  1075. WRITE(HEATER_1_PIN,1);
  1076. #endif
  1077. } else WRITE(HEATER_0_PIN,0);
  1078. #if EXTRUDERS > 1
  1079. soft_pwm_1 = soft_pwm[1];
  1080. if(soft_pwm_1 > 0) WRITE(HEATER_1_PIN,1); else WRITE(HEATER_1_PIN,0);
  1081. #endif
  1082. #if EXTRUDERS > 2
  1083. soft_pwm_2 = soft_pwm[2];
  1084. if(soft_pwm_2 > 0) WRITE(HEATER_2_PIN,1); else WRITE(HEATER_2_PIN,0);
  1085. #endif
  1086. }
  1087. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1088. #if 0 // @@DR vypnuto pro hw pwm bedu
  1089. // tuhle prasarnu bude potreba poustet ve stanovenych intervalech, jinak nemam moc sanci zareagovat
  1090. // teoreticky by se tato cast uz vubec nemusela poustet
  1091. if ((pwm_count & ((1 << HEATER_BED_SOFT_PWM_BITS) - 1)) == 0)
  1092. {
  1093. soft_pwm_b = soft_pwm_bed >> (7 - HEATER_BED_SOFT_PWM_BITS);
  1094. # ifndef SYSTEM_TIMER_2
  1095. // tady budu krokovat pomalou frekvenci na automatu - tohle je rizeni spinani a rozepinani
  1096. // jako ridici frekvenci mam 2khz, jako vystupni frekvenci mam 30hz
  1097. // 2kHz jsou ovsem ve slysitelnem pasmu, mozna bude potreba jit s frekvenci nahoru (a tomu taky prizpusobit ostatni veci)
  1098. // Teoreticky bych mohl stahnout OCR0B citac na 6, cimz bych se dostal nekam ke 40khz a tady potom honit PWM rychleji nebo i pomaleji
  1099. // to nicemu nevadi. Soft PWM scale by se 20x zvetsilo (no dobre, 16x), cimz by se to posunulo k puvodnimu 30Hz PWM
  1100. //if(soft_pwm_b > 0) WRITE(HEATER_BED_PIN,1); else WRITE(HEATER_BED_PIN,0);
  1101. # endif //SYSTEM_TIMER_2
  1102. }
  1103. #endif
  1104. #endif
  1105. #ifdef FAN_SOFT_PWM
  1106. if ((pwm_count & ((1 << FAN_SOFT_PWM_BITS) - 1)) == 0)
  1107. {
  1108. soft_pwm_fan = fanSpeedSoftPwm / (1 << (8 - FAN_SOFT_PWM_BITS));
  1109. if(soft_pwm_fan > 0) WRITE(FAN_PIN,1); else WRITE(FAN_PIN,0);
  1110. }
  1111. #endif
  1112. if(soft_pwm_0 < pwm_count)
  1113. {
  1114. WRITE(HEATER_0_PIN,0);
  1115. #ifdef HEATERS_PARALLEL
  1116. WRITE(HEATER_1_PIN,0);
  1117. #endif
  1118. }
  1119. #if EXTRUDERS > 1
  1120. if(soft_pwm_1 < pwm_count) WRITE(HEATER_1_PIN,0);
  1121. #endif
  1122. #if EXTRUDERS > 2
  1123. if(soft_pwm_2 < pwm_count) WRITE(HEATER_2_PIN,0);
  1124. #endif
  1125. #if 0 // @@DR
  1126. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1127. if (soft_pwm_b < (pwm_count & ((1 << HEATER_BED_SOFT_PWM_BITS) - 1))){
  1128. //WRITE(HEATER_BED_PIN,0);
  1129. }
  1130. //WRITE(HEATER_BED_PIN, pwm_count & 1 );
  1131. #endif
  1132. #endif
  1133. #ifdef FAN_SOFT_PWM
  1134. if (soft_pwm_fan < (pwm_count & ((1 << FAN_SOFT_PWM_BITS) - 1))) WRITE(FAN_PIN,0);
  1135. #endif
  1136. pwm_count += (1 << SOFT_PWM_SCALE);
  1137. pwm_count &= 0x7f;
  1138. #else //ifndef SLOW_PWM_HEATERS
  1139. /*
  1140. * SLOW PWM HEATERS
  1141. *
  1142. * for heaters drived by relay
  1143. */
  1144. #ifndef MIN_STATE_TIME
  1145. #define MIN_STATE_TIME 16 // MIN_STATE_TIME * 65.5 = time in milliseconds
  1146. #endif
  1147. if (slow_pwm_count == 0) {
  1148. // EXTRUDER 0
  1149. soft_pwm_0 = soft_pwm[0];
  1150. if (soft_pwm_0 > 0) {
  1151. // turn ON heather only if the minimum time is up
  1152. if (state_timer_heater_0 == 0) {
  1153. // if change state set timer
  1154. if (state_heater_0 == 0) {
  1155. state_timer_heater_0 = MIN_STATE_TIME;
  1156. }
  1157. state_heater_0 = 1;
  1158. WRITE(HEATER_0_PIN, 1);
  1159. #ifdef HEATERS_PARALLEL
  1160. WRITE(HEATER_1_PIN, 1);
  1161. #endif
  1162. }
  1163. } else {
  1164. // turn OFF heather only if the minimum time is up
  1165. if (state_timer_heater_0 == 0) {
  1166. // if change state set timer
  1167. if (state_heater_0 == 1) {
  1168. state_timer_heater_0 = MIN_STATE_TIME;
  1169. }
  1170. state_heater_0 = 0;
  1171. WRITE(HEATER_0_PIN, 0);
  1172. #ifdef HEATERS_PARALLEL
  1173. WRITE(HEATER_1_PIN, 0);
  1174. #endif
  1175. }
  1176. }
  1177. #if EXTRUDERS > 1
  1178. // EXTRUDER 1
  1179. soft_pwm_1 = soft_pwm[1];
  1180. if (soft_pwm_1 > 0) {
  1181. // turn ON heather only if the minimum time is up
  1182. if (state_timer_heater_1 == 0) {
  1183. // if change state set timer
  1184. if (state_heater_1 == 0) {
  1185. state_timer_heater_1 = MIN_STATE_TIME;
  1186. }
  1187. state_heater_1 = 1;
  1188. WRITE(HEATER_1_PIN, 1);
  1189. }
  1190. } else {
  1191. // turn OFF heather only if the minimum time is up
  1192. if (state_timer_heater_1 == 0) {
  1193. // if change state set timer
  1194. if (state_heater_1 == 1) {
  1195. state_timer_heater_1 = MIN_STATE_TIME;
  1196. }
  1197. state_heater_1 = 0;
  1198. WRITE(HEATER_1_PIN, 0);
  1199. }
  1200. }
  1201. #endif
  1202. #if EXTRUDERS > 2
  1203. // EXTRUDER 2
  1204. soft_pwm_2 = soft_pwm[2];
  1205. if (soft_pwm_2 > 0) {
  1206. // turn ON heather only if the minimum time is up
  1207. if (state_timer_heater_2 == 0) {
  1208. // if change state set timer
  1209. if (state_heater_2 == 0) {
  1210. state_timer_heater_2 = MIN_STATE_TIME;
  1211. }
  1212. state_heater_2 = 1;
  1213. WRITE(HEATER_2_PIN, 1);
  1214. }
  1215. } else {
  1216. // turn OFF heather only if the minimum time is up
  1217. if (state_timer_heater_2 == 0) {
  1218. // if change state set timer
  1219. if (state_heater_2 == 1) {
  1220. state_timer_heater_2 = MIN_STATE_TIME;
  1221. }
  1222. state_heater_2 = 0;
  1223. WRITE(HEATER_2_PIN, 0);
  1224. }
  1225. }
  1226. #endif
  1227. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1228. // BED
  1229. soft_pwm_b = soft_pwm_bed;
  1230. if (soft_pwm_b > 0) {
  1231. // turn ON heather only if the minimum time is up
  1232. if (state_timer_heater_b == 0) {
  1233. // if change state set timer
  1234. if (state_heater_b == 0) {
  1235. state_timer_heater_b = MIN_STATE_TIME;
  1236. }
  1237. state_heater_b = 1;
  1238. //WRITE(HEATER_BED_PIN, 1);
  1239. }
  1240. } else {
  1241. // turn OFF heather only if the minimum time is up
  1242. if (state_timer_heater_b == 0) {
  1243. // if change state set timer
  1244. if (state_heater_b == 1) {
  1245. state_timer_heater_b = MIN_STATE_TIME;
  1246. }
  1247. state_heater_b = 0;
  1248. WRITE(HEATER_BED_PIN, 0);
  1249. }
  1250. }
  1251. #endif
  1252. } // if (slow_pwm_count == 0)
  1253. // EXTRUDER 0
  1254. if (soft_pwm_0 < slow_pwm_count) {
  1255. // turn OFF heather only if the minimum time is up
  1256. if (state_timer_heater_0 == 0) {
  1257. // if change state set timer
  1258. if (state_heater_0 == 1) {
  1259. state_timer_heater_0 = MIN_STATE_TIME;
  1260. }
  1261. state_heater_0 = 0;
  1262. WRITE(HEATER_0_PIN, 0);
  1263. #ifdef HEATERS_PARALLEL
  1264. WRITE(HEATER_1_PIN, 0);
  1265. #endif
  1266. }
  1267. }
  1268. #if EXTRUDERS > 1
  1269. // EXTRUDER 1
  1270. if (soft_pwm_1 < slow_pwm_count) {
  1271. // turn OFF heather only if the minimum time is up
  1272. if (state_timer_heater_1 == 0) {
  1273. // if change state set timer
  1274. if (state_heater_1 == 1) {
  1275. state_timer_heater_1 = MIN_STATE_TIME;
  1276. }
  1277. state_heater_1 = 0;
  1278. WRITE(HEATER_1_PIN, 0);
  1279. }
  1280. }
  1281. #endif
  1282. #if EXTRUDERS > 2
  1283. // EXTRUDER 2
  1284. if (soft_pwm_2 < slow_pwm_count) {
  1285. // turn OFF heather only if the minimum time is up
  1286. if (state_timer_heater_2 == 0) {
  1287. // if change state set timer
  1288. if (state_heater_2 == 1) {
  1289. state_timer_heater_2 = MIN_STATE_TIME;
  1290. }
  1291. state_heater_2 = 0;
  1292. WRITE(HEATER_2_PIN, 0);
  1293. }
  1294. }
  1295. #endif
  1296. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1297. // BED
  1298. if (soft_pwm_b < slow_pwm_count) {
  1299. // turn OFF heather only if the minimum time is up
  1300. if (state_timer_heater_b == 0) {
  1301. // if change state set timer
  1302. if (state_heater_b == 1) {
  1303. state_timer_heater_b = MIN_STATE_TIME;
  1304. }
  1305. state_heater_b = 0;
  1306. WRITE(HEATER_BED_PIN, 0);
  1307. }
  1308. }
  1309. #endif
  1310. #ifdef FAN_SOFT_PWM
  1311. if ((pwm_count & ((1 << FAN_SOFT_PWM_BITS) - 1)) == 0)
  1312. soft_pwm_fan = fanSpeedSoftPwm / (1 << (8 - FAN_SOFT_PWM_BITS));
  1313. if (soft_pwm_fan > 0) WRITE(FAN_PIN,1); else WRITE(FAN_PIN,0);
  1314. }
  1315. if (soft_pwm_fan < pwm_count) WRITE(FAN_PIN,0);
  1316. #endif
  1317. pwm_count += (1 << SOFT_PWM_SCALE);
  1318. pwm_count &= 0x7f;
  1319. // increment slow_pwm_count only every 64 pwm_count circa 65.5ms
  1320. if ((pwm_count % 64) == 0) {
  1321. slow_pwm_count++;
  1322. slow_pwm_count &= 0x7f;
  1323. // Extruder 0
  1324. if (state_timer_heater_0 > 0) {
  1325. state_timer_heater_0--;
  1326. }
  1327. #if EXTRUDERS > 1
  1328. // Extruder 1
  1329. if (state_timer_heater_1 > 0)
  1330. state_timer_heater_1--;
  1331. #endif
  1332. #if EXTRUDERS > 2
  1333. // Extruder 2
  1334. if (state_timer_heater_2 > 0)
  1335. state_timer_heater_2--;
  1336. #endif
  1337. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1338. // Bed
  1339. if (state_timer_heater_b > 0)
  1340. state_timer_heater_b--;
  1341. #endif
  1342. } //if ((pwm_count % 64) == 0) {
  1343. #endif //ifndef SLOW_PWM_HEATERS
  1344. }
  1345. FORCE_INLINE static void soft_pwm_isr()
  1346. {
  1347. lcd_buttons_update();
  1348. soft_pwm_core();
  1349. #ifdef BABYSTEPPING
  1350. applyBabysteps();
  1351. #endif //BABYSTEPPING
  1352. // Check if a stack overflow happened
  1353. if (!SdFatUtil::test_stack_integrity()) stack_error();
  1354. #if (defined(FANCHECK) && defined(TACH_0) && (TACH_0 > -1))
  1355. readFanTach();
  1356. #endif //(defined(TACH_0))
  1357. }
  1358. // Timer2 (originaly timer0) is shared with millies
  1359. #ifdef SYSTEM_TIMER_2
  1360. ISR(TIMER2_COMPB_vect)
  1361. #else //SYSTEM_TIMER_2
  1362. ISR(TIMER0_COMPB_vect)
  1363. #endif //SYSTEM_TIMER_2
  1364. {
  1365. DISABLE_SOFT_PWM_INTERRUPT();
  1366. sei();
  1367. soft_pwm_isr();
  1368. cli();
  1369. ENABLE_SOFT_PWM_INTERRUPT();
  1370. }
  1371. void check_max_temp_raw()
  1372. {
  1373. //heater
  1374. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1375. if (current_temperature_raw[0] <= maxttemp_raw[0]) {
  1376. #else
  1377. if (current_temperature_raw[0] >= maxttemp_raw[0]) {
  1378. #endif
  1379. set_temp_error(TempErrorSource::hotend, 0, TempErrorType::max);
  1380. }
  1381. //bed
  1382. #if defined(BED_MAXTEMP) && (TEMP_SENSOR_BED != 0)
  1383. #if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1384. if (current_temperature_bed_raw <= bed_maxttemp_raw) {
  1385. #else
  1386. if (current_temperature_bed_raw >= bed_maxttemp_raw) {
  1387. #endif
  1388. set_temp_error(TempErrorSource::bed, 0, TempErrorType::max);
  1389. }
  1390. #endif
  1391. //ambient
  1392. #if defined(AMBIENT_MAXTEMP) && (TEMP_SENSOR_AMBIENT != 0)
  1393. #if AMBIENT_RAW_LO_TEMP > AMBIENT_RAW_HI_TEMP
  1394. if (current_temperature_raw_ambient <= ambient_maxttemp_raw) {
  1395. #else
  1396. if (current_temperature_raw_ambient >= ambient_maxttemp_raw) {
  1397. #endif
  1398. set_temp_error(TempErrorSource::ambient, 0, TempErrorType::max);
  1399. }
  1400. #endif
  1401. }
  1402. //! number of repeating the same state with consecutive step() calls
  1403. //! used to slow down text switching
  1404. struct alert_automaton_mintemp {
  1405. const char *m2;
  1406. alert_automaton_mintemp(const char *m2):m2(m2){}
  1407. private:
  1408. enum { ALERT_AUTOMATON_SPEED_DIV = 5 };
  1409. enum class States : uint8_t { Init = 0, TempAboveMintemp, ShowPleaseRestart, ShowMintemp };
  1410. States state = States::Init;
  1411. uint8_t repeat = ALERT_AUTOMATON_SPEED_DIV;
  1412. void substep(States next_state){
  1413. if( repeat == 0 ){
  1414. state = next_state; // advance to the next state
  1415. repeat = ALERT_AUTOMATON_SPEED_DIV; // and prepare repeating for it too
  1416. } else {
  1417. --repeat;
  1418. }
  1419. }
  1420. public:
  1421. //! brief state automaton step routine
  1422. //! @param current_temp current hotend/bed temperature (for computing simple hysteresis)
  1423. //! @param mintemp minimal temperature including hysteresis to check current_temp against
  1424. void step(float current_temp, float mintemp){
  1425. static const char m1[] PROGMEM = "Please restart";
  1426. switch(state){
  1427. case States::Init: // initial state - check hysteresis
  1428. if( current_temp > mintemp ){
  1429. state = States::TempAboveMintemp;
  1430. }
  1431. // otherwise keep the Err MINTEMP alert message on the display,
  1432. // i.e. do not transfer to state 1
  1433. break;
  1434. case States::TempAboveMintemp: // the temperature has risen above the hysteresis check
  1435. lcd_setalertstatuspgm(m2);
  1436. substep(States::ShowMintemp);
  1437. last_alert_sent_to_lcd = LCDALERT_MINTEMPFIXED;
  1438. break;
  1439. case States::ShowPleaseRestart: // displaying "Please restart"
  1440. lcd_updatestatuspgm(m1);
  1441. substep(States::ShowMintemp);
  1442. last_alert_sent_to_lcd = LCDALERT_PLEASERESTART;
  1443. break;
  1444. case States::ShowMintemp: // displaying "MINTEMP fixed"
  1445. lcd_updatestatuspgm(m2);
  1446. substep(States::ShowPleaseRestart);
  1447. last_alert_sent_to_lcd = LCDALERT_MINTEMPFIXED;
  1448. break;
  1449. }
  1450. }
  1451. };
  1452. static const char m2hotend[] PROGMEM = "MINTEMP HOTEND fixed";
  1453. static const char m2bed[] PROGMEM = "MINTEMP BED fixed";
  1454. static alert_automaton_mintemp alert_automaton_hotend(m2hotend), alert_automaton_bed(m2bed);
  1455. void check_min_temp_heater0()
  1456. {
  1457. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1458. if (current_temperature_raw[0] >= minttemp_raw[0]) {
  1459. #else
  1460. if (current_temperature_raw[0] <= minttemp_raw[0]) {
  1461. #endif
  1462. set_temp_error(TempErrorSource::hotend, 0, TempErrorType::min);
  1463. }
  1464. }
  1465. void check_min_temp_bed()
  1466. {
  1467. #if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1468. if (current_temperature_bed_raw >= bed_minttemp_raw) {
  1469. #else
  1470. if (current_temperature_bed_raw <= bed_minttemp_raw) {
  1471. #endif
  1472. set_temp_error(TempErrorSource::bed, 0, TempErrorType::min);
  1473. }
  1474. }
  1475. #ifdef AMBIENT_MINTEMP
  1476. void check_min_temp_ambient()
  1477. {
  1478. #if AMBIENT_RAW_LO_TEMP > AMBIENT_RAW_HI_TEMP
  1479. if (current_temperature_raw_ambient >= ambient_minttemp_raw) {
  1480. #else
  1481. if (current_temperature_raw_ambient <= ambient_minttemp_raw) {
  1482. #endif
  1483. set_temp_error(TempErrorSource::ambient, 0, TempErrorType::min);
  1484. }
  1485. }
  1486. #endif
  1487. void handle_temp_error()
  1488. {
  1489. // TODO: Stop and the various *_error() functions need an overhaul!
  1490. // The heaters are kept disabled already at a higher level, making almost
  1491. // all the code inside the invidual handlers useless!
  1492. // relay to the original handler
  1493. switch((TempErrorType)temp_error_state.type) {
  1494. case TempErrorType::min:
  1495. switch((TempErrorSource)temp_error_state.source) {
  1496. case TempErrorSource::hotend:
  1497. if(temp_error_state.assert) {
  1498. menu_set_serious_error(SERIOUS_ERR_MINTEMP_HEATER);
  1499. min_temp_error(temp_error_state.index);
  1500. } else if( menu_is_serious_error(SERIOUS_ERR_MINTEMP_HEATER) ) {
  1501. // no recovery, just force the user to restart the printer
  1502. // which is a safer variant than just continuing printing
  1503. // The automaton also checks for hysteresis - the temperature must have reached a few degrees above the MINTEMP, before
  1504. // we shall signalize, that MINTEMP has been fixed
  1505. // Code notice: normally the alert_automaton instance would have been placed here
  1506. // as static alert_automaton_mintemp alert_automaton_hotend, but
  1507. alert_automaton_hotend.step(current_temperature[0], minttemp[0] + TEMP_HYSTERESIS);
  1508. }
  1509. break;
  1510. case TempErrorSource::bed:
  1511. if(temp_error_state.assert) {
  1512. menu_set_serious_error(SERIOUS_ERR_MINTEMP_BED);
  1513. bed_min_temp_error();
  1514. } else if( menu_is_serious_error(SERIOUS_ERR_MINTEMP_BED) ){
  1515. // no recovery, just force the user to restart the printer
  1516. // which is a safer variant than just continuing printing
  1517. alert_automaton_bed.step(current_temperature_bed, BED_MINTEMP + TEMP_HYSTERESIS);
  1518. }
  1519. break;
  1520. case TempErrorSource::ambient:
  1521. ambient_min_temp_error();
  1522. break;
  1523. }
  1524. break;
  1525. case TempErrorType::max:
  1526. switch((TempErrorSource)temp_error_state.source) {
  1527. case TempErrorSource::hotend:
  1528. max_temp_error(temp_error_state.index);
  1529. break;
  1530. case TempErrorSource::bed:
  1531. bed_max_temp_error();
  1532. break;
  1533. case TempErrorSource::ambient:
  1534. ambient_max_temp_error();
  1535. break;
  1536. }
  1537. break;
  1538. case TempErrorType::preheat:
  1539. case TempErrorType::runaway:
  1540. switch((TempErrorSource)temp_error_state.source) {
  1541. case TempErrorSource::hotend:
  1542. case TempErrorSource::bed:
  1543. temp_runaway_stop(
  1544. ((TempErrorType)temp_error_state.type == TempErrorType::preheat),
  1545. ((TempErrorSource)temp_error_state.source == TempErrorSource::bed));
  1546. break;
  1547. case TempErrorSource::ambient:
  1548. // not needed
  1549. break;
  1550. }
  1551. break;
  1552. }
  1553. }
  1554. #ifdef PIDTEMP
  1555. // Apply the scale factors to the PID values
  1556. float scalePID_i(float i)
  1557. {
  1558. return i*PID_dT;
  1559. }
  1560. float unscalePID_i(float i)
  1561. {
  1562. return i/PID_dT;
  1563. }
  1564. float scalePID_d(float d)
  1565. {
  1566. return d/PID_dT;
  1567. }
  1568. float unscalePID_d(float d)
  1569. {
  1570. return d*PID_dT;
  1571. }
  1572. #endif //PIDTEMP
  1573. #ifdef PINDA_THERMISTOR
  1574. //! @brief PINDA thermistor detected
  1575. //!
  1576. //! @retval true firmware should do temperature compensation and allow calibration
  1577. //! @retval false PINDA thermistor is not detected, disable temperature compensation and calibration
  1578. //! @retval true/false when forced via LCD menu Settings->HW Setup->SuperPINDA
  1579. //!
  1580. bool has_temperature_compensation()
  1581. {
  1582. #ifdef SUPERPINDA_SUPPORT
  1583. #ifdef PINDA_TEMP_COMP
  1584. uint8_t pinda_temp_compensation = eeprom_read_byte((uint8_t*)EEPROM_PINDA_TEMP_COMPENSATION);
  1585. if (pinda_temp_compensation == EEPROM_EMPTY_VALUE) //Unkown PINDA temp compenstation, so check it.
  1586. {
  1587. #endif //PINDA_TEMP_COMP
  1588. return (current_temperature_pinda >= PINDA_MINTEMP) ? true : false;
  1589. #ifdef PINDA_TEMP_COMP
  1590. }
  1591. else if (pinda_temp_compensation == 0) return true; //Overwritten via LCD menu SuperPINDA [No]
  1592. else return false; //Overwritten via LCD menu SuperPINDA [YES]
  1593. #endif //PINDA_TEMP_COMP
  1594. #else
  1595. return true;
  1596. #endif
  1597. }
  1598. #endif //PINDA_THERMISTOR
  1599. #define TEMP_MGR_INTV 0.27 // seconds, ~3.7Hz
  1600. #define TIMER5_PRESCALE 256
  1601. #define TIMER5_OCRA_OVF (uint16_t)(TEMP_MGR_INTV / ((long double)TIMER5_PRESCALE / F_CPU))
  1602. #define TEMP_MGR_INTERRUPT_STATE() (TIMSK5 & (1<<OCIE5A))
  1603. #define ENABLE_TEMP_MGR_INTERRUPT() TIMSK5 |= (1<<OCIE5A)
  1604. #define DISABLE_TEMP_MGR_INTERRUPT() TIMSK5 &= ~(1<<OCIE5A)
  1605. // RAII helper class to run a code block with temp_mgr_isr disabled
  1606. class TempMgrGuard
  1607. {
  1608. bool temp_mgr_state;
  1609. public:
  1610. TempMgrGuard() {
  1611. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  1612. temp_mgr_state = TEMP_MGR_INTERRUPT_STATE();
  1613. }
  1614. }
  1615. ~TempMgrGuard() throw() {
  1616. ATOMIC_BLOCK(ATOMIC_RESTORESTATE) {
  1617. if(temp_mgr_state) ENABLE_TEMP_MGR_INTERRUPT();
  1618. }
  1619. }
  1620. };
  1621. void temp_mgr_init()
  1622. {
  1623. // initialize the ADC and start a conversion
  1624. adc_init();
  1625. adc_start_cycle();
  1626. // initialize timer5
  1627. CRITICAL_SECTION_START;
  1628. // CTC
  1629. TCCR5B &= ~(1<<WGM53);
  1630. TCCR5B |= (1<<WGM52);
  1631. TCCR5A &= ~(1<<WGM51);
  1632. TCCR5A &= ~(1<<WGM50);
  1633. // output mode = 00 (disconnected)
  1634. TCCR5A &= ~(3<<COM5A0);
  1635. TCCR5A &= ~(3<<COM5B0);
  1636. // x/256 prescaler
  1637. TCCR5B |= (1<<CS52);
  1638. TCCR5B &= ~(1<<CS51);
  1639. TCCR5B &= ~(1<<CS50);
  1640. // reset counter
  1641. TCNT5 = 0;
  1642. OCR5A = TIMER5_OCRA_OVF;
  1643. // clear pending interrupts, enable COMPA
  1644. TIFR5 |= (1<<OCF5A);
  1645. ENABLE_TEMP_MGR_INTERRUPT();
  1646. CRITICAL_SECTION_END;
  1647. }
  1648. static void pid_heater(uint8_t e, const float current, const int target)
  1649. {
  1650. float pid_input;
  1651. float pid_output;
  1652. #ifdef TEMP_RUNAWAY_EXTRUDER_HYSTERESIS
  1653. temp_runaway_check(e+1, target, current, (int)soft_pwm[e], false);
  1654. #endif
  1655. #ifdef PIDTEMP
  1656. pid_input = current;
  1657. #ifndef PID_OPENLOOP
  1658. if(target == 0) {
  1659. pid_output = 0;
  1660. pid_reset[e] = true;
  1661. } else {
  1662. pid_error[e] = target - pid_input;
  1663. if(pid_reset[e]) {
  1664. iState_sum[e] = 0.0;
  1665. dTerm[e] = 0.0; // 'dState_last[e]' initial setting is not necessary (see end of if-statement)
  1666. pid_reset[e] = false;
  1667. }
  1668. #ifndef PonM
  1669. pTerm[e] = cs.Kp * pid_error[e];
  1670. iState_sum[e] += pid_error[e];
  1671. iState_sum[e] = constrain(iState_sum[e], iState_sum_min[e], iState_sum_max[e]);
  1672. iTerm[e] = cs.Ki * iState_sum[e];
  1673. // PID_K1 defined in Configuration.h in the PID settings
  1674. #define K2 (1.0-PID_K1)
  1675. dTerm[e] = (cs.Kd * (pid_input - dState_last[e]))*K2 + (PID_K1 * dTerm[e]); // e.g. digital filtration of derivative term changes
  1676. pid_output = pTerm[e] + iTerm[e] - dTerm[e]; // subtraction due to "Derivative on Measurement" method (i.e. derivative of input instead derivative of error is used)
  1677. if (pid_output > PID_MAX) {
  1678. if (pid_error[e] > 0 ) iState_sum[e] -= pid_error[e]; // conditional un-integration
  1679. pid_output=PID_MAX;
  1680. } else if (pid_output < 0) {
  1681. if (pid_error[e] < 0 ) iState_sum[e] -= pid_error[e]; // conditional un-integration
  1682. pid_output=0;
  1683. }
  1684. #else // PonM ("Proportional on Measurement" method)
  1685. iState_sum[e] += cs.Ki * pid_error[e];
  1686. iState_sum[e] -= cs.Kp * (pid_input - dState_last[e]);
  1687. iState_sum[e] = constrain(iState_sum[e], 0, PID_INTEGRAL_DRIVE_MAX);
  1688. dTerm[e] = cs.Kd * (pid_input - dState_last[e]);
  1689. pid_output = iState_sum[e] - dTerm[e]; // subtraction due to "Derivative on Measurement" method (i.e. derivative of input instead derivative of error is used)
  1690. pid_output = constrain(pid_output, 0, PID_MAX);
  1691. #endif // PonM
  1692. }
  1693. dState_last[e] = pid_input;
  1694. #else //PID_OPENLOOP
  1695. pid_output = constrain(target[e], 0, PID_MAX);
  1696. #endif //PID_OPENLOOP
  1697. #ifdef PID_DEBUG
  1698. SERIAL_ECHO_START;
  1699. SERIAL_ECHO(" PID_DEBUG ");
  1700. SERIAL_ECHO(e);
  1701. SERIAL_ECHO(": Input ");
  1702. SERIAL_ECHO(pid_input);
  1703. SERIAL_ECHO(" Output ");
  1704. SERIAL_ECHO(pid_output);
  1705. SERIAL_ECHO(" pTerm ");
  1706. SERIAL_ECHO(pTerm[e]);
  1707. SERIAL_ECHO(" iTerm ");
  1708. SERIAL_ECHO(iTerm[e]);
  1709. SERIAL_ECHO(" dTerm ");
  1710. SERIAL_ECHOLN(-dTerm[e]);
  1711. #endif //PID_DEBUG
  1712. #else /* PID off */
  1713. pid_output = 0;
  1714. if(current[e] < target[e]) {
  1715. pid_output = PID_MAX;
  1716. }
  1717. #endif
  1718. // Check if temperature is within the correct range
  1719. if((current < maxttemp[e]) && (target != 0))
  1720. soft_pwm[e] = (int)pid_output >> 1;
  1721. else
  1722. soft_pwm[e] = 0;
  1723. }
  1724. static void pid_bed(const float current, const int target)
  1725. {
  1726. float pid_input;
  1727. float pid_output;
  1728. #ifdef TEMP_RUNAWAY_BED_HYSTERESIS
  1729. temp_runaway_check(0, target, current, (int)soft_pwm_bed, true);
  1730. #endif
  1731. #ifndef PIDTEMPBED
  1732. if(_millis() - previous_millis_bed_heater < BED_CHECK_INTERVAL)
  1733. return;
  1734. previous_millis_bed_heater = _millis();
  1735. #endif
  1736. #if TEMP_SENSOR_BED != 0
  1737. #ifdef PIDTEMPBED
  1738. pid_input = current;
  1739. #ifndef PID_OPENLOOP
  1740. pid_error_bed = target - pid_input;
  1741. pTerm_bed = cs.bedKp * pid_error_bed;
  1742. temp_iState_bed += pid_error_bed;
  1743. temp_iState_bed = constrain(temp_iState_bed, temp_iState_min_bed, temp_iState_max_bed);
  1744. iTerm_bed = cs.bedKi * temp_iState_bed;
  1745. //PID_K1 defined in Configuration.h in the PID settings
  1746. #define K2 (1.0-PID_K1)
  1747. dTerm_bed= (cs.bedKd * (pid_input - temp_dState_bed))*K2 + (PID_K1 * dTerm_bed);
  1748. temp_dState_bed = pid_input;
  1749. pid_output = pTerm_bed + iTerm_bed - dTerm_bed;
  1750. if (pid_output > MAX_BED_POWER) {
  1751. if (pid_error_bed > 0 ) temp_iState_bed -= pid_error_bed; // conditional un-integration
  1752. pid_output=MAX_BED_POWER;
  1753. } else if (pid_output < 0){
  1754. if (pid_error_bed < 0 ) temp_iState_bed -= pid_error_bed; // conditional un-integration
  1755. pid_output=0;
  1756. }
  1757. #else
  1758. pid_output = constrain(target, 0, MAX_BED_POWER);
  1759. #endif //PID_OPENLOOP
  1760. if(current < BED_MAXTEMP)
  1761. {
  1762. soft_pwm_bed = (int)pid_output >> 1;
  1763. timer02_set_pwm0(soft_pwm_bed << 1);
  1764. }
  1765. else
  1766. {
  1767. soft_pwm_bed = 0;
  1768. timer02_set_pwm0(soft_pwm_bed << 1);
  1769. }
  1770. #elif !defined(BED_LIMIT_SWITCHING)
  1771. // Check if temperature is within the correct range
  1772. if(current < BED_MAXTEMP)
  1773. {
  1774. if(current >= target)
  1775. {
  1776. soft_pwm_bed = 0;
  1777. timer02_set_pwm0(soft_pwm_bed << 1);
  1778. }
  1779. else
  1780. {
  1781. soft_pwm_bed = MAX_BED_POWER>>1;
  1782. timer02_set_pwm0(soft_pwm_bed << 1);
  1783. }
  1784. }
  1785. else
  1786. {
  1787. soft_pwm_bed = 0;
  1788. timer02_set_pwm0(soft_pwm_bed << 1);
  1789. WRITE(HEATER_BED_PIN,LOW);
  1790. }
  1791. #else //#ifdef BED_LIMIT_SWITCHING
  1792. // Check if temperature is within the correct band
  1793. if(current < BED_MAXTEMP)
  1794. {
  1795. if(current > target + BED_HYSTERESIS)
  1796. {
  1797. soft_pwm_bed = 0;
  1798. timer02_set_pwm0(soft_pwm_bed << 1);
  1799. }
  1800. else if(current <= target - BED_HYSTERESIS)
  1801. {
  1802. soft_pwm_bed = MAX_BED_POWER>>1;
  1803. timer02_set_pwm0(soft_pwm_bed << 1);
  1804. }
  1805. }
  1806. else
  1807. {
  1808. soft_pwm_bed = 0;
  1809. timer02_set_pwm0(soft_pwm_bed << 1);
  1810. WRITE(HEATER_BED_PIN,LOW);
  1811. }
  1812. #endif //BED_LIMIT_SWITCHING
  1813. if(target==0)
  1814. {
  1815. soft_pwm_bed = 0;
  1816. timer02_set_pwm0(soft_pwm_bed << 1);
  1817. }
  1818. #endif //TEMP_SENSOR_BED
  1819. }
  1820. // ISR-safe temperatures
  1821. static volatile bool adc_values_ready = false;
  1822. float current_temperature_isr[EXTRUDERS];
  1823. int target_temperature_isr[EXTRUDERS];
  1824. float current_temperature_bed_isr;
  1825. int target_temperature_bed_isr;
  1826. #ifdef PINDA_THERMISTOR
  1827. float current_temperature_pinda_isr;
  1828. #endif
  1829. #ifdef AMBIENT_THERMISTOR
  1830. float current_temperature_ambient_isr;
  1831. #endif
  1832. // ISR callback from adc when sampling finished
  1833. void adc_callback()
  1834. {
  1835. current_temperature_raw[0] = adc_values[ADC_PIN_IDX(TEMP_0_PIN)]; //heater
  1836. current_temperature_bed_raw = adc_values[ADC_PIN_IDX(TEMP_BED_PIN)];
  1837. #ifdef PINDA_THERMISTOR
  1838. current_temperature_raw_pinda = adc_values[ADC_PIN_IDX(TEMP_PINDA_PIN)];
  1839. #endif //PINDA_THERMISTOR
  1840. #ifdef AMBIENT_THERMISTOR
  1841. current_temperature_raw_ambient = adc_values[ADC_PIN_IDX(TEMP_AMBIENT_PIN)]; // 5->6
  1842. #endif //AMBIENT_THERMISTOR
  1843. #ifdef VOLT_PWR_PIN
  1844. current_voltage_raw_pwr = adc_values[ADC_PIN_IDX(VOLT_PWR_PIN)];
  1845. #endif
  1846. #ifdef VOLT_BED_PIN
  1847. current_voltage_raw_bed = adc_values[ADC_PIN_IDX(VOLT_BED_PIN)]; // 6->9
  1848. #endif
  1849. #ifdef IR_SENSOR_ANALOG
  1850. current_voltage_raw_IR = adc_values[ADC_PIN_IDX(VOLT_IR_PIN)];
  1851. #endif //IR_SENSOR_ANALOG
  1852. adc_values_ready = true;
  1853. }
  1854. static void setCurrentTemperaturesFromIsr()
  1855. {
  1856. for(uint8_t e=0;e<EXTRUDERS;e++)
  1857. current_temperature[e] = current_temperature_isr[e];
  1858. current_temperature_bed = current_temperature_bed_isr;
  1859. #ifdef PINDA_THERMISTOR
  1860. current_temperature_pinda = current_temperature_pinda_isr;
  1861. #endif
  1862. #ifdef AMBIENT_THERMISTOR
  1863. current_temperature_ambient = current_temperature_ambient_isr;
  1864. #endif
  1865. }
  1866. static void setIsrTargetTemperatures()
  1867. {
  1868. for(uint8_t e=0;e<EXTRUDERS;e++)
  1869. target_temperature_isr[e] = target_temperature[e];
  1870. target_temperature_bed_isr = target_temperature_bed;
  1871. }
  1872. /* Synchronize temperatures:
  1873. - fetch updated values from temp_mgr_isr to current values
  1874. - update target temperatures for temp_mgr_isr regulation *if* no temperature error is set
  1875. This function is blocking: check temp_meas_ready before calling! */
  1876. static void updateTemperatures()
  1877. {
  1878. TempMgrGuard temp_mgr_guard;
  1879. setCurrentTemperaturesFromIsr();
  1880. if(!temp_error_state.v) {
  1881. // refuse to update target temperatures in any error condition!
  1882. setIsrTargetTemperatures();
  1883. }
  1884. temp_meas_ready = false;
  1885. }
  1886. /* Convert raw values into actual temperatures for temp_mgr. The raw values are created in the ADC
  1887. interrupt context, while this function runs from temp_mgr_isr which *is* preemptible as
  1888. analog2temp is relatively slow */
  1889. static void setIsrTemperaturesFromRawValues()
  1890. {
  1891. for(uint8_t e=0;e<EXTRUDERS;e++)
  1892. current_temperature_isr[e] = analog2temp(current_temperature_raw[e], e);
  1893. current_temperature_bed_isr = analog2tempBed(current_temperature_bed_raw);
  1894. #ifdef PINDA_THERMISTOR
  1895. current_temperature_pinda_isr = analog2tempBed(current_temperature_raw_pinda);
  1896. #endif
  1897. #ifdef AMBIENT_THERMISTOR
  1898. current_temperature_ambient_isr = analog2tempAmbient(current_temperature_raw_ambient); //thermistor for ambient is NTCG104LH104JT1 (2000)
  1899. #endif
  1900. temp_meas_ready = true;
  1901. }
  1902. static void temp_mgr_pid()
  1903. {
  1904. for(uint8_t e = 0; e < EXTRUDERS; e++)
  1905. pid_heater(e, current_temperature_isr[e], target_temperature_isr[e]);
  1906. pid_bed(current_temperature_bed_isr, target_temperature_bed_isr);
  1907. }
  1908. void check_temp_raw();
  1909. static void temp_mgr_isr()
  1910. {
  1911. // update *_isr temperatures from raw values for PID regulation
  1912. setIsrTemperaturesFromRawValues();
  1913. // clear the error assertion flag before checking again
  1914. temp_error_state.assert = false;
  1915. // check min/max temp using raw values
  1916. check_temp_raw();
  1917. // PID regulation
  1918. temp_mgr_pid();
  1919. }
  1920. ISR(TIMER5_COMPA_vect)
  1921. {
  1922. // immediately schedule a new conversion
  1923. if(adc_values_ready != true) return;
  1924. adc_start_cycle();
  1925. adc_values_ready = false;
  1926. // run temperature management with interrupts enabled to reduce latency
  1927. DISABLE_TEMP_MGR_INTERRUPT();
  1928. sei();
  1929. temp_mgr_isr();
  1930. cli();
  1931. ENABLE_TEMP_MGR_INTERRUPT();
  1932. }
  1933. void disable_heater()
  1934. {
  1935. setAllTargetHotends(0);
  1936. setTargetBed(0);
  1937. CRITICAL_SECTION_START;
  1938. // propagate all values down the chain
  1939. setIsrTargetTemperatures();
  1940. temp_mgr_pid();
  1941. // we can't call soft_pwm_core directly to toggle the pins as it would require removing the inline
  1942. // attribute, so disable each pin individually
  1943. #if defined(HEATER_0_PIN) && HEATER_0_PIN > -1 && EXTRUDERS > 0
  1944. WRITE(HEATER_0_PIN,LOW);
  1945. #endif
  1946. #if defined(HEATER_1_PIN) && HEATER_1_PIN > -1 && EXTRUDERS > 1
  1947. WRITE(HEATER_1_PIN,LOW);
  1948. #endif
  1949. #if defined(HEATER_2_PIN) && HEATER_2_PIN > -1 && EXTRUDERS > 2
  1950. WRITE(HEATER_2_PIN,LOW);
  1951. #endif
  1952. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1953. // TODO: this doesn't take immediate effect!
  1954. timer02_set_pwm0(0);
  1955. bedPWMDisabled = 0;
  1956. #endif
  1957. CRITICAL_SECTION_END;
  1958. }
  1959. void check_min_temp_raw()
  1960. {
  1961. static bool bCheckingOnHeater = false; // state variable, which allows to short no-checking delay (is set, when temperature is (first time) over heaterMintemp)
  1962. static bool bCheckingOnBed = false; // state variable, which allows to short no-checking delay (is set, when temperature is (first time) over bedMintemp)
  1963. static ShortTimer oTimer4minTempHeater;
  1964. static ShortTimer oTimer4minTempBed;
  1965. #ifdef AMBIENT_THERMISTOR
  1966. #ifdef AMBIENT_MINTEMP
  1967. // we need to check ambient temperature
  1968. check_min_temp_ambient();
  1969. #endif
  1970. #if AMBIENT_RAW_LO_TEMP > AMBIENT_RAW_HI_TEMP
  1971. if(current_temperature_raw_ambient>(OVERSAMPLENR*MINTEMP_MINAMBIENT_RAW)) // thermistor is NTC type
  1972. #else
  1973. if(current_temperature_raw_ambient=<(OVERSAMPLENR*MINTEMP_MINAMBIENT_RAW))
  1974. #endif
  1975. {
  1976. // ambient temperature is low
  1977. #endif //AMBIENT_THERMISTOR
  1978. // *** 'common' part of code for MK2.5 & MK3
  1979. // * nozzle checking
  1980. if(target_temperature_isr[active_extruder]>minttemp[active_extruder]) {
  1981. // ~ nozzle heating is on
  1982. bCheckingOnHeater=bCheckingOnHeater||(current_temperature_isr[active_extruder]>(minttemp[active_extruder]+TEMP_HYSTERESIS)); // for eventually delay cutting
  1983. if(oTimer4minTempHeater.expired(HEATER_MINTEMP_DELAY)||(!oTimer4minTempHeater.running())||bCheckingOnHeater) {
  1984. bCheckingOnHeater=true; // not necessary
  1985. check_min_temp_heater0(); // delay is elapsed or temperature is/was over minTemp => periodical checking is active
  1986. }
  1987. }
  1988. else {
  1989. // ~ nozzle heating is off
  1990. oTimer4minTempHeater.start();
  1991. bCheckingOnHeater=false;
  1992. }
  1993. // * bed checking
  1994. if(target_temperature_bed_isr>BED_MINTEMP) {
  1995. // ~ bed heating is on
  1996. bCheckingOnBed=bCheckingOnBed||(current_temperature_bed_isr>(BED_MINTEMP+TEMP_HYSTERESIS)); // for eventually delay cutting
  1997. if(oTimer4minTempBed.expired(BED_MINTEMP_DELAY)||(!oTimer4minTempBed.running())||bCheckingOnBed) {
  1998. bCheckingOnBed=true; // not necessary
  1999. check_min_temp_bed(); // delay is elapsed or temperature is/was over minTemp => periodical checking is active
  2000. }
  2001. }
  2002. else {
  2003. // ~ bed heating is off
  2004. oTimer4minTempBed.start();
  2005. bCheckingOnBed=false;
  2006. }
  2007. // *** end of 'common' part
  2008. #ifdef AMBIENT_THERMISTOR
  2009. }
  2010. else {
  2011. // ambient temperature is standard
  2012. check_min_temp_heater0();
  2013. check_min_temp_bed();
  2014. }
  2015. #endif //AMBIENT_THERMISTOR
  2016. }
  2017. void check_temp_raw()
  2018. {
  2019. // order is relevant: check_min_temp_raw requires max to be reliable due to
  2020. // ambient temperature being used for low handling temperatures
  2021. check_max_temp_raw();
  2022. check_min_temp_raw();
  2023. }