Skip to content

Commit a00304f

Browse files
authored
Merge pull request #299 from thedropbears/trap-profile
Always work on your traps, fellas!
2 parents f28ed1b + 00688f2 commit a00304f

File tree

2 files changed

+51
-30
lines changed

2 files changed

+51
-30
lines changed

components/intake.py

Lines changed: 30 additions & 17 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, tunable
@@ -67,23 +66,37 @@ def __init__(self, intake_mech_root: wpilib.MechanismRoot2d) -> None:
6766

6867
self.motor_encoder = self.arm_motor.getEncoder()
6968

70-
self.desired_angle = IntakeComponent.RETRACTED_ANGLE
71-
72-
self.last_setpoint_update_time = time.monotonic()
69+
self.desired_state = TrapezoidProfile.State(
70+
IntakeComponent.RETRACTED_ANGLE, 0.0
71+
)
72+
self.last_setpoint_update_time = wpilib.Timer.getFPGATimestamp()
73+
self.initial_state = TrapezoidProfile.State(self.position(), self.velocity())
7374

7475
def intake(self):
7576
if not math.isclose(
76-
self.desired_angle, IntakeComponent.DEPLOYED_ANGLE, abs_tol=0.1
77+
self.desired_state.position, IntakeComponent.DEPLOYED_ANGLE, abs_tol=0.1
7778
):
78-
self.desired_angle = IntakeComponent.DEPLOYED_ANGLE
79-
self.last_setpoint_update_time = time.monotonic()
79+
self.desired_state = TrapezoidProfile.State(
80+
IntakeComponent.DEPLOYED_ANGLE, 0.0
81+
)
82+
self.last_setpoint_update_time = wpilib.Timer.getFPGATimestamp()
83+
self.initial_state = TrapezoidProfile.State(
84+
self.position(), self.velocity()
85+
)
8086

8187
self.desired_output = self.intake_output
8288

8389
def retract(self):
84-
if not math.isclose(self.desired_angle, self.RETRACTED_ANGLE, abs_tol=0.1):
85-
self.desired_angle = IntakeComponent.RETRACTED_ANGLE
86-
self.last_setpoint_update_time = time.monotonic()
90+
if not math.isclose(
91+
self.desired_state.position, self.RETRACTED_ANGLE, abs_tol=0.1
92+
):
93+
self.desired_state = TrapezoidProfile.State(
94+
IntakeComponent.RETRACTED_ANGLE, 0.0
95+
)
96+
self.initial_state = TrapezoidProfile.State(
97+
self.position(), self.velocity()
98+
)
99+
self.last_setpoint_update_time = wpilib.Timer.getFPGATimestamp()
87100

88101
@feedback
89102
def raw_encoder(self) -> float:
@@ -104,18 +117,18 @@ def execute(self) -> None:
104117

105118
self.desired_output = 0.0
106119

107-
desired_state = self.motion_profile.calculate(
108-
time.monotonic() - self.last_setpoint_update_time,
109-
TrapezoidProfile.State(self.position(), self.velocity()),
110-
TrapezoidProfile.State(self.desired_angle, 0.0),
120+
tracked_state = self.motion_profile.calculate(
121+
wpilib.Timer.getFPGATimestamp() - self.last_setpoint_update_time,
122+
self.initial_state,
123+
self.desired_state,
111124
)
112-
ff = self.arm_ff.calculate(desired_state.position, desired_state.velocity)
125+
ff = self.arm_ff.calculate(tracked_state.position, tracked_state.velocity)
113126

114127
if not math.isclose(
115-
self.desired_angle, self.position(), abs_tol=math.radians(5)
128+
self.desired_state.position, self.position(), abs_tol=math.radians(5)
116129
):
117130
self.arm_motor.setVoltage(
118-
self.pid.calculate(self.position(), desired_state.position) + ff
131+
self.pid.calculate(self.position(), tracked_state.position) + ff
119132
)
120133
else:
121134
self.arm_motor.setVoltage(0.0)

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)