transformable_markers_client.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 import os.path as osp
4 import sys
5 
6 import yaml
7 
8 from geometry_msgs.msg import Vector3
9 from geometry_msgs.msg import Quaternion
10 from geometry_msgs.msg import Pose
11 from jsk_rviz_plugins.srv import RequestMarkerOperate
12 from jsk_rviz_plugins.msg import TransformableMarkerOperate
13 from jsk_interactive_marker.msg import MarkerDimensions
14 from jsk_interactive_marker.msg import PoseStampedWithName
15 from jsk_interactive_marker.srv import GetTransformableMarkerFocus
16 from jsk_interactive_marker.srv import GetTransformableMarkerFocusRequest
17 from jsk_interactive_marker.srv import SetTransformableMarkerPose
18 from jsk_interactive_marker.srv import SetTransformableMarkerPoseRequest
19 from jsk_interactive_marker.srv import SetTransformableMarkerColor
20 from jsk_interactive_marker.srv import SetTransformableMarkerColorRequest
21 from jsk_interactive_marker.srv import SetMarkerDimensions
22 from jsk_interactive_marker.srv import SetMarkerDimensionsRequest
23 from jsk_recognition_utils.color import labelcolormap
24 from jsk_recognition_msgs.msg import BoundingBox
25 from jsk_recognition_msgs.msg import BoundingBoxArray
26 import rospy
27 
28 
29 # Python2's xrange equals Python3's range, and xrange is removed on Python3
30 if not hasattr(__builtins__, 'xrange'):
31  xrange = range
32 
33 
35 
36  def __init__(self):
37  self.server = rospy.resolve_name('~server')
38 
39  self.config_file = rospy.get_param('~config_file')
40  if not osp.exists(self.config_file):
41  rospy.logfatal("config_file '{}' does not exist"
42  .format(self.config_file))
43  sys.exit(1)
44  self.config = yaml.safe_load(open(self.config_file))
45 
46  self.req_marker = rospy.ServiceProxy(
47  osp.join(self.server, 'request_marker_operate'),
48  RequestMarkerOperate)
49  self.req_color = rospy.ServiceProxy(
50  osp.join(self.server, 'set_color'), SetTransformableMarkerColor)
51  self.req_pose = rospy.ServiceProxy(
52  osp.join(self.server, 'set_pose'), SetTransformableMarkerPose)
53  self.req_dim = rospy.ServiceProxy(
54  osp.join(self.server, 'set_dimensions'), SetMarkerDimensions)
55  self.req_focus = rospy.ServiceProxy(
56  osp.join(self.server, 'get_focus'),
57  GetTransformableMarkerFocus)
58  rospy.wait_for_service(self.req_marker.resolved_name)
59  rospy.wait_for_service(self.req_color.resolved_name)
60  rospy.wait_for_service(self.req_pose.resolved_name)
61  rospy.wait_for_service(self.req_dim.resolved_name)
62  rospy.wait_for_service(self.req_focus.resolved_name)
63 
64  self.object_poses = {}
66 
67  # TODO: support other than box: ex. torus, cylinder, mesh
68 
69  # insert box
70  boxes = self.config['boxes']
71  n_boxes = len(boxes)
72  cmap = labelcolormap(n_boxes)
73  for i in xrange(n_boxes):
74  box = boxes[i]
75  name = box['name']
76  frame_id = box.get('frame_id', '/map')
77  self.insert_marker(name, frame_id,
78  type=TransformableMarkerOperate.BOX)
79  self.set_color(name, (cmap[i][0], cmap[i][1], cmap[i][2], 0.5))
80  dim = box.get('dimensions', [1, 1, 1])
81  self.set_dimensions(name, dim)
82  pos = box.get('position', [0, 0, 0])
83  ori = box.get('orientation', [0, 0, 0, 1])
84  self.set_pose(box['name'], box['frame_id'], pos, ori)
85  self.object_poses[box['name']] = Pose(
86  position=Vector3(*pos),
87  orientation=Quaternion(*ori),
88  )
89  self.object_dimensions[box['name']] = Vector3(*dim)
90  rospy.loginfo("Inserted transformable box '{}'.".format(name))
91 
92  self.sub_dimensions = rospy.Subscriber(
93  osp.join(self.server, 'marker_dimensions'),
94  MarkerDimensions,
96  self.sub_pose = rospy.Subscriber(
97  osp.join(self.server, 'pose_with_name'),
98  PoseStampedWithName,
100 
101  do_auto_save = rospy.get_param('~config_auto_save', True)
102  if do_auto_save:
103  self.timer_save = rospy.Timer(
104  rospy.Duration(1), self._save_callback)
105 
106  self.pub_bboxes = rospy.Publisher(
107  '~output/boxes', BoundingBoxArray, queue_size=1)
108  self.timer_pub_bboxes = rospy.Timer(
109  rospy.Duration(0.1), self._pub_bboxes_callback)
110 
111  def insert_marker(self, name, frame_id, type):
112  msg = TransformableMarkerOperate(
113  name=name,
114  type=type,
115  action=TransformableMarkerOperate.INSERT,
116  frame_id=frame_id,
117  description=name,
118  )
119  self.req_marker(msg)
120 
121  def set_color(self, name, rgba):
122  req = SetTransformableMarkerColorRequest()
123  req.target_name = name
124  req.color.r = rgba[0]
125  req.color.g = rgba[1]
126  req.color.b = rgba[2]
127  req.color.a = rgba[3]
128  self.req_color(req)
129 
130  def set_dimensions(self, name, dimensions):
131  req = SetMarkerDimensionsRequest()
132  req.target_name = name
133  req.dimensions.x = dimensions[0]
134  req.dimensions.y = dimensions[1]
135  req.dimensions.z = dimensions[2]
136  self.req_dim(req)
137 
138  def set_pose(self, name, frame_id, position, orientation):
139  req = SetTransformableMarkerPoseRequest()
140  req.target_name = name
141  req.pose_stamped.header.frame_id = frame_id
142  req.pose_stamped.pose.position.x = position[0]
143  req.pose_stamped.pose.position.y = position[1]
144  req.pose_stamped.pose.position.z = position[2]
145  req.pose_stamped.pose.orientation.x = orientation[0]
146  req.pose_stamped.pose.orientation.y = orientation[1]
147  req.pose_stamped.pose.orientation.z = orientation[2]
148  req.pose_stamped.pose.orientation.w = orientation[3]
149  self.req_pose(req)
150 
151  def get_focus_marker(self):
152  req = GetTransformableMarkerFocusRequest()
153  res = self.req_focus(req)
154  return res.target_name
155 
157  name = self.get_focus_marker()
158  self.object_dimensions[name] = msg
159 
160  def _pose_change_callback(self, msg):
161  self.object_poses[msg.name] = msg.pose.pose
162 
163  def _pub_bboxes_callback(self, event):
164  bbox_array_msg = BoundingBoxArray()
165  bbox_array_msg.header.frame_id = self.config['boxes'][0]['frame_id']
166  bbox_array_msg.header.stamp = event.current_real
167  for box in self.config['boxes']:
168  bbox_msg = BoundingBox()
169  pose = self.object_poses[box['name']]
170  bbox_msg.header.frame_id = bbox_array_msg.header.frame_id
171  bbox_msg.header.stamp = bbox_array_msg.header.stamp
172  bbox_msg.pose = pose
173  dimensions = self.object_dimensions[box['name']]
174  bbox_msg.dimensions.x = dimensions.x
175  bbox_msg.dimensions.y = dimensions.y
176  bbox_msg.dimensions.z = dimensions.z
177  bbox_array_msg.boxes.append(bbox_msg)
178  self.pub_bboxes.publish(bbox_array_msg)
179 
180  def _save_callback(self, event):
181  for i, box in enumerate(self.config['boxes']):
182  box_cfg = self.config['boxes'][i]
183  box_cfg['name'] = box['name']
184  pose = self.object_poses[box['name']]
185  dimensions = self.object_dimensions[box['name']]
186  # TODO: support transformed bbox dimensions like pose
187  box_cfg.update({
188  'frame_id': box['frame_id'],
189  'dimensions': [
190  dimensions.x,
191  dimensions.y,
192  dimensions.z,
193  ],
194  'position': [
195  pose.position.x,
196  pose.position.y,
197  pose.position.z,
198  ],
199  'orientation': [
200  pose.orientation.x,
201  pose.orientation.y,
202  pose.orientation.z,
203  pose.orientation.w,
204  ],
205  })
206  yaml.dump(self.config, open(self.config_file, 'w'))
207 
208 
209 if __name__ == '__main__':
210  rospy.init_node('transformable_markers_client')
212  rospy.spin()
object
transformable_markers_client.TransformableMarkersClient.sub_pose
sub_pose
Definition: transformable_markers_client.py:96
transformable_markers_client.TransformableMarkersClient.pub_bboxes
pub_bboxes
Definition: transformable_markers_client.py:106
msg
transformable_markers_client.xrange
xrange
Definition: transformable_markers_client.py:31
transformable_markers_client.TransformableMarkersClient.config
config
Definition: transformable_markers_client.py:44
transformable_markers_client.TransformableMarkersClient.object_dimensions
object_dimensions
Definition: transformable_markers_client.py:65
transformable_markers_client.TransformableMarkersClient.get_focus_marker
def get_focus_marker(self)
Definition: transformable_markers_client.py:151
transformable_markers_client.TransformableMarkersClient._dimensions_change_callback
def _dimensions_change_callback(self, msg)
Definition: transformable_markers_client.py:156
transformable_markers_client.TransformableMarkersClient.req_marker
req_marker
Definition: transformable_markers_client.py:46
transformable_markers_client.TransformableMarkersClient.req_pose
req_pose
Definition: transformable_markers_client.py:51
transformable_markers_client.TransformableMarkersClient.insert_marker
def insert_marker(self, name, frame_id, type)
Definition: transformable_markers_client.py:111
transformable_markers_client.TransformableMarkersClient._pose_change_callback
def _pose_change_callback(self, msg)
Definition: transformable_markers_client.py:160
transformable_markers_client.TransformableMarkersClient.config_file
config_file
Definition: transformable_markers_client.py:39
transformable_markers_client.TransformableMarkersClient
Definition: transformable_markers_client.py:34
transformable_markers_client.TransformableMarkersClient.sub_dimensions
sub_dimensions
Definition: transformable_markers_client.py:92
transformable_markers_client.TransformableMarkersClient.timer_pub_bboxes
timer_pub_bboxes
Definition: transformable_markers_client.py:108
transformable_markers_client.TransformableMarkersClient.req_dim
req_dim
Definition: transformable_markers_client.py:53
transformable_markers_client.TransformableMarkersClient._pub_bboxes_callback
def _pub_bboxes_callback(self, event)
Definition: transformable_markers_client.py:163
transformable_markers_client.TransformableMarkersClient.object_poses
object_poses
Definition: transformable_markers_client.py:64
transformable_markers_client.TransformableMarkersClient._save_callback
def _save_callback(self, event)
Definition: transformable_markers_client.py:180
transformable_markers_client.TransformableMarkersClient.req_color
req_color
Definition: transformable_markers_client.py:49
transformable_markers_client.TransformableMarkersClient.req_focus
req_focus
Definition: transformable_markers_client.py:55
transformable_markers_client.TransformableMarkersClient.__init__
def __init__(self)
Definition: transformable_markers_client.py:36
transformable_markers_client.TransformableMarkersClient.set_pose
def set_pose(self, name, frame_id, position, orientation)
Definition: transformable_markers_client.py:138
transformable_markers_client.TransformableMarkersClient.set_color
def set_color(self, name, rgba)
Definition: transformable_markers_client.py:121
transformable_markers_client.TransformableMarkersClient.server
server
Definition: transformable_markers_client.py:37
transformable_markers_client.TransformableMarkersClient.timer_save
timer_save
Definition: transformable_markers_client.py:103
transformable_markers_client.TransformableMarkersClient.set_dimensions
def set_dimensions(self, name, dimensions)
Definition: transformable_markers_client.py:130


jsk_interactive_marker
Author(s): furuta
autogenerated on Fri Aug 2 2024 08:50:24