Repository navigation
Expand file tree
/
Copy pathrosinterface.py
More file actions
executable file
·386 lines (342 loc) · 14.2 KB
/
Copy pathrosinterface.py
File metadata and controls
executable file
·386 lines (342 loc) · 14.2 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
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
#!/usr/bin/env python3
from typing import List, cast
import numpy as np
import open3d as o3d
import rospy
import tf2_ros
from costmap_converter.msg import ObstacleArrayMsg, ObstacleMsg
from geometry_msgs.msg import Point32, Pose, PoseStamped, PoseWithCovariance, Twist
from nav_msgs.msg import Odometry, Path
from people_msgs.msg import People
from sensor_msgs.msg import PointCloud
from tf.transformations import euler_from_quaternion
from tf2_ros import Buffer, TransformListener
from visualization_msgs.msg import Marker, MarkerArray
from mpc.agent import EgoAgent
from mpc.dynamic_obstacle import DynamicObstacle
from mpc.environment import ROSEnvironment
from mpc.geometry import Circle, Polygon
from mpc.obstacle import StaticObstacle
class ROSInterface:
"""
ROSInterface class to interface with ROS
Creates a node and subscribes to people messages for obstacles, and publishes commands on the /cmd_vel topic
Also subscribes to waypoint pose messages for the next goal
"""
def __init__(self):
self.environment = ROSEnvironment(
agent=EgoAgent(
id=1,
radius=0.5,
initial_position=(0, 0),
initial_orientation=np.deg2rad(90),
horizon=10,
use_warm_start=True,
planning_time_step=0.8,
linear_velocity_bounds=(0, 0.3),
angular_velocity_bounds=(-0.5, 0.5),
linear_acceleration_bounds=(-0.5, 0.5),
angular_acceleration_bounds=(-1, 1),
sensor_radius=3,
),
static_obstacles=[],
dynamic_obstacles=[],
waypoints=[],
plot=True,
)
self.counter = 0
rospy.init_node("ros_mpc_interface")
self.tfbuffer = Buffer()
self.listener = TransformListener(self.tfbuffer)
# rospy.Subscriber("/vel_pub", PeopleVelocity, self.people_callback)
rospy.Subscriber("/onboard_detector/people_velocity", People, self.people_callback)
rospy.Subscriber(
"/locomotor/VoronoiPlannerROS/voronoi_path",
Path,
self.waypoint_callback,
)
# rospy.Subscriber(
# "/costmap_converter/costmap_obstacles",
# ObstacleArrayMsg,
# self.obstacle_callback,
# )
rospy.Subscriber("/point_cloud", PointCloud, self.obstacle_callback)
rospy.Subscriber("/odom", Odometry, self.odom_callback, queue_size=1)
# rospy.Subscriber("/move_base_simple/goal", PoseStamped, self.goal_update_callback)
self.velocity_publisher = rospy.Publisher(
"wheelchair_diff/cmd_vel", Twist, queue_size=1
)
self.marker_publisher = rospy.Publisher(
"/future_states", MarkerArray, queue_size=10
)
self.static_obstacle_list = []
self.waypoints = []
# self.current_goal = None
# self.static_obstacle_list.append(static_obstacle_circle)
# self.polygon_obstacles = [
# StaticObstacle(id=i, geometry=Polygon(vertices=vertices))
# for i, vertices in enumerate(polygons)
# ]
# def goal_update_callback(self, pose: PoseStamped):
# self.current_goal = (
# pose.pose.position.x,
# pose.pose.position.y,
# euler_from_quaternion(
# [
# pose.pose.orientation.x,
# pose.pose.orientation.y,
# pose.pose.orientation.z,
# pose.pose.orientation.w,
# ]
# )[2],
# )
# self.waypoints = []
def run(self):
rate = rospy.Rate(100)
# self.environment.static_obstacles = self.polygon_obstacles
# self.environment.plotter.update_static_obstacles(self.polygon_obstacles)
while not rospy.is_shutdown():
print("=======================================")
self.environment.static_obstacles = self.static_obstacle_list
# print("No. of Obstacles: ", len(self.static_obstacle_list))
self.environment.step()
self.future_states_pub()
# print(self.environment.agent.goal_state, self.environment.agent.state)
# print(
# "Velocity",
# self.environment.agent.linear_velocity,
# self.environment.agent.angular_velocity,
# )
# Publish the control command
control_command = Twist()
control_command.linear.x = self.environment.agent.linear_velocity
control_command.angular.z = self.environment.agent.angular_velocity
print(control_command.linear.x, control_command.angular.z)
self.velocity_publisher.publish(control_command)
rate.sleep()
def future_states_pub(self):
marker_array = MarkerArray()
future_states = self.environment.agent.states_matrix
i = 0
for state in future_states.T:
marker = Marker()
marker.header.frame_id = "map"
marker.header.stamp = rospy.Time.now()
marker.type = Marker.SPHERE
marker.action = Marker.ADD
marker.id = i
marker.pose.position.x = state[0]
marker.pose.position.y = state[1]
marker.pose.position.z = 0
marker.pose.orientation.x = 0.0
marker.pose.orientation.y = 0.0
marker.pose.orientation.z = 0.0
marker.pose.orientation.w = 1.0
marker.scale.x = 0.05
marker.scale.y = 0.05
marker.scale.z = 0.05
marker.color.a = 1.0
marker.color.r = 0.0
marker.color.g = 1.0
marker.color.b = 1.0
marker_array.markers.append(marker)
i += 1
# print(marker_array.markers)
# print(len(marker_array.markers))
self.marker_publisher.publish(marker_array)
def odom_callback(self, message: Odometry):
try:
trans = self.tfbuffer.lookup_transform("map", "base_link", rospy.Time())
self.environment.agent.initial_state = np.array(
[
trans.transform.translation.x,
trans.transform.translation.y,
euler_from_quaternion(
[
trans.transform.rotation.x,
trans.transform.rotation.y,
trans.transform.rotation.z,
trans.transform.rotation.w,
]
)[2],
]
)
self.environment.agent.reset(matrices_only=True)
except (
tf2_ros.LookupException,
tf2_ros.ConnectivityException,
tf2_ros.ExtrapolationException,
):
pass
# Update the agent's state with the current position and orientation
# self.environment.agent.initial_state = np.array(
# [
# message.pose.pose.position.x,
# message.pose.pose.position.y,
# euler_from_quaternion(
# [
# message.pose.pose.orientation.x,
# message.pose.pose.orientation.y,
# message.pose.pose.orientation.z,
# message.pose.pose.orientation.w,
# ]
# )[2],
# ]
# )
# self.environment.agent.reset(matrices_only=True)
# def obstacle_callback(self, message: ObstacleArrayMsg):
# if self.counter == 0:
# self.static_obstacle_list = []
# for obstacle in message.obstacles:
# obstacle: ObstacleMsg
# # Create a static obstacle for each polygon
# if len(obstacle.polygon.points[:-1]) > 2:
# points = [
# (point.x, point.y)
# for point in cast(List[Point32], obstacle.polygon.points[:-1])
# ]
# else:
# continue
# self.static_obstacle_list.append(
# StaticObstacle(
# id=obstacle.id,
# geometry=Polygon(
# # id=obstacle.id,
# vertices=points,
# ),
# )
# )
# self.counter += 1
# else:
# pass
# self.environment.plotter.update_static_obstacles(static_obstacle_list)
def _process_point_cloud(self, point_cloud: np.ndarray):
max_points = 500
output_point_cloud = np.zeros((2, max_points))
if point_cloud.shape[0] == 0 or point_cloud.shape[1] == 0:
return output_point_cloud
# output_point_cloud = point_cloud[:, :2][np.argsort(np.linalg.norm(point_cloud, axis=-1), axis=-1)][
# : self.max_projection_static_obstacles, :2
# ]
# return output_point_cloud.T
pcd = o3d.geometry.PointCloud()
static_obstacles = np.stack(
(point_cloud[:, 0], point_cloud[:, 1], np.zeros_like(point_cloud[:, 0]))
)
pcd.points = o3d.utility.Vector3dVector(static_obstacles.T)
downsampled_pcd = pcd.voxel_down_sample(voxel_size=0.16)
pcd_array = np.asarray(downsampled_pcd.points)
pcd_array = pcd_array[:, :2][
np.argsort(np.linalg.norm(pcd_array, axis=-1), axis=-1)
][:max_points]
return pcd_array.T
def obstacle_callback(self, message: PointCloud):
if self.counter == 0:
point_cloud_array = []
points = message.points if message.points else []
for point in points:
point_cloud_array.append([point.x, point.y, 0])
point_cloud = self._process_point_cloud(np.array(point_cloud_array))
self.static_obstacle_list = []
for i, point in enumerate(point_cloud.T):
self.static_obstacle_list.append(
StaticObstacle(
id=i,
geometry=Circle(
center=(point[0], point[1]),
radius=0.1,
),
)
)
self.counter += 1
else:
pass
def people_callback(self, message: People):
print("people_callback invoked")
dynamic_obstacle_list: List[DynamicObstacle] = []
for i, person in enumerate(message.people):
# person.position and person.velocity come from people_msgs::Person
dynamic_obstacle_list.append(
DynamicObstacle(
id=i,
position=(person.position.x, person.position.y),
orientation=np.arctan2(person.velocity.y, person.velocity.x),
linear_velocity=(person.velocity.x**2 + person.velocity.y**2)**0.5,
angular_velocity=0,
horizon=10,
)
)
# rospy.loginfo("Dynamic Obstacle %d: pos=(%.2f, %.2f), vel=(%.2f, %.2f)", i, person.position.x, person.position.y, person.velocity.x, person.velocity.y)
self.environment.dynamic_obstacles = dynamic_obstacle_list
print("---")
for obstacle in dynamic_obstacle_list:
print(obstacle.state)
print("---")
def waypoint_callback(self, message: Path):
# Update the agent's goal with the waypoint position
# if message.header.seq == 0:
# if self.environment.final_goal_reached:
# self.waypoints = []
try:
diff = np.array(self.waypoints[-1]) - np.array(
(
message.poses[-1].pose.position.x,
message.poses[-1].pose.position.y,
euler_from_quaternion(
[
message.poses[-1].pose.orientation.x,
message.poses[-1].pose.orientation.y,
message.poses[-1].pose.orientation.z,
message.poses[-1].pose.orientation.w,
]
)[2],
)
)
print(diff, diff.sum())
diff = diff.sum()
except:
diff = 0
if self.waypoints == [] or abs(diff) > 0.1:
print("Updating waypoints")
waypoints = [
(
pose.pose.position.x,
pose.pose.position.y,
euler_from_quaternion(
[
pose.pose.orientation.x,
pose.pose.orientation.y,
pose.pose.orientation.z,
pose.pose.orientation.w,
]
)[2],
)
for pose in message.poses[::30]
]
# waypoints = []
# print("Length of waypoints",len(waypoints))
# orientation_euler = euler_from_quaternion((0, 0, message.poses[-1].pose.orientation, 0))
waypoints.append(
(
message.poses[-1].pose.position.x,
message.poses[-1].pose.position.y,
euler_from_quaternion(
[
message.poses[-1].pose.orientation.x,
message.poses[-1].pose.orientation.y,
message.poses[-1].pose.orientation.z,
message.poses[-1].pose.orientation.w,
]
)[2],
)
)
# waypoints.append(self.current_goal)
self.waypoints = waypoints
print("Waypoints", waypoints)
self.environment.waypoints = np.array(waypoints)
self.environment.waypoint_index = 0
self.environment.agent.update_goal(self.environment.current_waypoint)
# self.environment.plotter.update_goal(self.environment.waypoints)
if __name__ == "__main__":
ros_interface = ROSInterface()
ros_interface.run()