This repository was archived by the owner on May 3, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathdrivetrain.py
More file actions
executable file
·105 lines (82 loc) · 3.8 KB
/
drivetrain.py
File metadata and controls
executable file
·105 lines (82 loc) · 3.8 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#
# Copyright (c) FIRST and other WPILib contributors.
# Open Source Software; you can modify and/or share it under the terms of
# the WPILib BSD license file in the root directory of this project.
#
import wpilib.drive
import wpimath.controller
import wpimath.kinematics
import wpimath.units
import math
class Drivetrain:
"""Represents a differential drive style drivetrain."""
MAX_SPEED = 3.0 # meters per second
MAX_ANGULAR_SPEED = 2 * math.pi # one rotation per second
TRACK_WIDTH = 0.381 * 2 # meters
WHEEL_RADIUS = 0.0508 # meters
ENCODER_RESOLUTION = 4096 # counts per revolution
def __init__(self):
self.leftLeader = wpilib.PWMSparkMax(1)
self.leftFollower = wpilib.PWMSparkMax(2)
self.rightLeader = wpilib.PWMSparkMax(3)
self.rightFollower = wpilib.PWMSparkMax(4)
# Make sure both motors for each side are in the same group
self.leftLeader.addFollower(self.leftFollower)
self.rightLeader.addFollower(self.rightFollower)
# We need to invert one side of the drivetrain so that positive voltages
# result in both sides moving forward. Depending on how your robot's
# gearbox is constructed, you might have to invert the left side instead.
self.rightLeader.setInverted(True)
self.leftEncoder = wpilib.Encoder(0, 1)
self.rightEncoder = wpilib.Encoder(2, 3)
self.gyro = wpilib.AnalogGyro(0)
self.leftPIDController = wpimath.controller.PIDController(1.0, 0.0, 0.0)
self.rightPIDController = wpimath.controller.PIDController(1.0, 0.0, 0.0)
self.kinematics = wpimath.kinematics.DifferentialDriveKinematics(
self.TRACK_WIDTH
)
# Gains are for example purposes only - must be determined for your own robot!
self.feedforward = wpimath.controller.SimpleMotorFeedforwardMeters(1, 3)
self.gyro.reset()
# Set the distance per pulse for the drive encoders. We can simply use the
# distance traveled for one rotation of the wheel divided by the encoder
# resolution.
self.leftEncoder.setDistancePerPulse(
2 * math.pi * self.WHEEL_RADIUS / self.ENCODER_RESOLUTION
)
self.rightEncoder.setDistancePerPulse(
2 * math.pi * self.WHEEL_RADIUS / self.ENCODER_RESOLUTION
)
self.leftEncoder.reset()
self.rightEncoder.reset()
self.odometry = wpimath.kinematics.DifferentialDriveOdometry(
self.gyro.getRotation2d(),
self.leftEncoder.getDistance(),
self.rightEncoder.getDistance(),
)
def setSpeeds(self, speeds: wpimath.kinematics.DifferentialDriveWheelSpeeds):
"""Sets the desired wheel speeds."""
leftFeedforward = self.feedforward.calculate(speeds.left)
rightFeedforward = self.feedforward.calculate(speeds.right)
leftOutput = self.leftPIDController.calculate(
self.leftEncoder.getRate(), speeds.left
)
rightOutput = self.rightPIDController.calculate(
self.rightEncoder.getRate(), speeds.right
)
# Controls the left and right sides of the robot using the calculated outputs
self.leftLeader.setVoltage(leftOutput + leftFeedforward)
self.rightLeader.setVoltage(rightOutput + rightFeedforward)
def drive(self, xSpeed, rot):
"""Drives the robot with the given linear velocity and angular velocity."""
wheelSpeeds = self.kinematics.toWheelSpeeds(
wpimath.kinematics.ChassisSpeeds(xSpeed, 0, rot)
)
self.setSpeeds(wheelSpeeds)
def updateOdometry(self):
"""Updates the field-relative position."""
self.odometry.update(
self.gyro.getRotation2d(),
self.leftEncoder.getDistance(),
self.rightEncoder.getDistance(),
)