-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdubinswrapper.py
More file actions
executable file
·234 lines (184 loc) · 7.42 KB
/
Copy pathdubinswrapper.py
File metadata and controls
executable file
·234 lines (184 loc) · 7.42 KB
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
#!/usr/bin/env python3
import os, sys, platform, math
import ctypes as ct
import numpy as np
class DubinsWrapper:
libgdip = None
def init_library(self):
try:
file_extension = '.so'
if platform.system() =='cli':
file_extension = '.dll'
elif platform.system() =='Windows':
file_extension = '.dll'
elif platform.system() == 'Darwin':
file_extension = '.dylib'
else:
file_extension = '.so'
libfullpath = os.path.abspath(os.path.join(os.path.dirname(__file__), '../gdip/lib/libGDIP' + file_extension))
print("Loading " + libfullpath)
DubinsWrapper.libgdip = ct.CDLL(libfullpath)
except:
print ('----------------------------------------------------')
print ('The GDIP library could not be loaded.')
print ('----------------------------------------------------')
exit(1)
DubinsWrapper.libgdip.new_GdipAPI.restype = ct.c_void_p
def __del__(self):
#print("Destruct Dubins maneuver")
self.gdip_destruct(self.object_hanle)
def __init__(self):
#initialize the GDIP library (only for the first time)
if DubinsWrapper.libgdip is None:
self.init_library()
# create instance of the maneuver
self.object_hanle = self.libgdip.new_GdipAPI()
self.gdip_set_configurations = ct.CFUNCTYPE \
(None, ct.c_void_p, ct.c_double*3, ct.c_double*3, ct.c_double) \
(("python_set_configurations", DubinsWrapper.libgdip))
self.gdip_set_configurations_dip = ct.CFUNCTYPE \
(None, ct.c_void_p, ct.c_double*2, ct.c_double*2, ct.c_double*2, ct.c_double*2, ct.c_double) \
(("python_set_configurations_dip", DubinsWrapper.libgdip))
self.gdip_set_configurations_gdip = ct.CFUNCTYPE \
(None, ct.c_void_p, ct.c_double*2, ct.c_double*2, ct.c_double, ct.c_double*2, ct.c_double*2, ct.c_double, ct.c_double) \
(("python_set_configurations_gdip", DubinsWrapper.libgdip))
self.gdip_get_length = ct.CFUNCTYPE (ct.c_double, ct.c_void_p) (("python_get_length", DubinsWrapper.libgdip))
self.gdip_sample_state_to_tmp = ct.CFUNCTYPE (None, ct.c_void_p, ct.c_double) (("python_sample_state_to_tmp", DubinsWrapper.libgdip))
self.gdip_get_tmp_x = ct.CFUNCTYPE(ct.c_double, ct.c_void_p) (("python_get_tmp_x", DubinsWrapper.libgdip))
self.gdip_get_tmp_y = ct.CFUNCTYPE(ct.c_double, ct.c_void_p) (("python_get_tmp_y", DubinsWrapper.libgdip))
self.gdip_get_tmp_theta = ct.CFUNCTYPE(ct.c_double, ct.c_void_p) (("python_get_tmp_theta", DubinsWrapper.libgdip))
self.gdip_destruct = ct.CFUNCTYPE(None, ct.c_void_p) (("python_destruct", DubinsWrapper.libgdip))
@staticmethod
def shortest_path(start, end, turning_radius):
"""
Construct Dubins maneuver between two configurations
Parameters
----------
start: numpy array(double*3)
start configuration
end: numpy array(double*3)
end configuration
turning_radius: double
minimum turning radius
"""
man = DubinsWrapper()
arrtype = ct.c_double * 3
arr_p1 = arrtype()
arr_p2 = arrtype()
c_radius = ct.c_double()
for i in range(0,3):
arr_p1[i] = start[i]
arr_p2[i] = end[i]
c_radius = turning_radius
man.gdip_set_configurations(man.object_hanle, arr_p1, arr_p2, c_radius)
return man
@staticmethod
def shortest_path_DIP(point1, interval1, point2, interval2, turning_radius):
"""
Construct Dubins maneuver between two configurations
Parameters
----------
point1: numpy array(double*2)
start position
interval1: numpy array(double*2)
angle interval for point1 (right_angle, diff)
the interval is then [right_angle, right_angle + diff]
point2: numpy array(double*2)
end position
interval2: numpy array(double*2)
angle interval for point2 (right_angle, diff)
the interval is then [right_angle, right_angle + diff]
turning_radius: double
minimum turning radius
"""
man = DubinsWrapper()
arrtype = ct.c_double * 2
arr_p1 = arrtype()
arr_i1 = arrtype()
arr_p2 = arrtype()
arr_i2 = arrtype()
c_radius = ct.c_double()
arr_p1[0] = point1[0]
arr_p1[1] = point1[1]
arr_i1[0] = interval1[0]
arr_i1[1] = interval1[1]
arr_p2[0] = point2[0]
arr_p2[1] = point2[1]
arr_i2[0] = interval2[0]
arr_i2[1] = interval2[1]
c_radius = turning_radius
man.gdip_set_configurations_dip(man.object_hanle, arr_p1, arr_i1, arr_p2, arr_i2, c_radius)
return man
@staticmethod
def shortest_path_GDIP(point1, interval1, radius1, point2, interval2, radius2, turning_radius):
"""
Construct Dubins maneuver between two configurations
Parameters
----------
point1: numpy array(double*2)
start position
interval1: numpy array(double*2)
angle interval for point1 (right_angle, diff)
the interval is then [right_angle, right_angle + diff]
point2: numpy array(double*2)
end position
interval2: numpy array(double*2)
angle interval for point2 (right_angle, diff)
the interval is then [right_angle, right_angle + diff]
turning_radius: double
minimum turning radius
"""
man = DubinsWrapper()
arrtype = ct.c_double * 2
arr_p1 = arrtype()
arr_i1 = arrtype()
arr_p2 = arrtype()
arr_i2 = arrtype()
arr_p1[0] = point1[0]
arr_p1[1] = point1[1]
arr_i1[0] = interval1[0]
arr_i1[1] = interval1[1]
arr_p2[0] = point2[0]
arr_p2[1] = point2[1]
arr_i2[0] = interval2[0]
arr_i2[1] = interval2[1]
c_radius = ct.c_double()
c_radius = turning_radius
c_radius1 = ct.c_double()
c_radius1 = radius1
c_radius2 = ct.c_double()
c_radius2 = radius2
man.gdip_set_configurations_gdip(man.object_hanle, arr_p1, arr_i1, c_radius1, arr_p2, arr_i2, c_radius2, c_radius)
return man
def get_length(self):
"""
Get length of the maneuver
Returns
-------
bool
True if there is collision, False otherwise
"""
return self.gdip_get_length(self.object_hanle)
def sample_many(self, step):
"""
Sample the manuver based on the step
Parameters
----------
step: double
step for the sampling
Returns
-------
states
"""
length = self.get_length()
lens = np.arange(0, length, step)
path = []
for l in lens:
self.gdip_sample_state_to_tmp(self.object_hanle, l)
state = [self.gdip_get_tmp_x(self.object_hanle), self.gdip_get_tmp_y(self.object_hanle), self.gdip_get_tmp_theta(self.object_hanle)]
path.append(state)
return [path, lens]
if __name__ == "__main__":
a = DubinsWrapper()
a.shortest_path((0,0,0), (1,1,2), 1)
print(a.get_length())