-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreeds_and_shepp_sectioned_paper_figs.py
1644 lines (1251 loc) · 50.6 KB
/
reeds_and_shepp_sectioned_paper_figs.py
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
import math
import matplotlib.pyplot as plt
import numpy as np
from rs_global_params import *
from utils import *
from path_dictionary import path_dict
import pickle
## Save History to this file
logs_pickle = dict()
"""
Reeds Shepp path planner sample code
author Ali Boyali
from the paper:
Optimal Paths for a Car That Goes Both Forwards and Backwards
and the code:
Check the followings
http://msl.cs.uiuc.edu/~lavalle/cs326a/rs.c
Steven Lavalle Planning Algorithms
and the Python Robotics RS Implementation
https://github.com/AtsushiSakai/PythonRobotics#reeds-shepp-planning
Paper Summary:
u is the angular turning length : pi/2 or similar
t is the turning direction
Manifold parameters: t, u, v , w --> sequence indices (lr, ru, sv, lw) or Ct, Cu, Cv,Sw
L(t, u, v, w) --> Length function
"""
show_animation = True
class Path:
steering_map = {'L': MAX_STEER, 'R': -MAX_STEER, 'S': 0}
def __init__(self):
self.curve_num = [] # curve number
self.lengths = [] # maneuver lengths of RS motion primitives, in radian and meters n
self.lengths_expanded = [] # homogenous lengths all in meters assigned in append control method
self.ctypes = [] # maneuver code names Left Right Straight
self.total_length = 0.0 # total length of the maneuver
self.x = [] # x coordinates list
self.y = []
self.yaw = []
self.directions = [] # whether forward or backward \in [-1, 1]
# Controls, final time and final distance
'''
Controls are paremetrized by the curve lengths
'''
self.controls = {} # with the keys acceleration, steering and acceleration-steering
self.final_time = None
self.final_distance = None
'''
We store steering and accelaration as piecewise functions, acc function is a function of time,
but the steering function as a function of distance. Later to transform the steering conditions from
distance to time, we need to store the steering conditions list [s0, sfinal, steering_val] --> [t0, tf,
steering_val]
'''
self.steering_conditions = None
# we also store each curves coordinates separately
self.man_patches = [] # [[px, py, pyaw], [px, py, pyaw], ....]
def set_coords(self, x, y, yaw):
self.x = x
self.y = y
self.yaw = yaw
def set_directions(self, dir):
self.directions = dir
def generate_path(q0, q1):
x, y, phi = change_of_basis(q0, q1)
paths = []
paths = CSC(x, y, phi, paths)
paths = CCC(x, y, phi, paths)
paths = CCCC(x, y, phi, paths)
paths = CCSC(x, y, phi, paths)
paths = CSCC(x, y, phi, paths)
paths = CCSCC(x, y, phi, paths)
return paths
def set_path(paths, lengths, ctypes, length_rs, num):
path = Path()
path.curve_num = num
path.ctypes = ctypes
path.lengths = lengths
path.total_length = length_rs
# check same path exist
for tpath in paths:
typeissame = (tpath.ctypes == path.ctypes)
if typeissame:
if sum(tpath.lengths) - sum(path.lengths) <= 0.01:
return paths # not insert path
# Base.Test.@test path.L >= 0.01
if path.total_length >= 0.01:
paths.append(path)
return paths
def curve_params(phi):
sphi = np.sin(phi)
cphi = np.cos(phi)
ap = RADCURV * sphi
am = -RADCURV * sphi
b1 = RADCURV * (cphi - 1)
b2 = RADCURV * (cphi + 1)
return [sphi, cphi, ap, am, b1, b2]
def time_fli(args):
x, y, phi = args
return -x, y, -phi
def reflect(args):
x, y, phi = args
return x, -y, -phi
def reflect_flip(args):
x, y, phi = args
return -x, -y, phi
# -------------------------------------- CURVES STARTS HERE ----------------------------------------------------
# ------------------------------------------ CCC CURVES --------------------------------------------------------
# -------------------------------------- SAME TURN CURVES ------------------------------------------------------
def c_c_c(x, y, phi, rs, rc):
a = x - rs
b = y + rc
if np.abs(a) < EPS and np.abs(b) < EPS:
return False, 0.0, 0.0, 0.0, INFINITY
u1, theta = polar(a, b)
if u1 < RADCURVMUL4:
alpha = np.arccos(u1 / RADCURVMUL4)
t = wrapToPi(MPIDIV2 + alpha + theta)
u = wrapToPi(MPI - 2 * alpha)
v = wrapToPi(phi - t - u)
length_rs = RADCURV * (t + v + u)
if t >= 0 and u >= 0 and v >= 0:
return True, t, u, v, length_rs
return False, 0.0, 0.0, 0.0, INFINITY
def c_cc(x, y, phi, rs, rc):
a = x - rs
b = y + rc
if np.abs(a) < EPS and np.abs(b) < EPS:
return False, 0.0, 0.0, 0.0, INFINITY
u1, theta = polar(a, b)
if (u1 <= RADCURVMUL4):
alpha = np.arccos(u1 / RADCURVMUL4)
t = wrapToPi(MPIDIV2 + alpha + theta)
u = wrapToPi(MPI - 2 * alpha)
v = wrapToPi(t + u - phi)
length_rs = RADCURV * (t + v + u)
if t >= 0 and u >= 0 and v >= 0:
return True, t, u, v, length_rs
return False, 0.0, 0.0, 0.0, INFINITY
def cc_c(x, y, phi, rs, rc):
a = x - rs
b = y + rc
if np.abs(a) < EPS and np.abs(b) < EPS:
return False, 0.0, 0.0, 0.0, INFINITY
u1, theta = polar(a, b)
if (u1 <= RADCURVMUL4):
u = np.arccos((8 * SQRADCURV - u1 * u1) / (8 * SQRADCURV))
va = np.sin(u)
if np.abs(va) < 0.001:
va = 0.0
if np.abs(va) < 0.001 and np.abs(u1) < 0.001:
return False, 0.0, 0.0, 0.0, INFINITY
alpha = np.arcsin(RADCURVMUL2 * va / u1)
t = wrapToPi(MPIDIV2 - alpha + theta)
v = wrapToPi(t - u - phi)
length_rs = RADCURV * (t + u + v)
if t >= 0 and u >= 0 and v >= 0:
return True, t, u, v, length_rs
return False, 0.0, 0.0, 0.0, INFINITY
def CCC(x, y, phi, paths):
"""
Formula 8.3: C|C|C
"""
sphi, cphi, ap, am, b1, b2 = curve_params(phi)
# num 1 : ["L+", "R-", "L+"], arguments -- c_c_c(x,y,phi,ap,b1)
flag, t, u, v, length_rs = c_c_c(x, y, phi, ap, b1)
if flag:
num = 1
cytpe = path_dict[num]
paths = set_path(paths, [t, u, v], cytpe, length_rs, num)
# num 2 : ["L-", "R+", "L-"], arguments -- c_c_c(-x,y,-phi,am,b1) -> time flip of num1
flag, t, u, v, length_rs = c_c_c(-x, y, -phi, am, b1)
if flag:
num = 2
cytpe = path_dict[num]
paths = set_path(paths, [-t, -u, -v], cytpe, length_rs, num)
# num 3 : ["R+", "L-", "R+"], arguments -- c_c_c(x,-y,-phi,am,b1) -> reflection of num1
flag, t, u, v, length_rs = c_c_c(x, -y, -phi, am, b1)
if flag:
num = 3
cytpe = path_dict[num]
paths = set_path(paths, [t, u, v], cytpe, length_rs, num)
# num 4 : ["R-", "L+", "R-"], arguments -- c_c_c(-x,-y,phi,ap,b1) -> time flip + reflection of num 1
flag, t, u, v, length_rs = c_c_c(-x, -y, phi, ap, b1)
if flag:
num = 4
cytpe = path_dict[num]
paths = set_path(paths, [-t, -u, -v], cytpe, length_rs, num)
# ------------ C | CC ----_
"""
Formula 8.4 (1): C|CC
"""
# num 5 : ["L+", "R-", "L-"], arguments -- c_cc(x,y,phi,ap,b1) -> is given
flag, t, u, v, length_rs = c_cc(x, y, phi, ap, b1)
if flag:
num = 5
cytpe = path_dict[num]
paths = set_path(paths, [t, u, v], cytpe, length_rs, num)
# num 6 : ["L-", "R+", "L+"], arguments -- c_cc(-x,y,-phi,am,b1) -> time flip of num 5
flag, t, u, v, length_rs = c_cc(-x, y, -phi, am, b1)
if flag:
num = 6
cytpe = path_dict[num]
paths = set_path(paths, [-t, -u, -v], cytpe, length_rs, num)
# # num 7 : ["R+", "L-", "R-"], arguments -- c_cc(x,-y,-phi,am,b1) -> reflection of num 5
flag, t, u, v, length_rs = c_cc(x, -y, -phi, am, b1)
if flag:
num = 7
cytpe = path_dict[num]
paths = set_path(paths, [t, u, v], cytpe, length_rs, num)
# # num 8 : ["R-", "L+", "R+"], arguments -- c_cc(-x,-y,phi,ap,b1)
flag, t, u, v, length_rs = c_cc(-x, -y, phi, ap, b1)
if flag:
num = 8
cytpe = path_dict[num]
paths = set_path(paths, [t, u, v], cytpe, length_rs, num)
# ------------ CC|C ----_
"""
Formula 8.4 (2): CC|C
"""
# num 37 : ["L+", "R+", "L-"], arguments -- cc_c(x,y,phi,ap,b1) -> given
flag, t, u, v, length_rs = cc_c(x, y, phi, ap, b1)
if flag:
num = 37
cytpe = path_dict[num]
paths = set_path(paths, [t, u, v], cytpe, length_rs, num)
# num 38 : ["R+", "L+", "R-"], arguments -- cc_c(x,-y,-phi,am,b1) -> reflection of num 37
flag, t, u, v, length_rs = cc_c(x, -y, -phi, am, b1)
if flag:
num = 38
cytpe = path_dict[num]
paths = set_path(paths, [t, u, v], cytpe, length_rs, num)
# num 39 : ["L-", "R-", "L+"], arguments -- cc_c(-x,y,-phi,am,b1) -> time flip of num 37
flag, t, u, v, length_rs = cc_c(-x, y, -phi, am, b1)
if flag:
num = 39
cytpe = path_dict[num]
paths = set_path(paths, [-t, -u, -v], cytpe, length_rs, num)
# num 40 : ["R-", "L-", "R+"], arguments -- cc_c(-x,-y,phi,ap,b1) -> time flip + reflection
flag, t, u, v, length_rs = cc_c(-x, -y, phi, ap, b1)
if flag:
num = 40
cytpe = path_dict[num]
paths = set_path(paths, [-t, -u, -v], cytpe, length_rs, num)
return paths
# ------------------------------------------ CSC CURVES ------------------------------------------------------
def csca(x, y, phi, rs, rc):
"""
Formula 8.1: CSC (same turns)
"""
a = x - rs
b = y + rc
u, t = polar(a, b) # polar coordinates returns r, theta
v = wrapToPi(phi - t)
if t >= 0 and u >= 0 and v >= 0:
length_rs = RADCURV * (t + v) + u
return True, t, u, v, length_rs
return False, 0.0, 0.0, 0.0, INFINITY
def cscb(x, y, phi, rs, rc):
"""
Formula 8.2: CSC (opposite turns)
"""
a = x + rs
b = y - rc
rho, t1 = polar(a, b)
if rho >= RADCURVMUL2:
u = np.sqrt(rho * rho - SQRADCURVMUL2)
alpha = np.arctan2(RADCURVMUL2, u)
t = wrapToPi(t1 + alpha)
v = wrapToPi(t - phi)
if t >= 0 and u >= 0 and v >= 0:
length_rs = RADCURV * (t + v) + u
return True, t, u, v, length_rs
return False, 0.0, 0.0, 0.0, INFINITY
def CSC(x, y, phi, paths):
sphi, cphi, ap, am, b1, b2 = curve_params(phi)
# ----------------------- SAME TURN CURVES ------------------------------------------------------
# num 9 : ["L+", "S+", "L+"], arguments -- csca(x,y,phi,ap,b1)
flag, t, u, v, length_rs = csca(x, y, phi, ap, b1)
if flag:
num = 9
cytpe = path_dict[num]
paths = set_path(paths, [t, u, v], cytpe, length_rs, num)
# num 10 : ["R+", "S+", "R+"], arguments -- csca(x,-y,-phi,am,b1)
flag, t, u, v, length_rs = csca(x, -y, -phi, am, b1)
if flag:
num = 10
cytpe = path_dict[num]
paths = set_path(paths, [t, u, v], cytpe, length_rs, num) # reflection of num 9
# num 11 : ["L-", "S-", "L-"], arguments -- csca(-x,y,-phi, am, b1)
flag, t, u, v, length_rs = csca(-x, y, -phi, am, b1)
if flag:
num = 11
cytpe = path_dict[num]
paths = set_path(paths, [-t, -u, -v], cytpe, length_rs, num) # time flip of num 9
# num 12 : ["R-", "S-", "R-"], arguments -- csca(-x,-y,phi,ap,b1) -> time flip and reflection of num 9
flag, t, u, v, length_rs = csca(-x, -y, phi, ap, b1)
if flag:
num = 12
cytpe = path_dict[num]
paths = set_path(paths, [-t, -u, -v], cytpe, length_rs, num) # both time flip + reflection
# ----------------------- OPPOSITE TURN CURVES ------------------------------------------------------
# num 13 : ["L+", "S+", "R+"], arguments -- cscb(x,y,phi,ap,b2)
flag, t, u, v, length_rs = cscb(x, y, phi, ap, b2)
if flag:
num = 13
cytpe = path_dict[num]
paths = set_path(paths, [t, u, v], cytpe, length_rs, num=13)
# num 14 : ["R+", "S+", "L+"], arguments -- cscb(x,-y,-phi,am,b2) reflection of num 13
flag, t, u, v, length_rs = cscb(x, -y, -phi, am, b2)
if flag:
num = 14
cytpe = path_dict[num]
paths = set_path(paths, [t, u, v], cytpe, length_rs, num) # reflection of num 13
# num 15 : ["L-", "S-", "R-"], arguments -- cscb(-x,y,-phi,am,b2) -> time flip of num 13
flag, t, u, v, length_rs = cscb(-x, y, -phi, am, b2)
if flag:
num = 15
cytpe = path_dict[num]
paths = set_path(paths, [-t, -u, -v], cytpe, length_rs, num) # time flip of curve 13
# num 16 : ["R-", "S-", "L-"], arguments -- cscb(-x,-y,phi,ap,b2)
flag, t, u, v, length_rs = cscb(-x, -y, phi, ap, b2) # time flip and reflection of 13
if flag:
num = 16
cytpe = path_dict[num]
paths = set_path(paths, [-t, -u, -v], cytpe, length_rs, num) # time flip of curve 14
return paths
# ------------------------------------------ CCCC CURVES ------------------------------------------------------
def ccu_cuc(x, y, phi, rs, rc):
"""
Formula 8.7: CCu|CuC
"""
a = x + rs
b = y - rc
if np.abs(a) < EPS and np.abs(b) < EPS:
return False, 0.0, 0.0, 0.0, INFINITY
u1, theta = polar(a, b)
if u1 <= RADCURVMUL4:
if u1 > RADCURVMUL2:
alpha = np.arccos((u1 / 2 - RADCURV) / RADCURVMUL2)
t = wrapToPi(MPIDIV2 + theta - alpha)
u = wrapToPi(MPI - alpha)
v = wrapToPi(phi - t + 2 * u)
else:
alpha = np.arccos((u1 / 2 + RADCURV) / RADCURVMUL2)
t = wrapToPi(MPIDIV2 + theta + alpha)
u = wrapToPi(alpha)
v = wrapToPi(phi - t + 2 * u)
length_rs = RADCURV * (2 * u + t + v)
if t >= 0 and u >= 0 and v >= 0:
return True, t, u, v, length_rs
return False, 0.0, 0.0, 0.0, INFINITY
def c_cucu_c(x, y, phi, rs, rc):
"""
Formula 8.7: CCu|CuC
"""
a = x + rs
b = y - rc
if np.abs(a) < EPS and np.abs(b) < EPS:
return False, 0.0, 0.0, 0.0, INFINITY
u1, theta = polar(a, b)
if u1 > 6 * RADCURV:
return False, 0.0, 0.0, 0.0, INFINITY
va1 = (5 * SQRADCURV - u1 * u1 / 4) / SQRADCURVMUL2
if va1 < 0 or va1 > 1:
return False, 0.0, 0.0, 0.0, INFINITY
u = np.arccos(va1)
va2 = np.sin(u)
alpha = np.arcsin(RADCURVMUL2 * va2 / u1)
t = wrapToPi(MPIDIV2 + theta + alpha)
v = wrapToPi(t - phi)
length_rs = RADCURV * (2 * u + t + v)
if t >= 0 and u >= 0 and v >= 0:
return True, t, u, v, length_rs
return False, 0.0, 0.0, 0.0, INFINITY
def CCCC(x, y, phi, paths):
sphi, cphi, ap, am, b1, b2 = curve_params(phi)
########## -------------------------------- C Cu | Cu C Curves# -------------------------------------------------
"""
Formula 8.7: CCu|CuC
"""
# num 17 : ["L+", "R+", "L-", "R-"], arguments -- cu_cuc(x,y,phi,ap,b2) -> is given
flag, t, u, v, length_rs = ccu_cuc(x, y, phi, ap, b2)
if flag:
num = 17
cytpe = path_dict[num]
paths = set_path(paths, [t, u, u, v], cytpe, length_rs, num)
# num 18 : ["R+", "L+", "R-", "L-"], arguments -- ccu_cuc(x,-y,-phi,am,b2)
flag, t, u, v, length_rs = ccu_cuc(x, -y, -phi, am, b2)
if flag:
num = 18
cytpe = path_dict[num]
paths = set_path(paths, [t, u, u, v], cytpe, length_rs, num) # -> reflection of num 17
# num 19 : ["L-", "R-", "L+", "R+"], arguments -- ccu_cuc(-x,y,-phi,am,b2)
flag, t, u, v, length_rs = ccu_cuc(-x, y, -phi, am, b2)
if flag:
num = 19
cytpe = path_dict[num]
paths = set_path(paths, [-t, -u, -u, -v], cytpe, length_rs, num) # -> time filip of num 17
# num 20: ["R-", "L-", "R+", "L+"], arguments -- ccu_cuc(-x,-y,phi,ap,b2) -> time flip and reflect of num 17
flag, t, u, v, length_rs = ccu_cuc(-x, -y, phi, ap, b2)
if flag:
num = 20
cytpe = path_dict[num]
paths = set_path(paths, [-t, -u, -u, -v], cytpe, length_rs, num) # -> time filip of num 17
########## ---------------------------------- C | Cu Cu | C Curves --------------------------------------------
"""
Formula 8.8: C|CuCu|C
"""
# num 21 : ["L+", "R-", "L-", "R+"], arguments -- c_cucu_c(x,y,phi,ap,b2)
flag, t, u, v, length_rs = c_cucu_c(x, y, phi, ap, b2)
if flag:
num = 21
cytpe = path_dict[num]
paths = set_path(paths, [t, u, u, v], cytpe, length_rs, num) # -> given
# num 22 : ["R+", "L-", "R-", "L+"], arguments -- c_cucu_c(x,-y,-phi,am,b2)
flag, t, u, v, length_rs = c_cucu_c(x, -y, -phi, am, b2)
if flag:
num = 22
cytpe = path_dict[num]
paths = set_path(paths, [t, u, u, v], cytpe, length_rs, num) # ---> reflection of num 21
# num 23 : ["L-", "R+", "L+", "R-"], arguments -- c_cucu_c(-x,y,-phi,am,b2) --> time flip of num 21
flag, t, u, v, length_rs = c_cucu_c(-x, y, -phi, am, b2)
if flag:
num = 23
cytpe = path_dict[num]
paths = set_path(paths, [-t, -u, -u, -v], cytpe, length_rs, num) # -> time flip of num 21
# num 24 : ["R-", "L+", "R+", "L-"], arguments -- c_cucu_c(-x,-y,phi,ap,b2)
flag, t, u, v, length_rs = c_cucu_c(-x, -y, phi, ap, b2)
if flag:
num = 24
cytpe = path_dict[num]
paths = set_path(paths, [-t, -u, -u, -v], cytpe, length_rs, num) # -> time flip and reflection
return paths
# ------------------------------------------ CCSC CURVES ------------------------------------------------------
def c_c2sca(x, y, phi, rs, rc):
"""
Formula 8.9 (1): C|C[pi/2]SC
"""
a = x - rs
b = y + rc
u1, theta = polar(a, b)
if u1 >= RADCURVMUL2:
u = np.sqrt(u1 ** 2 - SQRADCURVMUL2) - RADCURVMUL2
if u >= 0:
alpha = np.arctan2(RADCURVMUL2, (u + RADCURVMUL2))
t = wrapToPi(MPIDIV2 + theta + alpha)
v = wrapToPi(t + MPIDIV2 - phi)
length_rs = RADCURV * (t + MPIDIV2 + v) + u
if t >= 0 and u >= 0 and v >= 0:
return True, t, u, v, length_rs
return False, 0.0, 0.0, 0.0, INFINITY
def c_c2scb(x, y, phi, rs, rc):
"""
Formula 8.9 (1): C|C[pi/2]SC
"""
a = x + rs
b = y - rc
u1, theta = polar(a, b)
if u1 >= RADCURVMUL2:
t = wrapToPi(MPIDIV2 + theta)
u = u1 - RADCURVMUL2
v = wrapToPi(phi - t - MPIDIV2)
length_rs = RADCURV * (t + MPIDIV2 + v) + u
if t >= 0 and u >= 0 and v >= 0:
return True, t, u, v, length_rs
return False, 0.0, 0.0, 0.0, INFINITY
def CCSC(x, y, phi, paths):
sphi, cphi, ap, am, b1, b2 = curve_params(phi)
######## ------------------------------- C | C[pi/2] S C Curves -----------------------------------------
"""
Formula 8.9 (1): C|C[pi/2]SC
"""
# ----------------------------------- SAME TURN MANEUVERS -----------------------------------------------------
# num 25 : ["L+", "R-", "S-", "L-"], arguments -- c_c2sca(x,y,phi,ap,b1) -> given
flag, t, u, v, length_rs = c_c2sca(x, y, phi, ap, b1)
if flag:
num = 25
cytpe = path_dict[num]
paths = set_path(paths, [t, MPIDIV2, u, v], cytpe, length_rs, num)
# num 26 : ["R+", "L-", "S-", "R-"], arguments -- c_c2sca(x,-y,-phi,am,b1)
flag, t, u, v, length_rs = c_c2sca(x, -y, -phi, am, b1)
if flag:
num = 26
cytpe = path_dict[num]
paths = set_path(paths, [t, MPIDIV2, u, v], cytpe, length_rs, num) # -> reflection of num 25
# num 27 : ["L-", "R+", "S+", "L+"], arguments -- c_c2sca(-x,y,-phi,am,b1) -> time flip of num 25
flag, t, u, v, length_rs = c_c2sca(-x, y, -phi, am, b1)
if flag:
num = 27
cytpe = path_dict[num]
paths = set_path(paths, [-t, -MPIDIV2, -u, -v], cytpe, length_rs, num)
# num 28 : ["R-", "L+", "S+", "R+"], arguments -- c_c2sca(-x,-y,phi,ap,b1) -> time flip of num 26
flag, t, u, v, length_rs = c_c2sca(-x, -y, phi, ap, b1)
if flag:
num = 28
cytpe = path_dict[num]
paths = set_path(paths, [-t, -MPIDIV2, -u, -v], cytpe, length_rs, num)
# ----------------------------------------OPPOSITE TURN MANEUVERS -------------------------------------------
# num 29 : ["L+", "R-", "S-", "R-"], arguments -- c_c2scb(x,y,phi,ap,b2) -> given
flag, t, u, v, length_rs = c_c2scb(x, y, phi, ap, b2)
if flag:
num = 29
cytpe = path_dict[num]
paths = set_path(paths, [t, MPIDIV2, u, v], cytpe, length_rs, num)
# num 30 : ["R+", "L-", "S-", "L-"], arguments -- c_c2scb(x,-y,-phi,am,b2) -> reflection of num 29
flag, t, u, v, length_rs = c_c2scb(x, -y, -phi, am, b2)
if flag:
num = 30
cytpe = path_dict[num]
paths = set_path(paths, [t, MPIDIV2, u, v], cytpe, length_rs, num)
# num 31 : ["L-", "R+", "S+", "R+"], arguments -- c_c2scb(-x,y,-phi,am,b2) -> time flip
flag, t, u, v, length_rs = c_c2scb(-x, y, -phi, am, b2)
if flag:
num = 31
cytpe = path_dict[num]
paths = set_path(paths, [-t, -MPIDIV2, -u, -v], cytpe, length_rs, num)
# num 32 : ["R-", "L+", "S+", "L+"], arguments -- c_c2scb(-x,-y,phi,ap,b2) -> time flip + reflection
flag, t, u, v, length_rs = c_c2scb(-x, -y, phi, ap, b2)
if flag:
num = 32
cytpe = path_dict[num]
paths = set_path(paths, [-t, -MPIDIV2, -u, -v], cytpe, length_rs, num)
return paths
# ------------------------------------------ CSCC CURVES ------------------------------------------------------
def csc2_ca(x, y, phi, rs, rc):
"""
Formula 8.9 (2): CSC[pi/2]|C
"""
a = x - rs
b = y + rc
u1, theta = polar(a, b)
if u1 >= RADCURVMUL2:
u = np.sqrt(u1 ** 2 - SQRADCURVMUL2) - RADCURVMUL2
if u >= 0:
alpha = np.arctan2(u + RADCURVMUL2, RADCURVMUL2)
t = wrapToPi(MPIDIV2 + theta - alpha)
v = wrapToPi(t - MPIDIV2 - phi)
length_rs = RADCURV * (t + MPIDIV2 + v) + u
if t >= 0 and u >= 0 and v >= 0:
return True, t, u, v, length_rs
return False, 0.0, 0.0, 0.0, INFINITY
def csc2_cb(x, y, phi, rs, rc):
"""
Formula 8.10 (2): CSC[pi/2]|C
"""
a = x + rs
b = y - rc
u1, theta = polar(a, b)
if u1 >= RADCURVMUL2:
t = wrapToPi(theta)
u = u1 - RADCURVMUL2
v = wrapToPi(-t - MPIDIV2 + phi)
length_rs = RADCURV * (t + MPIDIV2 + v) + u
if t >= 0 and u >= 0 and v >= 0:
return True, t, u, v, length_rs
return False, 0.0, 0.0, 0.0, INFINITY
def CSCC(x, y, phi, paths):
"""
Formula 8.9 (2): CSC[pi/2]|C
"""
sphi, cphi, ap, am, b1, b2 = curve_params(phi)
# ----------------------------------- SAME TURN MANEUVERS -----------------------------------------------------
# num 41 : ["L+", "S+", "R+", "L-"], arguments -- csc2_ca(x,y,phi,ap,b1) -> given
flag, t, u, v, length_rs = csc2_ca(x, y, phi, ap, b1)
if flag:
num = 41
cytpe = path_dict[num]
paths = set_path(paths, [t, u, MPIDIV2, v], cytpe, length_rs, num)
# num 42 : ["R+", "S+", "L+", "R-"], arguments -- csc2_ca(x,-y,-phi,am,b1) -> reflection
flag, t, u, v, length_rs = csc2_ca(x, -y, -phi, am, b1)
if flag:
num = 42
cytpe = path_dict[num]
paths = set_path(paths, [t, u, MPIDIV2, v], cytpe, length_rs, num)
# num 43 : ["L-", "S-", "R-", "L+"], arguments -- csc2_ca(-x,y,-phi,am,b1) -> time flip
flag, t, u, v, length_rs = csc2_ca(-x, y, -phi, am, b1)
if flag:
num = 43
cytpe = path_dict[num]
paths = set_path(paths, [-t, -u, -MPIDIV2, -v], cytpe, length_rs, num)
# num 44 : ["R-", "S-", "L-", "R+"], arguments -- csc2_ca(-x,-y,phi,ap,b1) -> time flip + reflection
flag, t, u, v, length_rs = csc2_ca(-x, -y, phi, ap, b1)
if flag:
num = 44
cytpe = path_dict[num]
paths = set_path(paths, [-t, -u, -MPIDIV2, -v], cytpe, length_rs, num)
# ----------------------------------- SAME TURN MANEUVERS -----------------------------------------------------
# num 45 : ["L+", "S+", "L+", "R-"], arguments -- csc2_cb(x,y,phi,ap,b2) -> given
flag, t, u, v, length_rs = csc2_cb(x, y, phi, ap, b2)
if flag:
num = 45
cytpe = path_dict[num]
paths = set_path(paths, [t, u, MPIDIV2, v], cytpe, length_rs, num)
# num 46 : ["R+", "S+", "R+", "L-"], arguments -- csc2_cb(x,-y,-phi,am,b2) -> reflection
flag, t, u, v, length_rs = csc2_cb(x, -y, -phi, am, b2)
if flag:
num = 46
cytpe = path_dict[num]
paths = set_path(paths, [t, u, MPIDIV2, v], cytpe, length_rs, num)
# num 47 : ["L-", "S-", "L-", "R+"], arguments -- csc2_cb(-x,y,-phi,am,b2) -> time flip
flag, t, u, v, length_rs = csc2_cb(-x, y, -phi, am, b2)
if flag:
num = 47
cytpe = path_dict[num]
paths = set_path(paths, [-t, -u, -MPIDIV2, -v], cytpe, length_rs, num)
# num 48 : ["R-", "S-", "R-", "L+"], arguments -- csc2_cb(-x,-y,phi,ap,b2) -> time flip and reflection
flag, t, u, v, length_rs = csc2_cb(-x, -y, phi, ap, b2)
if flag:
num = 48
cytpe = path_dict[num]
paths = set_path(paths, [-t, -u, -MPIDIV2, -v], cytpe, length_rs, num)
return paths
# ------------------------------------------ CCSCC CURVES ------------------------------------------------------
def c_c2sc2_c(x, y, phi, rs, rc):
"""
Formula 8.11: C|C[pi/2]SC[pi/2]|C
"""
a = x + rs
b = y - rc
u1, theta = polar(a, b)
if u1 >= RADCURVMUL4:
u = np.sqrt(u1 ** 2 - SQRADCURVMUL2) - RADCURVMUL4
if u >= 0:
alpha = np.arctan2(RADCURVMUL2, (u + RADCURVMUL4))
t = wrapToPi(MPIDIV2 + theta + alpha)
v = wrapToPi(t - phi)
length_rs = RADCURV * (t + MPI + v) + u
if t >= 0 and u >= 0 and v >= 0:
return True, t, u, v, length_rs
return False, 0.0, 0.0, 0.0, INFINITY
def CCSCC(x, y, phi, paths):
"""
Formula 8.11: C|C[pi/2]SC[pi/2]|C
"""
sphi, cphi, ap, am, b1, b2 = curve_params(phi)
########## ---------------------------------- C | C2 S C2 | C Curves --------------------------------------------
# num 33 : ["L+", "R-", "S-", "L-", "R+"], arguments -- c_c2sc2_c(x,y,phi,ap,b2) -> given
flag, t, u, v, length_rs = c_c2sc2_c(x, y, phi, ap, b2)
if flag:
num = 33
cytpe = path_dict[num]
paths = set_path(paths, [t, MPIDIV2, u, MPIDIV2, v], cytpe, length_rs, num)
# num 34 : ["R+", "L-", "S-", "R-", "L+"], arguments -- c_c2sc2_c(x,-y,-phi,am,b2) -> reflection of num 33
flag, t, u, v, length_rs = c_c2sc2_c(x, -y, -phi, am, b2)
if flag:
num = 34
cytpe = path_dict[num]
paths = set_path(paths, [t, MPIDIV2, u, MPIDIV2, v], cytpe, length_rs, num)
# num 35 : ["L-", "R+", "S+", "L+", "R-"], arguments -- c_c2sc2_c(-x,y,-phi,am,b2) -> time flip of num 33
flag, t, u, v, length_rs = c_c2sc2_c(-x, y, -phi, am, b2)
if flag:
num = 35
cytpe = path_dict[num]
paths = set_path(paths, [-t, -MPIDIV2, -u, -MPIDIV2, -v], cytpe, length_rs, num)
# num 36 : ["R-", "L+", "S+", "R+", "L-"], arguments -- c_c2sc2_c(-x,-y,phi,ap,b2) -> time flip of num 34
flag, t, u, v, length_rs = c_c2sc2_c(-x, -y, phi, ap, b2)
if flag:
num = 36
cytpe = path_dict[num]
paths = set_path(paths, [-t, -MPIDIV2, -u, -MPIDIV2, -v], cytpe, length_rs, num)
return paths
### -------------------------------------- PATH PLANNING STARTS HERE ---------------------------------------------
def calc_shortest_path_length(sx, sy, syaw, gx, gy, gyaw):
'''
Finding the shortest path
'''
q0 = [sx, sy, syaw] # starting node
q1 = [gx, gy, gyaw] # ending node
paths = generate_path(q0, q1)
minL = np.Inf
best_path_index = -1
for i, _ in enumerate(paths):
if paths[i].total_length <= minL:
minL = paths[i].total_length
best_path_index = i
# bpath = paths[best_path_index]
return minL
def generate_coordinates(path, q0):
'''
Given the path object, returns px, py, pyaw and directions with the interpolated points
ds = rho * dheta --> dheta = ds / rho
:param path:
:return:
'''
rho = RADCURV
ds = MOTION_RESOLUTION
mode = path.ctypes
clen = path.lengths
# angle resolution
dth = ds / rho
# path always start at 0, 0, 0
x0, y0, yaw0 = 0.0, 0.0, 0.0
px, py, pyaw, directions = [], [], [], []
for maneuver, ll in zip(mode, clen):
'''
First compute the pure maneuver, than stitch
'''
if maneuver == 'L+':
ll_pos = np.abs(ll)
t = np.arange(0, ll_pos, dth)
t[-1] = ll_pos
x = rho * np.sin(t)
y = rho * (1 - np.cos(t))
yaw = t
dirs = np.zeros(t.shape)
# Rotate to the world coordinates
xb, yb = rotate_bw(x, y, yaw0)
# pxsec = (xb + x0).tolist()
# pysec = (yb + y0).tolist()
# pyawsec = (yaw + yaw0).tolist()
# pdirsec = (dirs + 1).tolist()
#
# ## Patch the sections
# path.man_pathches += [pxsec, pysec, pyawsec, pdirsec]
#
# px += pxsec
# py += pysec
# pyaw += pyawsec
# directions += pdirsec
px += (xb + x0).tolist()
py += (yb + y0).tolist()
pyaw += (yaw + yaw0).tolist()
directions += (dirs + 1).tolist()
x0, y0, yaw0 = px[-1], py[-1], pyaw[-1]
# plot_xy(px, py)
if maneuver == 'S+':
ll_pos = np.abs(ll)
s = np.arange(0, ll_pos, ds)
s[-1] = ll_pos
x = s
y = np.zeros(x.shape[0])
yaw = np.zeros(x.shape[0])
dirs = np.zeros(s.shape)
xb, yb = rotate_bw(x, y, yaw0)
px += (xb + x0).tolist()
py += (yb + y0).tolist()
pyaw += (yaw + yaw0).tolist()
directions += (dirs + 1).tolist()
x0, y0, yaw0 = px[-1], py[-1], pyaw[-1]
# plot_xy(px, py)
if maneuver == 'R+':
ll_pos = np.abs(ll)
t = np.arange(0, ll_pos, dth)
if len(t):
t[-1] = ll_pos
else:
t = np.array([0.0])
x = rho * np.sin(t)
y = - rho * (1 - np.cos(t))
yaw = -t
dirs = np.zeros(t.shape)
# Rotate to the world coordinates
xb, yb = rotate_bw(x, y, yaw0)
px += (xb + x0).tolist()
py += (yb + y0).tolist()
pyaw += (yaw + yaw0).tolist()
directions += (dirs + 1).tolist()
x0, y0, yaw0 = px[-1], py[-1], pyaw[-1]
# plot_xy(px, py)
if maneuver == 'L-':
ll_pos = np.abs(ll)
t = np.arange(0, ll_pos, dth)
if len(t):
t[-1] = ll_pos
else: