这里只介绍使用roslaunch指令运行launch文件时进行参数传递的情况
0. 常见launch文件写法
//launch文件写法
<launch>
<node pkg="camera_driver" type="robot_detector" name="cama_detector" launch-prefix="xterm-e" output="screen"/>
</launch>
- pkg:功能包名
- type:可执行文件名称
- name:节点名字
- launch-prefix:rosrun命令的启动前缀,输出信息在另一个独立终端
- output:输出到屏幕上
1. 使用参数服务器进行参数传递
//launch文件写法
<launch>
<param name="file_name" type="string" value="/home/nvidia/camera_parameters.yml" />
<node pkg="camera_driver" type="robot_detector" name="camera_detector" launch-prefix="xterm-e" output="screen">
<param name="camera_name" type="string" value="/camera" />
</node>
<node pkg="camera_driver" type="robot_detector" name="camerab_detector" launch-prefix="xterm-e" output="screen" />
</launch>
//CPP文件中的调用方法
std::string camera_name; //如果该变量为全局变量,将该变量的声明放到全局范围内
ros::param::get("~camera_name", camera_name); //"~camera_name"表私有参数,在launch文件中,将<param.../>写到<node.../>里面,那么参数就是私有参数
ros::param::get("file_name", filename); //"filename"这里使用全局参数,即其他节点也可以使用
2. 使用mian参数进行参数传递
//launch文件写法
<launch>
<param name="file_name" type="string" value="/home/nvidia/camera_parameters.yml" />
<node pkg="camera_driver" type="robot_detector" name="camera_detector" args="filename" launch-prefix="xterm-e" output="screen" />
</launch>
//CPP文件中的调用方法
ros::init(argc, argv, "robot_detector");
if (argc != 2)
{
ROS_ERROR("need turtle name as argument");
return -1;
};
camera_name = argv[1];
3. 具体案例解析之main参数传递
- launch文件
<launch>
<node pkg="camera_driver" type="camera_sub" name="cameraa_shower" args=" /cameraa" launch-prefix="xterm -e" output="screen" />
<node pkg="camera_driver" type="camera_sub" name="camerab_shower" args=" /camerab" launch-prefix="xterm -e" output="screen" />
<node pkg="camera_driver" type="camera_sub" name="camerac_shower" args=" /camerac" launch-prefix="xterm -e" output="screen" />
<node pkg="camera_driver" type="camera_sub" name="camerad_shower" args=" /camerad" launch-prefix="xterm -e" output="screen" />
</launch>
// args文件之前有一个空格,一定不要忘记了
// pkg是包名,type是可执行文件名称,name是节点名
- CPP文件
#include <ros/ros.h>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <image_transport/image_transport.h>
#include <cv_bridge/cv_bridge.h>
using namespace cv;
using namespace std;
std::string camera_name; //全局变量
void imageCallback(const sensor_msgs::ImageConstPtr &msg)
{
try
{
cout << camera_name << " : all is ok!!!" << endl;
imshow(camera_name, cv_bridge::toCvShare(msg, "bgr8")->image);
waitKey(5);
cout << "================================================================" << endl;
}
catch (cv_bridge::Exception &e)
{
ROS_ERROR("Could not convert from '%s' to 'bgr8'.", msg->encoding.c_str());
}
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "image_shower");
if (argc != 2) //对输入参数数量进行判断
{
ROS_ERROR("need turtle name as argument");
return -1;
};
camera_name = argv[1];
ros::NodeHandle nh;
namedWindow(camera_name, CV_WINDOW_NORMAL);
image_transport::ImageTransport it(nh);
image_transport::Subscriber sub = it.subscribe(camera_name + "/image", 1, imageCallback);
ros::spin();
cv::destroyWindow(camera_name);
}
4. 具体案例解析之参数服务器参数传递
- launch文件
<launch>
<node pkg="video_driver" type="camera_sub" name="camera_shower" launch-prefix="xterm -e" output="screen">
<param name="camera_name" type="string" value="/camera" />
</node>
</launch>
- CPP文件
#include <ros/ros.h>
#include <opencv2/highgui.hpp>
#include <opencv2/opencv.hpp>
#include <image_transport/image_transport.h>
#include <cv_bridge/cv_bridge.h>
using namespace cv;
using namespace std;
std::string camera_name;
void imageCallback(const sensor_msgs::ImageConstPtr &msg)
{
try
{
cout << camera_name << " : all is ok!!!" << endl;
imshow(camera_name, cv_bridge::toCvShare(msg, "bgr8")->image);
waitKey(5);
cout << "================================================================" << endl;
}
catch (cv_bridge::Exception &e)
{
ROS_ERROR("Could not convert from '%s' to 'bgr8'.", msg->encoding.c_str());
}
}
int main(int argc, char **argv)
{
ros::init(argc, argv, "image_shower");
ros::param::get("~camera_name", camera_name); //私有参数解析,也可以通过ros::param::set("~camera_name", camera_name)进行参数设置
ros::NodeHandle nh;
namedWindow(camera_name, CV_WINDOW_NORMAL);
image_transport::ImageTransport it(nh);
image_transport::Subscriber sub = it.subscribe(camera_name + "/image", 1, imageCallback);
ros::spin();
cv::destroyWindow(camera_name);
}
网友评论