-
Notifications
You must be signed in to change notification settings - Fork 0
Add fixes from 26 Nov #80
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
506beba
add go to ball example strat
energy-in-joles 399fc96
fixes from nov 26
energy-in-joles 44036c8
update settings
energy-in-joles 4331045
Update utama_core/strategy/examples/one_robot_placement_strategy.py
energy-in-joles 641ea7a
Update utama_core/strategy/examples/go_to_ball_ex.py
energy-in-joles 5b8904e
Update utama_core/strategy/examples/defense_strategy.py
energy-in-joles 10ec0e4
correctly handle non sequential robot IDs
energy-in-joles 06aa047
Merge branch 'fix_real' of https://github.com/First-Order-RoboCup-SSL…
energy-in-joles 6c2ce0a
clean init
energy-in-joles e25017a
use timestep instead of mysterious 0.0167
energy-in-joles dd8dc73
remove orientation if not in use
energy-in-joles 4237da9
patch non sequential handling in replays
energy-in-joles d0091a2
copy empty command instead of passing by reference
energy-in-joles 267270c
remove reference of squared distance
energy-in-joles f960a6a
add todo tag for get_robot_responses
energy-in-joles File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +0,0 @@ | ||
| from .examples.strategies.defense_strategy import DefenceStrategy | ||
| from .examples.strategies.one_robot_placement_strategy import RobotPlacementStrategy | ||
| from .examples.strategies.startup_strategy import StartupStrategy | ||
|
|
||
| __all__ = [ | ||
| "StartupStrategy", | ||
| "DefenceStrategy", | ||
| "RobotPlacementStrategy", | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| from utama_core.strategy.examples.defense_strategy import DefenceStrategy | ||
| from utama_core.strategy.examples.go_to_ball_ex import GoToBallExampleStrategy | ||
| from utama_core.strategy.examples.one_robot_placement_strategy import ( | ||
| RobotPlacementStrategy, | ||
| ) | ||
| from utama_core.strategy.examples.startup_strategy import StartupStrategy |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,14 +38,19 @@ class BlockAttackerStep(AbstractBehaviour): | |
|
|
||
| **Blackboard Interaction:** | ||
| Reads: | ||
| - `rd_defender_id` (int): The ID of the robot to check for ball possession. Typically from the `SetBlackboardVariable` node. | ||
| - `rd_defender_id` (int): The ID of the defending robot that will block the attacker. Typically from the `SetBlackboardVariable` node. | ||
|
||
| - `rd_blocking_target` (int): The ID of the enemy robot to block. Typically from the `FindBlockingTarget` node. | ||
|
|
||
| **Returns:** | ||
| - `py_trees.common.Status.RUNNING`: The behaviour is actively commanding the robot to block the attacker. | ||
| """ | ||
|
|
||
| def __init__(self, rd_defender_id: str, rd_locking_target: str, name: Optional[str] = "BlockAttackerStep"): | ||
| def __init__( | ||
| self, | ||
| rd_defender_id: str, | ||
| rd_locking_target: str, | ||
energy-in-joles marked this conversation as resolved.
Show resolved
Hide resolved
energy-in-joles marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| name: Optional[str] = "BlockAttackerStep", | ||
| ): | ||
| super().__init__(name=name) | ||
| self.defender_id_key = rd_defender_id | ||
| self.locking_target_key = rd_locking_target | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,189 @@ | ||
| from typing import Any, Optional | ||
|
|
||
| import py_trees | ||
| from py_trees.composites import Selector, Sequence | ||
|
|
||
| from utama_core.config.physical_constants import ROBOT_RADIUS | ||
| from utama_core.config.settings import TIMESTEP | ||
| from utama_core.entities.data.vector import Vector2D | ||
| from utama_core.entities.game import Game | ||
| from utama_core.skills.src.go_to_ball import go_to_ball | ||
| from utama_core.strategy.common import AbstractBehaviour, AbstractStrategy | ||
|
|
||
|
|
||
| class HasBall(AbstractBehaviour): | ||
| """ | ||
| Checks whether the configured robot currently owns the ball. | ||
| **Args:** | ||
| - visual (bool): If True, uses distance-based possession check; otherwise uses game state. | ||
| **Blackboard Interaction:** | ||
| - Reads: | ||
| - `robot_id` (int): The ID of the robot to check for ball possession. | ||
| **Returns:** | ||
| - `py_trees.common.Status.SUCCESS`: The robot has possession of the ball. | ||
| - `py_trees.common.Status.FAILURE`: The robot does not have possession of the ball. | ||
| """ | ||
|
|
||
| def __init__(self, rd_robot_id: str, visual: bool = True, name: Optional[str] = None): | ||
| super().__init__(name) | ||
| self.robot_id_key = rd_robot_id | ||
|
|
||
| self.visual = visual | ||
| DISTANCE_BUFFER = 0.04 # meters | ||
| self.ball_capture_dist = DISTANCE_BUFFER + ROBOT_RADIUS | ||
energy-in-joles marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| def setup_(self): | ||
| self.blackboard.register_key(key=self.robot_id_key, access=py_trees.common.Access.READ) | ||
|
|
||
| def update(self): | ||
| # print(f"Checking if robot {self.blackboard.get(self.robot_id_key)} has the ball") | ||
| game = self.blackboard.game | ||
| robot_id = self.blackboard.get(self.robot_id_key) | ||
|
|
||
| if self.visual: | ||
| return self._has_ball_visual(game, robot_id) | ||
| else: | ||
| return self._has_ball_from_state(game, robot_id) | ||
|
|
||
| def _has_ball_visual(self, game: Game, robot_id: int) -> py_trees.common.Status: | ||
| """ | ||
| Visual possession: success if the robot is within `ball_capture_radius` of the ball. | ||
energy-in-joles marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Success if the robot is within `ball_capture_radius` of the ball (uses Euclidean distance). | ||
| """ | ||
| robot = game.friendly_robots[robot_id] | ||
| ball = game.ball | ||
|
|
||
| r_pos = Vector2D(robot.p.x, robot.p.y) | ||
| b_pos = Vector2D(ball.p.x, ball.p.y) | ||
|
|
||
| dist = r_pos.distance_to(b_pos) | ||
| return py_trees.common.Status.SUCCESS if dist < self.ball_capture_dist else py_trees.common.Status.FAILURE | ||
|
|
||
| def _has_ball_from_state(self, game: Game, robot_id: int) -> py_trees.common.Status: | ||
| """ | ||
| State possession: success if the game state's `has_ball` flag is true. | ||
| """ | ||
| has_ball = game.current.friendly_robots[robot_id].has_ball | ||
|
|
||
| # print(f"Robot {robot_id} has_ball: {has_ball}") | ||
| return py_trees.common.Status.SUCCESS if has_ball else py_trees.common.Status.FAILURE | ||
|
|
||
|
|
||
| class GoToBallStep(AbstractBehaviour): | ||
| """ | ||
| Continuously moves the robot towards the ball using the core go_to_ball skill. | ||
| **Blackboard Interaction:** | ||
| - Reads: | ||
| - `robot_id` (int): The ID of the robot to move towards the ball. | ||
| **Returns:** | ||
| - `py_trees.common.Status.RUNNING`: The behaviour is actively commanding the robot to go to the ball. | ||
| """ | ||
|
|
||
| def __init__(self, rd_robot_id: str, name: str = "GoToBallStep"): | ||
| super().__init__(name) | ||
| self.robot_id_key = rd_robot_id | ||
|
|
||
| def setup_(self): | ||
| self.blackboard.register_key(key=self.robot_id_key, access=py_trees.common.Access.READ) | ||
|
|
||
| def update(self) -> py_trees.common.Status: | ||
| game = self.blackboard.game | ||
| env = self.blackboard.rsim_env | ||
| robot_id = self.blackboard.get(self.robot_id_key) | ||
| if env: | ||
| v = game.friendly_robots[robot_id].v | ||
| p = game.friendly_robots[robot_id].p | ||
| env.draw_point(p.x + v.x * TIMESTEP * 5, p.y + v.y * TIMESTEP * 5, color="green") | ||
|
|
||
| command = go_to_ball(game, self.blackboard.motion_controller, robot_id) | ||
| self.blackboard.cmd_map[robot_id] = command | ||
| return py_trees.common.Status.RUNNING | ||
|
|
||
|
|
||
| class SetBlackboardVariable(AbstractBehaviour): | ||
| """ | ||
| Writes a constant `value` onto the blackboard with the key `variable_name`. | ||
| **Blackboard Interaction:** | ||
| - Writes: | ||
| - `variable_name` (Any): The name of the blackboard variable to be set. | ||
| **Returns:** | ||
| - `py_trees.common.Status.SUCCESS`: The variable has been set. | ||
| """ | ||
|
|
||
| def __init__(self, name: str, variable_name: str, value: Any): | ||
| super().__init__(name=name) | ||
| self.variable_name = variable_name | ||
| self.value = value | ||
|
|
||
| def setup_(self): | ||
| self.blackboard.register_key(key=self.variable_name, access=py_trees.common.Access.WRITE) | ||
|
|
||
| def update(self) -> py_trees.common.Status: | ||
| self.blackboard.set(self.variable_name, self.value, overwrite=True) | ||
| return py_trees.common.Status.SUCCESS | ||
|
|
||
|
|
||
| def go_to_ball_subtree(rd_robot_id: str) -> py_trees.behaviour.Behaviour: | ||
| """ | ||
| Builds a selector that drives the robot to the ball until it gains possession. | ||
| **Args:** | ||
| - rd_robot_id: Blackboard key that holds the robot ID to control. **Required.** | ||
| **Status:** | ||
| - py_trees.common.Status.SUCCESS: The robot already has the ball. | ||
| - py_trees.common.Status.RUNNING: The robot is being commanded to go to the ball. | ||
| **Returns:** | ||
| - py_trees.behaviour.Behaviour: The root node of the Go-To-Ball subtree. | ||
| """ | ||
| root = Selector( | ||
| name="GoToBallSubtree", | ||
| memory=False, | ||
| ) | ||
|
|
||
| ### Assemble the tree ### | ||
|
|
||
| root.add_children( | ||
| [ | ||
| HasBall(name="HasBall?", rd_robot_id=rd_robot_id), | ||
| GoToBallStep(name="GoToBallStep", rd_robot_id=rd_robot_id), | ||
| ] | ||
| ) | ||
|
|
||
| return root | ||
|
|
||
|
|
||
| class GoToBallExampleStrategy(AbstractStrategy): | ||
| def __init__(self, robot_id: int): | ||
| """Initializes the GoToBallStrategy with a specific robot ID. | ||
| :param robot_id: The ID of the robot this strategy will control. | ||
| """ | ||
| self.robot_id = robot_id | ||
| self.robot_id_key = "robot_id" | ||
| super().__init__() | ||
|
|
||
| def assert_exp_robots(self, n_runtime_friendly: int, n_runtime_enemy: int): | ||
| if 1 <= n_runtime_friendly <= 3: | ||
| return True | ||
| return False | ||
|
|
||
| def assert_exp_goals(self, includes_my_goal_line: bool, includes_opp_goal_line: bool): | ||
| return True | ||
|
|
||
| def get_min_bounding_zone(self): | ||
| return None | ||
|
|
||
| def create_behaviour_tree(self) -> py_trees.behaviour.Behaviour: | ||
| """Factory function to create a complete go_to_ball behaviour tree.""" | ||
|
|
||
| root = Sequence(name="GoToBallRoot", memory=True) | ||
|
|
||
| set_robot_id = SetBlackboardVariable( | ||
| name="SetRobotID", | ||
| variable_name=self.robot_id_key, | ||
| value=self.robot_id, | ||
| ) | ||
|
|
||
| ### Assemble the tree ### | ||
|
|
||
| root.add_children([set_robot_id, go_to_ball_subtree(rd_robot_id=self.robot_id_key)]) | ||
|
|
||
| return root | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.