Skip to content

Commit 61f2f7c

Browse files
committed
Move flywheel sim functionality to physics.py
1 parent b1b1528 commit 61f2f7c

File tree

2 files changed

+58
-18
lines changed

2 files changed

+58
-18
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#
2+
# Copyright (c) FIRST and other WPILib contributors.
3+
# Open Source Software; you can modify and/or share it under the terms of
4+
# the WPILib BSD license file in the root directory of this project.
5+
#
6+
7+
import wpilib.simulation
8+
from wpimath.system.plant import DCMotor
9+
10+
from pyfrc.physics.core import PhysicsInterface
11+
12+
import typing
13+
14+
if typing.TYPE_CHECKING:
15+
from robot import MyRobot
16+
17+
18+
class PhysicsEngine:
19+
"""
20+
Simulates a flywheel
21+
"""
22+
23+
def __init__(self, physics_controller: PhysicsInterface, robot: "MyRobot"):
24+
"""
25+
:param physics_controller: `pyfrc.physics.core.Physics` object
26+
to communicate simulation effects to
27+
"""
28+
29+
self.physics_controller = physics_controller
30+
31+
# Motors
32+
self.flywheelMotor = wpilib.simulation.PWMSim(robot.flywheelMotor.getChannel())
33+
34+
# Sensors
35+
self.encoder = wpilib.simulation.EncoderSim(robot.encoder)
36+
37+
# Flywheel
38+
self.flywheel = wpilib.simulation.FlywheelSim(
39+
DCMotor.NEO(1), robot.kFlywheelGearing, robot.kFlywheelMomentOfInertia
40+
)
41+
42+
def update_sim(self, now: float, tm_diff: float) -> None:
43+
"""
44+
Called when the simulation parameters for the program need to be
45+
updated.
46+
47+
:param now: The current time as a float
48+
:param tm_diff: The amount of time that has passed since the last
49+
time that this function was called
50+
"""
51+
52+
# To update our simulation, we set motor voltage inputs, update the
53+
# simulation, and write the simulated velocities to our simulated encoder
54+
self.flywheel.setInputVoltage(
55+
self.flywheelMotor.getSpeed() * wpilib.RobotController.getInputVoltage()
56+
)
57+
self.flywheel.update(0.02)
58+
self.encoder.setRate(self.flywheel.getAngularVelocity())

flywheel-bangbang-controller/robot.py

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import wpilib
99
import wpilib.simulation
1010
import wpimath.controller
11-
from wpimath.system.plant import DCMotor
1211
import wpimath.units
1312

1413
import math
@@ -58,12 +57,6 @@ def robotInit(self):
5857

5958
self.bangBangControler = wpimath.controller.BangBangController()
6059

61-
# Simulation classes help us simulate our robot
62-
self.flywheelSim = wpilib.simulation.FlywheelSim(
63-
DCMotor.NEO(1), self.kFlywheelGearing, self.kFlywheelMomentOfInertia
64-
)
65-
self.encoderSim = wpilib.simulation.EncoderSim(self.encoder)
66-
6760
# Add bang-bang controler to SmartDashboard and networktables.
6861
wpilib.SmartDashboard.putData(self.bangBangControler)
6962

@@ -91,17 +84,6 @@ def teleopPeriodic(self):
9184
bangOutput + 0.9 * self.feedforward.calculate(setpoint)
9285
)
9386

94-
def _simulationPeriodic(self):
95-
"""Update our simulation. This should be run every robot loop in simulation."""
96-
97-
# To update our simulation, we set motor voltage inputs, update the
98-
# simulation, and write the simulated velocities to our simulated encoder
99-
self.flywheelSim.setInputVoltage(
100-
self.flywheelMotor.get() * wpilib.RobotController.getInputVoltage()
101-
)
102-
self.flywheelSim.update(0.02)
103-
self.encoderSim.setRate(self.flywheelSim.getAngularVelocity())
104-
10587

10688
if __name__ == "__main__":
10789
wpilib.run(MyRobot)

0 commit comments

Comments
 (0)