-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrobot.py
More file actions
72 lines (53 loc) · 2.85 KB
/
robot.py
File metadata and controls
72 lines (53 loc) · 2.85 KB
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
import wpilib
from ntcore import NetworkTableInstance
from robotcontainer import RobotContainer
from wpilib import DriverStation
from commands2 import CommandScheduler
class MyRobot(wpilib.TimedRobot):
"""Robot class. Notice it has no constructor.
I don't know why I felt the need to point that out. Oh well"""
def robotInit(self) -> None:
"""Robot initialization"""
self.alliance = DriverStation.getAlliance() # the alliance we are on
self.container = RobotContainer() # ROBOT CONTAINER RAHHH
self.swerve = self.container.drive # drivetrain!
# Logging stuff
nt_instance = NetworkTableInstance.getDefault()
self.nt_table = nt_instance.getTable("SmartDashboard")
self.pose_pub = self.nt_table.getDoubleArrayTopic("RobotPose").publish()
self.speeds_pub = self.nt_table.getDoubleArrayTopic("ChassisSpeeds").publish()
self.mode_pub = self.nt_table.getStringTopic("RobotMode").publish()
self.states_pub = self.nt_table.getDoubleArrayTopic("SwerveModuleStates").publish()
self.autonomous_command = None
def robotPeriodic(self) -> None:
"""Runs periodically."""
CommandScheduler.getInstance().run() # ok, so what this does is, uhhh it gets the scheduled commands and then runs them. I think. I'm not fully sure how it works. Command based programming is extremely wonky.
self.updateTelemetry()
def updateTelemetry(self) -> None:
"""Update telemetry"""
try:
# gets the drivetrain's data
pose = self.swerve.get_pose()
speeds = self.swerve.get_robot_relative_speeds()
module_states = self.swerve.drivetrain.get_state().module_states
# publishes drivetrain data
self.pose_pub.set([pose.X(), pose.Y(), pose.rotation().degrees()])
self.speeds_pub.set([speeds.vx, speeds.vy, speeds.omega])
self.states_pub.set([state.angle.degrees() for state in module_states])
# publishes the current mode.
mode_str = "Autonomous" if self.isAutonomous() else "Teleop" if self.isTeleop() else "Disabled"
self.mode_pub.set(mode_str)
except Exception as e:
print(f"Telemetry error: {e}")
def autonomousInit(self):
"""Runs when auto begins. Basically just runs the Autonomous commands as seen here."""
self.autonomous_command = self.container.getAutonomousCommand()
if self.autonomous_command:
self.autonomous_command.schedule()
def teleopInit(self):
"""Runs when teleop begins"""
if self.autonomous_command:
self.autonomous_command.cancel()
def testExit(self):
"""Cancels all commands when the robot exits testing mode."""
CommandScheduler.getInstance().cancelAll()