forked from steff393/hgdo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathautoClose.cpp
156 lines (139 loc) · 3.88 KB
/
autoClose.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
// Copyright (c) 2021 steff393, MIT license
#include <Arduino.h>
#include <autoClose.h>
#include <globalConfig.h>
#include <logger.h>
#include <uap.h>
#define CYCLE_TIME 100 // ms
#define AC_TRIGGER_INTERVAL 5000 // ms
static const uint8_t m = 5;
typedef enum {
AC_INIT = 0,
AC_START = 1,
AC_PREWARN = 2,
AC_WAIT = 3,
AC_CLOSE = 4,
AC_ABORT_WAIT = 5,
PD_CORRECT_ERROR = 10,
PD_START = 11,
PD_GOTO_VENTING = 12,
PD_WAIT = 13
} acState_t;
static acState_t acState = AC_INIT;
static uint32_t acTimer = 0;
static uint32_t lastCall = 0;
void ac_trigger() {
if (acState == AC_INIT) {
LOG(m, "Start", "");
acState = AC_START;
}
}
void pd_trigger() {
if (acState == AC_INIT && (uap_getBroadcast() & UAP_STATUS_CLOSED)) {
LOG(m, "Start Paketdienst", "");
if (uap_getBroadcast() & UAP_STATUS_ERROR) {
// if an error is present, then it's not possible to go directly to venting. Send first a "close", wait and then start as usual
uap_triggerAction(UAP_ACTION_CLOSE, SRC_AUTOCLOSE);
acState = PD_CORRECT_ERROR;
acTimer = millis();
} else {
acState = PD_START;
}
}
}
void ac_abort(bool forceStop) {
// stop and abort everything
LOG(m, "Aborted", "");
acState = AC_ABORT_WAIT;
acTimer = millis();
if (forceStop) {
LOG(m, "Stopped", "");
uap_triggerAction(UAP_ACTION_STOP, SRC_AUTOCLOSE);
}
}
void ac_loop() {
if (millis() - lastCall < CYCLE_TIME) {
// avoid unnecessary frequent calls
return;
}
lastCall = millis();
uint32_t seconds = log_getSecSinceMidnight();
if ((seconds >= (uint32_t)cfgAcTime * 3600) && (seconds <= (uint32_t)cfgAcTime * 3600 + AC_TRIGGER_INTERVAL / 1000)) {
// check for a 5s time-window to safely detect, but not trigger multiple times
ac_trigger();
}
// --- STATE MACHINE ---
switch (acState) {
case AC_INIT: {
break;
}
case AC_START: {
// start a short closing to warn
uap_triggerAction(UAP_ACTION_CLOSE, SRC_AUTOCLOSE);
acState = AC_PREWARN;
acTimer = millis();
break;
}
case AC_PREWARN: {
if (millis() - acTimer > cfgAcDur1 * 1000) {
// wait to give people time to react
uap_triggerAction(UAP_ACTION_STOP, SRC_AUTOCLOSE);
acState = AC_WAIT;
acTimer = millis();
}
break;
}
case AC_WAIT: {
if (millis() - acTimer > cfgAcDur2 * 1000) {
// continue the movement
uap_triggerAction(UAP_ACTION_CLOSE, SRC_AUTOCLOSE);
acState = AC_CLOSE;
acTimer = millis();
}
// if during AC_WAIT a movement is detected, then abort the auto-closeing (but give 1000ms to stop actually)
if ((uap_getBroadcast() & UAP_STATUS_MOVING) && (millis() - acTimer > 1000)) {
ac_abort(false);
}
break;
}
case AC_CLOSE: // fall-through
case AC_ABORT_WAIT: {
// wait to avoid triggering again
if (millis() - acTimer > AC_TRIGGER_INTERVAL + 1000) {
acState = AC_INIT;
acTimer = 0;
}
break;
}
case PD_CORRECT_ERROR: {
if (millis() - acTimer > cfgPdWaitError) {
acState = PD_START;
}
break;
}
case PD_START: {
// start the first movement, e.g. a short closing to warn
uap_triggerAction(UAP_ACTION_VENTING, SRC_AUTOCLOSE);
acState = PD_GOTO_VENTING;
acTimer = millis();
break;
}
case PD_GOTO_VENTING: {
if ((uap_getBroadcast() & UAP_STATUS_VENTPOS) || (millis() - acTimer > cfgPdTimeout * 1000)) {
// venting position reached or timeout elapsed
acState = PD_WAIT;
acTimer = millis();
}
break;
}
case PD_WAIT: {
if (millis() - acTimer > cfgPdWaitTime * 1000) {
// close again
uap_triggerAction(UAP_ACTION_CLOSE, SRC_AUTOCLOSE);
acState = AC_CLOSE;
}
break;
}
default: ; // do nothing
}
}