Skip to content

Commit 75982f8

Browse files
authored
Merge pull request #293 from thedropbears/make_shooter_check_reasonable
ease requirement for stationary chassis and fix sim
2 parents 21562c4 + 97eaf1e commit 75982f8

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

components/chassis.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,15 @@ def stop_snapping(self) -> None:
390390
"""stops the heading_controller"""
391391
self.snapping_to_heading = False
392392

393+
@feedback
394+
def is_stationary(self) -> bool:
395+
velocity = self.get_velocity()
396+
return (
397+
math.isclose(velocity.vx, 0.0, abs_tol=0.1)
398+
and math.isclose(velocity.vy, 0.0, abs_tol=0.1)
399+
and math.isclose(velocity.omega, 0.0, abs_tol=math.radians(3))
400+
)
401+
393402
def execute(self) -> None:
394403
# rotate desired velocity to compensate for skew caused by discretization
395404
# see https://www.chiefdelphi.com/t/field-relative-swervedrive-drift-even-with-simulated-perfect-modules/413892/

controllers/algae_shooter.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ def preparing(self):
6161
self.wrist.at_setpoint()
6262
and self.shooter_component.top_flywheels_up_to_speed()
6363
and self.shooter_component.bottom_flywheels_up_to_speed()
64-
and math.isclose(self.chassis.get_velocity().vx, 0)
65-
and math.isclose(self.chassis.get_velocity().vy, 0)
64+
and self.chassis.is_stationary()
6665
):
6766
self.next_state(self.shooting)
6867

physics.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,22 @@
3131
from robot import MyRobot
3232

3333

34+
class RollingBuffer:
35+
def __init__(self, max_length: int):
36+
self.max_length = max_length
37+
38+
self.buffer_: list[float] = []
39+
40+
def average(self) -> float:
41+
return sum(self.buffer_) / len(self.buffer_) if len(self.buffer_) > 0 else 0.0
42+
43+
def add_sample(self, sample: float):
44+
self.buffer_.append(sample)
45+
46+
if len(self.buffer_) > self.max_length:
47+
self.buffer_.pop(0)
48+
49+
3450
class SimpleTalonFXMotorSim:
3551
def __init__(
3652
self, motor: phoenix6.hardware.TalonFX, units_per_rev: float, kV: float
@@ -39,9 +55,16 @@ def __init__(
3955
self.sim_state.set_supply_voltage(12.0)
4056
self.kV = kV # volt seconds per unit
4157
self.units_per_rev = units_per_rev
58+
self.voltage_buffer = RollingBuffer(10)
4259

4360
def update(self, dt: float) -> None:
4461
voltage = self.sim_state.motor_voltage
62+
63+
self.voltage_buffer.add_sample(voltage)
64+
65+
if math.isclose(self.voltage_buffer.average(), 0.0, abs_tol=0.1):
66+
voltage = 0.0
67+
4568
velocity = voltage / self.kV # units per second
4669
velocity_rps = velocity * self.units_per_rev
4770
self.sim_state.set_rotor_velocity(velocity_rps)

0 commit comments

Comments
 (0)