-
Notifications
You must be signed in to change notification settings - Fork 1
/
packet.cpp
528 lines (430 loc) · 11.6 KB
/
packet.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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
/* Space Whales Team: Packet Class for Weather Balloon Ground Station
Last Updated March 11, 2013
Released under GNU GPL - version 2 or later
By The Space Whales */
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include "packet.h"
#include "base.h"
using namespace std;
//////////////////////////////////////////////////////////////////////////////
/// Class Function Implementations
//////////////////////////////////////////////////////////////////////////////
double Packet::extractSens(string &raw)
{
// Find Data Boundaries
unsigned int indx = 0, endx = 0;
endx = raw.find(SENSDLIM, indx + 1);
// Check For Valid Length
if ((endx == string::npos) || (endx - indx == 1)) {
string error = "Invalid";
throw error;
}
// Try to Extract Data
string var = raw.substr(indx, endx - indx);
// Attempt Extraction
double reslt;
if (!(reslt = atof(var.c_str()))) {
string error = "Invalid";
throw error;
}
// Advance String
raw = raw.substr(endx + 1);
// Return Value
return reslt;
}
void Packet::parseSens(string &raw)
{
// Try to Extract Data
string dattyp;
try {
// Get Pressure
dattyp = "pressure data";
pres = extractSens(raw);
// Convert Pressure
pres = pres * 0.108506944 - 4.44444444;
// MPX5100 with Arduino at 1024 resolution
// 5V sensor voltage input
// Output in kPa
// Get Humidity
dattyp = "humidity data";
humd = extractSens(raw);
// Convert Humidity
humd = humd * 0.157510081 - 25.806451613;
// HIH-4030 with Arduino at 1024 resolution
// 5V sensor voltage input
// Output as Relative Humidity
// Get Acceleration
dattyp = "acceleration data";
for (int i = 0; i < 3; ++i) {
accel[i] = extractSens(raw);
// Covert Data
accel[i] = (accel[i] - ACCALSUB[i]) * ACCALMULT[i];
// Hand calibrated, see base.h
// Output in mG
}
// Get Temperature
dattyp = "temperature data";
for (int i = 0; i < 2; ++i) {
temp[i] = extractSens(raw);
// Convert Data
temp[i] = temp[i] * 0.48828125 - 50;
// TMP36 with Arduino at 1024 resolution
// 3.3V sensor voltage input
// Output in Celcius
}
} catch (string error) {
error = error + " " + dattyp + ".\n";
throw error;
}
// Indicate Success
cout << "Success.\n";
}
void Packet::parseGPS(const string &raw)
{
// Local Variables
int indx = 0;
int endx = 0;
string tmp;
// Check for "$GPGGA"
if (raw.find("$GPGGA") == string::npos) {
string error = "Unable to find $GPGGA.\n";
throw error;
}
// Verify GPS Lock
indx = raw.find("$GPGGA");
endx = raw.find("#", indx);
if ((endx - indx) == 47) {
string error = "No GPS Lock.\n";
throw error;
}
// Verify Length
if ((endx - indx) < 72) {
string error = "Incomplete GPS data.\n";
throw error;
}
// Interate Through 2 Commas
for (int i = 0; i < 2; ++i) {
tmp = raw.substr(indx);
indx = indx + tmp.find(',') + 1;
}
// Check That There isn't a Comma
if (raw.at(indx) == ',') {
string error = "No latitude data.\n";
throw error;
}
// Check Latitude Characters
bool valid = true;
for (int i = 0; i < 9; ++i) {
// Don't Check the Period
if (i != 4) {
// Verify all are Numbers
char num = raw.at(indx + i);
if ((num > '9') || (num < '0')) {
valid = false;
break;
}
}
}
// Try to Process Latitude
if (!valid) {
string error = "Bad latitude format.\n";
throw error;
}
// Extract Number and Change to Decimal
lat = convrtGPS(raw.substr(indx, 9));
char dir = raw.at(indx + 10);
// Correct North/South
if (dir == 'S')
lat = -lat;
// Check That There isn't a Comma
if (raw.at(indx + 12) == ',') {
string error = "No longitude data.\n";
throw error;
}
// Check Longitude Characters
for (int i = 0; i < 10; ++i) {
// Don't Check the Period
if (i != 5) {
char num = raw.at(indx + i + 12);
// Verify all are Numbers
if ((num > '9') || (num < '0')) {
valid = false;
break;
}
}
}
// Try to Process Longitude
if (!valid) {
string error = "Bad longitude format.\n";
throw error;
}
// Extract Number and Change to Decimal
lng = convrtGPS(raw.substr(indx + 12, 10));
dir = raw.at(indx + 23);
// Correct East/West
if (dir == 'W')
lng = -lng;
// Interate Through 5 Commas
tmp = raw.substr(indx + 25);
indx = 0;
for (int i = 0; i < 3; ++i) {
indx = tmp.find(',') + 1;
tmp = tmp.substr(indx);
}
// Check for Value
indx = tmp.find(',');
if (indx < 5 || indx > 8)
valid = false;
// Try to Process Altitude
if (!valid) {
string error = "Bad altitude format.\n";
throw error;
}
// Extract Number
tmp = tmp.substr(0, indx);
alt = atof(tmp.c_str());
// Indicate Success
cout << "Success.\n";
}
double Packet::convrtGPS(const string &raw)
{
// Extract the Minutes
string smin = raw.substr(raw.size() - 7, 7);
double fixd = atof(smin.c_str());
// Convert the Minutes
fixd *= DCONV;
// Add the Degrees
string sdeg = raw.substr(0, raw.size() - 7);
fixd += atof(sdeg.c_str());
// Return Result
return fixd;
}
void Packet::writeHeader(ofstream &maphtml, int mapdlay)
{
// Write Beginning of HTML Header
maphtml << "<!DOCTYPE html>\n<html>\n <head>\n <meta name=\"viewport\" content=\"initial-scale=1.0, user-scalable=no\" />";
// Swtich for HTML Refresh
if (mapdlay != 0)
maphtml << "<meta http-equiv=\"refresh\" content=\"" << mapdlay << "\" />";
// Write Remaining HTML Header
maphtml << "\n <style type=\"text/css\">\n html { height: 100% }\n body { height: 100%; margin: 0; padding: 0 }"
<< "\n #map_canvas { height: 100% }\n </style>\n <script type=\"text/javascript\""
<< "\n src=\"https://maps.googleapis.com/maps/api/js?key=AIzaSyBlMSLG5cI9PteGfyoqCG8OhkTdLqv5lmE&sensor=false\">"
<< "\n </script>\n <script type=\"text/javascript\">\n var map;\n function initialize() {"
<< "\n var initialLatlng = new google.maps.LatLng(42.1653, -83.4454);\n var lats, lngs;\n"
<< "\n var mapOptions = {\n center: initialLatlng,\n zoom: 8,\n mapTypeId: google.maps.MapTypeId.ROADMAP\n }"
<< "\n map = new google.maps.Map(document.getElementById(\"map_canvas\"),\n mapOptions);\n"
<< "\n lats = [";
}
void Packet::writePts(ofstream &maphtml, const string &dfilenm)
{
// Open Datafile
ifstream gpsdat;
gpsdat.open(dfilenm.c_str());
if (!gpsdat.good()) {
cout << "Could not open datafile.\n";
return;
}
// Storage for Lat/Long Data
double lat[MAXPTS], lon[MAXPTS];
int numpts = 0, pnt = 0;
// Read Until File Ends
while (gpsdat.good()) {
// Advance Past Sensor Data
string tmp;
getline(gpsdat, tmp);
int indx = 0;
for (int i = 0; i < 10; ++i) {
indx = tmp.find('\t', indx + 1);
}
// Read Lattitude
int lnth = tmp.find('\t', indx + 1) - indx - 1;
if (lnth)
lat[pnt] = atof((tmp.substr(indx + 1, lnth)).c_str());
else
lat[pnt] = 0;
// Read Longitude
indx += lnth + 1;
lnth = tmp.find('\t', indx + 1) - indx - 1;
if (lnth)
lon[pnt] = atof((tmp.substr(indx + 1, lnth)).c_str());
else
lon[pnt] = 0;
// Increment count
pnt = (pnt + 1) % MAXPTS;
if (numpts < MAXPTS)
++numpts;
}
// Don't Use End-of-File
--numpts;
// Write Latitude Data
for (int i = 0; i < numpts; ++i)
maphtml << lat[i] << ",";
maphtml << "];\n\t\tlngs = [";
// Write Longitude Data
for (int i = 0; i < numpts; ++i)
maphtml << lon[i] << ",";
// Close Datafile
gpsdat.close();
}
void Packet::writeEnd(ofstream &maphtml)
{
// Write File End
maphtml << "];\n\n var location;"
<< "\n for (var i=0; i<lats.length; i++) {\n location = new google.maps.LatLng(lats[i], lngs[i]);"
<< "\n var marker = new google.maps.Marker({\n position: location,"
<< "\n map: map,\n });\n }"
<< "\n\n map.setCenter(location);\n }\n </script>\n </head>\n <body onload=\"initialize()\">"
<< "\n <div id=\"map_canvas\" style=\"width:100%; height:100%\"></div>\n </body>\n</html>";
}
//////////////////////////////////////////////////////////////////////////////
/// Public Function Implementations
//////////////////////////////////////////////////////////////////////////////
void Packet::parseData(const unsigned char *buff, const int buff_size)
{
// String for Packet Data
string data;
echo = "";
int indx = 0;
// Try to Process Data Packet
cout << "Verifying data packet ..... \t";
try {
// Check for Empty Packet
if (buff_size == 0) {
string error = "No Data Received.\n";
throw error;
}
// Indicate Success
cout << "Success.\n";
// Catch Unusable Packet
} catch (string error) {
cout << "Error: " << error;
gps = false;
sens = false;
return;
}
// Repackage Packet Data
char tmp[MAXBUF]; // create a char[] to transfer data
castArray(buff, tmp, buff_size);
data = tmp;
// Remove Received Command
indx = data.find(DATDLIM);
if (indx)
echo = data.substr(0, indx);
data = data.substr(indx + 1);
// Remove Flight Time
indx = data.find(DATDLIM);
if (indx) {
string tmp1 = data.substr(0, indx);
int tmp2 = atoi(tmp1.c_str());
fltme.tm_sec = tmp2 % 60;
fltme.tm_min = (tmp2 / 60) % 60;
fltme.tm_hour = tmp2 / 3600;
}
data = data.substr(indx + 1);
// Print Flight Time
char flttime[20];
strftime(flttime, 20, "%T", &fltme);
cout << "Flight Time: " << flttime << "\t\t";
// Print Received Command
cout << "Balloon received \"" << echo << "\"\n";
// Try to Parse Sensor Data
try {
cout << "Parsing sensor data ..... \t";
parseSens(data);
sens = true;
} catch (string error) {
cout << "Error: " << error;
sens = false;
}
// Try to Parse GPS Data
indx = data.find(DATDLIM);
data = data.substr(indx + 1);
try {
cout << "Parsing GPS data ..... \t\t";
parseGPS(data);
gps = true;
// Print Altitude
cout << "Current Altitude: " << alt << " meters.\n";
// Catch Bad GPS Data
} catch (string error) {
cout << "Error: " << error;
gps = false;
}
}
void Packet::writeData(Param &inst)
{
// Write Data to File
if (openDFile(inst.datfile, inst.dfilenm)) {
// If All Data is Bad, Don't Write
if (!sens && !gps) {
cout << "No data to write.\n";
inst.datfile.close();
return;
}
// Write Timestamp
time_t now;
char systime[20];
time(&now);
strftime(systime, 20, "%T", localtime(&now));
inst.datfile << systime << "\t";
// Write Time of Flight
char flttime[20];
strftime(flttime, 20, "%T", &fltme);
inst.datfile << flttime << "\t";
// Write Echo
inst.datfile << echo << "\t";
// Write Sensor Data
if (sens)
inst.datfile << pres << "\t" << humd << "\t"
<< accel[0] << "\t" << accel[1] << "\t"
<< accel[2] << "\t" << temp[0] << "\t"
<< temp[1] << "\t";
else
inst.datfile << "\t\t\t\t\t\t\t";
// Write GPS Data
if (gps) {
inst.datfile << lat << "\t" << lng << "\t" << alt << "\t\n";
} else
inst.datfile << "\t\t\t\n";
// Save Datafile
inst.datfile.close();
// Indicate Success
cout << "Success.\n";
// Refresh HTML Map
if (gps) {
cout << "Writing new HTML file .....\t";
try {
writeHTML(inst.dfilenm, inst.mapdlay);
} catch (string error) {
cout << "Error: " << error;
}
}
// Throw Error
} else {
string error = "Error opening the datafile.\n";
throw error;
}
}
void Packet::writeHTML(const string &dfilenm, const int mapdlay)
{
// Make HTML File and Write Header
ofstream maphtml;
maphtml.open("GPSmap.html", ios_base::trunc);
if (!maphtml.good()) {
string error = "Error creating html file.\n";
throw error;
}
writeHeader(maphtml, mapdlay);
// Read Data from File and Place on Map
writePts(maphtml, dfilenm);
// Write HTML File Ending and Close
writeEnd(maphtml);
maphtml.close();
// Indicate Success.
cout << "Success.\n";
}