-
Notifications
You must be signed in to change notification settings - Fork 0
/
rosview.py
executable file
·287 lines (212 loc) · 7.85 KB
/
rosview.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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Author: Ozer Ozkahraman (ozkahramanozer@gmail.com)
# Date: 2018-05-24
import rospy
from geometry_msgs.msg import Pose, Point, Quaternion, PoseStamped
from visualization_msgs.msg import Marker, MarkerArray
class RosPoseView:
def __init__(self,
body,
topic,
mesh_path=None,
mesh_scale=(1,1,1),
mesh_rgba=(0,0,0,1)):
self.body = body
self.pub = rospy.Publisher(topic, PoseStamped, queue_size=1)
# prepare the published stuff
# these will simply be updated and not re-created
point = Point()
quat = Quaternion()
pose = Pose()
pose.position = point
pose.orientation = quat
self.pose = pose
self.pose_stamped = PoseStamped()
self.pose_stamped.header.frame_id = '/world'
# start with the current state of the body
self._update_pose()
def _update_pose(self):
x,y,z = self.body.get_position()
self.pose.position.x = x
self.pose.position.y = y
self.pose.position.z = z
x,y,z,w = self.body.get_orientation_quat()
self.pose.orientation.x = x
self.pose.orientation.y = y
self.pose.orientation.z = z
self.pose.orientation.w = w
self.pose_stamped.header.stamp = rospy.Time.now()
self.pose_stamped.pose = self.pose
def update(self):
self._update_pose()
self.pub.publish(self.pose_stamped)
class RosCubeView:
def __init__(self, id, pose_view, scale, rgba):
self.id = id
self.pub = rospy.Publisher('/rviz_cube_'+str(id), Marker, queue_size=1)
self.pose_view = pose_view
marker = Marker()
marker.ns = 'cube'
marker.id = self.id
marker.action = 0
marker.type = 1 # cube
marker.pose = self.pose_view.pose
x,y,z = scale
marker.scale.x = x
marker.scale.y = y
marker.scale.z = z
r,g,b,a = rgba
marker.color.a = a
marker.color.r = r
marker.color.g = g
marker.color.b = b
marker.header.frame_id = '/world'
self.marker = marker
def update(self):
self.pub.publish(self.marker)
class RosMarkerArrayView:
def __init__(self, namespace='array_item'):
self.last_used_id = 0
self.pub = rospy.Publisher('/rviz_marker_array', MarkerArray, queue_size=1)
self.marker_array = MarkerArray()
self.ros_pose_viewers = []
self.namespace = namespace
self.mesh_paths = []
self.mesh_scales = []
self.mesh_rgbas = []
def add_view(self, pose_view, mesh_path, mesh_scale, mesh_rgba):
self.ros_pose_viewers.append(pose_view)
self.mesh_paths.append(mesh_path)
self.mesh_scales.append(mesh_scale)
self.mesh_rgbas.append(mesh_rgba)
def init(self):
for poseview,mesh_path,mesh_scale,mesh_rgba in zip(self.ros_pose_viewers,
self.mesh_paths,
self.mesh_scales,
self.mesh_rgbas):
marker = Marker()
marker.ns = self.namespace
marker.id = self.last_used_id+1
self.last_used_id += 1
marker.action = 0
# 10 for mesh
marker.type = 10
marker.pose = poseview.pose
x,y,z = mesh_scale
marker.scale.x = x
marker.scale.y = y
marker.scale.z = z
r,g,b,a = mesh_rgba
marker.color.a = a
marker.color.r = r
marker.color.g = g
marker.color.b = b
marker.mesh_resource = 'file://'+mesh_path
marker.header.frame_id = '/world'
self.marker_array.markers.append(marker)
def update(self):
self.pub.publish(self.marker_array)
class RosSwarmView:
def __init__(self,
swarm,
mesh_path,
mesh_rgba,
mesh_scale,
namespace='swarm_item',
last_used_id=0):
"""
Similar to marker array view, but this assumes that the given array of stuff
are all identical.
swarm needs to have get_positions() and get_orientation_quats() functions.
if more than 1 swarm view is created, take care of last_used_id!
"""
self.swarm = swarm
self.last_used_id = last_used_id
self.pub = rospy.Publisher('/rviz_swarm', MarkerArray, queue_size=1)
# create these ahead of time, just need to update before publishing
self.marker_array = MarkerArray()
self.poses = []
for i in range(len(swarm.get_position())):
point = Point()
quat = Quaternion()
pose = Pose()
pose.position = point
pose.orientation = quat
marker = Marker()
marker.ns = namespace
marker.id = self.last_used_id+1
self.last_used_id += 1
marker.action = 0
# 10 for mesh
marker.type = 10
marker.pose = pose
x,y,z = mesh_scale
marker.scale.x = x
marker.scale.y = y
marker.scale.z = z
r,g,b,a = mesh_rgba
marker.color.a = a
marker.color.r = r
marker.color.g = g
marker.color.b = b
marker.mesh_resource = 'file://'+mesh_path
marker.header.frame_id = '/world'
self.marker_array.markers.append(marker)
def update(self, frame_locked=False, frame=None):
for pos,quat,marker in zip(self.swarm.get_position(),
self.swarm.get_orientation_quat(),
self.marker_array.markers):
marker.pose.position.x = pos[0]
marker.pose.position.y = pos[1]
marker.pose.position.z = pos[2]
marker.pose.orientation.x = quat[0]
marker.pose.orientation.y = quat[1]
marker.pose.orientation.z = quat[2]
marker.pose.orientation.w = quat[3]
if frame is not None and frame_locked:
marker.frame_locked=frame_locked
marker.header.seq = frame
self.pub.publish(self.marker_array)
if __name__=='__main__':
rospy.init_node('rosviewtest', anonymous=True)
N = 1
ups = 30
rate = rospy.Rate(ups)
from dynamic_point import PIDPoint, VelocityPoint
bodies = []
pose_views = []
for i in range(N):
pidp = PIDPoint(damping=0.05)
# pidp = VelocityPoint(speed=1)
rv = RosPoseView(body=pidp,
topic='/test'+str(i))
bodies.append(pidp)
pose_views.append(rv)
marker_array = RosMarkerArrayView(ros_pose_viewers=pose_views,
mesh_paths=['/home/ozer/suzanne.dae']*N,
mesh_rgbas=[(0.8,0.2,0.5,1)]*N,
mesh_scales=[(1,1,1)]*N)
target_view = RosPoseView(body=bodies[0].target,
topic='/target0')
i = 0
import random
bodies[0].set_target((1,1,0))
while not rospy.is_shutdown():
for body,poseview in zip(bodies,pose_views):
if i%200 == 0:
rx = -5+random.random()*10
ry = -5+random.random()*10
rz = -5+random.random()*10
body.set_target((rx,ry,rz))
body.update(1/ups)
poseview.update()
print('body:',body.pos, 't;', body.target.pos)
marker_array.update()
target_view.update()
rate.sleep()
i += 1
else:
print('rospy is_shutdown')