ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

string argument roslaunch

asked 2014-06-19 14:03:18 -0500

Pototo gravatar image

Hi,

I am trying to pass arguments to my launch file, but I can never get the values that I sent: for instance, I declared my variables this way:

<?xml version="1.0"?>
<launch>

    <arg name="my_KP" />
    <arg name="my_KI" />
    <arg name="my_KD" />
    <arg name="my_EL" />
    <arg name="my_ADT" />

    <node name="en_pub" pkg="driver" type="en_repub.py" />

    <node name="left_motor" pkg="driver" type="left" output="screen">
        <!--
        <param name="KP" type="string" value="$(arg my_KP)" />
        <param name="KI" type="string" value="$(arg my_KI)"/>
        <param name="KD" type="string" value="$(arg my_KD)"/>
        <param name="EL" type="string" value="$(arg my_EL)"/>
        <param name="ADT" type="string" value="$(arg my_ADT)"/>
        -->
    </node>

    <node name="right_motor" pkg="driver" type="right" output="screen">
        <!--
        <param name="KP" type="string" value="$(arg my_KP)" />
        <param name="KI" type="string" value="$(arg my_KI)"/>
        <param name="KD" type="string" value="$(arg my_KD)"/>
        <param name="EL" type="string" value="$(arg my_EL)"/>
        <param name="ADT" type="string" value="$(arg my_ADT)"/>
        -->
    </node>

And on my node I did this:

    ros::NodeHandle p_nh;

    string KP = "1000";
    string KI = "240";
    string KD = "12000";
    string EL = "30000";
    string ADT = "1";

    //This allows for setting params instead of recompile to change
    p_nh.param<string>("KP", KP, "1000");
    p_nh.param<string>("KI", KI, "240");
    p_nh.param<string>("KD", KD, "12000");
    p_nh.param<string>("EL", EL, "30000");
    p_nh.param<string>("ADT", ADT, "1");

But my program does not seem to catch them. I only get the assigned default values.

What am I doing wrong?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2014-06-19 17:28:30 -0500

updated 2014-06-19 17:35:07 -0500

I believe that the problem is that in your launch file, the params are all private params (as they are in the node tag). Therefore you can either declare a private nodehandle i.e. your first line of C++ would read

ros::NodeHandle p_nh("~");

or you could use the "bare" versions from the API i.e. your parameter calls would read something like

ros::param::param<string>("~KI", KI, "240");

Here's a complete C++ file that does it both ways:

#include <ros/ros.h>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
    ros::init(argc, argv, "test");
    ros::NodeHandle nh("~");

    string KP = "1000";
    string KI = "240";

    nh.param<string>("KP", KP, "1000");
    ros::param::param<string>("~KI", KI, "240");

    ROS_INFO("KP = %s", KP.c_str());
    ROS_INFO("KI = %s", KI.c_str());

    return 0;
}
edit flag offensive delete link more

Comments

In your attached launch file, your params are commented out. I'm assuming you didn't mean to post it this way.

jarvisschultz gravatar image jarvisschultz  ( 2014-06-19 17:36:35 -0500 )edit

Great! This works, thanks!

Pototo gravatar image Pototo  ( 2014-06-20 15:54:19 -0500 )edit

Why isn't this in the tutorials? I went crazy looking for this 0.o

Robocop87 gravatar image Robocop87  ( 2015-02-16 03:22:39 -0500 )edit
1
jarvisschultz gravatar image jarvisschultz  ( 2015-02-16 14:36:07 -0500 )edit
1

Here's a description of private params in general: http://wiki.ros.org/Parameter%20Serve... And here's the roslaunch XML documentation on <param> describing its behavior inside of the <node> tag http://wiki.ros.org/roslaunch/XML/param

jarvisschultz gravatar image jarvisschultz  ( 2015-02-16 14:37:48 -0500 )edit
1

Hopefully those links clear up any remaining confusion!

jarvisschultz gravatar image jarvisschultz  ( 2015-02-16 14:49:54 -0500 )edit

Question Tools

1 follower

Stats

Asked: 2014-06-19 14:03:18 -0500

Seen: 4,755 times

Last updated: Jun 19 '14