interactive_marker.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 #
3 # Copyright 2011 Shadow Robot Company Ltd.
4 #
5 # This program is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation, either version 2 of the License, or (at your option)
8 # any later version.
9 #
10 # This program is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 # more details.
14 #
15 # You should have received a copy of the GNU General Public License along
16 # with this program. If not, see <http://www.gnu.org/licenses/>.
17 #
18 
19 import rospy
20 
21 from interactive_markers.interactive_marker_server import InteractiveMarker, InteractiveMarkerFeedback
22 from visualization_msgs.msg import Marker, InteractiveMarkerControl
23 
24 
26  def __init__(self, object_names, callback_fct, interactive_server):
27  # create an interactive marker server on the topic namespace simple_marker
28  self.server = interactive_server
29 
30  self.callback_function = callback_fct
31  self.object_names = object_names
32 
33  self.int_markers = {}
34  self.object_markers = {}
35  self.object_controls = {}
36  self.select_controls = {}
37 
38  for object_name in self.object_names:
39  self.create_marker(object_name)
40 
41  def create_marker(self, object_name):
42  # create an interactive marker for our server
43  int_marker = InteractiveMarker()
44  int_marker.header.frame_id = "/"+object_name
45  int_marker.name = object_name
46  int_marker.description = "Select this object"
47  int_marker.pose.position.z = -0.3
48 
49  self.int_markers[object_name] = int_marker
50 
51  # create a marker over the object
52  object_marker = Marker()
53  object_marker.type = Marker.SPHERE
54  object_marker.pose.position.z = 0.6
55 
56  object_marker.scale.x = 0.1
57  object_marker.scale.y = 0.1
58  object_marker.scale.z = 0.1
59  object_marker.color.r = 0.6
60  object_marker.color.g = 0.02
61  object_marker.color.b = 1.0
62  object_marker.color.a = 1.0
63  self.object_markers[object_name] = object_marker
64 
65  # create a non-interactive control which contains the box
66  object_control = InteractiveMarkerControl()
67  object_control.interaction_mode = InteractiveMarkerControl.BUTTON
68  object_control.always_visible = True
69  object_control.markers.append(object_marker)
70 
71  self.object_controls[object_name] = object_control
72 
73  # add the control to the interactive marker
74  self.int_markers[object_name].controls.append(object_control)
75 
76  # add the interactive marker to our collection &
77  # tell the server to call processFeedback() when feedback arrives for it
78  self.server.insert(self.int_markers[object_name], self.processFeedback)
79 
80  # 'commit' changes and send to all clients
81  self.server.applyChanges()
82 
83  def processFeedback(self, feedback):
84  # this was not a click on the sphere
85  if feedback.event_type != InteractiveMarkerFeedback.BUTTON_CLICK:
86  return
87  # One of the object has been selected.
88  # We'll update all the dynamic markers
89  # to make the selected one prominent.
90 
91  # this is the name of the selected object.
92  selected_name = feedback.marker_name
93 
94  # we loop through all our interactive markers.
95  for index, name in enumerate(self.object_controls):
96  self.object_controls[name].markers.remove(self.object_markers[name])
97 
98  if name == selected_name:
99  # the selected object marker is set
100  # higher, bigger and in green
101  self.object_markers[name].pose.position.z = 0.65
102 
103  self.object_markers[name].scale.x = 0.15
104  self.object_markers[name].scale.y = 0.15
105  self.object_markers[name].scale.z = 0.15
106  self.object_markers[name].color.r = 0.32
107  self.object_markers[name].color.g = 0.83
108  self.object_markers[name].color.b = 0.15
109  else:
110  # the other objects are slightly lower,
111  # smaller and in purple
112  self.object_markers[name].pose.position.z = 0.6
113 
114  self.object_markers[name].scale.x = 0.1
115  self.object_markers[name].scale.y = 0.1
116  self.object_markers[name].scale.z = 0.1
117  self.object_markers[name].color.r = 0.6
118  self.object_markers[name].color.g = 0.02
119  self.object_markers[name].color.b = 1.0
120 
121  self.object_controls[name].markers.append(self.object_markers[name])
122 
123  self.server.insert(self.int_markers[name])
124 
125  # we update the interactive marker server
126  self.server.applyChanges()
127  self.callback_function(selected_name)
128 
129 
130 # this is just used for debug
131 if __name__ == "__main__":
132  rospy.init_node("simple_marker")
133 
134  int_mark = InteractiveConnectorSelector(["srh/position/palm"], None)
135  rospy.spin()
def __init__(self, object_names, callback_fct, interactive_server)


sr_utilities
Author(s): Ugo Cupcic
autogenerated on Tue Oct 13 2020 03:55:49