forked from RustyRaptor/Robot2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrobot.py
80 lines (54 loc) · 2.83 KB
/
robot.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
77
78
79
80
# Imports the packages so they can be used to run the robot. You can import a package from a package to avoid having
# to type in a lengthy string such as wpilib.drive.DifferentialDrive.
import wpilib
from wpilib.drive import DifferentialDrive
import robotpy_ext.autonomous
# ctre lets you use the can bus on the RoboRio.
import ctre
class MyRobot(wpilib.TimedRobot):
# Defines the channels on the RoboRio that the motors are plugged into. There can be up to eight.
FLChannel = 0
FRChannel = 1
RLChannel = 2
RRChannel = 3
# Defines the order that the sticks that are plugged in are assigned.
DriveStickChannel = 0
# ExtraStickChannel = 1
def robotInit(self):
# Launches the camera server so that we can have video through any cameras on the robot.
wpilib.CameraServer.launch()
# Defines the motors that will actually be on the robot for use in the drive function.
self.FLMotor = wpilib.Spark(self.FLChannel)
self.FRMotor = wpilib.Spark(self.FRChannel)
self.RLMotor = wpilib.Spark(self.RLChannel)
self.RRMotor = wpilib.Spark(self.RRChannel)
# Puts the motors into groups so that they fit the parameters of the function.
self.LMG = wpilib.SpeedControllerGroup(self.FLMotor, self.RLMotor)
self.RMG = wpilib.SpeedControllerGroup(self.FRMotor, self.RRMotor)
# The drive function that tells the computer what kind of drive to use and where the motors are.
self.drive = DifferentialDrive(self.LMG, self.RMG)
# Tells the computer how long to wait without input to turn off the motors
self.drive.setExpiration(0.1)
# Defines the Joystick that we will be using for driving.
self.DriveStick = wpilib.Joystick(self.DriveStickChannel)
def autonomous(self):
self.drive.setSafetyEnabled(True)
while self.isAutonomous() and self.isEnabled():
robotpy_ext.autonomous.AutonomousModeSelector("Game_Left")
def operatorControl(self):
# Enables the safety on the drive. Very important. DO NOT FORGET!
self.drive.setSafetyEnabled(True)
# Checks to see if the robot is activated and that operator control is active, so your robot does not move
# when it is not supposed to.
while self.isOperatorControl() and self.isEnabled():
# drives the robot with the arcade drive, which uses one joystick and is a bit easier to use.
# It is a part of DifferentialDrive
self.drive.arcadeDrive(
self.DriveStick.getY(),
self.DriveStick.getX(),
squareInputs=True
)
# Keeps the robot from constantly bombarding your computer for inputs, saving some CPU time for changing
# control modes or turning off the robot
if __name__ == '__main__':
wpilib.run(MyRobot)