📖 Scenario: You are programming a simple robot using ROS (Robot Operating System). You want to control the robot's behavior using a behavior tree. Behavior trees help robots decide what to do step-by-step, like following a recipe.In this project, you will create a basic behavior tree structure in ROS using Python. The robot will have simple tasks like checking battery, moving forward, and stopping.
🎯 Goal: Build a simple behavior tree in ROS Python that checks the battery level, moves the robot forward if the battery is okay, and stops if the battery is low.
📋 What You'll Learn
Create a dictionary to represent the robot's status
Add a battery threshold variable
Write a function to decide the next action based on battery level
Complete the behavior tree logic to print the robot's action
💡 Why This Matters
🌍 Real World
Behavior trees are used in robotics to organize complex robot actions into simple, manageable steps. This helps robots react to changes in their environment safely and efficiently.
💼 Career
Understanding behavior trees and ROS programming is valuable for robotics engineers and developers working on autonomous robots, drones, or industrial automation.
Progress0 / 4 steps
1
Create the robot status dictionary
Create a dictionary called robot_status with these exact entries: 'battery_level': 75, 'is_moving': false, 'obstacle_detected': false.
ROS
Hint
Think of robot_status as a small notebook where the robot writes down its current battery and if it is moving or sees an obstacle.
2
Add a battery threshold variable
Create a variable called battery_threshold and set it to 20. This will be the minimum battery level to keep moving.
ROS
Hint
This threshold helps the robot decide when to stop moving to save power.
3
Write the behavior decision function
Define a function called decide_action that takes status and threshold as parameters. Inside, use an if statement to return 'stop' if status['battery_level'] < threshold, else return 'move_forward'.
ROS
Hint
This function is like the robot's brain deciding what to do based on battery level.
4
Complete the behavior tree logic
Call the decide_action function with robot_status and battery_threshold. Store the result in action. Then set robot_status['is_moving'] to true if action is 'move_forward', else false. Finally, add a comment # Behavior tree decision complete.
ROS
Hint
This step finishes the behavior tree by deciding and updating the robot's moving state.
Practice
(1/5)
1. What is the main purpose of a behavior tree in robotics?
easy
A. To create 3D models of the robot environment
B. To store large amounts of sensor data
C. To replace the robot's hardware components
D. To organize robot tasks step-by-step for clear control flow
Solution
Step 1: Understand behavior tree role
Behavior trees help organize robot actions in a clear, step-by-step way.
Step 2: Compare options
Only To organize robot tasks step-by-step for clear control flow describes organizing tasks; others describe unrelated functions.
Final Answer:
To organize robot tasks step-by-step for clear control flow -> Option D
Quick Check:
Behavior trees = organize tasks [OK]
Hint: Behavior trees control robot actions stepwise [OK]
Common Mistakes:
Confusing behavior trees with data storage
Thinking behavior trees modify hardware
Mixing behavior trees with 3D modeling
2. Which of the following is the correct way to define a Sequence node in a ROS behavior tree XML?
easy
A. ...
B. ...
C. ...
D. ...
Solution
Step 1: Identify node types in behavior tree XML
Sequence nodes are defined with the tag <Sequence> in ROS behavior trees.
Step 2: Match option tags
Only <Sequence name="root"> ... </Sequence> uses <Sequence> correctly; others use different node types.
Final Answer:
<Sequence name="root"> ... </Sequence> -> Option A
Quick Check:
Sequence node tag = <Sequence> [OK]
Hint: Sequence nodes use <Sequence> tags in XML [OK]
B. CheckBattery is after TryCharging, so charging never triggers on low battery
C. Action nodes must have child nodes
D. Sequence nodes always run children in reverse order
Solution
Step 1: Review node order in Sequence
Sequence runs children in order; TryCharging runs before CheckBattery.
Step 2: Understand logic error
Charging tries before checking battery status, so it never triggers when needed.
Final Answer:
CheckBattery is after TryCharging, so charging never triggers on low battery -> Option B
Quick Check:
Order matters: check battery first [OK]
Hint: Check battery before charging in Sequence [OK]
Common Mistakes:
Thinking Fallback can't be inside Sequence
Believing Action nodes need children
Assuming Sequence runs children backwards
5. You want a robot to try multiple ways to open a door: first try UseKey, then PushDoor, and finally CallForHelp. Which behavior tree structure best models this?
hard
A.
B.
C.
D.
Solution
Step 1: Understand Fallback node behavior
Fallback tries children in order until one succeeds, perfect for trying multiple options.
Step 2: Compare node types
Sequence requires all children to succeed; Decorator modifies one child; Parallel runs all simultaneously.
Final Answer:
Fallback node with UseKey, PushDoor, CallForHelp actions -> Option A
Quick Check:
Try options in order = Fallback [OK]
Hint: Use Fallback to try alternatives in order [OK]
Common Mistakes:
Using Sequence which requires all actions to succeed
Confusing Decorator with control flow node
Using Parallel which runs all actions at once
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.