Skip to content

Commit 32cb5f0

Browse files
committed
Update TrapezoidProfileSubsystem
1 parent 770d22f commit 32cb5f0

File tree

1 file changed

+24
-9
lines changed

1 file changed

+24
-9
lines changed

commands2/trapezoidprofilesubsystem.py

+24-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
# validated: 2024-01-20 DS 192a28af4731 TrapezoidProfileSubsystem.java
2+
#
23
# Copyright (c) FIRST and other WPILib contributors.
34
# Open Source Software; you can modify and/or share it under the terms of
45
# the WPILib BSD license file in the root directory of this project.
6+
#
57
from __future__ import annotations
68

7-
from typing import Union
9+
from typing import Any, Union
810

911
from wpimath import units
10-
from wpimath.trajectory import TrapezoidProfile
12+
from wpimath.trajectory import TrapezoidProfile, TrapezoidProfileRadians
1113

1214

1315
from .subsystem import Subsystem
@@ -19,9 +21,14 @@ class TrapezoidProfileSubsystem(Subsystem):
1921
how to use the current state of the motion profile by overriding the `useState` method.
2022
"""
2123

24+
_profile: Any
25+
_stateCls: Any
26+
2227
def __init__(
2328
self,
24-
constraints: TrapezoidProfile.Constraints,
29+
constraints: Union[
30+
TrapezoidProfile.Constraints, TrapezoidProfileRadians.Constraints
31+
],
2532
initial_position: float = 0.0,
2633
period: units.seconds = 0.02,
2734
):
@@ -32,8 +39,16 @@ def __init__(
3239
:param initial_position: The initial position of the controlled mechanism when the subsystem is constructed.
3340
:param period: The period of the main robot loop, in seconds.
3441
"""
35-
self._profile = TrapezoidProfile(constraints)
36-
self._state = TrapezoidProfile.State(initial_position, 0)
42+
if isinstance(constraints, TrapezoidProfile.Constraints):
43+
self._profile = TrapezoidProfile(constraints)
44+
self._stateCls = TrapezoidProfile.State
45+
elif isinstance(constraints, TrapezoidProfileRadians.Constraints):
46+
self._profile = TrapezoidProfileRadians(constraints)
47+
self._stateCls = TrapezoidProfileRadians.State
48+
else:
49+
raise ValueError(f"Invalid constraints {constraints}")
50+
51+
self._state = self._stateCls(initial_position, 0)
3752
self.setGoal(initial_position)
3853
self._period = period
3954
self._enabled = True
@@ -44,11 +59,11 @@ def periodic(self):
4459
4560
This method is called synchronously from the subsystem's periodic() method.
4661
"""
47-
self._state = self._profile.calculate(self._period, self._goal, self._state)
62+
self._state = self._profile.calculate(self._period, self._state, self._goal)
4863
if self._enabled:
4964
self.useState(self._state)
5065

51-
def setGoal(self, goal: Union[TrapezoidProfile.State, float]):
66+
def setGoal(self, goal):
5267
"""
5368
Sets the goal state for the subsystem. Goal velocity assumed to be zero.
5469
@@ -58,7 +73,7 @@ def setGoal(self, goal: Union[TrapezoidProfile.State, float]):
5873
"""
5974
# If we got a float, instantiate the state
6075
if isinstance(goal, (float, int)):
61-
goal = TrapezoidProfile.State(goal, 0)
76+
goal = self._stateCls(goal, 0)
6277

6378
self._goal = goal
6479

@@ -70,7 +85,7 @@ def disable(self):
7085
"""Disable the TrapezoidProfileSubsystem's output."""
7186
self._enabled = False
7287

73-
def useState(self, state: TrapezoidProfile.State):
88+
def useState(self, state):
7489
"""
7590
Users should override this to consume the current state of the motion profile.
7691

0 commit comments

Comments
 (0)