-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add nav_types.py and extract GlobalRoute class
- Loading branch information
Showing
7 changed files
with
120 additions
and
112 deletions.
There are no files selected for viewing
This file contains 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,99 @@ | ||
|
||
from typing import List, Tuple | ||
from math import dist | ||
|
||
|
||
from dataclasses import dataclass | ||
|
||
from robot_sf.nav.nav_types import Vec2D, Rect | ||
|
||
@dataclass | ||
class GlobalRoute: | ||
""" | ||
A class to represent a global route. | ||
Attributes | ||
---------- | ||
spawn_id : int | ||
The id of the spawn point. | ||
goal_id : int | ||
The id of the goal point. | ||
waypoints : List[Vec2D] | ||
The waypoints of the route. | ||
spawn_zone : Rect | ||
The spawn zone of the route. | ||
goal_zone : Rect | ||
The goal zone of the route. | ||
Methods | ||
------- | ||
__post_init__(): | ||
Validates the spawn_id, goal_id, and waypoints. | ||
sections(): | ||
Returns the sections of the route. | ||
section_lengths(): | ||
Returns the lengths of the sections. | ||
section_offsets(): | ||
Returns the offsets of the sections. | ||
total_length(): | ||
Returns the total length of the route. | ||
""" | ||
|
||
spawn_id: int | ||
goal_id: int | ||
waypoints: List[Vec2D] | ||
spawn_zone: Rect | ||
goal_zone: Rect | ||
|
||
def __post_init__(self): | ||
""" | ||
Validates the spawn_id, goal_id, and waypoints. | ||
Raises a ValueError if spawn_id or goal_id is less than 0 or if waypoints is empty. | ||
""" | ||
|
||
if self.spawn_id < 0: | ||
raise ValueError('Spawn id needs to be an integer >= 0!') | ||
if self.goal_id < 0: | ||
raise ValueError('Goal id needs to be an integer >= 0!') | ||
if len(self.waypoints) < 1: | ||
raise ValueError(f'Route {self.spawn_id} -> {self.goal_id} contains no waypoints!') | ||
|
||
@property | ||
def sections(self) -> List[Tuple[Vec2D, Vec2D]]: | ||
""" | ||
Returns the sections of the route as a list of tuples, where each tuple | ||
contains two Vec2D objects | ||
representing the start and end points of the section. | ||
""" | ||
|
||
return [] if len(self.waypoints) < 2 else list(zip(self.waypoints[:-1], self.waypoints[1:])) | ||
|
||
@property | ||
def section_lengths(self) -> List[float]: | ||
""" | ||
Returns the lengths of the sections as a list of floats. | ||
""" | ||
|
||
return [dist(p1, p2) for p1, p2 in self.sections] | ||
|
||
@property | ||
def section_offsets(self) -> List[float]: | ||
""" | ||
Returns the offsets of the sections as a list of floats. | ||
""" | ||
|
||
lengths = self.section_lengths | ||
offsets = [] | ||
temp_offset = 0.0 | ||
for section_length in lengths: | ||
offsets.append(temp_offset) | ||
temp_offset += section_length | ||
return offsets | ||
|
||
@property | ||
def total_length(self) -> float: | ||
""" | ||
Returns the total length of the route as a float. | ||
""" | ||
|
||
return 0 if len(self.waypoints) < 2 else sum(self.section_lengths) |
This file contains 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 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,8 @@ | ||
from typing import Tuple | ||
|
||
Vec2D = Tuple[float, float] | ||
Line2D = Tuple[float, float, float, float] | ||
Rect = Tuple[Vec2D, Vec2D, Vec2D] | ||
# TODO: Is there a difference between a Rect and a Zone? | ||
# rect ABC with sides |A B|, |B C| and diagonal |A C| | ||
Zone = Tuple[Vec2D, Vec2D, Vec2D] |
This file contains 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 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 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 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