-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerialCom.h
379 lines (330 loc) · 10.1 KB
/
SerialCom.h
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
/*
* SerialCom.h
*
* Created: 12/01/2015 23:41:18
* Author:
*/
#ifndef SERIALCOM_H_
#define SERIALCOM_H_
//************************************************************************************
//
// general parameter modification function
//
//************************************************************************************
#include "variables.h"
#include "EPROMAnything.h"
#include "generalFunctions.h"
// serial commands
SerialCommand sCmd; // The demo SerialCommand object
bool updateYawPIDParams = false;
bool updatePitchPIDParams = false;
// types of config parameters
enum confType {
BOOL,
INT8,
INT16,
INT32,
UINT8,
UINT16,
UINT32,
DOUBLE,
FLOAT
};
#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();
void writeEEPROM();
void initPitchPIDs();
void initYawPIDs();
void updateStepTime(void);
//******************************************************************************
//
// 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/
//
//******************************************************************************
t_configDef PROGMEM configListPGM[] =
{
{"configSet", UINT8, &config.configSet, &readEEPROM}, // select another EEPROM set
{"PitchKp", FLOAT, &config.PitchKp, &initPitchPIDs},
{"PitchKi", FLOAT, &config.PitchKi, &initPitchPIDs},
{"PitchKd", FLOAT, &config.PitchKd, &initPitchPIDs},
{"YawKp", FLOAT, &config.YawKp, &initYawPIDs},
{"YawKi", FLOAT, &config.YawKi, &initYawPIDs},
{"YawKd", FLOAT, &config.YawKd, &initYawPIDs},
{"AngleStep", FLOAT, &config.AngleStep, &updateStepTime},
{"", 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;
case FLOAT : Serial.print(*(float *)(def->address)); break;
case DOUBLE : Serial.print(*(double *)(def->address)); break;
}
Serial.println("");
}
else
{
Serial.println("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;
case DOUBLE : *(double *)(def->address) = val; break;
case FLOAT : *(float *)(def->address) = val; break;
}
// call update function
if (def->updateFunction != NULL) def->updateFunction();
}
else
{
Serial.println(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)
Serial.println(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()
{
initPitchPIDs();
initYawPIDs();
}
//******************************************************************************
// write current config set number into EEPROM
//*****************************************************************************
void writeConfigSetNumberToEEPROM(uint8_t configSet)
{
// write current config set number to EEPROM
EEPROM_writeAnything(0, 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;
config.crc8 = crcSlow((crc *)&config, sizeof(config)-1); // set proper CRC
EEPROM_writeAnything(configBlockAddr, config);
}
//******************************************************************************
// read EEPROM into config
//*****************************************************************************
void readEEPROM()
{
const uint16_t configBlockSize = sizeof(config);
uint8_t configSet = config.configSet;
uint16_t configBlockAddr;
// 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;
}
void sayHello() {
char *arg;
arg = sCmd.next(); // Get the next argument from the SerialCommand object buffer
if (arg != NULL) { // As long as it existed, take it
Serial.print("Hello ");
Serial.println(arg);
}
else {
Serial.println("Hello, whoever you are");
}
}
/*
void processCommand() {
int aNumber;
char *arg;
Serial.println("We're in processCommand");
arg = sCmd.next();
if (arg != NULL) {
aNumber = atoi(arg); // Converts a char string to an integer
Serial.print("First argument was: ");
Serial.println(aNumber);
}
else
{
Serial.println("No arguments");
}
arg = sCmd.next();
if (arg != NULL) {
aNumber = atol(arg);
Serial.print("Second argument was: ");
Serial.println(aNumber);
}
else {
Serial.println("No second argument");
}
}
*/
void printHelpUsage()
{
Serial.println(F("commands:"));
Serial.println(F(""));
Serial.println(F("sd # set defaults"));
Serial.println(F("par <parName> <parValue> # parameter read/set command"));
Serial.println(F(" e.g."));
Serial.println(F(" par"));
Serial.println(F(" par PitchKi"));
Serial.println(F(" par PitchKi 1.6"));
Serial.println(F(""));
Serial.println(F("help # print help"));
Serial.println(F(""));
}
// This gets set as the default handler, and gets called when no other command matches.
void unrecognized(const char *command) {
Serial.println("What?");
}
void initPitchPIDs(void)
{
updatePitchPIDParams = true;
}
void initYawPIDs(void)
{
updateYawPIDParams = true;
}
void updateStepTime(void)
{
step_time_ms = (unsigned long)(24.0 * 60.0 * 60.0 * 1000.0 * config.AngleStep / 360.0);
}
void initSerialCommands()
{
sCmd.addCommand("HELLO", sayHello); // Echos the string argument back
sCmd.addCommand("par", parameterMod); // Converts two arguments to integers and echos them back
sCmd.addCommand("help", printHelpUsage);
sCmd.addCommand("sd", setDefaultParameters);
sCmd.setDefaultHandler(unrecognized); // Handler for command that isn't matched (says "What?")
Serial.println("Ready");
}
#endif /* SERIALCOM_H_ */