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


jsk_interactive_marker
Author(s): furuta
autogenerated on Sat Mar 20 2021 03:03:33