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

roslaunch xml <env> tag

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

shawnysh gravatar image

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

<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
4

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

  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

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

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/

SUMMARY
========

PARAMETERS
 * /rosdistro: indigo
 * /rosversion: 1.11.21

NODES
  /
    env_test (test_package/environment_reader.py)

auto-starting new master
process[master]: started with pid [11449]
ROS_MASTER_URI=http://localhost:11311/

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
done
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 ...
(more)
edit flag offensive delete link more

Comments

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 -0600 )edit

one more question, in official doc

$(env ENVIRONMENT_VARIABLE)
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 -0600 )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 -0600 )edit

you deserve a prize for this answer

anikk gravatar image anikk  ( 2023-08-06 20:03:48 -0600 )edit

Question Tools

2 followers

Stats

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

Seen: 9,038 times

Last updated: May 04 '17