interactive_marker.py
Go to the documentation of this file.
00001 #!/usr/bin/env python
00002 #
00003 # Copyright 2011 Shadow Robot Company Ltd.
00004 #
00005 # This program is free software: you can redistribute it and/or modify it
00006 # under the terms of the GNU General Public License as published by the Free
00007 # Software Foundation, either version 2 of the License, or (at your option)
00008 # any later version.
00009 #
00010 # This program is distributed in the hope that it will be useful, but WITHOUT
00011 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00012 # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
00013 # more details.
00014 #
00015 # You should have received a copy of the GNU General Public License along
00016 # with this program.  If not, see <http://www.gnu.org/licenses/>.
00017 #
00018 
00019 import rospy
00020 
00021 from interactive_markers.interactive_marker_server import InteractiveMarker, InteractiveMarkerFeedback
00022 from visualization_msgs.msg import Marker, InteractiveMarkerControl
00023 
00024 
00025 class InteractiveConnectorSelector(object):
00026     def __init__(self, object_names, callback_fct, interactive_server):
00027         # create an interactive marker server on the topic namespace simple_marker
00028         self.server = interactive_server
00029 
00030         self.callback_function = callback_fct
00031         self.object_names = object_names
00032 
00033         self.int_markers = {}
00034         self.object_markers = {}
00035         self.object_controls = {}
00036         self.select_controls = {}
00037 
00038         for object_name in self.object_names:
00039             self.create_marker( object_name )
00040 
00041     def create_marker(self, object_name):
00042         # create an interactive marker for our server
00043         int_marker = InteractiveMarker()
00044         int_marker.header.frame_id = "/"+object_name
00045         int_marker.name = object_name
00046         int_marker.description = "Select this object"
00047         int_marker.pose.position.z = -0.3
00048 
00049         self.int_markers[ object_name ] = int_marker
00050 
00051         # create a marker over the object
00052         object_marker = Marker()
00053         object_marker.type = Marker.SPHERE
00054         object_marker.pose.position.z = 0.6
00055 
00056         object_marker.scale.x = 0.1
00057         object_marker.scale.y = 0.1
00058         object_marker.scale.z = 0.1
00059         object_marker.color.r = 0.6
00060         object_marker.color.g = 0.02
00061         object_marker.color.b = 1.0
00062         object_marker.color.a = 1.0
00063         self.object_markers[object_name] = object_marker
00064 
00065         # create a non-interactive control which contains the box
00066         object_control = InteractiveMarkerControl()
00067         object_control.interaction_mode = InteractiveMarkerControl.BUTTON
00068         object_control.always_visible = True
00069         object_control.markers.append( object_marker )
00070 
00071         self.object_controls[object_name] = object_control
00072 
00073         # add the control to the interactive marker
00074         self.int_markers[object_name].controls.append( object_control )
00075 
00076         # add the interactive marker to our collection &
00077         # tell the server to call processFeedback() when feedback arrives for it
00078         self.server.insert(self.int_markers[object_name], self.processFeedback)
00079 
00080         # 'commit' changes and send to all clients
00081         self.server.applyChanges()
00082 
00083     def processFeedback(self, feedback):
00084         # this was not a click on the sphere
00085         if feedback.event_type != InteractiveMarkerFeedback.BUTTON_CLICK:
00086             return
00087         # One of the object has been selected.
00088         # We'll update all the dynamic markers
00089         # to make the selected one prominent.
00090 
00091         # this is the name of the selected object.
00092         selected_name = feedback.marker_name
00093 
00094         # we loop through all our interactive markers.
00095         for index, name in enumerate(self.object_controls):
00096             self.object_controls[name].markers.remove( self.object_markers[name] )
00097 
00098             if name == selected_name:
00099                 # the selected object marker is set
00100                 # higher, bigger and in green
00101                 self.object_markers[name].pose.position.z = 0.65
00102 
00103                 self.object_markers[name].scale.x = 0.15
00104                 self.object_markers[name].scale.y = 0.15
00105                 self.object_markers[name].scale.z = 0.15
00106                 self.object_markers[name].color.r = 0.32
00107                 self.object_markers[name].color.g = 0.83
00108                 self.object_markers[name].color.b = 0.15
00109             else:
00110                 # the other objects are slightly lower,
00111                 # smaller and in purple
00112                 self.object_markers[name].pose.position.z = 0.6
00113 
00114                 self.object_markers[name].scale.x = 0.1
00115                 self.object_markers[name].scale.y = 0.1
00116                 self.object_markers[name].scale.z = 0.1
00117                 self.object_markers[name].color.r = 0.6
00118                 self.object_markers[name].color.g = 0.02
00119                 self.object_markers[name].color.b = 1.0
00120 
00121             self.object_controls[name].markers.append( self.object_markers[name] )
00122 
00123             self.server.insert(self.int_markers[name])
00124 
00125         # we update the interactive marker server
00126         self.server.applyChanges()
00127         self.callback_function( selected_name )
00128 
00129 
00130 # this is just used for debug
00131 if __name__ == "__main__":
00132     rospy.init_node("simple_marker")
00133 
00134     int_mark = InteractiveConnectorSelector(["srh/position/palm"], None)
00135     rospy.spin()


sr_utilities
Author(s): Ugo Cupcic
autogenerated on Fri Aug 21 2015 12:24:47