Skip to content

Commit

Permalink
πŸ§‘β€πŸ’» Add Temperature::is_above_target
Browse files Browse the repository at this point in the history
  • Loading branch information
thinkyhead committed Jan 12, 2023
1 parent 58e9dc0 commit 9383c2a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 28 deletions.
12 changes: 6 additions & 6 deletions Marlin/src/lcd/e3v2/jyersui/dwin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1213,7 +1213,7 @@ void CrealityDWINClass::Menu_Item_Handler(uint8_t menu, uint8_t item, bool draw/
if (thermalManager.temp_hotend[0].target < thermalManager.extrude_min_temp)
Popup_Handler(ETemp);
else {
if (thermalManager.temp_hotend[0].is_below_target(-2)) {
if (thermalManager.temp_hotend[0].is_below_target(2)) {
Popup_Handler(Heating);
thermalManager.wait_for_hotend(0);
}
Expand Down Expand Up @@ -1356,7 +1356,7 @@ void CrealityDWINClass::Menu_Item_Handler(uint8_t menu, uint8_t item, bool draw/
Popup_Handler(ETemp);
}
else {
if (thermalManager.temp_hotend[0].is_below_target(-2)) {
if (thermalManager.temp_hotend[0].is_below_target(2)) {
Popup_Handler(Heating);
thermalManager.wait_for_hotend(0);
Redraw_Menu();
Expand Down Expand Up @@ -1743,7 +1743,7 @@ void CrealityDWINClass::Menu_Item_Handler(uint8_t menu, uint8_t item, bool draw/
if (thermalManager.temp_hotend[0].target < thermalManager.extrude_min_temp)
Popup_Handler(ETemp);
else {
if (thermalManager.temp_hotend[0].is_below_target(-2)) {
if (thermalManager.temp_hotend[0].is_below_target(2)) {
Popup_Handler(Heating);
thermalManager.wait_for_hotend(0);
}
Expand All @@ -1762,7 +1762,7 @@ void CrealityDWINClass::Menu_Item_Handler(uint8_t menu, uint8_t item, bool draw/
Popup_Handler(ETemp);
}
else {
if (thermalManager.temp_hotend[0].is_below_target(-2)) {
if (thermalManager.temp_hotend[0].is_below_target(2)) {
Popup_Handler(Heating);
thermalManager.wait_for_hotend(0);
}
Expand All @@ -1780,7 +1780,7 @@ void CrealityDWINClass::Menu_Item_Handler(uint8_t menu, uint8_t item, bool draw/
if (thermalManager.temp_hotend[0].target < thermalManager.extrude_min_temp)
Popup_Handler(ETemp);
else {
if (thermalManager.temp_hotend[0].is_below_target(-2)) {
if (thermalManager.temp_hotend[0].is_below_target(2)) {
Popup_Handler(Heating);
thermalManager.wait_for_hotend(0);
}
Expand Down Expand Up @@ -4505,7 +4505,7 @@ void CrealityDWINClass::Popup_Control() {
if (thermalManager.temp_hotend[0].target < thermalManager.extrude_min_temp)
Popup_Handler(ETemp);
else {
if (thermalManager.temp_hotend[0].is_below_target(-2)) {
if (thermalManager.temp_hotend[0].is_below_target(2)) {
Popup_Handler(Heating);
thermalManager.wait_for_hotend(0);
}
Expand Down
42 changes: 21 additions & 21 deletions Marlin/src/module/temperature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1619,22 +1619,25 @@ void Temperature::mintemp_error(const heater_id_t heater_id) {
#endif

#if HEATER_IDLE_HANDLER
if (heater_idle[IDLE_INDEX_BED].timed_out) {
const bool bed_timed_out = heater_idle[IDLE_INDEX_BED].timed_out;
if (bed_timed_out) {
temp_bed.soft_pwm_amount = 0;
if (DISABLED(PIDTEMPBED)) WRITE_HEATER_BED(LOW);
}
else
#else
constexpr bool bed_timed_out = false;
#endif
{

if (!bed_timed_out) {
#if ENABLED(PIDTEMPBED)
temp_bed.soft_pwm_amount = WITHIN(temp_bed.celsius, BED_MINTEMP, BED_MAXTEMP) ? (int)get_pid_output_bed() >> 1 : 0;
#else
// Check if temperature is within the correct band
if (WITHIN(temp_bed.celsius, BED_MINTEMP, BED_MAXTEMP)) {
#if ENABLED(BED_LIMIT_SWITCHING)
if (temp_bed.celsius >= temp_bed.target + BED_HYSTERESIS)
if (temp_bed.is_above_target((BED_HYSTERESIS) - 1))
temp_bed.soft_pwm_amount = 0;
else if (temp_bed.is_below_target(-(BED_HYSTERESIS) + 1))
else if (temp_bed.is_below_target((BED_HYSTERESIS) - 1))
temp_bed.soft_pwm_amount = MAX_BED_POWER >> 1;
#else // !PIDTEMPBED && !BED_LIMIT_SWITCHING
temp_bed.soft_pwm_amount = temp_bed.is_below_target() ? MAX_BED_POWER >> 1 : 0;
Expand All @@ -1661,7 +1664,7 @@ void Temperature::mintemp_error(const heater_id_t heater_id) {
#endif

#if ENABLED(THERMAL_PROTECTION_CHAMBER)
if (degChamber() > CHAMBER_MAXTEMP) maxtemp_error(H_CHAMBER);
if (degChamber() > (CHAMBER_MAXTEMP)) maxtemp_error(H_CHAMBER);
#endif

#if WATCH_CHAMBER
Expand Down Expand Up @@ -1689,13 +1692,12 @@ void Temperature::mintemp_error(const heater_id_t heater_id) {
#if CHAMBER_FAN_MODE == 0
fan_chamber_pwm = CHAMBER_FAN_BASE;
#elif CHAMBER_FAN_MODE == 1
fan_chamber_pwm = (temp_chamber.celsius > temp_chamber.target) ? (CHAMBER_FAN_BASE) + (CHAMBER_FAN_FACTOR) * (temp_chamber.celsius - temp_chamber.target) : 0;
fan_chamber_pwm = temp_chamber.is_above_target() ? (CHAMBER_FAN_BASE) + (CHAMBER_FAN_FACTOR) * (temp_chamber.celsius - temp_chamber.target) : 0;
#elif CHAMBER_FAN_MODE == 2
fan_chamber_pwm = (CHAMBER_FAN_BASE) + (CHAMBER_FAN_FACTOR) * ABS(temp_chamber.celsius - temp_chamber.target);
if (temp_chamber.soft_pwm_amount)
fan_chamber_pwm += (CHAMBER_FAN_FACTOR) * 2;
if (temp_chamber.soft_pwm_amount) fan_chamber_pwm += (CHAMBER_FAN_FACTOR) * 2;
#elif CHAMBER_FAN_MODE == 3
fan_chamber_pwm = CHAMBER_FAN_BASE + _MAX((CHAMBER_FAN_FACTOR) * (temp_chamber.celsius - temp_chamber.target), 0);
fan_chamber_pwm = (CHAMBER_FAN_BASE) + _MAX((CHAMBER_FAN_FACTOR) * (temp_chamber.celsius - temp_chamber.target), 0);
#endif
NOMORE(fan_chamber_pwm, 255);
set_fan_speed(CHAMBER_FAN_INDEX, fan_chamber_pwm);
Expand All @@ -1708,7 +1710,7 @@ void Temperature::mintemp_error(const heater_id_t heater_id) {
#ifndef MIN_COOLING_SLOPE_DEG_CHAMBER_VENT
#define MIN_COOLING_SLOPE_DEG_CHAMBER_VENT 1.5
#endif
if (!flag_chamber_excess_heat && temp_chamber.celsius - temp_chamber.target >= HIGH_EXCESS_HEAT_LIMIT) {
if (!flag_chamber_excess_heat && temp_chamber.is_above_target((HIGH_EXCESS_HEAT_LIMIT) - 1)) {
// Open vent after MIN_COOLING_SLOPE_TIME_CHAMBER_VENT seconds if the
// temperature didn't drop at least MIN_COOLING_SLOPE_DEG_CHAMBER_VENT
if (next_cool_check_ms_2 == 0 || ELAPSED(ms, next_cool_check_ms_2)) {
Expand All @@ -1722,7 +1724,7 @@ void Temperature::mintemp_error(const heater_id_t heater_id) {
next_cool_check_ms_2 = 0;
old_temp = 9999;
}
if (flag_chamber_excess_heat && (temp_chamber.target - temp_chamber.celsius >= LOW_EXCESS_HEAT_LIMIT))
if (flag_chamber_excess_heat && temp_chamber.is_above_target((LOW_EXCESS_HEAT_LIMIT) - 1))
flag_chamber_excess_heat = false;
#endif
}
Expand Down Expand Up @@ -1754,9 +1756,9 @@ void Temperature::mintemp_error(const heater_id_t heater_id) {
}
else {
#if ENABLED(CHAMBER_LIMIT_SWITCHING)
if (temp_chamber.celsius >= temp_chamber.target + TEMP_CHAMBER_HYSTERESIS)
if (temp_chamber.is_above_target((TEMP_CHAMBER_HYSTERESIS) - 1))
temp_chamber.soft_pwm_amount = 0;
else if (temp_chamber.is_below_target(-(TEMP_CHAMBER_HYSTERESIS) + 1))
else if (temp_chamber.is_below_target((TEMP_CHAMBER_HYSTERESIS) - 1))
temp_chamber.soft_pwm_amount = (MAX_CHAMBER_POWER) >> 1;
#else
temp_chamber.soft_pwm_amount = temp_chamber.is_below_target() ? (MAX_CHAMBER_POWER) >> 1 : 0;
Expand Down Expand Up @@ -1808,20 +1810,18 @@ void Temperature::mintemp_error(const heater_id_t heater_id) {
if (temp_cooler.target == 0) temp_cooler.target = COOLER_MIN_TARGET;
if (ELAPSED(ms, next_cooler_check_ms)) {
next_cooler_check_ms = ms + COOLER_CHECK_INTERVAL;
if (temp_cooler.celsius > temp_cooler.target) {
temp_cooler.soft_pwm_amount = temp_cooler.celsius > temp_cooler.target ? MAX_COOLER_POWER : 0;
flag_cooler_state = temp_cooler.soft_pwm_amount > 0 ? true : false; // used to allow M106 fan control when cooler is disabled
if (temp_cooler.is_above_target()) { // too warm?
temp_cooler.soft_pwm_amount = MAX_COOLER_POWER;
#if ENABLED(COOLER_FAN)
int16_t fan_cooler_pwm = (COOLER_FAN_BASE) + (COOLER_FAN_FACTOR) * ABS(temp_cooler.celsius - temp_cooler.target);
NOMORE(fan_cooler_pwm, 255);
set_fan_speed(COOLER_FAN_INDEX, fan_cooler_pwm); // Set cooler fan pwm
const int16_t fan_cooler_pwm = (COOLER_FAN_BASE) + (COOLER_FAN_FACTOR) * ABS(temp_cooler.celsius - temp_cooler.target);
set_fan_speed(COOLER_FAN_INDEX, _MIN(fan_cooler_pwm, 255)); // Set cooler fan pwm
cooler_fan_flush_ms = ms + 5000;
#endif
}
else {
temp_cooler.soft_pwm_amount = 0;
#if ENABLED(COOLER_FAN)
set_fan_speed(COOLER_FAN_INDEX, temp_cooler.celsius > temp_cooler.target - 2 ? COOLER_FAN_BASE : 0);
set_fan_speed(COOLER_FAN_INDEX, temp_cooler.is_above_target(-2) ? COOLER_FAN_BASE : 0);
#endif
WRITE_HEATER_COOLER(LOW);
}
Expand Down
3 changes: 2 additions & 1 deletion Marlin/src/module/temperature.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,8 @@ typedef struct TempInfo {
typedef struct HeaterInfo : public TempInfo {
celsius_t target;
uint8_t soft_pwm_amount;
bool is_below_target(const celsius_t offs=0) const { return (celsius < (target + offs)); }
bool is_below_target(const celsius_t offs=0) const { return (target - celsius > offs); } // celsius < target - offs
bool is_above_target(const celsius_t offs=0) const { return (celsius - target > offs); } // celsius > target + offs
} heater_info_t;

// A heater with PID stabilization
Expand Down

0 comments on commit 9383c2a

Please sign in to comment.