-
Notifications
You must be signed in to change notification settings - Fork 0
/
verification1-avoid.xml
1263 lines (1169 loc) · 41.1 KB
/
verification1-avoid.xml
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
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE nta PUBLIC '-//Uppaal Team//DTD Flat System 1.1//EN' 'http://www.it.uu.se/research/group/darts/uppaal/flat-1_2.dtd'>
<nta>
<declaration>// Place global declarations here.
typedef struct
{
int w;
int heading;
}RotationalState;
typedef struct
{
int acc;
int speed;
}LinearState;
typedef struct
{
int x;
int y;
}Point;
const int PM = 1; //period of movement
const int PC = 2; //period of control
const int PS = 3; //period of sensing
const int PI = 3142;
const int ANGLESCALE = 1000; //1 radian=1000 miliradian
const int POSITIONSCALE = 10; //1m = 10*10cm
const int AVVECTOR = 1;
const int POSITIONID = 0;
const int AVSCALAR = 4;
const int WID = 0;
const int HEADID = 1;
const int ACCID = 2;
const int SPEEDID = 3;
const int CONSTRATE = -1;
const int SPACEDIMENSION = 2;
const int X = 0;
const int Y = 1;
const int NOTDIMENSIONAL = -1;
//Max number of moving obstacles
const int NUMOBS = 2;
const int ACTUALOBS = 1;
const int NUMAV = 1;
//const int INITOBS[NUMOBS][AVDIMENSION] = {{0,0,0,0,0}};
//MAX: time unit - 500ms = 0.5s
const int MAXSCALAR[AVSCALAR] = {393, //angular speed
PI, //heading
7, //linear acceleration
8}; //linear speed
const int MAXVECTOR[AVVECTOR][SPACEDIMENSION] = {{100*POSITIONSCALE}, {100*POSITIONSCALE}}; //position: 3000(10cm) * 10 = 3000m
//MAX OBS: time unit - 500ms = 0.5s
const int MAXSCALAROBS[NUMOBS][AVSCALAR] = {{393,
PI,
7,
5},
{393,
PI,
7,
5}};
const int MAXVECTOROBS[NUMOBS][AVVECTOR][SPACEDIMENSION] = {{{100*POSITIONSCALE}, {100*POSITIONSCALE}},{{100*POSITIONSCALE}, {100*POSITIONSCALE}}}; //position: 3000(10cm) * 10 = 3000m
//MIN: time unit - 500ms = 0.5s
const int MINSCALAR[AVSCALAR] = {-393,
-PI,
-10,
0};
const int MINVECTOR[AVVECTOR][SPACEDIMENSION] = {{0},{0}};
//MIN: time unit - 500ms = 0.5s
const int MINSCALAROBS[NUMOBS][AVSCALAR] = {{-393,
-PI,
-10,
5},
{-393,
-PI,
-10,
5}};
const int MINVECTOROBS[NUMOBS][AVVECTOR][SPACEDIMENSION] = {{{0},{0}},{{0},{0}}}; //position:0
//const int AVNORMALSPEED = MAXSCALAR[SPEEDID]/2;
//INIT
const int INITSCALAR[AVSCALAR] = {0,PI/4,MAXSCALAR[ACCID],MAXSCALAR[SPEEDID]};
const int INITVECTOR[AVVECTOR][SPACEDIMENSION] = {{0},{0}};
const int VIEWRADIUS = MAXSCALAROBS[0][SPEEDID]*PS*2;
const int FARRADIUS = MAXSCALAROBS[0][SPEEDID]*PS*3;
//width of the safe region
int widthSafeRegion = 5;
const int NUMSTATICOBSS = 1;
const int NUMSTATICOBSVERDICES = 4;
const Point staticObsVerdices[NUMSTATICOBSVERDICES] = {{2,2},{2,5},{9,5},{9,2}};
const int NUMWAYPOINTS = 2;
const int LENGTHPATH = NUMWAYPOINTS + 1;
const int WAYPOINTS[NUMWAYPOINTS][SPACEDIMENSION]={{5*POSITIONSCALE,5*POSITIONSCALE},{10*POSITIONSCALE,15*POSITIONSCALE}};
const Point avpath[LENGTHPATH] = {{INITVECTOR[POSITIONID][X],INITVECTOR[POSITIONID][Y]},{WAYPOINTS[0][X],WAYPOINTS[0][Y]},{WAYPOINTS[1][X],WAYPOINTS[1][Y]}};
const int UNKNOWN = 0;
const int MOVING = 1; //move straightly
const int TURNING = 2; //turn direction
const int ARRIVING = 3; //arriving
const int AVOIDING = 4;
const int VINIT = 1;
const int GINIT = 2;
const int CRITICALLENGTH = 2;
const int STEPPOS = 50;
//const int STEPACC = 5;
const int STEPSPEED = 10;
const int STEPW = 393;
const int STEPHEAD = 393;
const int PTS = 5;
const int PTE = 20;
import "FM2021/lib/lib.so" {
int getProjection(int value, int angle, int axis);
int getAngle(int sX, int sY, int eX, int eY, int scale);
int getDisPointToLine(int px, int py, int sx, int sy, int ex, int ey);
int getDisPointToPoint(int x1, int y1, int x2, int y2);
int getValidRange(int length, int angle, int scale);
void collisionAvoidance(int obsNum, int &appeared[ACTUALOBS], int staticObsNum, Point &staticObsVerdices[NUMSTATICOBSVERDICES], int &probe[1], Point &obsPosition[ACTUALOBS], RotationalState &rotationalObsStates[ACTUALOBS], LinearState &linearObsStates[ACTUALOBS],
Point &avPosition[NUMAV], RotationalState &rotationalAVStates[NUMAV], LinearState &linearAVStates[NUMAV], Point &goalPosition[NUMAV],
Point &path[2], int pathLen, RotationalState &rotationalGoalStates[NUMAV], LinearState &linearGoalStates[NUMAV]);
};
//int obsNum = 0;
int waypointsIndex = 0;
//GOAL
int goalScalar[AVSCALAR] = {MAXSCALAR[WID],PI/4,MAXSCALAR[ACCID],MAXSCALAR[SPEEDID]};
int goalVector[AVVECTOR][SPACEDIMENSION] = {{MAXVECTOR[POSITIONID][X]},{MAXVECTOR[POSITIONID][Y]}};
//int goalProjection[AVDIMENSION][SPACEDIMENSION];
//variables of the autonomous vehicle
int avScalarVariables[AVSCALAR] = {INITSCALAR[WID],INITSCALAR[HEADID],INITSCALAR[ACCID],INITSCALAR[SPEEDID]};
int avVectorVariables[AVVECTOR][SPACEDIMENSION] = {{INITVECTOR[POSITIONID][X]},{INITVECTOR[POSITIONID][Y]}};
//int criticalIndex = 0;
//int criticalSegment[CRITICALLENGTH][SPACEDIMENSION] = {{0,0},{0,0}};
//bool avStop = false;
bool collided = false;
int appeared[ACTUALOBS] = {0};
//variables of the moving obstacles
int obsScalarVariables[ACTUALOBS][AVSCALAR];
int obsVectorVariables[ACTUALOBS][AVVECTOR][SPACEDIMENSION];
int minOut[SPACEDIMENSION], minIn[SPACEDIMENSION], maxOut[SPACEDIMENSION], maxIn[SPACEDIMENSION];
//channels
chan obsLeft;
broadcast chan start, stop, invoke, proceed, stay, change, obsGo[ACTUALOBS];
clock time;
int distance;
bool isCollide(int obsID)
{
distance = MAXVECTOR[POSITIONID][X];
if(appeared[obsID])
{
distance = getDisPointToPoint(obsVectorVariables[obsID][POSITIONID][X], obsVectorVariables[obsID][POSITIONID][Y],
avVectorVariables[POSITIONID][X], avVectorVariables[POSITIONID][Y]);
/*distance = getDisPointToLine(obsVectorVariables[obsID][POSITIONID][X], obsVectorVariables[obsID][POSITIONID][Y],
criticalSegment[0][X],criticalSegment[0][Y],criticalSegment[1][X],criticalSegment[1][Y]);*/
if(distance < widthSafeRegion)
{
return true;
}
}
else
{
return false;
}
return false;
}
bool hitWall()
{
int i = 0;
bool hit = false;
for(i = 0; i < SPACEDIMENSION; i++)
{
hit = (avVectorVariables[POSITIONID][i]==MAXVECTOR[POSITIONID][i]) || (avVectorVariables[POSITIONID][i]==MINVECTOR[POSITIONID][i]);
if(hit)return hit;
}
return hit;
}
</declaration>
<template>
<name x="5" y="5">AVVariable</name>
<parameter>const int VID, const int RATEID, const int SPACEID</parameter>
<declaration>void update()
{
int rateValue = 0, overshoot = 0, direction = 0;
int currentAngle = avScalarVariables[HEADID];
//rateValue = 0;overshoot = 0;direction = 0;
//if the variable does not change at a constant rate: speed, position, heading
//its rate refers to the corresponding variable
if(RATEID != CONSTRATE)
{
rateValue = avScalarVariables[RATEID];
}
//if the variable changes at a constant rate: acceleration, angular speed
//its rate is the difference between its current value and its goal
//which means it goes to its goal value in one time unit
else
{
rateValue = goalScalar[VID] - avScalarVariables[VID];
}
//if the rate is not zero, we calculate the variable's changing direction: +1/-1
if(rateValue != 0)
{
direction = rateValue/abs(rateValue);
}
//if the variable is dimensional: position
//its changing rate is projected to the corresponding axies
if(SPACEID != NOTDIMENSIONAL)
{
rateValue = getProjection(rateValue, currentAngle, SPACEID);
avVectorVariables[VID][SPACEID] += rateValue;
if(avVectorVariables[VID][SPACEID] > MAXVECTOR[VID][SPACEID])
{
avVectorVariables[VID][SPACEID] = MAXVECTOR[VID][SPACEID];
}
else if(avVectorVariables[VID][SPACEID] < MINVECTOR[VID][SPACEID])
{
avVectorVariables[VID][SPACEID] = MINVECTOR[VID][SPACEID];
}
//criticalSegment[criticalIndex][SPACEID] = avVectorVariables[VID][SPACEID];
/*overshoot = direction * (goalVector[VID][SPACEID] - avVectorVariables[VID][SPACEID]);
if(overshoot < 0)
{
avVectorVariables[VID][SPACEID] = goalVector[VID][SPACEID];
}*/
}
else
{
avScalarVariables[VID] += rateValue;
overshoot = direction * (goalScalar[VID] - avScalarVariables[VID]);
if(overshoot < 0)
{
avScalarVariables[VID] = goalScalar[VID];
}
if(RATEID != CONSTRATE && goalScalar[VID] == avScalarVariables[VID])
{
rateValue = avScalarVariables[RATEID];
avScalarVariables[RATEID] = 0;
goalScalar[RATEID] = 0;
}
}
//the variable overshoots its goal value,
//which means a negative value of goal minus current value times the direction means an overshooting,
// e.g., when the goal value is negative: -5, and the current value is -4 or -6:
//e.g., -5 - (-4) = -1, (-1)*(-1)=1 (no overshoot);
//but, -5 - (-6) = +1, 1*(-1) = -1 (overshoot)
//or when the goal value is positive: 5, and the current value is 4 or 6:
//e.g., 5 - 4 = 1, 1*1=1(no overshoot),
//but, 5 - 6 = -1, (-1)*1=-1(overshoot)
}
bool reach()
{
bool result = false;
if(SPACEID != NOTDIMENSIONAL)
{
//result = (avVectorVariables[VID][SPACEID] == goalVector[VID][SPACEID]);
}
else
{
result = (avScalarVariables[VID] == goalScalar[VID]);
}
return result;
}
</declaration>
<location id="id0" x="-127" y="-68">
<name x="-203" y="-85">MOVING</name>
</location>
<location id="id1" x="51" y="-161">
<name x="-8" y="-195">CONSTANT</name>
</location>
<location id="id2" x="-127" y="-161">
<name x="-178" y="-195">CHANGING</name>
</location>
<location id="id3" x="-280" y="-161">
<name x="-272" y="-195">WAIT</name>
</location>
<init ref="id3"/>
<transition>
<source ref="id1"/>
<target ref="id2"/>
<label kind="synchronisation" x="-68" y="-178">change?</label>
</transition>
<transition>
<source ref="id0"/>
<target ref="id1"/>
<label kind="guard" x="-17" y="-102">reach()</label>
<label kind="synchronisation" x="-17" y="-85">stay?</label>
<nail x="51" y="-68"/>
</transition>
<transition>
<source ref="id0"/>
<target ref="id2"/>
<label kind="guard" x="-68" y="-136">!reach()</label>
<label kind="synchronisation" x="-68" y="-119">proceed?</label>
<nail x="-50" y="-93"/>
<nail x="-50" y="-135"/>
</transition>
<transition>
<source ref="id2"/>
<target ref="id0"/>
<label kind="synchronisation" x="-186" y="-135">invoke?</label>
<label kind="assignment" x="-195" y="-119">update()</label>
</transition>
<transition>
<source ref="id3"/>
<target ref="id2"/>
<label kind="synchronisation" x="-229" y="-178">start?</label>
</transition>
</template>
<template>
<name>AVController</name>
<declaration>clock timer;
int[0,4] status = UNKNOWN;
int[0,4] oldStatus = UNKNOWN;
void initialize()
{
int i = 0;
for(i = 0; i < SPACEDIMENSION; i++)
{
goalVector[POSITIONID][i] = WAYPOINTS[waypointsIndex][i];
}
}
int modified = 0;
int modify(Point obsPosition[ACTUALOBS], Point avPosition, int newHeading_v, int originalHeading)
{
bool shouldModify = true;
int oldDistance = 0, newDistance = 0, obsNewHeading = 0, newHeading_l = 0;
Point originalNewPosition, newPosition, obsNewPosition;
int i = 0;
modified = 0;
newHeading_l = newHeading_v;
newPosition.x = avPosition.x + getProjection(avScalarVariables[SPEEDID], newHeading_v, X);
newPosition.y = avPosition.y + getProjection(avScalarVariables[SPEEDID], newHeading_v, Y);
originalNewPosition.x = avPosition.x + getProjection(avScalarVariables[SPEEDID], originalHeading, X);
originalNewPosition.y = avPosition.y + getProjection(avScalarVariables[SPEEDID], originalHeading, Y);
for(i = 0; i< ACTUALOBS; i++)
{
if(appeared[i] == 1)
{
obsNewHeading = obsScalarVariables[i][HEADID] + obsScalarVariables[i][WID];
obsNewPosition.x = obsPosition[i].x + getProjection(obsScalarVariables[i][SPEEDID], obsNewHeading, X);
obsNewPosition.y = obsPosition[i].y + getProjection(obsScalarVariables[i][SPEEDID], obsNewHeading, Y);
oldDistance = getDisPointToPoint(originalNewPosition.x, originalNewPosition.y, obsNewPosition.x, obsNewPosition.y);
newDistance = getDisPointToPoint(newPosition.x, newPosition.y, obsNewPosition.x, obsNewPosition.y);
if (newDistance < oldDistance)
{
shouldModify &= true;
}
else
{
shouldModify &= false;
}
}
}
if(shouldModify)
{
modified = 1;
//new position of AV
newHeading_l = avScalarVariables[HEADID];
newPosition.x = avPosition.x + getProjection(avScalarVariables[SPEEDID], newHeading_l, X);
newPosition.y = avPosition.y + getProjection(avScalarVariables[SPEEDID], newHeading_l, Y);
//new position of obs[0]
obsNewHeading = obsScalarVariables[0][HEADID] + obsScalarVariables[0][WID];
obsNewPosition.x = obsPosition[0].x + getProjection(obsScalarVariables[0][SPEEDID], obsNewHeading, X);
obsNewPosition.y = obsPosition[0].y + getProjection(obsScalarVariables[0][SPEEDID], obsNewHeading, Y);
//distances
oldDistance = getDisPointToPoint(avPosition.x, avPosition.y, obsNewPosition.x, obsNewPosition.y);
newDistance = getDisPointToPoint(newPosition.x, newPosition.y, obsNewPosition.x, obsNewPosition.y);
while (newDistance < oldDistance)
{
modified = 2;
newHeading_l = newHeading_l - PI / 4;
if (newHeading_l < -PI)
{
newHeading_l = 2 * PI + newHeading_l;
}
newPosition.x = avPosition.x + getProjection(avScalarVariables[SPEEDID], newHeading_l, X);
newPosition.y = avPosition.y + getProjection(avScalarVariables[SPEEDID], newHeading_l, Y);
newDistance = getDisPointToPoint(newPosition.x, newPosition.y, obsNewPosition.x, obsNewPosition.y);
if(newHeading_l == avScalarVariables[HEADID])
{
newDistance = oldDistance + 1;//for break out
}
}
}
return newHeading_l;
}
int deviate(int newDir)
{
bool deviated = false;
int i = 0, absObs = 0, absAV = 0, dirObs, dirAV, result = newDir;
deviated = false;
for(i = 0; i < ACTUALOBS; i++)
{
if(appeared[i] == 1)
{
absAV = abs(avScalarVariables[HEADID]);
absObs = abs(obsScalarVariables[i][HEADID]);
if(absAV!=0 && absObs!=0)
{
dirAV = avScalarVariables[HEADID]/absAV;
dirObs = obsScalarVariables[i][HEADID]/absObs;
if((absAV + absObs == PI) && (dirAV * dirObs == -1))
{
deviated = true;
result = avScalarVariables[HEADID] - PI / 4;
if (result < -PI)
{
result = 2 * PI + result;
}
}
}
}
}
return result;
}
void avoid()
{
int probe[1];
int newHeading = 0, angleToGoal = 0;
int originalGoalHeading = 0;
RotationalState rotationalGoalStates[NUMAV];
int i = 0;
Point staticObsVerdices_l[NUMSTATICOBSVERDICES] = staticObsVerdices;
Point localPath[2];
Point obsPosition[ACTUALOBS];
RotationalState rotationalObsStates[ACTUALOBS];
LinearState linearObsStates[ACTUALOBS];
Point avPosition[NUMAV];
RotationalState rotationalAVStates[NUMAV];
LinearState linearAVStates[NUMAV];
Point goalPositions[NUMAV];
LinearState linearGoalStates[NUMAV];
localPath[0] = avpath[waypointsIndex];
localPath[1] = avpath[waypointsIndex+1];
probe[0] = 0;
for(i = 0; i < ACTUALOBS; i++)
{
obsPosition[i].x = obsVectorVariables[i][POSITIONID][X];
obsPosition[i].y = obsVectorVariables[i][POSITIONID][Y];
rotationalObsStates[i].w = obsScalarVariables[i][WID];
rotationalObsStates[i].heading = obsScalarVariables[i][HEADID];
linearObsStates[i].acc = obsScalarVariables[i][ACCID];
linearObsStates[i].speed = obsScalarVariables[i][SPEEDID];
//obsStates[i].acc = 0;
//obsStates[i].speed = 0;
}
avPosition[0].x = avVectorVariables[POSITIONID][X];
avPosition[0].y = avVectorVariables[POSITIONID][Y];
rotationalAVStates[0].w = avScalarVariables[WID];
rotationalAVStates[0].heading = avScalarVariables[HEADID];
linearAVStates[0].acc = avScalarVariables[ACCID];
linearAVStates[0].speed = avScalarVariables[SPEEDID];
//avStates[0].acc = 0;
//avStates[0].speed = 0;
goalPositions[0].x = goalVector[POSITIONID][X];
goalPositions[0].y = goalVector[POSITIONID][Y];
rotationalGoalStates[0].w = goalScalar[WID];
rotationalGoalStates[0].heading = goalScalar[HEADID];
linearGoalStates[0].acc = goalScalar[ACCID];
linearGoalStates[0].speed = goalScalar[SPEEDID];
//goalStates[0].acc = 0;
//goalStates[0].speed = 0;
collisionAvoidance(ACTUALOBS, appeared, NUMSTATICOBSS, staticObsVerdices_l, probe, obsPosition, rotationalObsStates, linearObsStates, avPosition,
rotationalAVStates, linearAVStates, goalPositions, localPath, LENGTHPATH, rotationalGoalStates, linearGoalStates);
//dffHeading = rotationalGoalStates[0].heading;
//without modify, the agent would be drawn to the obstacle in some cases
angleToGoal = getAngle(avVectorVariables[POSITIONID][X], avVectorVariables[POSITIONID][Y],
goalVector[POSITIONID][X],goalVector[POSITIONID][Y], ANGLESCALE);
newHeading = modify(obsPosition,avPosition[0],rotationalGoalStates[0].heading, angleToGoal);
//without deviate, the agent would be coming back and forth when the obstacle is moving exactly forward it
newHeading = deviate(newHeading);
if(newHeading != rotationalGoalStates[0].heading)
{
probe[0] = 2;
originalGoalHeading = rotationalGoalStates[0].heading;
rotationalGoalStates[0].heading = newHeading;
}
goalScalar[WID] = rotationalGoalStates[0].w;
goalScalar[HEADID] = rotationalGoalStates[0].heading;
goalScalar[ACCID] = linearGoalStates[0].acc;
goalScalar[SPEEDID] = linearGoalStates[0].speed;
avScalarVariables[HEADID] = goalScalar[HEADID];
avScalarVariables[ACCID] = goalScalar[ACCID];
}
void move()
{
goalScalar[HEADID] = getAngle(avVectorVariables[POSITIONID][X], avVectorVariables[POSITIONID][Y],
goalVector[POSITIONID][X],goalVector[POSITIONID][Y], ANGLESCALE);
avScalarVariables[HEADID] = goalScalar[HEADID];
}
void adjust()
{
int i = 0;
for(i = 0; i < SPACEDIMENSION; i++)
{
avVectorVariables[POSITIONID][i] = goalVector[POSITIONID][i];
}
}
void turn()
{
int i = 0;
if(oldStatus != AVOIDING){adjust();}
for(i = 0; i < SPACEDIMENSION; i++)
{
goalVector[POSITIONID][i] = WAYPOINTS[waypointsIndex][i];
}
goalScalar[SPEEDID] = MAXSCALAR[SPEEDID];
goalScalar[ACCID] = MAXSCALAR[ACCID];
goalScalar[HEADID] = getAngle(avVectorVariables[POSITIONID][X], avVectorVariables[POSITIONID][Y],
goalVector[POSITIONID][X],goalVector[POSITIONID][Y], ANGLESCALE);
avScalarVariables[HEADID] = goalScalar[HEADID];
avScalarVariables[ACCID] = goalScalar[ACCID];
/*if(avScalarVariables[HEADID] < goalScalar[HEADID])
{
goalScalar[WID] = MAXSCALAR[WID];
}
else if(avScalarVariables[HEADID] > goalScalar[HEADID])
{
goalScalar[WID] = MINSCALAR[WID];
}
else if(avScalarVariables[HEADID] == goalScalar[HEADID])
{
goalScalar[WID] = 0;
}*/
}
void calcSafeRegion()
{
widthSafeRegion--;
}
bool isOut()
{
int i = 0;
int dis = 0;
bool result = true;
for(i = 0; i < ACTUALOBS; i++)
{
if(appeared[i] == 1)
{
dis = getDisPointToPoint(obsVectorVariables[i][POSITIONID][X], obsVectorVariables[i][POSITIONID][Y],
avVectorVariables[POSITIONID][X], avVectorVariables[POSITIONID][Y]);
if(dis <= VIEWRADIUS)
{
result = false;
}
}
}
return result;
}
bool reachWaypoint(int index)
{
int currentDis = 0, newDis = 0, speed[2] = {0,0}, newPosi[SPACEDIMENSION] = {0,0};
int i = 0;
bool reach = false, overshoot = true;
for(i = 0; i < SPACEDIMENSION; i++)
{
speed[i] = getProjection(avScalarVariables[SPEEDID], avScalarVariables[HEADID], i);
newPosi[i] = avVectorVariables[POSITIONID][i] + speed[i];
}
currentDis = getDisPointToPoint(avVectorVariables[POSITIONID][X], avVectorVariables[POSITIONID][Y],
WAYPOINTS[index][X], WAYPOINTS[index][Y]);
newDis = getDisPointToPoint(newPosi[X], newPosi[Y], WAYPOINTS[index][X], WAYPOINTS[index][Y]);
if(currentDis < widthSafeRegion || newDis < widthSafeRegion)
{
if(currentDis < newDis)
{
reach = true;
}
}
return reach;
}
void makeDecision()
{
bool reach = true, arriving = true;
int i = 0;
oldStatus = status;
status = UNKNOWN;
/*criticalIndex++;
if(criticalIndex == CRITICALLENGTH)
{
criticalIndex = 0;
}*/
//should the agent avoid any obstacles?
if(!isOut())
{
status = AVOIDING;
for(i = 0; i < ACTUALOBS; i++)
{
if(appeared[i] == 1)
{
collided = isCollide(i);
}
}
}
else
{
if(oldStatus == AVOIDING)
{
status = TURNING;
}
}
//judge if the vehicle has reached the next waypoint
reach = reachWaypoint(waypointsIndex);
arriving = reach&(waypointsIndex == NUMWAYPOINTS-1);
//should the agent turn?
if(status != AVOIDING)
{
if(!reach)
{
status = MOVING;
}
//if the vehicle has reaches a waypoint and it is not the destination, then it turns its direction
if(reach && !arriving)
{
status = TURNING;
waypointsIndex++;
calcSafeRegion();
}
}
if(arriving)
{
status = ARRIVING;
}
}</declaration>
<location id="id4" x="-221" y="8">
<name x="-238" y="-25">STOP</name>
</location>
<location id="id5" x="-42" y="-26">
<committed/>
</location>
<location id="id6" x="221" y="-26">
<committed/>
</location>
<location id="id7" x="221" y="-127">
<committed/>
</location>
<location id="id8" x="-42" y="-127">
<name x="-59" y="-161">MOVE</name>
<label kind="invariant" x="-153" y="-127">timer<=PM</label>
</location>
<location id="id9" x="-221" y="-127">
<name x="-231" y="-161">INIT</name>
<committed/>
</location>
<init ref="id9"/>
<transition>
<source ref="id5"/>
<target ref="id8"/>
<label kind="guard" x="51" y="-93">status==
AVOIDING</label>
<label kind="synchronisation" x="51" y="-59">change!</label>
<label kind="assignment" x="51" y="-42">avoid()</label>
<nail x="93" y="-25"/>
<nail x="93" y="-68"/>
</transition>
<transition>
<source ref="id9"/>
<target ref="id8"/>
<label kind="synchronisation" x="-203" y="-144">start!</label>
<label kind="assignment" x="-153" y="-144">initialize()</label>
</transition>
<transition>
<source ref="id5"/>
<target ref="id4"/>
<label kind="guard" x="-195" y="-8">status==ARRIVING</label>
<nail x="-68" y="8"/>
</transition>
<transition>
<source ref="id5"/>
<target ref="id8"/>
<label kind="guard" x="-161" y="-93">status==
TURNING</label>
<label kind="synchronisation" x="-161" y="-59">change!</label>
<label kind="assignment" x="-161" y="-42">turn()</label>
<nail x="-110" y="-25"/>
<nail x="-110" y="-68"/>
</transition>
<transition>
<source ref="id5"/>
<target ref="id8"/>
<label kind="guard" x="-59" y="-93">status==
MOVING</label>
<label kind="assignment" x="-42" y="-59">move()</label>
</transition>
<transition>
<source ref="id6"/>
<target ref="id5"/>
<label kind="synchronisation" x="161" y="1">stay!</label>
<label kind="assignment" x="-25" y="1">makeDecision()</label>
<nail x="221" y="17"/>
<nail x="-42" y="17"/>
</transition>
<transition>
<source ref="id7"/>
<target ref="id6"/>
<label kind="synchronisation" x="230" y="-85">proceed!</label>
</transition>
<transition>
<source ref="id8"/>
<target ref="id7"/>
<label kind="guard" x="25" y="-144">timer>=PM</label>
<label kind="synchronisation" x="119" y="-144">invoke!</label>
<label kind="assignment" x="119" y="-127">timer=0</label>
</transition>
</template>
<template>
<name>ObsInitialization</name>
<parameter>const int ID</parameter>
<declaration>clock t;
void updateVector(int space, int boundary)
{
obsVectorVariables[ID][POSITIONID][space] += STEPPOS;
if(obsVectorVariables[ID][POSITIONID][space] > boundary)
{
obsVectorVariables[ID][POSITIONID][space] = boundary;
}
}
void updateScalar(int vID, int step)
{
obsScalarVariables[ID][vID] -= step;
if(obsScalarVariables[ID][vID] < MINSCALAROBS[ID][vID])
{
obsScalarVariables[ID][vID] = MINSCALAROBS[ID][vID];
}
}
bool ready()
{
if(avScalarVariables[SPEEDID] != 0 && waypointsIndex < NUMWAYPOINTS)
{
return true;
}
return false;
}
void initialize()
{
int maxExtension = 0;
int minExtension = 0;
int minDelta[SPACEDIMENSION] = {0,0};
int maxDelta[SPACEDIMENSION] = {0,0};
int target[SPACEDIMENSION] = {0,0};
int speed[SPACEDIMENSION] = {0,0};
int min[SPACEDIMENSION] = {0,0};
int max[SPACEDIMENSION] = {0,0};
int speedMax = getProjection(MAXSCALAROBS[ID][SPEEDID],PI/4,X);
int spaceIndex = 0;
int heading = 0;
int alpha = 0;
t = 0;
maxExtension = widthSafeRegion + speedMax * (PS + PC);
minExtension = widthSafeRegion + speedMax * PC;
heading = abs(avScalarVariables[HEADID]);
if(heading >=0 && heading<=PI/4)
{
alpha = heading;
}
else if(heading > PI/4 && heading <= PI/2)
{
alpha = PI/2 - heading;
}
else if(heading > PI/2 && heading <= 3*PI/4)
{
alpha = heading - PI/2;
}
else if(heading > 3*PI/4 && heading <= PI)
{
alpha = PI - heading;
}
alpha += PI/4;
for(spaceIndex = 0; spaceIndex < SPACEDIMENSION; spaceIndex++)
{
maxDelta[spaceIndex] = getValidRange(maxExtension, alpha, ANGLESCALE);
minDelta[spaceIndex] = getValidRange(minExtension, alpha, ANGLESCALE);
speed[spaceIndex] = getProjection(avScalarVariables[SPEEDID],avScalarVariables[HEADID],spaceIndex);
target[spaceIndex] = avVectorVariables[POSITIONID][spaceIndex] + speed[spaceIndex]*PS;
if(avVectorVariables[POSITIONID][spaceIndex] >= target[spaceIndex])
{
max[spaceIndex] = avVectorVariables[POSITIONID][spaceIndex];
min[spaceIndex] = target[spaceIndex];
}
else
{
max[spaceIndex] = target[spaceIndex];
min[spaceIndex] = avVectorVariables[POSITIONID][spaceIndex];
}
minOut[spaceIndex] = min[spaceIndex] - maxDelta[spaceIndex];
minIn[spaceIndex] = min[spaceIndex] - minDelta[spaceIndex];
maxOut[spaceIndex] = max[spaceIndex] + maxDelta[spaceIndex];
maxIn[spaceIndex] = max[spaceIndex] + minDelta[spaceIndex];
if(minOut[spaceIndex] < MINVECTOROBS[ID][POSITIONID][spaceIndex])
{
minOut[spaceIndex] = MINVECTOROBS[ID][POSITIONID][spaceIndex];
}
if(minIn[spaceIndex] < MINVECTOROBS[ID][POSITIONID][spaceIndex])
{
minIn[spaceIndex] = MINVECTOROBS[ID][POSITIONID][spaceIndex];
}
if(maxOut[spaceIndex] > MAXVECTOROBS[ID][POSITIONID][spaceIndex])
{
maxOut[spaceIndex] = MAXVECTOROBS[ID][POSITIONID][spaceIndex];
}
if(maxIn[spaceIndex] > MAXVECTOROBS[ID][POSITIONID][spaceIndex])
{
maxIn[spaceIndex] = MAXVECTOROBS[ID][POSITIONID][spaceIndex];
}
obsVectorVariables[ID][POSITIONID][spaceIndex] = minOut[spaceIndex];
}
obsScalarVariables[ID][ACCID] = 0;
obsScalarVariables[ID][SPEEDID] = MAXSCALAROBS[ID][SPEEDID];
obsScalarVariables[ID][WID] = 0;
obsScalarVariables[ID][HEADID] = MAXSCALAROBS[ID][HEADID];
}
void adjust()
{
int distance = getDisPointToPoint(obsVectorVariables[ID][POSITIONID][X], obsVectorVariables[ID][POSITIONID][Y],
avVectorVariables[POSITIONID][X], avVectorVariables[POSITIONID][Y]);
if(distance < widthSafeRegion*4)
{
if(obsVectorVariables[ID][POSITIONID][X]<avVectorVariables[POSITIONID][X])
{
obsVectorVariables[ID][POSITIONID][X] = obsVectorVariables[ID][POSITIONID][X] - 3*obsScalarVariables[ID][SPEEDID];
}
else
{
obsVectorVariables[ID][POSITIONID][X] = obsVectorVariables[ID][POSITIONID][X] + 3*obsScalarVariables[ID][SPEEDID];
}
if(obsVectorVariables[ID][POSITIONID][Y]<avVectorVariables[POSITIONID][Y])
{
obsVectorVariables[ID][POSITIONID][Y] = obsVectorVariables[ID][POSITIONID][Y] - 3*obsScalarVariables[ID][SPEEDID];
}
else
{
obsVectorVariables[ID][POSITIONID][Y] = obsVectorVariables[ID][POSITIONID][Y] + 3*obsScalarVariables[ID][SPEEDID];
}
}
}</declaration>
<location id="id10" x="136" y="110">
<name x="162" y="102">Appear</name>
</location>
<location id="id11" x="93" y="-136">
<label kind="invariant" x="119" y="-178">obsScalarVariables
[ID][HEADID]
>= MINSCALAROBS
[ID][HEADID]</label>
<committed/>
</location>
<location id="id12" x="-59" y="110">
<label kind="invariant" x="-42" y="59">obsScalarVariables
[ID][SPEEDID]
>= MINSCALAROBS
[ID][SPEEDID]</label>
<committed/>
</location>
<location id="id13" x="-340" y="-136">
<label kind="invariant" x="-323" y="-127">obsVectorVariables
[ID][POSITIONID][Y]
<= minIn[Y]</label>
<committed/>
</location>
<location id="id14" x="-340" y="110">
<label kind="invariant" x="-323" y="50">obsVectorVariables
[ID][POSITIONID][Y]
<= maxOut[Y]</label>
<committed/>
</location>
<location id="id15" x="-612" y="-25">
<name x="-622" y="-59">P1</name>
<label kind="invariant" x="-765" y="-68">obsVectorVariables
[ID][POSITIONID][X]
<= maxOut[X]</label>
<committed/>
</location>
<location id="id16" x="-612" y="-187">
</location>
<init ref="id16"/>
<transition>
<source ref="id12"/>
<target ref="id11"/>
</transition>
<transition>
<source ref="id11"/>
<target ref="id10"/>
<label kind="synchronisation" x="93" y="-34">obsGo[ID]!</label>
<label kind="assignment" x="102" y="-17">appeared[ID]=1</label>
</transition>
<transition>
<source ref="id11"/>
<target ref="id11"/>
<label kind="guard" x="-85" y="-238">obsScalarVariables[ID][HEADID]
> MINSCALAROBS[ID][HEADID]</label>
<label kind="assignment" x="-85" y="-204">updateScalar(HEADID, STEPHEAD)</label>
<nail x="68" y="-187"/>
<nail x="119" y="-187"/>
</transition>
<transition>
<source ref="id12"/>
<target ref="id12"/>
<label kind="guard" x="-221" y="136">obsScalarVariables[ID][SPEEDID]
> MINSCALAROBS[ID][SPEEDID]</label>
<label kind="assignment" x="-221" y="178">updateScalar(SPEEDID, STEPSPEED)</label>
<nail x="-85" y="153"/>
<nail x="-34" y="153"/>
</transition>
<transition>
<source ref="id13"/>
<target ref="id14"/>
<label kind="guard" x="-323" y="-59">obsVectorVariables
[ID][POSITIONID][Y]
>= minIn[Y]</label>
<label kind="assignment" x="-323" y="-8">obsVectorVariables
[ID][POSITIONID][Y]=maxIn[Y]</label>
</transition>
<transition>
<source ref="id14"/>
<target ref="id12"/>
<label kind="assignment" x="-161" y="85">adjust()</label>
</transition>
<transition>
<source ref="id13"/>
<target ref="id12"/>
<label kind="assignment" x="-161" y="-161">adjust()</label>
<nail x="-59" y="-136"/>
</transition>
<transition>
<source ref="id13"/>
<target ref="id13"/>
<label kind="guard" x="-442" y="-238">obsVectorVariables
[ID][POSITIONID][Y]
< minIn[Y]</label>
<label kind="assignment" x="-442" y="-187">updateVector(Y,minIn[Y])</label>
<nail x="-365" y="-170"/>
<nail x="-314" y="-170"/>
</transition>
<transition>
<source ref="id15"/>
<target ref="id13"/>
<label kind="guard" x="-518" y="-136">obsVectorVariables