Skip to content

Commit cc92765

Browse files
committed
Correctly use trapezoidal profile
1 parent f28ed1b commit cc92765

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

components/wrist.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import math
2-
import time
32

43
import wpilib
54
from magicbot import feedback
@@ -65,9 +64,12 @@ def __init__(self, mech_root: wpilib.MechanismRoot2d):
6564

6665
self.motor_encoder = self.motor.getEncoder()
6766

68-
self.desired_angle = WristComponent.NEUTRAL_ANGLE
67+
self.desired_state = TrapezoidProfile.State(WristComponent.NEUTRAL_ANGLE, 0.0)
6968

70-
self.last_setpoint_update_time = time.monotonic()
69+
self.last_setpoint_update_time = wpilib.Timer.getFPGATimestamp()
70+
self.initial_state = TrapezoidProfile.State(
71+
self.inclination(), self.current_velocity()
72+
)
7173

7274
def on_enable(self):
7375
self.tilt_to(WristComponent.NEUTRAL_ANGLE)
@@ -117,15 +119,21 @@ def current_velocity(self) -> float:
117119

118120
@feedback
119121
def at_setpoint(self) -> bool:
120-
return abs(self.desired_angle - self.inclination()) < WristComponent.TOLERANCE
122+
return (
123+
abs(self.desired_state.position - self.inclination())
124+
< WristComponent.TOLERANCE
125+
)
121126

122127
def tilt_to(self, pos: float) -> None:
123128
clamped_angle = clamp(pos, self.MAXIMUM_DEPRESSION, self.MAXIMUM_ELEVATION)
124129

125130
# If the new setpoint is within the tolerance we wouldn't move anyway
126-
if abs(clamped_angle - self.desired_angle) > self.TOLERANCE:
127-
self.desired_angle = clamped_angle
128-
self.last_setpoint_update_time = time.monotonic()
131+
if abs(clamped_angle - self.desired_state.position) > self.TOLERANCE:
132+
self.desired_state = TrapezoidProfile.State(clamped_angle, 0.0)
133+
self.last_setpoint_update_time = wpilib.Timer.getFPGATimestamp()
134+
self.initial_state = TrapezoidProfile.State(
135+
self.inclination(), self.current_velocity()
136+
)
129137

130138
def go_to_neutral(self) -> None:
131139
self.tilt_to(WristComponent.NEUTRAL_ANGLE)
@@ -134,15 +142,15 @@ def reset_windup(self) -> None:
134142
self.tilt_to(self.inclination())
135143

136144
def execute(self) -> None:
137-
desired_state = self.wrist_profile.calculate(
138-
time.monotonic() - self.last_setpoint_update_time,
139-
TrapezoidProfile.State(self.inclination(), self.current_velocity()),
140-
TrapezoidProfile.State(self.desired_angle, 0.0),
145+
tracked_state = self.wrist_profile.calculate(
146+
wpilib.Timer.getFPGATimestamp() - self.last_setpoint_update_time,
147+
self.initial_state,
148+
self.desired_state,
141149
)
142-
ff = self.wrist_ff.calculate(desired_state.position, desired_state.velocity)
150+
ff = self.wrist_ff.calculate(tracked_state.position, tracked_state.velocity)
143151

144152
self.motor.setVoltage(
145-
self.pid.calculate(self.inclination(), desired_state.position) + ff
153+
self.pid.calculate(self.inclination(), tracked_state.position) + ff
146154
)
147155

148156
self.wrist_ligament.setAngle(self.inclination_deg())

0 commit comments

Comments
 (0)