-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDoorMotor_GarageDoor.cpp
368 lines (315 loc) · 9.72 KB
/
DoorMotor_GarageDoor.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
////////////////////////////////////////////////////////////
// Door Motor - Open / close coop door by cycling a relay
////////////////////////////////////////////////////////////
#include <Arduino.h>
#include <GPSParser.h>
#include <SaveController.h>
#include "ICommInterface.h"
#include "TelemetryTags.h"
#include "Telemetry.h"
#include "MilliTimer.h"
#include "Pins.h"
#include "SunCalc.h"
#include "DoorController.h"
#include "LightController.h"
#include "BeepController.h"
#include "GaryCooper.h"
#include "DoorMotor_GarageDoor.h"
typedef enum
{
doorSwitchOpen = 1 << 0,
doorSwitchClosed = 1 << 1,
} doorSwitchMaskE;
////////////////////////////////////////////////////////////
// Implementation of a chicken coop door controller that
// treats the door as a garage door style system with a
// button to open / close the door. This is broken out
// of the door controller so that other types of door
// motor (such as stepper motors) can be used with minimal
// change to the other software, especially the CDoorController class.
////////////////////////////////////////////////////////////
IDoorMotor *getDoorMotor()
{
static CDoorMotor_GarageDoor s_doorMotor;
return &s_doorMotor;
}
CDoorMotor_GarageDoor::CDoorMotor_GarageDoor()
{
m_seekingKnownState = true;
m_state = doorState_unknown;
m_lastCommand = (doorCommandE) - 1;
}
CDoorMotor_GarageDoor::~CDoorMotor_GarageDoor()
{
}
void CDoorMotor_GarageDoor::setup()
{
// Setup the door relay
pinMode(PIN_DOOR_RELAY, OUTPUT);
digitalWrite(PIN_DOOR_RELAY, RELAY_OFF);
// Setup the door position switch sensor inputs
pinMode(PIN_DOOR_OPEN_SWITCH, INPUT_PULLUP);
pinMode(PIN_DOOR_CLOSED_SWITCH, INPUT_PULLUP);
}
telemetrycommandResponseE CDoorMotor_GarageDoor::command(doorCommandE _command)
{
#ifdef DEBUG_DOOR_MOTOR
DEBUG_SERIAL.print(F("CDoorMotor_GarageDoor - command door: "));
DEBUG_SERIAL.println((_command == doorCommand_open) ? F("open.") :
(_command == doorCommand_close) ? F("close.") :
F("*** INVALID ***"));
#endif
if((_command != doorCommand_open) && (_command != doorCommand_close))
return telemetry_cmd_response_nak_invalid_value;
// Very special case at startup when coop door should be open
if(m_seekingKnownState)
{
#ifdef DEBUG_DOOR_MOTOR
DEBUG_SERIAL.println(F("CDoorMotor_GarageDoor - received command while seeking known state. Command delayed."));
#endif
m_lastCommand = _command;
return telemetry_cmd_response_ack;
}
// Only accept commands when I am in a stable state
if((m_state != doorState_closed) && (m_state != doorState_open))
{
#ifdef DEBUG_DOOR_MOTOR
DEBUG_SERIAL.println(F("CDoorMotor_GarageDoor - commmand() info:"));
#endif
return telemetry_cmd_response_nak_not_ready;
}
// Well, act on the command
if( ((m_state == doorState_closed) && (_command == doorCommand_open)) ||
((m_state == doorState_open) && (_command == doorCommand_close)))
{
#ifdef DEBUG_DOOR_MOTOR
DEBUG_SERIAL.println(F("CDoorMotor_GarageDoor - cycling relay."));
#endif
// Remember my last valid command
m_lastCommand = _command;
// Start the relay on timer
m_relayTimer.start(CDoorMotor_GarageDoor_relayMS);
digitalWrite(PIN_DOOR_RELAY, RELAY_ON);
// Set door state to moving and start the stuck timer
m_state = doorState_moving;
m_stuckDoorTimer.start(CDoorMotor_GarageDoor_stuck_door_delayMS);
return telemetry_cmd_response_ack;
}
else
{
#ifdef DEBUG_DOOR_MOTOR
DEBUG_SERIAL.println(F("CDoorMotor_GarageDoor - not commanded to opposite state, ignoring."));
#endif
return telemetry_cmd_response_ack;
}
return telemetry_cmd_response_nak_internal_error;
}
doorStateE CDoorMotor_GarageDoor::getDoorState()
{
if(uglySwitches())
return doorState_unknown;
return m_state;
}
void CDoorMotor_GarageDoor::tick()
{
// Don't do anything if the switches are in an ugly state
if(uglySwitches())
{
digitalWrite(PIN_DOOR_RELAY, RELAY_OFF);
return;
}
// Monitor the relay and turn it off
// when the timer hits zero
// First "running" test returns to keep the door switch debounce
// logic from messing with the relay timing
if(m_relayTimer.getState() == CMilliTimer::running)
return;
if(m_relayTimer.getState() == CMilliTimer::expired)
{
m_relayTimer.reset();
digitalWrite(PIN_DOOR_RELAY, RELAY_OFF);
}
///////////////////////////////////////////////////////////////////////
// If I have not checked my initial state then do it now.
if(m_seekingKnownState)
{
// I just started. This is my first tick. If I don't know
// where the door is then toggle the relay to get it to
// go somewhere!
if((getSwitches() == 0) && (m_seekKnownStateRelayCommandIssued == false))
{
#ifdef DEBUG_DOOR_MOTOR
DEBUG_SERIAL.println(F("CDoorMotor_GarageDoor - Started in unknown state, seeking a switch."));
#endif
// Start the relay on timer
m_relayTimer.start( CDoorMotor_GarageDoor_relayMS);
digitalWrite(PIN_DOOR_RELAY, RELAY_ON);
// And a delay to see if we ever get there
m_state = doorState_unknown;
m_stuckDoorTimer.start(CDoorMotor_GarageDoor_stuck_door_delayMS);
m_seekKnownStateRelayCommandIssued = true;
}
// I'm waiting for a known state - good or bad.
if(getSwitches() == doorSwitchOpen)
{
#ifdef DEBUG_DOOR_MOTOR
DEBUG_SERIAL.println(F("CDoorMotor_GarageDoor - found open-switch, now in known state."));
#endif
m_state = doorState_open;
m_seekingKnownState = false;
m_stuckDoorTimer.reset();
}
else if(getSwitches() == doorSwitchClosed)
{
#ifdef DEBUG_DOOR_MOTOR
DEBUG_SERIAL.println(F("CDoorMotor_GarageDoor - found closed-switch, now in known state."));
#endif
m_state = doorState_closed;
m_seekingKnownState = false;
m_stuckDoorTimer.reset();
}
else if(m_stuckDoorTimer.getState() == CMilliTimer::expired)
{
#ifdef DEBUG_DOOR_MOTOR
DEBUG_SERIAL.println(F("CDoorMotor_GarageDoor - time is up, entering unknown state."));
#endif
m_state = doorState_unknown;
m_seekingKnownState = false;
m_stuckDoorTimer.reset();
}
return;
}
///////////////////////////////////////////////////////////////////////
// We have passed the m_seekingKnownState startup phase. This is normal
// operation
switch(m_state)
{
case doorState_moving:
if((m_lastCommand == doorCommand_open) && (getSwitches() == doorSwitchOpen))
{
#ifdef DEBUG_DOOR_MOTOR
DEBUG_SERIAL.println(F("CDoorMotor_GarageDoor - reached command state: open"));
#endif
m_state = doorState_open;
m_stuckDoorTimer.reset();
}
else if((m_lastCommand == doorCommand_close) && (getSwitches() == doorSwitchClosed))
{
#ifdef DEBUG_DOOR_MOTOR
DEBUG_SERIAL.println(F("CDoorMotor_GarageDoor - reached command state: closed"));
#endif
m_state = doorState_closed;
m_stuckDoorTimer.reset();
}
else if(m_stuckDoorTimer.getState() == CMilliTimer::expired)
{
#ifdef DEBUG_DOOR_MOTOR
DEBUG_SERIAL.println(F("CDoorMotor_GarageDoor - *** Timed out waiting for commanded state. ***"));
#endif
m_state = doorState_unknown;
m_stuckDoorTimer.reset();
}
return;
case doorState_open:
// Closed from the open state
if((m_state == doorState_open) && (getSwitches() == doorSwitchClosed))
{
#ifdef DEBUG_DOOR_MOTOR
DEBUG_SERIAL.println(F("CDoorMotor_GarageDoor - spontaneous change to: closed"));
#endif
m_state = doorState_closed;
}
break;
case doorState_closed:
// Open from the closed state
if((m_state == doorState_closed) && (getSwitches() == doorSwitchOpen))
{
#ifdef DEBUG_DOOR_MOTOR
DEBUG_SERIAL.println(F("CDoorMotor_GarageDoor - spontaneous change to: open"));
#endif
m_state = doorState_open;
}
break;
case doorState_unknown:
if(getSwitches() == doorSwitchOpen)
m_state = doorState_open;
if(getSwitches() == doorSwitchClosed)
m_state = doorState_closed;
break;
default:
#ifdef DEBUG_DOOR_MOTOR
DEBUG_SERIAL.println(F("CDoorMotor_GarageDoor - *** UNKNOWN STATE VALUE ***"));
#endif
return;
}
////////////////////////////////////////////
// We are not moving, so we should be in the open or closed state.
// Now we monitor for spontaneous changes in the door switches
// Loss of door switches
if(getSwitches() == 0)
{
// The switches went to 0. Someone might be out there
// opening the door, so wait until we know for sure.
if(m_lostSwitchesTimer.getState() == CMilliTimer::notSet)
{
m_lostSwitchesTimer.start(CDoorMotor_GarageDoor_stuck_door_delayMS);
#ifdef DEBUG_DOOR_MOTOR
DEBUG_SERIAL.println(F("CDoorMotor_GarageDoor - both door switches open. Is the door moving?"));
#endif
}
else if(m_lostSwitchesTimer.getState() == CMilliTimer::expired)
{
#ifdef DEBUG_DOOR_MOTOR
DEBUG_SERIAL.println(F("CDoorMotor_GarageDoor - spontaneous change to: * UNKNOWN *"));
#endif
m_lostSwitchesTimer.reset();
m_state = doorState_unknown;
}
}
else
{
#ifdef DEBUG_DOOR_MOTOR
if(m_lostSwitchesTimer.getState() == CMilliTimer::running)
DEBUG_SERIAL.println(F("CDoorMotor_GarageDoor - switches recovered before timeout to unknown state."));
#endif
m_lostSwitchesTimer.reset();
}
}
static unsigned int rawSwitchRead()
{
unsigned int mask = 0;
// NOTE, Inputs are active LOW, so a zero means that
// the switch is closed and the door is in that position
if(digitalRead(PIN_DOOR_OPEN_SWITCH) == 0)
mask |= doorSwitchOpen;
if(digitalRead(PIN_DOOR_CLOSED_SWITCH) == 0)
mask |= doorSwitchClosed;
return mask;
}
unsigned int CDoorMotor_GarageDoor::getSwitches()
{
unsigned int mask1 = 0;
unsigned int mask2 = 0;
// Make sure the switches are not bouncing
// and producing false readings.
do
{
mask1 = rawSwitchRead();
delay(2);
mask2 = rawSwitchRead();
delay(2);
}
while(mask1 != mask2);
return mask2;
}
bool CDoorMotor_GarageDoor::uglySwitches()
{
if((getSwitches() & doorSwitchOpen) && (getSwitches() & doorSwitchClosed))
{
#ifdef DEBUG_DOOR_MOTOR
DEBUG_SERIAL.println(F("CDoorMotor_GarageDoor - *** Ugly switches ***"));
#endif
return true;
}
return false;
}