Carollour
Carollour

Reputation: 85

ROS launchfile to set variable value

I have two different but related questions

1st: Is it possible to set the value of a variable using the launch file? I read about it online but I couldn't understand how to use it.

2nd: I couldn't find anything on how to use the launch file to set the value of a variable in a message. for instance if I have:

mymessagevector.msg:

mymessage myvector[2]

mymessage.msg:

int32 value
string ID

Is there a way to set the myvector[0].ID="ID1" and myvector[1].ID="ID2" and always publish that without having to define it everytime I sent the message to a topic?

Upvotes: 0

Views: 1283

Answers (1)

alextoind
alextoind

Reputation: 1203

The first answer could be easily found in the roslaunch documentation (and relative subsections).

A simple example is the following:

<launch>
  <node pkg="pkg_name" type="node_type" name="node_name">
    <param name="var_name" type="var_type" value="var_value" />
  </node>
</launch>

You could find the meanings of all these parameters in the following links: param and node.

I am sorry, but I don't know how could be initialized a "first message" using roslaunch (if it is possible...). You could use a variable instead.

UPDATE:

I'll show you an example in a c++ file

/* Retrieves information from parameters (if there are some specified) */

/* this handle let you access all the parameters of the node (and other stuffs) */
ros::NodeHandle node_handle = new ros::NodeHandle("~");

/* for example I need to retrieve the name of the serial port which is a parameter of my node */
std::string serial_port = "";
/* this member function searches for the parameter and stores it into 'serial_port' if it exists, 
 * otherwise it uses the third parameter of the function (a default value) */
node_handle->param("<name_of_param_in_roslaunch>", serial_port, std::string(DEFAULT_VALUE));

You can find the ros::NodeHandle documentation here.

Upvotes: 1

Related Questions