1
1
# validated: 2024-01-20 DS 192a28af4731 TrapezoidProfileSubsystem.java
2
+ #
2
3
# Copyright (c) FIRST and other WPILib contributors.
3
4
# Open Source Software; you can modify and/or share it under the terms of
4
5
# the WPILib BSD license file in the root directory of this project.
6
+ #
5
7
from __future__ import annotations
6
8
7
- from typing import Union
9
+ from typing import Any , Union
8
10
9
11
from wpimath import units
10
- from wpimath .trajectory import TrapezoidProfile
12
+ from wpimath .trajectory import TrapezoidProfile , TrapezoidProfileRadians
11
13
12
14
13
15
from .subsystem import Subsystem
@@ -19,9 +21,14 @@ class TrapezoidProfileSubsystem(Subsystem):
19
21
how to use the current state of the motion profile by overriding the `useState` method.
20
22
"""
21
23
24
+ _profile : Any
25
+ _stateCls : Any
26
+
22
27
def __init__ (
23
28
self ,
24
- constraints : TrapezoidProfile .Constraints ,
29
+ constraints : Union [
30
+ TrapezoidProfile .Constraints , TrapezoidProfileRadians .Constraints
31
+ ],
25
32
initial_position : float = 0.0 ,
26
33
period : units .seconds = 0.02 ,
27
34
):
@@ -32,8 +39,16 @@ def __init__(
32
39
:param initial_position: The initial position of the controlled mechanism when the subsystem is constructed.
33
40
:param period: The period of the main robot loop, in seconds.
34
41
"""
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 )
37
52
self .setGoal (initial_position )
38
53
self ._period = period
39
54
self ._enabled = True
@@ -44,11 +59,11 @@ def periodic(self):
44
59
45
60
This method is called synchronously from the subsystem's periodic() method.
46
61
"""
47
- self ._state = self ._profile .calculate (self ._period , self ._goal , self ._state )
62
+ self ._state = self ._profile .calculate (self ._period , self ._state , self ._goal )
48
63
if self ._enabled :
49
64
self .useState (self ._state )
50
65
51
- def setGoal (self , goal : Union [ TrapezoidProfile . State , float ] ):
66
+ def setGoal (self , goal ):
52
67
"""
53
68
Sets the goal state for the subsystem. Goal velocity assumed to be zero.
54
69
@@ -58,7 +73,7 @@ def setGoal(self, goal: Union[TrapezoidProfile.State, float]):
58
73
"""
59
74
# If we got a float, instantiate the state
60
75
if isinstance (goal , (float , int )):
61
- goal = TrapezoidProfile . State (goal , 0 )
76
+ goal = self . _stateCls (goal , 0 )
62
77
63
78
self ._goal = goal
64
79
@@ -70,7 +85,7 @@ def disable(self):
70
85
"""Disable the TrapezoidProfileSubsystem's output."""
71
86
self ._enabled = False
72
87
73
- def useState (self , state : TrapezoidProfile . State ):
88
+ def useState (self , state ):
74
89
"""
75
90
Users should override this to consume the current state of the motion profile.
76
91
0 commit comments