Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add failsafe shutdown command for PID controller #19318

Merged
merged 1 commit into from
Aug 16, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions tasmota/tasmota_xdrv_driver/xdrv_49_pid.ino
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@
#define D_CMND_PID_SETMANUAL_POWER "ManualPower"
#define D_CMND_PID_SETMAX_INTERVAL "MaxInterval"
#define D_CMND_PID_SETUPDATE_SECS "UpdateSecs"
#define D_CMND_PID_SETSHUTDOWN "Shutdown"

const char kPIDCommands[] PROGMEM = D_PRFX_PID "|" // Prefix
D_CMND_PID_SETPV "|"
Expand All @@ -209,7 +210,8 @@ const char kPIDCommands[] PROGMEM = D_PRFX_PID "|" // Prefix
D_CMND_PID_SETAUTO "|"
D_CMND_PID_SETMANUAL_POWER "|"
D_CMND_PID_SETMAX_INTERVAL "|"
D_CMND_PID_SETUPDATE_SECS;
D_CMND_PID_SETUPDATE_SECS "|"
D_CMND_PID_SETSHUTDOWN;
;

void (* const PIDCommand[])(void) PROGMEM = {
Expand All @@ -223,7 +225,8 @@ void (* const PIDCommand[])(void) PROGMEM = {
&CmndSetAuto,
&CmndSetManualPower,
&CmndSetMaxInterval,
&CmndSetUpdateSecs
&CmndSetUpdateSecs,
&CmndSetShutdown
};

struct {
Expand All @@ -233,6 +236,7 @@ struct {
unsigned long last_pv_update_secs = 0;
bool run_pid_now = false; // tells PID_Every_Second to run the pid algorithm
long current_time_secs = 0; // a counter that counts seconds since initialisation
bool shutdown = false; // power commands will be ignored when true
} Pid;

void PIDInit()
Expand Down Expand Up @@ -351,16 +355,20 @@ void CmndSetDSmooth(void) {

void CmndSetAuto(void) {
if (XdrvMailbox.payload >= 0) {
Pid.pid.setAuto(XdrvMailbox.payload);
if(!Pid.shutdown) {
Pid.pid.setAuto(XdrvMailbox.payload);
}
}
ResponseCmndNumber(Pid.pid.getAuto());
}

void CmndSetManualPower(void) {
if (XdrvMailbox.data_len > 0) {
Pid.pid.setManualPower(CharToFloat(XdrvMailbox.data));
if(!Pid.shutdown) {
Pid.pid.setManualPower(CharToFloat(XdrvMailbox.data));
}
}
ResponseCmndFloat(Pid.pid.getManualPower(), 1);
ResponseCmndFloat(Pid.pid.getManualPower(), 2);
}

void CmndSetMaxInterval(void) {
Expand All @@ -381,6 +389,18 @@ void CmndSetUpdateSecs(void) {
ResponseCmndNumber(Pid.update_secs);
}

void CmndSetShutdown(void) {
if (XdrvMailbox.payload >= 0) {
AddLog(LOG_LEVEL_INFO, PSTR("PID: Shutdown mode %s"), XdrvMailbox.payload>0 ? "activated" : "cleared");
Pid.shutdown = (XdrvMailbox.payload>0);
if(Pid.shutdown) {
Pid.pid.setAuto(0);
Pid.pid.setManualPower(0.0);
}
}
ResponseCmndNumber(Pid.shutdown);
}

void PIDShowValues(void) {
char str_buf[FLOATSZ];
char chr_buf;
Expand All @@ -394,6 +414,7 @@ void PIDShowValues(void) {
d_buf = Pid.pid.getSp();
dtostrfd(d_buf, 2, str_buf);
ResponseAppend_P(PSTR("\"PidSp\":%s,"), str_buf);
ResponseAppend_P(PSTR("\"PidShutdown\":%d,"), Pid.shutdown);

#if PID_REPORT_MORE_SETTINGS
d_buf = Pid.pid.getPb();
Expand Down