11import math
2- import time
32
43import wpilib
54from 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 )
0 commit comments