-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtelemetryDump.c
267 lines (226 loc) · 5.8 KB
/
telemetryDump.c
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
/*
This file is part of AutoQuad.
AutoQuad 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 3 of the License, or
(at your option) any later version.
AutoQuad 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 AutoQuad. If not, see <http://www.gnu.org/licenses/>.
Copyright © 2011-2014 Bill Nesbitt
*/
#include "telemetryDump.h"
#include "serial.h"
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <getopt.h>
#include <stdlib.h>
#include <errno.h>
static struct telemetryFieldStruct telemetryFields[] = {
{"Roll Angle", FLOAT_T},
{"Pitch Angle", FLOAT_T},
{"Yaw Angle", FLOAT_T},
{"PWM Throttle", INT_T},
{"PWM Rudd", INT_T},
{"PWM Pitch", INT_T},
{"PWM Roll", INT_T},
{"PWM PZT", INT_T},
{"PWM PLT", INT_T},
{"GYO X", FLOAT_T},
{"GYO Y", FLOAT_T},
{"GYO Z", FLOAT_T},
{"ACC X", FLOAT_T},
{"ACC Y", FLOAT_T},
{"ACC Z", FLOAT_T},
{"Hold Heading", FLOAT_T},
{"Pressure", FLOAT_T},
{"Temperature", FLOAT_T},
{"Press Alt", FLOAT_T},
{"Volts In", FLOAT_T},
{"Micros per Sec", INT_T},
{"Pos N", FLOAT_T},
{"Pos E", FLOAT_T},
{"Pos D", FLOAT_T},
{"GPS Lat", FLOAT_T},
{"GPS Lon", FLOAT_T},
{"GPS HAcc", FLOAT_T},
{"GPS COG", FLOAT_T},
{"GPS Alt", FLOAT_T},
{"GPS PDOP", FLOAT_T},
{"Hold Bearing", FLOAT_T},
{"Hold Dist", FLOAT_T},
{"Hold Alt", FLOAT_T},
{"Pitch Tilt", FLOAT_T},
{"Roll Tilt", FLOAT_T},
{"Vel N", FLOAT_T},
{"Vel E", FLOAT_T},
{"Vel D", FLOAT_T},
{"MAG X", FLOAT_T},
{"MAG Y", FLOAT_T},
{"MAG Z", FLOAT_T},
{"Loop Freq", FLOAT_T},
{"Radio Signal", FLOAT_T},
{"Motor 1", FLOAT_T},
{"Motor 2", FLOAT_T},
{"Motor 3", FLOAT_T},
{"Motor 4", FLOAT_T},
{"Idle Percent", FLOAT_T},
{"Nav Bias X", FLOAT_T},
{"Nav Bias Y", FLOAT_T},
{"Nav Bias Z", FLOAT_T},
{"AGL", FLOAT_T},
{NULL, NO_T}
};
#define DEFAULT_PORT "/dev/ttyUSB0"
#define DEFAULT_BAUD 115200
char port[256];
unsigned int baud;
unsigned char parityA, parityB;
void telemetryDumpUsage(void) {
fprintf(stderr, "usage: telemetryDump <-h> <-p device_file> <-b baud_rate>\n");
}
int telemetryDumpInit(int argc, char **argv) {
int ch;
strncpy(port, DEFAULT_PORT, sizeof(port));
baud = DEFAULT_BAUD;
/* options descriptor */
static struct option longopts[] = {
{ "help", required_argument, NULL, 'h' },
{ "port", required_argument, NULL, 'p' },
{ "baud", required_argument, NULL, 's' },
{ NULL, 0, NULL, 0 }
};
while ((ch = getopt_long(argc, argv, "hp:b:f:o", longopts, NULL)) != -1)
switch (ch) {
case 'h':
telemetryDumpUsage();
exit(0);
break;
case 'p':
strncpy(port, optarg, sizeof(port));
break;
case 'b':
baud = atoi(optarg);
break;
default:
telemetryDumpUsage();
return 0;
}
argc -= optind;
argv += optind;
return 1;
}
void telemetryDumpHeaders(void) {
int i = 0;
while (telemetryFields[i].fieldName) {
if (i)
printf(", ");
printf("\"%s\"", telemetryFields[i].fieldName);
i++;
}
printf("\n");
}
void telemetryDumpFail(serialStruct_t *s) {
fprintf(stderr, "telemetryDump: read 1 failed with errno = %d, aborting...\n", errno);
serialFree(s);
fflush(stdout);
exit(1);
}
unsigned char telemetryDumpRead(serialStruct_t *s) {
unsigned char c;
if (read(s->fd, &c, 1) < 1)
telemetryDumpFail(s);
return c;
}
void telemetryDumpChecksum(unsigned char c) {
parityA += c;
parityB += parityA;
}
int telemetryDumpGetFloat(serialStruct_t *s, float *v) {
unsigned int p = 0;
unsigned char *c = (unsigned char *)v;
unsigned int i;
for (i = 0; i < sizeof(float); i++) {
*c = telemetryDumpRead(s);
telemetryDumpChecksum(*c++);
}
}
int telemetryDumpGetInt(serialStruct_t *s, int *v) {
unsigned int p = 0;
unsigned char *c = (unsigned char *)v;
unsigned int i;
for (i = 0; i < sizeof(int); i++) {
*c = telemetryDumpRead(s);
telemetryDumpChecksum(*c++);
}
}
void telemetryDump(serialStruct_t *s) {
char outputBuf[2048];
char buf[64];
unsigned char c;
float floatVal;
int intVal;
int i, j;
while (1) {
start:
// look for 'Aq' to indicate new line
c = telemetryDumpRead(s);
if (c != 'A')
goto start;
c = telemetryDumpRead(s);
if (c != 'q')
goto start;
parityA = parityB = 0;
c = telemetryDumpRead(s);
if (c == 'T') {
*outputBuf = 0;
i = 0;
while (telemetryFields[i].fieldName) {
if (i)
strncat(outputBuf, ", ", sizeof(outputBuf));
if (telemetryFields[i].fieldType == FLOAT_T) {
telemetryDumpGetFloat(s, &floatVal);
snprintf(buf, sizeof(buf), "%g", floatVal);
}
else if (telemetryFields[i].fieldType == INT_T) {
telemetryDumpGetInt(s, &intVal);
snprintf(buf, sizeof(buf), "%d", intVal);
}
strcat(outputBuf, buf);
i++;
}
c = telemetryDumpRead(s);
if (parityA != c) {
fprintf(stderr, "telemetryDump: checksum error\n");
goto start;
}
c = telemetryDumpRead(s);
if (parityB != c) {
fprintf(stderr, "telemetryDump: checksum error\n");
goto start;
}
printf("%s\n", outputBuf);
fflush(stdout);
}
}
}
int main(int argc, char **argv) {
serialStruct_t *s;
// init
if (!telemetryDumpInit(argc, argv)) {
fprintf(stderr, "Init failed, aborting...\n");
return 0;
}
if (!(s = initSerial(port, baud, 1))) {
fprintf(stderr, "Cannot open port '%s', aborting...\n", port);
return 0;
}
telemetryDumpHeaders();
telemetryDump(s);
return 1;
}