-
Notifications
You must be signed in to change notification settings - Fork 2
/
sailor_statemachine.cpp
848 lines (735 loc) · 39 KB
/
sailor_statemachine.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
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
/************************************************************************/
/* */
/* P R O J E K T A V A L O N */
/* */
/* sailor_statemachine.cpp This is the sailor state machine */
/* */
/* Last Change April 12, 2009; Hendrik Erckens */
/* */
/************************************************************************/
//bla to test git, and again, and yet again
// General Project Constants
#include "avalon.h"
// General Things
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
// General gsl-Things
#include <gsl/gsl_errno.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_roots.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>
// Specific Things
#include <rtx/pid.h>
#include <DDXStore.h>
#include <DDXVariable.h>
#include "flags.h"
#include "sail-target.h"
#include "rudder-target.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 dataRudder;
DDXVariable dataSail;
DDXVariable dataFlags;
DDXVariable dataSailorFlags;
DDXVariable dataSailState;
DDXVariable dataRudderStateLeft;
DDXVariable dataRudderStateRight;
DDXVariable dataWindClean;
DDXVariable dataImu;
DDXVariable dataImuClean;
DDXVariable dataDesiredHeading;
/**
* Prototypes for 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_ruddertarget = "rudder";
const char * varname_sailtarget = "sail";
const char * varname_windDataClean = "cleanwind";
const char * varname_sailstate = "sailstate";
const char * varname_rudderstateleft = "rudderstateleft";
const char * varname_rudderstateright = "rudderstateright";
const char * varname_imu = "imu";
const char * varname_imuClean = "cleanimu";
const char * varname_desiredheading = "desiredheading";
const char * producerHelpStr = "This is the Ship's skipper";
/**
* Command line arguments
* */
RtxGetopt producerOpts[] =
{
{"flagsname", "Store-Variable where the flags are",
{
{RTX_GETOPT_STR, &varname_flags, ""},
RTX_GETOPT_END_ARG
}
},
{"sailorflagsname", "Store-Variable where the sailor flags are",
{
{RTX_GETOPT_STR, &varname_sailorflags, ""},
RTX_GETOPT_END_ARG
}
},
{"ruddername", "Store Variable where the rudder-angle target is written",
{
{RTX_GETOPT_STR, &varname_ruddertarget, ""},
RTX_GETOPT_END_ARG
}
},
{"sailname", "Store Variable where the sail-angle target is written",
{
{RTX_GETOPT_STR, &varname_sailtarget , ""},
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
}
},
{"rudderstateleftname", "Store Variable where left rudder state is",
{
{RTX_GETOPT_STR, &varname_rudderstateleft, ""},
RTX_GETOPT_END_ARG
}
},
{"rudderstaterightname", "Store Variable where right rudder state is",
{
{RTX_GETOPT_STR, &varname_rudderstateright, ""},
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
};
double sailor_inverted_linear_model(double heading_speed, double torque_des,
double speed_x, double speed_y); // Prototype of the controllerfunction defined in sailor_inverted_linear_model.cpp
/**
* Working thread, wait for the data, transform them and write them again
* */
void * translation_thread(void * dummy)
{
rudderTarget rudder = {0.0, 0.0, 0, 0};
sailTarget sail = {0.0, 0};
Flags flags;
sailorFlags sailorflags;
Ship avalon;
Sailstate sailstate;
Rudderstate rudderstateleft, rudderstateright;
WindCleanData wind_clean;
imuData imu;
imuCleanData imu_clean;
DesiredHeading desired_heading;
double theta_dot_des; //desired theta_dot by first controller
double e; // the current error
RtxPid* mypid = NULL;
RtxPid* thetapid = NULL;
RtxParamStream* myparamstream = NULL;
RtxParamStream* paramstream_theta_dot = NULL;
int sign_wanted_sail_angle = 1; // 1 or -1 depending on port or starboard driving
int sign_wanted_rudder_angle = 1; // 1 or -1 depending on port or starboard driving before maneuver
int sign_wanted_sail_angle_after_tack;
int sign_wanted_sail_angle_after_change;
int last_state;
double sail_pre_jibe;
double heading_pre_jibe;
double wind_global_pre_tack;
double wind_global_pre_jibe;
double wind_global_pre_change;
double desired_heading_after_change;
double desired_bearing_after_change;
double wanted_sail_angle_after_change;
double calculated_sail_degrees;
double desired_heading_after_tack;
// double desired_heading_while_no_tack_or_jibe;
// int last_no_tack_or_jibe_value;
double torque_des;
double u; // the input that will be written to rudderangle
double speed;
int count=0;
while (1)
{
dataFlags.t_readto(flags,0,0);
dataSailorFlags.t_readto(sailorflags,0,0);
if (flags.man_in_charge != AV_FLAGS_MIC_SAILOR) // someone else is in charge...
{
rtx_timer_sleep(0.1);
continue; // don't do anything if someone else is in charge
}
// wait 10 sec for IMU data
if (dataImu.t_readto(imu,10.0,1))
{
dataWindClean.t_readto(wind_clean,0,0);
dataImuClean.t_readto(imu_clean,0,0);
dataSailState.t_readto(sailstate,0,0);
dataRudderStateLeft.t_readto(rudderstateleft,0,0);
dataRudderStateRight.t_readto(rudderstateright,0,0);
dataDesiredHeading.t_readto(desired_heading,0,0);
// if(flags.sailor_no_tack_or_jibe == 1)
// {
// if(last_no_tack_or_jibe_value == 0)
// {
// // set desired heading to current heading before beginning
// // the wait during no_tack_or_jibe
// desired_heading_while_no_tack_or_jibe = imu.attitude.yaw;
// }
// desired_heading.heading = desired_heading_while_no_tack_or_jibe;
// }
FILE * thetafile;
if(count < 5000)
{
thetafile = fopen("thetaplot.txt","a+");
}
if(count > 5000 && count < 10000)
{
thetafile = fopen("thetaplot2.txt","a+");
}
if(count == 10000)
{
count = 0;
thetafile = fopen("thetaplot.txt","w+");
}
if(count == 5000)
{
thetafile = fopen("thetaplot2.txt","w+");
}
switch(flags.state)
{
case AV_FLAGS_ST_IDLE:
rtx_timer_sleep(0.1);
last_state = AV_FLAGS_ST_IDLE;
continue; // don't do anything
break;
case AV_FLAGS_ST_DOCK:
//old case-version:
/* Sail: */
avalon.wanted_sail_angle_to_wind = 0;
sail.degrees = remainder((wind_clean.bearing_app
- sign_wanted_sail_angle * avalon.wanted_sail_angle_to_wind),360.0);
/* Rudder: */
rudder.degrees_left = -45.0;
rudder.degrees_right = 45.0;
last_state = AV_FLAGS_ST_DOCK;
break;
#if 0
//To test the PID-Parameters, this state only does rudder-control
/* Torque & Theta_dot initialization: */
if(last_state != flags.state) // initialize only when newly in this state
{
myparamstream = rtx_param_open("sailor_pid_torque.txt", 0, NULL); //NULL = errorfunction
mypid = rtx_pid_init(mypid, myparamstream, "torque", 0.01, 0); //0.01=dt
rtx_pid_integral_enable(mypid);
paramstream_theta_dot = rtx_param_open("sailor_pid_theta_dot.txt", 0, NULL); //NULL = errorfunction
thetapid = rtx_pid_init(thetapid, paramstream_theta_dot, "theta_dot", 0.01, 0); //0.01=dt
rtx_pid_integral_enable(thetapid);
}
/* evaluate rudder-torquei: */
theta_dot_des = rtx_pid_eval(thetapid, remainder(imu.attitude.yaw-desired_heading.heading,360.),0., 0);
torque_des = rtx_pid_eval(mypid, imu.gyro.z, theta_dot_des, 0); //with p_max = I(3)/delta_t
rudder.torque_des = torque_des;
/* calling the linear model: */
speed = 0.5144*sqrt((imu_clean.velocity.x*imu_clean.velocity.x) + (imu_clean.velocity.y*imu_clean.velocity.y));
u = sailor_inverted_linear_model(imu.gyro.z*M_PI/180.0, torque_des, imu_clean.velocity.x*0.5144, -imu_clean.velocity.y*0.5144);
fprintf(thetafile,"%d %f %f %f %f %f %f %d\n",count, desired_heading.heading, imu.attitude.yaw, theta_dot_des,imu.gyro.z,torque_des,rudder.degrees_left,flags.state);
rudder.degrees_left = u;
rudder.degrees_right = u;
last_state = AV_FLAGS_ST_DOCK;
rtx_message("theta_difference: %f, theta_dot_des: %f, torque_des: %f, rudder_degrees: %f, speed: %f \n ",
(remainder(desired_heading.heading - imu.attitude.yaw, 360.0)),theta_dot_des, torque_des, rudder.degrees_left,speed);
break;
#endif
case AV_FLAGS_ST_NORMALSAILING:
/* Sail: */
sign_wanted_sail_angle = sign(wind_clean.bearing_app); // +1 for wind from starboard, -1 for port
avalon.wanted_sail_angle_to_wind = AV_SAILOR_WANTED_AOA * get_sail_AOA_coeff(wind_clean.speed);
// The following ensures that the sail is never set to the wrong side
// of closer than AV_SAILOR_UPWIND_MIN_SAIL_DEGREES
calculated_sail_degrees = remainder((wind_clean.bearing_app
- sign_wanted_sail_angle * avalon.wanted_sail_angle_to_wind),360.0);
if(sign_wanted_sail_angle * calculated_sail_degrees > (AV_SAILOR_UPWIND_MIN_SAIL_DEGREES))
{
// slack the sail
sail.degrees = calculated_sail_degrees;
}
else
{
// set tight
sail.degrees = sign_wanted_sail_angle * AV_SAILOR_UPWIND_MIN_SAIL_DEGREES;
}
/* Torque & Theta_dot initialization: */
if(last_state != flags.state) // initialize only when newly in this state
{
myparamstream = rtx_param_open("sailor_pid_torque.txt", 0, NULL); //NULL = errorfunction
mypid = rtx_pid_init(mypid, myparamstream, "torque", 0.01, 0); //0.01=dt
rtx_pid_integral_enable(mypid);
paramstream_theta_dot = rtx_param_open("sailor_pid_theta_dot.txt", 0, NULL); //NULL = errorfunction
thetapid = rtx_pid_init(thetapid, paramstream_theta_dot, "theta_dot", 0.01, 0); //0.01=dt
rtx_pid_integral_enable(thetapid);
}
/* evaluate rudder-torquei: */
theta_dot_des = rtx_pid_eval(thetapid, remainder(imu.attitude.yaw-desired_heading.heading,360.),0., 0);
// decrease theta_dot_des if we are sailing slowly
if(imu_clean.velocity.x < 2.0)
{
theta_dot_des = 0.4 * theta_dot_des + 0.2;
}
torque_des = rtx_pid_eval(mypid, imu.gyro.z, theta_dot_des, 0); //with p_max = I(3)/delta_t
rudder.torque_des = torque_des;
/* calling the linear model: */
speed = 0.5144*sqrt((imu_clean.velocity.x*imu_clean.velocity.x) + (imu_clean.velocity.y*imu_clean.velocity.y));
u = sailor_inverted_linear_model(imu.gyro.z*M_PI/180.0, torque_des, imu_clean.velocity.x*0.5144, -imu_clean.velocity.y*0.5144);
fprintf(thetafile,"%d %f %f %f %f %f %f %d\n",count, desired_heading.heading, imu.attitude.yaw, theta_dot_des,imu.gyro.z,torque_des,rudder.degrees_left,flags.state);
/// compensate drift, at the moment not done since the drift assumption is to inaccurate!!
// 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.y, imu_clean.velocity.x)*180/AV_PI,360.0);
// }
// e = desired_heading.heading - imu.attitude.yaw;
// if(fabs(e) > 180)
// {
// imu.attitude.yaw = 0;
// desired_heading.heading = fabs(360.0 - fabs(e)) * sign(-e);
// }
// rtx_message("norm: des_head: %f actual_head: %f delta_head: %f",desired_heading.heading, imu.attitude.yaw,remainder(imu.attitude.yaw-desired_heading.heading,360.));
rudder.degrees_left = u;
rudder.degrees_right = u;
last_state = AV_FLAGS_ST_NORMALSAILING;
break;
case AV_FLAGS_ST_UPWINDSAILING:
/* Sail: */
sign_wanted_sail_angle = sign(wind_clean.bearing_app); // +1 for wind from starboard, -1 for port
avalon.wanted_sail_angle_to_wind = AV_SAILOR_WANTED_AOA * get_sail_AOA_coeff(wind_clean.speed);
// The following ensures that the sail is never set to the wrong side
// of closer than AV_SAILOR_UPWIND_MIN_SAIL_DEGREES
calculated_sail_degrees = remainder((sign_wanted_sail_angle * AV_SAILOR_UPWIND_MIN_SAIL_DEGREES
- sign_wanted_sail_angle * (avalon.wanted_sail_angle_to_wind - AV_SAILOR_WANTED_AOA)),360.0);
if(sign_wanted_sail_angle * calculated_sail_degrees > AV_SAILOR_UPWIND_MIN_SAIL_DEGREES)
{
// slack the sail
sail.degrees = calculated_sail_degrees;
}
else
{
// set tight
sail.degrees = sign_wanted_sail_angle * AV_SAILOR_UPWIND_MIN_SAIL_DEGREES;
}
/* Torque & Theta_dot initialization: */
if(last_state != flags.state) // initialize only when newly in this state
{
myparamstream = rtx_param_open("sailor_pid_torque.txt", 0, NULL); //NULL = errorfunction
mypid = rtx_pid_init(mypid, myparamstream, "torque", 0.01, 0); //0.01=dt
rtx_pid_integral_enable(mypid);
paramstream_theta_dot = rtx_param_open("sailor_pid_theta_dot.txt", 0, NULL); //NULL = errorfunction
thetapid = rtx_pid_init(thetapid, paramstream_theta_dot, "theta_dot", 0.01, 0); //0.01=dt
rtx_pid_integral_enable(thetapid);
}
// redefine desired heading to stay "close to the wind":
desired_heading.heading = remainder(wind_clean.global_direction_real - sign_wanted_sail_angle * AV_SAILOR_MAX_HEIGHT_TO_WIND, 360.0);
/* evaluate rudder-torquei: */
theta_dot_des = rtx_pid_eval(thetapid, remainder(imu.attitude.yaw-desired_heading.heading,360.),0., 0);
torque_des = rtx_pid_eval(mypid, imu.gyro.z, theta_dot_des, 0); //with p_max = I(3)/delta_t
rudder.torque_des = torque_des;
/* calling the linear model: */
speed = 0.5144*sqrt((imu_clean.velocity.x*imu_clean.velocity.x) + (imu_clean.velocity.y*imu_clean.velocity.y));
u = sailor_inverted_linear_model(imu.gyro.z*M_PI/180.0, torque_des, imu_clean.velocity.x*0.5144, -imu_clean.velocity.y*0.5144);
fprintf(thetafile,"%d %f %f %f %f %f %f %d\n",count, desired_heading.heading, imu.attitude.yaw, theta_dot_des,imu.gyro.z,torque_des,rudder.degrees_left,flags.state);
// e = desired_heading.heading - imu.attitude.yaw;
// if(fabs(e) > 180) //TODO: check: why not remainder???
// {
// imu.attitude.yaw = 0;
// desired_heading.heading = fabs(360.0 - fabs(e)) * sign(-e);
// }
// rtx_message("down: des_head: %f actual_head: %f delta_head: %f",desired_heading.heading, imu.attitude.yaw,remainder(imu.attitude.yaw-desired_heading.heading,360.));
rudder.degrees_left = u;
rudder.degrees_right = u;
last_state = AV_FLAGS_ST_UPWINDSAILING;
break;
case AV_FLAGS_ST_DOWNWINDSAILING:
/* Sail: */
sign_wanted_sail_angle = sign(wind_clean.bearing_app); // +1 for wind from starboard, -1 for port
avalon.wanted_sail_angle_to_wind = AV_SAILOR_WANTED_AOA * get_sail_AOA_coeff(wind_clean.speed);
calculated_sail_degrees = remainder((wind_clean.bearing_app
- sign_wanted_sail_angle * avalon.wanted_sail_angle_to_wind),360.0);
if(sign_wanted_sail_angle * calculated_sail_degrees > 0) // if still on "correct" side
{
// slack the sail
sail.degrees = calculated_sail_degrees;
}
else
{
// set to 179 deg
sail.degrees = sign_wanted_sail_angle * 179.0;
}
sail.degrees = sign_wanted_sail_angle * AV_SAILOR_DOWNWIND_SAIL_DEGREES;
/* Torque & Theta_dot initialization: */
if(last_state != flags.state) // initialize only when newly in this state
{
myparamstream = rtx_param_open("sailor_pid_torque.txt", 0, NULL); //NULL = errorfunction
mypid = rtx_pid_init(mypid, myparamstream, "torque", 0.01, 0); //0.01=dt
rtx_pid_integral_enable(mypid);
paramstream_theta_dot = rtx_param_open("sailor_pid_theta_dot.txt", 0, NULL); //NULL = errorfunction
thetapid = rtx_pid_init(thetapid, paramstream_theta_dot, "theta_dot", 0.01, 0); //0.01=dt
rtx_pid_integral_enable(thetapid);
}
// redefine desired heading to stay fixed to the wind:
desired_heading.heading = remainder(wind_clean.global_direction_real - sign_wanted_sail_angle * AV_SAILOR_MAX_DOWNWIND_ANGLE, 360.0);
/* evaluate rudder-torquei: */
theta_dot_des = rtx_pid_eval(thetapid, remainder(imu.attitude.yaw-desired_heading.heading,360.),0., 0);
torque_des = rtx_pid_eval(mypid, imu.gyro.z, theta_dot_des, 0); //with p_max = I(3)/delta_t
rudder.torque_des = torque_des;
/* calling the linear model: */
speed = 0.5144*sqrt((imu_clean.velocity.x*imu_clean.velocity.x) + (imu_clean.velocity.y*imu_clean.velocity.y));
u = sailor_inverted_linear_model(imu.gyro.z*M_PI/180.0, torque_des, imu_clean.velocity.x*0.5144, -imu_clean.velocity.y*0.5144);
fprintf(thetafile,"%d %f %f %f %f %f %f %d\n",count, desired_heading.heading, imu.attitude.yaw, theta_dot_des,imu.gyro.z,torque_des,rudder.degrees_left,flags.state);
// e = desired_heading.heading - imu.attitude.yaw;
// if(fabs(e) > 180) // take care of +-180 thing
// {
// imu.attitude.yaw = 0;
// desired_heading.heading = fabs(360.0 - fabs(e)) * sign(-e);
// }
// rtx_message("down: des_head: %f actual_head: %f delta_head: %f",desired_heading.heading, imu.attitude.yaw,remainder(imu.attitude.yaw-desired_heading.heading,360.));
rudder.degrees_left = u;
rudder.degrees_right = u;
last_state = AV_FLAGS_ST_DOWNWINDSAILING;
break;
case AV_FLAGS_ST_TACK:
if(last_state != flags.state) // only when newly in this state, otherwise rudder angle changes in the middle of the tack
{
sign_wanted_rudder_angle = -sign(wind_clean.bearing_app); // +1 for wind from starboard, -1 for port
wind_global_pre_tack = wind_clean.global_direction_real;
myparamstream = rtx_param_open("sailor_pid_torque.txt", 0, NULL); //NULL = errorfunction
mypid = rtx_pid_init(mypid, myparamstream, "torque", 0.01, 0); //0.01=dt
desired_heading_after_tack = remainder(wind_clean.global_direction_real - 45.0 *
sign(remainder(imu.attitude.yaw - wind_clean.global_direction_real,360.0)),360.0);
sign_wanted_sail_angle_after_tack = -sign(wind_clean.bearing_app);
}
/* Torque & Theta_dot initialization: */
if(last_state != flags.state) // initialize only when newly in this state
{
myparamstream = rtx_param_open("sailor_pid_torque.txt", 0, NULL); //NULL = errorfunction
mypid = rtx_pid_init(mypid, myparamstream, "torque", 0.01, 0); //0.01=dt
rtx_pid_integral_enable(mypid);
paramstream_theta_dot = rtx_param_open("sailor_pid_theta_dot.txt", 0, NULL); //NULL = errorfunction
thetapid = rtx_pid_init(thetapid, paramstream_theta_dot, "theta_dot", 0.01, 0); //0.01=dt
rtx_pid_integral_enable(thetapid);
}
/* Sail: */
// if heading is still on 'wrong' side:
if(remainder((wind_global_pre_tack - imu.attitude.yaw),360.0) * sign_wanted_sail_angle_after_tack < 0)
{
sail.degrees = AV_SAILOR_UPWIND_MIN_SAIL_DEGREES / AV_SAILOR_MAX_HEIGHT_TO_WIND * remainder((wind_global_pre_tack - imu.attitude.yaw),360.0);
}
else // sail on correct (for after tack) side
{
sail.degrees = sign_wanted_sail_angle_after_tack * AV_SAILOR_UPWIND_MIN_SAIL_DEGREES;
}
/* Rudder: */
/* evaluate rudder-torquei: */
theta_dot_des = rtx_pid_eval(thetapid, remainder(imu.attitude.yaw-desired_heading_after_tack,360.),0., 0);
torque_des = rtx_pid_eval(mypid, imu.gyro.z, theta_dot_des, 0); //with p_max = I(3)/delta_t
rudder.torque_des = torque_des;
/* calling the linear model: */
speed = 0.5144*sqrt((imu_clean.velocity.x*imu_clean.velocity.x) + (imu_clean.velocity.y*imu_clean.velocity.y));
u = sailor_inverted_linear_model(imu.gyro.z*M_PI/180.0, torque_des, imu_clean.velocity.x*0.5144, -imu_clean.velocity.y*0.5144);
fprintf(thetafile,"%d %f %f %f %f %f %f %d\n",count, desired_heading.heading, imu.attitude.yaw, theta_dot_des,imu.gyro.z,torque_des,rudder.degrees_left,flags.state);
rudder.degrees_left = u;
rudder.degrees_right = u;
last_state = AV_FLAGS_ST_TACK;
break;
case AV_FLAGS_ST_JIBE:
if(last_state != flags.state) // only when newly in this state
{
sail_pre_jibe = sailstate.degrees_sail;
heading_pre_jibe = imu.attitude.yaw;
wind_global_pre_jibe = wind_clean.global_direction_real;
// desired_heading_after_jibe = fabs(remainder((wind_clean.global_direction_real
// + 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(wind_clean.bearing_real); // +1 for wind from starboard, -1 for port
}
/* Sail: */
if(sailstate.degrees_sail * sign_wanted_sail_angle <= 0 || fabs(sailstate.degrees_sail) > 170.0)
// sail already on "correct" side or turned out to the front
{
sail.degrees = -sign_wanted_sail_angle * AV_SAILOR_DOWNWIND_SAIL_DEGREES;
}
else //sail still on "wrong/old" side
{
sail.degrees = 180.0; // set to front
}
/* Torque & Theta_dot initialization: */
if(last_state != flags.state) // initialize only when newly in this state
{
myparamstream = rtx_param_open("sailor_pid_torque.txt", 0, NULL); //NULL = errorfunction
mypid = rtx_pid_init(mypid, myparamstream, "torque", 0.01, 0); //0.01=dt
rtx_pid_integral_enable(mypid);
paramstream_theta_dot = rtx_param_open("sailor_pid_theta_dot.txt", 0, NULL); //NULL = errorfunction
thetapid = rtx_pid_init(thetapid, paramstream_theta_dot, "theta_dot", 0.01, 0); //0.01=dt
rtx_pid_integral_enable(thetapid);
}
if(sailstate.degrees_sail * sign_wanted_sail_angle <= 0) // Sail already on correct side
{
// desiredheading in function of sailstate.degrees_sail...
desired_heading.heading = remainder(remainder((wind_global_pre_jibe + 180.0),360.0)
- sign_wanted_sail_angle * ((180-AV_SAILOR_MAX_DOWNWIND_ANGLE)
* fabs(fabs(sailstate.degrees_sail) - 180.0) / fabs(AV_SAILOR_DOWNWIND_SAIL_DEGREES - 180.0)),360.0);
}
else // sail still on wrong side
{
desired_heading.heading = remainder(remainder((wind_global_pre_jibe + 180.0),360.0)
- (remainder((remainder(wind_global_pre_jibe + 180,360.0) - heading_pre_jibe),360.0)
* (fabs(sailstate.degrees_sail) - 180.0) / (fabs(sail_pre_jibe) - 180.0)),360.0);
}
// e = desired_heading.heading - imu.attitude.yaw;
// if(fabs(e) > 180) // +-180 thing...
// {
// imu.attitude.yaw = 0;
// desired_heading.heading = fabs(360.0 - fabs(e)) * sign(-e);
//
// }
/* evaluate rudder-torquei: */
theta_dot_des = rtx_pid_eval(thetapid, remainder(imu.attitude.yaw-desired_heading.heading,360.),0., 0);
torque_des = rtx_pid_eval(mypid, imu.gyro.z, theta_dot_des, 0); //with p_max = I(3)/delta_t
rudder.torque_des = torque_des;
/* calling the linear model: */
speed = 0.5144*sqrt((imu_clean.velocity.x*imu_clean.velocity.x) + (imu_clean.velocity.y*imu_clean.velocity.y));
u = sailor_inverted_linear_model(imu.gyro.z*M_PI/180.0, torque_des, imu_clean.velocity.x*0.5144, -imu_clean.velocity.y*0.5144);
fprintf(thetafile,"%d %f %f %f %f %f %f %d\n",count, desired_heading.heading, imu.attitude.yaw, theta_dot_des,imu.gyro.z,torque_des,rudder.degrees_left,flags.state);
rudder.degrees_left = u;
rudder.degrees_right = u;
last_state = AV_FLAGS_ST_JIBE;
break;
case AV_FLAGS_ST_MAXENERGYSAVING:
/* Sail: */
sail.degrees = sign(sailstate.degrees_sail) * 179.0;
/* Rudder: */
rudder.degrees_left = -45.0;
rudder.degrees_right = 45.0;
last_state = AV_FLAGS_ST_MAXENERGYSAVING;
break;
case AV_FLAGS_ST_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);
}
/* Sail: */
switch(flags.sail_direction)
{
case AV_FLAGS_SAIL_DIR_NOPREFERENCE:
sail.degrees = wanted_sail_angle_after_change;
break;
case AV_FLAGS_SAIL_DIR_ZERO:
sail.degrees = 0.0;
break;
case AV_FLAGS_SAIL_DIR_FRONT:
sail.degrees = 179.0;
break;
}
/* Rudder: */
if(last_state != flags.state) // initialize only when newly in this state
{
// TODO maybe use different parameters here (I=0)
myparamstream = rtx_param_open("sailor_pid_torque.txt", 0, NULL); //NULL = errorfunction
mypid = rtx_pid_init(mypid, myparamstream, "torque", 0.01, 0); //0.01=dt
rtx_pid_integral_enable(mypid);
}
/* Theta_dot: */
if(last_state != flags.state) // initialize only when newly in this state
{
paramstream_theta_dot = rtx_param_open("sailor_pid_theta_dot.txt", 0, NULL); //NULL = errorfunction
thetapid = rtx_pid_init(thetapid, paramstream_theta_dot, "theta_dot", 0.01, 0); //0.01=dt
rtx_pid_integral_enable(thetapid);
}
desired_heading.heading = remainder((wind_clean.global_direction_real
- avalon.wanted_sail_angle_to_wind * sign(sailstate.degrees_sail)
- sailstate.degrees_sail),360.0);
e = desired_heading.heading - imu.attitude.yaw;
if(fabs(e) > 180) // +-180 thing...
{
imu.attitude.yaw = 0;
desired_heading.heading = fabs(360.0 - fabs(e)) * sign(-e);
}
theta_dot_des = rtx_pid_eval(thetapid, remainder(imu.attitude.yaw-desired_heading.heading,360.),0., 0) ;
torque_des = rtx_pid_eval(mypid, imu.gyro.z, theta_dot_des, 0); //with p_max = I(3)/delta_t
rudder.torque_des = torque_des;
last_state = AV_FLAGS_ST_HEADINGCHANGE;
break;
default:
rtx_message("Sailor state machine is in some illegal state. Behaving as in IDLE --> stopping motion");
sail.degrees = sailstate.degrees_sail;
last_state = 0;
break;
}
fclose(thetafile);
// Bring to store
count++;
dataRudder.t_writefrom(rudder);
dataSail.t_writefrom(sail);
// Do not write to sailorflags in here! Sailor_transitions does that.
// Take care of timeouts:
} else if (dataWindClean.hasTimedOut())
{
rtx_message("Timeout while reading windClean data.\n");
} else if (dataSailState.hasTimedOut())
{
rtx_message("Timeout while reading Sailstate data.\n");
} else if (dataRudderStateLeft.hasTimedOut())
{
rtx_message("Timeout while reading left Rudderstate data.\n");
} else if (dataRudderStateRight.hasTimedOut())
{
rtx_message("Timeout while reading right Rudderstate data.\n");
} else if (dataImuClean.hasTimedOut())
{
rtx_message("Timeout while reading ImuClean data.\n");
}
//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 Statemachine 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(), rudderTarget));
DOC(DDX_STORE_REGISTER_TYPE (store.getId(), sailTarget));
DOC(DDX_STORE_REGISTER_TYPE (store.getId(), WindCleanData));
DOC(DDX_STORE_REGISTER_TYPE (store.getId(), Sailstate));
DOC(DDX_STORE_REGISTER_TYPE (store.getId(), Rudderstate));
DOC(DDX_STORE_REGISTER_TYPE (store.getId(), DesiredHeading));
DOC(DDX_STORE_REGISTER_TYPE (store.getId(), imuData));
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(dataRudder, varname_ruddertarget, "rudderTarget"));
DOB(store.registerVariable(dataSail, varname_sailtarget, "sailTarget"));
DOB(store.registerVariable(dataWindClean, varname_windDataClean, "WindCleanData"));
DOB(store.registerVariable(dataSailState, varname_sailstate, "Sailstate"));
DOB(store.registerVariable(dataRudderStateLeft, varname_rudderstateleft, "Rudderstate"));
DOB(store.registerVariable(dataRudderStateRight, varname_rudderstateright, "Rudderstate"));
DOB(store.registerVariable(dataDesiredHeading, varname_desiredheading, "DesiredHeading"));
DOB(store.registerVariable(dataImu, varname_imu, "imuData"));
DOB(store.registerVariable(dataImuClean, varname_imuClean, "imuCleanData"));
// Start the working thread
DOP(th = rtx_thread_create ("Sailor Statemachine 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_statemachine Routine...");
// Terminating the thread
rtx_thread_destroy_sync (th);
// The destructors will take care of cleaning up the memory
return (0);
}