-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphysics.py
64 lines (51 loc) · 2.31 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
#
# Copyright (c) FIRST and other WPILib contributors.
# Open Source Software; you can modify and/or share it under the terms of
# the WPILib BSD license file in the root directory of this project.
#
#
# See the documentation for more details on how this works
#
# The idea here is you provide a simulation object that overrides specific
# pieces of WPILib, and modifies motors/sensors accordingly depending on the
# state of the simulation. An example of this would be measuring a motor
# moving for a set period of time, and then changing a limit switch to turn
# on after that period of time. This can help you do more complex simulations
# of your robot code without too much extra effort.
#
import math
from wpimath.kinematics import (
SwerveDrive4Kinematics,
)
from wpilib.simulation import DCMotorSim, SimDeviceSim
from pyfrc.physics.core import PhysicsInterface
import typing
if typing.TYPE_CHECKING:
from robot import MyRobot
class PhysicsEngine:
"""
Simulates a motor moving something that strikes two limit switches,
one on each end of the track. Obviously, this is not particularly
realistic, but it's good enough to illustrate the point
"""
def __init__(self, physics_controller: PhysicsInterface, robot: "MyRobot"):
self.physics_controller = physics_controller
self.kinematics: SwerveDrive4Kinematics = robot.drive.kinematics
self.swerve_modules = robot.drive.modules
self.imu = SimDeviceSim("navX-Sensor", 4)
self.imu_yaw = self.imu.getDouble("Yaw")
def update_sim(self, now: float, tm_diff: float) -> None:
"""
Called when the simulation parameters for the program need to be
updated.
:param now: The current time as a float
:param tm_diff: The amount of time that has passed since the last
time that this function was called
"""
speeds = self.kinematics.toChassisSpeeds(
# TODO: this should get current swerve state, but we don't have that simulated yet
tuple((module.get_target_swerve_state() for module in self.swerve_modules))
)
# Update the yaw of the robot based on the rotation of the robot
self.imu_yaw.set(self.imu_yaw.get() - math.degrees(speeds.omega * tm_diff))
self.physics_controller.drive(speeds, tm_diff)