-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathflag-checker.cpp
488 lines (428 loc) · 16.2 KB
/
flag-checker.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
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
/************************************************************************/
/* */
/* P R O J E K T A V A L O N */
/* */
/* flag-checker.cpp ALL GENERAL CONSTANTS. */
/* */
/* Last Change April 02, 2009; Stefan Wismer */
/* */
/************************************************************************/
/**
* This sets all the general flags based on the individual flags
*
**/
// General Project Constants
#include "avalon.h"
// General Things
#include <stdlib.h>
#include <stdio.h>
// General rtx-Things
#include <rtx/getopt.h>
#include <rtx/main.h>
#include <rtx/error.h>
#include <rtx/timer.h>
#include <rtx/thread.h>
#include <rtx/message.h>
#include <DDXStore.h>
#include <DDXVariable.h>
#include "flags.h"
/**
* Global variable for all DDX object
* */
DDXStore store;
DDXVariable dataFlags;
DDXVariable dataRcFlags;
DDXVariable dataSailorFlags;
DDXVariable dataNaviFlags;
DDXVariable skipperFlagData;
// DDXVariable errorFlagData;
/**
* Storage for the command line arguments
* */
const char * varname_flags = "flags";
const char * varname_rcflags = "rcflags";
const char * varname_sailorflags = "sailorflags";
const char * varname_naviflags = "naviflags";
const char * varname_skipperflags = "skipperflags";
// const char * varname_errorflags = "errorflags";
const char * producerHelpStr = "Flags-Checker who sets all the general Flags";
/**
* Command line arguments
* */
RtxGetopt producerOpts[] = {
{"flagsname", "Store Variable where the flags are",
{
{RTX_GETOPT_STR, &varname_flags, ""},
RTX_GETOPT_END_ARG
}
},
{"rcflagsname", "Store Variable where the RC-Flags are",
{
{RTX_GETOPT_STR, &varname_rcflags, ""},
RTX_GETOPT_END_ARG
}
},
{"sailorflags", "Store Variable where the Sailor-Flags are",
{
{RTX_GETOPT_STR, &varname_sailorflags, ""},
RTX_GETOPT_END_ARG
}
},
{"naviflags", "Store Variable where the Navi-Flags are",
{
{RTX_GETOPT_STR, &varname_naviflags, "NaviFlags"},
RTX_GETOPT_END_ARG
}
},
#if 0
{"errorflags", "Store Variable where the flags are",
{
{RTX_GETOPT_STR, &varname_errorflags, ""},
RTX_GETOPT_END_ARG
}
},
#endif
RTX_GETOPT_END
};
/**
* Working thread, wait the data, transform them and write them again
* */
void * translation_thread(void * dummy)
{
Flags flags;
rcFlags rcflags;
sailorFlags sailorflags;
NaviFlags naviflags;
SkipperFlags skipperflags;
// ErrorFlags errorflags;
/* Initialization */
dataFlags.t_readto(flags,0,0);
dataRcFlags.t_readto(rcflags,0,0);
dataSailorFlags.t_readto(sailorflags,0,0);
dataNaviFlags.t_readto(naviflags,0,0);
skipperFlagData.t_readto(skipperflags,0,0);
// errorFlagData.t_readto(errorflags,0,0);
// General Flags:
flags.man_in_charge = AV_FLAGS_MIC_REMOTECONTROL;
flags.state = AV_FLAGS_ST_IDLE;
flags.state_requested = 0;
flags.sail_direction = 0;
flags.sailor_no_tack_or_jibe = 0;
flags.joystick_timeout = 0;
flags.navi_state = AV_FLAGS_NAVI_IDLE;
flags.navi_index_call = 0;
flags.navi_index_answer = 0;
flags.autonom_navigation = 0;
flags.global_locator = AV_FLAGS_GLOBALSK_LOCATOR;
// RC-Flags:
rcflags.emergency_stop = 0;
rcflags.motion_stop = 0;
rcflags.joystick_timeout = 0;
rcflags.sailorstate_requested = 0;
rcflags.man_in_charge = AV_FLAGS_MIC_REMOTECONTROL;
rcflags.autonom_navigation = 0;
// Sailor Flags:
sailorflags.state = 0;
sailorflags.sail_direction = 0;
sailorflags.no_tack_or_jibe = 0;
// Navigation Flags:
//naviflags.navi_state = AV_FLAGS_NAVI_NORMALNAVIGATION; //shoudn't make a difference, gets filled further down!!
naviflags.navi_state = AV_FLAGS_NAVI_NEWCALCULATION; //shoudn't make a difference, gets filled further down!!
naviflags.navi_index_call = 0;
naviflags.navi_index_answer = 0;
naviflags.sail_reset_index = 0;
//Skipper Flags:
skipperflags.global_locator = AV_FLAGS_GLOBALSK_LOCATOR;
//Error Flags_
// errorflags.state = 0;
//writing to store the initialized flags:
// errorFlagData.t_writefrom(errorflags);
skipperFlagData.t_writefrom(skipperflags);
dataFlags.t_writefrom(flags);
dataRcFlags.t_writefrom(rcflags);
dataSailorFlags.t_writefrom(sailorflags);
dataNaviFlags.t_writefrom(naviflags);
while (1) {
// Read the next data available, or wait at most 5 seconds
if (1) {
dataFlags.t_readto(flags,10.0,1);
dataRcFlags.t_readto(rcflags,0,0);
dataSailorFlags.t_readto(sailorflags,0,0);
dataNaviFlags.t_readto(naviflags,0,0);
skipperFlagData.t_readto(skipperflags,0,0);
// Reset state_requested if state has been reached:
if(flags.state == flags.state_requested)
{
flags.state_requested = 0;
}
switch(rcflags.man_in_charge)
{
case AV_FLAGS_MIC_NOONE:
flags.man_in_charge = AV_FLAGS_MIC_NOONE;
break;
case AV_FLAGS_MIC_SAILOR:
flags.man_in_charge = AV_FLAGS_MIC_SAILOR;
break;
case AV_FLAGS_MIC_REMOTECONTROL:
flags.man_in_charge = AV_FLAGS_MIC_REMOTECONTROL;
break;
default:
flags.man_in_charge = AV_FLAGS_MIC_NOONE;
}
// Set joystick_timeout
if(rcflags.joystick_timeout == 0)
{
flags.joystick_timeout = 0;
}
else
{
flags.joystick_timeout = 1;
}
//write the naviflags.indexes to flags:
flags.navi_index_call = naviflags.navi_index_call;
flags.navi_index_answer = naviflags.navi_index_answer;
flags.sail_reset_index = naviflags.sail_reset_index;
switch(skipperflags.global_locator)
{
case AV_FLAGS_GLOBALSK_LOCATOR:
flags.global_locator = AV_FLAGS_GLOBALSK_LOCATOR;
break;
case AV_FLAGS_GLOBALSK_TRACKER:
flags.global_locator = AV_FLAGS_GLOBALSK_TRACKER;
break;
case AV_FLAGS_GLOBALSK_CLOSING:
flags.global_locator = AV_FLAGS_GLOBALSK_CLOSING;
break;
case AV_FLAGS_GLOBALSK_AVOIDANCE:
flags.global_locator = AV_FLAGS_GLOBALSK_AVOIDANCE;
break;
case AV_FLAGS_GLOBALSK_SURVIVE:
flags.global_locator = AV_FLAGS_GLOBALSK_SURVIVE;
break;
default:
flags.global_locator = AV_FLAGS_GLOBALSK_LOCATOR;
}
#if 0
switch(errorflags.state)
{
case 0:
flags.error_state = 0;
case AV_FLAGS_ERROR_IMU:
flags.error_state = AV_FLAGS_ERROR_IMU;
case AV_FLAGS_ERROR_SYSTEM_ID:
flags.error_state = AV_FLAGS_ERROR_SYSTEM_ID;
}
#endif
switch(rcflags.autonom_navigation)
{
case 0:
flags.autonom_navigation = 0;
break;
case 1:
flags.autonom_navigation = 1;
break;
default:
flags.autonom_navigation = 0;
}
// Process sailor state transitions (just a pro forma action)
switch(sailorflags.state)
{
case 0:
break; // stay in current state
case AV_FLAGS_ST_IDLE:
flags.state = AV_FLAGS_ST_IDLE;
break;
case AV_FLAGS_ST_DOCK:
flags.state = AV_FLAGS_ST_DOCK;
break;
case AV_FLAGS_ST_NORMALSAILING:
flags.state = AV_FLAGS_ST_NORMALSAILING;
break;
case AV_FLAGS_ST_TACK:
flags.state = AV_FLAGS_ST_TACK;
break;
case AV_FLAGS_ST_JIBE:
flags.state = AV_FLAGS_ST_JIBE;
break;
case AV_FLAGS_ST_UPWINDSAILING:
flags.state = AV_FLAGS_ST_UPWINDSAILING;
break;
case AV_FLAGS_ST_DOWNWINDSAILING:
flags.state = AV_FLAGS_ST_DOWNWINDSAILING;
break;
case AV_FLAGS_ST_MAXENERGYSAVING:
flags.state = AV_FLAGS_ST_MAXENERGYSAVING;
break;
case AV_FLAGS_ST_HEADINGCHANGE:
flags.state = AV_FLAGS_ST_HEADINGCHANGE;
break;
default:
rtx_message("sailor_transitions requested illegal state transition...");
}
// set sail_direction (just pro forma)
switch(sailorflags.sail_direction)
{
case AV_FLAGS_SAIL_DIR_NOPREFERENCE:
flags.sail_direction = AV_FLAGS_SAIL_DIR_NOPREFERENCE;
break;
case AV_FLAGS_SAIL_DIR_ZERO:
flags.sail_direction = AV_FLAGS_SAIL_DIR_ZERO;
break;
case AV_FLAGS_SAIL_DIR_FRONT:
flags.sail_direction = AV_FLAGS_SAIL_DIR_FRONT;
break;
default:
rtx_message("sailor_transitions requested illegal sail direction for HEADINGCHANGE...");
flags.sail_direction = AV_FLAGS_SAIL_DIR_NOPREFERENCE;
}
// Set no_tack_or_jibe (just pro forma)
switch(sailorflags.no_tack_or_jibe)
{
case 0:
flags.sailor_no_tack_or_jibe = 0;
break;
case 1:
flags.sailor_no_tack_or_jibe = 1;
break;
default:
rtx_message("sailorflags.no_tack_or_jibe has illegal state...");
flags.sailor_no_tack_or_jibe = 0;
}
// Process sailor state requests from Remote Control (just a pro forma action)
switch(rcflags.sailorstate_requested)
{
case 0:
break; // dont do anything
case AV_FLAGS_ST_IDLE:
flags.state_requested = AV_FLAGS_ST_IDLE;
break;
case AV_FLAGS_ST_DOCK:
flags.state_requested = AV_FLAGS_ST_DOCK;
break;
case AV_FLAGS_ST_NORMALSAILING:
flags.state_requested = AV_FLAGS_ST_NORMALSAILING;
break;
case AV_FLAGS_ST_TACK:
flags.state_requested = AV_FLAGS_ST_TACK;
break;
case AV_FLAGS_ST_JIBE:
flags.state_requested = AV_FLAGS_ST_JIBE;
break;
case AV_FLAGS_ST_UPWINDSAILING:
flags.state_requested = AV_FLAGS_ST_UPWINDSAILING;
break;
case AV_FLAGS_ST_DOWNWINDSAILING:
flags.state_requested = AV_FLAGS_ST_DOWNWINDSAILING;
break;
case AV_FLAGS_ST_MAXENERGYSAVING:
flags.state_requested = AV_FLAGS_ST_MAXENERGYSAVING;
break;
case AV_FLAGS_ST_HEADINGCHANGE:
flags.state_requested = AV_FLAGS_ST_HEADINGCHANGE;
break;
default:
rtx_message("remote control requested illegal state transition...");
}
switch(rcflags.autonom_navigation)
{
case 0:
{
flags.navi_state = AV_FLAGS_NAVI_IDLE;
break;
}
case 1:
{
//Navigator Calling:
switch(naviflags.navi_state)
{
case 0:
rtx_message("something wrong with the navi_state, should not go through");
break; // dont do anything
case AV_FLAGS_NAVI_IDLE: //notfall, this case should not get used!
flags.navi_state = AV_FLAGS_NAVI_IDLE;
break;
case AV_FLAGS_NAVI_NEWCALCULATION:
flags.navi_state = AV_FLAGS_NAVI_NEWCALCULATION;
break;
case AV_FLAGS_NAVI_NORMALNAVIGATION:
flags.navi_state = AV_FLAGS_NAVI_NORMALNAVIGATION;
break;
case AV_FLAGS_NAVI_GOAL_APPROACH:
flags.navi_state = AV_FLAGS_NAVI_GOAL_APPROACH;
break;
default:
flags.navi_state = AV_FLAGS_NAVI_IDLE;
rtx_message("navi-state requested illegal...");
}
break;
}
default:
flags.navi_state = AV_FLAGS_NAVI_IDLE;
}
// write flags data to store
dataFlags.t_writefrom(flags);
// Do not write to any other flags here. Only one program per
// flags-variable!!
} else if (dataFlags.hasTimedOut()) {
// Timeout.
// Inform user
rtx_message("Timeout while reading flags data");
}
else
{
// Something strange happend. Critical Error.
rtx_error("Critical error while reading data");
// Emergency-Stop
rtx_main_signal_shutdown();
}
rtx_timer_sleep(0.1);
}
return NULL;
}
// Error handling for C functions (return 0 on success)
#define DOC(c) {int ret = c;if (ret != 0) {rtx_error("Command "#c" failed with value %d",ret);return -1;}}
// Error handling for C++ function (return true on success)
#define DOB(c) if (!(c)) {rtx_error("Command "#c" failed");return -1;}
// Error handling for pointer-returning function (return NULL on failure)
#define DOP(c) if ((c)==NULL) {rtx_error("Command "#c" failed");return -1;}
int
main (int argc, const char * argv[])
{
RtxThread * th;
int ret;
// Process the command line
if ((ret = RTX_GETOPT_CMD (producerOpts, argc, argv, NULL, producerHelpStr)) == -1) {
RTX_GETOPT_PRINT (producerOpts, argv[0], NULL, producerHelpStr);
exit (1);
}
rtx_main_init ("Flag-checker", RTX_ERROR_STDERR);
// Open the store
DOB(store.open());
// Register the new Datatypes
DOC(DDX_STORE_REGISTER_TYPE (store.getId(), Flags));
DOC(DDX_STORE_REGISTER_TYPE (store.getId(), rcFlags));
DOC(DDX_STORE_REGISTER_TYPE (store.getId(), sailorFlags));
DOC(DDX_STORE_REGISTER_TYPE (store.getId(), NaviFlags));
DOC(DDX_STORE_REGISTER_TYPE (store.getId(), SkipperFlags));
// DOC(DDX_STORE_REGISTER_TYPE (store.getId(), ErrorFlags));
// Connect to variables, and create variables for the target-data
DOB(store.registerVariable(dataFlags, varname_flags, "Flags"));
DOB(store.registerVariable(dataRcFlags, varname_rcflags, "rcFlags"));
DOB(store.registerVariable(dataSailorFlags, varname_sailorflags, "sailorFlags"));
DOB(store.registerVariable(dataNaviFlags, varname_naviflags, "NaviFlags"));
DOB(store.registerVariable(skipperFlagData, varname_skipperflags, "SkipperFlags"));
// DOB(store.registerVariable(errorFlagData, varname_errorflags, "ErrorFlags"));
// Start the working thread
DOP(th = rtx_thread_create ("Flag Checker Thread", 0,
RTX_THREAD_SCHED_OTHER, RTX_THREAD_PRIO_MIN, 0,
RTX_THREAD_CANCEL_DEFERRED,
translation_thread, NULL,
NULL, NULL));
// Wait for Ctrl-C
DOC (rtx_main_wait_shutdown (0));
rtx_message_routine ("Ctrl-C detected. Shutting down Flag Checker...");
// Terminating the thread
rtx_thread_destroy_sync (th);
// The destructors will take care of cleaning up the memory
return (0);
}