-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsailor_transitions.cpp
700 lines (653 loc) · 30.5 KB
/
sailor_transitions.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
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
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
/************************************************************************/
/* */
/* P R O J E K T A V A L O N */
/* */
/* sailor_transitions.cpp This sets the different sailor states */
/* */
/* Last Change May 14th, 2009; Hendrik Erckens */
/* */
/************************************************************************/
// General Project Constants
#include "avalon.h"
// General Things
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.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>
// Specific Things
#include "flags.h"
#include "Ship.h"
#include "Sailstate.h"
#include "Rudderstate.h"
#include "windcleaner.h"
#include "imu.h"
#include "imucleaner.h"
#include "desired_course.h"
/**
* Global variable for all DDX object
* */
DDXStore store;
DDXVariable dataFlags;
DDXVariable dataSailorFlags;
DDXVariable dataSailState;
DDXVariable dataWindClean;
DDXVariable dataImu;
DDXVariable dataImuClean;
DDXVariable dataDesiredHeading;
/**
* Prototypes for self-defined utility functions
* */
int sign(int i);
int sign(float i);
int sign(double i);
double get_sail_AOA_coeff(double roll);
/**
* Storage for the command line arguments
* */
const char * varname_flags = "flags";
const char * varname_sailorflags = "sailorflags";
const char * varname_windDataClean = "cleanwind";
const char * varname_sailstate = "sailstate";
const char * varname_imu = "imu";
const char * varname_imuClean = "cleanimu";
const char * varname_desiredheading = "desiredheading";
const char * producerHelpStr = "This is the Sailor's transitions state machine";
/**
* Command line arguments
* */
RtxGetopt producerOpts[] =
{
{"flagsname", "Store-Variable where the data from the flags are",
{
{RTX_GETOPT_STR, &varname_flags, ""},
RTX_GETOPT_END_ARG
}
},
{"sailorflagsname", "Store Variable for sailor flags",
{
{RTX_GETOPT_STR, &varname_sailorflags, ""},
RTX_GETOPT_END_ARG
}
},
{"cleanwindname", "Store Variable where clean wind data is",
{
{RTX_GETOPT_STR, &varname_windDataClean, ""},
RTX_GETOPT_END_ARG
}
},
{"sailstatename", "Store Variable where sail state is",
{
{RTX_GETOPT_STR, &varname_sailstate, ""},
RTX_GETOPT_END_ARG
}
},
{"imuname", "Store Variable where the imuData is",
{
{RTX_GETOPT_STR, &varname_imu, ""},
RTX_GETOPT_END_ARG
}
},
{"cleanimuname", "Store Variable where the cleaned imu data is written to",
{
{RTX_GETOPT_STR, &varname_imuClean, ""},
RTX_GETOPT_END_ARG
}
},
{"desiredheadingname", "Store Variable where the desired heading is",
{
{RTX_GETOPT_STR, &varname_desiredheading, ""},
RTX_GETOPT_END_ARG
}
},
RTX_GETOPT_END
};
/**
* Working thread, wait for the data, transform them and write them again
* */
void * translation_thread(void * dummy)
{
Flags flags;
sailorFlags sailorflags;
Ship avalon;
Sailstate sailstate;
WindCleanData wind_clean;
imuData imu;
imuCleanData imu_clean;
DesiredHeading desired_heading;
double desired_heading_after_tack;
double desired_heading_after_jibe;
double desired_heading_after_change;
double wind_global_pre_jibe;
double wind_global_pre_tack;
double wind_global_pre_change;
double desired_bearing_after_jibe;
double desired_bearing_after_change;
int sign_wanted_sail_angle;
int sign_wanted_sail_angle_after_change;
double wanted_sail_angle_after_jibe;
double wanted_sail_angle_after_change;
int last_state = 0;
time_t tacktimeout_start, tacktimeout_currenttime;
double tacktimeout_diff;
time_t tackendtimeout_start, tackendtimeout_currenttime;
double tackendtimeout_diff;
time_t jibetimeout_start, jibetimeout_currenttime;
double jibetimeout_diff;
time_t jibeendtimeout_start, jibeendtimeout_currenttime;
double jibeendtimeout_diff;
time_t headingchangeendtimeout_start, headingchangeendtimeout_currenttime;
double headingchangeendtimeout_diff;
while (1)
{
dataFlags.t_readto(flags,0,0);
dataSailorFlags.t_readto(sailorflags,0,0);
dataImu.t_readto(imu,0,0);
dataSailState.t_readto(sailstate,0,0);
dataImuClean.t_readto(imu_clean,0,0);
dataWindClean.t_readto(wind_clean,0,0);
dataDesiredHeading.t_readto(desired_heading,0,0);
// calculate desired heading with drift compensation
if(imu_clean.velocity.x > 0.5) // below 0.5kn it probably doesn't make sense to compensate drift
{
desired_heading.heading = remainder(desired_heading.heading + atan2(imu_clean.velocity.drift, imu_clean.velocity.x),360.0);
}
// reset no_tack_or_jibe flag
sailorflags.no_tack_or_jibe = 0;
switch(flags.state)
{
case AV_FLAGS_ST_IDLE: //current state IDLE
switch(flags.state_requested)
{
case 0:
break;
case AV_FLAGS_ST_IDLE:
break;
case AV_FLAGS_ST_DOCK:
sailorflags.state = AV_FLAGS_ST_DOCK;
break;
case AV_FLAGS_ST_NORMALSAILING:
sailorflags.state = AV_FLAGS_ST_NORMALSAILING;
break;
case AV_FLAGS_ST_MAXENERGYSAVING:
sailorflags.state = AV_FLAGS_ST_MAXENERGYSAVING;
break;
default:
rtx_message("illegal state request %d. Staying in IDLE",flags.state_requested);
}
last_state = AV_FLAGS_ST_IDLE;
break;
case AV_FLAGS_ST_DOCK: //current state DOCK
switch(flags.state_requested)
{
case 0:
break;
case AV_FLAGS_ST_DOCK:
break;
case AV_FLAGS_ST_IDLE:
sailorflags.state = AV_FLAGS_ST_IDLE;
break;
case AV_FLAGS_ST_NORMALSAILING:
sailorflags.state = AV_FLAGS_ST_NORMALSAILING;
break;
case AV_FLAGS_ST_MAXENERGYSAVING:
sailorflags.state = AV_FLAGS_ST_MAXENERGYSAVING;
break;
default:
rtx_message("illegal state request %d. Staying in DOCK",flags.state_requested);
}
last_state = AV_FLAGS_ST_DOCK;
break;
case AV_FLAGS_ST_NORMALSAILING: //current state NORMALSAILING
// to UPWINDSAILING
// rtx_message("desired_head= %lf wind_clean= %lf \n",desired_heading.heading,wind_clean.global_direction_real );
if(fabs(remainder((desired_heading.heading - wind_clean.global_direction_real),360.0)) <= (AV_SAILOR_MAX_HEIGHT_TO_WIND-5))
{
sailorflags.state = AV_FLAGS_ST_UPWINDSAILING;
}
// to TACK
time(&tacktimeout_currenttime);
tacktimeout_diff = difftime(tacktimeout_currenttime, tacktimeout_start); // yields time in [s]
if((sign(remainder((desired_heading.heading - imu.attitude.yaw),360.0))
* sign(remainder((remainder((wind_clean.global_direction_real),360.0) - imu.attitude.yaw),360.0)) > 0)
&& (fabs(remainder((desired_heading.heading - imu.attitude.yaw),360.0))
> (fabs(remainder((remainder((wind_clean.global_direction_real),360.0) - imu.attitude.yaw),360.0)) + AV_SAILOR_TACK_HYSTERESIS)))
{
// rtx_message("head= %f wind_dir= %f des_head= %f",imu.attitude.yaw, wind_clean.global_direction_real, desired_heading.heading);
//if(tacktimeout_diff > 20.0) // next tack only if 20 seconds after the previous
if(1) // next tack only if 20 seconds after the previous
{
sailorflags.state = AV_FLAGS_ST_TACK;
time(&tackendtimeout_start); // start the timer
}
else // tell statemachine not to tack/jibe without TACK/JIBE...
{
// set the flag. It is being reset to 0 at the very top of
// the while loop
sailorflags.no_tack_or_jibe = 1;
}
}
// to DOWNWINDSAILING
if(fabs(remainder((desired_heading.heading - wind_clean.global_direction_real),360.0)) >= AV_SAILOR_MAX_DOWNWIND_ANGLE)
{
// rtx_message("NORM2UPW des_head: %f wind_dir: %f", desired_heading.heading, wind_clean.global_direction_real);
sailorflags.state = AV_FLAGS_ST_DOWNWINDSAILING;
}
// to JIBE
time(&jibetimeout_currenttime);
jibetimeout_diff = difftime(jibetimeout_currenttime, jibetimeout_start); // yields time in [s]
if((sign(remainder((desired_heading.heading - imu.attitude.yaw),360.0))
* sign(remainder((remainder((wind_clean.global_direction_real + 180.0),360.0) - imu.attitude.yaw),360.0)) > 0)
&& (fabs(remainder((desired_heading.heading - imu.attitude.yaw),360.0))
> (fabs(remainder((remainder((wind_clean.global_direction_real + 180.0),360.0) - imu.attitude.yaw),360.0)) + AV_SAILOR_JIBE_HYSTERESIS)))
{
//if(jibetimeout_diff > 20.0) // next jibe only if 20 seconds after the previous
if(1) // next jibe only if 20 seconds after the previous
{
sailorflags.state = AV_FLAGS_ST_JIBE;
//sailorflags.state = AV_FLAGS_ST_TACK;
time(&jibeendtimeout_start); // start the timer, will kick boat out of JIBE after x seconds
}
else // tell statemachine not to tack/jibe without TACK/JIBE...
{
// set the flag. It is being reset to 0 at the very top of
// the while loop
sailorflags.no_tack_or_jibe = 1;
}
}
// to HEADINGCHANGE
if(fabs(remainder((desired_heading.heading - imu.attitude.yaw),360.0)) > AV_SAILOR_HEADINGCHANGE_ERROR)
// maybe add wind speed condition...
{
if(sailorflags.state == AV_FLAGS_ST_TACK
&& wind_clean.bearing_real > 80.0)
{
sailorflags.sail_direction = AV_FLAGS_SAIL_DIR_ZERO;
sailorflags.state = AV_FLAGS_ST_HEADINGCHANGE;
}
else if(sailorflags.state == AV_FLAGS_ST_JIBE
&& wind_clean.bearing_real < 130.0)
{
sailorflags.sail_direction = AV_FLAGS_SAIL_DIR_FRONT;
sailorflags.state = AV_FLAGS_ST_HEADINGCHANGE;
}
else
{
sailorflags.sail_direction = AV_FLAGS_SAIL_DIR_NOPREFERENCE;
sailorflags.state = AV_FLAGS_ST_HEADINGCHANGE;
}
time(&headingchangeendtimeout_start); // start the timer, will kick boat out of HEADINGCHANGE after x seconds
}
switch(flags.state_requested)
{
case 0:
break;
case AV_FLAGS_ST_NORMALSAILING:
break;
case AV_FLAGS_ST_IDLE:
sailorflags.state = AV_FLAGS_ST_IDLE;
break;
case AV_FLAGS_ST_DOCK:
sailorflags.state = AV_FLAGS_ST_DOCK;
break;
case AV_FLAGS_ST_MAXENERGYSAVING:
sailorflags.state = AV_FLAGS_ST_MAXENERGYSAVING;
break;
default:
rtx_message("illegal state request %d. Staying in NORMALSAILING",flags.state_requested);
}
last_state = AV_FLAGS_ST_NORMALSAILING;
break;
case AV_FLAGS_ST_UPWINDSAILING: //current state UPWINDSAILING
// to NORMALSAILING
if(fabs(remainder((desired_heading.heading - wind_clean.global_direction_real),360.0)) > AV_SAILOR_MAX_HEIGHT_TO_WIND)
{
sailorflags.state = AV_FLAGS_ST_NORMALSAILING;
}
// to TACK
time(&tacktimeout_currenttime);
tacktimeout_diff = difftime(tacktimeout_currenttime, tacktimeout_start); // yields time in [s]
if((sign(remainder((desired_heading.heading - imu.attitude.yaw),360.0))
* sign(remainder((remainder((wind_clean.global_direction_real),360.0) - imu.attitude.yaw),360.0)) > 0)
&& (fabs(remainder((desired_heading.heading - imu.attitude.yaw),360.0))
> (fabs(remainder((remainder((wind_clean.global_direction_real),360.0) - imu.attitude.yaw),360.0)) + AV_SAILOR_TACK_HYSTERESIS)))
{
//if(tacktimeout_diff > 20.0) // next tack only if 20 seconds after the previous
if(1) // next tack only if 20 seconds after the previous
{
sailorflags.state = AV_FLAGS_ST_TACK;
time(&tackendtimeout_start); // start the timer
}
else // tell statemachine not to tack/jibe without TACK/JIBE...
{
// set the flag. It is being reset to 0 at the very top of
// the while loop
sailorflags.no_tack_or_jibe = 1;
}
}
switch(flags.state_requested)
{
case 0:
break;
case AV_FLAGS_ST_UPWINDSAILING:
break;
case AV_FLAGS_ST_IDLE:
sailorflags.state = AV_FLAGS_ST_IDLE;
break;
case AV_FLAGS_ST_DOCK:
sailorflags.state = AV_FLAGS_ST_DOCK;
break;
case AV_FLAGS_ST_NORMALSAILING:
sailorflags.state = AV_FLAGS_ST_NORMALSAILING;
break;
case AV_FLAGS_ST_MAXENERGYSAVING:
sailorflags.state = AV_FLAGS_ST_MAXENERGYSAVING;
break;
default:
rtx_message("illegal state request %d. Staying in UPWINDSAILING",flags.state_requested);
}
last_state = AV_FLAGS_ST_UPWINDSAILING;
break;
case AV_FLAGS_ST_DOWNWINDSAILING: //current state DOWNWINDSAILING
// to NORMALSAILING
if(fabs(remainder((desired_heading.heading - wind_clean.global_direction_real),360.0)) < AV_SAILOR_MAX_DOWNWIND_ANGLE)
{
sailorflags.state = AV_FLAGS_ST_NORMALSAILING;
}
// to JIBE
time(&jibetimeout_currenttime);
jibetimeout_diff = difftime(jibetimeout_currenttime, jibetimeout_start); // yields time in [s]
if((sign(remainder((desired_heading.heading - imu.attitude.yaw),360.0))
* sign(remainder((remainder((wind_clean.global_direction_real + 180.0),360.0) - imu.attitude.yaw),360.0)) > 0)
&& (fabs(remainder((desired_heading.heading - imu.attitude.yaw),360.0))
> (fabs(remainder((remainder((wind_clean.global_direction_real + 180.0),360.0) - imu.attitude.yaw),360.0)) + AV_SAILOR_JIBE_HYSTERESIS)))
{
//if(jibetimeout_diff > 20.0) // next jibe only if 20 seconds after the previous
if(1) // next jibe only if 20 seconds after the previous
{
sailorflags.state = AV_FLAGS_ST_JIBE;
time(&jibeendtimeout_start); // start the timer, will kick boat out of JIBE after x seconds
}
else // tell statemachine not to tack/jibe without TACK/JIBE...
{
// set the flag. It is being reset to 0 at the very top of
// the while loop
sailorflags.no_tack_or_jibe = 1;
}
}
switch(flags.state_requested)
{
case 0:
break;
case AV_FLAGS_ST_UPWINDSAILING:
break;
case AV_FLAGS_ST_IDLE:
sailorflags.state = AV_FLAGS_ST_IDLE;
break;
case AV_FLAGS_ST_DOCK:
sailorflags.state = AV_FLAGS_ST_DOCK;
break;
case AV_FLAGS_ST_NORMALSAILING:
sailorflags.state = AV_FLAGS_ST_NORMALSAILING;
break;
case AV_FLAGS_ST_MAXENERGYSAVING:
sailorflags.state = AV_FLAGS_ST_MAXENERGYSAVING;
break;
default:
rtx_message("illegal state request %d. Staying in DOWNWINDSAILING",flags.state_requested);
}
last_state = AV_FLAGS_ST_DOWNWINDSAILING;
break;
case AV_FLAGS_ST_TACK: //current state TACK
if(last_state != flags.state) //only when new in this state
{
desired_heading_after_tack = remainder(wind_clean.global_direction_real
- AV_SAILOR_MAX_HEIGHT_TO_WIND * sign(remainder(imu.attitude.yaw
- wind_clean.global_direction_real,360.0)),360.0);
wind_global_pre_tack = wind_clean.global_direction_real; // for HEADINGCHANGE
}
// rtx_message("des_head = %f, head = %f \n",desired_heading_after_tack,imu.attitude.yaw);
time(&tackendtimeout_currenttime);
tackendtimeout_diff = difftime(tackendtimeout_currenttime, tackendtimeout_start); // yields time in [s]
// to NORMALSAILING
if((fabs(remainder((desired_heading_after_tack - imu.attitude.yaw),360.0)) < AV_SAILOR_EPSILON_TACK)
|| (tackendtimeout_diff > AV_SAILOR_TACK_END_TIMEOUT))
{
// rtx_message("change to normalsailing");
sailorflags.state = AV_FLAGS_ST_NORMALSAILING;
time(&tacktimeout_start); // start the timer
}
switch(flags.state_requested)
{
case 0:
break;
case AV_FLAGS_ST_IDLE:
sailorflags.state = AV_FLAGS_ST_IDLE;
break;
case AV_FLAGS_ST_DOCK:
sailorflags.state = AV_FLAGS_ST_DOCK;
break;
case AV_FLAGS_ST_MAXENERGYSAVING:
sailorflags.state = AV_FLAGS_ST_MAXENERGYSAVING;
break;
default:
rtx_message("illegal state request %d. Staying in TACK",flags.state_requested);
}
last_state = AV_FLAGS_ST_TACK;
break;
case AV_FLAGS_ST_JIBE: //current state JIBE
if(last_state != flags.state) //only when new in this state
{
// set desired heading after jibe all the way to "the other side"
wind_global_pre_jibe = wind_clean.global_direction_real;
desired_heading_after_jibe = remainder((wind_clean.global_direction_real ////TODO fabs is weird!!!
+ AV_SAILOR_MAX_DOWNWIND_ANGLE * sign(wind_clean.bearing_real)),360.0);
desired_bearing_after_jibe = remainder((wind_global_pre_jibe - desired_heading_after_jibe),360.0);
sign_wanted_sail_angle = sign(desired_bearing_after_jibe); // +1 for wind from starboard, -1 for port
wanted_sail_angle_after_jibe = sign_wanted_sail_angle * AV_SAILOR_DOWNWIND_SAIL_DEGREES;
}
// rtx_message("des_head_af = %f des_bear_af = %f sail_ang_af = %f head = %f",desired_heading_after_jibe,desired_bearing_after_jibe, wanted_sail_angle_after_jibe, imu.attitude.yaw);
// to NORMALSAILING
time(&jibeendtimeout_currenttime);
jibeendtimeout_diff = difftime(jibeendtimeout_currenttime, jibeendtimeout_start); // yields time in [s]
if((fabs(remainder((wanted_sail_angle_after_jibe - sailstate.degrees_sail),360.0)) < AV_SAILOR_EPSILON_JIBE)
|| (jibeendtimeout_diff > AV_SAILOR_JIBE_END_TIMEOUT))
{
// rtx_message("to normal sailing; head= %f", imu.attitude.yaw);
sailorflags.state = AV_FLAGS_ST_NORMALSAILING;
time(&jibetimeout_start); // start the timer
}
switch(flags.state_requested)
{
case 0:
break;
case AV_FLAGS_ST_IDLE:
sailorflags.state = AV_FLAGS_ST_IDLE;
break;
case AV_FLAGS_ST_DOCK:
sailorflags.state = AV_FLAGS_ST_DOCK;
break;
case AV_FLAGS_ST_MAXENERGYSAVING:
sailorflags.state = AV_FLAGS_ST_MAXENERGYSAVING;
break;
default:
rtx_message("illegal state request %d. Staying in JIBE",flags.state_requested);
}
last_state = AV_FLAGS_ST_JIBE;
break;
case AV_FLAGS_ST_MAXENERGYSAVING: // current state MAXENERGYSAVING
switch(flags.state_requested)
{
case 0:
break;
case AV_FLAGS_ST_MAXENERGYSAVING:
break;
case AV_FLAGS_ST_IDLE:
sailorflags.state = AV_FLAGS_ST_IDLE;
break;
case AV_FLAGS_ST_DOCK:
sailorflags.state = AV_FLAGS_ST_DOCK;
break;
case AV_FLAGS_ST_NORMALSAILING:
sailorflags.state = AV_FLAGS_ST_NORMALSAILING;
break;
default:
rtx_message("illegal state request %d. Staying in MAXENERGYSAVING",flags.state_requested);
}
last_state = AV_FLAGS_ST_MAXENERGYSAVING;
break;
case AV_FLAGS_ST_HEADINGCHANGE: // current state HEADINGCHANGE
if(last_state != flags.state) // only when newly in this state
{
switch(last_state)
{
case AV_FLAGS_ST_TACK: // use old wind direction
wind_global_pre_change = wind_global_pre_tack;
break;
case AV_FLAGS_ST_JIBE: // use old wind direction
wind_global_pre_change = wind_global_pre_jibe;
break;
default: // get current wind reading
wind_global_pre_change = wind_clean.global_direction_real;
}
avalon.wanted_sail_angle_to_wind = AV_SAILOR_WANTED_AOA * get_sail_AOA_coeff(wind_clean.speed);
desired_heading_after_change = desired_heading.heading;
desired_bearing_after_change = remainder((wind_clean.global_direction_real - desired_heading_after_change),360.0);
sign_wanted_sail_angle_after_change = sign(desired_bearing_after_change);
wanted_sail_angle_after_change = remainder((desired_bearing_after_change - avalon.wanted_sail_angle_to_wind * sign_wanted_sail_angle),360.0);
}
// to NORMALSAILING
time(&headingchangeendtimeout_currenttime);
headingchangeendtimeout_diff = difftime(headingchangeendtimeout_currenttime, headingchangeendtimeout_start); // yields time in [s]
if((fabs(remainder((wanted_sail_angle_after_change - sailstate.degrees_sail),360.0)) < AV_SAILOR_EPSILON_HEADINGCHANGE)
|| (headingchangeendtimeout_diff > AV_SAILOR_HEADINGCHANGE_END_TIMEOUT))
{
sailorflags.state = AV_FLAGS_ST_NORMALSAILING;
}
// to UPWINDSAILING
if(fabs(sailstate.degrees_sail) < AV_SAILOR_UPWIND_MIN_SAIL_DEGREES)
{
sailorflags.state = AV_FLAGS_ST_UPWINDSAILING;
}
// to DOWNWINDSAILING
if(fabs(sailstate.degrees_sail) > AV_SAILOR_DOWNWIND_SAIL_DEGREES)
{
sailorflags.state = AV_FLAGS_ST_DOWNWINDSAILING;
}
switch(flags.state_requested)
{
case 0:
break;
case AV_FLAGS_ST_HEADINGCHANGE:
break;
case AV_FLAGS_ST_MAXENERGYSAVING:
sailorflags.state = AV_FLAGS_ST_MAXENERGYSAVING;
break;
case AV_FLAGS_ST_IDLE:
sailorflags.state = AV_FLAGS_ST_IDLE;
break;
case AV_FLAGS_ST_DOCK:
sailorflags.state = AV_FLAGS_ST_DOCK;
break;
case AV_FLAGS_ST_NORMALSAILING:
sailorflags.state = AV_FLAGS_ST_NORMALSAILING;
break;
default:
rtx_message("illegal state request %d. Staying in HEADINGCHANGE",flags.state_requested);
}
last_state = AV_FLAGS_ST_HEADINGCHANGE;
break;
default:
rtx_message("current sailor state %d is illegal. Switching to IDLE",flags.state_requested);
sailorflags.state = AV_FLAGS_ST_IDLE;
}
dataSailorFlags.t_writefrom(sailorflags);
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;}
// Some self-defined utility functions:
int sign(int i) // gives back the sign of an int
{
if (i>=0)
return 1;
else
return -1;
}
int sign(float i) // gives back the sign of a float
{
if (i>=0)
return 1;
else
return -1;
}
int sign(double i) // gives back the sign of a double
{
if (i>=0)
return 1;
else
return -1;
}
double get_sail_AOA_coeff(double roll)
{
// 0.7∙1/(1+exp(0.2(abs(x)−23)))+0.3
return (0.7*(1 / (1 + exp(0.2 * (fabs(roll) - 26))))+0.3);
// = 0.5 * cos((imu_clean.attitude.roll + 17) / 30) + 0.5 - 0.12 * cos((imu_clean.attitude.roll + 17) / 14) + 0.12;
}
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 ("Sailor Transitions Main", 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(), sailorFlags));
DOC(DDX_STORE_REGISTER_TYPE (store.getId(), WindCleanData));
DOC(DDX_STORE_REGISTER_TYPE (store.getId(), Sailstate));
DOC(DDX_STORE_REGISTER_TYPE (store.getId(), imuData));
DOC(DDX_STORE_REGISTER_TYPE (store.getId(), DesiredHeading));
DOC(DDX_STORE_REGISTER_TYPE (store.getId(), imuCleanData));
// Connect to existing variables, or create new variables
DOB(store.registerVariable(dataFlags, varname_flags, "Flags"));
DOB(store.registerVariable(dataSailorFlags, varname_sailorflags, "sailorFlags"));
DOB(store.registerVariable(dataWindClean, varname_windDataClean, "WindCleanData"));
DOB(store.registerVariable(dataSailState, varname_sailstate, "Sailstate"));
DOB(store.registerVariable(dataImu, varname_imu, "imuData"));
DOB(store.registerVariable(dataDesiredHeading, varname_desiredheading, "DesiredHeading"));
DOB(store.registerVariable(dataImuClean, varname_imuClean, "imuCleanData"));
// Start the working thread
DOP(th = rtx_thread_create ("Sailor Transitions 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 Sailor Transitions...");
// Terminating the thread
rtx_thread_destroy_sync (th);
// The destructors will take care of cleaning up the memory
return (0);
}