ros中交互显示InteractiveMarker的使用例子

7 篇文章 0 订阅

直接贴例子代码,实现画多条线,鼠标单击每条线时会有菜单显示信息。

#!/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()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ROS,Visualiztion::marker是用于在RViz可视化数据的常用工具之一。下面是一个简单的例子: 假设我们有一个名为“/my_marker”的话题,我们想在RViz显示一个立方体,我们可以使用以下代码: ```cpp #include <ros/ros.h> #include <visualization_msgs/Marker.h> int main( int argc, char** argv ) { ros::init(argc, argv, "my_marker_publisher"); ros::NodeHandle n; ros::Publisher marker_pub = n.advertise<visualization_msgs::Marker>("my_marker", 1); // Set the frame ID and timestamp. See the TF tutorials for information on these. visualization_msgs::Marker marker; marker.header.frame_id = "my_frame"; marker.header.stamp = ros::Time::now(); // Set the namespace and id for this marker. This serves to create a unique ID // Any marker sent with the same namespace and id will overwrite the old one marker.ns = "basic_shapes"; marker.id = 0; // Set the marker type. Initially this is CUBE, and cycles between that and SPHERE, ARROW, and CYLINDER marker.type = visualization_msgs::Marker::CUBE; // Set the pose of the marker. This is a full 6DOF pose relative to the frame/time specified in the header marker.pose.position.x = 1; marker.pose.position.y = 1; marker.pose.position.z = 1; marker.pose.orientation.x = 0.0; marker.pose.orientation.y = 0.0; marker.pose.orientation.z = 0.0; marker.pose.orientation.w = 1.0; // Set the scale of the marker -- 1x1x1 here means 1m on a side marker.scale.x = 1.0; marker.scale.y = 1.0; marker.scale.z = 1.0; // Set the color -- be sure to set alpha to something non-zero! marker.color.r = 0.0f; marker.color.g = 1.0f; marker.color.b = 0.0f; marker.color.a = 1.0; marker.lifetime = ros::Duration(); // Publish the marker while (marker_pub.getNumSubscribers() < 1) { if (!ros::ok()) { return 0; } ROS_WARN_ONCE("Please create a subscriber to the marker"); sleep(1); } marker_pub.publish(marker); ros::spinOnce(); return 0; } ``` 这个例子,我们创建了一个立方体的visualization_msgs::Marker消息,并将其发布到“/my_marker”话题上。在RViz,我们可以通过添加一个“Marker显示来查看它。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值