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

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