-
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.
Refactored pid code to use Trajectory class
- Loading branch information
1 parent
da88b1c
commit 4ddfb85
Showing
6 changed files
with
101 additions
and
53 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
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 @@ | ||
from .trajectory_factory import TrajectoryFactory |
Empty file.
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,58 @@ | ||
from .waypoint import Waypoint | ||
import numpy as np | ||
import math | ||
|
||
class Trajectory: | ||
|
||
def __init__(self) -> None: | ||
pass | ||
|
||
def get_waypoint(self, time: float) -> Waypoint: | ||
""" | ||
Needs to be implemented by subclass. time \in [0,1] | ||
""" | ||
raise NotImplementedError() | ||
|
||
class SquareLinearTrajectory(Trajectory): | ||
""" | ||
Linear interpolation for four corner points of a 2D square. | ||
""" | ||
|
||
square_scale: float | ||
time_scale: float | ||
corner_points: np.ndarray | ||
|
||
def __init__(self, square_scale: float=1, time_scale: float=1) -> None: | ||
super().__init__() | ||
self.square_scale = square_scale | ||
self.time_scale = time_scale | ||
self.corner_points = np.array([ | ||
[0 ,0 ,self.square_scale], | ||
[self.square_scale ,0 ,self.square_scale], | ||
[self.square_scale ,self.square_scale ,self.square_scale], | ||
[0 ,self.square_scale ,self.square_scale], | ||
], np.float32) | ||
|
||
def get_waypoint(self, time: float): | ||
assert time >= 0 and time <= 1 | ||
time = time * 4 | ||
|
||
if int(time) == time: | ||
# exactly at corner point | ||
target_pos = self.corner_points[int(time)] | ||
else: | ||
# in-between two points, linear interpolation | ||
cur_corner = math.floor(time) | ||
upcomimg_corner = math.ceil(time) | ||
diff = upcomimg_corner - cur_corner | ||
target_pos = cur_corner + (time - int(time)) * (diff) | ||
|
||
target_wp = Waypoint( | ||
coordinate=target_pos, | ||
timestamp=time * self.time_scale | ||
) | ||
|
||
return target_wp | ||
|
||
|
||
|
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 |
---|---|---|
@@ -1,33 +1,14 @@ | ||
from time import thread_time | ||
import numpy as np | ||
|
||
class Waypoint: | ||
""" | ||
3D coordinate with a timestamp. | ||
""" | ||
|
||
coordinate: np.ndarray | ||
"""R^3 coordinate""" | ||
timestamp: float | ||
"""Time as float""" | ||
|
||
def __init__(self, coordinate: np.ndarray, timestamp: float) -> None: | ||
self.coordinate = coordinate | ||
self.timestamp = timestamp | ||
|
||
from .trajectories import Trajectory, SquareLinearTrajectory | ||
|
||
class TrajectoryFactory: | ||
""" | ||
Generates waypoints | ||
Wrapper class for instantiating target trajectories. | ||
""" | ||
|
||
def __init__(self) -> None: | ||
pass | ||
|
||
def get_waypoint(self) -> Waypoint: | ||
waypoint = Waypoint( | ||
coordinate=np.asarray([0,0,0]), | ||
timestamp=0 | ||
@classmethod | ||
def get_linear_square_trajectory(cls, square_scale: float, time_scale: float) -> Trajectory: | ||
return SquareLinearTrajectory( | ||
square_scale, time_scale | ||
) | ||
return waypoint | ||
|
||
|
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,16 @@ | ||
import numpy as np | ||
from enum import Enum, auto | ||
|
||
class Waypoint: | ||
""" | ||
3D coordinate with a timestamp. | ||
""" | ||
|
||
coordinate: np.ndarray | ||
"""R^3 coordinate""" | ||
timestamp: float | ||
"""Time as float""" | ||
|
||
def __init__(self, coordinate: np.ndarray, timestamp: float) -> None: | ||
self.coordinate = coordinate | ||
self.timestamp = timestamp |