Free Shipping for orders over ₹999

support@thinkrobotics.com | +91 93183 94903

FlexBE for ROS 2: A Tutorial on Behavior Trees

FlexBE for ROS 2: A Tutorial on Behavior Trees

1 comment


Behavior trees have revolutionized how we approach complex robotic task coordination, and FlexBE (Flexible Behavior Engine) stands at the forefront of this evolution in ROS 2. This comprehensive tutorial will guide you through understanding and implementing FlexBE for creating sophisticated robotic behaviors using hierarchical state machines and behavior tree concepts.

FlexBE is a high-level behavior engine coordinating the capabilities of a robot in order to solve complex tasks. Behaviors are modeled as hierarchical state machines (HFSM) where states correspond to active actions and transitions describe the reaction to outcomes. Unlike traditional behavior trees, FlexBE offers exceptional operator integration and collaborative autonomy features that make it particularly powerful for real-world robotic applications.

Understanding FlexBE and Behavior Trees

FlexBE bridges the gap between pure behavior trees and hierarchical state machines, creating what researchers call the "mythical HFSMBTH" (Hierarchical Finite State Machine Behavior Tree Hybrid). This approach combines the modularity and readability of behavior trees with the precision and control of state machines.

The main advantage of FlexBE over similar approaches is the good operator integration and an intuitive user interface. Besides executing behaviors in full autonomy, the operator can restrict execution of certain transitions or trigger them manually. Furthermore, FlexBE supports modifying the whole structure of a behavior during its execution without restarting it.

Key Features of FlexBE

Collaborative Autonomy: FlexBE enables seamless human-robot collaboration where operators can intervene during behavior execution, modify transitions, or take control when needed.

Runtime Modification: The system supports real-time behavior modification without stopping execution, allowing for dynamic adaptation to changing conditions.

Hierarchical Design: Complex behaviors are broken down into manageable hierarchical state machines that can be reused and combined.

Visual Interface: The FlexBE WebUI provides both a graphical editor for creating behaviors and a runtime control interface for monitoring and controlling execution.

Setting Up FlexBE for ROS 2

Installation Requirements

Before installing FlexBE, ensure you have a recent ROS 2 distribution installed. FlexBE is tested on ROS 2 Humble under Ubuntu 22.04, though it supports other recent distributions.

To build from source, execute the following commands to install FlexBE for ROS 2 systems:

bash

# Create a workspace

mkdir -p ~/flexbe_ws/src

cd ~/flexbe_ws/src


# Clone FlexBE repositories

git clone https://github.com/FlexBE/flexbe_behavior_engine.git

git clone https://github.com/FlexBE/flexbe_webui.git


# Build the workspace

cd ~/flexbe_ws

colcon build --symlink-install


# Source the workspace

source install/setup.bash

This will clone a project template (requires internet access) that contains examples and proper package definitions, and create the ROS 2 package structure and three subfolders for custom state implementations and HFSM-based behaviors.

FlexBE WebUI Installation

This release of the FlexBE Behavior Engine requires version 4.1+ of the FlexBE UI. It is recommended to install the FlexBE WebUI user interface which provides both editing and runtime control capabilities through a web-based interface.

Creating Your First FlexBE Behavior

Project Structure

FlexBE organizes behaviors into three main components:

States: Individual action nodes that perform specific tasks Behaviors: Complete hierarchical state machines composed of states and transitions Projects: Collections of related behaviors and states for specific applications

Basic State Implementation

States in FlexBE inherit from base classes and implement specific functionality. Here's a simple example of a custom state:

python

from flexbe_core import EventState, Logger


class SayHelloState(EventState):

    def __init__(self, message="Hello World"):

        super(SayHelloState, self).__init__(outcomes=['done'])

        self._message = message

    

    def execute(self, userdata):

        Logger.loginfo(f"Robot says: {self._message}")

        return 'done'

Building Hierarchical State Machines

FlexBE behaviors are constructed using the visual editor or programmatically. The hierarchical nature allows for complex nesting of behaviors:

python

# Behavior structure example

class NavigationBehavior(Behavior):

    def __init__(self):

        super(NavigationBehavior, self).__init__()

        self.name = 'Navigation Behavior'

        

        # Add states to the behavior

        self.add_state('PlanPath', PlanPathState(), 

                      transitions={'success': 'ExecutePath', 

                                 'failed': 'RecoveryBehavior'})

        

        self.add_state('ExecutePath', ExecutePathState(),

                      transitions={'success': 'done',

                                 'failed': 'RecoveryBehavior'})

FlexBE vs Traditional Behavior Trees

Advantages of FlexBE Approach

Operator Integration: Unlike pure behavior trees, FlexBE provides built-in mechanisms for human oversight and intervention.

State Persistence: Hierarchical state machines maintain state information more effectively than traditional behavior trees that restart from the root.

Debugging Capabilities: The visual interface provides excellent debugging and monitoring tools that show exact execution paths and state transitions.

Flexible Execution: Operators can modify execution flow, force transitions, or pause execution at any point.

Integration with ROS 2 Ecosystem

FlexBE integrates seamlessly with ROS 2 navigation, manipulation, and perception stacks. The Flexible Behavior Trees system provides specific integration with BehaviorTree.CPP, allowing FlexBE to orchestrate the execution of different BTs while the BehaviorTree.CPP framework executes the BTs and passes data between nodes.

Practical Applications

Robot Navigation with FlexBE

Consider a navigation scenario where a robot must navigate to multiple waypoints while handling obstacles and recoveries:

python

class MultiWaypointNavigation(Behavior):

    def __init__(self):

        super(MultiWaypointNavigation, self).__init__()

        

        # Concurrent execution for monitoring and navigation

        with Concurrence(outcomes=['completed', 'failed'],

                        default_outcome='failed',

                        outcome_mapping={'completed': {'NavigateWaypoints': 'completed'},

                                       'failed': {'NavigateWaypoints': 'failed',

                                                'MonitorBattery': 'low_battery'}}) as cc:

            

            Concurrence.add('NavigateWaypoints', 

                           WaypointNavigationSM(),

                           remapping={'waypoints': 'target_waypoints'})

            

            Concurrence.add('MonitorBattery', 

                           BatteryMonitorState(),

                           remapping={'battery_level': 'current_battery'})

Collaborative Manipulation Tasks

FlexBE excels in manipulation scenarios where human oversight is crucial:

python

class CollaborativePickAndPlace(Behavior):

    def __init__(self):

        super(CollaborativePickAndPlace, self).__init__()

        

        # Request operator confirmation before critical actions

        self.add_state('RequestPickConfirmation',

                      ConfirmationState(text="Confirm object pick?"),

                      transitions={'yes': 'ExecutePick',

                                 'no': 'WaitForNewTarget'})

Advanced FlexBE Features

Runtime Behavior Modification

One of FlexBE's most powerful features is the ability to modify behaviors during execution. This enables:

Dynamic Recovery: Adding new recovery behaviors when unexpected situations arise Mission Updates: Modifying objectives without stopping the current mission Parameter Adjustment: Changing behavior parameters based on real-time conditions

Autonomy Levels

FlexBE implements configurable autonomy levels:

Off: Complete manual control - operator must confirm every transition Low: Semi-autonomous with operator confirmation for critical decisions High: Autonomous execution with operator notification of important events Full: Complete autonomous operation with minimal operator interaction

Integration with Modern ROS 2 Tools

BehaviorTree.CPP Integration

FlexBE can orchestrate BehaviorTree.CPP implementations through the Flexible Behavior Trees package. This allows combining the strengths of both approaches:

xml

<!-- XML configuration for BT integration -->

<root main_tree_to_execute="NavigationBT">

  <BehaviorTree ID="NavigationBT">

    <Sequence>

      <Action ID="PlanPath" planner="GridBased"/>

      <Action ID="FollowPath" controller="FollowPath"/>

      <Action ID="CheckGoalReached"/>

    </Sequence>

  </BehaviorTree>

</root>

Navigation2 Integration

FlexBE integrates with ROS 2 Navigation2 stack, allowing sophisticated navigation behaviors that combine path planning, obstacle avoidance, and recovery behaviors under unified operator control.

Best Practices and Design Patterns

State Design Principles

Single Responsibility: Each state should perform one specific task Clear Outcomes: Define clear, meaningful outcome names that describe the result Error Handling: Always include failure outcomes and appropriate error handling Reusability: Design states to be reusable across different behaviors

Behavior Composition

Modular Design: Break complex behaviors into smaller, manageable sub-behaviors Clear Interfaces: Use userdata effectively to pass information between states Operator Feedback: Provide meaningful feedback to operators at critical decision points

Troubleshooting Common Issues

State Transition Problems

When states don't transition correctly, check outcome definitions and transition mappings. Use the FlexBE monitor to visualize execution flow and identify issues.

Performance Optimization

For high-frequency operations, consider using concurrent execution patterns and minimize state overhead by grouping related operations.

Integration Challenges

When integrating with external ROS 2 nodes, ensure proper synchronization and handle timeout scenarios gracefully.

Conclusion

FlexBE for ROS 2 represents a significant advancement in robotic behavior coordination, offering the perfect balance between autonomous operation and human oversight. Its hierarchical state machine approach, combined with behavior tree concepts, provides a powerful framework for creating complex, maintainable robotic behaviors.

The system's strength lies in its collaborative autonomy features, runtime modification capabilities, and excellent debugging tools. Whether you're developing navigation behaviors, manipulation tasks, or complex multi-robot coordination, FlexBE provides the tools and flexibility needed for real-world robotic applications.

As robotics continues to evolve toward more collaborative and adaptive systems, FlexBE's approach to behavior coordination will become increasingly valuable. Its integration with modern ROS 2 tools and continued development make it an excellent choice for robotics projects requiring sophisticated behavior management.

Frequently Asked Questions

1. How does FlexBE differ from standard BehaviorTree.CPP implementations?

FlexBE combines hierarchical state machines with behavior tree concepts, offering superior operator integration and runtime modification capabilities. While BehaviorTree.CPP focuses on pure behavior trees executed autonomously, FlexBE enables collaborative autonomy where operators can intervene, modify transitions, and adjust behaviors during execution without stopping the system.

2. Can I integrate existing ROS 2 action servers and services with FlexBE states?

Yes, FlexBE provides wrapper classes for seamless integration with ROS 2 action servers and services. You can create custom states that call existing ROS 2 interfaces, allowing you to leverage your existing robotics stack while benefiting from FlexBE's behavior coordination capabilities.

3. What's the performance overhead of using FlexBE compared to direct ROS 2 programming?

FlexBE introduces minimal performance overhead while providing significant benefits in behavior organization and debugging. The hierarchical state machine execution is efficient, and the ability to modify behaviors at runtime often eliminates the need for system restarts, improving overall operational efficiency.

4. How do I handle failures and recovery behaviors in FlexBE?

FlexBE excels at failure handling through its hierarchical design. You can implement recovery behaviors as separate state machines that are triggered by failure outcomes. The system supports nested recovery strategies and allows operators to intervene in recovery decisions based on the configured autonomy level.

5. Is FlexBE suitable for real-time robotic applications?

FlexBE is designed for task-level coordination rather than real-time control loops. It works best when coordinating higher-level behaviors while delegating time-critical operations to dedicated ROS 2 nodes. For real-time applications, use FlexBE to orchestrate behaviors while maintaining real-time control at the hardware interface level.

 

1 comment

  • Thank you for sharing.

    - Shambhuraj Anil Mane

Post a comment