My Marlin configs for Fabrikator Mini and CTC i3 Pro B
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

temperature.cpp 37KB

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