直接贴例子代码,实现画多条线,鼠标单击每条线时会有菜单显示信息。
#!/usr/bin/env python """ Copyright (c) 2011, Willow Garage, Inc. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the Willow Garage, Inc. nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES LOSS OF USE, DATA, OR PROFITS OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ import rospy import copy from interactive_markers.interactive_marker_server import * from interactive_markers.menu_handler import * from visualization_msgs.msg import * from geometry_msgs.msg import Point from tf.broadcaster import TransformBroadcaster import random from math import sin server = None menu_handler = MenuHandler() br = None counter = 0 global nMark_ nMark_ = 0 def frameCallback(msg): global counter, br time = rospy.Time.now() br.sendTransform((0, 0, sin(counter / 140.0) * 2.0), (0, 0, 0, 1.0), time, "map", "moving_frame") counter += 1 def processFeedback(feedback): s = "Feedback from marker '" + feedback.marker_name s += "' / control '" + feedback.control_name + "'" mp = "" if feedback.mouse_point_valid: mp = " at " + str(feedback.mouse_point.x) mp += ", " + str(feedback.mouse_point.y) mp += ", " + str(feedback.mouse_point.z) mp += " in frame " + feedback.header.frame_id if feedback.event_type == InteractiveMarkerFeedback.BUTTON_CLICK: rospy.loginfo(s + ": button click" + mp + ".") elif feedback.event_type == InteractiveMarkerFeedback.MENU_SELECT: rospy.loginfo(s + ": menu item " + str(feedback.menu_entry_id) + " clicked" + mp + ".") elif feedback.event_type == InteractiveMarkerFeedback.POSE_UPDATE: rospy.loginfo(s + ": pose changed") # TODO # << "\nposition = " # << feedback.pose.position.x # << ", " << feedback.pose.position.y # << ", " << feedback.pose.position.z # << "\norientation = " # << feedback.pose.orientation.w # << ", " << feedback.pose.orientation.x # << ", " << feedback.pose.orientation.y # << ", " << feedback.pose.orientation.z # << "\nframe: " << feedback.header.frame_id # << " time: " << feedback.header.stamp.sec << "sec, " # << feedback.header.stamp.nsec << " nsec" ) elif feedback.event_type == InteractiveMarkerFeedback.MOUSE_DOWN: rospy.loginfo(s + ": mouse down" + mp + ".") elif feedback.event_type == InteractiveMarkerFeedback.MOUSE_UP: rospy.loginfo(s + ": mouse up" + mp + ".") server.applyChanges() def makeBox(msg): marker = Marker() marker.type = Marker.CUBE marker.scale.x = msg.scale * 0.45 marker.scale.y = msg.scale * 0.45 marker.scale.z = msg.scale * 0.45 marker.color.r = 0.5 marker.color.g = 0.5 marker.color.b = 0.5 marker.color.a = 1.0 return marker def _markerLineStrip(msg): global nMark_ markerLine = Marker() markerLine.type = Marker.LINE_STRIP markerLine.header.frame_id = "map" markerLine.ns = "lanes_edge" markerLine.action = Marker.ADD markerLine.id = nMark_ nMark_ += 1 markerLine.scale.x = 0.15 markerLine.scale.y = 0.15 markerLine.scale.z = 0.15 markerLine.color.r = 1.0 markerLine.color.g = 1.0 markerLine.color.b = 0.0 markerLine.color.a = 1.0 markerLine.pose.orientation.x = 0.0 markerLine.pose.orientation.y = 0.0 markerLine.pose.orientation.z = 0.0 markerLine.pose.orientation.w = 1.0 markerLine.pose.position.x = 0.0 markerLine.pose.position.y = 0.0 markerLine.pose.position.z = 0.0 markerLine.lifetime = rospy.Duration() for i in range(10): p = Point() p.x = i p.y = i markerLine.points.append(p) return markerLine def _markerLineStrip22(msg): global nMark_ markerLine = Marker() markerLine.type = Marker.LINE_STRIP markerLine.header.frame_id = "map" markerLine.ns = "lanes_edge" markerLine.action = Marker.ADD markerLine.id = nMark_ nMark_ += 1 markerLine.scale.x = 0.15 markerLine.scale.y = 0.15 markerLine.scale.z = 0.15 markerLine.color.r = 1.0 markerLine.color.g = 1.0 markerLine.color.b = 0.0 markerLine.color.a = 1.0 markerLine.pose.orientation.x = 0.0 markerLine.pose.orientation.y = 0.0 markerLine.pose.orientation.z = 0.0 markerLine.pose.orientation.w = 1.0 markerLine.pose.position.x = 0.0 markerLine.pose.position.y = 0.0 markerLine.pose.position.z = 0.0 markerLine.lifetime = rospy.Duration() return markerLine def makeSomeLine(): for nLine in range(10): line1 = _markerLineStrip22(None) for i in range(10): p = Point() a = random.randint(1, 10) b = random.randint(1, 10) p.x = a p.y = b p.z = 1 line1.points.append(p) int_marker = InteractiveMarker() int_marker.header.frame_id = "map" # int_marker.pose.position = position int_marker.scale = 1 name111 = str(nLine) + "context_menu" int_marker.name = name111 # int_marker.description = "Context Menu\n(Right Click)" # make one control using default visuals control = InteractiveMarkerControl() control.interaction_mode = InteractiveMarkerControl.MENU # control.description = "Options" # control.name = "menu_only_control" # int_marker.controls.append(copy.deepcopy(control)) control.always_visible = True control.markers.append(line1) int_marker.controls.append(control) server.insert(int_marker, processFeedback) menu_handler1 = MenuHandler() str1 = str(nLine) + "First Entry" menu_handler1.insert(str1, callback = processFeedback) menu_handler1.insert("Second Entry", callback = processFeedback) menu_handler1.apply(server, int_marker.name) def normalizeQuaternion(quaternion_msg): norm = quaternion_msg.x ** 2 + quaternion_msg.y ** 2 + quaternion_msg.z ** 2 + quaternion_msg.w ** 2 s = norm ** (-0.5) quaternion_msg.x *= s quaternion_msg.y *= s quaternion_msg.z *= s quaternion_msg.w *= s def makeMenuMarker(position): menu_handler1 = MenuHandler() menu_handler1.insert("First Entry11", callback=processFeedback) menu_handler1.insert("Second Entry", callback=processFeedback) int_marker = InteractiveMarker() int_marker.header.frame_id = "map" int_marker.pose.position = position int_marker.scale = 1 int_marker.name = "context_menu22" int_marker.description = "Context Menu\n(Right Click)" # make one control using default visuals control = InteractiveMarkerControl() control.interaction_mode = InteractiveMarkerControl.MENU control.description = "Options11" control.name = "menu_only_control11" int_marker.controls.append(copy.deepcopy(control)) # make one control showing a box marker = makeBox(int_marker) control.markers.append(marker) control.always_visible = True int_marker.controls.append(control) server.insert(int_marker, processFeedback) menu_handler1.apply(server, int_marker.name) def makeMenuMarker2(position): menu_handler2 = MenuHandler() menu_handler2.insert("First Entry 222", callback=processFeedback) menu_handler2.insert("Second Entry222", callback=processFeedback) int_marker = InteractiveMarker() int_marker.header.frame_id = "map" # int_marker.pose.position = position int_marker.scale = 1 int_marker.name = "context_menu22" int_marker.description = "Context Menu\n(Right Click)" # make one control using default visuals control = InteractiveMarkerControl() control.interaction_mode = InteractiveMarkerControl.MENU control.description = "Options2222" control.name = "menu_only_control2222" int_marker.controls.append(copy.deepcopy(control)) # make one control showing a box marker = _markerLineStrip(int_marker) control.markers.append(marker) control.always_visible = True int_marker.controls.append(control) server.insert(int_marker, processFeedback) menu_handler2.apply(server, int_marker.name) if __name__ == "__main__": rospy.init_node("basic_controls") # br = TransformBroadcaster() # create a timer to update the published transforms rospy.Timer(rospy.Duration(0.01), frameCallback) server = InteractiveMarkerServer("basic_controls") # menu_handler.insert("First Entry", callback=processFeedback) # menu_handler.insert("Second Entry", callback=processFeedback) # sub_menu_handle = menu_handler.insert("Submenu") # menu_handler.insert("First Entry", parent=sub_menu_handle, callback=processFeedback) # menu_handler.insert("Second Entry", parent=sub_menu_handle, callback=processFeedback) # position = Point(3, -6, 0) # makeMenuMarker(position) # makeMenuMarker2(position) makeSomeLine() server.applyChanges() rospy.spin()