/
power.cpp
282 lines (219 loc) · 7.53 KB
/
power.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/**
* Marlin 3D Printer Firmware
* Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
*
* Based on Sprinter and grbl.
* Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
*
* 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
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
/**
* power.cpp - power control
*/
#include "../inc/MarlinConfigPre.h"
#if ANY(PSU_CONTROL, AUTO_POWER_CONTROL)
#include "power.h"
#include "../module/planner.h"
#include "../module/stepper/indirection.h" // for restore_stepper_drivers
#include "../module/temperature.h"
#include "../MarlinCore.h"
#if ENABLED(MAX7219_REINIT_ON_POWERUP)
#include "max7219.h"
#endif
#if ENABLED(PS_OFF_SOUND)
#include "../libs/buzzer.h"
#endif
#if defined(PSU_POWERUP_GCODE) || defined(PSU_POWEROFF_GCODE)
#include "../gcode/gcode.h"
#endif
Power powerManager;
bool Power::psu_on;
#if ENABLED(AUTO_POWER_CONTROL)
#include "../module/stepper.h"
#include "../module/temperature.h"
#if ALL(USE_CONTROLLER_FAN, AUTO_POWER_CONTROLLERFAN)
#include "controllerfan.h"
#endif
#if ANY(LASER_FEATURE, SPINDLE_FEATURE)
#include "spindle_laser.h"
#endif
millis_t Power::lastPowerOn;
#endif
#if PSU_TRACK_STATE_MS
millis_t Power::last_state_change_ms = 0;
#endif
/**
* 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 (psu_on) return;
#if ANY(POWER_OFF_TIMER, POWER_OFF_WAIT_FOR_COOLDOWN)
cancelAutoPowerOff();
#endif
OUT_WRITE(PS_ON_PIN, PSU_ACTIVE_STATE);
#if ENABLED(PSU_OFF_REDUNDANT)
OUT_WRITE(PS_ON1_PIN, TERN_(PSU_OFF_REDUNDANT_INVERTED, !)PSU_ACTIVE_STATE);
#endif
TERN_(PSU_TRACK_STATE_MS, last_state_change_ms = millis());
psu_on = true;
safe_delay(PSU_POWERUP_DELAY);
restore_stepper_drivers();
TERN_(MAX7219_REINIT_ON_POWERUP, max7219.init());
TERN_(HAS_TRINAMIC_CONFIG, safe_delay(PSU_POWERUP_DELAY));
#ifdef PSU_POWERUP_GCODE
gcode.process_subcommands_now(F(PSU_POWERUP_GCODE));
#endif
}
/**
* 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() {
TERN_(HAS_SUICIDE, suicide());
if (!psu_on) return;
SERIAL_ECHOLNPGM(STR_POWEROFF);
#ifdef PSU_POWEROFF_GCODE
gcode.process_subcommands_now(F(PSU_POWEROFF_GCODE));
#endif
#if ENABLED(PS_OFF_SOUND)
BUZZ(1000, 659);
#endif
OUT_WRITE(PS_ON_PIN, !PSU_ACTIVE_STATE);
#if ENABLED(PSU_OFF_REDUNDANT)
OUT_WRITE(PS_ON1_PIN, IF_DISABLED(PSU_OFF_REDUNDANT_INVERTED, !)PSU_ACTIVE_STATE);
#endif
TERN_(PSU_TRACK_STATE_MS, last_state_change_ms = millis());
psu_on = false;
#if ANY(POWER_OFF_TIMER, POWER_OFF_WAIT_FOR_COOLDOWN)
cancelAutoPowerOff();
#endif
}
#if ANY(AUTO_POWER_CONTROL, POWER_OFF_WAIT_FOR_COOLDOWN)
bool Power::is_cooling_needed() {
#if HAS_HOTEND && AUTO_POWER_E_TEMP
HOTEND_LOOP() if (thermalManager.degHotend(e) >= (AUTO_POWER_E_TEMP)) return true;
#endif
#if HAS_HEATED_CHAMBER && AUTO_POWER_CHAMBER_TEMP
if (thermalManager.degChamber() >= (AUTO_POWER_CHAMBER_TEMP)) return true;
#endif
#if HAS_COOLER && AUTO_POWER_COOLER_TEMP
if (thermalManager.degCooler() >= (AUTO_POWER_COOLER_TEMP)) return true;
#endif
return false;
}
#endif
#if ANY(POWER_OFF_TIMER, POWER_OFF_WAIT_FOR_COOLDOWN)
#if ENABLED(POWER_OFF_TIMER)
millis_t Power::power_off_time = 0;
void Power::setPowerOffTimer(const millis_t delay_ms) { power_off_time = millis() + delay_ms; }
#endif
#if ENABLED(POWER_OFF_WAIT_FOR_COOLDOWN)
bool Power::power_off_on_cooldown = false;
void Power::setPowerOffOnCooldown(const bool ena) { power_off_on_cooldown = ena; }
#endif
void Power::cancelAutoPowerOff() {
TERN_(POWER_OFF_TIMER, power_off_time = 0);
TERN_(POWER_OFF_WAIT_FOR_COOLDOWN, power_off_on_cooldown = false);
}
void Power::checkAutoPowerOff() {
if (TERN1(POWER_OFF_TIMER, !power_off_time) && TERN1(POWER_OFF_WAIT_FOR_COOLDOWN, !power_off_on_cooldown)) return;
if (TERN0(POWER_OFF_WAIT_FOR_COOLDOWN, power_off_on_cooldown && is_cooling_needed())) return;
if (TERN0(POWER_OFF_TIMER, power_off_time && PENDING(millis(), power_off_time))) return;
power_off();
}
#endif // POWER_OFF_TIMER || POWER_OFF_WAIT_FOR_COOLDOWN
#if ENABLED(AUTO_POWER_CONTROL)
#ifndef POWER_TIMEOUT
#define POWER_TIMEOUT 0
#endif
/**
* Check all conditions that would signal power needing to be on.
*
* @returns bool if power is needed
*/
bool Power::is_power_needed() {
// If any of the stepper drivers are enabled...
if (stepper.axis_enabled.bits) return true;
if (printJobOngoing() || printingIsPaused()) return true;
#if ENABLED(AUTO_POWER_FANS)
FANS_LOOP(i) if (thermalManager.fan_speed[i]) return true;
#endif
#if ENABLED(AUTO_POWER_E_FANS)
HOTEND_LOOP() if (thermalManager.autofan_speed[e]) return true;
#endif
#if ALL(USE_CONTROLLER_FAN, AUTO_POWER_CONTROLLERFAN)
if (controllerFan.state()) return true;
#endif
#if ANY(LASER_FEATURE, SPINDLE_FEATURE)
if (TERN0(AUTO_POWER_SPINDLE_LASER, cutter.enabled())) return true;
#endif
if (TERN0(AUTO_POWER_CHAMBER_FAN, thermalManager.chamberfan_speed))
return true;
if (TERN0(AUTO_POWER_COOLER_FAN, thermalManager.coolerfan_speed))
return true;
#if HAS_HOTEND
HOTEND_LOOP() if (thermalManager.degTargetHotend(e) > 0 || thermalManager.temp_hotend[e].soft_pwm_amount > 0) return true;
#endif
if (TERN0(HAS_HEATED_BED, thermalManager.degTargetBed() > 0 || thermalManager.temp_bed.soft_pwm_amount > 0)) return true;
return is_cooling_needed();
}
/**
* 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