test_interactive_obstacle_node.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # Copyright 2017 Fraunhofer Institute for Manufacturing Engineering and Automation (IPA)
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 
17 
18 import sys
19 import math
20 from copy import deepcopy
21 
22 
23 import rospy
24 import tf
25 from geometry_msgs.msg import Pose, PoseStamped
26 from moveit_msgs.msg import CollisionObject
27 from shape_msgs.msg import SolidPrimitive
28 from visualization_msgs.msg import Marker, InteractiveMarker, InteractiveMarkerControl
30 
31 
33  def __init__(self):
34 
35  #Specify a frame_id - transformation to root_frame of obstacle_distance node is handled in according subscriber callback
36  self.root_frame = rospy.get_param("root_frame")
37  self.obstacle_frame = rospy.get_namespace() + "interactive_box_frame"
38 
39  self.pub = rospy.Publisher("obstacle_distance/registerObstacle", CollisionObject, queue_size=1, latch=True)
41 
42  while self.pub.get_num_connections() < 1:
43  rospy.logwarn("Please create a subscriber to '" + rospy.get_namespace() + "/obstacle_distance/registerObstacle' topic (Type: moveit_msgs/CollisionObject)")
44  rospy.sleep(1.0)
45 
46  rospy.loginfo("Continue ...")
47 
48  # Compose CollisionObject (Box)
49  self.co = CollisionObject()
50  self.co.id = "Interactive Box"
51  self.co.header.frame_id = self.obstacle_frame
52  self.co.operation = CollisionObject.ADD
53 
54  primitive = SolidPrimitive()
55  primitive.type = SolidPrimitive.SPHERE
56  primitive.dimensions = [0.1] # extent x, y, z
57  self.co.primitives.append(primitive)
58 
59  pose = Pose()
60  pose.orientation.w = 1.0;
61  self.co.primitive_poses.append(pose)
62 
63  # Compose InteractiveMarker
64  self.interactive_obstacle_pose = PoseStamped()
65  self.interactive_obstacle_pose.header.stamp = rospy.Time.now()
66  self.interactive_obstacle_pose.header.frame_id = self.root_frame
67 
68  self.interactive_obstacle_pose.pose.position.x = -0.5
69  self.interactive_obstacle_pose.pose.position.y = 0.3
70  self.interactive_obstacle_pose.pose.position.z = 0.8
71  self.interactive_obstacle_pose.pose.orientation.w = 1.0
72 
73  self.ia_server = InteractiveMarkerServer("obstacle_marker_server")
74  self.int_marker = InteractiveMarker()
75  self.int_marker.header.frame_id = self.root_frame
76  self.int_marker.pose = self.interactive_obstacle_pose.pose
77  self.int_marker.name = "interactive_obstacle_marker"
78  self.int_marker.scale = 0.5
79 
80  # Setup MarkerControls
81  control_3d = InteractiveMarkerControl()
82  control_3d.always_visible = True
83  control_3d.name = "move_rotate_3D"
84  control_3d.interaction_mode = InteractiveMarkerControl.MOVE_ROTATE_3D
85  self.int_marker.controls.append(control_3d)
86 
87  control = InteractiveMarkerControl()
88  control.always_visible = True
89  control.orientation.w = 1
90  control.orientation.x = 1
91  control.orientation.y = 0
92  control.orientation.z = 0
93  control.name = "move_x"
94  control.interaction_mode = InteractiveMarkerControl.MOVE_AXIS
95  self.int_marker.controls.append(deepcopy(control))
96  control.name = "move_y"
97  control.orientation.x = 0
98  control.orientation.y = 1
99  control.interaction_mode = InteractiveMarkerControl.MOVE_AXIS
100  self.int_marker.controls.append(deepcopy(control))
101  control.name = "move_z"
102  control.orientation.y = 0
103  control.orientation.z = 1
104  control.interaction_mode = InteractiveMarkerControl.MOVE_AXIS
105  self.int_marker.controls.append(deepcopy(control))
106  control.orientation.w = 1
107  control.orientation.x = 1
108  control.orientation.y = 0
109  control.orientation.z = 0
110  control.name = "rotate_x"
111  control.interaction_mode = InteractiveMarkerControl.ROTATE_AXIS
112  self.int_marker.controls.append(deepcopy(control))
113  control.name = "rotate_y"
114  control.orientation.x = 0
115  control.orientation.y = 1
116  control.interaction_mode = InteractiveMarkerControl.ROTATE_AXIS
117  self.int_marker.controls.append(deepcopy(control))
118  control.name = "rotate_z"
119  control.orientation.y = 0
120  control.orientation.z = 1
121  control.interaction_mode = InteractiveMarkerControl.ROTATE_AXIS
122  self.int_marker.controls.append(deepcopy(control))
123 
124  self.ia_server.insert(self.int_marker, self.marker_fb)
125  self.ia_server.applyChanges()
126 
127  # initial send
128  self.br.sendTransform(
129  (self.interactive_obstacle_pose.pose.position.x, self.interactive_obstacle_pose.pose.position.y, self.interactive_obstacle_pose.pose.position.z),
130  (self.interactive_obstacle_pose.pose.orientation.x, self.interactive_obstacle_pose.pose.orientation.y, self.interactive_obstacle_pose.pose.orientation.z, self.interactive_obstacle_pose.pose.orientation.w),
131  rospy.Time.now(), self.obstacle_frame, self.interactive_obstacle_pose.header.frame_id)
132  rospy.sleep(1.0)
133  self.pub.publish(self.co)
134  rospy.sleep(1.0)
135 
136  def marker_fb(self, fb):
137  #p = feedback.pose.position
138  #print feedback.marker_name + " is now at " + str(p.x) + ", " + str(p.y) + ", " + str(p.z)
139  self.interactive_obstacle_pose.header = fb.header
140  self.interactive_obstacle_pose.pose = fb.pose
141  self.ia_server.applyChanges()
142 
143  def run(self):
144  self.br.sendTransform(
145  (self.interactive_obstacle_pose.pose.position.x, self.interactive_obstacle_pose.pose.position.y, self.interactive_obstacle_pose.pose.position.z),
146  (self.interactive_obstacle_pose.pose.orientation.x, self.interactive_obstacle_pose.pose.orientation.y, self.interactive_obstacle_pose.pose.orientation.z, self.interactive_obstacle_pose.pose.orientation.w),
147  rospy.Time.now(), self.obstacle_frame, self.interactive_obstacle_pose.header.frame_id)
148 
149  self.co.operation = CollisionObject.MOVE
150  self.pub.publish(self.co)
151 
152 
153 if __name__ == "__main__":
154  rospy.init_node("interactive_obstacle_node")
155 
157 
158  r = rospy.Rate(20)
159  while not rospy.is_shutdown():
160  io.run()
161  try:
162  r.sleep()
163  except rospy.ROSInterruptException as e:
164  #print "ROSInterruptException"
165  pass


cob_obstacle_distance
Author(s): Marco Bezzon
autogenerated on Thu Apr 8 2021 02:39:47