My Marlin configs for Fabrikator Mini and CTC i3 Pro B
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

temperature.cpp 40KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400
  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 "Sd2PinMap.h"
  28. //===========================================================================
  29. //=============================public variables============================
  30. //===========================================================================
  31. int target_temperature[EXTRUDERS] = { 0 };
  32. int target_temperature_bed = 0;
  33. int current_temperature_raw[EXTRUDERS] = { 0 };
  34. float current_temperature[EXTRUDERS] = { 0.0 };
  35. int current_temperature_bed_raw = 0;
  36. float current_temperature_bed = 0.0;
  37. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  38. int redundant_temperature_raw = 0;
  39. float redundant_temperature = 0.0;
  40. #endif
  41. #ifdef PIDTEMP
  42. float Kp=DEFAULT_Kp;
  43. float Ki=(DEFAULT_Ki*PID_dT);
  44. float Kd=(DEFAULT_Kd/PID_dT);
  45. #ifdef PID_ADD_EXTRUSION_RATE
  46. float Kc=DEFAULT_Kc;
  47. #endif
  48. #endif //PIDTEMP
  49. #ifdef PIDTEMPBED
  50. float bedKp=DEFAULT_bedKp;
  51. float bedKi=(DEFAULT_bedKi*PID_dT);
  52. float bedKd=(DEFAULT_bedKd/PID_dT);
  53. #endif //PIDTEMPBED
  54. #ifdef FAN_SOFT_PWM
  55. unsigned char fanSpeedSoftPwm;
  56. #endif
  57. unsigned char soft_pwm_bed;
  58. #ifdef BABYSTEPPING
  59. volatile int babystepsTodo[3]={0,0,0};
  60. #endif
  61. //===========================================================================
  62. //=============================private variables============================
  63. //===========================================================================
  64. static volatile bool temp_meas_ready = false;
  65. #ifdef PIDTEMP
  66. //static cannot be external:
  67. static float temp_iState[EXTRUDERS] = { 0 };
  68. static float temp_dState[EXTRUDERS] = { 0 };
  69. static float pTerm[EXTRUDERS];
  70. static float iTerm[EXTRUDERS];
  71. static float dTerm[EXTRUDERS];
  72. //int output;
  73. static float pid_error[EXTRUDERS];
  74. static float temp_iState_min[EXTRUDERS];
  75. static float temp_iState_max[EXTRUDERS];
  76. // static float pid_input[EXTRUDERS];
  77. // static float pid_output[EXTRUDERS];
  78. static bool pid_reset[EXTRUDERS];
  79. #endif //PIDTEMP
  80. #ifdef PIDTEMPBED
  81. //static cannot be external:
  82. static float temp_iState_bed = { 0 };
  83. static float temp_dState_bed = { 0 };
  84. static float pTerm_bed;
  85. static float iTerm_bed;
  86. static float dTerm_bed;
  87. //int output;
  88. static float pid_error_bed;
  89. static float temp_iState_min_bed;
  90. static float temp_iState_max_bed;
  91. #else //PIDTEMPBED
  92. static unsigned long previous_millis_bed_heater;
  93. #endif //PIDTEMPBED
  94. static unsigned char soft_pwm[EXTRUDERS];
  95. #ifdef FAN_SOFT_PWM
  96. static unsigned char soft_pwm_fan;
  97. #endif
  98. #if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
  99. (defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
  100. (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1)
  101. static unsigned long extruder_autofan_last_check;
  102. #endif
  103. #if EXTRUDERS > 3
  104. # error Unsupported number of extruders
  105. #elif EXTRUDERS > 2
  106. # define ARRAY_BY_EXTRUDERS(v1, v2, v3) { v1, v2, v3 }
  107. #elif EXTRUDERS > 1
  108. # define ARRAY_BY_EXTRUDERS(v1, v2, v3) { v1, v2 }
  109. #else
  110. # define ARRAY_BY_EXTRUDERS(v1, v2, v3) { v1 }
  111. #endif
  112. // Init min and max temp with extreme values to prevent false errors during startup
  113. static int minttemp_raw[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_RAW_LO_TEMP , HEATER_1_RAW_LO_TEMP , HEATER_2_RAW_LO_TEMP );
  114. static int maxttemp_raw[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_RAW_HI_TEMP , HEATER_1_RAW_HI_TEMP , HEATER_2_RAW_HI_TEMP );
  115. static int minttemp[EXTRUDERS] = ARRAY_BY_EXTRUDERS( 0, 0, 0 );
  116. static int maxttemp[EXTRUDERS] = ARRAY_BY_EXTRUDERS( 16383, 16383, 16383 );
  117. //static int bed_minttemp_raw = HEATER_BED_RAW_LO_TEMP; /* No bed mintemp error implemented?!? */
  118. #ifdef BED_MAXTEMP
  119. static int bed_maxttemp_raw = HEATER_BED_RAW_HI_TEMP;
  120. #endif
  121. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  122. static void *heater_ttbl_map[2] = {(void *)HEATER_0_TEMPTABLE, (void *)HEATER_1_TEMPTABLE };
  123. static uint8_t heater_ttbllen_map[2] = { HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN };
  124. #else
  125. static void *heater_ttbl_map[EXTRUDERS] = ARRAY_BY_EXTRUDERS( (void *)HEATER_0_TEMPTABLE, (void *)HEATER_1_TEMPTABLE, (void *)HEATER_2_TEMPTABLE );
  126. static uint8_t heater_ttbllen_map[EXTRUDERS] = ARRAY_BY_EXTRUDERS( HEATER_0_TEMPTABLE_LEN, HEATER_1_TEMPTABLE_LEN, HEATER_2_TEMPTABLE_LEN );
  127. #endif
  128. static float analog2temp(int raw, uint8_t e);
  129. static float analog2tempBed(int raw);
  130. static void updateTemperaturesFromRawValues();
  131. #ifdef WATCH_TEMP_PERIOD
  132. int watch_start_temp[EXTRUDERS] = ARRAY_BY_EXTRUDERS(0,0,0);
  133. unsigned long watchmillis[EXTRUDERS] = ARRAY_BY_EXTRUDERS(0,0,0);
  134. #endif //WATCH_TEMP_PERIOD
  135. #ifndef SOFT_PWM_SCALE
  136. #define SOFT_PWM_SCALE 0
  137. #endif
  138. //===========================================================================
  139. //============================= functions ============================
  140. //===========================================================================
  141. void PID_autotune(float temp, int extruder, int ncycles)
  142. {
  143. float input = 0.0;
  144. int cycles=0;
  145. bool heating = true;
  146. unsigned long temp_millis = millis();
  147. unsigned long t1=temp_millis;
  148. unsigned long t2=temp_millis;
  149. long t_high = 0;
  150. long t_low = 0;
  151. long bias, d;
  152. float Ku, Tu;
  153. float Kp, Ki, Kd;
  154. float max = 0, min = 10000;
  155. if ((extruder >= EXTRUDERS)
  156. #if (TEMP_BED_PIN <= -1)
  157. ||(extruder < 0)
  158. #endif
  159. ){
  160. SERIAL_ECHOLN("PID Autotune failed. Bad extruder number.");
  161. return;
  162. }
  163. SERIAL_ECHOLN("PID Autotune start");
  164. disable_heater(); // switch off all heaters.
  165. if (extruder<0)
  166. {
  167. soft_pwm_bed = (MAX_BED_POWER)/2;
  168. bias = d = (MAX_BED_POWER)/2;
  169. }
  170. else
  171. {
  172. soft_pwm[extruder] = (PID_MAX)/2;
  173. bias = d = (PID_MAX)/2;
  174. }
  175. for(;;) {
  176. if(temp_meas_ready == true) { // temp sample ready
  177. updateTemperaturesFromRawValues();
  178. input = (extruder<0)?current_temperature_bed:current_temperature[extruder];
  179. max=max(max,input);
  180. min=min(min,input);
  181. if(heating == true && input > temp) {
  182. if(millis() - t2 > 5000) {
  183. heating=false;
  184. if (extruder<0)
  185. soft_pwm_bed = (bias - d) >> 1;
  186. else
  187. soft_pwm[extruder] = (bias - d) >> 1;
  188. t1=millis();
  189. t_high=t1 - t2;
  190. max=temp;
  191. }
  192. }
  193. if(heating == false && input < temp) {
  194. if(millis() - t1 > 5000) {
  195. heating=true;
  196. t2=millis();
  197. t_low=t2 - t1;
  198. if(cycles > 0) {
  199. bias += (d*(t_high - t_low))/(t_low + t_high);
  200. bias = constrain(bias, 20 ,(extruder<0?(MAX_BED_POWER):(PID_MAX))-20);
  201. if(bias > (extruder<0?(MAX_BED_POWER):(PID_MAX))/2) d = (extruder<0?(MAX_BED_POWER):(PID_MAX)) - 1 - bias;
  202. else d = bias;
  203. SERIAL_PROTOCOLPGM(" bias: "); SERIAL_PROTOCOL(bias);
  204. SERIAL_PROTOCOLPGM(" d: "); SERIAL_PROTOCOL(d);
  205. SERIAL_PROTOCOLPGM(" min: "); SERIAL_PROTOCOL(min);
  206. SERIAL_PROTOCOLPGM(" max: "); SERIAL_PROTOCOLLN(max);
  207. if(cycles > 2) {
  208. Ku = (4.0*d)/(3.14159*(max-min)/2.0);
  209. Tu = ((float)(t_low + t_high)/1000.0);
  210. SERIAL_PROTOCOLPGM(" Ku: "); SERIAL_PROTOCOL(Ku);
  211. SERIAL_PROTOCOLPGM(" Tu: "); SERIAL_PROTOCOLLN(Tu);
  212. Kp = 0.6*Ku;
  213. Ki = 2*Kp/Tu;
  214. Kd = Kp*Tu/8;
  215. SERIAL_PROTOCOLLNPGM(" Classic PID ");
  216. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(Kp);
  217. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(Ki);
  218. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(Kd);
  219. /*
  220. Kp = 0.33*Ku;
  221. Ki = Kp/Tu;
  222. Kd = Kp*Tu/3;
  223. SERIAL_PROTOCOLLNPGM(" Some overshoot ");
  224. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(Kp);
  225. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(Ki);
  226. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(Kd);
  227. Kp = 0.2*Ku;
  228. Ki = 2*Kp/Tu;
  229. Kd = Kp*Tu/3;
  230. SERIAL_PROTOCOLLNPGM(" No overshoot ");
  231. SERIAL_PROTOCOLPGM(" Kp: "); SERIAL_PROTOCOLLN(Kp);
  232. SERIAL_PROTOCOLPGM(" Ki: "); SERIAL_PROTOCOLLN(Ki);
  233. SERIAL_PROTOCOLPGM(" Kd: "); SERIAL_PROTOCOLLN(Kd);
  234. */
  235. }
  236. }
  237. if (extruder<0)
  238. soft_pwm_bed = (bias + d) >> 1;
  239. else
  240. soft_pwm[extruder] = (bias + d) >> 1;
  241. cycles++;
  242. min=temp;
  243. }
  244. }
  245. }
  246. if(input > (temp + 20)) {
  247. SERIAL_PROTOCOLLNPGM("PID Autotune failed! Temperature too high");
  248. return;
  249. }
  250. if(millis() - temp_millis > 2000) {
  251. int p;
  252. if (extruder<0){
  253. p=soft_pwm_bed;
  254. SERIAL_PROTOCOLPGM("ok B:");
  255. }else{
  256. p=soft_pwm[extruder];
  257. SERIAL_PROTOCOLPGM("ok T:");
  258. }
  259. SERIAL_PROTOCOL(input);
  260. SERIAL_PROTOCOLPGM(" @:");
  261. SERIAL_PROTOCOLLN(p);
  262. temp_millis = millis();
  263. }
  264. if(((millis() - t1) + (millis() - t2)) > (10L*60L*1000L*2L)) {
  265. SERIAL_PROTOCOLLNPGM("PID Autotune failed! timeout");
  266. return;
  267. }
  268. if(cycles > ncycles) {
  269. SERIAL_PROTOCOLLNPGM("PID Autotune finished! Put the last Kp, Ki and Kd constants from above into Configuration.h");
  270. return;
  271. }
  272. lcd_update();
  273. }
  274. }
  275. void updatePID()
  276. {
  277. #ifdef PIDTEMP
  278. for(int e = 0; e < EXTRUDERS; e++) {
  279. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / Ki;
  280. }
  281. #endif
  282. #ifdef PIDTEMPBED
  283. temp_iState_max_bed = PID_INTEGRAL_DRIVE_MAX / bedKi;
  284. #endif
  285. }
  286. int getHeaterPower(int heater) {
  287. if (heater<0)
  288. return soft_pwm_bed;
  289. return soft_pwm[heater];
  290. }
  291. #if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
  292. (defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
  293. (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1)
  294. #if defined(FAN_PIN) && FAN_PIN > -1
  295. #if EXTRUDER_0_AUTO_FAN_PIN == FAN_PIN
  296. #error "You cannot set EXTRUDER_0_AUTO_FAN_PIN equal to FAN_PIN"
  297. #endif
  298. #if EXTRUDER_1_AUTO_FAN_PIN == FAN_PIN
  299. #error "You cannot set EXTRUDER_1_AUTO_FAN_PIN equal to FAN_PIN"
  300. #endif
  301. #if EXTRUDER_2_AUTO_FAN_PIN == FAN_PIN
  302. #error "You cannot set EXTRUDER_2_AUTO_FAN_PIN equal to FAN_PIN"
  303. #endif
  304. #endif
  305. void setExtruderAutoFanState(int pin, bool state)
  306. {
  307. unsigned char newFanSpeed = (state != 0) ? EXTRUDER_AUTO_FAN_SPEED : 0;
  308. // this idiom allows both digital and PWM fan outputs (see M42 handling).
  309. pinMode(pin, OUTPUT);
  310. digitalWrite(pin, newFanSpeed);
  311. analogWrite(pin, newFanSpeed);
  312. }
  313. void checkExtruderAutoFans()
  314. {
  315. uint8_t fanState = 0;
  316. // which fan pins need to be turned on?
  317. #if defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1
  318. if (current_temperature[0] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  319. fanState |= 1;
  320. #endif
  321. #if defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1
  322. if (current_temperature[1] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  323. {
  324. if (EXTRUDER_1_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN)
  325. fanState |= 1;
  326. else
  327. fanState |= 2;
  328. }
  329. #endif
  330. #if defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1
  331. if (current_temperature[2] > EXTRUDER_AUTO_FAN_TEMPERATURE)
  332. {
  333. if (EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_0_AUTO_FAN_PIN)
  334. fanState |= 1;
  335. else if (EXTRUDER_2_AUTO_FAN_PIN == EXTRUDER_1_AUTO_FAN_PIN)
  336. fanState |= 2;
  337. else
  338. fanState |= 4;
  339. }
  340. #endif
  341. // update extruder auto fan states
  342. #if defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1
  343. setExtruderAutoFanState(EXTRUDER_0_AUTO_FAN_PIN, (fanState & 1) != 0);
  344. #endif
  345. #if defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1
  346. if (EXTRUDER_1_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN)
  347. setExtruderAutoFanState(EXTRUDER_1_AUTO_FAN_PIN, (fanState & 2) != 0);
  348. #endif
  349. #if defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1
  350. if (EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_0_AUTO_FAN_PIN
  351. && EXTRUDER_2_AUTO_FAN_PIN != EXTRUDER_1_AUTO_FAN_PIN)
  352. setExtruderAutoFanState(EXTRUDER_2_AUTO_FAN_PIN, (fanState & 4) != 0);
  353. #endif
  354. }
  355. #endif // any extruder auto fan pins set
  356. void manage_heater()
  357. {
  358. float pid_input;
  359. float pid_output;
  360. if(temp_meas_ready != true) //better readability
  361. return;
  362. updateTemperaturesFromRawValues();
  363. for(int e = 0; e < EXTRUDERS; e++)
  364. {
  365. #ifdef THERMAL_RUNAWAY_PROTECTION_PERIOD && THERMAL_RUNAWAY_PROTECTION_PERIOD > 0
  366. 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);
  367. #endif
  368. #ifdef PIDTEMP
  369. pid_input = current_temperature[e];
  370. #ifndef PID_OPENLOOP
  371. pid_error[e] = target_temperature[e] - pid_input;
  372. if(pid_error[e] > PID_FUNCTIONAL_RANGE) {
  373. pid_output = BANG_MAX;
  374. pid_reset[e] = true;
  375. }
  376. else if(pid_error[e] < -PID_FUNCTIONAL_RANGE || target_temperature[e] == 0) {
  377. pid_output = 0;
  378. pid_reset[e] = true;
  379. }
  380. else {
  381. if(pid_reset[e] == true) {
  382. temp_iState[e] = 0.0;
  383. pid_reset[e] = false;
  384. }
  385. pTerm[e] = Kp * pid_error[e];
  386. temp_iState[e] += pid_error[e];
  387. temp_iState[e] = constrain(temp_iState[e], temp_iState_min[e], temp_iState_max[e]);
  388. iTerm[e] = Ki * temp_iState[e];
  389. //K1 defined in Configuration.h in the PID settings
  390. #define K2 (1.0-K1)
  391. dTerm[e] = (Kd * (pid_input - temp_dState[e]))*K2 + (K1 * dTerm[e]);
  392. pid_output = constrain(pTerm[e] + iTerm[e] - dTerm[e], 0, PID_MAX);
  393. }
  394. temp_dState[e] = pid_input;
  395. #else
  396. pid_output = constrain(target_temperature[e], 0, PID_MAX);
  397. #endif //PID_OPENLOOP
  398. #ifdef PID_DEBUG
  399. SERIAL_ECHO_START;
  400. SERIAL_ECHO(" PID_DEBUG ");
  401. SERIAL_ECHO(e);
  402. SERIAL_ECHO(": Input ");
  403. SERIAL_ECHO(pid_input);
  404. SERIAL_ECHO(" Output ");
  405. SERIAL_ECHO(pid_output);
  406. SERIAL_ECHO(" pTerm ");
  407. SERIAL_ECHO(pTerm[e]);
  408. SERIAL_ECHO(" iTerm ");
  409. SERIAL_ECHO(iTerm[e]);
  410. SERIAL_ECHO(" dTerm ");
  411. SERIAL_ECHOLN(dTerm[e]);
  412. #endif //PID_DEBUG
  413. #else /* PID off */
  414. pid_output = 0;
  415. if(current_temperature[e] < target_temperature[e]) {
  416. pid_output = PID_MAX;
  417. }
  418. #endif
  419. // Check if temperature is within the correct range
  420. if((current_temperature[e] > minttemp[e]) && (current_temperature[e] < maxttemp[e]))
  421. {
  422. soft_pwm[e] = (int)pid_output >> 1;
  423. }
  424. else {
  425. soft_pwm[e] = 0;
  426. }
  427. #ifdef WATCH_TEMP_PERIOD
  428. if(watchmillis[e] && millis() - watchmillis[e] > WATCH_TEMP_PERIOD)
  429. {
  430. if(degHotend(e) < watch_start_temp[e] + WATCH_TEMP_INCREASE)
  431. {
  432. setTargetHotend(0, e);
  433. LCD_MESSAGEPGM("Heating failed");
  434. SERIAL_ECHO_START;
  435. SERIAL_ECHOLN("Heating failed");
  436. }else{
  437. watchmillis[e] = 0;
  438. }
  439. }
  440. #endif
  441. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  442. if(fabs(current_temperature[0] - redundant_temperature) > MAX_REDUNDANT_TEMP_SENSOR_DIFF) {
  443. disable_heater();
  444. if(IsStopped() == false) {
  445. SERIAL_ERROR_START;
  446. SERIAL_ERRORLNPGM("Extruder switched off. Temperature difference between temp sensors is too high !");
  447. LCD_ALERTMESSAGEPGM("Err: REDUNDANT TEMP ERROR");
  448. }
  449. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  450. Stop();
  451. #endif
  452. }
  453. #endif
  454. } // End extruder for loop
  455. #if (defined(EXTRUDER_0_AUTO_FAN_PIN) && EXTRUDER_0_AUTO_FAN_PIN > -1) || \
  456. (defined(EXTRUDER_1_AUTO_FAN_PIN) && EXTRUDER_1_AUTO_FAN_PIN > -1) || \
  457. (defined(EXTRUDER_2_AUTO_FAN_PIN) && EXTRUDER_2_AUTO_FAN_PIN > -1)
  458. if(millis() - extruder_autofan_last_check > 2500) // only need to check fan state very infrequently
  459. {
  460. checkExtruderAutoFans();
  461. extruder_autofan_last_check = millis();
  462. }
  463. #endif
  464. #ifndef PIDTEMPBED
  465. if(millis() - previous_millis_bed_heater < BED_CHECK_INTERVAL)
  466. return;
  467. previous_millis_bed_heater = millis();
  468. #endif
  469. #if TEMP_SENSOR_BED != 0
  470. #ifdef THERMAL_RUNAWAY_PROTECTION_PERIOD && THERMAL_RUNAWAY_PROTECTION_PERIOD > 0
  471. 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);
  472. #endif
  473. #ifdef PIDTEMPBED
  474. pid_input = current_temperature_bed;
  475. #ifndef PID_OPENLOOP
  476. pid_error_bed = target_temperature_bed - pid_input;
  477. pTerm_bed = bedKp * pid_error_bed;
  478. temp_iState_bed += pid_error_bed;
  479. temp_iState_bed = constrain(temp_iState_bed, temp_iState_min_bed, temp_iState_max_bed);
  480. iTerm_bed = bedKi * temp_iState_bed;
  481. //K1 defined in Configuration.h in the PID settings
  482. #define K2 (1.0-K1)
  483. dTerm_bed= (bedKd * (pid_input - temp_dState_bed))*K2 + (K1 * dTerm_bed);
  484. temp_dState_bed = pid_input;
  485. pid_output = constrain(pTerm_bed + iTerm_bed - dTerm_bed, 0, MAX_BED_POWER);
  486. #else
  487. pid_output = constrain(target_temperature_bed, 0, MAX_BED_POWER);
  488. #endif //PID_OPENLOOP
  489. if((current_temperature_bed > BED_MINTEMP) && (current_temperature_bed < BED_MAXTEMP))
  490. {
  491. soft_pwm_bed = (int)pid_output >> 1;
  492. }
  493. else {
  494. soft_pwm_bed = 0;
  495. }
  496. #elif !defined(BED_LIMIT_SWITCHING)
  497. // Check if temperature is within the correct range
  498. if((current_temperature_bed > BED_MINTEMP) && (current_temperature_bed < BED_MAXTEMP))
  499. {
  500. if(current_temperature_bed >= target_temperature_bed)
  501. {
  502. soft_pwm_bed = 0;
  503. }
  504. else
  505. {
  506. soft_pwm_bed = MAX_BED_POWER>>1;
  507. }
  508. }
  509. else
  510. {
  511. soft_pwm_bed = 0;
  512. WRITE(HEATER_BED_PIN,LOW);
  513. }
  514. #else //#ifdef BED_LIMIT_SWITCHING
  515. // Check if temperature is within the correct band
  516. if((current_temperature_bed > BED_MINTEMP) && (current_temperature_bed < BED_MAXTEMP))
  517. {
  518. if(current_temperature_bed > target_temperature_bed + BED_HYSTERESIS)
  519. {
  520. soft_pwm_bed = 0;
  521. }
  522. else if(current_temperature_bed <= target_temperature_bed - BED_HYSTERESIS)
  523. {
  524. soft_pwm_bed = MAX_BED_POWER>>1;
  525. }
  526. }
  527. else
  528. {
  529. soft_pwm_bed = 0;
  530. WRITE(HEATER_BED_PIN,LOW);
  531. }
  532. #endif
  533. #endif
  534. }
  535. #define PGM_RD_W(x) (short)pgm_read_word(&x)
  536. // Derived from RepRap FiveD extruder::getTemperature()
  537. // For hot end temperature measurement.
  538. static float analog2temp(int raw, uint8_t e) {
  539. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  540. if(e > EXTRUDERS)
  541. #else
  542. if(e >= EXTRUDERS)
  543. #endif
  544. {
  545. SERIAL_ERROR_START;
  546. SERIAL_ERROR((int)e);
  547. SERIAL_ERRORLNPGM(" - Invalid extruder number !");
  548. kill();
  549. return 0.0;
  550. }
  551. #ifdef HEATER_0_USES_MAX6675
  552. if (e == 0)
  553. {
  554. return 0.25 * raw;
  555. }
  556. #endif
  557. if(heater_ttbl_map[e] != NULL)
  558. {
  559. float celsius = 0;
  560. uint8_t i;
  561. short (*tt)[][2] = (short (*)[][2])(heater_ttbl_map[e]);
  562. for (i=1; i<heater_ttbllen_map[e]; i++)
  563. {
  564. if (PGM_RD_W((*tt)[i][0]) > raw)
  565. {
  566. celsius = PGM_RD_W((*tt)[i-1][1]) +
  567. (raw - PGM_RD_W((*tt)[i-1][0])) *
  568. (float)(PGM_RD_W((*tt)[i][1]) - PGM_RD_W((*tt)[i-1][1])) /
  569. (float)(PGM_RD_W((*tt)[i][0]) - PGM_RD_W((*tt)[i-1][0]));
  570. break;
  571. }
  572. }
  573. // Overflow: Set to last value in the table
  574. if (i == heater_ttbllen_map[e]) celsius = PGM_RD_W((*tt)[i-1][1]);
  575. return celsius;
  576. }
  577. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  578. }
  579. // Derived from RepRap FiveD extruder::getTemperature()
  580. // For bed temperature measurement.
  581. static float analog2tempBed(int raw) {
  582. #ifdef BED_USES_THERMISTOR
  583. float celsius = 0;
  584. byte i;
  585. for (i=1; i<BEDTEMPTABLE_LEN; i++)
  586. {
  587. if (PGM_RD_W(BEDTEMPTABLE[i][0]) > raw)
  588. {
  589. celsius = PGM_RD_W(BEDTEMPTABLE[i-1][1]) +
  590. (raw - PGM_RD_W(BEDTEMPTABLE[i-1][0])) *
  591. (float)(PGM_RD_W(BEDTEMPTABLE[i][1]) - PGM_RD_W(BEDTEMPTABLE[i-1][1])) /
  592. (float)(PGM_RD_W(BEDTEMPTABLE[i][0]) - PGM_RD_W(BEDTEMPTABLE[i-1][0]));
  593. break;
  594. }
  595. }
  596. // Overflow: Set to last value in the table
  597. if (i == BEDTEMPTABLE_LEN) celsius = PGM_RD_W(BEDTEMPTABLE[i-1][1]);
  598. return celsius;
  599. #elif defined BED_USES_AD595
  600. return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
  601. #else
  602. return 0;
  603. #endif
  604. }
  605. /* Called to get the raw values into the the actual temperatures. The raw values are created in interrupt context,
  606. and this function is called from normal context as it is too slow to run in interrupts and will block the stepper routine otherwise */
  607. static void updateTemperaturesFromRawValues()
  608. {
  609. for(uint8_t e=0;e<EXTRUDERS;e++)
  610. {
  611. current_temperature[e] = analog2temp(current_temperature_raw[e], e);
  612. }
  613. current_temperature_bed = analog2tempBed(current_temperature_bed_raw);
  614. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  615. redundant_temperature = analog2temp(redundant_temperature_raw, 1);
  616. #endif
  617. //Reset the watchdog after we know we have a temperature measurement.
  618. watchdog_reset();
  619. CRITICAL_SECTION_START;
  620. temp_meas_ready = false;
  621. CRITICAL_SECTION_END;
  622. }
  623. void tp_init()
  624. {
  625. #if (MOTHERBOARD == 80) && ((TEMP_SENSOR_0==-1)||(TEMP_SENSOR_1==-1)||(TEMP_SENSOR_2==-1)||(TEMP_SENSOR_BED==-1))
  626. //disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector
  627. MCUCR=(1<<JTD);
  628. MCUCR=(1<<JTD);
  629. #endif
  630. // Finish init of mult extruder arrays
  631. for(int e = 0; e < EXTRUDERS; e++) {
  632. // populate with the first value
  633. maxttemp[e] = maxttemp[0];
  634. #ifdef PIDTEMP
  635. temp_iState_min[e] = 0.0;
  636. temp_iState_max[e] = PID_INTEGRAL_DRIVE_MAX / Ki;
  637. #endif //PIDTEMP
  638. #ifdef PIDTEMPBED
  639. temp_iState_min_bed = 0.0;
  640. temp_iState_max_bed = PID_INTEGRAL_DRIVE_MAX / bedKi;
  641. #endif //PIDTEMPBED
  642. }
  643. #if defined(HEATER_0_PIN) && (HEATER_0_PIN > -1)
  644. SET_OUTPUT(HEATER_0_PIN);
  645. #endif
  646. #if defined(HEATER_1_PIN) && (HEATER_1_PIN > -1)
  647. SET_OUTPUT(HEATER_1_PIN);
  648. #endif
  649. #if defined(HEATER_2_PIN) && (HEATER_2_PIN > -1)
  650. SET_OUTPUT(HEATER_2_PIN);
  651. #endif
  652. #if defined(HEATER_BED_PIN) && (HEATER_BED_PIN > -1)
  653. SET_OUTPUT(HEATER_BED_PIN);
  654. #endif
  655. #if defined(FAN_PIN) && (FAN_PIN > -1)
  656. SET_OUTPUT(FAN_PIN);
  657. #ifdef FAST_PWM_FAN
  658. setPwmFrequency(FAN_PIN, 1); // No prescaling. Pwm frequency = F_CPU/256/8
  659. #endif
  660. #ifdef FAN_SOFT_PWM
  661. soft_pwm_fan = fanSpeedSoftPwm / 2;
  662. #endif
  663. #endif
  664. #ifdef HEATER_0_USES_MAX6675
  665. #ifndef SDSUPPORT
  666. SET_OUTPUT(SCK_PIN);
  667. WRITE(SCK_PIN,0);
  668. SET_OUTPUT(MOSI_PIN);
  669. WRITE(MOSI_PIN,1);
  670. SET_INPUT(MISO_PIN);
  671. WRITE(MISO_PIN,1);
  672. #endif
  673. /* Using pinMode and digitalWrite, as that was the only way I could get it to compile */
  674. //Have to toggle SD card CS pin to low first, to enable firmware to talk with SD card
  675. pinMode(SS_PIN, OUTPUT);
  676. digitalWrite(SS_PIN,0);
  677. pinMode(MAX6675_SS, OUTPUT);
  678. digitalWrite(MAX6675_SS,1);
  679. #endif
  680. // Set analog inputs
  681. ADCSRA = 1<<ADEN | 1<<ADSC | 1<<ADIF | 0x07;
  682. DIDR0 = 0;
  683. #ifdef DIDR2
  684. DIDR2 = 0;
  685. #endif
  686. #if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
  687. #if TEMP_0_PIN < 8
  688. DIDR0 |= 1 << TEMP_0_PIN;
  689. #else
  690. DIDR2 |= 1<<(TEMP_0_PIN - 8);
  691. #endif
  692. #endif
  693. #if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1)
  694. #if TEMP_1_PIN < 8
  695. DIDR0 |= 1<<TEMP_1_PIN;
  696. #else
  697. DIDR2 |= 1<<(TEMP_1_PIN - 8);
  698. #endif
  699. #endif
  700. #if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
  701. #if TEMP_2_PIN < 8
  702. DIDR0 |= 1 << TEMP_2_PIN;
  703. #else
  704. DIDR2 |= 1<<(TEMP_2_PIN - 8);
  705. #endif
  706. #endif
  707. #if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
  708. #if TEMP_BED_PIN < 8
  709. DIDR0 |= 1<<TEMP_BED_PIN;
  710. #else
  711. DIDR2 |= 1<<(TEMP_BED_PIN - 8);
  712. #endif
  713. #endif
  714. // Use timer0 for temperature measurement
  715. // Interleave temperature interrupt with millies interrupt
  716. OCR0B = 128;
  717. TIMSK0 |= (1<<OCIE0B);
  718. // Wait for temperature measurement to settle
  719. delay(250);
  720. #ifdef HEATER_0_MINTEMP
  721. minttemp[0] = HEATER_0_MINTEMP;
  722. while(analog2temp(minttemp_raw[0], 0) < HEATER_0_MINTEMP) {
  723. #if HEATER_0_RAW_LO_TEMP < HEATER_0_RAW_HI_TEMP
  724. minttemp_raw[0] += OVERSAMPLENR;
  725. #else
  726. minttemp_raw[0] -= OVERSAMPLENR;
  727. #endif
  728. }
  729. #endif //MINTEMP
  730. #ifdef HEATER_0_MAXTEMP
  731. maxttemp[0] = HEATER_0_MAXTEMP;
  732. while(analog2temp(maxttemp_raw[0], 0) > HEATER_0_MAXTEMP) {
  733. #if HEATER_0_RAW_LO_TEMP < HEATER_0_RAW_HI_TEMP
  734. maxttemp_raw[0] -= OVERSAMPLENR;
  735. #else
  736. maxttemp_raw[0] += OVERSAMPLENR;
  737. #endif
  738. }
  739. #endif //MAXTEMP
  740. #if (EXTRUDERS > 1) && defined(HEATER_1_MINTEMP)
  741. minttemp[1] = HEATER_1_MINTEMP;
  742. while(analog2temp(minttemp_raw[1], 1) < HEATER_1_MINTEMP) {
  743. #if HEATER_1_RAW_LO_TEMP < HEATER_1_RAW_HI_TEMP
  744. minttemp_raw[1] += OVERSAMPLENR;
  745. #else
  746. minttemp_raw[1] -= OVERSAMPLENR;
  747. #endif
  748. }
  749. #endif // MINTEMP 1
  750. #if (EXTRUDERS > 1) && defined(HEATER_1_MAXTEMP)
  751. maxttemp[1] = HEATER_1_MAXTEMP;
  752. while(analog2temp(maxttemp_raw[1], 1) > HEATER_1_MAXTEMP) {
  753. #if HEATER_1_RAW_LO_TEMP < HEATER_1_RAW_HI_TEMP
  754. maxttemp_raw[1] -= OVERSAMPLENR;
  755. #else
  756. maxttemp_raw[1] += OVERSAMPLENR;
  757. #endif
  758. }
  759. #endif //MAXTEMP 1
  760. #if (EXTRUDERS > 2) && defined(HEATER_2_MINTEMP)
  761. minttemp[2] = HEATER_2_MINTEMP;
  762. while(analog2temp(minttemp_raw[2], 2) < HEATER_2_MINTEMP) {
  763. #if HEATER_2_RAW_LO_TEMP < HEATER_2_RAW_HI_TEMP
  764. minttemp_raw[2] += OVERSAMPLENR;
  765. #else
  766. minttemp_raw[2] -= OVERSAMPLENR;
  767. #endif
  768. }
  769. #endif //MINTEMP 2
  770. #if (EXTRUDERS > 2) && defined(HEATER_2_MAXTEMP)
  771. maxttemp[2] = HEATER_2_MAXTEMP;
  772. while(analog2temp(maxttemp_raw[2], 2) > HEATER_2_MAXTEMP) {
  773. #if HEATER_2_RAW_LO_TEMP < HEATER_2_RAW_HI_TEMP
  774. maxttemp_raw[2] -= OVERSAMPLENR;
  775. #else
  776. maxttemp_raw[2] += OVERSAMPLENR;
  777. #endif
  778. }
  779. #endif //MAXTEMP 2
  780. #ifdef BED_MINTEMP
  781. /* No bed MINTEMP error implemented?!? */ /*
  782. while(analog2tempBed(bed_minttemp_raw) < BED_MINTEMP) {
  783. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  784. bed_minttemp_raw += OVERSAMPLENR;
  785. #else
  786. bed_minttemp_raw -= OVERSAMPLENR;
  787. #endif
  788. }
  789. */
  790. #endif //BED_MINTEMP
  791. #ifdef BED_MAXTEMP
  792. while(analog2tempBed(bed_maxttemp_raw) > BED_MAXTEMP) {
  793. #if HEATER_BED_RAW_LO_TEMP < HEATER_BED_RAW_HI_TEMP
  794. bed_maxttemp_raw -= OVERSAMPLENR;
  795. #else
  796. bed_maxttemp_raw += OVERSAMPLENR;
  797. #endif
  798. }
  799. #endif //BED_MAXTEMP
  800. }
  801. void setWatch()
  802. {
  803. #ifdef WATCH_TEMP_PERIOD
  804. for (int e = 0; e < EXTRUDERS; e++)
  805. {
  806. if(degHotend(e) < degTargetHotend(e) - (WATCH_TEMP_INCREASE * 2))
  807. {
  808. watch_start_temp[e] = degHotend(e);
  809. watchmillis[e] = millis();
  810. }
  811. }
  812. #endif
  813. }
  814. #ifdef THERMAL_RUNAWAY_PROTECTION_PERIOD && THERMAL_RUNAWAY_PROTECTION_PERIOD > 0
  815. void thermal_runaway_protection(int *state, unsigned long *timer, float temperature, float target_temperature, int heater_id, int period_seconds, int hysteresis_degc)
  816. {
  817. /*
  818. SERIAL_ECHO_START;
  819. SERIAL_ECHO("Thermal Thermal Runaway Running. Heater ID:");
  820. SERIAL_ECHO(heater_id);
  821. SERIAL_ECHO(" ; State:");
  822. SERIAL_ECHO(*state);
  823. SERIAL_ECHO(" ; Timer:");
  824. SERIAL_ECHO(*timer);
  825. SERIAL_ECHO(" ; Temperature:");
  826. SERIAL_ECHO(temperature);
  827. SERIAL_ECHO(" ; Target Temp:");
  828. SERIAL_ECHO(target_temperature);
  829. SERIAL_ECHOLN("");
  830. */
  831. if ((target_temperature == 0) || thermal_runaway)
  832. {
  833. *state = 0;
  834. *timer = 0;
  835. return;
  836. }
  837. switch (*state)
  838. {
  839. case 0: // "Heater Inactive" state
  840. if (target_temperature > 0) *state = 1;
  841. break;
  842. case 1: // "First Heating" state
  843. if (temperature >= target_temperature) *state = 2;
  844. break;
  845. case 2: // "Temperature Stable" state
  846. if (temperature >= (target_temperature - hysteresis_degc))
  847. {
  848. *timer = millis();
  849. }
  850. else if ( (millis() - *timer) > period_seconds*1000)
  851. {
  852. SERIAL_ERROR_START;
  853. SERIAL_ERRORLNPGM("Thermal Runaway, system stopped! Heater_ID: ");
  854. SERIAL_ERRORLN((int)heater_id);
  855. LCD_ALERTMESSAGEPGM("THERMAL RUNAWAY");
  856. thermal_runaway = true;
  857. while(1)
  858. {
  859. disable_heater();
  860. disable_x();
  861. disable_y();
  862. disable_z();
  863. disable_e0();
  864. disable_e1();
  865. disable_e2();
  866. manage_heater();
  867. lcd_update();
  868. }
  869. }
  870. break;
  871. }
  872. }
  873. #endif
  874. void disable_heater()
  875. {
  876. for(int i=0;i<EXTRUDERS;i++)
  877. setTargetHotend(0,i);
  878. setTargetBed(0);
  879. #if defined(TEMP_0_PIN) && TEMP_0_PIN > -1
  880. target_temperature[0]=0;
  881. soft_pwm[0]=0;
  882. #if defined(HEATER_0_PIN) && HEATER_0_PIN > -1
  883. WRITE(HEATER_0_PIN,LOW);
  884. #endif
  885. #endif
  886. #if defined(TEMP_1_PIN) && TEMP_1_PIN > -1 && EXTRUDERS > 1
  887. target_temperature[1]=0;
  888. soft_pwm[1]=0;
  889. #if defined(HEATER_1_PIN) && HEATER_1_PIN > -1
  890. WRITE(HEATER_1_PIN,LOW);
  891. #endif
  892. #endif
  893. #if defined(TEMP_2_PIN) && TEMP_2_PIN > -1 && EXTRUDERS > 2
  894. target_temperature[2]=0;
  895. soft_pwm[2]=0;
  896. #if defined(HEATER_2_PIN) && HEATER_2_PIN > -1
  897. WRITE(HEATER_2_PIN,LOW);
  898. #endif
  899. #endif
  900. #if defined(TEMP_BED_PIN) && TEMP_BED_PIN > -1
  901. target_temperature_bed=0;
  902. soft_pwm_bed=0;
  903. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  904. WRITE(HEATER_BED_PIN,LOW);
  905. #endif
  906. #endif
  907. }
  908. void max_temp_error(uint8_t e) {
  909. disable_heater();
  910. if(IsStopped() == false) {
  911. SERIAL_ERROR_START;
  912. SERIAL_ERRORLN((int)e);
  913. SERIAL_ERRORLNPGM(": Extruder switched off. MAXTEMP triggered !");
  914. LCD_ALERTMESSAGEPGM("Err: MAXTEMP");
  915. }
  916. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  917. Stop();
  918. #endif
  919. }
  920. void min_temp_error(uint8_t e) {
  921. disable_heater();
  922. if(IsStopped() == false) {
  923. SERIAL_ERROR_START;
  924. SERIAL_ERRORLN((int)e);
  925. SERIAL_ERRORLNPGM(": Extruder switched off. MINTEMP triggered !");
  926. LCD_ALERTMESSAGEPGM("Err: MINTEMP");
  927. }
  928. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  929. Stop();
  930. #endif
  931. }
  932. void bed_max_temp_error(void) {
  933. #if HEATER_BED_PIN > -1
  934. WRITE(HEATER_BED_PIN, 0);
  935. #endif
  936. if(IsStopped() == false) {
  937. SERIAL_ERROR_START;
  938. SERIAL_ERRORLNPGM("Temperature heated bed switched off. MAXTEMP triggered !!");
  939. LCD_ALERTMESSAGEPGM("Err: MAXTEMP BED");
  940. }
  941. #ifndef BOGUS_TEMPERATURE_FAILSAFE_OVERRIDE
  942. Stop();
  943. #endif
  944. }
  945. #ifdef HEATER_0_USES_MAX6675
  946. #define MAX6675_HEAT_INTERVAL 250
  947. long max6675_previous_millis = MAX6675_HEAT_INTERVAL;
  948. int max6675_temp = 2000;
  949. int read_max6675()
  950. {
  951. if (millis() - max6675_previous_millis < MAX6675_HEAT_INTERVAL)
  952. return max6675_temp;
  953. max6675_previous_millis = millis();
  954. max6675_temp = 0;
  955. #ifdef PRR
  956. PRR &= ~(1<<PRSPI);
  957. #elif defined PRR0
  958. PRR0 &= ~(1<<PRSPI);
  959. #endif
  960. SPCR = (1<<MSTR) | (1<<SPE) | (1<<SPR0);
  961. // enable TT_MAX6675
  962. WRITE(MAX6675_SS, 0);
  963. // ensure 100ns delay - a bit extra is fine
  964. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  965. asm("nop");//50ns on 20Mhz, 62.5ns on 16Mhz
  966. // read MSB
  967. SPDR = 0;
  968. for (;(SPSR & (1<<SPIF)) == 0;);
  969. max6675_temp = SPDR;
  970. max6675_temp <<= 8;
  971. // read LSB
  972. SPDR = 0;
  973. for (;(SPSR & (1<<SPIF)) == 0;);
  974. max6675_temp |= SPDR;
  975. // disable TT_MAX6675
  976. WRITE(MAX6675_SS, 1);
  977. if (max6675_temp & 4)
  978. {
  979. // thermocouple open
  980. max6675_temp = 2000;
  981. }
  982. else
  983. {
  984. max6675_temp = max6675_temp >> 3;
  985. }
  986. return max6675_temp;
  987. }
  988. #endif
  989. // Timer 0 is shared with millies
  990. ISR(TIMER0_COMPB_vect)
  991. {
  992. //these variables are only accesible from the ISR, but static, so they don't lose their value
  993. static unsigned char temp_count = 0;
  994. static unsigned long raw_temp_0_value = 0;
  995. static unsigned long raw_temp_1_value = 0;
  996. static unsigned long raw_temp_2_value = 0;
  997. static unsigned long raw_temp_bed_value = 0;
  998. static unsigned char temp_state = 8;
  999. static unsigned char pwm_count = (1 << SOFT_PWM_SCALE);
  1000. static unsigned char soft_pwm_0;
  1001. #if (EXTRUDERS > 1) || defined(HEATERS_PARALLEL)
  1002. static unsigned char soft_pwm_1;
  1003. #endif
  1004. #if EXTRUDERS > 2
  1005. static unsigned char soft_pwm_2;
  1006. #endif
  1007. #if HEATER_BED_PIN > -1
  1008. static unsigned char soft_pwm_b;
  1009. #endif
  1010. if(pwm_count == 0){
  1011. soft_pwm_0 = soft_pwm[0];
  1012. if(soft_pwm_0 > 0) {
  1013. WRITE(HEATER_0_PIN,1);
  1014. #ifdef HEATERS_PARALLEL
  1015. WRITE(HEATER_1_PIN,1);
  1016. #endif
  1017. } else WRITE(HEATER_0_PIN,0);
  1018. #if EXTRUDERS > 1
  1019. soft_pwm_1 = soft_pwm[1];
  1020. if(soft_pwm_1 > 0) WRITE(HEATER_1_PIN,1); else WRITE(HEATER_1_PIN,0);
  1021. #endif
  1022. #if EXTRUDERS > 2
  1023. soft_pwm_2 = soft_pwm[2];
  1024. if(soft_pwm_2 > 0) WRITE(HEATER_2_PIN,1); else WRITE(HEATER_2_PIN,0);
  1025. #endif
  1026. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1027. soft_pwm_b = soft_pwm_bed;
  1028. if(soft_pwm_b > 0) WRITE(HEATER_BED_PIN,1); else WRITE(HEATER_BED_PIN,0);
  1029. #endif
  1030. #ifdef FAN_SOFT_PWM
  1031. soft_pwm_fan = fanSpeedSoftPwm / 2;
  1032. if(soft_pwm_fan > 0) WRITE(FAN_PIN,1); else WRITE(FAN_PIN,0);
  1033. #endif
  1034. }
  1035. if(soft_pwm_0 < pwm_count) {
  1036. WRITE(HEATER_0_PIN,0);
  1037. #ifdef HEATERS_PARALLEL
  1038. WRITE(HEATER_1_PIN,0);
  1039. #endif
  1040. }
  1041. #if EXTRUDERS > 1
  1042. if(soft_pwm_1 < pwm_count) WRITE(HEATER_1_PIN,0);
  1043. #endif
  1044. #if EXTRUDERS > 2
  1045. if(soft_pwm_2 < pwm_count) WRITE(HEATER_2_PIN,0);
  1046. #endif
  1047. #if defined(HEATER_BED_PIN) && HEATER_BED_PIN > -1
  1048. if(soft_pwm_b < pwm_count) WRITE(HEATER_BED_PIN,0);
  1049. #endif
  1050. #ifdef FAN_SOFT_PWM
  1051. if(soft_pwm_fan < pwm_count) WRITE(FAN_PIN,0);
  1052. #endif
  1053. pwm_count += (1 << SOFT_PWM_SCALE);
  1054. pwm_count &= 0x7f;
  1055. switch(temp_state) {
  1056. case 0: // Prepare TEMP_0
  1057. #if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
  1058. #if TEMP_0_PIN > 7
  1059. ADCSRB = 1<<MUX5;
  1060. #else
  1061. ADCSRB = 0;
  1062. #endif
  1063. ADMUX = ((1 << REFS0) | (TEMP_0_PIN & 0x07));
  1064. ADCSRA |= 1<<ADSC; // Start conversion
  1065. #endif
  1066. lcd_buttons_update();
  1067. temp_state = 1;
  1068. break;
  1069. case 1: // Measure TEMP_0
  1070. #if defined(TEMP_0_PIN) && (TEMP_0_PIN > -1)
  1071. raw_temp_0_value += ADC;
  1072. #endif
  1073. #ifdef HEATER_0_USES_MAX6675 // TODO remove the blocking
  1074. raw_temp_0_value = read_max6675();
  1075. #endif
  1076. temp_state = 2;
  1077. break;
  1078. case 2: // Prepare TEMP_BED
  1079. #if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
  1080. #if TEMP_BED_PIN > 7
  1081. ADCSRB = 1<<MUX5;
  1082. #else
  1083. ADCSRB = 0;
  1084. #endif
  1085. ADMUX = ((1 << REFS0) | (TEMP_BED_PIN & 0x07));
  1086. ADCSRA |= 1<<ADSC; // Start conversion
  1087. #endif
  1088. lcd_buttons_update();
  1089. temp_state = 3;
  1090. break;
  1091. case 3: // Measure TEMP_BED
  1092. #if defined(TEMP_BED_PIN) && (TEMP_BED_PIN > -1)
  1093. raw_temp_bed_value += ADC;
  1094. #endif
  1095. temp_state = 4;
  1096. break;
  1097. case 4: // Prepare TEMP_1
  1098. #if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1)
  1099. #if TEMP_1_PIN > 7
  1100. ADCSRB = 1<<MUX5;
  1101. #else
  1102. ADCSRB = 0;
  1103. #endif
  1104. ADMUX = ((1 << REFS0) | (TEMP_1_PIN & 0x07));
  1105. ADCSRA |= 1<<ADSC; // Start conversion
  1106. #endif
  1107. lcd_buttons_update();
  1108. temp_state = 5;
  1109. break;
  1110. case 5: // Measure TEMP_1
  1111. #if defined(TEMP_1_PIN) && (TEMP_1_PIN > -1)
  1112. raw_temp_1_value += ADC;
  1113. #endif
  1114. temp_state = 6;
  1115. break;
  1116. case 6: // Prepare TEMP_2
  1117. #if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
  1118. #if TEMP_2_PIN > 7
  1119. ADCSRB = 1<<MUX5;
  1120. #else
  1121. ADCSRB = 0;
  1122. #endif
  1123. ADMUX = ((1 << REFS0) | (TEMP_2_PIN & 0x07));
  1124. ADCSRA |= 1<<ADSC; // Start conversion
  1125. #endif
  1126. lcd_buttons_update();
  1127. temp_state = 7;
  1128. break;
  1129. case 7: // Measure TEMP_2
  1130. #if defined(TEMP_2_PIN) && (TEMP_2_PIN > -1)
  1131. raw_temp_2_value += ADC;
  1132. #endif
  1133. temp_state = 0;
  1134. temp_count++;
  1135. break;
  1136. case 8: //Startup, delay initial temp reading a tiny bit so the hardware can settle.
  1137. temp_state = 0;
  1138. break;
  1139. // default:
  1140. // SERIAL_ERROR_START;
  1141. // SERIAL_ERRORLNPGM("Temp measurement error!");
  1142. // break;
  1143. }
  1144. if(temp_count >= OVERSAMPLENR) // 8 * 16 * 1/(16000000/64/256) = 131ms.
  1145. {
  1146. if (!temp_meas_ready) //Only update the raw values if they have been read. Else we could be updating them during reading.
  1147. {
  1148. current_temperature_raw[0] = raw_temp_0_value;
  1149. #if EXTRUDERS > 1
  1150. current_temperature_raw[1] = raw_temp_1_value;
  1151. #endif
  1152. #ifdef TEMP_SENSOR_1_AS_REDUNDANT
  1153. redundant_temperature_raw = raw_temp_1_value;
  1154. #endif
  1155. #if EXTRUDERS > 2
  1156. current_temperature_raw[2] = raw_temp_2_value;
  1157. #endif
  1158. current_temperature_bed_raw = raw_temp_bed_value;
  1159. }
  1160. temp_meas_ready = true;
  1161. temp_count = 0;
  1162. raw_temp_0_value = 0;
  1163. raw_temp_1_value = 0;
  1164. raw_temp_2_value = 0;
  1165. raw_temp_bed_value = 0;
  1166. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1167. if(current_temperature_raw[0] <= maxttemp_raw[0]) {
  1168. #else
  1169. if(current_temperature_raw[0] >= maxttemp_raw[0]) {
  1170. #endif
  1171. max_temp_error(0);
  1172. }
  1173. #if HEATER_0_RAW_LO_TEMP > HEATER_0_RAW_HI_TEMP
  1174. if(current_temperature_raw[0] >= minttemp_raw[0]) {
  1175. #else
  1176. if(current_temperature_raw[0] <= minttemp_raw[0]) {
  1177. #endif
  1178. min_temp_error(0);
  1179. }
  1180. #if EXTRUDERS > 1
  1181. #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
  1182. if(current_temperature_raw[1] <= maxttemp_raw[1]) {
  1183. #else
  1184. if(current_temperature_raw[1] >= maxttemp_raw[1]) {
  1185. #endif
  1186. max_temp_error(1);
  1187. }
  1188. #if HEATER_1_RAW_LO_TEMP > HEATER_1_RAW_HI_TEMP
  1189. if(current_temperature_raw[1] >= minttemp_raw[1]) {
  1190. #else
  1191. if(current_temperature_raw[1] <= minttemp_raw[1]) {
  1192. #endif
  1193. min_temp_error(1);
  1194. }
  1195. #endif
  1196. #if EXTRUDERS > 2
  1197. #if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
  1198. if(current_temperature_raw[2] <= maxttemp_raw[2]) {
  1199. #else
  1200. if(current_temperature_raw[2] >= maxttemp_raw[2]) {
  1201. #endif
  1202. max_temp_error(2);
  1203. }
  1204. #if HEATER_2_RAW_LO_TEMP > HEATER_2_RAW_HI_TEMP
  1205. if(current_temperature_raw[2] >= minttemp_raw[2]) {
  1206. #else
  1207. if(current_temperature_raw[2] <= minttemp_raw[2]) {
  1208. #endif
  1209. min_temp_error(2);
  1210. }
  1211. #endif
  1212. /* No bed MINTEMP error? */
  1213. #if defined(BED_MAXTEMP) && (TEMP_SENSOR_BED != 0)
  1214. # if HEATER_BED_RAW_LO_TEMP > HEATER_BED_RAW_HI_TEMP
  1215. if(current_temperature_bed_raw <= bed_maxttemp_raw) {
  1216. #else
  1217. if(current_temperature_bed_raw >= bed_maxttemp_raw) {
  1218. #endif
  1219. target_temperature_bed = 0;
  1220. bed_max_temp_error();
  1221. }
  1222. #endif
  1223. }
  1224. #ifdef BABYSTEPPING
  1225. for(uint8_t axis=0;axis<3;axis++)
  1226. {
  1227. int curTodo=babystepsTodo[axis]; //get rid of volatile for performance
  1228. if(curTodo>0)
  1229. {
  1230. babystep(axis,/*fwd*/true);
  1231. babystepsTodo[axis]--; //less to do next time
  1232. }
  1233. else
  1234. if(curTodo<0)
  1235. {
  1236. babystep(axis,/*fwd*/false);
  1237. babystepsTodo[axis]++; //less to do next time
  1238. }
  1239. }
  1240. #endif //BABYSTEPPING
  1241. }
  1242. #ifdef PIDTEMP
  1243. // Apply the scale factors to the PID values
  1244. float scalePID_i(float i)
  1245. {
  1246. return i*PID_dT;
  1247. }
  1248. float unscalePID_i(float i)
  1249. {
  1250. return i/PID_dT;
  1251. }
  1252. float scalePID_d(float d)
  1253. {
  1254. return d/PID_dT;
  1255. }
  1256. float unscalePID_d(float d)
  1257. {
  1258. return d*PID_dT;
  1259. }
  1260. #endif //PIDTEMP