• ROS-节点参数param


    ROS中有如下3种获取参数的方式。

    在launch中写

    <param name="debug_imu"  value="true">     不管是上面何种数据类型都要带引号

    #include "ros/ros.h" 
    #include <cstdlib> 
    
    using namespace std; 
    
    int main(int argc,char** argv) 
    { 
        //initial and name node 
        ros::init(argc,argv,"node_param"); 
        if(argc!=2) 
        { 
            cout<<"Error command paramter! Please run command eg:
    " 
                <<"rosrun book_param book_param 1
    " 
                <<"help information:
    " 
                <<" 1 ------ set param mode(ros::param::set())
    " 
                <<" 2 ------ set param mode(ros::NodeHandle::setParam())
    " 
                <<" 3 ------ get param mode(ros::param::get())
    " 
                <<" 4 ------ get param mode(ros::NodeHandle::getParam())
    " 
                <<" 5 ------ get param mode(ros::NodeHandle::param())
    " 
                <<endl; 
            return 1; 
        } 
    
        //create node handle 
        ros::NodeHandle nh; 
        //param variable 
        int IntParam; 
        string StrParam; 
        bool isIntParam, isStrParam; 
        //mode flag 
        int flag = atoi(argv[1]); 
    
        // set or get param with different ways 
        switch(flag) 
        { 
            case 1: 
                ROS_INFO("set param mode(ros::param::set()):"); 
                ros::param::set("IntParam",1); 
                ros::param::set("StrParam","stringdemo"); 
                break; 
            case 2: 
                ROS_INFO("set param mode(ros::NodeHandle::setParam()):"); 
                nh.setParam("IntParam",1); 
                nh.setParam("StrParam","stringdemo"); 
                break; 
            case 3: 
                ROS_INFO("get param mode(ros::param::get()):"); 
                isIntParam = ros::param::get("IntParam",IntParam); 
                isStrParam = ros::param::get("StrParam",StrParam); 
    
                if(isIntParam){ 
                    ROS_INFO("The IntParam is:%d",IntParam); 
                }else{ 
                    ROS_INFO("Get IntParam fail!"); 
                } 
    
                if(isIntParam){ 
                    ROS_INFO("The StrParam is:%s",StrParam.c_str()); 
                }else{ 
                    ROS_INFO("Get StrParam fail!"); 
                } 
                break; 
            case 4: 
                ROS_INFO("get param mode(ros::NodeHandle::getParam()):"); 
                isIntParam = nh.getParam("IntParam",IntParam); 
                isStrParam = nh.getParam("StrParam",StrParam); 
    
                if(isIntParam){ 
                    ROS_INFO("The IntParam is:%d",IntParam); 
                }else{ 
                    ROS_INFO("Get IntParam fail!"); 
                } 
    
                if(isIntParam){ 
                    ROS_INFO("The StrParam is:%s",StrParam.c_str()); 
                }else{ 
                    ROS_INFO("Get StrParam fail!"); 
                } 
                break; 
            case 5: 
                ROS_INFO("get param mode(ros::NodeHandle::param()):"); 
                //warning: this way will set default value when get no param! 
                nh.param("IntParam",IntParam,11); 
                // be careful when use ros::NodeHandle::param get string param! 
                nh.param<std::string>("StrParam",StrParam,"stringdemo_default"); 
                ROS_INFO("
    The IntParam is:%d
    The StrParam is:%s",IntParam,StrParam.c_str()); 
                break; 
            default: 
                ROS_INFO("flag value is not in range: [1,5]"); 
        } 
        return 0; 
    }

  • 相关阅读:
    通过网页源代码看“饭否”的网站架构
    SD2.0课程等待时候的摘抄
    SD2.0阿里妈妈的研发过程
    转:编程8字专静谦筹悟慎透恒
    SD2.0第一天总体印象
    SD2.0《Web2.0网站性能调优实践》貌似是抄袭的文章
    SD2.02个课合成一个课
    SD2.0第2天总体印象
    variant
    [zz]C++程序员的阅读清单
  • 原文地址:https://www.cnblogs.com/long5683/p/11383066.html
Copyright © 2020-2023  润新知