-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathagent.py
193 lines (161 loc) · 6.26 KB
/
agent.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
import cv2
import math
import numpy as np
from modules.Robot import Robot
from modules import PathPlanner as PathPlanner
from Cogenvdecoder.CogEnvDecoder import CogEnvDecoder
from modules import extended_kalman_filter as ex
import matplotlib.pyplot as plt
import history.kalman as his_kal
from Cogenvdecoder.CogEnvDecoder import CogEnvDecoder
# from modules.lidar_data_mapping import lidar_mapping
# from modules.lidar_data_mapping import update as lidar_update
import modules.lidar_data_mapping as lidar
lidar_mapping = lidar.lidar_mapping
lidar_update = lidar.update
np.random.seed(19260817)
goal_prec = 0.5
map_size = [8.08, 4.48]
activation_step_control = 0
debias_sum_x = 0
debias_sum_y = 0
debias_steps = 0
last_activation_tar = -1
la_fight_tar = None
fight_step_control = 0
action = [1, 1, -0.1, 0]
class Agent:
def __init__(self, model_path=None):
self.model_path = model_path
self.cleaned = False
self.robot = None
self.la_v_x = 0
self.la_v_y = 0
self.la_w = 0
self.his = []
self.emergency = False
def clear(self, obs):
ex.clear(obs['vector'][0][0], obs['vector'][0][1])
self.robot = Robot(obs)
self.la_v_x = 0
self.la_v_y = 0
self.la_w = 0
self.his = []
self.emergency = False
vector_data = obs['vector']
dynamic_obstacles = [vector_data[5][:2], vector_data[6][:2], vector_data[7][:2], vector_data[8][:2], vector_data[9][:2]]
lidar_update(dynamic_obstacles)
global debias_sum_x
global debias_sum_y
global debias_steps
global last_activation_tar
global la_fight_tar
global fight_step_control
global activation_step_control
global action
debias_sum_x = 0
debias_sum_y = 0
debias_steps = 0
last_activation_tar = -1
la_fight_tar = None
fight_step_control = 0
activation_step_control = 0
action = [1, 1, -0.1, 0]
def agent_control(self, obs, done, info):
global debias_sum_x
global debias_sum_y
global debias_steps
global last_activation_tar
global la_fight_tar
global fight_step_control
global activation_step_control
###### please don't stuck there ######
self.his.append((obs['vector'][0][0], obs['vector'][0][1]))
if len(self.his) >= 10:
self.his = self.his[-10:]
mean_x = np.array(self.his)[:, 0].mean()
mean_y = np.array(self.his)[:, 1].mean()
rmse = np.mean([math.hypot(x - mean_x, y - mean_y) for x, y in self.his])
# print('rmse', rmse)
# print('his', self.his)
if rmse <= 0.03:
if not self.emergency:
self.emergency = True
global action
action = [-x for x in action]
else:
self.emergency = False
###### please don't stuck there ######
########## reset each epoch ##########
if obs['vector'][5][2] == False and self.cleaned == False:
self.clear(obs)
self.cleaned = True
elif obs['vector'][5][2] == True:
self.cleaned = False
########## reset each epoch ##########
########## noise reduction ##########
xxx = obs['vector'][0][0]
yyy = obs['vector'][0][1]
yaw = obs['vector'][0][2]
xEst = ex.noise_reduce(xxx, yyy, yaw, math.hypot(self.la_v_x,self.la_v_y), self.la_w)
obs["vector"][0][0] = xEst[0, 0]
obs["vector"][0][1] = xEst[1, 0]
x, y = obs["vector"][0][0], obs["vector"][0][1]
obs["vector"][0][0] = min(max(x, 0.05), map_size[0] - 0.05)
obs["vector"][0][1] = min(max(y, 0.05), map_size[1] - 0.05)
laser_data = np.array(obs["laser"])
try:
x, y, check = lidar_mapping(obs['vector'], laser_data)
if not (abs(obs['vector'][0][0] - x) > 0.6 or abs(obs['vector'][0][1] - y) > 0.6):
if( check ):
debias_sum_x += obs['vector'][0][0] - x
debias_sum_y += obs['vector'][0][1] - y
debias_steps += 1
except:
pass
if debias_steps != 0:
obs["vector"][0][0] -= debias_sum_x / debias_steps
obs["vector"][0][1] -= debias_sum_y / debias_steps
print('fixed coordinate', obs['vector'][0])
print('lidar\'s status_x & status_y', lidar.status_x, lidar.status_y)
########## noise reduction ##########
############# get action #############
# global action
activation_tar = self.robot.check_activation(obs)
cu_x, cu_y = obs["vector"][0][0], obs["vector"][0][1]
if self.emergency:
action = action
last_activation_tar = -1
self.robot.random_tar = None
elif activation_tar != -1:
# print(activation_step_control)
if activation_tar != last_activation_tar or activation_step_control >= 100:
activation_step_control = 0
self.robot.update_activation_path(obs, activation_tar)
last_activation_tar = activation_tar
activation_step_control += 1
action = self.robot.get_activation_action(cu_x, cu_y)
action[2] = self.robot.get_activation_rotation(obs, activation_tar)
else:
# print(fight_step_control)
if la_fight_tar == self.robot.random_tar:
fight_step_control += 1
else:
la_fight_tar = self.robot.random_tar
fight_step_control = 1
if fight_step_control >= 50:
self.robot.random_tar = None
fight_step_control = 0
action = self.robot.get_fight_action(obs)
theta = obs["vector"][0][2] + action[2] * 0.0001
vx = action[0]
vy = action[1]
action[0] = math.cos(-theta) * vx - math.sin(-theta) * vy
action[1] = math.sin(-theta) * vx + math.cos(-theta) * vy
############# get action #############
############ remember me #############
self.la_v_x = action[0]
self.la_v_y = action[1]
self.la_w = action[2]
############ remember me #############
return action