Ask Your Question

roslaunch xml <env> tag

asked 2017-05-04 06:21:28 -0500

shawnysh gravatar image

updated 2017-05-04 11:29:49 -0500

<env name="GAZEBO_MODEL_PATH" value="$(find ubr1_gazebo)/models:$(optenv GAZEBO_MODEL_PATH)" />

1, I am still confused with <env> tag though having read through the roslaunch xml tutorial, why to set the value like that ? Could u elaborate it for the script above?

2,Why multiple values are separated by colons, instead of spaces like in args tag and optenv tag ?

3, what's more, when do we need to use env and optenv args?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2017-05-04 13:55:18 -0500

  1. Without the full launch file from the snippet you posted, it's difficult to say exactly why they are using the <env> tag. Presumably, they are starting a node (gazebo_ros?) that uses the value of the GAZEBO_MODEL_PATH environment variable. They are using the <env> tag to modify the environment variables that one or more nodes are executed with.
  2. Environment variables (e.g. PATH, PYTHONPATH, ROS_PACKAGE_PATH, etc.) generally use colons to separate values if there are multiple values. This is a Unix concept not a ROS concept. The $(env) and $(optenv) and $(arg) substitution args can expand values using environment variables from the environment that roslaunch was invoked from. If you use multiple words separated with spaces for the default_value in the optenv substitution arg, then that is exactly what the value will expand to (assuming the ENVIRONMENT_VARIABLE value is not set).
  3. You don't ever need to use either of these, but if you'd like environment variables that have been set in your terminal to expand into values in a launch file, then you could use the substitution args. As an example, let's assume you've written a node that takes in an argument, and that you are going to pass a value for this argument to the node using the args attribute of the <node> tag. If you used the optenv expansion arg to set the value of the argument, then, you'd be able to decide the value of the argument simply by setting an environment variable.

As an example consider the following launch file that runs a single Python script:

Launch file

  <env name="TESTVAR" value="$(optenv INTVAR /home/user/default_directory1:/home/user/default_directory2)" />
  <node name="env_test" type="" pkg="test_package" output="screen"/>

Python script

#!/usr/bin/env python
import os
print "\r\n"
print "TESTVAR = ", os.environ['TESTVAR']
print "\r\n"

Now consider the following bash commands to illustrate the functionality of the environment variables and the substitution args:

user@hostname:~/Desktop⟫ roslaunch test_package tmp.launch
... logging to /home/user/.ros/log/b401c412-30fa-11e7-a2a4-28d2448c624c/roslaunch-hostname-11437.log
Checking log directory for disk usage. This may take awhile.
Press Ctrl-C to interrupt
Done checking log file disk usage. Usage is <1GB.

started roslaunch server http://hostname.local:35062/


 * /rosdistro: indigo
 * /rosversion: 1.11.21

    env_test (test_package/

auto-starting new master
process[master]: started with pid [11449]

setting /run_id to b401c412-30fa-11e7-a2a4-28d2448c624c
process[rosout-1]: started with pid [11462]
started core service [/rosout]
process[env_test-2]: started with pid [11465]

TESTVAR =  /home/user/default_directory1:/home/user/default_directory2

[env_test-2] process has finished cleanly
log file: /home/user/.ros/log/b401c412-30fa-11e7-a2a4-28d2448c624c/env_test-2*.log
^C[rosout-1] killing on exit
[master] killing on exit
shutting down processing monitor...
... shutting down processing monitor complete
user@hostname:~/Desktop⟫ export INTVAR=/different/directory
user@hostname:~/Desktop⟫ roslaunch test_package tmp.launch
... logging to /home/user/.ros/log/c58bed7a-30fa-11e7-9984-28d2448c624c/roslaunch-hostname-11933.log
Checking log directory for disk usage. This may take awhile.
Press Ctrl-C to interrupt
Done checking log ...
edit flag offensive delete link more


Thanks for your detailed explanation! You are really sweet and thoughtful! Thanks a lot! You are a great teacher!

shawnysh gravatar image shawnysh  ( 2017-05-05 00:30:48 -0500 )edit

one more question, in official doc

Substitute the value of a variable from the current environment. The launch will fail if environment variable is not set. This value cannot be overridden by <env> tags.

Why it says 'This value cannot be overridden by env tags'?

shawnysh gravatar image shawnysh  ( 2017-05-05 00:37:06 -0500 )edit

This means that the $(env) substitution arg will only read from the environment that the launch file was launched from. If you were to set an environment variable with the <env> tag, the $(env) substitution arg would not pick up on that variable.

jarvisschultz gravatar image jarvisschultz  ( 2017-05-05 07:47:35 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools



Asked: 2017-05-04 06:21:28 -0500

Seen: 6,082 times

Last updated: May 04 '17