ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange |
1 | initial version |
I ended up using a custom Substitution. Not sure if that's the cleanest solution, but it worked for me.
class ConditionalText(Substitution):
def __init__(self, text, condition):
self.text = text
self.condition = condition
def perform(self, context: 'LaunchContext') -> Text:
if self.condition:
return self.text
else:
return ''
Putting everything together:
def generate_launch_description():
use_prefix = (EnvironmentVariable('SOME_VAR', default_value='0').perform(LaunchContext()) == '1')
n = launch_ros.actions.Node(
package='my_pkg',
executable='my_node',
prefix=ConditionalText('some_prefix', use_prefix),
)
return launch.LaunchDescription([n])
2 | No.2 Revision |
Edit: Just stumbled across a simpler solution in Gazebo's launch files: https://github.com/shiveshkhaitan/gazebo_ros_pkgs/blob/a6e395195bc878a76200fb948cc8cfce414b98fe/gazebo_ros/launch/gzclient.launch.py#L82
def generate_launch_description():
use_prefix = (EnvironmentVariable('SOME_VAR', default_value='0').perform(LaunchContext()) == '1')
n = launch_ros.actions.Node(
package='my_pkg',
executable='my_node',
prefix=PythonExpression(['"some_prefix" if "1" == "', EnvironmentVariable('SOME_VAR', default_value='0'), '" else ""']),
)
return launch.LaunchDescription([n])
Anyway, here's my original solution:
I ended up using a custom Substitution. Not sure if that's the cleanest solution, but it worked for me.
class ConditionalText(Substitution):
def __init__(self, text, condition):
self.text = text
self.condition = condition
def perform(self, context: 'LaunchContext') -> Text:
if self.condition:
return self.text
else:
return ''
Putting everything together:
def generate_launch_description():
use_prefix = (EnvironmentVariable('SOME_VAR', default_value='0').perform(LaunchContext()) == '1')
n = launch_ros.actions.Node(
package='my_pkg',
executable='my_node',
prefix=ConditionalText('some_prefix', use_prefix),
)
return launch.LaunchDescription([n])