forked from AeroQuad/AeroQuad
-
Notifications
You must be signed in to change notification settings - Fork 1
/
SerialCom.pde
568 lines (544 loc) · 17.4 KB
/
SerialCom.pde
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/*
AeroQuad v2.3 - March 2011
www.AeroQuad.com
Copyright (c) 2011 Ted Carancho. All rights reserved.
An Open Source Arduino based multicopter.
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 3 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, see <http://www.gnu.org/licenses/>.
*/
// SerialCom.pde is responsible for the serial communication for commands and telemetry from the AeroQuad
// This comtains readSerialCommand() which listens for a serial command and it's arguments
// This also contains readSerialTelemetry() which listens for a telemetry request and responds with the requested data
// For more information on each command/telemetry look at: http://aeroquad.com/content.php?117
// Includes re-write / fixes from Aadamson and ala42, special thanks to those guys!
// http://aeroquad.com/showthread.php?1461-We-have-some-hidden-warnings&p=14618&viewfull=1#post14618
//***************************************************************************************************
//********************************** Serial Commands ************************************************
//***************************************************************************************************
void readSerialPID(unsigned char PIDid) {
struct PIDdata* pid = &PID[PIDid];
pid->P = readFloatSerial();
pid->I = readFloatSerial();
pid->D = readFloatSerial();
pid->lastPosition = 0;
pid->integratedError = 0;
}
void readSerialCommand() {
// Check for serial message
if (Serial.available()) {
digitalWrite(LEDPIN, LOW);
queryType = Serial.read();
switch (queryType) {
case 'A': // Receive roll and pitch gyro PID
readSerialPID(ROLL);
readSerialPID(PITCH);
minAcro = readFloatSerial();
break;
case 'C': // Receive yaw PID
readSerialPID(YAW);
readSerialPID(HEADING);
headingHoldConfig = readFloatSerial();
heading = 0;
relativeHeading = 0;
headingHold = 0;
break;
case 'E': // Receive roll and pitch auto level PID
readSerialPID(LEVELROLL);
readSerialPID(LEVELPITCH);
readSerialPID(LEVELGYROROLL);
readSerialPID(LEVELGYROPITCH);
windupGuard = readFloatSerial();
break;
case 'G': // Receive auto level configuration
levelLimit = readFloatSerial();
levelOff = readFloatSerial();
break;
case 'I': // Receiver altitude hold PID
#ifdef AltitudeHold
readSerialPID(ALTITUDE);
PID[ALTITUDE].windupGuard = readFloatSerial();
minThrottleAdjust = readFloatSerial();
maxThrottleAdjust = readFloatSerial();
altitude.setSmoothFactor(readFloatSerial());
readSerialPID(ZDAMPENING);
#endif
break;
case 'K': // Receive data filtering values
gyro.setSmoothFactor(readFloatSerial());
accel.setSmoothFactor(readFloatSerial());
timeConstant = readFloatSerial();
break;
case 'M': // Receive transmitter smoothing values
receiver.setXmitFactor(readFloatSerial());
for(byte channel = ROLL; channel<LASTCHANNEL; channel++) {
receiver.setSmoothFactor(channel, readFloatSerial());
}
break;
case 'O': // Receive transmitter calibration values
for(byte channel = ROLL; channel<LASTCHANNEL; channel++) {
receiver.setTransmitterSlope(channel, readFloatSerial());
receiver.setTransmitterOffset(channel, readFloatSerial());
}
break;
case 'W': // Write all user configurable values to EEPROM
writeEEPROM(); // defined in DataStorage.h
zeroIntegralError();
break;
case 'Y': // Initialize EEPROM with default values
initializeEEPROM(); // defined in DataStorage.h
gyro.calibrate();
accel.calibrate();
zeroIntegralError();
#ifdef HeadingMagHold
compass.initialize();
#endif
#ifdef AltitudeHold
altitude.initialize();
#endif
break;
case '1': // Calibrate ESCS's by setting Throttle high on all channels
armed = 0;
calibrateESC = 1;
break;
case '2': // Calibrate ESC's by setting Throttle low on all channels
armed = 0;
calibrateESC = 2;
break;
case '3': // Test ESC calibration
armed = 0;
testCommand = readFloatSerial();
calibrateESC = 3;
break;
case '4': // Turn off ESC calibration
armed = 0;
calibrateESC = 0;
testCommand = 1000;
break;
case '5': // Send individual motor commands (motor, command)
armed = 0;
calibrateESC = 5;
for (byte motor = FRONT; motor < LASTMOTOR; motor++)
motors.setRemoteCommand(motor, readFloatSerial());
break;
case 'a': // fast telemetry transfer
if (readFloatSerial() == 1.0)
fastTransfer = ON;
else
fastTransfer = OFF;
break;
case 'b': // calibrate gyros
gyro.calibrate();
break;
case 'c': // calibrate accels
accel.calibrate();
#if defined(AeroQuadMega_CHR6DM) || defined(APM_OP_CHR6DM)
flightAngle->calibrate();
accel.setOneG(accel.getFlightData(ZAXIS));
#endif
break;
case 'd': // send aref
aref = readFloatSerial();
break;
case 'f': // calibrate magnetometer
#ifdef HeadingMagHold
compass.setMagCal(XAXIS, readFloatSerial(), readFloatSerial());
compass.setMagCal(YAXIS, readFloatSerial(), readFloatSerial());
compass.setMagCal(ZAXIS, readFloatSerial(), readFloatSerial());
#endif
break;
case '~': // read Camera values
#ifdef Camera
camera.setMode(readFloatSerial());
camera.setCenterPitch(readFloatSerial());
camera.setCenterRoll(readFloatSerial());
camera.setCenterYaw(readFloatSerial());
camera.setmCameraPitch(readFloatSerial());
camera.setmCameraRoll(readFloatSerial());
camera.setmCameraYaw(readFloatSerial());
camera.setServoMinPitch(readFloatSerial());
camera.setServoMinRoll(readFloatSerial());
camera.setServoMinYaw(readFloatSerial());
camera.setServoMaxPitch(readFloatSerial());
camera.setServoMaxRoll(readFloatSerial());
camera.setServoMaxYaw(readFloatSerial());
#endif
break;
}
digitalWrite(LEDPIN, HIGH);
}
}
//***************************************************************************************************
//********************************* Serial Telemetry ************************************************
//***************************************************************************************************
void PrintValueComma(float val) {
Serial.print(val);
comma();
}
void PrintValueComma(double val) {
Serial.print(val);
comma();
}
void PrintValueComma(char val) {
Serial.print(val);
comma();
}
void PrintValueComma(int val) {
Serial.print(val);
comma();
}
void PrintValueComma(unsigned long val)
{
Serial.print(val);
comma();
}
void PrintPID(unsigned char IDPid)
{
PrintValueComma(PID[IDPid].P);
PrintValueComma(PID[IDPid].I);
PrintValueComma(PID[IDPid].D);
}
void sendSerialTelemetry() {
update = 0;
switch (queryType) {
case '=': // Reserved debug command to view any variable from Serial Monitor
//PrintValueComma(degrees(flightAngle->getHeading()));
//PrintValueComma(rollPidUpdate);
//PrintValueComma(commandedYaw);
//Serial.print(degrees(flightAngle->getData(YAW)));
//Serial.println();
//printFreeMemory();
//queryType = 'X';
break;
case 'B': // Send roll and pitch gyro PID values
PrintPID(ROLL);
PrintPID(PITCH);
Serial.println(minAcro);
queryType = 'X';
break;
case 'D': // Send yaw PID values
PrintPID(YAW);
PrintPID(HEADING);
Serial.println(headingHoldConfig, BIN);
queryType = 'X';
break;
case 'F': // Send roll and pitch auto level PID values
PrintPID(LEVELROLL);
PrintPID(LEVELPITCH);
PrintPID(LEVELGYROROLL);
PrintPID(LEVELGYROPITCH);
Serial.println(windupGuard);
queryType = 'X';
break;
case 'H': // Send auto level configuration values
//Serial.print(levelLimit);
//comma();
PrintValueComma(levelLimit);
Serial.println(levelOff);
queryType = 'X';
break;
case 'J': // Altitude Hold
#ifdef AltitudeHold
PrintPID(ALTITUDE);
PrintValueComma(PID[ALTITUDE].windupGuard);
PrintValueComma(minThrottleAdjust);
PrintValueComma(maxThrottleAdjust);
PrintValueComma(altitude.getSmoothFactor());
PrintValueComma(PID[ZDAMPENING].P);
PrintValueComma(PID[ZDAMPENING].I);
Serial.println(PID[ZDAMPENING].D);
#else
for(byte i=0; i<9; i++) {
PrintValueComma(0);
}
Serial.println('0');
#endif
queryType = 'X';
break;
case 'L': // Send data filtering values
PrintValueComma(gyro.getSmoothFactor());
PrintValueComma(accel.getSmoothFactor());
Serial.println(timeConstant);
// comma();
// Serial.println(flightMode, DEC);
queryType = 'X';
break;
case 'N': // Send transmitter smoothing values
PrintValueComma(receiver.getXmitFactor());
for (byte axis = ROLL; axis < AUX; axis++) {
PrintValueComma(receiver.getSmoothFactor(axis));
}
Serial.println(receiver.getSmoothFactor(AUX));
queryType = 'X';
break;
case 'P': // Send transmitter calibration data
for (byte axis = ROLL; axis < AUX; axis++) {
PrintValueComma(receiver.getTransmitterSlope(axis));
PrintValueComma(receiver.getTransmitterOffset(axis));
}
PrintValueComma(receiver.getTransmitterSlope(AUX));
Serial.println(receiver.getTransmitterOffset(AUX));
queryType = 'X';
break;
case 'Q': // Send sensor data
for (byte axis = ROLL; axis < LASTAXIS; axis++) {
PrintValueComma(gyro.getData(axis));
}
for (byte axis = ROLL; axis < LASTAXIS; axis++) {
PrintValueComma(accel.getData(axis));
}
for (byte axis = ROLL; axis < YAW; axis++) {
PrintValueComma(levelAdjust[axis]);
}
PrintValueComma(degrees(flightAngle->getData(ROLL)));
PrintValueComma(degrees(flightAngle->getData(PITCH)));
#if defined(HeadingMagHold) || defined(AeroQuadMega_CHR6DM) || defined(APM_OP_CHR6DM)
//PrintValueComma(compass.getAbsoluteHeading());
PrintValueComma(flightAngle->getDegreesHeading(YAW));
#else
PrintValueComma(0);
#endif
#ifdef AltitudeHold
PrintValueComma(altitude.getData());
#else
PrintValueComma(0);
#endif
#ifdef BattMonitor
Serial.print(batteryMonitor.getData());
#else
Serial.print(0);
#endif
Serial.println();
break;
case 'R': // Raw magnetometer data
#if defined(HeadingMagHold)
PrintValueComma(compass.getRawData(XAXIS));
PrintValueComma(compass.getRawData(YAXIS));
Serial.println(compass.getRawData(ZAXIS));
#else
PrintValueComma(0);
PrintValueComma(0);
Serial.println('0');
#endif
break;
case 'S': // Send all flight data
PrintValueComma(deltaTime);
for (byte axis = ROLL; axis < LASTAXIS; axis++) {
if (axis == PITCH)
PrintValueComma(-gyro.getFlightData(axis));
else
PrintValueComma(gyro.getFlightData(axis));
}
#ifdef BattMonitor
PrintValueComma(batteryMonitor.getData());
#else
PrintValueComma(0);
#endif
for (byte axis = ROLL; axis < LASTAXIS; axis++) {
PrintValueComma(motors.getMotorAxisCommand(axis));
}
for (byte motor = FRONT; motor < LASTMOTOR; motor++) {
PrintValueComma(motors.getMotorCommand(motor));
}
for (byte axis = ROLL; axis < LASTAXIS; axis++) {
if (axis == ROLL)
PrintValueComma(accel.getFlightData(YAXIS));
else if (axis == PITCH)
PrintValueComma(accel.getFlightData(XAXIS));
else
PrintValueComma(accel.getFlightData(ZAXIS));
}
Serial.print(armed, BIN);
comma();
if (flightMode == STABLE)
PrintValueComma(2000);
if (flightMode == ACRO)
PrintValueComma(1000);
#ifdef HeadingMagHold
//PrintValueComma(compass.getAbsoluteHeading());
PrintValueComma(flightAngle->getDegreesHeading(YAW));
#else
PrintValueComma(0);
#endif
#ifdef AltitudeHold
PrintValueComma(altitude.getData());
Serial.print(altitudeHold, DEC);
#else
PrintValueComma(0);
Serial.print('0');
#endif
Serial.println();
break;
case 'T': // Send processed transmitter values
PrintValueComma(receiver.getXmitFactor());
for (byte axis = ROLL; axis < LASTAXIS; axis++) {
PrintValueComma(receiver.getData(axis));
}
for (byte axis = ROLL; axis < YAW; axis++) {
PrintValueComma(levelAdjust[axis]);
}
PrintValueComma(motors.getMotorAxisCommand(ROLL));
PrintValueComma(motors.getMotorAxisCommand(PITCH));
Serial.println(motors.getMotorAxisCommand(YAW));
break;
case 'U': // Send smoothed receiver with Transmitter Factor applied values
for (byte channel = ROLL; channel < AUX; channel++) {
PrintValueComma(receiver.getData(channel));
}
Serial.println(receiver.getData(AUX));
break;
case 'V': // Send receiver status
for (byte channel = ROLL; channel < AUX; channel++) {
PrintValueComma(receiver.getRaw(channel));
}
Serial.println(receiver.getRaw(AUX));
break;
case 'X': // Stop sending messages
break;
case 'Z': // Send heading
PrintValueComma(receiver.getData(YAW));
PrintValueComma(headingHold);
PrintValueComma(setHeading);
// AKA - Configurator wants -180/180 for headings,
// when heading hold active, the relative heading can be > 180 due to the way it's calculated
// this corrects it just for the configurator.
if ((setHeading + relativeHeading) > 180)
Serial.println(-360 + relativeHeading);
else
Serial.println(relativeHeading);
break;
case '6': // Report remote commands
for (byte motor = FRONT; motor < LEFT; motor++) {
PrintValueComma(motors.getRemoteCommand(motor));
}
Serial.println(motors.getRemoteCommand(LEFT));
break;
case '!': // Send flight software version
Serial.println(VERSION, 1);
queryType = 'X';
break;
case '#': // Send software configuration
// Determine which hardware is used to define max/min sensor values for Configurator plots
#if defined(AeroQuad_v1)
PrintValueComma(0);
#elif defined(AeroQuadMega_v1)
PrintValueComma(1);
#elif defined(AeroQuad_v18)
PrintValueComma(2);
#elif defined(AeroQuadMega_v2)
PrintValueComma(3);
#elif defined(AeroQuad_Wii)
PrintValueComma(4);
#elif defined(AeroQuadMega_Wii)
PrintValueComma(5);
#elif defined(ArduCopter)
PrintValueComma(6);
#elif defined(AeroQuadMega_CHR6DM)
PrintValueComma(5);
#elif defined(APM_OP_CHR6DM)
PrintValueComma(6);
#endif
// Determine which motor flight configuration for Configurator GUI
#if defined(plusConfig)
Serial.print('0');
#elif defined(XConfig)
Serial.print('1');
#elif defined(HEXACOAXIAL)
Serial.print('2');
#elif defined(HEXARADIAL)
Serial.print('3');
#endif
Serial.println();
queryType = 'X';
break;
case 'e': // Send AREF value
Serial.println(aref);
queryType = 'X';
break;
case 'g': // Send magnetometer cal values
#ifdef HeadingMagHold
Serial.print(compass.getMagMax(XAXIS), 2);
comma();
Serial.print(compass.getMagMin(XAXIS), 2);
comma();
Serial.print(compass.getMagMax(YAXIS), 2);
comma();
Serial.print(compass.getMagMin(YAXIS), 2);
comma();
Serial.print(compass.getMagMax(ZAXIS), 2);
comma();
Serial.println(compass.getMagMin(ZAXIS), 2);
#endif
queryType = 'X';
break;
case '`': // Send Camera values
#ifdef Camera
PrintValueComma(camera.getMode());
PrintValueComma(camera.getCenterPitch());
PrintValueComma(camera.getCenterRoll());
PrintValueComma(camera.getCenterYaw());
Serial.print(camera.getmCameraPitch() , 2);
comma();
Serial.print(camera.getmCameraRoll() , 2);
comma();
Serial.print(camera.getmCameraYaw() , 2);
comma();
PrintValueComma(camera.getServoMinPitch());
PrintValueComma(camera.getServoMinRoll());
PrintValueComma(camera.getServoMinYaw());
PrintValueComma(camera.getServoMaxPitch());
PrintValueComma(camera.getServoMaxRoll());
Serial.println(camera.getServoMaxYaw());
#endif
break;
}
}
// Used to read floating point values from the serial port
float readFloatSerial() {
#define SERIALFLOATSIZE 10
byte index = 0;
byte timeout = 0;
char data[SERIALFLOATSIZE] = "";
do {
if (Serial.available() == 0) {
delay(10);
timeout++;
}
else {
data[index] = Serial.read();
timeout = 0;
index++;
}
}
while ((index == 0 || data[index-1] != ';') && (timeout < 5) && (index < sizeof(data)-1));
data[index] = '\0';
return atof(data);
}
void comma() {
Serial.print(',');
}
void printInt(int data) {
byte msb, lsb;
msb = data >> 8;
lsb = data & 0xff;
Serial.print(msb, BYTE);
Serial.print(lsb, BYTE);
}
void sendBinaryFloat(float data) {
union binaryFloatType {
byte floatByte[4];
float floatVal;
} binaryFloat;
binaryFloat.floatVal = data;
Serial.print(binaryFloat.floatByte[3], BYTE);
Serial.print(binaryFloat.floatByte[2], BYTE);
Serial.print(binaryFloat.floatByte[1], BYTE);
Serial.print(binaryFloat.floatByte[0], BYTE);
}