-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommandTransceiver.cpp
428 lines (366 loc) · 8.51 KB
/
CommandTransceiver.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
//
//
//
#include "CommandTransceiver.h"
void CommandTransceiverClass::init()
{
Serial1.begin(9600);
memset(input, 0, INPUT_LENGTH);
finished = 0;
for (byte i = 0; i < MAX_COMMAND_ID; ++i)
{
Command& cmd = commandList[i];
cmd.MotorID = 0;
cmd.Direction = 'N';
cmd.Delivered = false;
cmd.NumSteps = 0;
cmd.Speed_ms = 0;
}
}
void CommandTransceiverClass::update()
{
// Ein neuer Befehl kann nur abgesetzt werden, wenn kein Motor mehr einen Befehl abarbeitet.
// Dies ist notwendig, da das Auslesen der seriellen Schnittstelle dauert und die Motoren ansonsten nicht richtig drehen (Timing!)
if (Serial1.available() && finished == 0)
{
reset();
String buffer;
buffer.reserve(INPUT_LENGTH);
do
{
int chunk = Serial1.read();
if (chunk >= 0)
{
//Serial1.println(chunk);
buffer += (char)chunk;
}
} while (buffer[buffer.length() - 2] != buffer[buffer.length() - 1] || buffer[buffer.length() - 1] != '\n');
if (buffer.length() > INPUT_LENGTH)
{
send(MessageResponse::Syntax, F("Received Message too long."));
return;
}
memcpy(input, buffer.c_str(), buffer.length());
char* methodParameters;
char* line = strtok_r(input, METHOD_LINE_DELIMITER, &methodParameters);
// methode bearbeiten
char* methodArgs;
char* method = strtok_r(input, METHOD_ARGS_DELIMITER, &methodArgs);
if (strcasecmp(method, "RUN") == 0)
{
interpretMethod_Run(methodArgs, methodParameters);
// Command erfolgreich erhalten und alles ok!
hasData = true;
}
else if (strcasecmp(method, "CAL") == 0)
{
inCalibration = true;
finished = MAX_COMMAND_ID;
// TODO motoren anpassen an echten Kran: Direction und Speed
for (byte i = 0; i < MAX_COMMAND_ID; ++i)
{
Command& cmd = commandList[i];
cmd.MotorID = i;
cmd.Direction = 'L';
cmd.NumSteps = NUM_STEPS_MAX;
cmd.Speed_ms = 5;
}
hasData = true;
send(MessageResponse::CalOperating);
}
else
{
send(MessageResponse::Syntax, F("Unknown Method"));
}
}
}
void CommandTransceiverClass::reset()
{
for (byte i = 0; i < MAX_COMMAND_ID; ++i)
{
commandList[i].Delivered = false;
commandList[i].Speed_ms = 0;
commandList[i].Direction = 'N';
commandList[i].NumSteps = 0;
}
inCalibration = false;
error = false;
memset(input, 0, INPUT_LENGTH);
}
void CommandTransceiverClass::interpretMethod_Run(char * methodArgs, char * methodParameters)
{
char* save;
byte numberOfCommands = 0; // muss kleinergleich COMMAND_LENGTH sein
char* args = strtok_r(methodArgs, METHOD_ARGS_DELIMITER, &save);
while (methodArgs != 0)
{
int id = atoi(methodArgs); // TODO gegen strtol austauschen aufgrund fehleranfälligkeit
if (id >= 0 && id < MAX_COMMAND_ID)
{
commandList[numberOfCommands].MotorID = id;
numberOfCommands++;
for (byte i = 0; i < numberOfCommands - 1; i++)
{
if (commandList[i].MotorID == id)
{
send(MessageResponse::Semantic, F("Multiple settings of a single motor"));
return;
}
}
finished = numberOfCommands;
}
else
{
send(MessageResponse::Semantic, F("Motor does not exist"));
return; // Auslesen abbrechen
}
methodArgs = strtok_r(NULL, METHOD_ARGS_DELIMITER, &save);
}
save = NULL;
char* keyValues = strtok_r(methodParameters, METHOD_LINE_DELIMITER, &save);
char* keyValuesSave;
char* key;
while (keyValues != 0)
{
key = strtok_r(keyValues, PARAMETER_DELIMITER, &keyValuesSave);
if (strcasecmp_PF(key, F("Speed")) == 0)
{
if (!interpretParameter_Speed(keyValuesSave, numberOfCommands))
{
send(MessageResponse::Syntax, 0xFF);
keyValues = 0;
}
}
else if (strcasecmp_PF(key, F("NumSteps")) == 0)
{
interpretParameter_NumberOfSteps(keyValuesSave, numberOfCommands);
}
else if (strcasecmp_PF(key, F("Direction")) == 0)
{
interpretParameter_Direction(keyValuesSave, numberOfCommands);
}
else
{
send(MessageResponse::Syntax, F("Unknown parameter found"));
keyValues = 0;
}
keyValues = strtok_r(NULL, METHOD_LINE_DELIMITER, &save);
key = NULL;
}
}
bool CommandTransceiverClass::interpretParameter_Speed(char * values, byte numberOfCommands)
{
char* save = NULL;
char* value = strtok_r(values, VALUE_DELIMITER, &save);
byte i = 0;
while (value != 0)
{
if (i < numberOfCommands)
{
int speed_Hz = atoi(value); // TODO konvertierung testen
if (speed_Hz > SPEED_MIN_HZ && speed_Hz <= SPEED_MAX_HZ)
{
commandList[i].Speed_ms = (unsigned int)1000 / speed_Hz;
i++;
value = strtok_r(NULL, VALUE_DELIMITER, &save);
}
else
{
send(MessageResponse::Syntax, F("Out of Range Speed"));
value = 0;
return false;
}
}
else
{
send(MessageResponse::Semantic, F("Motor does not exist"));
value = 0;
return false;
}
}
return true;
}
bool CommandTransceiverClass::interpretParameter_Direction(char * values, byte numberOfCommands)
{
char* save = NULL;
char* value = strtok_r(values, VALUE_DELIMITER, &save);
byte i = 0;
while (value != 0)
{
if (i < numberOfCommands)
{
int direction = value[0];
if (direction == DIRECTION_LEFT || direction == DIRECTION_RIGHT)
{
commandList[i].Direction = direction;
i++;
value = strtok_r(NULL, VALUE_DELIMITER, &save);
}
else
{
send(MessageResponse::Syntax, F("Unknown Direction"));
value = 0;
return false;
}
}
else
{
send(MessageResponse::Semantic, F("Motor does not exist"));
value = 0;
return false;
}
}
return true;
}
bool CommandTransceiverClass::interpretParameter_HoldingTorgue(char * values, byte numberOfCommands)
{
return false;
}
bool CommandTransceiverClass::interpretParameter_NumberOfSteps(char * values, byte numberOfCommands)
{
char* save = NULL;
char* value = strtok_r(values, VALUE_DELIMITER, &save);
byte i = 0;
while (value != 0)
{
if (i < numberOfCommands)
{
int nsteps = atoi(value); // TODO konvertierung testen
if (nsteps > NUM_STEPS_MIN && nsteps <= NUM_STEPS_MAX)
{
commandList[i].NumSteps = nsteps;
i++;
value = strtok_r(NULL, VALUE_DELIMITER, &save);
}
else
{
send(MessageResponse::Syntax, F("Out of Range NumSteps"));
value = 0;
return false;
}
}
else
{
send(MessageResponse::Semantic, F("Motor does not exist"));
value = 0;
return false;
}
}
return true;
}
void CommandTransceiverClass::send(String & message)
{
message += F("\n\n");
Serial1.print(message);
Serial1.flush();
}
bool CommandTransceiverClass::isAvailable()
{
return hasData;
}
Command * CommandTransceiverClass::getCommand(byte motorId)
{
for (byte i = 0; i < MAX_COMMAND_ID; i++)
{
if (commandList[i].MotorID == motorId && commandList[i].Direction != 'N' && !commandList[i].Delivered)
{
commandList[i].Delivered = true;
return &commandList[i];
}
}
return nullptr;
}
void CommandTransceiverClass::setFinished(byte motorId)
{
finished--;
if (finished == 0)
{
send(MessageResponse::OK);
hasData = false;
}
}
void CommandTransceiverClass::setError(byte motorId)
{
if (inCalibration) // Kalibrierungsfahrt beendet
{
finished--;
if (finished == 0)
{
send(MessageResponse::CalOK);
inCalibration = false;
}
}
else // Echter Fehler
{
finished = 0;
hasData = false;
error = true;
send(MessageResponse::OutOfBound, motorId);
}
}
bool CommandTransceiverClass::hasError()
{
return error;
}
bool CommandTransceiverClass::isCalibrating()
{
return inCalibration;
}
void CommandTransceiverClass::send(MessageResponse type)
{
String message;
message.reserve(32);
message += type;
if (type == MessageResponse::OK)
{
message += F(" OK");
}
else if (type == MessageResponse::CalOperating)
{
message += F(" CALOPERATING");
}
else if (type == MessageResponse::CalOK)
{
message += F(" CALOK");
}
send(message);
}
void CommandTransceiverClass::send(MessageResponse type, byte motorId)
{
String message;
message.reserve(32);
message += type;
if (type == MessageResponse::Operating)
{
message += F(" OPERATING \nMotor: ");
message += motorId;
}
else if (type == MessageResponse::OutOfBound)
{
message += F(" OUTOFBOUND ERROR\nMotor: ");
message += motorId;
}
send(message);
}
void CommandTransceiverClass::send(MessageResponse type, const __FlashStringHelper *textLiteral)
{
String message;
message.reserve(32);
message += type;
if (type == MessageResponse::Semantic)
{
message += F(" SEMANTIC");
}
else if (type == MessageResponse::Syntax)
{
message += F(" SYNTAX");
}
String text(textLiteral);
if (text.length() > 0)
{
message += F("\nMessage: ");
message += text;
}
send(message);
}
CommandTransceiverClass CommandTransceiver;