-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFlightComputer.cpp
400 lines (320 loc) · 12.9 KB
/
FlightComputer.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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA *
* *
***************************************************************************
* *
* (c) Copyright, 2001-2012, ANSR *
* *
***************************************************************************
* *
* Filename: FlightComputer.cpp *
* *
***************************************************************************/
#include "main.h"
bool_t GPSNmea::HasData()
{
return UART1::GetInstance()->IsCharReady();
}
uint8_t GPSNmea::ReadData()
{
return UART1::GetInstance()->ReadChar();
}
void GPSNmea::WriteData(uint8_t data)
{
UART1::GetInstance()->WriteChar (data);
}
void M25P80::IOSetCS(bool_t state)
{
SPI1::GetInstance()->SetCS (state);
}
uint8_t M25P80::SPIRead()
{
return SPI1::GetInstance()->Read();
}
void M25P80::SPIWrite(uint8_t data)
{
SPI1::GetInstance()->Write(data);
}
void CMX867A::IOSetCS(bool_t state)
{
SPI0::GetInstance()->SetCS(state);
}
uint8_t CMX867A::SPIRead()
{
return SPI0::GetInstance()->Read();
}
void CMX867A::SPIWrite(uint8_t data)
{
SPI0::GetInstance()->Write(data);
}
/// Reserve memory for singleton object.
static FlightComputer flightComputerSingletonObject;
/**
* Get a pointer to the HF-APRS control object.
*/
FlightComputer *FlightComputer::GetInstance()
{
return &flightComputerSingletonObject;
}
/**
* Constructor.
*/
FlightComputer::FlightComputer()
{
this->peakAltitude = 0;
this->gpsFixTime = 0;
this->statusLEDOffTick = 0;
this->timer100msFlag = false;
this->timer10sFlag = false;
this->timer1sFlag = false;
this->ms100Elapsed = 0;
this->msElapsed = 0;
this->sElapsed = 0;
this->launchDetect = false;
this->passThruMode = false;
}
/**
* Generate a plain text status packet.
*
* @param gps pointer to current GPS fix object
* @param text pointer to NULL terminated string with status packet
*/
void FlightComputer::StatusPacket(const GPSData *gps, char *text)
{
uint32_t time;
char buffer[100];
// Generate the GPRMC message.
if (this->gpsFixTime == 0)
strcpy (text, ">Balloon -:--:-- ");
else {
time = this->gpsFixTime;
sprintf (text, ">Balloon %ld:%02ld:%02ld ", time / 3600, ((time / 60) % 60), time % 60);
} // END if-else
sprintf (buffer, "%ld' %ld'pk ", gps->AltitudeFeet(), this->peakAltitude);
strcat (text, buffer);
// DOP and number of tracked birds.
sprintf (buffer, "%u.%01u%cdop %utrk ", gps->dop / 10, gps->dop % 10, (gps->fixType == GPSData::NoFix ? '-' : (gps->fixType == GPSData::Fix2D ? 'h' : 'p')), gps->trackedSats);
strcat (text, buffer);
// Internal temperature.
sprintf (buffer, "%lddegF ", LM92::GetInstance()->ReadTempF() / 10);
strcat (text, buffer);
strcat (text, "www.ansr.org\015");
}
void FlightComputer::LandingPrediction() {
MICEncoder micEncoder;
if(Log::GetInstance()->burstDetect) {
Log::GetInstance()->PredictLanding(&landingPrediction);
//NMEA::GPGGA(&landingPrediction, buffer);
//UART0::GetInstance()->WriteLine(buffer);
// transmit the predictions on a different SSID
this->afsk->PredictPacketSrc();
micEncoder.Encode (&landingPrediction);
this->afsk->Transmit (micEncoder.GetInformationField(), micEncoder.GetDestAddress());
this->afsk->NormalPacketSrc();
}
}
/**
* Generate an APRS packet based on the current GPS time.
*/
void FlightComputer::ScheduleMessage()
{
char buffer[150];
MICEncoder micEncoder;
RTCTime * time;
// Check for new characters from the GPS engine
this->gps->Update();
if (SystemControl::GetTick() > this->statusLEDOffTick)
IOPorts::StatusLED(IOPorts::LEDGreen, false);
if (this->gps->IsDataReady())
{
// Get a pointer to the GPS data set.
GPSData *gpsData = this->gps->Data();
// Process certain elements when we have a 3D fix.
if (gpsData->fixType == GPSData::Fix3D)
{
// Record the peak altitude.
if (gpsData->AltitudeFeet() > this->peakAltitude)
this->peakAltitude = gpsData->AltitudeFeet();
// Record the starting time.
if (this->gpsFixTime == 0)
{
this->gpsFixTime = 1;
// set the real-time clock
RTC::GetInstance()->Set(gpsData);
}
// update the wind data table
Log::GetInstance()->UpdateWindTable();
// check for launch. GPS must have been locked for more than 5 mins
if(!launchDetect && gpsFixTime > 300) {
posAscentCount = posAscentCount << 1;
if(Log::GetInstance()->RawAscentRate() > 300)
posAscentCount |= 0x01;
// if there's been 2 consecutive readings with a positive ascent rate
if((posAscentCount & 0x03) == 0x03) {
launchDetect = true;
// Initialize the wind data log
Log::GetInstance()->InitWindLog();
Log::GetInstance()->LaunchDetected();
}
}
}
IOPorts::StatusLED(IOPorts::LEDGreen, true);
if (gpsData->fixType == GPSData::NoFix)
this->statusLEDOffTick = SystemControl::GetTick() + 400;
else {
this->statusLEDOffTick = SystemControl::GetTick() + 50;
time = RTC::GetInstance()->Get();
switch (time->seconds)
{
case 10:
StatusPacket (this->gps->Data(), buffer);
this->afsk->Transmit (buffer);
UART0::GetInstance()->WriteLine (buffer);
break;
case 20:
// transmit a landing prediction packet
this->LandingPrediction();
break;
case 15:
case 45:
// transmit current location
micEncoder.Encode (this->gps->Data());
this->afsk->Transmit (micEncoder.GetInformationField(), micEncoder.GetDestAddress());
break;
} // END switch
}
// Engineering data set reported
sprintf (buffer, "%s Time:%02d:%02d:%02d %d/%d/%d\r\n", (gpsData->fixType == GPSData::NoFix ? "no fix" : (gpsData->fixType == GPSData::Fix2D ? "2D fix" : "3D fix")), gpsData->hours, gpsData->minutes, gpsData->seconds, gpsData->month, gpsData->day, gpsData->year);
//UART0::GetInstance()->Write (buffer);
sprintf (buffer, "Lat:%ld Long:%ld Dop:%d Tracked Sats:%d\r\n", gpsData->latitude, gpsData->longitude, gpsData->dop, gpsData->trackedSats);
//UART0::GetInstance()->Write (buffer);
sprintf (buffer, "%d deg @ %ld knots ", gpsData->heading, gpsData->SpeedKnots());
//UART0::GetInstance()->Write (buffer);
sprintf (buffer, "Alt:%ld ft", gpsData->AltitudeFeet());
UART0::GetInstance()->WriteLine (buffer);
UART0::GetInstance()->WriteLine ("AscentRate: %d", Log::GetInstance()->FilteredAscentRate());
} // END if
}
/**
* Function called every 1 ms to generate the application's timer tick
*/
void FlightComputer::SystemTimerTick()
{
// Get a pointer to the application's instance
FlightComputer * fc = FlightComputer::GetInstance();
// run the 1ms task necessary for the SD card library
disk_timerproc();
fc->msElapsed++;
// set 100ms, 1s, and 10s flags used for timing in the main loop
if(fc->msElapsed >= 100) {
fc->ms100Elapsed++;
fc->msElapsed = 0;
fc->timer100msFlag = true;
}
if(fc->ms100Elapsed >= 10) {
fc->sElapsed++;
fc->ms100Elapsed = 0;
fc->timer1sFlag = true;
}
if(fc->sElapsed >= 10) {
fc->sElapsed = 0;
fc->timer10sFlag = true;
}
}
/**
* Start and run the mission application.
*/
void FlightComputer::Run()
{
// Set the system clock to the minimum speed required for USB operation.
SystemControl::GetInstance()->Enable(SystemControl::Clock24MHz, SystemControl::Timer1Base);
// Setup Timer1 to run our system timer tick function on each tick
Timer1::GetInstance()->SetCallback((void (*)())&FlightComputer::SystemTimerTick);
// Set the GPIO to the default state.
IOPorts::Enable();
// Turn on both LEDs.
IOPorts::StatusLED (IOPorts::LEDYellow, true);
// UART 0 is the console serial port, UART 1 is the GPS engine.
UART0::GetInstance()->Enable(UART0::BaudRate57600);
UART1::GetInstance()->Enable(UART1::BaudRate9600, UART1::Control8N1);
// SPI 0 is the CMX867A MODEM. SPI 1 is the SD card which is setup by the SDlogger class
SPI0::GetInstance()->Enable();
// I2C Bus 0 is the temp sensor
I2C0::GetInstance()->Enable();
// Enable and set the output DAC voltage to 0 VDC.
DAC::GetInstance()->Enable();
DAC::GetInstance()->Set (0);
// CMX867A MODEM
CMX867A::GetInstance()->Enable();
// Temperature sensor.
LM92::GetInstance()->Enable();
// Setup the flash memory for logging.
Log::GetInstance()->Enable();
// Enable the RTC
RTC::GetInstance()->Enable();
// Get the engineering control object and enable it.
eng = Engineering::GetInstance();
eng->Enable();
// Objects used for the application
this->afsk = AFSK::GetInstance();
this->gps = GPSNmea::GetInstance();
// Show a startup message on the serial port.
UART0::GetInstance()->WriteLine (">ANSR Flight Computer v0.3 booted!");
// Turn off the LEDs.
IOPorts::StatusLED (IOPorts::LEDYellow, false);
// Transmit a startup message.
this->afsk->Transmit (">ANSR Flight Computer v0.3");
Log::GetInstance()->SystemBooted();
for (;;)
{
// forward packets to the UART from the GPS if in passthru mode
if(passThruMode) {
GPSNmea::GetInstance()->GpsPassthru();
}
else {
// Check the serial port for engineering commands.
eng->ProcessCommand();
// Update the waveform state machine as required.
this->afsk->Update();
if (!this->afsk->IsTransmit())
ScheduleMessage();
// 100 ms tasks
if(this->timer100msFlag) {
this->timer100msFlag = false;
// update the repeater controller
Repeater::GetInstance()->Update();
}
// 1 second tasks
if(this->timer1sFlag) {
this->timer1sFlag = false;
if (this->gpsFixTime > 0) {
this->gpsFixTime++;
}
}
// 10 second tasks
if(this->timer10sFlag && !this->afsk->IsTransmit()) {
this->timer10sFlag = false;
// log the current GPS fix
Log::GetInstance()->GPSFix(gps->Data());
// log the current temperature
Log::GetInstance()->CurrentTemp();
IOPorts::StatusLED(IOPorts::LEDRed, true);
Log::GetInstance()->Flush();
IOPorts::StatusLED(IOPorts::LEDRed, false);
}
}
} // END for(;;)
}