-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSunCalc.cpp
222 lines (190 loc) · 5.19 KB
/
SunCalc.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
////////////////////////////////////////////////////////////
// Sun Calc
////////////////////////////////////////////////////////////
#include <Arduino.h>
#include <GPSParser.h>
#include <SaveController.h>
#include "ICommInterface.h"
#include "Telemetry.h"
#include "TelemetryTags.h"
#include "MilliTimer.h"
#include "Pins.h"
#include "SunCalc.h"
#include "sunriset.h"
#include "DoorController.h"
#include "LightController.h"
#include "BeepController.h"
#include "GaryCooper.h"
extern CGPSParser g_GPSParser;
////////////////////////////////////////////////////////////
// Use GPS data to calculate sunrise, sunset, and current times.
// Times are in UTC and represented as float with hour in the
// integer and decimal hours instead of minutes in the fractional.
// ie 11:30 AM (UTC) is represented as 11.50, 11:45 AM (UTC)
// is represented as 11.75, and so on.
////////////////////////////////////////////////////////////
CSunCalc::CSunCalc()
{
m_currentTime = CSunCalc_INVALID_TIME;
m_sunriseTime = CSunCalc_INVALID_TIME;
m_sunsetTime = CSunCalc_INVALID_TIME;
}
CSunCalc::~CSunCalc()
{
}
bool CSunCalc::processGPSData(CGPSParserData &_gpsData)
{
// Assume he worst
m_currentTime = CSunCalc_INVALID_TIME;
m_sunriseTime = CSunCalc_INVALID_TIME;
m_sunsetTime = CSunCalc_INVALID_TIME;
#ifdef DEBUG_SUNCALC
DEBUG_SERIAL.println();
#endif
// Location
double lat = _gpsData.m_position.m_lat;
double lon = _gpsData.m_position.m_lon;
// If we don't have a lock then
// don't bother with the other stuff because
// it will not be set
if(!_gpsData.m_GPSLocked)
{
#ifdef DEBUG_SUNCALC
DEBUG_SERIAL.println(F("CSunCalc - GPS not locked."));
#endif
reportError(telemetry_error_GPS_not_locked, true);
return false;
}
else
{
reportError(telemetry_error_GPS_not_locked, false);
}
#ifdef DEBUG_SUNCALC
DEBUG_SERIAL.println(F("CSunCalc - GPS locked."));
#endif
// Date
int year = _gpsData.m_date.m_year;
int month = _gpsData.m_date.m_month;
int day = _gpsData.m_date.m_day;
int hour = _gpsData.m_time.m_hour;
int minute = _gpsData.m_time.m_minute;
#ifdef DEBUG_SUNCALC
DEBUG_SERIAL.print(F("CSunCalc - Date: "));
DEBUG_SERIAL.print(month);
DEBUG_SERIAL.print(F("/"));
DEBUG_SERIAL.print(day);
DEBUG_SERIAL.print(F("/"));
DEBUG_SERIAL.println(year);
DEBUG_SERIAL.print(F("CSunCalc - Lat: "));
DEBUG_SERIAL.print(lat);
DEBUG_SERIAL.print(F(" Lon: "));
DEBUG_SERIAL.println(lon);
DEBUG_SERIAL.println();
#endif
// Make sure we have good data
if(!GPS_IS_VALID_DATA(year) ||
!GPS_IS_VALID_DATA(month) ||
!GPS_IS_VALID_DATA(day) ||
!GPS_IS_VALID_DATA(hour) ||
!GPS_IS_VALID_DATA(minute) ||
!GPS_IS_VALID_DATA(lat) ||
!GPS_IS_VALID_DATA(lon))
{
reportError(telemetry_error_GPS_bad_data, true);
return false;
}
else
{
reportError(telemetry_error_GPS_bad_data, false);
}
// Figure current time
m_currentTime = hour + (minute / 60.);
// Get the rise and set times
civil_twilight( year, month, day, lon, lat,
&m_sunriseTime, &m_sunsetTime);
// Make sure the times make sense
normalizeTime(m_sunriseTime);
normalizeTime(m_sunsetTime);
#ifdef DEBUG_SUNCALC
DEBUG_SERIAL.print(F("CSunCalc - Current Time (UTC): "));
debugPrintDoubleTime(m_currentTime);
DEBUG_SERIAL.print(F("CSunCalc - Sunrise - Sunset (UTC): "));
debugPrintDoubleTime(m_sunriseTime, false);
DEBUG_SERIAL.print(" - ");
debugPrintDoubleTime(m_sunsetTime);
DEBUG_SERIAL.println();
#endif
return true;
}
void CSunCalc::sendTelemetry()
{
const char *emptyS = "";
// Telemetry
CGPSParserData gpsData = g_GPSParser.getGPSData();
g_telemetry.transmissionStart();
g_telemetry.sendTerm(telemetry_tag_GPSStatus);
g_telemetry.sendTerm(gpsData.m_GPSLocked);
if(gpsData.m_GPSLocked)
{
g_telemetry.sendTerm(gpsData.m_nSatellites);
g_telemetry.sendTerm(gpsData.m_position.m_lat);
g_telemetry.sendTerm(gpsData.m_position.m_lon);
}
else
{
g_telemetry.sendTerm(emptyS);
g_telemetry.sendTerm(emptyS);
g_telemetry.sendTerm(emptyS);
}
g_telemetry.transmissionEnd();
g_telemetry.transmissionStart();
g_telemetry.sendTerm(telemetry_tag_date_time);
if(gpsData.m_GPSLocked)
{
g_telemetry.sendTerm(gpsData.m_date.m_year);
g_telemetry.sendTerm(gpsData.m_date.m_month);
g_telemetry.sendTerm(gpsData.m_date.m_day);
g_telemetry.sendTerm(m_currentTime);
}
else
{
g_telemetry.sendTerm(emptyS);
g_telemetry.sendTerm(emptyS);
g_telemetry.sendTerm(emptyS);
g_telemetry.sendTerm(emptyS);
}
g_telemetry.transmissionEnd();
g_telemetry.transmissionStart();
g_telemetry.sendTerm(telemetry_tag_sun_times);
g_telemetry.sendTerm(m_sunriseTime);
g_telemetry.sendTerm(m_sunsetTime);
g_telemetry.transmissionEnd();
}
bool timeIsBetween(double _currentTime, double _first, double _second)
{
// See if they are practically the same
double difference = fabs(_first - _second);
if(difference < 0.02)
return false;
if(_first < _second)
{
if((_currentTime >= _first) && (_currentTime < _second))
return true;
else
return false;
}
else
{
if( ((_currentTime >= _first) && (_currentTime < 24.)) ||
((_currentTime > 0.) && (_currentTime < _second)) )
return true;
else
return false;
}
}
// Deal with rolling to the next day
void normalizeTime(double &_t)
{
while (_t < 0.) _t += 24.;
while (_t > 24.) _t -= 24.;
}