py_trees.idioms module
Creators of common subtree patterns.
- py_trees.idioms.either_or(conditions: List[ComparisonExpression], subtrees: List[Behaviour], name: str = 'Either Or', namespace: str | None = None) Behaviour
Create an idiom with selector-like qualities, but no priority concerns.
Often you need a kind of selector that doesn’t implement prioritisations, i.e. you would like different paths to be selected on a first-come, first-served basis.
task_one = py_trees.behaviours.TickCounter(name="Subtree 1", duration=2) task_two = py_trees.behaviours.TickCounter(name="Subtree 2", duration=2) either_or = py_trees.idioms.either_or( name="EitherOr", conditions=[ py_trees.common.ComparisonExpression("joystick_one", "enabled", operator.eq), py_trees.common.ComparisonExpression("joystick_two", "enabled", operator.eq), ], subtrees=[task_one, task_two], namespace="either_or", )
Up front is an XOR conditional check which locks in the result on the blackboard under the specified namespace. Locking the result in permits the conditional variables to vary in future ticks without interrupting the execution of the chosen subtree (an example of a conditional variable may be one that has registered joystick button presses).
Once the result is locked in, the relevant subtree is activated beneath the selector. The children of the selector are, from left to right, not in any order of priority since the previous xor choice has been locked in and isn’t revisited until the subtree executes to completion. Only one may be active and it cannot be interrupted by the others.
The only means of interrupting the execution is via a higher priority in the tree that this idiom is embedded in.
- Args:
conditions: list of triggers that ultimately select the subtree to enable subtrees: list of subtrees to tick from in the either_or operation name: the name to use for this idiom’s root behaviour preemptible: whether the subtrees may preempt (interrupt) each other namespace: this idiom’s private variables will be put behind this namespace
- Raises:
ValueError if the number of conditions does not match the number of subtrees
If no namespace is provided, a unique one is derived from the idiom’s name.
See also
py-trees-demo-either-or
- py_trees.idioms.oneshot(behaviour: Behaviour, name: str = 'Oneshot', variable_name: str = 'oneshot', policy: OneShotPolicy = OneShotPolicy.ON_SUCCESSFUL_COMPLETION) Behaviour
Ensure that a particular pattern is executed through to completion just once.
Thereafter it will just rebound with the completion status.
Note
Set the policy to configure the oneshot to keep trying if failing, or to abort further attempts regardless of whether it finished with status
FAILURE
.- Args:
behaviour: single behaviour or composited subtree to oneshot name: the name to use for the oneshot root (selector) variable_name: name for the variable used on the blackboard, may be nested policy: execute just once regardless of success or failure, or keep trying if failing
- Returns:
Behaviour
: the root of the oneshot subtree
See also
- py_trees.idioms.pick_up_where_you_left_off(name: str, tasks: List[BehaviourSubClass]) Behaviour
Create an idiom that enables a sequence of tasks to pick up where it left off.
Rudely interrupted while enjoying a sandwich, a caveman (just because they wore loincloths does not mean they were not civilised), picks up his club and fends off the sabre-tooth tiger invading his sanctum as if he were swatting away a gnat. Task accomplished, he returns to the joys of munching through the layers of his sandwich.
Note
There are alternative ways to accomplish this idiom with their pros and cons.
a) The tasks in the sequence could be replaced by a factory behaviour that dynamically checks the state of play and spins up the tasks required each time the task sequence is first entered and invalidates/deletes them when it is either finished or invalidated. That has the advantage of not requiring much of the blackboard machinery here, but disadvantage in not making visible the task sequence itself at all times (i.e. burying details under the hood).
b) A new composite which retains the index between initialisations can also achieve the same pattern with fewer blackboard shenanigans, but suffers from an increased logical complexity cost for your trees (each new composite increases decision making complexity (O(n!)).