Continue to add code for cooler

parent 832050e2
......@@ -18,12 +18,12 @@
/// #define HIGH_TO_FIRE
// Uncomment the following to enable the use of the PWM (the one for the extruder 0) to drive a peltier cell or any PWM driven cooler for the laser
#define LASER_WATER_COOLING
#define LASER_WATER_MAXTEMP 25
#define COOLER
#define COOLER_MAXTEMP 25
// Uncomment the following to enable LASER_WATER_COOLING PWM instead of bang-bang
#define LASER_PWM
#define LASER_PWM_FREQUENCY 1000 // Frequency in Hz
// Uncomment the following to enable COOLER PWM instead of bang-bang
#define COOLER_PWM
#define COOLER_PWM_FREQUENCY 1000 // Frequency in Hz
//// The following defines select which G codes tell the laser to fire. It's OK to uncomment more than one.
#define LASER_FIRE_G1 10 // fire the laser on a G1 move, extinguish when the move ends
......@@ -37,7 +37,7 @@
#define LASER_RASTER_MM_PER_PULSE 0.2 //Can be overridden by providing an R value in M649 command : M649 S17 B2 D0 R0.1 F4000
//// Uncomment the following if the laser cutter is equipped with a peripheral relay board
//// to control power to an exhaust fan, water pump, laser power supply, etc.
//// to control power to an exhaust fan, cooler pump, laser power supply, etc.
//#define LASER_PERIPHERALS
//#define LASER_PERIPHERALS_TIMEOUT 30000 // Number of milliseconds to wait for status signal from peripheral control board
......@@ -68,12 +68,12 @@
/***********************************************************************
************************ PID Settings - WATER ***************************
************************ PID Settings - COOLER ************************
***********************************************************************
* *
* PID Tuning Guide here: http://reprap.org/wiki/PID_Tuning *
* Select PID or bang-bang with PIDTEMPWATER. *
* If bang-bang, WATER_LIMIT_SWITCHING will enable hysteresis *
* Select PID or bang-bang with PIDTEMPCOOLER. *
* If bang-bang, COOLER_LIMIT_SWITCHING will enable hysteresis *
* *
***********************************************************************/
// Uncomment this to enable PID on the bed. It uses the same frequency PWM as the extruder.
......@@ -83,28 +83,28 @@
// If your configuration is significantly different than this and you don't understand the issues involved, you probably
// shouldn't use bed PID until someone else verifies your hardware works.
// If this is enabled, find your own PID constants below.
#define PIDTEMPWATER
#define PIDTEMPCOOLER
//#define WATER_LIMIT_SWITCHING
#define WATER_HYSTERESIS 2 //only disable heating if T<target-WATER_HYSTERESIS and enable heating if T<target+WATER_HYSTERESIS (works only if WATER_LIMIT_SWITCHING is enabled)
#define WATER_CHECK_INTERVAL 5000 //ms between checks in bang-bang control
//#define COOLER_LIMIT_SWITCHING
#define COOLER_HYSTERESIS 2 //only disable heating if T<target-COOLER_HYSTERESIS and enable heating if T<target+COOLER_HYSTERESIS (works only if COOLER_LIMIT_SWITCHING is enabled)
#define COOLER_CHECK_INTERVAL 5000 //ms between checks in bang-bang control
// This sets the max power delivered to the bed.
// all forms of bed control obey this (PID, bang-bang, bang-bang with hysteresis)
// setting this to anything other than 255 enables a form of PWM to the bed,
// so you shouldn't use it unless you are OK with PWM on your bed. (see the comment on enabling PIDTEMPWATER)
#define MAX_WATER_POWER 255 // limits duty cycle to bed; 255=full current
// so you shouldn't use it unless you are OK with PWM on your bed. (see the comment on enabling PIDTEMPCOOLER)
#define MAX_COOLER_POWER 255 // limits duty cycle to bed; 255=full current
#define PID_WATER_INTEGRAL_DRIVE_MAX MAX_WATER_POWER // limit for the integral term
#define PID_COOLER_INTEGRAL_DRIVE_MAX MAX_COOLER_POWER // limit for the integral term
// 120v 250W silicone heater into 4mm borosilicate (MendelMax 1.5+)
// from FOPDT model - kp=.39 Tp=405 Tdead=66, Tc set to 79.2, aggressive factor of .15 (vs .1, 1, 10)
#define DEFAULT_waterKp 10.00
#define DEFAULT_waterKi .023
#define DEFAULT_waterKd 305.4
#define DEFAULT_coolerKp 10.00
#define DEFAULT_coolerKi .023
#define DEFAULT_coolerKd 305.4
// FIND YOUR OWN: "M303 E-1 C8 S90" to run autotune on the bed at 90 degreesC for 8 cycles.
//#define PID_WATER_DEBUG // Sends debug data to the serial port.
//#define PID_COOLER_DEBUG // Sends debug data to the serial port.
/***********************************************************************/
......
......@@ -121,9 +121,9 @@
#define LASER_PERIPHERALS_PIN 11
#define LASER_PERIPHERALS_STATUS_PIN 4
#endif // LASER_PERIPHERALS
#if ENABLED(LASER_WATER_COOLING)
#define LASER_WATER_COOLING_PIN 2 // Digital pins 2, 3, 5, 6, 7, 8 are attached to timers we can use
#endif // LASER WATER_COOLING
#if ENABLED(COOLER)
#define COOLER_PIN 2 // Digital pins 2, 3, 5, 6, 7, 8 are attached to timers we can use
#endif // COOLER
#endif
......
......@@ -73,7 +73,7 @@
*
* PIDTEMPBED:
* M304 PID bedKp, bedKi, bedKd
* M304 L PID waterKp, waterKi, waterKd
* M304 L PID coolerKp, coolerKi, coolerKd
*
* DOGLCD:
* M250 C lcd_contrast
......@@ -211,10 +211,10 @@ void Config_StoreSettings() {
EEPROM_WRITE_VAR(i, bedKd);
#endif
#if ENABLED(PIDTEMPWATER)
EEPROM_WRITE_VAR(i, waterKp);
EEPROM_WRITE_VAR(i, waterKi);
EEPROM_WRITE_VAR(i, waterKd);
#if ENABLED(PIDTEMPCOOLER)
EEPROM_WRITE_VAR(i, coolerKp);
EEPROM_WRITE_VAR(i, coolerKi);
EEPROM_WRITE_VAR(i, coolerKd);
#endif
#if HASNT(LCD_CONTRAST)
......@@ -366,10 +366,10 @@ void Config_RetrieveSettings() {
EEPROM_READ_VAR(i, bedKd);
#endif
#if ENABLED(PIDTEMPWATER)
EEPROM_READ_VAR(i, waterKp);
EEPROM_READ_VAR(i, waterKi);
EEPROM_READ_VAR(i, waterKd);
#if ENABLED(PIDTEMPCOOLER)
EEPROM_READ_VAR(i, coolerKp);
EEPROM_READ_VAR(i, coolerKi);
EEPROM_READ_VAR(i, coolerKd);
#endif
......@@ -598,10 +598,10 @@ void Config_ResetDefault() {
bedKd = scalePID_d(DEFAULT_bedKd);
#endif
#if ENABLED(PIDTEMPWATER)
waterKp = DEFAULT_waterKp;
waterKi = scalePID_i(DEFAULT_waterKi);
waterKd = scalePID_d(DEFAULT_waterKd);
#if ENABLED(PIDTEMPCOOLER)
coolerKp = DEFAULT_coolerKp;
coolerKi = scalePID_i(DEFAULT_coolerKi);
coolerKd = scalePID_d(DEFAULT_coolerKd);
#endif
......@@ -807,7 +807,7 @@ void Config_ResetDefault() {
ECHO_EM(" (Material GUM)");
#endif // ULTIPANEL
#if ENABLED(PIDTEMP) || ENABLED(PIDTEMPBED) || ENABLED(PIDTEMPWATER)
#if ENABLED(PIDTEMP) || ENABLED(PIDTEMPBED) || ENABLED(PIDTEMPCOOLER)
if (!forReplay) {
ECHO_LM(CFG, "PID settings:");
}
......@@ -831,10 +831,10 @@ void Config_ResetDefault() {
ECHO_MV(" I", unscalePID_i(bedKi));
ECHO_EMV(" D", unscalePID_d(bedKd));
#endif
#if ENABLED(PIDTEMPWATER)
ECHO_SMV(CFG, " M304 L P", waterKp); // for compatibility with hosts, only echos values for E0
ECHO_MV(" I", unscalePID_i(waterKi));
ECHO_EMV(" D", unscalePID_d(waterKd));
#if ENABLED(PIDTEMPCOOLER)
ECHO_SMV(CFG, " M304 L P", coolerKp); // for compatibility with hosts, only echos values for E0
ECHO_MV(" I", unscalePID_i(coolerKi));
ECHO_EMV(" D", unscalePID_d(coolerKd));
#endif
#endif
......
......@@ -149,7 +149,7 @@
* M301 - Set PID parameters P I D and C
* M302 - Allow cold extrudes, or set the minimum extrude S<temperature>.
* M303 - PID relay autotune S<temperature> sets the target temperature (default target temperature = 150C). H<hotend> C<cycles> U<Apply result>
* M304 - Set bed PID parameters P I and D or Water cooling if L parameter
* M304 - Set bed PID parameters P I and D or cooling if L parameter
* M350 - Set microstepping mode.
* M351 - Toggle MS1 MS2 pins directly.
* M380 - Activate solenoid on active extruder
......
......@@ -6479,11 +6479,11 @@ inline void gcode_M226() {
}
#endif // PREVENT_DANGEROUS_EXTRUDE
#if HAS(PID_HEATING)
#if HAS(PID_HEATING) || HAS(PID_COOLING)
/**
* M303: PID relay autotune
* S<temperature> sets the target temperature. (default target temperature = 150C)
* H<hotend> (-1 for the bed) (default 0)
* H<hotend> (-1 for the bed, -2 for cooler) (default 0)
* C<cycles>
* U<bool> with a non-zero value will apply the result to current settings
*/
......@@ -6504,23 +6504,23 @@ inline void gcode_M226() {
}
#endif
#if ENABLED(PIDTEMPBED) || ENABLED(PIDTEMPWATER)
#if ENABLED(PIDTEMPBED) || ENABLED(PIDTEMPCOOLER)
// M304: Set bed PID parameters P I and D
inline void gcode_M304() {
#if ENABLED(PIDTEMPWATER)
#if ENABLED(PIDTEMPCOOLER)
if (code_seen('L')) {
if (code_seen('P')) waterKp = code_value();
if (code_seen('I')) waterKi = scalePID_i(code_value());
if (code_seen('D')) waterKd = scalePID_d(code_value());
if (code_seen('P')) coolerKp = code_value();
if (code_seen('I')) coolerKi = scalePID_i(code_value());
if (code_seen('D')) coolerKd = scalePID_d(code_value());
updatePID();
ECHO_SMV(OK, " L p:", waterKp);
ECHO_MV(" i:", unscalePID_i(waterKi));
ECHO_EMV(" d:", unscalePID_d(waterKd));
ECHO_SMV(OK, " L p:", coolerKp);
ECHO_MV(" i:", unscalePID_i(coolerKi));
ECHO_EMV(" d:", unscalePID_d(coolerKd));
}
#endif
#if ENABLED(PIDTEMPWATER) && ENABLED(PIDTEMPBED)
#if ENABLED(PIDTEMPCOOLER) && ENABLED(PIDTEMPBED)
else {
#endif
if (code_seen('P')) bedKp = code_value();
......@@ -6531,11 +6531,11 @@ inline void gcode_M226() {
ECHO_SMV(OK, "p:", bedKp);
ECHO_MV(" i:", unscalePID_i(bedKi));
ECHO_EMV(" d:", unscalePID_d(bedKd));
#if ENABLED(PIDTEMPWATER) && ENABLED(PIDTEMPBED)
#if ENABLED(PIDTEMPCOOLER) && ENABLED(PIDTEMPBED)
}
#endif
}
#endif // PIDTEMPBED || PIDTEMPWATER
#endif // PIDTEMPBED || PIDTEMPCOOLER
#if HAS(MICROSTEPS)
// M350 Set microstepping mode. Warning: Steps per unit remains unchanged. S code sets stepping mode for all drivers.
......@@ -8318,7 +8318,7 @@ void process_next_command() {
gcode_M303(); break;
#endif
#if ENABLED(PIDTEMPBED) || ENABLED(PIDTEMPWATER)
#if ENABLED(PIDTEMPBED) || ENABLED(PIDTEMPCOOLER)
case 304: // M304
gcode_M304(); break;
#endif // PIDTEMPBED
......@@ -9336,7 +9336,7 @@ void kill(const char* lcd_msg) {
}
#endif
#if ENABLED(FAST_PWM_FAN) || ENABLED(LASER_WATER_COOLING)
#if ENABLED(FAST_PWM_FAN) || ENABLED(LASER_COOLER_COOLING)
void setPwmFrequency(uint8_t pin, int val) {
val &= 0x07;
......
......@@ -117,7 +117,7 @@ void refresh_cmd_timeout();
extern void delay_ms(millis_t ms);
#if ENABLED(FAST_PWM_FAN) || ENABLED(LASER_WATER_COOLING)
#if ENABLED(FAST_PWM_FAN) || ENABLED(COOLER_PWM)
void setPwmFrequency(uint8_t pin, int val);
#endif
......
......@@ -597,6 +597,7 @@
* Flags for PID handling
*/
#define HAS_PID_HEATING (ENABLED(PIDTEMP) || ENABLED(PIDTEMPBED))
#define HAS_PID_HEATING (ENABLED(PIDTEMPCOOLER))
#define HAS_PID_FOR_BOTH (ENABLED(PIDTEMP) && ENABLED(PIDTEMPBED))
/**
......@@ -647,11 +648,13 @@
#define HAS_TEMP_2 (PIN_EXISTS(TEMP_2) && TEMP_SENSOR_2 != 0)
#define HAS_TEMP_3 (PIN_EXISTS(TEMP_3) && TEMP_SENSOR_3 != 0)
#define HAS_TEMP_BED (PIN_EXISTS(TEMP_BED) && TEMP_SENSOR_BED != 0)
#define HAS_TEMP_COOLER (PIN_EXISTS(TEMP_COOLER) && TEMP_SENSOR_COOLER != 0)
#define HAS_HEATER_0 (PIN_EXISTS(HEATER_0))
#define HAS_HEATER_1 (PIN_EXISTS(HEATER_1))
#define HAS_HEATER_2 (PIN_EXISTS(HEATER_2))
#define HAS_HEATER_3 (PIN_EXISTS(HEATER_3))
#define HAS_HEATER_BED (PIN_EXISTS(HEATER_BED))
#define HAS_COOLER (PIN_EXISTS(COOLER))
#define HAS_AUTO_FAN_0 (ENABLED(EXTRUDER_AUTO_FAN) && PIN_EXISTS(EXTRUDER_0_AUTO_FAN))
#define HAS_AUTO_FAN_1 (ENABLED(EXTRUDER_AUTO_FAN) && PIN_EXISTS(EXTRUDER_1_AUTO_FAN))
#define HAS_AUTO_FAN_2 (ENABLED(EXTRUDER_AUTO_FAN) && PIN_EXISTS(EXTRUDER_2_AUTO_FAN))
......
......@@ -191,8 +191,9 @@
#define SERIAL_PID_AUTOTUNE "PID Autotune"
#define SERIAL_PID_AUTOTUNE_START SERIAL_PID_AUTOTUNE " start"
#define SERIAL_PID_AUTOTUNE_FAILED SERIAL_PID_AUTOTUNE " failed!"
#define SERIAL_PID_BAD_EXTRUDER_NUM SERIAL_PID_AUTOTUNE_FAILED " Bad extruder number"
#define SERIAL_PID_BAD_TEMP_CONTROLLER_NUM SERIAL_PID_AUTOTUNE_FAILED " Bad temperature controller number"
#define SERIAL_PID_TEMP_TOO_HIGH SERIAL_PID_AUTOTUNE_FAILED " Temperature too high"
#define SERIAL_PID_TEMP_TOO_LOW SERIAL_PID_AUTOTUNE_FAILED " Temperature too low"
#define SERIAL_PID_TIMEOUT SERIAL_PID_AUTOTUNE_FAILED " timeout"
#define SERIAL_BIAS " bias: "
#define SERIAL_D " d: "
......@@ -221,6 +222,7 @@
#define SERIAL_HEATER_BED "bed"
#define SERIAL_STOPPED_HEATER ", system stopped! Heater_ID: "
#define SERIAL_STOPPED_COOLER "system stopped! Cooler"
//#define SERIAL_REDUNDANCY "Heater switched off. Temperature difference between temp sensors is too high !"
#define SERIAL_T_HEATING_FAILED "Heating failed"
#define SERIAL_T_THERMAL_RUNAWAY "Thermal Runaway"
......
......@@ -223,18 +223,18 @@
#error DEPENDENCY ERROR: Missing setting DEFAULT_bedKd
#endif
#endif
#if ENABLED(PIDTEMPWATER)
#if DISABLED(PID_WATER_INTEGRAL_DRIVE_MAX)
#error DEPENDENCY ERROR: Missing setting PID_WATER_INTEGRAL_DRIVE_MAX
#if ENABLED(PIDTEMPCOOLER)
#if DISABLED(PID_COOLER_INTEGRAL_DRIVE_MAX)
#error DEPENDENCY ERROR: Missing setting PID_COOLER_INTEGRAL_DRIVE_MAX
#endif
#if DISABLED(DEFAULT_waterKp)
#error DEPENDENCY ERROR: Missing setting DEFAULT_waterKp
#if DISABLED(DEFAULT_coolerKp)
#error DEPENDENCY ERROR: Missing setting DEFAULT_coolerKp
#endif
#if DISABLED(DEFAULT_waterKi)
#error DEPENDENCY ERROR: Missing setting DEFAULT_waterKi
#if DISABLED(DEFAULT_coolerKi)
#error DEPENDENCY ERROR: Missing setting DEFAULT_coolerKi
#endif
#if DISABLED(DEFAULT_waterKd)
#error DEPENDENCY ERROR: Missing setting DEFAULT_waterKd
#if DISABLED(DEFAULT_coolerKd)
#error DEPENDENCY ERROR: Missing setting DEFAULT_coolerKd
#endif
#endif
......
......@@ -51,7 +51,7 @@
#define K2 (1.0 - K1)
#endif
#if ENABLED(PIDTEMPBED) || ENABLED(PIDTEMP)
#if ENABLED(PIDTEMPBED) || ENABLED(PIDTEMP) || ENABLED(PIDTEMPCOOLER)
#define PID_dT ((OVERSAMPLENR * 14.0)/(F_CPU / 64.0 / 256.0))
#endif
......@@ -65,9 +65,10 @@ int current_temperature_raw[4] = { 0 };
float current_temperature[4] = { 0.0 };
int current_temperature_bed_raw = 0;
float current_temperature_bed = 0.0;
#if ENABLED(LASER_WATER_COOLING)
int current_temperature_water_raw = 0;
float current_temperature_water = 0.0;
#if ENABLED(COOLER)
int target_temperature_cooler = 0;
int current_temperature_cooler_raw = 0;
float current_temperature_cooler = 0.0;
#endif
#if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
......@@ -81,10 +82,10 @@ float current_temperature_water = 0.0;
float bedKd = ((DEFAULT_bedKd) / (PID_dT));
#endif //PIDTEMPBED
#if ENABLED(PIDTEMPWATER)
float waterKp = DEFAULT_waterKp;
float waterKi = ((DEFAULT_waterKi) * (PID_dT));
float waterKd = ((DEFAULT_waterKd) / (PID_dT));
#if ENABLED(PIDTEMPCOOLER)
float coolerKp = DEFAULT_coolerKp;
float coolerKi = ((DEFAULT_coolerKi) * (PID_dT));
float coolerKd = ((DEFAULT_coolerKd) / (PID_dT));
#endif
#if ENABLED(FAN_SOFT_PWM)
......@@ -107,9 +108,9 @@ unsigned char soft_pwm_bed;
int current_raw_filwidth = 0; //Holds measured filament diameter - one extruder only
#endif
#if ENABLED(THERMAL_PROTECTION_HOTENDS) || ENABLED(THERMAL_PROTECTION_BED)
enum TRState { TRReset, TRInactive, TRFirstHeating, TRStable, TRRunaway };
void thermal_runaway_protection(TRState* state, millis_t* timer, float temperature, float target_temperature, int heater_id, int period_seconds, int hysteresis_degc);
#if ENABLED(THERMAL_PROTECTION_HOTENDS) || ENABLED(THERMAL_PROTECTION_BED) || ENABLED(THERMAL_PROTECTION_COOLER)
enum TRState { TRReset, TRInactive, TRFirstRunnig, TRStable, TRRunaway };
void thermal_runaway_protection(TRState* state, millis_t* timer, float temperature, float target_temperature, int temp_controller_id, int period_seconds, int hysteresis_degc);
#if ENABLED(THERMAL_PROTECTION_HOTENDS)
static TRState thermal_runaway_state_machine[4] = { TRReset, TRReset, TRReset, TRReset };
static millis_t thermal_runaway_timer[4]; // = {0,0,0,0};
......@@ -118,6 +119,10 @@ unsigned char soft_pwm_bed;
static TRState thermal_runaway_bed_state_machine = TRReset;
static millis_t thermal_runaway_bed_timer;
#endif
#if ENABLED(THERMAL_PROTECTION_COOLER) && TEMP_SENSOR_COOLER != 0
static TRState thermal_runaway_cooler_state_machine = TRReset;
static millis_t thermal_runaway_cooler_timer;
#endif
#endif
#if HAS(POWER_CONSUMPTION_SENSOR)
int current_raw_powconsumption = 0; //Holds measured power consumption
......@@ -163,6 +168,19 @@ static volatile bool temp_meas_ready = false;
#else //PIDTEMPBED
static millis_t next_bed_check_ms;
#endif //PIDTEMPBED
#if ENABLED(PIDTEMPCOOLER)
//static cannot be external:
static float temp_iState_cooler = { 0 };
static float temp_dState_cooler = { 0 };
static float pTerm_cooler;
static float iTerm_cooler;
static float dTerm_cooler;
//int output;
static float pid_error_cooler;
static float temp_iState_min_cooler;
static float temp_iState_max_cooler;
#endif
static unsigned char soft_pwm[HOTENDS];
#if ENABLED(FAN_SOFT_PWM)
......@@ -182,6 +200,7 @@ static unsigned char soft_pwm[HOTENDS];
float Kp[HOTENDS], Ki[HOTENDS], Kd[HOTENDS], Kc[HOTENDS];
#endif //PIDTEMP
// Init min and max temp with extreme values to prevent false errors during startup
static int minttemp_raw[HOTENDS] = ARRAY_BY_HOTENDS( HEATER_0_RAW_LO_TEMP , HEATER_1_RAW_LO_TEMP , HEATER_2_RAW_LO_TEMP, HEATER_3_RAW_LO_TEMP);
static int maxttemp_raw[HOTENDS] = ARRAY_BY_HOTENDS( HEATER_0_RAW_HI_TEMP , HEATER_1_RAW_HI_TEMP , HEATER_2_RAW_HI_TEMP, HEATER_3_RAW_HI_TEMP);
......@@ -193,6 +212,12 @@ static int maxttemp[HOTENDS] = ARRAY_BY_HOTENDS1(16383);
#if ENABLED(BED_MAXTEMP)
static int bed_maxttemp_raw = HEATER_BED_RAW_HI_TEMP;
#endif
#if ENABLED(COOLER_MINTEMP)
static int cooler_minttemp_raw = COOLER_COOLER_RAW_LO_TEMP;
#endif
#if ENABLED(COOLER_MAXTEMP)
static int cooler_maxttemp_raw = COOLER_COOLER_RAW_HI_TEMP;
#endif
#if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
static void* heater_ttbl_map[2] = {(void*)HEATER_0_TEMPTABLE, (void*)HEATER_1_TEMPTABLE };
......@@ -204,6 +229,7 @@ static int maxttemp[HOTENDS] = ARRAY_BY_HOTENDS1(16383);
static float analog2temp(int raw, uint8_t e);
static float analog2tempBed(int raw);
static float analog2tempCooler(int raw);
static void updateTemperaturesFromRawValues();
#if ENABLED(THERMAL_PROTECTION_HOTENDS)
......@@ -237,11 +263,11 @@ void autotempShutdown() {
#endif
}
#if HAS(PID_HEATING)
void PID_autotune(float temp, int hotend, int ncycles, bool set_result/*=false*/) {
#if HAS(PID_HEATING) || HAS(PID_COOLING)
void PID_autotune(float temp, int temp_controller, int ncycles, bool set_result/*=false*/) {
float input = 0.0;
int cycles = 0;
bool heating = true;
bool running = true;
millis_t temp_ms = millis(), t1 = temp_ms, t2 = temp_ms;
long t_high = 0, t_low = 0;
......@@ -255,31 +281,41 @@ void autotempShutdown() {
millis_t next_auto_fan_check_ms = temp_ms + 2500;
#endif
if (hotend >= HOTENDS
#if HASNT(TEMP_BED)
|| hotend < 0
if (temp_controller >= HOTENDS
#if HASNT(TEMP_BED) && HASNT(TEMP_COOLER)
|| temp_controller < 0
#elif HASNT(TEMP_BED)
|| (temp_controller == -1 && temp_controller < -2)
#elif HASNT(TEMP_COOLER)
|| temp_controller < -1
#endif
) {
ECHO_LM(ER, SERIAL_PID_BAD_EXTRUDER_NUM);
ECHO_LM(ER, SERIAL_PID_BAD_TEMP_CONTROLLER_NUM);
return;
}
ECHO_LM(DB, SERIAL_PID_AUTOTUNE_START);
if (hotend < 0) {
if (temp_controller == -1) {
ECHO_SM(DB, "BED");
}
else if(hotend < -1) {
ECHO_SM(DB, "COOLER");
}
else {
ECHO_SMV(DB, "Hotend: ", hotend);
ECHO_SMV(DB, "Hotend: ", temp_controller);
}
ECHO_MV(" Temp: ", temp);
ECHO_EMV(" Cycles: ", ncycles);
disable_all_heaters(); // switch off all heaters.
disable_all_coolers(); // switch off all coolers.
if (hotend < 0)
if (temp_controller == -1)
soft_pwm_bed = bias = d = MAX_BED_POWER / 2;
else if(temp_controller < -1)
soft_pwm_cooler = bias = d = MAX_COOLER_POWER / 2;
else
soft_pwm[hotend] = bias = d = PID_MAX / 2;
soft_pwm[temp_controller] = bias = d = PID_MAX / 2;
// PID Tuning loop
for (;;) {
......@@ -289,7 +325,12 @@ void autotempShutdown() {
if (temp_meas_ready) { // temp sample ready
updateTemperaturesFromRawValues();
input = (hotend < 0) ? current_temperature_bed:current_temperature[hotend];
if(temp_controller == -1)
input = current_temperature_bed;
else if(temp_controller < -1)
input = current_temperature_cooler;
else {
input = current_temperature[temp_controller];
max = max(max, input);
min = min(min, input);
......@@ -301,26 +342,34 @@ void autotempShutdown() {
}
#endif
if (heating && input > temp) {
if (running && ((input > temp && temp_controller >= -1) || (input < temp && temp_controller < -1))) {
if (ms > t2 + 5000) {
heating = false;
if (hotend < 0)
runnig = false;
if (temp_controller < -1)
soft_pwm_cooler = (bias - d) >> 1;
else if (temp_controller < 0)
soft_pwm_bed = (bias - d) >> 1;
else
soft_pwm[hotend] = (bias - d) >> 1;
soft_pwm[temp_controller] = (bias - d) >> 1;
t1 = ms;
t_high = t1 - t2;
max = temp;
if (temp_controller < -1)
min = temp;
else
max = temp;
}
}
if (!heating && input < temp) {
if (!running && ((input < temp && temp_controller >= -1) || (input > temp && temp_controller < -1))) {
if (ms > t1 + 5000) {
heating = true;
running = true;
t2 = ms;
t_low = t2 - t1;
if (cycles > 0) {
long max_pow = hotend < 0 ? MAX_BED_POWER : PID_MAX;
if (temp_controller < -1)
long max_pow = MAX_COOLER_POWER;
else:
long max_pow = hotend < 0 ? MAX_BED_POWER : PID_MAX;
bias += (d * (t_high - t_low)) / (t_low + t_high);
bias = constrain(bias, 20, max_pow - 20);
d = (bias > max_pow / 2) ? max_pow - 1 - bias : bias;
......@@ -347,32 +396,44 @@ void autotempShutdown() {
ECHO_E;
}
}
#if HAS(PID_FOR_BOTH)
if (hotend < 0)
#if ENABLED(PIDTEMP)
if (temp_controller >= 0)
soft_pwm[hotend] = (bias + d) >> 1;
#endif
#if ENABLED(PIDTEMPBED)
if (temp_controller == -1)
soft_pwm_bed = (bias + d) >> 1;
else
soft_pwm[hotend] = (bias + d) >> 1;
#elif ENABLED(PIDTEMP)
soft_pwm[hotend] = (bias + d) >> 1;
#else
soft_pwm_bed = (bias + d) >> 1;
#endif
#if ENABLED(PIDTEMPCOOLER)
if (temp_controller < -1)
soft_pwm_cooler = (bias + d) >> 1;
#endif
cycles++;
min = temp;
if(temp_cooler < -1)
max = temp;
else
min = temp;
}
}
}
if (input > temp + MAX_OVERSHOOT_PID_AUTOTUNE) {
if (input > temp + MAX_OVERSHOOT_PID_AUTOTUNE && temp_controller >= -1) {
ECHO_LM(ER, SERIAL_PID_TEMP_TOO_HIGH);
return;
}
}
else if (input < temp + MAX_OVERSHOOT_PID_AUTOTUNE && temp_controller < -1) {
ECHO_LM(ER, SERIAL_PID_TEMP_TOO_LOW);
return;
}
// Every 2 seconds...
if (ELAPSED(ms, temp_ms + 2000UL)) {
#if HAS(TEMP_0) || HAS(TEMP_BED) || ENABLED(HEATER_0_USES_MAX6675)
print_heaterstates();
ECHO_E;
#endif
#if HAS(TEMP_COOLER)
print_coolerstates();
ECHO_E;
#endif
temp_ms = ms;
} // every 2 seconds
......@@ -384,8 +445,8 @@ void autotempShutdown() {
}
if (cycles > ncycles) {
ECHO_LM(DB, SERIAL_PID_AUTOTUNE_FINISHED);
#if HAS(PID_FOR_BOTH)
if (hotend >= 0) {
#if ENABLED(PIDTEMP)
if (temp_controller >= 0) {
ECHO_SMV(DB, SERIAL_KP, PID_PARAM(Kp, hotend));
ECHO_MV(SERIAL_KI, unscalePID_i(PID_PARAM(Ki, hotend)));
ECHO_EMV(SERIAL_KD, unscalePID_d(PID_PARAM(Kd, hotend)));
......@@ -396,7 +457,9 @@ void autotempShutdown() {
updatePID();
}
}
else {
#endif
#if ENABLED(PIDTEMPBED)
if(temp_controller == -1) {
ECHO_LMV(DB, "#define DEFAULT_bedKp ", workKp);
ECHO_LMV(DB, "#define DEFAULT_bedKi ", unscalePID_i(workKi));
ECHO_LMV(DB, "#define DEFAULT_bedKd ", unscalePID_d(workKd));
......@@ -407,25 +470,18 @@ void autotempShutdown() {
updatePID();
}
}
#elif ENABLED(PIDTEMP)
ECHO_SMV(DB, SERIAL_KP, PID_PARAM(Kp, hotend));
ECHO_MV(SERIAL_KI, unscalePID_i(PID_PARAM(Ki, hotend)));
ECHO_EMV(SERIAL_KD, unscalePID_d(PID_PARAM(Kd, hotend)));
if (set_result) {
PID_PARAM(Kp, hotend) = workKp;
PID_PARAM(Ki, hotend) = scalePID_i(workKi);
PID_PARAM(Kd, hotend) = scalePID_d(workKd);
updatePID();
}
#else
ECHO_LMV(DB, "#define DEFAULT_bedKp ", workKp);
ECHO_LMV(DB, "#define DEFAULT_bedKi ", unscalePID_i(workKi));
ECHO_LMV(DB, "#define DEFAULT_bedKd ", unscalePID_d(workKd));
if (set_result) {
bedKp = workKp;
bedKi = scalePID_i(workKi);
bedKd = scalePID_d(workKd);
updatePID();
#endif
#if ENABLED(PIDTEMPCOOLER)
if(temp_controller < -1) {
ECHO_LMV(DB, "#define DEFAULT_coolerKp ", workKp);
ECHO_LMV(DB, "#define DEFAULT_coolerKi ", unscalePID_i(workKi));
ECHO_LMV(DB, "#define DEFAULT_coolerKd ", unscalePID_d(workKd));
if (set_result) {
coolerKp = workKp;
coolerKi = scalePID_i(workKi);
coolerKd = scalePID_d(workKd);
updatePID();
}
}
#endif
......@@ -448,12 +504,19 @@ void updatePID() {
#if ENABLED(PIDTEMPBED)
temp_iState_max_bed = PID_BED_INTEGRAL_DRIVE_MAX / bedKi;
#endif
#if ENABLED(PIDTEMPCOOLER)
temp_iState_max_cooler = PID_COOLER_INTEGRAL_DRIVE_MAX / coolerKi;
#endif
}
int getHeaterPower(int heater) {
return heater < 0 ? soft_pwm_bed : soft_pwm[heater];
}
int getCoolerPower(void) {
return soft_pwm_cooler;
}
#if HAS(AUTO_FAN)
void setExtruderAutoFanState(int pin, bool state) {
......@@ -532,12 +595,20 @@ void checkExtruderAutoFans() {
//
// Temperature Error Handlers
//
inline void _temp_error(int h, const char* serial_msg, const char* lcd_msg) {
inline void _temp_error(int tc, const char* serial_msg, const char* lcd_msg) {
static bool killed = false;
if (IsRunning()) {
ECHO_ST(ER, serial_msg);
ECHO_M(SERIAL_STOPPED_HEATER);
if (h >= 0) ECHO_EV((int)h); else ECHO_EM(SERIAL_HEATER_BED);
if (tc >= 0) {
ECHO_M(SERIAL_STOPPED_HEATER);
ECHO_EV((int)tc);
}
else if (tc == -1) {
ECHO_M(SERIAL_STOPPED_HEATER);
ECHO_EM(SERIAL_HEATER_BED);
}
else
ECHO_EM(SERIAL_STOPPED_COOLER);
#if ENABLED(ULTRA_LCD)
lcd_setalertstatuspgm(lcd_msg);
#endif
......@@ -548,8 +619,10 @@ inline void _temp_error(int h, const char* serial_msg, const char* lcd_msg) {
killed = true;
kill(lcd_msg);
}
else
disable_all_heaters(); // paranoia
else {
disable_all_heaters(); // paranoia
disable_all_coolers();
}
#endif
}
......@@ -560,7 +633,7 @@ void min_temp_error(uint8_t h) {
_temp_error(h, PSTR(SERIAL_T_MINTEMP), PSTR(MSG_ERR_MINTEMP));
}
float get_pid_output(int h) {
float get_pid_output(int h) {
float pid_output;
#if ENABLED(PIDTEMP)
#if ENABLED(PID_OPENLOOP)
......@@ -690,6 +763,48 @@ float get_pid_output(int h) {
}
#endif
#if ENABLED(PIDTEMPCOOLER)
float get_pid_output_cooler() {
float pid_output;
#if ENABLED(PID_OPENLOOP)
pid_output = constrain(target_temperature_cooler, 0, MAX_COOLER_POWER);
#else
//pid_error_cooler = target_temperature_cooler - current_temperature_cooler;
pid_error_cooler = current_temperature_cooler - target_temperature_cooler;
pTerm_cooler = coolerKp * pid_error_cooler;
temp_iState_cooler += pid_error_cooler;
temp_iState_cooler = constrain(temp_iState_cooler, temp_iState_min_cooler, temp_iState_max_cooler);
iTerm_cooler = coolerKi * temp_iState_cooler;
//dTerm_cooler = K2 * coolerKd * (current_temperature_cooler - temp_dState_cooler) + K1 * dTerm_cooler;
dTerm_cooler = K2 * coolerKd * (temp_dState_cooler - current_temperature_cooler) + K1 * dTerm_cooler;
temp_dState_cooler = current_temperature_cooler;
pid_output = pTerm_cooler + iTerm_cooler - dTerm_cooler;
if (pid_output > MAX_COOLER_POWER) {
if (pid_error_cooler > 0) temp_iState_cooler -= pid_error_cooler; // conditional un-integration
pid_output = MAX_COOLER_POWER;
}
else if (pid_output < 0) {
if (pid_error_cooler < 0) temp_iState_cooler -= pid_error_cooler; // conditional un-integration
pid_output = 0;
}
#endif // PID_OPENLOOP
#if ENABLED(PID_COOLER_DEBUG)
ECHO_SM(DB ," PID_COOLER_DEBUG ");
ECHO_MV(": Input ", current_temperature_cooler);
ECHO_MV(" Output ", pid_output);
ECHO_MV(" pTerm ", pTerm_cooler);
ECHO_MV(" iTerm ", iTerm_cooler);
ECHO_EMV(" dTerm ", dTerm_cooler);
#endif //PID_COOLER_DEBUG
}
#endif
/**
* Manage heating activities for extruder hot-ends and a heated bed
* - Acquire updated temperature readings
......@@ -698,7 +813,7 @@ float get_pid_output(int h) {
* - Apply filament width to the extrusion rate (may move)
* - Update the heated bed PID output value
*/
void manage_heater() {
void manage_heater() { // NEXTIME cambia nome GUARDA TUTTA LA FUNZIONE
if (!temp_meas_ready) return;
......@@ -868,7 +983,7 @@ static float analog2temp(int raw, uint8_t h) {
// Derived from RepRap FiveD extruder::getTemperature()
// For bed temperature measurement.
static float analog2tempBed(int raw) {
static float analog2tempBed(int raw) {
#if ENABLED(BED_USES_THERMISTOR)
float celsius = 0;
byte i;
......@@ -901,9 +1016,44 @@ static float analog2tempBed(int raw) {
#endif
}
static float analog2tempCooler(int raw) {
#if ENABLED(BED_COOLER_THERMISTOR)
float celsius = 0;
byte i;
for (i = 1; i < COOLERTEMPTABLE_LEN; i++) {
if (PGM_RD_W(COOLERTEMPTABLE[i][0]) > raw) {
celsius = PGM_RD_W(COOLERTEMPTABLE[i - 1][1]) +
(raw - PGM_RD_W(COOLERTEMPTABLE[i - 1][0])) *
(float)(PGM_RD_W(COOLERTEMPTABLE[i][1]) - PGM_RD_W(COOLERTEMPTABLE[i - 1][1])) /
(float)(PGM_RD_W(COOLERTEMPTABLE[i][0]) - PGM_RD_W(COOLERTEMPTABLE[i - 1][0]));
break;
}
}
// Overflow: Set to last value in the table
if (i == COOLERTEMPTABLE_LEN) celsius = PGM_RD_W(COOLERTEMPTABLE[i - 1][1]);
return celsius;
#elif ENABLED(COOLER_USES_AD595)
#ifdef __SAM3X8E__
return ((raw * ((3.3 * 100.0) / 1024.0) / OVERSAMPLENR) * ad595_gain[h]) + ad595_offset[h];
#else
return ((raw * ((5.0 * 100.0) / 1024.0) / OVERSAMPLENR) * TEMP_SENSOR_AD595_GAIN) + TEMP_SENSOR_AD595_OFFSET;
#endif
#else
UNUSED(raw);
return 0;
#endif
}
/* Called to get the raw values into the the actual temperatures. The raw values are created in interrupt context,
and this function is called from normal context as it is too slow to run in interrupts and will block the stepper routine otherwise */
static void updateTemperaturesFromRawValues() {
static void updateTemperaturesFromRawValues() {
static millis_t last_update = millis();
millis_t temp_last_update = millis();
millis_t from_last_update = temp_last_update - last_update;
......@@ -914,6 +1064,7 @@ static void updateTemperaturesFromRawValues() {
current_temperature[h] = analog2temp(current_temperature_raw[h], h);
}
current_temperature_bed = analog2tempBed(current_temperature_bed_raw);
current_temperature_cooler = analog2TempCooler(current_temperature_cooler_raw);
#if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
redundant_temperature = analog2temp(redundant_temperature_raw, 1);
#endif
......@@ -1007,8 +1158,8 @@ static void updateTemperaturesFromRawValues() {
* Initialize the temperature manager
* The manager is implemented by periodic calls to manage_heater()
*/
void tp_init() {
#if MB(RUMBA) && ((TEMP_SENSOR_0==-1)||(TEMP_SENSOR_1==-1)||(TEMP_SENSOR_2==-1)||(TEMP_SENSOR_BED==-1))
void tp_init() {
#if MB(RUMBA) && ((TEMP_SENSOR_0==-1)||(TEMP_SENSOR_1==-1)||(TEMP_SENSOR_2==-1)||(TEMP_SENSOR_BED==-1)||(TEMP_SENSOR_COOLER==-1)
// disable RUMBA JTAG in case the thermocouple extension is plugged on top of JTAG connector
MCUCR = _BV(JTD);
MCUCR = _BV(JTD);
......@@ -1026,6 +1177,10 @@ void tp_init() {
temp_iState_min_bed = 0.0;
temp_iState_max_bed = PID_BED_INTEGRAL_DRIVE_MAX / bedKi;
#endif // PIDTEMPBED
#if ENABLED(PIDTEMPCOOLER)
temp_iState_min_cooler = 0.0;
temp_iState_max_cooler = PID_COOLER_INTEGRAL_DRIVE_MAX / coolerKi;
#endif
}
#if ENABLED(PID_ADD_EXTRUSION_RATE)
......@@ -1047,6 +1202,14 @@ void tp_init() {
#if HAS(HEATER_BED)
SET_OUTPUT(HEATER_BED_PIN);
#endif
#if HAS(COOLER)
SET_OUTPUT(COOLER_PIN)
#if ENABLED(COOLER_PWM)
setPwmFrequency(COOLER_PIN, 2); // No prescaling. Pwm frequency = F_CPU/256/64
#else
soft_pwm_cooler = coolerSpeedSoftPwm / 2;
#endif
#endif
#if HAS(FAN)
SET_OUTPUT(FAN_PIN);
#if ENABLED(FAST_PWM_FAN)
......@@ -1057,11 +1220,6 @@ void tp_init() {
#endif
#endif
#if ENABLED(LASER_WATER_COOLING)
SET_OUTPUT(LASER_WATER_COOLING_PIN);
setPwmFrequency(LASER_WATER_COOLING_PIN, 2); // No prescaling. Pwm frequency = F_CPU/256/64
#endif
#if ENABLED(HEATER_0_USES_MAX6675)
#if DISABLED(SDSUPPORT)
......@@ -1101,6 +1259,9 @@ void tp_init() {
#if HAS(TEMP_BED)
ANALOG_SELECT(TEMP_BED_PIN);
#endif
#if HAS(TEMP_COOLER)
ANALOG_SELECT(TEMP_COOLER_PIN);
#endif
#if HAS(FILAMENT_SENSOR)
ANALOG_SELECT(FILWIDTH_PIN);
#endif
......@@ -1224,9 +1385,28 @@ void tp_init() {
#endif
}
#endif // BED_MAXTEMP
#if ENABLED(COOLER_MINTEMP)
while(analog2tempCooler(cooler_minttemp_raw) < COOLER_MINTEMP) {
#if COOLER_RAW_LO_TEMP < HEATER_COOLER_RAW_HI_TEMP
cooler_minttemp_raw += OVERSAMPLENR;
#else
cooler_minttemp_raw -= OVERSAMPLENR;
#endif
}
#endif //COOLER_MINTEMP
#if ENABLED(COOLER_MAXTEMP)
while(analog2tempCooler(cooler_maxttemp_raw) > COOLER_MAXTEMP) {
#if COOLER_RAW_LO_TEMP < COOLER_RAW_HI_TEMP
cooler_maxttemp_raw -= OVERSAMPLENR;
#else
cooler_maxttemp_raw += OVERSAMPLENR;
#endif
}
#endif // BED_MAXTEMP
}
#if ENABLED(THERMAL_PROTECTION_HOTENDS)
#if ENABLED(THERMAL_PROTECTION_HOTENDS) // NEXTIME this has be done even for cooler, but cooler uses M140? or anything else?
/**
* Start Heating Sanity Check for hotends that are below
* their target temperature by a configurable margin.
......@@ -1242,11 +1422,11 @@ void tp_init() {
}
#endif
#if ENABLED(THERMAL_PROTECTION_HOTENDS) || ENABLED(THERMAL_PROTECTION_BED)
#if ENABLED(THERMAL_PROTECTION_HOTENDS) || ENABLED(THERMAL_PROTECTION_BED) || ENABLED(THERMAL_PROTECTION_COOLER)
void thermal_runaway_protection(TRState* state, millis_t* timer, float temperature, float target_temperature, int heater_id, int period_seconds, int hysteresis_degc) {
void thermal_runaway_protection(TRState* state, millis_t* timer, float temperature, float target_temperature, int temp_controller_id, int period_seconds, int hysteresis_degc) {
static float tr_target_temperature[HOTENDS + 1] = { 0.0 };
static float tr_target_temperature[HOTENDS + 2] = { 0.0 };
/*
ECHO_SM(DB, "Thermal Thermal Runaway Running. Heater ID: ");
......@@ -1256,11 +1436,17 @@ void tp_init() {
ECHO_MV(" ; Temperature:", temperature);
ECHO_EMV(" ; Target Temp:", target_temperature);
*/
int temp_controller_index;
int heater_index = heater_id >= 0 ? heater_id : HOTENDS;
if(temp_controller_id >= 0)
temp_controller_index = temp_controller_id;
else if(temp_controller_id == -1)
temp_controller_index = HOTENDS; // BED
else:
temp_controller_index = HOTENDS + 1; // COOLER
// If the target temperature changes, restart
if (tr_target_temperature[heater_index] != target_temperature)
if (tr_target_temperature[temp_controller_index] != target_temperature)
*state = TRReset;
switch (*state) {
......@@ -1270,31 +1456,42 @@ void tp_init() {
// Inactive state waits for a target temperature to be set
case TRInactive:
if (target_temperature > 0) {
tr_target_temperature[heater_index] = target_temperature;
*state = TRFirstHeating;
tr_target_temperature[temp_controller_index] = target_temperature;
*state = TRFirstRunning;
}
break;
// When first heating, wait for the temperature to be reached then go to Stable state
case TRFirstHeating:
if (temperature >= tr_target_temperature[heater_index]) *state = TRStable;
// When first heating/cooling, wait for the temperature to be reached then go to Stable state
case TRFirstRunning:
if (temperature <= tr_target_temperature[temp_controller_index] && temp_controller_index > HEATERS) *state = TRStable;
else if((temperature >= tr_target_temperature[temp_controller_index] && temp_controller_index <= HEATERS) *state = TRStable;
break;
// While the temperature is stable watch for a bad temperature
case TRStable:
// If the temperature is over the target (-hysteresis) restart the timer
if (temperature >= tr_target_temperature[heater_index] - hysteresis_degc)
*timer = millis();
// If the timer goes too long without a reset, trigger shutdown
else if (millis() > *timer + period_seconds * 1000UL)
*state = TRRunaway;
// If the temperature is over (-hysteresys, for heaters) or lower (+hysteresys, for coolers) the target restart the timer
if(temp_controller_index <= HEATERS) { // HEATERS
if (temperature >= tr_target_temperature[temp_controller_index] - hysteresis_degc)
*timer = millis();
// If the timer goes too long without a reset, trigger shutdown
else if (millis() > *timer + period_seconds * 1000UL)
*state = TRRunaway;
}
else { // COOLERS
if (temperature <= tr_target_temperature[temp_controller_index] + hysteresis_degc)
*timer = millis();
// If the timer goes too long without a reset, trigger shutdown
else if (millis() > *timer + period_seconds * 1000UL)
*state = TRRunaway;
}
break;
case TRRunaway:
_temp_error(heater_id, PSTR(SERIAL_T_THERMAL_RUNAWAY), PSTR(MSG_THERMAL_RUNAWAY));
_temp_error(temp_controller_id, PSTR(SERIAL_T_THERMAL_RUNAWAY), PSTR(MSG_THERMAL_RUNAWAY));
}
}
#endif // THERMAL_PROTECTION_HOTENDS || THERMAL_PROTECTION_BED
void disable_all_heaters() {
void disable_all_heaters() {
for (int i = 0; i < HOTENDS; i++) setTargetHotend(0, i);
setTargetBed(0);
......@@ -1392,11 +1589,13 @@ void disable_all_heaters() {
/**
* Stages in the ISR loop
*/
enum TempState {
enum TempState {
PrepareTemp_0,
MeasureTemp_0,
PrepareTemp_BED,
MeasureTemp_BED,
PrepareTemp_COOLER,
MeasureTemp_COOLER,
PrepareTemp_1,
MeasureTemp_1,
PrepareTemp_2,
......@@ -1412,6 +1611,8 @@ enum TempState {
static unsigned long raw_temp_value[4] = { 0 };
static unsigned long raw_temp_bed_value = 0;
static unsigned long raw_temp_cooler_value = 0;
static void set_current_temp_raw() {
#if HAS(TEMP_0) && DISABLED(HEATER_0_USES_MAX6675)
......@@ -1431,6 +1632,7 @@ static void set_current_temp_raw() {
#endif
#endif
current_temperature_bed_raw = raw_temp_bed_value;
current_temperature_cooler_raw = raw_temp_cooler_value;
#if HAS(POWER_CONSUMPTION_SENSOR)
current_raw_powconsumption = raw_powconsumption_value;
......@@ -1445,7 +1647,7 @@ static void set_current_temp_raw() {
* - Check new temperature values for MIN/MAX errors
* - Step the babysteps value for each axis towards 0
*/
ISR(TIMER0_COMPB_vect) {
ISR(TIMER0_COMPB_vect) { // NEXTIME tutta la funzione
static unsigned char temp_count = 0;
static TempState temp_state = StartupDelay;
......@@ -1752,9 +1954,21 @@ ISR(TIMER0_COMPB_vect) {
#if HAS(TEMP_BED)
raw_temp_bed_value += ADC;
#endif
temp_state = PrepareTemp_COOLER;
break;
case PrepareTemp_COOLER:
#if HAS(TEMP_COOLER)
START_ADC(TEMP_COOLER_PIN);
#endif
lcd_buttons_update();
temp_state = MeasureTemp_COOLER;
break;
case MeasureTemp_COOLER:
#if HAS(TEMP_COOLER)
raw_temp_cooler_value += ADC;
#endif
temp_state = PrepareTemp_1;
break;
case PrepareTemp_1:
#if HAS(TEMP_1)
START_ADC(TEMP_1_PIN);
......@@ -1924,7 +2138,7 @@ ISR(TIMER0_COMPB_vect) {
#endif //BABYSTEPPING
}
#if ENABLED(PIDTEMP) || ENABLED(PIDTEMPBED)
#if ENABLED(PIDTEMP) || ENABLED(PIDTEMPBED) || ENABLED(PIDTEMPCOOLER)
// Apply the scale factors to the PID values
float scalePID_i(float i) { return i * PID_dT; }
float unscalePID_i(float i) { return i / PID_dT; }
......
......@@ -72,9 +72,12 @@ extern float current_temperature[4];
#if ENABLED(SHOW_TEMP_ADC_VALUES)
extern int current_temperature_raw[4];
extern int current_temperature_bed_raw;
extern int current_temperature_cooler_raw;
#endif
extern int target_temperature_bed;
extern float current_temperature_bed;
extern int target_temperature_cooler;
extern float current_temperature_cooler;
#if ENABLED(TEMP_SENSOR_1_AS_REDUNDANT)
extern float redundant_temperature;
#endif
......@@ -92,7 +95,11 @@ extern float current_temperature_bed;
extern float bedKp, bedKi, bedKd;
#endif
#if ENABLED(PIDTEMP) || ENABLED(PIDTEMPBED)
#if ENABLED(PIDTEMPCOOLER)
extern float coolerKp, coolerKi, coolerKd;
#endif
#if ENABLED(PIDTEMP) || ENABLED(PIDTEMPBED) || ENABLED(PIDTEMPCOOLER)
float scalePID_i(float i);
float scalePID_d(float d);
float unscalePID_i(float i);
......@@ -114,14 +121,17 @@ extern float current_temperature_bed;
FORCE_INLINE float degHotend(uint8_t hotend) { return current_temperature[HOTEND_ARG]; }
FORCE_INLINE float degBed() { return current_temperature_bed; }
FORCE_INLINE float degCooler() { return current_temperature_cooler; }
#if ENABLED(SHOW_TEMP_ADC_VALUES)
FORCE_INLINE float rawHotendTemp(uint8_t hotend) { return current_temperature_raw[HOTEND_ARG]; }
FORCE_INLINE float rawBedTemp() { return current_temperature_bed_raw; }
FORCE_INLINE float rawCoolerTemp() { return current_temperature_cooler_raw; }
#endif
FORCE_INLINE float degTargetHotend(uint8_t hotend) { return target_temperature[HOTEND_ARG]; }
FORCE_INLINE float degTargetBed() { return target_temperature_bed; }
FORCE_INLINE float degTargetCooler() { return target_temperature_cooler; }
#if ENABLED(THERMAL_PROTECTION_HOTENDS)
void start_watching_heater(int h = 0);
......@@ -134,12 +144,15 @@ FORCE_INLINE void setTargetHotend(const float& celsius, uint8_t hotend) {
#endif
}
FORCE_INLINE void setTargetBed(const float& celsius) { target_temperature_bed = celsius; }
FORCE_INLINE void setTargetCooler(const float& celsius) { target_temperature_cooler = celsius; }
FORCE_INLINE bool isHeatingHotend(uint8_t hotend) { return target_temperature[HOTEND_ARG] > current_temperature[HOTEND_ARG]; }
FORCE_INLINE bool isHeatingBed() { return target_temperature_bed > current_temperature_bed; }
FORCE_INLINE bool isHeatingCooler() { return target_temperature_cooler > current_temperature_cooler; }
FORCE_INLINE bool isCoolingHotend(uint8_t hotend) { return target_temperature[HOTEND_ARG] < current_temperature[HOTEND_ARG]; }
FORCE_INLINE bool isCoolingBed() { return target_temperature_bed < current_temperature_bed; }
FORCE_INLINE bool isCoolingCooler() { return target_temperature_cooler < current_temperature_cooler; }
#define HOTEND_ROUTINES(NR) \
FORCE_INLINE float degHotend##NR() { return degHotend(NR); } \
......@@ -165,11 +178,14 @@ HOTEND_ROUTINES(0);
#endif
int getHeaterPower(int heater);
void disable_all_heaters();
int getCoolerPower(void);
void disable_all_heaters();
#define disable_all_coolers() setTargetCooler(0)
void updatePID();
#if HAS(PID_HEATING)
void PID_autotune(float temp, int hotend, int ncycles, bool set_result = false);
#if HAS(PID_HEATING) || HAS(PID_COOLING)
void PID_autotune(float temp, int temp_controller, int ncycles, bool set_result = false);
#endif
void setExtruderAutoFanState(int pin, bool state);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment