Set string value based on boolean argument in ROS2 Python Launch File
Suppose I have a boolean argument foo
def generate_launch_description() -> launch.LaunchDescription:
foo = LaunchConfiguration("foo")
foo_arg = DeclareLaunchArgument("foo_arg", default_value="False")
First part of my question: let's say I have a node that takes in a string parameter. And I would like to set the value of the string parameter conditioned on the value of foo
. How do I achieve this?
node = Node(
package="mypkg",
executable="mynode",
output="screen",
parameters=[{
"my_string_param": # <...> value should depend on `foo`
}]
)
ld.add_action(node)
Here is the second part of my question.
What if, let's say, the value to my_string_param
is the output of some function func(str) -> str
, so the node block looks like:
node = Node(
package="mypkg",
executable="mynode",
output="screen",
parameters=[{
"my_string_param": func(temp_str) # temp_str's value depends on `foo`
}]
)
ld.add_action(node)
and I would like to set temp_str
to be dependent on the truth value of foo
. How do I achieve this?
Assume that the node itself is complex and there is no way to change how it behaves.
Asked by zkytony on 2023-06-13 09:55:09 UTC
Answers
You have two options here:
You can use the OpaqueFunction idiom, where you can retrieve the parameter as a Python Boolean and do your logic. There are several tutorials out there on this approach.
Or, as you only have a Boolean argument, run it twice with opposite conditions:
Node(
package="my_pkg",
executable="my_node",
parameters=[{"my_string_param": "value if True"}],
condition=IfCondition(your_boolean_arg)
)
Node(
package="my_pkg",
executable="my_node",
parameters=[{"my_string_param": "value if False"}],
condition=UnlessCondition(your_boolean_arg)
)
Asked by Olivier Kermorgant on 2023-06-15 04:07:56 UTC
Comments