|
2 | 2 | # Open Source Software; you can modify and/or share it under the terms of
|
3 | 3 | # the WPILib BSD license file in the root directory of this project.
|
4 | 4 | from __future__ import annotations
|
5 |
| - |
| 5 | +from typing import Union |
6 | 6 | from .subsystem import Subsystem
|
7 | 7 | from wpimath.trajectory import TrapezoidProfile
|
8 | 8 |
|
| 9 | + |
9 | 10 | class TrapezoidProfileSubsystem(Subsystem):
|
10 | 11 | """A subsystem that generates and runs trapezoidal motion profiles automatically. The user specifies
|
11 | 12 | how to use the current state of the motion profile by overriding the `useState` method.
|
12 | 13 |
|
13 | 14 | This class is provided by the NewCommands VendorDep
|
14 | 15 | """
|
| 16 | + |
15 | 17 | def __init__(
|
16 |
| - self, constraints: TrapezoidProfile.Constraints, |
17 |
| - initial_position: float, |
18 |
| - period: float = 0.02 |
19 |
| - ): |
| 18 | + self, |
| 19 | + constraints: TrapezoidProfile.Constraints, |
| 20 | + initial_position: float, |
| 21 | + period: float = 0.02, |
| 22 | + ): |
20 | 23 | """
|
21 | 24 | Creates a new TrapezoidProfileSubsystem.
|
22 | 25 |
|
23 | 26 | :param constraints: The constraints (maximum velocity and acceleration) for the profiles.
|
24 | 27 | :param initial_position: The initial position of the controlled mechanism when the subsystem is constructed.
|
25 | 28 | :param period: The period of the main robot loop, in seconds.
|
26 | 29 | """
|
27 |
| - self.profile = TrapezoidProfile(constraints) |
28 |
| - self.state = TrapezoidProfile.State(initial_position, 0) |
| 30 | + self._profile = TrapezoidProfile(constraints) |
| 31 | + self._state = TrapezoidProfile.State(initial_position, 0) |
29 | 32 | self.setGoal(initial_position)
|
30 |
| - self.period = period |
31 |
| - self.enabled = True |
| 33 | + self._period = period |
| 34 | + self._enabled = True |
32 | 35 |
|
33 | 36 | def periodic(self):
|
34 | 37 | """
|
35 | 38 | Executes the TrapezoidProfileSubsystem logic during each periodic update.
|
36 | 39 |
|
37 | 40 | This method is called synchronously from the subsystem's periodic() method.
|
38 | 41 | """
|
39 |
| - self.state = self.profile.calculate(self.period, self.goal, self.state) |
40 |
| - if self.enabled: |
41 |
| - self.useState(self.state) |
| 42 | + self._state = self._profile.calculate(self._period, self.goal, self._state) |
| 43 | + if self._enabled: |
| 44 | + self.useState(self._state) |
42 | 45 |
|
43 |
| - def setGoal(self, goal: TrapezoidProfile.State): |
| 46 | + def __setGoal(self, goal: TrapezoidProfile.State): |
44 | 47 | """
|
45 | 48 | Sets the goal state for the subsystem.
|
46 | 49 |
|
47 | 50 | :param goal: The goal state for the subsystem's motion profile.
|
48 | 51 | """
|
49 | 52 | self.goal = goal
|
50 | 53 |
|
51 |
| - def setGoal(self, goal: float): |
| 54 | + def setGoal(self, goal: Union[TrapezoidProfile.State, float]): |
52 | 55 | """
|
53 | 56 | Sets the goal state for the subsystem. Goal velocity assumed to be zero.
|
54 | 57 |
|
55 |
| - :param goal: The goal position for the subsystem's motion profile. |
| 58 | + :param goal: The goal position for the subsystem's motion profile. The goal |
| 59 | + can either be a `TrapezoidProfile.State` or `float`. If float is provided, |
| 60 | + the assumed velocity for the goal will be 0. |
56 | 61 | """
|
57 |
| - self.setGoal(TrapezoidProfile.State(goal, 0)) |
| 62 | + newGoal: TrapezoidProfile.State = goal |
| 63 | + # If we got a float, instantiate the state |
| 64 | + if isinstance(goal, float): |
| 65 | + newGoal = TrapezoidProfile.State(goal, 0) |
| 66 | + |
| 67 | + self.__setGoal(newGoal) |
58 | 68 |
|
59 | 69 | def enable(self):
|
60 | 70 | """Enable the TrapezoidProfileSubsystem's output."""
|
61 |
| - self.enabled = True |
| 71 | + self._enabled = True |
62 | 72 |
|
63 | 73 | def disable(self):
|
64 | 74 | """Disable the TrapezoidProfileSubsystem's output."""
|
65 |
| - self.enabled = False |
| 75 | + self._enabled = False |
66 | 76 |
|
67 | 77 | def useState(self, state: TrapezoidProfile.State):
|
68 | 78 | """
|
|
0 commit comments