My Marlin configs for Fabrikator Mini and CTC i3 Pro B
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

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