Skip to content

Commit

Permalink
♻️ Consolidate PSU_CONTROL (MarlinFirmware#22304)
Browse files Browse the repository at this point in the history
  • Loading branch information
slowbro authored and AlexColello committed Jan 14, 2022
1 parent b2909fa commit fd58b82
Show file tree
Hide file tree
Showing 10 changed files with 199 additions and 160 deletions.
9 changes: 6 additions & 3 deletions Marlin/src/MarlinCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,10 @@
#include "feature/stepper_driver_safety.h"
#endif

#if ENABLED(PSU_CONTROL)
#include "feature/power.h"
#endif

PGMSTR(M112_KILL_STR, "M112 Shutdown");

MarlinState marlin_state = MF_INITIALIZING;
Expand Down Expand Up @@ -932,7 +936,7 @@ void minkill(const bool steppers_off/*=false*/) {
// Power off all steppers (for M112) or just the E steppers
steppers_off ? disable_all_steppers() : disable_e_steppers();

TERN_(PSU_CONTROL, PSU_OFF());
TERN_(PSU_CONTROL, powerManager.power_off());

TERN_(HAS_SUICIDE, suicide());

Expand Down Expand Up @@ -1235,8 +1239,7 @@ void setup() {

#if ENABLED(PSU_CONTROL)
SETUP_LOG("PSU_CONTROL");
powersupply_on = ENABLED(PSU_DEFAULT_OFF);
if (ENABLED(PSU_DEFAULT_OFF)) PSU_OFF(); else PSU_ON();
powerManager.init();
#endif

#if ENABLED(POWER_LOSS_RECOVERY)
Expand Down
19 changes: 0 additions & 19 deletions Marlin/src/MarlinCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,25 +81,6 @@ extern bool wait_for_heatup;
void wait_for_user_response(millis_t ms=0, const bool no_sleep=false);
#endif

#if ENABLED(PSU_CONTROL)
extern bool powersupply_on;
#define PSU_PIN_ON() do{ OUT_WRITE(PS_ON_PIN, PSU_ACTIVE_STATE); powersupply_on = true; }while(0)
#define PSU_PIN_OFF() do{ OUT_WRITE(PS_ON_PIN, !PSU_ACTIVE_STATE); powersupply_on = false; }while(0)
#if ENABLED(AUTO_POWER_CONTROL)
#define PSU_ON() powerManager.power_on()
#define PSU_OFF() powerManager.power_off()
#define PSU_OFF_SOON() powerManager.power_off_soon()
#else
#define PSU_ON() PSU_PIN_ON()
#if ENABLED(PS_OFF_SOUND)
#define PSU_OFF() do{ BUZZ(1000, 659); PSU_PIN_OFF(); }while(0)
#else
#define PSU_OFF() PSU_PIN_OFF()
#endif
#define PSU_OFF_SOON PSU_OFF
#endif
#endif

bool pin_is_protected(const pin_t pin);

#if HAS_SUICIDE
Expand Down
243 changes: 145 additions & 98 deletions Marlin/src/feature/power.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,7 @@

#include "../inc/MarlinConfig.h"

#if ENABLED(AUTO_POWER_CONTROL)

#include "power.h"
#include "../module/temperature.h"
#include "../module/stepper/indirection.h"
#include "../MarlinCore.h"

Expand All @@ -41,133 +38,183 @@
#include "../gcode/gcode.h"
#endif

#if BOTH(USE_CONTROLLER_FAN, AUTO_POWER_CONTROLLERFAN)
#include "controllerfan.h"
#endif
#if EITHER(PSU_CONTROL, AUTO_POWER_CONTROL)

Power powerManager;
bool Power::psu_on;

millis_t Power::lastPowerOn;
#if ENABLED(AUTO_POWER_CONTROL)
#include "../module/temperature.h"

bool Power::is_power_needed() {
#if BOTH(USE_CONTROLLER_FAN, AUTO_POWER_CONTROLLERFAN)
#include "controllerfan.h"
#endif

if (printJobOngoing() || printingIsPaused()) return true;
millis_t Power::lastPowerOn;
#endif

#if ENABLED(AUTO_POWER_FANS)
FANS_LOOP(i) if (thermalManager.fan_speed[i]) return true;
/**
* Initialize pins & state for the power manager.
*
*/
void Power::init(){
psu_on = ENABLED(PSU_DEFAULT_OFF); // Set opposite state to get full power_off/on
TERN(PSU_DEFAULT_OFF, power_off(), power_on());
}

/**
* Power on if the power is currently off.
* Restores stepper drivers and processes any PSU_POWERUP_GCODE.
*
*/
void Power::power_on() {
#if ENABLED(AUTO_POWER_CONTROL)
const millis_t now = millis();
lastPowerOn = now + !now;
#endif

#if ENABLED(AUTO_POWER_E_FANS)
HOTEND_LOOP() if (thermalManager.autofan_speed[e]) return true;
if (psu_on) return;

OUT_WRITE(PS_ON_PIN, PSU_ACTIVE_STATE);
psu_on = true;
safe_delay(PSU_POWERUP_DELAY);
restore_stepper_drivers();
TERN_(HAS_TRINAMIC_CONFIG, safe_delay(PSU_POWERUP_DELAY));

#ifdef PSU_POWERUP_GCODE
GcodeSuite::process_subcommands_now_P(PSTR(PSU_POWERUP_GCODE));
#endif
}

#if BOTH(USE_CONTROLLER_FAN, AUTO_POWER_CONTROLLERFAN)
if (controllerFan.state()) return true;
/**
* Power off if the power is currently on.
* Processes any PSU_POWEROFF_GCODE and makes a PS_OFF_SOUND if enabled.
*
*/
void Power::power_off() {
if (!psu_on) return;

#ifdef PSU_POWEROFF_GCODE
GcodeSuite::process_subcommands_now_P(PSTR(PSU_POWEROFF_GCODE));
#endif

if (TERN0(AUTO_POWER_CHAMBER_FAN, thermalManager.chamberfan_speed))
return true;
#if ENABLED(PS_OFF_SOUND)
BUZZ(1000, 659);
#endif

if (TERN0(AUTO_POWER_COOLER_FAN, thermalManager.coolerfan_speed))
return true;
OUT_WRITE(PS_ON_PIN, !PSU_ACTIVE_STATE);
psu_on = false;
}

// If any of the drivers or the bed are enabled...
if (X_ENABLE_READ() == X_ENABLE_ON || Y_ENABLE_READ() == Y_ENABLE_ON || Z_ENABLE_READ() == Z_ENABLE_ON
#if HAS_X2_ENABLE
|| X2_ENABLE_READ() == X_ENABLE_ON
#endif
#if HAS_Y2_ENABLE
|| Y2_ENABLE_READ() == Y_ENABLE_ON
#endif
#if HAS_Z2_ENABLE
|| Z2_ENABLE_READ() == Z_ENABLE_ON
#endif
#if E_STEPPERS
#define _OR_ENABLED_E(N) || E##N##_ENABLE_READ() == E_ENABLE_ON
REPEAT(E_STEPPERS, _OR_ENABLED_E)
#endif
) return true;

#if HAS_HOTEND
HOTEND_LOOP() if (thermalManager.degTargetHotend(e) > 0 || thermalManager.temp_hotend[e].soft_pwm_amount > 0) return true;
#if ENABLED(AUTO_POWER_CONTROL)

#ifndef POWER_TIMEOUT
#define POWER_TIMEOUT 0
#endif

if (TERN0(HAS_HEATED_BED, thermalManager.degTargetBed() > 0 || thermalManager.temp_bed.soft_pwm_amount > 0)) return true;
/**
* Check all conditions that would signal power needing to be on.
*
* @returns bool if power is needed
*/
bool Power::is_power_needed() {

#if HAS_HOTEND && AUTO_POWER_E_TEMP
HOTEND_LOOP() if (thermalManager.degHotend(e) >= (AUTO_POWER_E_TEMP)) return true;
#endif
if (printJobOngoing() || printingIsPaused()) return true;

#if HAS_HEATED_CHAMBER && AUTO_POWER_CHAMBER_TEMP
if (thermalManager.degChamber() >= (AUTO_POWER_CHAMBER_TEMP)) return true;
#endif
#if ENABLED(AUTO_POWER_FANS)
FANS_LOOP(i) if (thermalManager.fan_speed[i]) return true;
#endif

#if HAS_COOLER && AUTO_POWER_COOLER_TEMP
if (thermalManager.degCooler() >= (AUTO_POWER_COOLER_TEMP)) return true;
#endif
#if ENABLED(AUTO_POWER_E_FANS)
HOTEND_LOOP() if (thermalManager.autofan_speed[e]) return true;
#endif

return false;
}
#if BOTH(USE_CONTROLLER_FAN, AUTO_POWER_CONTROLLERFAN)
if (controllerFan.state()) return true;
#endif

#ifndef POWER_TIMEOUT
#define POWER_TIMEOUT 0
#endif
if (TERN0(AUTO_POWER_CHAMBER_FAN, thermalManager.chamberfan_speed))
return true;

if (TERN0(AUTO_POWER_COOLER_FAN, thermalManager.coolerfan_speed))
return true;

// If any of the drivers or the bed are enabled...
if (X_ENABLE_READ() == X_ENABLE_ON || Y_ENABLE_READ() == Y_ENABLE_ON || Z_ENABLE_READ() == Z_ENABLE_ON
#if HAS_X2_ENABLE
|| X2_ENABLE_READ() == X_ENABLE_ON
#endif
#if HAS_Y2_ENABLE
|| Y2_ENABLE_READ() == Y_ENABLE_ON
#endif
#if HAS_Z2_ENABLE
|| Z2_ENABLE_READ() == Z_ENABLE_ON
#endif
#if E_STEPPERS
#define _OR_ENABLED_E(N) || E##N##_ENABLE_READ() == E_ENABLE_ON
REPEAT(E_STEPPERS, _OR_ENABLED_E)
#endif
) return true;

#if HAS_HOTEND
HOTEND_LOOP() if (thermalManager.degTargetHotend(e) > 0 || thermalManager.temp_hotend[e].soft_pwm_amount > 0) return true;
#endif

void Power::check(const bool pause) {
static bool _pause = false;
static millis_t nextPowerCheck = 0;
const millis_t now = millis();
#if POWER_TIMEOUT > 0
if (pause != _pause) {
lastPowerOn = now + !now;
_pause = pause;
}
if (pause) return;
#endif
if (ELAPSED(now, nextPowerCheck)) {
nextPowerCheck = now + 2500UL;
if (is_power_needed())
power_on();
else if (!lastPowerOn || (POWER_TIMEOUT > 0 && ELAPSED(now, lastPowerOn + SEC_TO_MS(POWER_TIMEOUT))))
power_off();
}
}
if (TERN0(HAS_HEATED_BED, thermalManager.degTargetBed() > 0 || thermalManager.temp_bed.soft_pwm_amount > 0)) return true;

void Power::power_on() {
const millis_t now = millis();
lastPowerOn = now + !now;
if (!powersupply_on) {
PSU_PIN_ON();
safe_delay(PSU_POWERUP_DELAY);
restore_stepper_drivers();
TERN_(HAS_TRINAMIC_CONFIG, safe_delay(PSU_POWERUP_DELAY));
#ifdef PSU_POWERUP_GCODE
GcodeSuite::process_subcommands_now_P(PSTR(PSU_POWERUP_GCODE));
#if HAS_HOTEND && AUTO_POWER_E_TEMP
HOTEND_LOOP() if (thermalManager.degHotend(e) >= (AUTO_POWER_E_TEMP)) return true;
#endif
}
}

void Power::power_off() {
if (powersupply_on) {
#ifdef PSU_POWEROFF_GCODE
GcodeSuite::process_subcommands_now_P(PSTR(PSU_POWEROFF_GCODE));
#if HAS_HEATED_CHAMBER && AUTO_POWER_CHAMBER_TEMP
if (thermalManager.degChamber() >= (AUTO_POWER_CHAMBER_TEMP)) return true;
#endif

#if ENABLED(PS_OFF_SOUND)
BUZZ(1000, 659);
#if HAS_COOLER && AUTO_POWER_COOLER_TEMP
if (thermalManager.degCooler() >= (AUTO_POWER_COOLER_TEMP)) return true;
#endif

PSU_PIN_OFF();
return false;
}
}

void Power::power_off_soon() {
#if POWER_OFF_DELAY
lastPowerOn = millis() - SEC_TO_MS(POWER_TIMEOUT) + SEC_TO_MS(POWER_OFF_DELAY);
//if (!lastPowerOn) ++lastPowerOn;
#else
power_off();
/**
* Check if we should power off automatically (POWER_TIMEOUT elapsed, !is_power_needed).
*
* @param pause pause the 'timer'
*/
void Power::check(const bool pause) {
static millis_t nextPowerCheck = 0;
const millis_t now = millis();
#if POWER_TIMEOUT > 0
static bool _pause = false;
if (pause != _pause) {
lastPowerOn = now + !now;
_pause = pause;
}
if (pause) return;
#endif
if (ELAPSED(now, nextPowerCheck)) {
nextPowerCheck = now + 2500UL;
if (is_power_needed())
power_on();
else if (!lastPowerOn || (POWER_TIMEOUT > 0 && ELAPSED(now, lastPowerOn + SEC_TO_MS(POWER_TIMEOUT))))
power_off();
}
}

#if POWER_OFF_DELAY > 0

/**
* Power off with a delay. Power off is triggered by check() after the delay.
*
*/
void Power::power_off_soon() {
lastPowerOn = millis() - SEC_TO_MS(POWER_TIMEOUT) + SEC_TO_MS(POWER_OFF_DELAY);
}

#endif
}

#endif // AUTO_POWER_CONTROL

#endif // PSU_CONTROL || AUTO_POWER_CONTROL
25 changes: 20 additions & 5 deletions Marlin/src/feature/power.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,32 @@
* power.h - power control
*/

#include "../core/millis_t.h"
#if ENABLED(AUTO_POWER_CONTROL)
#include "../core/millis_t.h"
#endif

class Power {
public:
static void check(const bool pause);
static bool psu_on;

static void init();
static void power_on();
static void power_off();

#if ENABLED(AUTO_POWER_CONTROL) && POWER_OFF_DELAY > 0
static void power_off_soon();
private:
static millis_t lastPowerOn;
static bool is_power_needed();
#else
static inline void power_off_soon() { power_off(); }
#endif

#if ENABLED(AUTO_POWER_CONTROL)
static void check(const bool pause);

private:
static millis_t lastPowerOn;
static bool is_power_needed();

#endif
};

extern Power powerManager;
Loading

0 comments on commit fd58b82

Please sign in to comment.