Skip to content

Commit 84db738

Browse files
committed
physics: Refactor wrist sim to common SparkArmSim
1 parent 76b9d52 commit 84db738

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

physics.py

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,18 @@ def update(self, dt: float) -> None:
7878
)
7979

8080

81+
class SparkArmSim:
82+
def __init__(self, mech_sim: SingleJointedArmSim, motor_sim: rev.SparkSim) -> None:
83+
self.mech_sim = mech_sim
84+
self.motor_sim = motor_sim
85+
86+
def update(self, dt: float) -> None:
87+
vbus = self.motor_sim.getBusVoltage()
88+
self.mech_sim.setInputVoltage(self.motor_sim.getAppliedOutput() * vbus)
89+
self.mech_sim.update(dt)
90+
self.motor_sim.iterate(self.mech_sim.getVelocity(), vbus, dt)
91+
92+
8193
# class ServoEncoderSim:
8294
# def __init__(self, pwm, encoder):
8395
# self.pwm_sim = PWMSim(pwm)
@@ -128,10 +140,10 @@ def __init__(self, physics_controller: PhysicsInterface, robot: MyRobot):
128140
]
129141

130142
wrist_gearbox = DCMotor.NEO(1)
131-
self.wrist_motor = rev.SparkMaxSim(robot.wrist.motor, wrist_gearbox)
143+
wrist_motor = rev.SparkMaxSim(robot.wrist.motor, wrist_gearbox)
132144
self.wrist_encoder_sim = AnalogEncoderSim(robot.wrist.wrist_encoder)
133145

134-
self.wrist_sim = SingleJointedArmSim(
146+
wrist_sim = SingleJointedArmSim(
135147
wrist_gearbox,
136148
WristComponent.wrist_gear_ratio,
137149
moi=0.295209215,
@@ -141,6 +153,7 @@ def __init__(self, physics_controller: PhysicsInterface, robot: MyRobot):
141153
simulateGravity=True,
142154
startingAngle=WristComponent.MAXIMUM_DEPRESSION,
143155
)
156+
self.wrist = SparkArmSim(wrist_sim, wrist_motor)
144157

145158
self.imu = robot.chassis.imu.sim_state
146159

@@ -217,12 +230,10 @@ def update_sim(self, now: float, tm_diff: float) -> None:
217230
self.vision_sim_counter = 0
218231

219232
# Update wrist simulation
220-
self.wrist_sim.setInputVoltage(self.wrist_motor.getAppliedOutput() * 12.0)
221-
self.wrist_sim.update(tm_diff)
233+
self.wrist.update(tm_diff)
222234
self.wrist_encoder_sim.set(
223-
self.wrist_sim.getAngle() + WristComponent.ENCODER_ZERO_OFFSET
235+
self.wrist.mech_sim.getAngle() + WristComponent.ENCODER_ZERO_OFFSET
224236
)
225-
self.wrist_motor.iterate(self.wrist_sim.getVelocity(), vbus=12.0, dt=tm_diff)
226237

227238
# Simulate algae pick up
228239
if self.floor_intake.current_state == "intaking":

0 commit comments

Comments
 (0)