Skip to content

Commit 4813439

Browse files
authored
Merge pull request #84 from fletch3555/add_tank-drive-xbox-controller_example
add tank-drive-xbox-controller example
2 parents 82298e6 + 8bb3906 commit 4813439

File tree

3 files changed

+52
-19
lines changed

3 files changed

+52
-19
lines changed

run_tests.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ BASE_TESTS="
3535
stateful-autonomous
3636
state-space-flywheel
3737
tank-drive
38+
tank-drive-xbox-controller
3839
timed/src
3940
"
4041

tank-drive-xbox-controller/robot.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env python3
2+
"""
3+
This is a demo program showing the use of the DifferentialDrive class.
4+
Runs the motors with tank steering and an Xbox controller.
5+
"""
6+
7+
import wpilib
8+
from wpilib.drive import DifferentialDrive
9+
10+
11+
class MyRobot(wpilib.TimedRobot):
12+
def robotInit(self):
13+
"""Robot initialization function"""
14+
15+
# object that handles basic drive operations
16+
left = wpilib.PWMSparkMax(0)
17+
right = wpilib.PWMSparkMax(1)
18+
19+
self.robotDrive = DifferentialDrive(left, right)
20+
21+
# xbox controller 0 on the driver station
22+
self.driverController = wpilib.XboxController(0)
23+
24+
# We need to invert one side of the drivetrain so that positive voltages
25+
# result in both sides moving forward. Depending on how your robot's
26+
# gearbox is constructed, you might have to invert the left side instead.
27+
right.setInverted(True)
28+
29+
def teleopPeriodic(self):
30+
# Drive with tank drive.
31+
# That means that the Y axis of the left stick moves the left side
32+
# of the robot forward and backward, and the Y axis of the right stick
33+
# moves the right side of the robot forward and backward.
34+
self.robotDrive.tankDrive(
35+
-self.driverController.getLeftY(), -self.driverController.getRightY()
36+
)
37+
38+
39+
if __name__ == "__main__":
40+
wpilib.run(MyRobot)

tank-drive/robot.py

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
#!/usr/bin/env python3
22
"""
3-
This is a demo program showing the use of the RobotDrive class,
4-
specifically it contains the code necessary to operate a robot with
5-
tank drive.
3+
This is a demo program showing the use of the DifferentialDrive class.
4+
Runs the motors with tank steering.
65
"""
76

87
import wpilib
@@ -14,30 +13,23 @@ def robotInit(self):
1413
"""Robot initialization function"""
1514

1615
# object that handles basic drive operations
17-
self.frontLeftMotor = wpilib.Talon(0)
18-
self.rearLeftMotor = wpilib.Talon(1)
19-
self.frontRightMotor = wpilib.Talon(2)
20-
self.rearRightMotor = wpilib.Talon(3)
16+
left = wpilib.PWMSparkMax(0)
17+
right = wpilib.PWMSparkMax(1)
2118

22-
self.left = wpilib.MotorControllerGroup(self.frontLeftMotor, self.rearLeftMotor)
23-
self.right = wpilib.MotorControllerGroup(
24-
self.frontRightMotor, self.rearRightMotor
25-
)
26-
27-
self.myRobot = DifferentialDrive(self.left, self.right)
28-
self.myRobot.setExpiration(0.1)
19+
self.robotDrive = DifferentialDrive(left, right)
2920

3021
# joysticks 1 & 2 on the driver station
3122
self.leftStick = wpilib.Joystick(0)
3223
self.rightStick = wpilib.Joystick(1)
3324

34-
def teleopInit(self):
35-
"""Executed at the start of teleop mode"""
36-
self.myRobot.setSafetyEnabled(True)
25+
# We need to invert one side of the drivetrain so that positive voltages
26+
# result in both sides moving forward. Depending on how your robot's
27+
# gearbox is constructed, you might have to invert the left side instead.
28+
right.setInverted(True)
3729

3830
def teleopPeriodic(self):
39-
"""Runs the motors with tank steering"""
40-
self.myRobot.tankDrive(self.leftStick.getY() * -1, self.rightStick.getY() * -1)
31+
"""Drive with tank style"""
32+
self.robotDrive.tankDrive(-self.leftStick.getY(), -self.rightStick.getY())
4133

4234

4335
if __name__ == "__main__":

0 commit comments

Comments
 (0)