• ros launch文件编写和节点启动顺序控制


    ROS可以通过launch文件进行节点的管理、初始参数的设置,但是launch文件不能指定节点的启动顺序,因此本文简单介绍下通过launch进行节点启动管理,通过shell来控制节点启动顺序。

    1,我将读取参数的代码片段放在了ros::init(argc,argv,"node_name")之后,首先定义变量类型,然后用ros::param::get进行参数获取。其中"~"表示该参数是一个私有参数,即在launch文件中,其应该写在

    <node pkg=....

     ......

     /node>

    之间。更全面的关于私有变量、命名空间会在后文有解释。

    如下示例:

        ros::init(argc,argv,"PCAN_Test");
        init();
    
        //parameter load test start ......
    
        string dev_name;
        ros::param::get("~dev_name",dev_name);
        ROS_INFO("PCAN Device:%s",dev_name.c_str());
    
        bool fifo_send_on=false;
        ros::param::get("~fifo_send_on",fifo_send_on);
        ROS_INFO("FIFO send :%s",fifo_send_on==false?"false":"true");
    
        bool display_on=false;
        ros::param::get("~display_on",display_on);
        ROS_INFO("Data display:%s",display_on==false?"false":"true");
        //parameter load test end ......
    定义了三个变量,dev_name用来配置pcan usb的端口,fifo_send_on和display则控制是否fifo发送,是否打开数据显示。此次dev_name使用时dev_name.c_str()可以匹配const char类型,该操作是取出string 存储的串地址,内容和string相同。

        if(dev_name.c_str() !="")
            pcan_handle = funLINUX_CAN_Open(dev_name.c_str(), O_RDWR );
        else
        {
            pcan_handle = funLINUX_CAN_Open(szDevNode, O_RDWR );//use mapping function
            dev_name=DEFAULT_NODE;
        }
        //judge whether the call is success.if pcan_handle=null,the call would be failed
        if(pcan_handle){
            printf("CAN Bus test: %s have been opened
    ", dev_name.c_str());
            errno = funCAN_VersionInfo(pcan_handle, txt);
            if (!errno)
                printf("CAN Bus test: driver version = %s
    ", txt);
            else {
                perror("CAN Bus test: CAN_VersionInfo()");
            }
            if (wBTR0BTR1) {
                    errno = funCAN_Init(pcan_handle, wBTR0BTR1, nExtended);
                    if (errno) {
                        perror("CAN Bus test: CAN_Init()");
                    }
                    else
                        printf("Device Info: %s; CAN_BAUD_1M; CAN_INIT_TYPE_ST
    ", dev_name.c_str());
                }
        }
        else
            printf("CAN Bus test: can't open %s
    ", dev_name.c_str());
    
        //initial a talker to publish the force data and can id.
        ros::NodeHandle force_handle;
        ros::Publisher force_pub=force_handle.advertise<beginner_tutorials::ForceData>("ArrayForcePUB",1);//advertise a topic named "ArrayForceDEV"
        ros::Rate loop_rate(500);
        //data receive test
        while(ros::ok())
        {   //HANDLE h,ros::Publisher pub,int server_fifo_fd,bool display_on,bool publish_on,bool fifo_send_on
            read_loop(pcan_handle,force_pub,server_fifo_fd,display_on,true,fifo_send_on);
            if(fifo_send_on)  printf("Send Count No.%d	
    ",send_count);
            ros::spinOnce();
            loop_rate.sleep();
        }
    2,launch文件中则如下进行设置,pkg后对应文件的包名,type后是CMakeList.txt中对应该文件add_executable(pcan_test src/pcan_test)中可执行文件的名称,在python中则是文件名,因为python的可执行文件就是文件本身(解释性语言,同matlab),所以此次若用c++编程不要误写为文件名全称,name表示节点启动后的名称,该名称会覆盖ros::init中初始化的名称,output后参数表示从屏幕输出打印信息,否则,打印信息会存储到某个临时文件里。

    参数里name是ros::param::get()中第一个字符串去掉“~”后的名称,launch会在运行时进行查找匹配,type是变量类型,value是具体值。以下为launch文件全文。

    <launch>
    	<node pkg="beginner_tutorials" type="pcan_test" name="PCAN_Test" output="screen">
    	<param name="dev_name" type="string" value="/dev/pcan1" />
    	<param name="fifo_send_on" type="bool" value="false" />	
    	<param name="display_on" type="bool" value="false" />	
    	</node>	
    </launch>
    注:只需要在src下建立launch文件夹,然后在其中创建launch文件即可,不需要做其他工作。
    3,上面关于ros::param::get内容介绍较为简单,一个更为全面的launch的例子如下,包含launch中私有变量和公有变量例子

    <param name="camera_topic_root" value="/camera/image_rect">
    <group ns="group_ns">
        <param name="camera_topic_ns" value="/camera/image_rect">
        <node name="node_name" pkg="foo" type="bar" >
            <param name="camera_topic_private" value="/camera/image_rect">
        </node>
    </group>
    group为一个节点组,可以通过某种设置进行整个组内的节点启停控制,参考ROS launch整理中详细介绍。 ns为命名空间。
    则用ros::param::get时有如下三种如下包含关系的变量,其中
    /camera_topic_root
    /group_ns/camera_topic_ns
    /group_ns/node_name/camera_topic_private
    因此如果通过ros::param::get来进行参数获取,则应如下所示:
    ros::param::get("/camera_topic_root",camera_topic);    // /camera_topic_root
    ros::param::get("camera_topic_ns",camera_topic);       // /group_ns/camera_topic_ns
    ros::param::get("~camera_topic_private",camera_topic); // /group_ns/node_name/camera_topic_private
    4,在ubuntu下进行节点启动顺序控制的简单策略就是通过shell实现。

    新建文件后命名为xxx.sh。

    #!/bin/bash
    
    roslaunch bhand_controller bhand_controller.launch &
    sleep 5
    echo "bhand controller starting success!"
    
    roslaunch beginner_tutorials bhand_force_control.launch &
    sleep 0.1
    wait
    exit 0
    第一行表示用bash执行,sleep表示演示,echo用来输出一定内容,有关ubuntu shell的其他操作,注意不用忘记句子后的"&"符号,可以自行查询相关资料。因此策略就是如果某个节点必须先执行,可以单独为其写一个launch文件,然后通过shell控制先行启动。

    编写保存后,在终端要给xxx.sh执行权限,sudo chmod a+x xxx.sh,之后可通过./xxx.sh进行启动,xxx代表任意字符。

  • 相关阅读:
    NABCD(校园包车)
    作业5.2~5.3
    作业5.1
    作业
    JAVA EE社团管理升级版-数据库设计
    JAVA EE社团管理升级版-微信小程序端说明文档
    社团项目软件展示
    社团项目个人总结
    北京地铁规划项目总结
    地铁出行线路规划项目设计
  • 原文地址:https://www.cnblogs.com/siahekai/p/11000805.html
Copyright © 2020-2023  润新知