2018-02-06 06:22:30 +00:00
|
|
|
/**
|
|
|
|
* Marlin 3D Printer Firmware
|
2020-02-03 14:00:57 +00:00
|
|
|
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
|
2018-02-06 06:22:30 +00:00
|
|
|
*
|
|
|
|
* Based on Sprinter and grbl.
|
2019-06-28 04:57:50 +00:00
|
|
|
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
|
2018-02-06 06:22:30 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2020-07-23 03:20:14 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-02-06 06:22:30 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* power.cpp - power control
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "../inc/MarlinConfig.h"
|
|
|
|
|
|
|
|
#include "power.h"
|
2019-09-01 00:44:45 +00:00
|
|
|
#include "../module/stepper/indirection.h"
|
2020-01-03 01:01:38 +00:00
|
|
|
#include "../MarlinCore.h"
|
2018-02-06 06:22:30 +00:00
|
|
|
|
2021-06-25 19:50:09 +00:00
|
|
|
#if ENABLED(PS_OFF_SOUND)
|
|
|
|
#include "../libs/buzzer.h"
|
|
|
|
#endif
|
|
|
|
|
2020-10-21 20:03:11 +00:00
|
|
|
#if defined(PSU_POWERUP_GCODE) || defined(PSU_POWEROFF_GCODE)
|
|
|
|
#include "../gcode/gcode.h"
|
|
|
|
#endif
|
|
|
|
|
2021-07-12 05:13:58 +00:00
|
|
|
#if EITHER(PSU_CONTROL, AUTO_POWER_CONTROL)
|
2020-07-09 00:21:04 +00:00
|
|
|
|
2018-02-06 06:22:30 +00:00
|
|
|
Power powerManager;
|
2021-07-12 05:13:58 +00:00
|
|
|
bool Power::psu_on;
|
2018-02-06 06:22:30 +00:00
|
|
|
|
2021-07-12 05:13:58 +00:00
|
|
|
#if ENABLED(AUTO_POWER_CONTROL)
|
|
|
|
#include "../module/temperature.h"
|
2018-02-06 06:22:30 +00:00
|
|
|
|
2021-07-12 05:13:58 +00:00
|
|
|
#if BOTH(USE_CONTROLLER_FAN, AUTO_POWER_CONTROLLERFAN)
|
|
|
|
#include "controllerfan.h"
|
|
|
|
#endif
|
2021-06-29 17:58:11 +00:00
|
|
|
|
2021-07-12 05:13:58 +00:00
|
|
|
millis_t Power::lastPowerOn;
|
|
|
|
#endif
|
2021-06-29 17:58:11 +00:00
|
|
|
|
2021-07-12 05:13:58 +00:00
|
|
|
/**
|
|
|
|
* Initialize pins & state for the power manager.
|
|
|
|
*
|
|
|
|
*/
|
2021-07-25 02:41:09 +00:00
|
|
|
void Power::init() {
|
2021-07-12 05:13:58 +00:00
|
|
|
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;
|
2018-02-06 06:22:30 +00:00
|
|
|
#endif
|
|
|
|
|
2021-07-12 05:13:58 +00:00
|
|
|
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));
|
2018-02-06 06:22:30 +00:00
|
|
|
#endif
|
2021-07-12 05:13:58 +00:00
|
|
|
}
|
2018-02-06 06:22:30 +00:00
|
|
|
|
2021-07-12 05:13:58 +00:00
|
|
|
/**
|
|
|
|
* 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));
|
2018-02-06 06:22:30 +00:00
|
|
|
#endif
|
|
|
|
|
2021-07-12 05:13:58 +00:00
|
|
|
#if ENABLED(PS_OFF_SOUND)
|
|
|
|
BUZZ(1000, 659);
|
|
|
|
#endif
|
2019-05-05 03:51:47 +00:00
|
|
|
|
2021-07-12 05:13:58 +00:00
|
|
|
OUT_WRITE(PS_ON_PIN, !PSU_ACTIVE_STATE);
|
|
|
|
psu_on = false;
|
|
|
|
}
|
2021-03-06 20:13:28 +00:00
|
|
|
|
2018-02-06 06:22:30 +00:00
|
|
|
|
2021-07-12 05:13:58 +00:00
|
|
|
#if ENABLED(AUTO_POWER_CONTROL)
|
|
|
|
|
|
|
|
#ifndef POWER_TIMEOUT
|
|
|
|
#define POWER_TIMEOUT 0
|
2021-06-28 04:02:11 +00:00
|
|
|
#endif
|
|
|
|
|
2021-07-12 05:13:58 +00:00
|
|
|
/**
|
|
|
|
* Check all conditions that would signal power needing to be on.
|
|
|
|
*
|
|
|
|
* @returns bool if power is needed
|
|
|
|
*/
|
|
|
|
bool Power::is_power_needed() {
|
2018-02-06 06:22:30 +00:00
|
|
|
|
2021-07-12 05:13:58 +00:00
|
|
|
if (printJobOngoing() || printingIsPaused()) return true;
|
2019-06-27 21:28:07 +00:00
|
|
|
|
2021-07-12 05:13:58 +00:00
|
|
|
#if ENABLED(AUTO_POWER_FANS)
|
|
|
|
FANS_LOOP(i) if (thermalManager.fan_speed[i]) return true;
|
|
|
|
#endif
|
2019-06-27 21:28:07 +00:00
|
|
|
|
2021-07-12 05:13:58 +00:00
|
|
|
#if ENABLED(AUTO_POWER_E_FANS)
|
|
|
|
HOTEND_LOOP() if (thermalManager.autofan_speed[e]) return true;
|
|
|
|
#endif
|
2021-03-06 20:13:28 +00:00
|
|
|
|
2021-07-12 05:13:58 +00:00
|
|
|
#if BOTH(USE_CONTROLLER_FAN, AUTO_POWER_CONTROLLERFAN)
|
|
|
|
if (controllerFan.state()) return true;
|
|
|
|
#endif
|
2018-02-06 06:22:30 +00:00
|
|
|
|
2021-07-12 05:13:58 +00:00
|
|
|
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
|
2021-05-02 07:06:55 +00:00
|
|
|
|
2021-07-12 05:13:58 +00:00
|
|
|
if (TERN0(HAS_HEATED_BED, thermalManager.degTargetBed() > 0 || thermalManager.temp_bed.soft_pwm_amount > 0)) return true;
|
2018-02-06 06:22:30 +00:00
|
|
|
|
2021-07-12 05:13:58 +00:00
|
|
|
#if HAS_HOTEND && AUTO_POWER_E_TEMP
|
|
|
|
HOTEND_LOOP() if (thermalManager.degHotend(e) >= (AUTO_POWER_E_TEMP)) return true;
|
2020-10-21 20:03:11 +00:00
|
|
|
#endif
|
2018-02-06 06:22:30 +00:00
|
|
|
|
2021-07-12 05:13:58 +00:00
|
|
|
#if HAS_HEATED_CHAMBER && AUTO_POWER_CHAMBER_TEMP
|
|
|
|
if (thermalManager.degChamber() >= (AUTO_POWER_CHAMBER_TEMP)) return true;
|
2020-10-21 20:03:11 +00:00
|
|
|
#endif
|
2021-06-25 19:50:09 +00:00
|
|
|
|
2021-07-12 05:13:58 +00:00
|
|
|
#if HAS_COOLER && AUTO_POWER_COOLER_TEMP
|
|
|
|
if (thermalManager.degCooler() >= (AUTO_POWER_COOLER_TEMP)) return true;
|
2021-06-25 19:50:09 +00:00
|
|
|
#endif
|
|
|
|
|
2021-07-12 05:13:58 +00:00
|
|
|
return false;
|
2020-10-21 20:03:11 +00:00
|
|
|
}
|
2018-02-06 06:22:30 +00:00
|
|
|
|
2021-07-12 05:13:58 +00:00
|
|
|
/**
|
|
|
|
* 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);
|
|
|
|
}
|
|
|
|
|
2020-11-09 00:37:41 +00:00
|
|
|
#endif
|
|
|
|
|
2018-02-06 06:22:30 +00:00
|
|
|
#endif // AUTO_POWER_CONTROL
|
2021-07-12 05:13:58 +00:00
|
|
|
|
|
|
|
#endif // PSU_CONTROL || AUTO_POWER_CONTROL
|