Skip to content

Commit 04dfbbe

Browse files
committed
driveTrain example updated
1 parent 7d601c3 commit 04dfbbe

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

DifferentialDriveBot/drivetrain.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,28 @@ class Drivetrain:
2323
ENCODER_RESOLUTION = 4096 # counts per revolution
2424

2525
def __init__(self):
26-
leftLeader = wpilib.PWMSparkMax(1)
27-
leftFollower = wpilib.PWMSparkMax(2)
28-
rightLeader = wpilib.PWMSparkMax(3)
29-
rightFollower = wpilib.PWMSparkMax(4)
26+
self.leftLeader = wpilib.PWMSparkMax(1)
27+
self.leftFollower = wpilib.PWMSparkMax(2)
28+
self.rightLeader = wpilib.PWMSparkMax(3)
29+
self.rightFollower = wpilib.PWMSparkMax(4)
30+
31+
32+
# Make sure both motors for each side are in the same group
33+
self.leftLeader.addFollower(self.leftFollower)
34+
self.rightLeader.addFollower(self.rightFollower)
35+
36+
# We need to invert one side of the drivetrain so that positive voltages
37+
# result in both sides moving forward. Depending on how your robot's
38+
# gearbox is constructed, you might have to invert the left side instead.
39+
self.rightLeader.setInverted(True)
40+
41+
# turns the motors into a group so that we can set voltage to both at once
42+
self.robotDrive = wpilib.drive.DifferentialDrive(self.leftLeader, self.rightLeader)
43+
3044

3145
self.leftEncoder = wpilib.Encoder(0, 1)
3246
self.rightEncoder = wpilib.Encoder(2, 3)
3347

34-
self.leftGroup = wpilib.MotorControllerGroup(leftLeader, leftFollower)
35-
self.rightGroup = wpilib.MotorControllerGroup(rightLeader, rightFollower)
3648

3749
self.gyro = wpilib.AnalogGyro(0)
3850

@@ -51,7 +63,6 @@ def __init__(self):
5163
# We need to invert one side of the drivetrain so that positive voltages
5264
# result in both sides moving forward. Depending on how your robot's
5365
# gearbox is constructed, you might have to invert the left side instead.
54-
self.rightGroup.setInverted(True)
5566

5667
# Set the distance per pulse for the drive encoders. We can simply use the
5768
# distance traveled for one rotation of the wheel divided by the encoder
@@ -84,8 +95,9 @@ def setSpeeds(self, speeds: wpimath.kinematics.DifferentialDriveWheelSpeeds):
8495
self.rightEncoder.getRate(), speeds.right
8596
)
8697

87-
self.leftGroup.setVoltage(leftOutput + leftFeedforward)
88-
self.rightGroup.setVoltage(rightOutput + rightFeedforward)
98+
# Controls the left and right sides of the robot using the calculated outputs
99+
self.robotDrive.tankDrive(leftOutput + leftFeedforward, rightOutput + rightFeedforward)
100+
89101

90102
def drive(self, xSpeed, rot):
91103
"""Drives the robot with the given linear velocity and angular velocity."""

0 commit comments

Comments
 (0)