๐ง Behavior Decision System (dbehavior) โ
The behavior decision system is the "brain" of the robot soccer application, responsible for making intelligent decisions based on the game situation and controlling robot behavior and strategy.
๐ System Overview โ
The dbehavior module is based on behavior tree architecture, providing a hierarchical decision framework:
- Behavior Tree Engine: Flexible task scheduling and execution
- Multi-role Support: Striker, Defender, Goalkeeper, PenaltyKicker
- Skill System: Reusable action primitives
- Team Coordination: Multi-robot role assignment and coordination
- Game State Management: Responding to referee commands
๐ฏ Core Concepts โ
1. Behavior Tree โ
A behavior tree is a hierarchical decision structure composed of nodes:
Node Types:
- Sequence: Execute child nodes in order; succeeds only if all succeed
- Selector: Try child nodes in order; succeeds if any one succeeds
- Parallel: Execute multiple child nodes simultaneously
- Decorator: Modify child node behavior (repeat, invert, etc.)
- Action: Execute a specific action
- Condition: Check a condition
Example Behavior Tree:
Striker
โโ Selector
โโ Sequence [Shoot]
โ โโ Condition: Ball nearby?
โ โโ Condition: Aligned with goal?
โ โโ Action: Kick ball
โโ Sequence [Chase ball]
โ โโ Condition: Ball visible?
โ โโ Action: Walk to ball
โโ Action: Search for ball2. Role System โ
Striker โ
Responsibility: Active attacking, chasing and shooting
Behavior Priority:
- Ball at feet and aligned with goal โ Shoot
- Ball visible โ Chase ball
- Teammate has possession โ Position for pass
- Otherwise โ Search for ball
Configuration Parameters:
striker:
kick_distance: 0.15 # Kick distance threshold
approach_speed: 0.3 # Speed when approaching ball
shoot_angle: 0.3 # Shooting angle toleranceDefender โ
Responsibility: Defensive positioning, intercepting opponents
Behavior Priority:
- Opponent approaching with ball โ Intercept
- Ball in defensive zone โ Clear ball
- Otherwise โ Defensive positioning
Positioning Strategy:
- Stay between ball and own goal
- 1-2 meters from goal
- Adjust position based on ball location
Goalkeeper โ
Responsibility: Guard the goal, save shots
Behavior Priority:
- Ball approaching fast โ Save
- Ball in penalty area โ Rush out
- Otherwise โ Goalkeeper positioning
Save Strategy:
def dive_direction(ball_position, ball_velocity):
# Predict ball trajectory
predicted_pos = ball_position + ball_velocity * 0.5
# Determine dive direction
if predicted_pos.x < -0.2:
return "left"
elif predicted_pos.x > 0.2:
return "right"
else:
return "center"PenaltyKicker โ
Responsibility: Penalty shootout specific strategy
Execution Flow:
- Align with goal
- Choose shot angle (left/right/center)
- Approach run
- Shoot
3. Skill System โ
Skills are reusable action primitives that encapsulate specific execution logic.
Common Skills:
WalkToBall
class WalkToBall(Skill):
def execute(self, blackboard):
ball = blackboard.get('ball_position')
robot = blackboard.get('robot_position')
# Calculate target position (behind ball)
target = ball - normalize(ball - robot) * 0.2
# Send walking command
self.motion.walk_to(target)
# Check if arrived
if distance(robot, target) < 0.05:
return Status.SUCCESS
return Status.RUNNINGKickBall
class KickBall(Skill):
def execute(self, blackboard):
goal = blackboard.get('opponent_goal')
ball = blackboard.get('ball_position')
# Calculate kick direction
kick_direction = normalize(goal - ball)
# Choose kick type
if abs(kick_direction.angle) < 0.2:
kick_type = "forward" # Instep kick
else:
kick_type = "side" # Inside kick
# Execute kick
self.motion.kick(kick_type, power=0.8)
return Status.SUCCESSSearchBall
class SearchBall(Skill):
def execute(self, blackboard):
# Rotate in place to search
self.motion.turn(speed=0.3)
# Check if ball found
if blackboard.get('ball_detected'):
return Status.SUCCESS
return Status.RUNNING4. Blackboard System โ
The blackboard is a mechanism for sharing data between behavior tree nodes.
Stored Data:
blackboard = {
# Vision information
'ball_position': [x, y],
'ball_detected': True,
'opponent_goal': [x, y],
'own_goal': [x, y],
'obstacles': [...],
# Robot state
'robot_position': [x, y, theta],
'robot_fallen': False,
'battery_level': 0.85,
# Game information
'game_state': 'playing',
'team_color': 'blue',
'role': 'striker',
# Team information
'teammates': [...],
'ball_owner': 'robot_2',
}๐ฎ Game State Management โ
Game Phases โ
Initial
- Robot stands, waits for ready signal
- Movement not allowed
Ready
- Robot walks to initial position
- Preparing for kickoff
Set
- Stay still
- Waiting for kickoff whistle
Playing
- Execute role behavior
- Autonomous decision making and action
Finished
- Stop all actions
- Wait for next round
Referee Command Response โ
def handle_game_controller(gc_info):
if gc_info.state == 'INITIAL':
robot.stand()
elif gc_info.state == 'READY':
robot.walk_to_initial_position()
elif gc_info.state == 'SET':
robot.stop()
elif gc_info.state == 'PLAYING':
robot.execute_role_behavior()
elif gc_info.state == 'FINISHED':
robot.celebrate() if gc_info.team_won else robot.stand()๐ค Team Coordination โ
Role Assignment โ
Dynamic Role Assignment Algorithm:
def assign_roles(team_robots, ball_position):
# Calculate each robot's distance to ball
distances = [distance(r.position, ball_position) for r in team_robots]
# Closest robot becomes striker
striker_idx = distances.index(min(distances))
team_robots[striker_idx].role = 'Striker'
# Assign roles to other robots based on position
for i, robot in enumerate(team_robots):
if i == striker_idx:
continue
if robot.position.y < 0: # Defensive half
robot.role = 'Defender'
else: # Attacking half
robot.role = 'Supporter'Information Sharing โ
Robots share information over the network:
- Ball position
- Own position
- Current role
- Target position
๐ง Configuration and Tuning โ
Behavior Parameter Configuration โ
# dbehavior/config/behavior.yaml
behavior:
# Decision frequency
update_rate: 30 # Hz
# Role parameters
striker:
aggressive: 0.8
kick_distance: 0.15
approach_speed: 0.3
defender:
defensive_distance: 2.0
intercept_threshold: 1.0
goalkeeper:
dive_threshold: 0.5
position_offset: 0.3
# Skill parameters
skills:
walk_speed: 0.25
turn_speed: 0.5
kick_power: 0.8Debugging Tools โ
Behavior Tree Visualization:
# Start behavior tree visualization tool
rosrun dbehavior behavior_tree_viewerLog Output:
# Add logging in code
self.logger.info(f"Executing skill: {skill_name}")
self.logger.debug(f"Blackboard: {blackboard}")๐ Performance Metrics โ
| Metric | Value |
|---|---|
| Decision Frequency | 30 Hz |
| Response Latency | <100ms |
| CPU Usage | ~20% |
| Role Switch Time | <1s |
๐ Related Documentation โ
- Vision System - Provides perception information for decision making
- Motion Control - Executes decision commands
- Network Communication - Team coordination communication
๐ก FAQ โ
Q: How to add a new role? A: Create a new behavior tree file, implement role logic, and register in configuration.
Q: How to adjust decision priorities? A: Modify behavior tree structure, adjust the order of child nodes in Selector nodes.
Q: Robot reacts too slowly, what should I do? A: Increase update_rate, optimize skill execution efficiency, reduce unnecessary computation.
Q: How to implement a new skill? A: Inherit from the Skill base class, implement the execute method, and register in the skill library.
