Skip to content

Commit d81952b

Browse files
bmagyarkscottz
andauthored
Add section on boolean substitutions (#6864)
* Add section on boolean substitutions * Add ref to ROS1 migration * truthy -> true Co-authored-by: Katherine Scott <katherineAScott@gmail.com> Signed-off-by: Bence Magyar <bence.magyar.robotics@gmail.com> * Add note on string-representation comparison --------- Signed-off-by: Bence Magyar <bence.magyar.robotics@gmail.com> Co-authored-by: Katherine Scott <katherineAScott@gmail.com>
1 parent 3f92805 commit d81952b

2 files changed

Lines changed: 82 additions & 0 deletions

File tree

source/How-To-Guides/Migrating-from-ROS1/Migrating-Launch-Files.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,9 @@ There are, however, some changes w.r.t. ROS 1:
428428
It looks at configurations defined either with ``arg`` or ``let`` tag.
429429
* ``eval`` and ``dirname`` substitutions require escape characters for string values, e.g. ``if="$(eval '\'$(var variable)\' == \'val1\'')"``.
430430
You can also use HTML escapes like ``&quot;`` .
431+
* Boolean predicates can also be expressed directly with the ``equals``, ``not-equals``, ``and``, ``or``, ``any``, and ``all`` substitutions.
432+
For example, ``if="$(equals $(var variable) val1)"`` is equivalent to ``if="$(eval '\'$(var variable)\' == \'val1\'')"``.
433+
See :ref:`Boolean substitutions <BooleanSubstitutions>` for details.
431434
* ``eval`` does not pass configurations ( ``arg`` ) as local Python variables.
432435
They have to be accessed via ``$(var name)``.
433436
* The argument of ``eval`` has to be a quoted string in ROS 2.

source/Tutorials/Intermediate/Launch/Using-Substitutions.rst

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,85 @@ Now you can pass the desired arguments to the launch file as follows:
455455
456456
$ ros2 launch launch_tutorial example_substitutions_launch.py turtlesim_ns:='turtlesim3' use_provided_red:='True' new_background_r:=200
457457
458+
.. _BooleanSubstitutions:
459+
460+
Boolean substitutions
461+
---------------------
462+
463+
In addition to ``$(eval <python-expression>)``, a set of dedicated boolean substitutions is available for comparing values and combining the results.
464+
They can be used anywhere a substitution is allowed, including the ``if`` and ``unless`` attributes of any action.
465+
466+
.. note::
467+
468+
Comparison is performed on the string representation of each argument.
469+
470+
.. list-table::
471+
:header-rows: 1
472+
:widths: 25 30 45
473+
474+
* - XML / YAML name
475+
- Python class
476+
- Description
477+
* - ``$(equals A B)``
478+
- ``EqualsSubstitution``
479+
- Resolves to ``'true'`` if ``A`` equals ``B``, otherwise ``'false'``.
480+
* - ``$(not-equals A B)``
481+
- ``NotEqualsSubstitution``
482+
- Resolves to ``'true'`` if ``A`` does not equal ``B``, otherwise ``'false'``.
483+
* - ``$(and A B)``
484+
- ``AndSubstitution``
485+
- Logical AND of two boolean substitutions.
486+
* - ``$(or A B)``
487+
- ``OrSubstitution``
488+
- Logical OR of two boolean substitutions.
489+
* - ``$(any A B ...)``
490+
- ``AnySubstitution``
491+
- Resolves to ``'true'`` if any argument is true.
492+
* - ``$(all A B ...)``
493+
- ``AllSubstitution``
494+
- Resolves to ``'true'`` only if every argument is true.
495+
496+
The ``if`` predicate from the previous section can also be expressed using boolean substitutions instead of a Python expression:
497+
498+
.. tabs::
499+
500+
.. group-tab:: XML
501+
502+
.. code-block:: xml
503+
504+
<executable cmd="ros2 param set /turtlesim background_r $(var new_background_r)"
505+
if="$(and $(equals $(var new_background_r) 200) $(var use_provided_red))"/>
506+
507+
.. group-tab:: YAML
508+
509+
.. code-block:: yaml
510+
511+
- executable:
512+
cmd: ros2 param set /turtlesim background_r $(var new_background_r)
513+
if: $(and $(equals $(var new_background_r) 200) $(var use_provided_red))
514+
515+
.. group-tab:: Python
516+
517+
.. code-block:: python
518+
519+
from launch.conditions import IfCondition
520+
from launch.substitutions import AndSubstitution, EqualsSubstitution, LaunchConfiguration
521+
522+
ExecuteProcess(
523+
cmd=[[
524+
FindExecutable(name='ros2'),
525+
' param set ',
526+
'/turtlesim background_r ',
527+
LaunchConfiguration('new_background_r'),
528+
]],
529+
condition=IfCondition(
530+
AndSubstitution(
531+
EqualsSubstitution(LaunchConfiguration('new_background_r'), '200'),
532+
LaunchConfiguration('use_provided_red'),
533+
)
534+
),
535+
)
536+
458537
Documentation
459538
-------------
460539

0 commit comments

Comments
 (0)