-
Notifications
You must be signed in to change notification settings - Fork 4
/
environment_stage_1.py
219 lines (186 loc) · 7.46 KB
/
environment_stage_1.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
#coding:utf-8
#################################################################################
# Copyright 2018 ROBOTIS CO., LTD.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#################################################################################
# Authors: Gilbert #
import rospy
import numpy as np
import math
from math import pi
from geometry_msgs.msg import Twist, Point, Pose
from sensor_msgs.msg import LaserScan
from nav_msgs.msg import Odometry
from std_srvs.srv import Empty
from tf.transformations import euler_from_quaternion, quaternion_from_euler
from respawnGoal import Respawn
import copy
target_not_movable = False
class Env():
def __init__(self, action_dim=2):
self.goal_x = 0
self.goal_y = 0
self.heading = 0
self.initGoal = True
self.get_goalbox = False
self.position = Pose()
self.pub_cmd_vel = rospy.Publisher('cmd_vel', Twist, queue_size=5)
self.sub_odom = rospy.Subscriber('odom', Odometry, self.getOdometry)
self.reset_proxy = rospy.ServiceProxy('gazebo/reset_simulation', Empty)
self.unpause_proxy = rospy.ServiceProxy('gazebo/unpause_physics', Empty)
self.pause_proxy = rospy.ServiceProxy('gazebo/pause_physics', Empty)
self.respawn_goal = Respawn()
self.past_distance = 0.
self.stopped = 0
self.action_dim = action_dim
#Keys CTRL + c will stop script
rospy.on_shutdown(self.shutdown)
#停止运动
def shutdown(self):
#you can stop turtlebot by publishing an empty Twist
#message
rospy.loginfo("Stopping TurtleBot")
self.pub_cmd_vel.publish(Twist())
rospy.sleep(1)
#获取目标距离
def getGoalDistace(self):
goal_distance = round(math.hypot(self.goal_x - self.position.x, self.goal_y - self.position.y), 2)
self.past_distance = goal_distance
return goal_distance
#获得里程计数据
def getOdometry(self, odom):
self.past_position = copy.deepcopy(self.position)
self.position = odom.pose.pose.position
orientation = odom.pose.pose.orientation
orientation_list = [orientation.x, orientation.y, orientation.z, orientation.w]
_, _, yaw = euler_from_quaternion(orientation_list)
goal_angle = math.atan2(self.goal_y - self.position.y, self.goal_x - self.position.x)
#print 'yaw', yaw
#print 'gA', goal_angle
heading = goal_angle - yaw
#print 'heading', heading
if heading > pi:
heading -= 2 * pi
elif heading < -pi:
heading += 2 * pi
self.heading = round(heading, 3)
#获取激光雷达数据
def getState(self, scan, past_action):
scan_range = []
heading = self.heading
min_range = 0.136
done = False
for i in range(len(scan.ranges)):
if scan.ranges[i] == float('Inf') or scan.ranges[i] == float('inf'):
scan_range.append(3.5)
elif np.isnan(scan.ranges[i]) or scan.ranges[i] == float('nan'):
scan_range.append(0)
else:
scan_range.append(scan.ranges[i])
if min_range > min(scan_range) > 0:
done = True
for pa in past_action:
scan_range.append(pa)
current_distance = round(math.hypot(self.goal_x - self.position.x, self.goal_y - self.position.y),2)
# current_distance = self.getGoalDistace()
if current_distance < 0.15:
self.get_goalbox = True
return scan_range + [heading, current_distance], done
#奖励设置
def setReward(self, state, done):
current_distance = state[-1]
heading = state[-2]
#print('cur:', current_distance, self.past_distance)
distance_rate = (self.past_distance - current_distance)
if distance_rate > 0:
# reward = 200.*distance_rate
reward = 2.
# if distance_rate == 0:
# reward = 0.
if distance_rate <= 0:
# reward = -8.
reward = -2.
#angle_reward = math.pi - abs(heading)
#print('d', 500*distance_rate)
#reward = 500.*distance_rate #+ 3.*angle_reward
self.past_distance = current_distance
a, b, c, d = float('{0:.3f}'.format(self.position.x)), float('{0:.3f}'.format(self.past_position.x)), float('{0:.3f}'.format(self.position.y)), float('{0:.3f}'.format(self.past_position.y))
if a == b and c == d:
# rospy.loginfo('\n<<<<<Stopped>>>>>\n')
# print('\n' + str(a) + ' ' + str(b) + ' ' + str(c) + ' ' + str(d) + '\n')
self.stopped += 1
if self.stopped == 20:
rospy.loginfo('Robot is in the same 20 times in a row')
self.stopped = 0
done = True
else:
# rospy.loginfo('\n>>>>> not stopped>>>>>\n')
self.stopped = 0
if done:
rospy.loginfo("Collision!!")
# reward = -500.
reward = -10.
self.pub_cmd_vel.publish(Twist())
if self.get_goalbox:
rospy.loginfo("Goal!!")
# reward = 500.
reward = 100.
self.pub_cmd_vel.publish(Twist())
# if world:
# self.goal_x, self.goal_y = self.respawn_goal.getPosition(True, delete=True, running=True)
# if target_not_movable:
# self.reset()
# else:
self.goal_x, self.goal_y = self.respawn_goal.getPosition(True, delete=True)
self.goal_distance = self.getGoalDistace()
self.get_goalbox = False
return reward, done
#
def step(self, action, past_action):
linear_vel = action[0]
ang_vel = action[1]
vel_cmd = Twist()
vel_cmd.linear.x = linear_vel
vel_cmd.angular.z = ang_vel
self.pub_cmd_vel.publish(vel_cmd)
data = None
while data is None:
try:
data = rospy.wait_for_message('scan', LaserScan, timeout=5)
except:
pass
state, done = self.getState(data, past_action)
reward, done = self.setReward(state, done)
return np.asarray(state), reward, done
def reset(self):
#print('aqui2_____________---')
rospy.wait_for_service('gazebo/reset_simulation')
try:
self.reset_proxy()
except (rospy.ServiceException) as e:
print("gazebo/reset_simulation service call failed")
data = None
while data is None:
try:
data = rospy.wait_for_message('scan', LaserScan, timeout=50)
except:
pass
if self.initGoal:
self.goal_x, self.goal_y = self.respawn_goal.getPosition()
self.initGoal = False
else:
self.goal_x, self.goal_y = self.respawn_goal.getPosition(True, delete=True)
self.goal_distance = self.getGoalDistace()
state, _ = self.getState(data, [0]*self.action_dim)
return np.asarray(state)