ROS在rviz中的不同位置显示文字(visualization_msgs::MarkerArray的使用)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ROS在rviz中的不同位置显示文字(visualization_msgs::MarkerArray的使用)相关的知识,希望对你有一定的参考价值。
参考技术A 本文实现在rviz中指定位置显示文字1.在特定地方显示一行文字
#include <ros/ros.h>
#include <visualization_msgs/Marker.h>
#include<iostream>
using namespace std;
int main( int argc, char** argv )
ros::init(argc, argv, "showline");
ros::NodeHandle n;
ros::Publisher markerPub = n.advertise<visualization_msgs::Marker>("TEXT_VIEW_FACING", 10);
visualization_msgs::Marker marker;
marker.header.frame_id="/odom";
marker.header.stamp = ros::Time::now();
marker.ns = "basic_shapes";
marker.action = visualization_msgs::Marker::ADD;
marker.pose.orientation.w = 1.0;
marker.id =0;
marker.type = visualization_msgs::Marker::TEXT_VIEW_FACING;
marker.scale.z = 0.2;
marker.color.b = 0;
marker.color.g = 0;
marker.color.r = 255;
marker.color.a = 1;
ros::Rate r(1);
int k=0;
while(1)
geometry_msgs::Pose pose;
pose.position.x = (float)(k++)/10;
pose.position.y = 0;
pose.position.z =0;
ostringstream str;
str<<k;
marker.text=str.str();
marker.pose=pose;
markerPub.publish(marker);
cout<<"k="<<k<<endl;
r.sleep();
return 0;
如上例子,动态的在geometry_msgs::Pose pose 处(x = (float)(k++)/10)显示当前的k值.
2.在同时在不同地方显示不同文字
#include <ros/ros.h>
#include <visualization_msgs/Marker.h>
#include<visualization_msgs/MarkerArray.h>
#include<iostream>
using namespace std;
int main( int argc, char** argv )
ros::init(argc, argv, "showline");
ros::NodeHandle n;
ros::Publisher markerArrayPub = n.advertise<visualization_msgs::MarkerArray>("MarkerArray", 10);
visualization_msgs::MarkerArray markerArray;
ros::Rate r(1);
int k=0;
while(k<100)
visualization_msgs::Marker marker;
marker.header.frame_id="/odom";
marker.header.stamp = ros::Time::now();
marker.ns = "basic_shapes";
marker.action = visualization_msgs::Marker::ADD;
marker.pose.orientation.w = 1.0;
marker.id =k;
marker.type = visualization_msgs::Marker::TEXT_VIEW_FACING;
marker.scale.z = 0.1;
marker.color.b = 255;
marker.color.g = 255;
marker.color.r = 255;
marker.color.a = 1;
geometry_msgs::Pose pose;
pose.position.x = (float)(k)/10;
pose.position.y = 0;
pose.position.z =0;
ostringstream str;
str<<k;
marker.text=str.str();
marker.pose=pose;
cout<<"k="<<k<<endl;
markerArray.markers.push_back(marker);
cout<<"markerArray.markers.size()"<<markerArray.markers.size()<<endl;
markerArrayPub.publish(markerArray);
r.sleep();
k++;
return 0;
[实例]ROS使用OpenCV读取图像并发布图像消息在rviz中显示
思路:
(1)使用opencv读取本地图像
(2)调用cv_bridge::CvImage().toImageMsg()将本地图像发送给rviz显示
一、使用opencv读取本地图像并发布图像消息
(1)利用catkin新建一个工程叫rosopencv,并进行初始化
mkdir -p rosopencv/src
cd rosopencv/src
catkin_create_pkg rosopencv sensor_msgs cv_bridge roscpp std_msgs image_transport
cd ..
catkin_make
source ./devel/setup.bash
(2)编辑主函数代码
主函数rosopencv.cpp内容如下
#include <ros/ros.h>
#include <image_transport/image_transport.h>
#include <opencv2/highgui/highgui.hpp>
#include <cv_bridge/cv_bridge.h>
#include <stdio.h>
int main(int argc, char** argv)
{
ros::init(argc, argv, "image_publisher");
ros::NodeHandle nh;
image_transport::ImageTransport it(nh);
image_transport::Publisher pub = it.advertise("camera/image", 1);
cv::Mat image = cv::imread("/home/topeet/test.jpg", CV_LOAD_IMAGE_COLOR);
if(image.empty()){
printf("open error\n");
}
sensor_msgs::ImagePtr msg = cv_bridge::CvImage(std_msgs::Header(), "bgr8", image).toImageMsg();
ros::Rate loop_rate(5);
while (nh.ok()) {
pub.publish(msg);
ros::spinOnce();
loop_rate.sleep();
}
}
注意:cv::Mat image = cv::imread("/home/topeet/test.jpg", CV_LOAD_IMAGE_COLOR);
这句中的"/home/topeet/test.jpg"
需要改成自己图片的路径.
(3)编辑CmakeLists.txt
在工程目录下的/src/rosopencv/src/CmakeLists.txt中加入如下内容
add_executable(rosopencv src/rosopencv.cpp)target_link_libraries(rosopencv ${catkin_LIBRARIES})
add_dependencies(rosopencv robot_vision_generate_messages_cpp)
二、编译和运行
编译
cd ~/dev/workspace/rosopencv
catkin_make
运行
rosrun rosopencv rosopencv
输入
rostopic echo /camera/image
全是数字。
三、在rviz 中显示
运行rviz
rosrun rviz rviz
左边点击add
选中image
在image的topic选项中选
/camera/image
转载自:http://blog.csdn.net/ktigerhero3/article/details/70157582
以上是关于ROS在rviz中的不同位置显示文字(visualization_msgs::MarkerArray的使用)的主要内容,如果未能解决你的问题,请参考以下文章
父子QGraphicsItem绘图示例,及与ros中rviz的不同
[实例]ROS使用OpenCV读取图像并发布图像消息在rviz中显示