-
Notifications
You must be signed in to change notification settings - Fork 1
/
dubins_gym.py
215 lines (175 loc) · 5.94 KB
/
dubins_gym.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
import numpy as np
import matplotlib.pyplot as plt
import math
import gym
from gym import spaces
import time
import itertools
from stable_baselines.sac.policies import MlpPolicy
from stable_baselines import SAC
MAX_STEER = np.pi/3
MAX_SPEED = 10.0
MIN_SPEED = 0.
THRESHOLD_DISTANCE_2_GOAL = 0.02
MAX_X = 10.
MAX_Y = 10.
max_ep_length = 800
# Vehicle parameters
LENGTH = 0.45 # [m]
WIDTH = 0.2 # [m]
BACKTOWHEEL = 0.1 # [m]
WHEEL_LEN = 0.03 # [m]
WHEEL_WIDTH = 0.02 # [m]
TREAD = 0.07 # [m]
WB = 0.25 # [m]
show_animation = True
class DubinGym(gym.Env):
def __init__(self,start_point, target_point):
super(DubinGym,self).__init__()
metadata = {'render.modes': ['console']}
self.action_space = spaces.Box(np.array([0., -1.]), np.array([1., 1.]), dtype = np.float32)
low = np.array([-1.,-1.,-4.])
high = np.array([1.,1.,4.])
self.observation_space = spaces.Box(low, high, dtype=np.float32)
self.target = [target_point[0]/MAX_X, target_point[1]/MAX_Y, target_point[2]]
self.pose = [start_point[0]/MAX_X, start_point[1]/MAX_Y, start_point[2]]
self.action = [0., 0.]
self.traj_x = [self.pose[0]*MAX_X]
self.traj_y = [self.pose[1]*MAX_Y]
self.traj_yaw = [self.pose[2]]
def reset(self):
self.pose = np.array([0., 0., 1.57])
self.traj_x = [0.*MAX_X]
self.traj_y = [0.*MAX_Y]
self.traj_yaw = [1.57]
return np.array([0., 0., 1.57])
def get_reward(self):
x_target = self.target[0]
y_target = self.target[1]
x = self.pose[0]
y = self.pose[1]
yaw_car = self.pose[2]
head = math.atan((y_target-y)/(x_target-x+0.01))
return -1*(abs(x - x_target) + abs(y - y_target) + abs (head - yaw_car))
def step(self,action):
reward = 0
done = False
info = {}
self.action = action
self.pose = self.update_state(self.pose, action, 0.005)
print(self.pose)
if ((abs(self.pose[0]) < 1.) and (abs(self.pose[1]) < 1.)):
if(abs(self.pose[0]-self.target[0])<THRESHOLD_DISTANCE_2_GOAL and abs(self.pose[1]-self.target[1])<THRESHOLD_DISTANCE_2_GOAL):
reward = 10
done = True
print('Goal Reached')
else:
reward = self.get_reward()
else :
done = True
reward = -1.
print("Outside range")
return np.array(self.pose), reward, done, info
def render(self):
# print("Rendering")
self.traj_x.append(self.pose[0]*MAX_X)
self.traj_y.append(self.pose[1]*MAX_Y)
self.traj_yaw.append(self.pose[2])
plt.cla()
# for stopping simulation with the esc key.
plt.gcf().canvas.mpl_connect('key_release_event',
lambda event: [exit(0) if event.key == 'escape' else None])
plt.plot(self.traj_x*10, self.traj_y*10, "ob", markersize = 2, label="trajectory")
plt.plot(self.target[0]*MAX_X, self.target[1]*MAX_Y, "xg", label="target")
self.plot_car()
plt.axis("equal")
plt.grid(True)
plt.title("Simulation")
plt.pause(0.0001)
def close(self):
pass
def update_state(self, state, a, DT):
# print("Updating state")
throttle = a[0]
steer = a[1]
if steer >= MAX_STEER:
steer = MAX_STEER
elif steer <= -MAX_STEER:
steer = -MAX_STEER
if throttle > MAX_SPEED:
throttle = MAX_SPEED
elif throttle < MIN_SPEED:
throttle = MIN_SPEED
state[0] = state[0] + throttle * math.cos(state[2]) * DT
state[1] = state[1] + throttle * math.sin(state[2]) * DT
state[2] = state[2] + throttle / WB * math.tan(steer) * DT
return state
def plot_car(self, cabcolor="-r", truckcolor="-k"): # pragma: no cover
# print("Plotting Car")
x = self.pose[0]*MAX_X #self.pose[0]
y = self.pose[1]*MAX_Y #self.pose[1]
yaw = self.pose[2] #self.pose[2]
steer = self.action[1]*MAX_STEER #self.action[1]
outline = np.array([[-BACKTOWHEEL, (LENGTH - BACKTOWHEEL), (LENGTH - BACKTOWHEEL), -BACKTOWHEEL, -BACKTOWHEEL],
[WIDTH / 2, WIDTH / 2, - WIDTH / 2, -WIDTH / 2, WIDTH / 2]])
fr_wheel = np.array([[WHEEL_LEN, -WHEEL_LEN, -WHEEL_LEN, WHEEL_LEN, WHEEL_LEN],
[-WHEEL_WIDTH - TREAD, -WHEEL_WIDTH - TREAD, WHEEL_WIDTH - TREAD, WHEEL_WIDTH - TREAD, -WHEEL_WIDTH - TREAD]])
rr_wheel = np.copy(fr_wheel)
fl_wheel = np.copy(fr_wheel)
fl_wheel[1, :] *= -1
rl_wheel = np.copy(rr_wheel)
rl_wheel[1, :] *= -1
Rot1 = np.array([[math.cos(yaw), math.sin(yaw)],
[-math.sin(yaw), math.cos(yaw)]])
Rot2 = np.array([[math.cos(steer), math.sin(steer)],
[-math.sin(steer), math.cos(steer)]])
fr_wheel = (fr_wheel.T.dot(Rot2)).T
fl_wheel = (fl_wheel.T.dot(Rot2)).T
fr_wheel[0, :] += WB
fl_wheel[0, :] += WB
fr_wheel = (fr_wheel.T.dot(Rot1)).T
fl_wheel = (fl_wheel.T.dot(Rot1)).T
outline = (outline.T.dot(Rot1)).T
rr_wheel = (rr_wheel.T.dot(Rot1)).T
rl_wheel = (rl_wheel.T.dot(Rot1)).T
outline[0, :] += x
outline[1, :] += y
fr_wheel[0, :] += x
fr_wheel[1, :] += y
rr_wheel[0, :] += x
rr_wheel[1, :] += y
fl_wheel[0, :] += x
fl_wheel[1, :] += y
rl_wheel[0, :] += x
rl_wheel[1, :] += y
plt.plot(np.array(outline[0, :]).flatten(),
np.array(outline[1, :]).flatten(), truckcolor)
plt.plot(np.array(fr_wheel[0, :]).flatten(),
np.array(fr_wheel[1, :]).flatten(), truckcolor)
plt.plot(np.array(rr_wheel[0, :]).flatten(),
np.array(rr_wheel[1, :]).flatten(), truckcolor)
plt.plot(np.array(fl_wheel[0, :]).flatten(),
np.array(fl_wheel[1, :]).flatten(), truckcolor)
plt.plot(np.array(rl_wheel[0, :]).flatten(),
np.array(rl_wheel[1, :]).flatten(), truckcolor)
plt.plot(x, y, "*")
def main():
start_point = [0., 0., 1.57]
target_point = [0., 5., 1.57]
env = DubinGym(start_point, target_point)
max_steps = int(1e6)
state = env.reset()
env.render()
for ep in range(5):
state = env.reset()
env.render()
for i in range(max_steps):
action = [1.0, 0.]
n_state,reward,done,info = env.step(action)
env.render()
if done:
state = env.reset()
done = False
break
if __name__ == '__main__':
main()