Vectors in Behavior Trees
This how-to guide explains how to create, modify, and manipulate vectors (dynamic arrays) in MoveIt Pro Behavior Trees using the built-in vector manipulation Behaviors. This is just an overview, for more details see the Doxygen docs.
Overview
Vectors are dynamic arrays that can store multiple elements of the same type. MoveIt Pro provides several Behaviors for working with vectors, allowing you to create empty vectors, add/remove elements, access specific elements, and query vector properties. This is particularly useful for managing collections of poses, waypoints, or other data structures in your robotic applications.
All of these vector operations use the BT::Any
type from BehaviorTree.cpp.
Key Behaviors
ResetVector
This Behavior creates an empty vector and stores it in the blackboard for use by other Behaviors.
ResetVector
is currently the required approach for creating new vectors.
GetSizeOfVector
Use this Behavior to get the number of elements in a vector.
GetElementOfVector
Retrieves a specific element from a vector using its index. Supports negative indexing (e.g., -1 for the last element), similarly to Python.
Negative Indexing:
-1
= last element-2
= second-to-last element-3
= third-to-last element- And so on...
InsertInVector
Inserts a new element into a vector at the specified index. All elements after this index are shifted to make room for the new element.
When configuring the element
parameter, the default value is {json:{"__type":"string","value":"{element}"}
which is intentionally designed to allow for BT::Any types. A better approach is in development.
The element you insert must be the same type as the existing elements in the vector. If the vector is empty, any type can be inserted to establish the vector's type.
PushBackVector
Inserts a new element into a vector at the end of the vector, similar to C++'s push_back
.
The element you push must be the same type as the existing elements in the vector. If the vector is empty, any type can be pushed to establish the vector's type.
ReplaceInVector
Replaces an existing element in a vector at the specified index with a new element.
RemoveFromVector
Removes an element from a vector at the specified index. All elements after the removed element are shifted down to fill the gap.
Practical Example
For a complete example showing how to work with vectors in a Behavior Tree, check out the Vector and String Example
inside of the lab_sim
robot config. This example demonstrates creating a vector, adding poses, accessing elements, and modifying the vector.
Error Handling
The vector manipulation Behaviors will return FAILURE
status in these cases:
- Index out of bounds - Accessing an index that doesn't exist
- Type mismatch - Trying to insert/replace with incompatible element type
- Empty vector access - Trying to access elements in an empty vector
- Invalid input - Missing required input ports