-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathTelemetry.cpp
479 lines (413 loc) · 9.26 KB
/
Telemetry.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
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "ICommInterface.h"
#include "Telemetry.h"
static char *Telemetry_ItoA(int num, char* str, int base);
static char *Telemetry_DtoA(char *str, double num, int places);
CTelemetry::CTelemetry()
{
m_commInterface = 0;
m_receiveTarget = 0;
}
CTelemetry::~CTelemetry()
{
}
void CTelemetry::setInterfaces(ICommunicationInterface *_commInterface,
ITelemetry_ReceiveTarget *_receiveTarget)
{
m_commInterface = _commInterface;
m_receiveTarget = _receiveTarget;
}
void CTelemetry::tick()
{
if(!m_commInterface)
return;
while(m_commInterface->bytesInReceiveBuffer())
{
char parseBuff[256];
int dataLen = m_commInterface->read((unsigned char *)parseBuff, sizeof(parseBuff));
if(dataLen)
{
if(parseBuff[0] != '\0')
parse((unsigned char *)parseBuff, dataLen);
}
}
}
// Interface for sending data
void CTelemetry::transmissionStart()
{
// Make sure we have a comm interface to send it
if(!m_commInterface)
return;
m_transmitTermNumber = 0;
m_transmitChecksum = 0;
m_commInterface->puts("$");
m_commInterface->tick();
}
void CTelemetry::sendTerm(const char *_value)
{
// Make sure we have a comm interface to send it
if(!m_commInterface)
return;
// Copy the data to the buffer, prepending a comma if needed
char termBuf[CTelemetry_TERMSIZE * 2];
termBuf[0] = '\0';
if(m_transmitTermNumber)
strcpy(termBuf, ",");
strncat(termBuf, _value, sizeof(termBuf));
// Calculate the checksum
for(const char *c = termBuf; *c; c++)
m_transmitChecksum ^= (int)(*c);
// Bump term number
m_transmitTermNumber++;
// Finally, send the data
m_commInterface->puts(termBuf);
m_commInterface->tick();
}
void CTelemetry::sendTerm(int _value)
{
char numberBuf[CTelemetry_TERMSIZE + 1];
Telemetry_ItoA(_value, numberBuf, 10);
sendTerm(numberBuf);
}
void CTelemetry::sendTerm(unsigned int _value)
{
char numberBuf[CTelemetry_TERMSIZE + 1];
Telemetry_ItoA(_value, numberBuf, 10);
sendTerm(numberBuf);
}
void CTelemetry::sendTerm(bool _value)
{
sendTerm((_value) ? 1 : 0);
}
void CTelemetry::sendTerm(double _value)
{
char numberBuf[CTelemetry_TERMSIZE + 1];
Telemetry_DtoA(numberBuf, _value, 2);
sendTerm(numberBuf);
}
static const char *s_hexChars = "0123456789ABCDEF";
void CTelemetry::transmissionEnd()
{
// Make sure we have a comm interface to send it
if(!m_commInterface)
return;
// Create the checksum string
char termBuf[6];
termBuf[0] = '*';
termBuf[1] = s_hexChars[((m_transmitChecksum & 0xF0) >> 4)];
termBuf[2] = s_hexChars[(m_transmitChecksum & 0x0F)];
termBuf[3] = '\r';
termBuf[4] = '\n';
termBuf[5] = '\0';
// Send it
m_commInterface->puts(termBuf);
m_commInterface->tick();
}
// =========================================================
// Returns true if any sentences were processed (data may have changed)
void CTelemetry::parse(const unsigned char *_buf, unsigned int _bufLen)
{
unsigned int index;
const unsigned char *cPtr = _buf;
// Sanity
if((_buf == 0) || (_bufLen == 0))
return;
// parse it one character at a time.
for(index = 0; index < _bufLen; ++index)
parseChar(*cPtr++);
}
// This method was inspired by Mikal Hart's TinyGPS
void CTelemetry::parseChar(unsigned char _c)
{
// Screen out the newline character because we don't use it
if(_c == '\n')
return;
// Extremely special case for very (I mean VERY) noisy data streams)
if((m_state != TParser_S_WaitingForStart) &&
(_c == '$'))
{
// We have received a '$' when we were not expecting one, so the data must be a mess.
// We'll just reset the parser and continue as though this '$' is the start of the
// sentence.
m_state = TParser_S_WaitingForStart;
}
// How the characters are handled depends on which
// state we are in.
switch(m_state)
{
// Waiting for $
case TParser_S_WaitingForStart:
if(_c == '$')
{
// Prep for parsing terms
reset();
// Change state to term processing
m_state = TParser_S_ParsingTerms;
// Let command target know that we are starting
if(m_receiveTarget)
m_receiveTarget->startReception();
}
break;
// Processing comma-separated terms
case TParser_S_ParsingTerms:
switch(_c)
{
// Comma and asterisk both end the current term, so we do some
// common processing to reduce code size. However, asterisk causes
// a state change
case ',':
// Process this term
m_receiveChecksum ^= _c;
// Fall through
case '*':
processTerm();
// And start the next
m_receiveTermOffset = 0;
m_receiveTerm[m_receiveTermOffset] = '\0';
m_receiveTermNumber++;
// Star also ends the current term, and it changes state to checksum processing
if(_c == '*')
m_state = TParser_S_ProcessingChecksum;
break;
default:
m_receiveChecksum ^= _c;
addReceiveTermChar(_c);
break;
}
break;
// Checking the checksum term
case TParser_S_ProcessingChecksum:
if(_c == '\r') // This ends the current string so do EOL processing
{
// First, verify the checksum
if(m_receiveTermOffset == 2)
{
// Check the sentence checksum against ours
unsigned char checksum = 16 * from_hex(m_receiveTerm[0]) + from_hex(m_receiveTerm[1]);
if(m_receiveTarget)
{
if(checksum == m_receiveChecksum)
m_receiveTarget->receiveChecksumCorrect();
else
m_receiveTarget->receiveChecksumError();
}
}
// reset the parser
reset();
// And start looking for the next one
m_state = TParser_S_WaitingForStart;
}
else
{
// Not the end of the string, so keep processing characters
addReceiveTermChar(_c);
}
break;
// Invalid state... we should never get here
default:
break;
}
#ifdef CTelemetry_DEBUG
// Line number tracking for diagnostics
if(_c == '\r')
{
// Bump the line number
m_TParser_LineNumber++;
}
#endif
}
// Add character to accumulating term, but do not
// overrun the buffer
void CTelemetry::addReceiveTermChar(unsigned char _c)
{
if(m_receiveTermOffset < (CTelemetry_TERMSIZE - 1))
{
m_receiveTerm[m_receiveTermOffset++] = _c;
m_receiveTerm[m_receiveTermOffset] = '\0';
}
}
void CTelemetry::processTerm()
{
if(m_receiveTarget)
m_receiveTarget->receiveTerm(m_receiveTermNumber, m_receiveTerm);
}
void CTelemetry::reset()
{
m_state = TParser_S_WaitingForStart;
m_receiveTermOffset = 0;
m_receiveTerm[m_receiveTermOffset] = '\0';
m_receiveTermNumber = 0;
m_receiveChecksum = 0;
}
unsigned char CTelemetry::from_hex(unsigned char _a)
{
if (_a >= 'A' && _a <= 'F')
return _a - 'A' + 10;
else if (_a >= 'a' && _a <= 'f')
return _a - 'a' + 10;
else
return _a - '0';
}
/**
* Double to ASCII
* "Borrowed" from androider at
* http://stackoverflow.com/questions/2302969/how-to-implement-char-ftoafloat-num-without-sprintf-library-function-i
*/
static char *Telemetry_DtoA(char *str, double num, int places)
{
double precision = (1.0) / (pow(10, (double)(places + 1)));
// handle special cases
if (isnan(num))
{
strcpy(str, "nan");
}
else if (isinf(num))
{
strcpy(str, "inf");
}
else if (num == 0.0)
{
strcpy(str, "0");
}
else
{
int digit, m;
int m1 = 0;
char *c = str;
int neg = (num < 0);
if (neg)
{
num = -num;
}
// calculate magnitude
m = log10(num);
int useExp = (m >= 14 || (neg && m >= 9) || m <= -9);
if (neg)
{
*(c++) = '-';
}
// set up for scientific notation
if (useExp)
{
if (m < 0)
{
m -= 1.0;
}
num = num / pow(10.0, m);
m1 = m;
m = 0;
}
if (m < 1.0)
{
m = 0;
}
// convert the number
while (num > precision || m >= 0)
{
double weight = pow(10.0, m);
if (weight > 0 && !isinf(weight))
{
digit = floor(num / weight);
num -= (digit * weight);
*(c++) = '0' + digit;
}
if (m == 0 && num > 0)
{
*(c++) = '.';
}
m--;
}
if (useExp)
{
// convert the exponent
int i, j;
*(c++) = 'e';
if (m1 > 0)
{
*(c++) = '+';
}
else
{
*(c++) = '-';
m1 = -m1;
}
m = 0;
while (m1 > 0)
{
*(c++) = '0' + m1 % 10;
m1 /= 10;
m++;
}
c -= m;
for (i = 0, j = m - 1; i < j; i++, j--)
{
// swap without temporary
c[i] ^= c[j];
c[j] ^= c[i];
c[i] ^= c[j];
}
c += m;
}
*(c) = '\0';
}
return str;
}
/* A C++ program to implement itoa() */
template<class T> inline
void swap(T &first, T &second)
{
if (&first != &second)
{
T tmp = first;
first = second;
second = tmp;
}
}
/* A utility function to reverse a string */
static void Telemetry_Reverse(char str[], int length)
{
int start = 0;
int end = length - 1;
while (start < end)
{
swap(*(str + start), *(str + end));
start++;
end--;
}
}
// Implementation of itoa()
static char* Telemetry_ItoA(int num, char* str, int base)
{
int i = 0;
bool isNegative = false;
/* Handle 0 explicitely, otherwise empty string is printed for 0 */
if (num == 0)
{
str[i++] = '0';
str[i] = '\0';
return str;
}
// In standard itoa(), negative numbers are handled only with
// base 10. Otherwise numbers are considered unsigned.
if (num < 0 && base == 10)
{
isNegative = true;
num = -num;
}
// Process individual digits
while (num != 0)
{
int rem = num % base;
str[i++] = (rem > 9) ? (rem - 10) + 'a' : rem + '0';
num = num / base;
}
// If number is negative, append '-'
if (isNegative)
str[i++] = '-';
str[i] = '\0'; // Append string terminator
// Reverse the string
Telemetry_Reverse(str, i);
return str;
}