-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGaryCooper.ino
373 lines (305 loc) · 8.47 KB
/
GaryCooper.ino
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
368
369
370
371
372
////////////////////////////////////////////////////
// Gary Cooper chicken coop controller
////////////////////////////////////////////////////
#include <Arduino.h>
#include <EEPROM.h>
#include <GPSParser.h>
#include <SaveController.h>
#include "ICommInterface.h"
#include "Telemetry.h"
#include "TelemetryTags.h"
#include "SlidingBuf.h"
#include "Comm_Arduino.h"
#include "Command.h"
#include "MilliTimer.h"
#include "Pins.h"
#include "SunCalc.h"
#include "DoorController.h"
#include "LightController.h"
#include "BeepController.h"
#include "GaryCooper.h"
// GPS parser
CGPSParser g_GPSParser;
static bool s_gpsDataStreamActive = false;
// Door controller
CDoorController g_doorController;
// Light controller
CLightController g_lightController;
// Beep controller
CBeepController g_beepController(PIN_BEEPER);
// Settings controller
CSaveController g_saveController('C', 'o', 'o', 'p');
bool settingsLoaded = false;
// Sunrise / Sunset calculator
CSunCalc g_sunCalc;
// Telemetry module
CTelemetry g_telemetry;
static CCommand s_commandProcessor;
static CComm_Arduino g_telemetryComm;
// Various behavioral delays
#define TIME_CHECK_UPDATE_NO_GPS_LOCK (5 * MILLIS_PER_SECOND)
#define TIME_CHECK_UPDATE_GPS_LOCK (60 * MILLIS_PER_SECOND)
CMilliTimer g_timeCheckTimer;
// Frequency of telemetry transmission
#define TELEMETRY_UPDATE (2 * MILLIS_PER_SECOND)
CMilliTimer g_telemetryUpdateTimer;
// Flashing the LED
bool g_heartbeat = false;
void saveSettings(bool _defaults)
{
#ifdef DEBUG_SETTINGS
DEBUG_SERIAL.print(F("Save settings... "));
#endif // DEBUG_SETTINGS
// Make sure the header is correct
g_saveController.updateHeader(GARYCOOPER_DATA_VERSION);
// Rewind so we write the settings in the correct place
g_saveController.rewind();
// Save everything
g_doorController.saveSettings(g_saveController, _defaults);
g_lightController.saveSettings(g_saveController, _defaults);
#ifdef DEBUG_SETTINGS
DEBUG_SERIAL.println(F("complete."));
#endif
}
void loadSettings()
{
#ifdef DEBUG_SETTINGS
DEBUG_SERIAL.print(F("Load settings checking header version: "));
#endif // DEBUG_SETTINGS
// If the data version is incorrect then we need to update the EEPROM
// to default settings
int headerVersion = g_saveController.getDataVersion();
#ifdef DEBUG_SETTINGS
DEBUG_SERIAL.print(headerVersion);
DEBUG_SERIAL.print(F(" - "));
#endif
if(headerVersion != GARYCOOPER_DATA_VERSION)
{
#ifdef DEBUG_SETTINGS
DEBUG_SERIAL.println(F("INCORRECT."));
// Save defaults from object constructors
DEBUG_SERIAL.println(F("Saving default settings."));
#endif
saveSettings(true);
}
else
{
#ifdef DEBUG_SETTINGS
DEBUG_SERIAL.println(F("CORRECT."));
#endif
}
#ifdef DEBUG_SETTINGS
DEBUG_SERIAL.println(F("Loading settings... "));
#endif
// Make sure we start at the beginning
g_saveController.rewind();
g_doorController.loadSettings(g_saveController);
g_lightController.loadSettings(g_saveController);
#ifdef DEBUG_SETTINGS
DEBUG_SERIAL.println(F("Load settings complete."));
#endif
}
void setup()
{
// Setup heartbeat indicator
pinMode(PIN_HEARTBEAT_LED, OUTPUT);
// Prep debug port
DEBUG_SERIAL.begin(DEBUG_BAUD_RATE);
// Prep the GPS port
GPS_SERIAL.begin(GPS_BAUD_RATE);
// Prep the telemetry port
g_telemetryComm.open(TELEMETRY_PORT, TELEMETRY_BAUD_RATE);
g_telemetry.setInterfaces(&g_telemetryComm, &s_commandProcessor);
// Setup the door controller
g_doorController.setup();
// And the light controller
g_lightController.setup();
// Beep to indicate starting the main loop
g_beepController.setup();
g_beepController.beep(BEEP_FREQ_INFO, 50, 50, 2);
// Telemetry updates every two seconds
g_telemetryUpdateTimer.start(TELEMETRY_UPDATE);
// Prep the update timer. This delay
// allows the GPS to get some data before we start
// processing more slowly
g_timeCheckTimer.start(TIME_CHECK_UPDATE_NO_GPS_LOCK);
}
void loop()
{
// Load settings?
if(!settingsLoaded)
{
loadSettings();
settingsLoaded = true;
}
// Process all available GPS data
while(GPS_SERIAL.available())
{
unsigned char GPSData[256];
unsigned int GPSDataLen = 0;
// Get the data from the serial port
GPSDataLen = GPS_SERIAL.available();
if(GPSDataLen > sizeof(GPSData) - 1)
GPSDataLen = sizeof(GPSData) - 1;
GPSDataLen = GPS_SERIAL.readBytes((unsigned char *)GPSData, GPSDataLen);
if(GPSDataLen)
{
#ifdef DEBUG_RAW_GPS
GPSData[GPSDataLen] = '\0';
String rawGPS((const char *)GPSData);
DEBUG_SERIAL.print(rawGPS);
#endif
// Parse the GPS data
g_GPSParser.parse(GPSData, GPSDataLen);
// Note that we have received some data
// from the GPS serial port.
s_gpsDataStreamActive = true;
}
}
// Let the telemetry module process serial data
g_telemetryComm.tick();
g_telemetry.tick();
// Let the beep controller run
g_beepController.tick();
// Let the door controller time its relay
g_doorController.tick();
// Send telemetry
if(g_telemetryUpdateTimer.getState() == CMilliTimer::expired)
{
// Blink the LED
g_heartbeat = !g_heartbeat;
digitalWrite(PIN_HEARTBEAT_LED, g_heartbeat);
// Reset the timer
g_telemetryUpdateTimer.start(TELEMETRY_UPDATE);
// Send telemetry version number
g_telemetry.transmissionStart();
g_telemetry.sendTerm(telemetry_tag_version);
g_telemetry.sendTerm(TELEMETRY_VERSION_01);
g_telemetry.transmissionEnd();
// Send standing errors
sendErrors();
// And the rest of the telemetry
g_sunCalc.sendTelemetry();
g_doorController.sendTelemetry();
g_lightController.sendTelemetry();
}
// If the update timer has completed then
// read the GPS data and check the time to
// see if anything needs to be done
if(g_timeCheckTimer.getState() == CMilliTimer::expired)
{
// Prep for next update
if(g_GPSParser.getGPSData().m_GPSLocked)
g_timeCheckTimer.start(TIME_CHECK_UPDATE_GPS_LOCK);
else
g_timeCheckTimer.start(TIME_CHECK_UPDATE_NO_GPS_LOCK);
// If the GPS is not sending any data then report an error
if(!s_gpsDataStreamActive)
{
g_GPSParser.getGPSData().clear();
reportError(telemetry_error_GPS_no_data, true);
}
else
{
reportError(telemetry_error_GPS_no_data, false);
}
s_gpsDataStreamActive = false;
// If we have a valid time fix, control the door and light
if(g_sunCalc.processGPSData(g_GPSParser.getGPSData()))
{
g_doorController.checkTime();
g_lightController.checkTime();
}
}
}
// Utility functions
void debugPrintDoubleTime(double _t, bool _newline)
{
int hour = (int)_t;
int minute = 60. * (_t - hour);
DEBUG_SERIAL.print(hour);
DEBUG_SERIAL.print(F(":"));
DEBUG_SERIAL.print(minute);
if(_newline) DEBUG_SERIAL.println();
}
static uint16_t s_errorFlags = 0;
void reportError(telemetryErrorE _errorTag, bool _set)
{
int beepCount = 0;
// Log the error for transmission
if(_set)
{
// Don't keep re-setting them
if(s_errorFlags & _errorTag)
return;
// Or it in
s_errorFlags |= _errorTag;
}
else
{
// Don't keep re-clearing them
if(!(s_errorFlags & _errorTag))
return;
// Mask it out
s_errorFlags &= ~_errorTag;
}
// Report error to console
String errorString(_errorTag);
switch(_errorTag)
{
case telemetry_error_GPS_no_data:
errorString = F("telemetry_error_GPS_no_data");
beepCount = 1;
break;
case telemetry_error_GPS_bad_data:
errorString = F("telemetry_error_GPS_bad_data");
beepCount = 2;
break;
case telemetry_error_GPS_not_locked:
errorString = F("telemetry_error_GPS_not_locked");
beepCount = 3;
break;
case telemetry_error_suncalc_invalid_time:
errorString = F("telemetry_error_suncalc_invalid_time");
beepCount = 4;
break;
case telemetry_error_no_door_motor:
errorString = F("telemetry_error_no_door_motor");
beepCount = 5;
break;
case telemetry_error_door_motor_unknown_state:
errorString = F("telemetry_error_door_motor_unknown_state");
beepCount = 6;
break;
case telemetry_error_door_motor_unknown_not_responding:
errorString = F("telemetry_error_door_motor_unknown_not_responding");
beepCount = 7;
break;
default:
// This is handled in the initialization of the error string
break;
}
DEBUG_SERIAL.println();
if(_set)
{
DEBUG_SERIAL.print(F("*** SET ERROR: "));
#ifdef BEEP_ON_ERROR
g_beepController.beep(BEEP_FREQ_ERROR, 100, 50, beepCount);
#else
beepCount = beepCount; // No warning if unused variable
#endif
}
else
{
DEBUG_SERIAL.print(F("*** CLEAR ERROR: "));
}
DEBUG_SERIAL.println(errorString);
DEBUG_SERIAL.println();
}
void sendErrors()
{
g_telemetry.transmissionStart();
g_telemetry.sendTerm(telemetry_tag_error_flags);
g_telemetry.sendTerm(s_errorFlags);
g_telemetry.transmissionEnd();
}