-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript_mirror.py
More file actions
186 lines (164 loc) · 6.66 KB
/
script_mirror.py
File metadata and controls
186 lines (164 loc) · 6.66 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
from lib import rapinterface as ri, dynamic_window_approach as dw
import matplotlib.pyplot as plt
import numpy as np
import math
sensor_pos = [(0.10638160980,-0.1381410850,0.06811071560),(0.15552160980,-0.1202410850,0.06811871560),(0.19054160980,-0.0785010850,0.06811471560),(0.20916160980,-0.0272730850,0.06810771560),(0.20916160980,0.0272739150,0.06810371560),(0.19058160980,0.0831339150,0.06810771560),(0.15540160980,0.1250489150,0.06811171560),(0.10638160980,0.1381989150,0.06811071560),(-0.11031839020,0.1381989150,0.06817271560),(-0.15956839020,0.1202689150,0.06816971560),(-0.19458839020,0.0784989150,0.06816771560),(-0.21320839020,0.0272729150,0.06816671560),(-0.21320839020,-0.0272760850,0.06816671560),(-0.19457839020,-0.0785010850,0.06816771560),(-0.15956839020,-0.1202410850,0.06816971560),(-0.11031839020,-0.1381510850,0.06817271560)]
sensor_ori = [270,310, 330, 350, 10, 30, 50, 90, 90, 130, 150, 170, 190, 210, 230, 270]
param = {}
config = dw.Config()
def get_ob(x):
_c, _h, status, distances, _s = ri.getDistanceCollection()
ob = []
if len(status) == 0: # nenhum objeto encontrado
return np.array([[0,0]])
for i in range(16):
if status[2*i]!=0: # verifica se aquele sensor identificou um valor válido
distance = (distances[6*i + 0]**2 + # distância euclidiana
distances[6*i + 1]**2 + #do sensor ao ponto identificado
distances[6*i + 2]**2)**(1/2)
orientation = sensor_ori[i] + x[2] # obter o angulo real do sensor
position = [x[0] + sensor_pos[i][0] + distance*math.cos(orientation),
x[1] + sensor_pos[i][1] + distance*math.sin(orientation)]
# estimar a posição real do objeto
# somar posição real do robo,
# posição relativa do sensor com o robo
# distancia dividida em coordenadas cartesianas
ob.append(position)#adicionar obstaculo identificado no vetor de obstaculos
return np.array(ob)
#return ob
def rap_motion(u):
wr = (u[0] + u[1]*config.c)/config.r
wl = (u[0] - u[1]*config.c)/config.r
ri.setvelocity(param['motorRight'], wr)
ri.setvelocity(param['motorLeft'], wl)
#ri.setvelocity(param['motorRight'], 0)
#ri.setvelocity(param['motorLeft'], 0)
ri.sleep(config.dt)
position = ri.getposition(param['robot'],param['base'])
orientation = ri.getorientation(param['robot'],param['base'])
x = [0]*5
x[0],x[1],x[2] = position[0],position[1], orientation[2]
x[3], x[4] = u
return x
def init():
param['target'] = ri.getobject("Target")
param['base'] = ri.getobject("base")
param['motorLeft'] = ri.getobject("Pioneer_p3dx_leftMotor")
param['motorRight'] = ri.getobject("Pioneer_p3dx_rightMotor")
param['robot'] = ri.getobject("Pioneer_p3dx")
param['sonic'] = ri.getcollection("sonic")
for i in range(16):
sensor_ori[i] = sensor_ori[i]*math.pi/180
position = ri.getpositionb(param['robot'],param['base'])
orientation = ri.getorientationb(param['robot'],param['base'])
goal = ri.getpositionb(param['target'],param['base'])
robot_type = dw.RobotType.circle
# initial state [x(m), y(m), yaw(rad), v(m/s), omega(rad/s)]
x = np.array([position[0], position[1], orientation[2], 0.0, 0.0])
# goal position [x(m), y(m)]
goal = np.array([goal[0], goal[1]])
# input [forward speed, yaw_rate]
config.robot_type = robot_type
config.goal = goal
param['trajectory'] = np.array(x)
param['x'] = x
config.max_speed = 0.75 # [m/s]
config.min_speed = -0.30 # [m/s]
config.max_yaw_rate = 100.0 * math.pi / 180.0 # [rad/s]
config.max_accel = 0.3 # [m/ss]
config.max_delta_yaw_rate = 100.0 * math.pi / 180.0 # [rad/ss]
config.v_resolution = 0.01 # [m/s]
config.yaw_rate_resolution = 0.1 * math.pi / 180.0 # [rad/s]
config.dt = 0.1 # [s] Time tick for motion prediction
config.predict_time = 0.5 # [s]
config.to_goal_cost_gain = 0.15
config.speed_cost_gain = 1.0
config.obstacle_cost_gain = 1.0
config.robot_stuck_flag_cons = 0.001 # constant to prevent robot stucked
# config.robot_type = dw.RobotType.circle
# # Also used to check if goal is reached in both types
config.robot_radius = 0.30 # [m] for collision check
config.robot_type = dw.RobotType.rectangle
# Also used to check if goal is reached in both types
config.robot_width = 0.38 # [m] for collision check
config.robot_length = 0.52 # [m] for collision check
config.c = 0.19 # [m] distancia do centro às rodas
config.r = 0.185# [m] raio da roda
ob = np.array([
# [2.0,0.5],
# [2.0,0.25],
# [2.5,0.7],
# [2.27,0.25],
# [0.5,1.5],
# [0.75,1.5],
# [1.0,1.5],
# [1.25,1.5],
# [1.50,1.5],
# [0.5,3.0],
# [0.75,3.0],
# [1.0,3.0],
# [1.25,3.0],
# [1.50,3.0],
# [1.5,3.25],
# [1.5,3.5],
# [1.0,3.5],
# [0.0,0.25],
# [0.0,0.5],
# [0.0,0.75],
# [0.0,1.0],
# [0.0,1.25],
# [0.0,1.5],
# [0.0,1.75],
# [0.0,2.0],
# [0.0,2.25],
# [0.0,2.5],
# [0.0,2.75],
# [0.0,3.0],
# [0.0,3.25],
# [0.0,3.5],
# [0.0,3.75],
# [0.0,4.0],
# [0.0,4.25],
# [0.0,4.5],
# [0.0,4.75],
# [0.0,5.0],
])
ob = np.array([[0,0]])
def loop():
x = param['x']
# ob = get_ob(x)
u, predicted_trajectory = dw.dwa_control(x, config, config.goal, ob)
#x = dw.motion(x,u,config.dt)
x = rap_motion(u) # simulate robot
if(x[0] == 0):
return
param['x'] = x
param['trajectory'] = np.vstack((param['trajectory'], x)) # store state history
if dw.show_animation:
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(predicted_trajectory[:, 0], predicted_trajectory[:, 1], "-g")
plt.plot(x[0], x[1], "xr")
plt.plot(config.goal[0], config.goal[1], "xb")
plt.plot(ob[:, 0], ob[:, 1], "ok")
dw.plot_robot(x[0], x[1], x[2], config)
dw.plot_arrow(x[0], x[1], x[2])
plt.axis("equal")
plt.grid(True)
plt.pause(0.01)
# check reaching goal
dist_to_goal = math.hypot(x[0] - config.goal[0], x[1] - config.goal[1])
if dist_to_goal <= config.robot_radius:
print("Goal!!")
rap_motion([0,0])
return True
def finnaly():
print("Done")
if dw.show_animation:
plt.plot(param['trajectory'][:, 0], param['trajectory'][:, 1], "-r")
plt.pause(0.001)
plt.show()
ri.start(init,loop,finnaly,30)