-
Notifications
You must be signed in to change notification settings - Fork 0
/
physics.py
76 lines (60 loc) · 2.51 KB
/
physics.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import math
from wpimath.system.plant import DCMotor
from pyfrc.physics.core import PhysicsInterface
from wpilib.simulation import SingleJointedArmSim, DIOSim, SimDeviceSim
from robot.subsystems.arm import ArmPosition
from robot.constants import *
class PhysicsEngine:
"""
Physics simulation setup
"""
def __init__(self, physics_controller: PhysicsInterface, robot: "Robot"):
self.physics_controller = physics_controller
self.arm_gearbox = DCMotor.NEO()
self.lower_arm_sim = SingleJointedArmSim(
gearbox=self.arm_gearbox,
gearing=100,
moi=SingleJointedArmSim.estimateMOI(0.762, 4),
armLength=0.762,
minAngle=0,
maxAngle=math.radians(360),
simulateGravity=True,
)
self.upper_arm_sim = SingleJointedArmSim(
gearbox=self.arm_gearbox,
gearing=100,
moi=SingleJointedArmSim.estimateMOI(0.659, 3),
armLength=0.659,
minAngle=0,
maxAngle=math.radians(180),
simulateGravity=True,
)
self.lower_encoder_pos = SimDeviceSim(
"DutyCycle:DutyCycleEncoder", LOWER_ARM_ENCODER
).getDouble("absPosition")
self.upper_encoder_pos = SimDeviceSim(
"DutyCycle:DutyCycleEncoder", UPPER_ARM_ENCODER
).getDouble("absPosition")
self.l_spark = SimDeviceSim("SPARK MAX ", LOWER_ARM_MOTOR)
self.l_spark_output = self.l_spark.getDouble("Applied Output")
self.u_spark = SimDeviceSim("SPARK MAX ", UPPER_ARM_MOTOR)
self.u_spark_output = self.u_spark.getDouble("Applied Output")
self.l_limit = DIOSim(LOWER_ARM_HOME)
self.u_limit = DIOSim(UPPER_ARM_HOME)
def update_sim(self, now: float, tm_diff: float) -> None:
self.lower_arm_sim.setInputVoltage(self.l_spark_output.get())
self.upper_arm_sim.setInputVoltage(self.u_spark_output.get())
self.lower_arm_sim.update(tm_diff)
self.upper_arm_sim.update(tm_diff)
self.lower_encoder_pos.set(
(ArmPosition.HOME[0] - self.lower_arm_sim.getAngle()) / math.radians(360)
)
self.upper_encoder_pos.set(
(ArmPosition.HOME[1] - self.upper_arm_sim.getAngle()) / math.radians(360)
)
self.l_limit.setValue(
self.lower_arm_sim.wouldHitLowerLimit(self.lower_arm_sim.getAngle())
)
self.u_limit.setValue(
self.upper_arm_sim.wouldHitLowerLimit(self.lower_arm_sim.getAngle())
)