-
Notifications
You must be signed in to change notification settings - Fork 0
/
dwa_simulation.py
421 lines (317 loc) · 12.7 KB
/
dwa_simulation.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
# controller
import numpy as np
np.seterr(divide='ignore', invalid='ignore')
import matplotlib.pyplot as plt
import pandas as pd
from animation.Animation_robot import Animation_robot
import math
import sys
# 基本関数
# 正規化
def min_max_normalize(data):
data = np.array(data)
max_data = max(data)
min_data = min(data)
if max_data - min_data == 0:
data = [0.0 for i in range(len(data))]
else:
data = (data - min_data) / (max_data - min_data)
return data
# 角度補正用
def angle_range_corrector(angle):
if angle > math.pi:
while angle > math.pi:
angle -= 2 * math.pi
elif angle < -math.pi:
while angle < -math.pi:
angle += 2 * math.pi
return angle
# 円を書く
def write_circle(center_x, center_y, angle, circle_size=0.2):#人の大きさは半径15cm
# 初期化
circle_x = [] #位置を表す円のx
circle_y = [] #位置を表す円のy
steps = 100 #円を書く分解能はこの程度で大丈夫
for i in range(steps):
circle_x.append(center_x + circle_size*math.cos(i*2*math.pi/steps))
circle_y.append(center_y + circle_size*math.sin(i*2*math.pi/steps))
circle_line_x = [center_x, center_x + math.cos(angle) * circle_size]
circle_line_y = [center_y, center_y + math.sin(angle) * circle_size]
return circle_x, circle_y, circle_line_x, circle_line_y
# ルール
# x, y, thは基本的に今のstate
# g_ はgoal
# traj_ は過去の軌跡
# 単位は,角度はrad,位置はm
# 二輪モデルなので入力は速度と角速度
# path
class Path():
def __init__(self, u_th, u_v):
self.x = None
self.y = None
self.th = None
self.u_v = u_v
self.u_th = u_th
class Obstacle():
def __init__(self, x, y, size):
self.x = x
self.y = y
self.size = size
class Two_wheeled_robot(): # 実際のロボット
def __init__(self, init_x, init_y, init_th):
# 初期状態
self.x = init_x
self.y = init_y
self.th = init_th
self.u_v = 0.0
self.u_th = 0.0
# 時刻歴保存用
self.traj_x = [init_x]
self.traj_y = [init_y]
self.traj_th = [init_th]
self.traj_u_v = [0.0]
self.traj_u_th = [0.0]
def update_state(self, u_th, u_v, dt): # stateを更新
self.u_th = u_th
self.u_v = u_v
next_x = self.u_v * math.cos(self.th) * dt + self.x
next_y = self.u_v * math.sin(self.th) * dt + self.y
next_th = self.u_th * dt + self.th
self.traj_x.append(next_x)
self.traj_y.append(next_y)
self.traj_th.append(next_th)
self.x = next_x
self.y = next_y
self.th = next_th
return self.x, self.y, self.th # stateを更新
class Simulator_DWA_robot(): # DWAのシミュレータ用
def __init__(self):
# self.model 独立二輪型
# 加速度制限
self.max_accelation = 1.0
self.max_ang_accelation = 100 * math.pi /180
# 速度制限
self.lim_max_velo = 1.6 # m/s
self.lim_min_velo = 0.0 # m/s
self.lim_max_ang_velo = math.pi
self.lim_min_ang_velo = -math.pi
# 予想状態を作成する
def predict_state(self, ang_velo, velo, x, y, th, dt, pre_step): # DWA用(何秒後かのstateを予測))
next_xs = []
next_ys = []
next_ths = []
for i in range(pre_step):
temp_x = velo * math.cos(th) * dt + x
temp_y = velo * math.sin(th) * dt + y
temp_th = ang_velo * dt + th
next_xs.append(temp_x)
next_ys.append(temp_y)
next_ths.append(temp_th)
x = temp_x
y = temp_y
th = temp_th
# print('next_xs = {0}'.format(next_xs))
return next_xs, next_ys, next_ths # 予想した軌跡
# DWA
class DWA():
def __init__(self):
# 初期化
# simulation用のロボット
self.simu_robot = Simulator_DWA_robot()
# 予測時間(s)
self.pre_time = 3
self.pre_step = 30
# 探索時の刻み幅
self.delta_velo = 0.02
self.delta_ang_velo = 0.02
# サンプリングタイム(変更の場合,共通する項があるので,必ずほかのところも確認する)
self.samplingtime = 0.1
# 重みづけ
self.weight_angle = 0.04
self.weight_velo = 0.2
self.weight_obs = 0.1
# すべてのPathを保存
self.traj_paths = []
self.traj_opt = []
def calc_input(self, g_x, g_y, state, obstacles): # stateはロボットクラスでくる
# Path作成
paths = self._make_path(state)
# Path評価
opt_path = self._eval_path(paths, g_x, g_y, state, obstacles)
self.traj_opt.append(opt_path)
return paths, opt_path
def _make_path(self, state):
# 角度と速度の範囲算出
min_ang_velo, max_ang_velo, min_velo, max_velo = self._calc_range_velos(state)
# 全てのpathのリスト
paths = []
# 角速度と速度の組み合わせを全探索
for ang_velo in np.arange(min_ang_velo, max_ang_velo, self.delta_ang_velo):
for velo in np.arange(min_velo, max_velo, self.delta_velo):
path = Path(ang_velo, velo)
next_x, next_y, next_th \
= self.simu_robot.predict_state(ang_velo, velo, state.x, state.y, state.th, self.samplingtime, self.pre_step)
path.x = next_x
path.y = next_y
path.th = next_th
# 作ったpathを追加
paths.append(path)
# 時刻歴Pathを保存
self.traj_paths.append(paths)
return paths
def _calc_range_velos(self, state): # 角速度と角度の範囲決定①
# 角速度
range_ang_velo = self.samplingtime * self.simu_robot.max_ang_accelation
min_ang_velo = state.u_th - range_ang_velo
max_ang_velo = state.u_th + range_ang_velo
# 最小値
if min_ang_velo < self.simu_robot.lim_min_ang_velo:
min_ang_velo = self.simu_robot.lim_min_ang_velo
# 最大値
if max_ang_velo > self.simu_robot.lim_max_ang_velo:
max_ang_velo = self.simu_robot.lim_max_ang_velo
# 速度
range_velo = self.samplingtime * self.simu_robot.max_accelation
min_velo = state.u_v - range_velo
max_velo = state.u_v + range_velo
# 最小値
if min_velo < self.simu_robot.lim_min_velo:
min_velo = self.simu_robot.lim_min_velo
# 最大値
if max_velo > self.simu_robot.lim_max_velo:
max_velo = self.simu_robot.lim_max_velo
return min_ang_velo, max_ang_velo, min_velo, max_velo
def _eval_path(self, paths, g_x, g_y, state, obastacles):
# 一番近い障害物判定
nearest_obs = self._calc_nearest_obs(state, obastacles)
score_heading_angles = []
score_heading_velos = []
score_obstacles = []
# 全てのpathで評価を検索
for path in paths:
# (1) heading_angle
score_heading_angles.append(self._heading_angle(path, g_x, g_y))
# (2) heading_velo
score_heading_velos.append(self._heading_velo(path))
# (3) obstacle
score_obstacles.append(self._obstacle(path, nearest_obs))
# print('angle = {0}'.format(score_heading_angles))
# print('velo = {0}'.format(score_heading_velos))
# print('obs = {0}'.format(score_obstacles))
# 正規化
for scores in [score_heading_angles, score_heading_velos, score_obstacles]:
scores = min_max_normalize(scores)
score = 0.0
# 最小pathを探索
for k in range(len(paths)):
temp_score = 0.0
temp_score = self.weight_angle * score_heading_angles[k] + \
self.weight_velo * score_heading_velos[k] + \
self.weight_obs * score_obstacles[k]
if temp_score > score:
opt_path = paths[k]
score = temp_score
return opt_path
def _heading_angle(self, path, g_x, g_y): # ゴールに向いているか
# 終端の向き
last_x = path.x[-1]
last_y = path.y[-1]
last_th = path.th[-1]
# 角度計算
angle_to_goal = math.atan2(g_y-last_y, g_x-last_x)
# score計算
score_angle = angle_to_goal - last_th
# ぐるぐる防止
score_angle = abs(angle_range_corrector(score_angle))
# 最大と最小をひっくり返す
score_angle = math.pi - score_angle
# print('score_sngle = {0}' .format(score_angle))
return score_angle
def _heading_velo(self, path): # 速く進んでいるか(直進)
score_heading_velo = path.u_v
return score_heading_velo
def _calc_nearest_obs(self, state, obstacles):
area_dis_to_obs = 5 # パラメータ(何メートル考慮するか,本当は制動距離)
nearest_obs = [] # あるエリアに入ってる障害物
for obs in obstacles:
temp_dis_to_obs = math.sqrt((state.x - obs.x) ** 2 + (state.y - obs.y) ** 2)
if temp_dis_to_obs < area_dis_to_obs :
nearest_obs.append(obs)
return nearest_obs
def _obstacle(self, path, nearest_obs):
# 障害物回避(エリアに入ったらその線は使わない)/ (障害物ともっとも近い距離距離)))
score_obstacle = 2
temp_dis_to_obs = 0.0
for i in range(len(path.x)):
for obs in nearest_obs:
temp_dis_to_obs = math.sqrt((path.x[i] - obs.x) * (path.x[i] - obs.x) + (path.y[i] - obs.y) * (path.y[i] - obs.y))
if temp_dis_to_obs < score_obstacle:
score_obstacle = temp_dis_to_obs # 一番近いところ
# そもそも中に入ってる判定
if temp_dis_to_obs < obs.size + 0.75: # マージン
score_obstacle = -float('inf')
break
else:
continue
break
return score_obstacle
class Const_goal():# goal作成プログラム
def __init__(self):
# self.human_trajectory = ...的な
self.traj_g_x = []
self.traj_g_y = []
def calc_goal(self, time_step): # 本当は人の値が入ってもよいかも
if time_step <= 100:
g_x = 10.0
g_y = 10.0
else:
g_x = -10.0
g_y = -10.0
self.traj_g_x.append(g_x)
self.traj_g_y.append(g_y)
return g_x, g_y
class Main_controller():# Mainの制御クラス
def __init__(self):
self.robot = Two_wheeled_robot(0.0, 0.0, 0.0)
self.goal_maker = Const_goal()
self.controller = DWA()
# 障害物(本当はレーザーの値等を使用)
self.obstacles = []
'''
obstacle_num = 3
for i in range(obstacle_num):
# x = np.random.randint(-5, 5)
# y = np.random.randint(-5, 5)
# size = np.random.randint(-5, 5)
self.obstacles.append(Obstacle(x, y, size))
'''
self.obstacles =[Obstacle(4, 1, 0.25), Obstacle(0, 4.5, 0.25), Obstacle(3, 4.5, 0.25), Obstacle(5, 3.5, 0.25), Obstacle(7.5, 9.0, 0.25)]
# ここを変えたら他もチェック
self.samplingtime = 0.1
def run_to_goal(self):
goal_flag = False
time_step = 0
while not goal_flag:
# for i in range(250):
g_x, g_y = self.goal_maker.calc_goal(time_step)
# 入力決定
paths, opt_path = self.controller.calc_input(g_x, g_y, self.robot, self.obstacles)
u_th = opt_path.u_th
u_v = opt_path.u_v
# 入力で状態更新
self.robot.update_state(u_th, u_v, self.samplingtime)
# goal判定
dis_to_goal = np.sqrt((g_x-self.robot.x)*(g_x-self.robot.x) + (g_y-self.robot.y)*(g_y-self.robot.y))
if dis_to_goal < 0.5:
goal_flag = True
time_step += 1
return self.robot.traj_x, self.robot.traj_y, self.robot.traj_th, \
self.goal_maker.traj_g_x, self.goal_maker.traj_g_y, self.controller.traj_paths, self.controller.traj_opt, self.obstacles
def main():
animation = Animation_robot()
animation.fig_set()
controller = Main_controller()
traj_x, traj_y, traj_th, traj_g_x, traj_g_y, traj_paths, traj_opt, obstacles = controller.run_to_goal()
ani = animation.func_anim_plot(traj_x, traj_y, traj_th, traj_paths, traj_g_x, traj_g_y, traj_opt, obstacles)
if __name__ == '__main__':
main()