-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSerialCom.h
More file actions
457 lines (382 loc) · 15.9 KB
/
SerialCom.h
File metadata and controls
457 lines (382 loc) · 15.9 KB
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
//************************************************************************************
//
// general parameter modification function
//
//************************************************************************************
// types of config parameters
enum confType {
BOOL,
INT8,
INT16,
INT32,
UINT8,
UINT16,
UINT32
};
#define CONFIGNAME_MAX_LEN 17
typedef struct configDef {
char name[CONFIGNAME_MAX_LEN]; // name of config parameter
confType type; // type of config parameters
void * address; // address of config parameter
void (* updateFunction)(void); // function is called when parameter update happens
} t_configDef;
t_configDef configDef;
// alternatively access config decriptor as array of bytes
typedef union {
t_configDef c;
char bytes[sizeof(t_configDef)];
} t_configUnion;
t_configUnion configUnion;
void readEEPROM();
//******************************************************************************
//
// list of all config parameters
// to be accessed by par command
//
// descriptor is stored in PROGMEN to preserve RAM space
// see http://www.arduino.cc/en/Reference/PROGMEM
// and http://jeelabs.org/2011/05/23/saving-ram-space/
//
//******************************************************************************
const t_configDef PROGMEM configListPGM[] = {
{"configSet", UINT8, &config.configSet, &readEEPROM}, // select another EEPROM set
{"gyroPitchKp", INT32, &config.gyroPitchKp, &initPIDs},
{"gyroPitchKi", INT32, &config.gyroPitchKi, &initPIDs},
{"gyroPitchKd", INT32, &config.gyroPitchKd, &initPIDs},
{"gyroRollKp", INT32, &config.gyroRollKp, &initPIDs},
{"gyroRollKi", INT32, &config.gyroRollKi, &initPIDs},
{"gyroRollKd", INT32, &config.gyroRollKd, &initPIDs},
{"accTimeConstant", INT16, &config.accTimeConstant, &initIMUtc},
{"angleOffsetPitch", INT16, &config.angleOffsetPitch, &updateAngleOffset},
{"angleOffsetRoll", INT16, &config.angleOffsetRoll, &updateAngleOffset},
{"dirMotorPitch", INT8, &config.dirMotorPitch, NULL},
{"dirMotorRoll", INT8, &config.dirMotorRoll, NULL},
{"motorNumberPitch", UINT8, &config.motorNumberPitch, NULL},
{"motorNumberRoll", UINT8, &config.motorNumberRoll, NULL},
{"maxPWMmotorPitch", UINT8, &config.maxPWMmotorPitch, NULL},
{"maxPWMmotorRoll", UINT8, &config.maxPWMmotorRoll, NULL},
{"refVoltageBat", UINT16, &config.refVoltageBat, NULL},
{"cutoffVoltage", UINT16, &config.cutoffVoltage, NULL},
{"motorPowerScale", BOOL, &config.motorPowerScale, NULL},
{"rcAbsolutePitch", BOOL, &config.rcAbsolutePitch, NULL},
{"rcAbsoluteRoll", BOOL, &config.rcAbsoluteRoll, NULL},
{"maxRCPitch", INT8, &config.maxRCPitch, NULL},
{"maxRCRoll", INT8, &config.maxRCRoll, NULL},
{"minRCPitch", INT8, &config.minRCPitch, NULL},
{"minRCRoll", INT8, &config.minRCRoll, NULL},
{"rcGainPitch", INT16, &config.rcGainPitch, NULL},
{"rcGainRoll", INT16, &config.rcGainRoll, NULL},
{"rcLPFPitch", INT16, &config.rcLPFPitch, &initRC},
{"rcLPFRoll", INT16, &config.rcLPFRoll, &initRC},
{"rcModePPMPitch", BOOL, &config.rcModePPMPitch, &initRCPins},
{"rcModePPMRoll", BOOL, &config.rcModePPMRoll, &initRCPins},
{"rcModePPMAux", BOOL, &config.rcModePPMAux, &initRCPins},
{"rcModePPMFpvP", BOOL, &config.rcModePPMFpvPitch,&initRCPins},
{"rcModePPMFpvR", BOOL, &config.rcModePPMFpvRoll, &initRCPins},
{"rcPinModeCH0", UINT8, &config.rcPinModeCH0, &initRCPins},
{"rcPinModeCH1", UINT8, &config.rcPinModeCH1, &initRCPins},
{"rcPinModeCH2", UINT8, &config.rcPinModeCH2, &initRCPins},
{"rcChannelPitch", INT8, &config.rcChannelPitch, &initRCPins},
{"rcChannelRoll", INT8, &config.rcChannelRoll, &initRCPins},
{"rcChannelAux", INT8, &config.rcChannelAux, &initRCPins},
{"rcChannelFpvP", INT8, &config.rcChannelFpvPitch,&initRCPins},
{"rcChannelFpvR", INT8, &config.rcChannelFpvRoll, &initRCPins},
{"rcChannelPt0", INT8, &config.rcChannelPt0, NULL},
{"rcChannelPt1", INT8, &config.rcChannelPt1, NULL},
{"fpvGainPitch", INT8, &config.fpvGainPitch, NULL},
{"fpvGainRoll", INT8, &config.fpvGainRoll, NULL},
{"rcLPFPitchFpv", INT16, &config.rcLPFPitchFpv, &initRC},
{"rcLPFRollFpv", INT16, &config.rcLPFRollFpv, &initRC},
{"rcMid", INT16, &config.rcMid, &initRCPins},
{"fTrace", UINT8, &config.fTrace, NULL},
{"sTrace", UINT8, &config.sTrace, NULL},
{"enableGyro", BOOL, &config.enableGyro, NULL},
{"enableACC", BOOL, &config.enableACC, NULL},
{"axisReverseZ", BOOL, &config.axisReverseZ, &initSensorOrientation},
{"axisSwapXY", BOOL, &config.axisSwapXY, &initSensorOrientation},
{"fpvFreezePitch", BOOL, &config.fpvFreezePitch, NULL},
{"fpvFreezeRoll", BOOL, &config.fpvFreezeRoll, NULL},
{"maxPWMfpvPitch", UINT8, &config.maxPWMfpvPitch, NULL},
{"maxPWMfpvRoll", UINT8, &config.maxPWMfpvRoll, NULL},
{"fpvSwPitch", INT8, &config.fpvSwPitch, NULL},
{"fpvSwRoll", INT8, &config.fpvSwRoll, NULL},
{"altSwAccTime", INT8, &config.altSwAccTime, NULL},
{"accTimeConstant2", INT16, &config.accTimeConstant2, &initIMUtc},
{"gyroCal", BOOL, &config.gyroCal, &initSensorOrientation},
{"gyrOffsetX", INT16, &config.gyrOffsetX, &initSensorOrientation},
{"gyrOffsetY", INT16, &config.gyrOffsetY, &initSensorOrientation},
{"gyrOffsetZ", INT16, &config.gyrOffsetZ, &initSensorOrientation},
{"accOffsetX", INT16, &config.accOffsetX, &initSensorOrientation},
{"accOffsetY", INT16, &config.accOffsetY, &initSensorOrientation},
{"accOffsetZ", INT16, &config.accOffsetZ, &initSensorOrientation},
{"", BOOL, NULL, NULL} // terminating NULL required !!
};
// read bytes from program memory
void getPGMstring (PGM_P s, char * d, int numBytes) {
char c;
for (int i=0; i<numBytes; i++) {
*d++ = pgm_read_byte(s++);
}
}
// find Config Definition for named parameter
t_configDef * getConfigDef(char * name) {
void * addr = NULL;
bool found = false;
t_configDef * p = (t_configDef *)configListPGM;
while (true) {
getPGMstring ((PGM_P)p, configUnion.bytes, sizeof(configDef)); // read structure from program memory
if (configUnion.c.address == NULL) break;
if (strncmp(configUnion.c.name, name, CONFIGNAME_MAX_LEN) == 0) {
addr = configUnion.c.address;
found = true;
break;
}
p++;
}
if (found)
return &configUnion.c;
else
return NULL;
}
// print single parameter value
void printConfig(t_configDef * def) {
if (def != NULL) {
Serial.print(def->name);
Serial.print(F(" "));
switch (def->type) {
case BOOL : Serial.print(*(bool *)(def->address)); break;
case UINT8 : Serial.print(*(uint8_t *)(def->address)); break;
case UINT16 : Serial.print(*(uint16_t *)(def->address)); break;
case UINT32 : Serial.print(*(uint32_t *)(def->address)); break;
case INT8 : Serial.print(*(int8_t *)(def->address)); break;
case INT16 : Serial.print(*(int16_t *)(def->address)); break;
case INT32 : Serial.print(*(int32_t *)(def->address)); break;
}
Serial.println("");
} else {
printMessage(MSG_WARNING, F("printConfig: illegal parameter"));
}
}
// write single parameter with value
void writeConfig(t_configDef * def, int32_t val) {
if (def != NULL) {
switch (def->type) {
case BOOL : *(bool *)(def->address) = val; break;
case UINT8 : *(uint8_t *)(def->address) = val; break;
case UINT16 : *(uint16_t *)(def->address) = val; break;
case UINT32 : *(uint32_t *)(def->address) = val; break;
case INT8 : *(int8_t *)(def->address) = val; break;
case INT16 : *(int16_t *)(def->address) = val; break;
case INT32 : *(int32_t *)(def->address) = val; break;
}
// call update function
if (def->updateFunction != NULL) def->updateFunction();
} else {
printMessage(MSG_WARNING, F("writeConfig: illegal parameter"));
}
}
// print all parameters
void printConfigAll(t_configDef * p) {
while (true) {
getPGMstring ((PGM_P)p, configUnion.bytes, sizeof(configDef)); // read structure from program memory
if (configUnion.c.address == NULL) break;
printConfig(&configUnion.c);
p++;
}
Serial.println(F("done."));
// show firmware version after parameter output (so it appears at older GUIs as well)
printMessage(MSG_VERSION, F(""));
}
//******************************************************************************
// general parameter modification function
// par print all parameters
// par <parameter_name> print parameter <parameter_name>
// par <parameter_name> <value> set parameter <parameter_name>=<value>
//*****************************************************************************
void parameterMod() {
char * paraName = NULL;
char * paraValue = NULL;
int32_t val = 0;
if ((paraName = sCmd.next()) == NULL) {
// no command parameter, print all config parameters
printConfigAll((t_configDef *)configListPGM);
} else if ((paraValue = sCmd.next()) == NULL) {
// one parameter, print single parameter
printConfig(getConfigDef(paraName));
} else {
// two parameters, set specified parameter
val = atol(paraValue);
writeConfig(getConfigDef(paraName), val);
}
}
//************************************************************************************
//******************************************************************************
// set config to default
//*****************************************************************************
void updateAllParameters() {
initMotorStuff();
initPIDs();
initIMU();
initSensorOrientation();
updateAngleOffset();
initRCPins();
initRC();
}
//******************************************************************************
// set config to default and update derived values
//*****************************************************************************
void setDefaultParametersAndUpdate() {
setDefaultParameters();
updateAllParameters();
}
//******************************************************************************
// write current config set number into EEPROM
//*****************************************************************************
void writeConfigSetNumberToEEPROM(uint8_t configSet)
{
// write current config set number to EEPROM
EEPROM_writeAnything(0, configSet);
}
//******************************************************************************
// write current config set number into EEPROM
//*****************************************************************************
uint8_t readConfigSetNumberFromEEPROM()
{
uint8_t configSet;
// read current config set number
EEPROM_readAnything(0, configSet);
if (configSet >= NUM_EEPROM_CONFIG_SETS) {
configSet = 0;
}
return configSet;
}
//******************************************************************************
// write config into EEPROM
//*****************************************************************************
void writeEEPROM()
{
const uint16_t configBlockSize = sizeof(config);
uint8_t configSet = config.configSet;
uint16_t configBlockAddr;
// write current config set number to EEPROM
writeConfigSetNumberToEEPROM(configSet);
// eeprom address of the selected config set
configBlockAddr = sizeof(configSet) + configSet*configBlockSize;
traceModeType oldfTrace = config.fTrace;
traceModeType oldsTrace = config.sTrace;
config.fTrace = TRC_OFF;
config.sTrace = TRC_OFF;
config.crc8 = crcSlow((crc *)&config, sizeof(config)-1); // set proper CRC
EEPROM_writeAnything(configBlockAddr, config);
config.fTrace = oldfTrace;
config.sTrace = oldsTrace;
}
//******************************************************************************
// read EEPROM into config
//*****************************************************************************
void readEEPROM()
{
const uint16_t configBlockSize = sizeof(config);
uint8_t configSet = config.configSet;
uint16_t configBlockAddr;
if (configSet >= NUM_EEPROM_CONFIG_SETS) {
configSet = 0;
}
// eeprom address of the selected config set
configBlockAddr = sizeof(configSet) + configSet*configBlockSize;
EEPROM_readAnything(configBlockAddr, config);
if (config.crc8 == crcSlow((crc *)&config, sizeof(config)-1))
{
updateAllParameters();
} else {
// crc failed intialize directly here, as readEEPROM is void
//printMessage(MSG_WARNING, F("EEPROM CRC failed, initialize to default"));
setDefaultParameters();
writeEEPROM();
}
// write current config set number to EEPROM
writeConfigSetNumberToEEPROM(configSet);
config.configSet = configSet;
}
//******************************************************************************
// run GYRO calibration
//*****************************************************************************
void gyroCalibrateCmd()
{
printMessage(MSG_WARNING, F("GYRO Calibration, do not move ..."));
gyroOffsetCalibration();
printMessage(MSG_INFO, F("GYRO Calibration OK"));
}
//******************************************************************************
// run ACC calibration
//*****************************************************************************
void accCalibrateCmd()
{
printMessage(MSG_INFO, F("ACC Calibration, do not move ..."));
if (accCalibration() >= 0) {
Serial.println(F("ok"));
printMessage(MSG_INFO, F("ACC Calibration OK"));
} else {
printMessage(MSG_WARNING, F("ACC Calibration FAILED"));
}
}
//******************************************************************************
// save battry voltage into config
//*****************************************************************************
void saveBatteryRefVoltage()
{
// save the actual battery voltage to configuration
config.refVoltageBat = voltageBat * 100;
}
//******************************************************************************
// send version string
//*****************************************************************************
void printVersionString()
{
printMessage(MSG_VERSION, F(""));
}
void printHelpUsage()
{
#ifdef MINIMIZE_TEXT
Serial.println(F("see Readme.txt"));
#else
Serial.println(F("commands:"));
Serial.println(F(""));
Serial.println(F("sd # set defaults"));
Serial.println(F("we # write config to eeprom"));
Serial.println(F("re # restore config from eeprom"));
Serial.println(F("gc # calibrate gyro"));
Serial.println(F("ac # calibrate acc"));
Serial.println(F("sbv # save battery voltage"));
Serial.println(F("sc <n> # select config num"));
Serial.println(F("par <parName> <parValue> # parameter read/set command"));
Serial.println(F(" e.g."));
Serial.println(F(" par"));
Serial.println(F(" par gyroPitchKi"));
Serial.println(F(" par gyroPitchKi 12000"));
Serial.println(F(""));
Serial.println(F("he # print help"));
Serial.println(F(""));
#endif
}
void unrecognized(const char *command)
{
Serial.print(F("'"));
Serial.print(command);
Serial.println(F("' bad command"));
printHelpUsage();
}
void setSerialProtocol()
{
// Setup callbacks for SerialCommand commands
sCmd.addCommand("sd", setDefaultParametersAndUpdate);
sCmd.addCommand("we", writeEEPROM);
sCmd.addCommand("re", readEEPROM);
sCmd.addCommand("par", parameterMod);
sCmd.addCommand("gc", gyroCalibrateCmd);
sCmd.addCommand("ac", accCalibrateCmd);
sCmd.addCommand("sbv", saveBatteryRefVoltage);
sCmd.addCommand("ver", printVersionString);
sCmd.addCommand("he", printHelpUsage);
sCmd.setDefaultHandler(unrecognized); // Handler for command that isn't matched (says "What?")
}