Why Substitutions?


Although I understand how to use substitutions I don’t see the reason for them. Why doesn’t the ROS2 launch framework allow for example to use the value of an argument directly? Does the launch system generate the code to be executed in two passes so that in the first one it does not know the values of said variables?
Or put another way, how does the launch framework work internally to require the use of substitutions?

Please, can someone clarify my doubt?


Hello @joseecm ,

Could you please share here a specific example using substitutions so that we can have a better context?

Let’s take for example the following launch file
to which we pass the argument “counter” from the command line.

from launch import LaunchDescription
from launch.actions import LogInfo
from launch.substitutions import LaunchConfiguration

def generate_launch_description():
counter = LaunchConfiguration(‘counter’, default=0)
counter = counter + 1

return LaunchDescription([

When trying to run it, an error occurs due to the line


because obviously the counter variable is of type LaunchConfiguration and cannot be converted to an integer. The only way to directly access the value of an argument to be able to perform some operation on it is by using an OpaqueFunction action and within the function that it calls, access the value through an instructions like the following:

value = LaunchConfiguration(‘counter’).perform(context)
value = value + 1

My question is why the launch framework forces to do this. That is, I would like to know what it does internally that makes it necessary to use substitutions. Why couldn’t something similar to .perform(context) be used outside of an OpaqueFunction action?


This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.