Skip to content

Commit d516cab

Browse files
authored
Merge pull request #178 from martinrioux/frc-doc-getting-started
Getting Started example added (basic Drivetrain)
2 parents f469977 + 5b8d33f commit d516cab

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

examples/getting-started/robot.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
#!/usr/bin/env python3
2+
#
3+
# Copyright (c) FIRST and other WPILib contributors.
4+
# Open Source Software; you can modify and/or share it under the terms of
5+
# the WPILib BSD license file in the root directory of this project.
6+
#
7+
8+
import wpilib
9+
import wpilib.drive
10+
import ctre
11+
12+
13+
class MyRobot(wpilib.TimedRobot):
14+
def robotInit(self):
15+
"""
16+
This function is called upon program startup and
17+
should be used for any initialization code.
18+
"""
19+
self.leftDrive = ctre.WPI_TalonFX(1)
20+
self.rightDrive = ctre.WPI_TalonFX(2)
21+
self.robotDrive = wpilib.drive.DifferentialDrive(
22+
self.leftDrive, self.rightDrive
23+
)
24+
self.controller = wpilib.XboxController(0)
25+
self.timer = wpilib.Timer()
26+
27+
# We need to invert one side of the drivetrain so that positive voltages
28+
# result in both sides moving forward. Depending on how your robot's
29+
# gearbox is constructed, you might have to invert the left side instead.
30+
self.rightDrive.setInverted(True)
31+
32+
def autonomousInit(self):
33+
"""This function is run once each time the robot enters autonomous mode."""
34+
self.timer.restart()
35+
36+
def autonomousPeriodic(self):
37+
"""This function is called periodically during autonomous."""
38+
39+
# Drive for two seconds
40+
if self.timer.get() < 2.0:
41+
# Drive forwards half speed, make sure to turn input squaring off
42+
self.robotDrive.arcadeDrive(0.5, 0, squareInputs=False)
43+
else:
44+
self.robotDrive.stopMotor() # Stop robot
45+
46+
def teleopInit(self):
47+
"""This function is called once each time the robot enters teleoperated mode."""
48+
49+
def teleopPeriodic(self):
50+
"""This function is called periodically during teleoperated mode."""
51+
self.robotDrive.arcadeDrive(
52+
-self.controller.getLeftY(), -self.controller.getRightX()
53+
)
54+
55+
def testInit(self):
56+
"""This function is called once each time the robot enters test mode."""
57+
58+
def testPeriodic(self):
59+
"""This function is called periodically during test mode."""
60+
61+
62+
if __name__ == "__main__":
63+
wpilib.run(MyRobot)

0 commit comments

Comments
 (0)