2026-02-20 18:30:35 -05:00
|
|
|
#
|
|
|
|
|
# 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 math
|
|
|
|
|
import wpilib
|
|
|
|
|
import swervemodule
|
|
|
|
|
import wpimath
|
|
|
|
|
|
2026-03-06 14:19:15 -08:00
|
|
|
kMaxVelocity = 3.0 # 3 meters per second
|
|
|
|
|
kMaxAngularVelocity = math.pi # 1/2 rotation per second
|
2026-02-20 18:30:35 -05:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class Drivetrain:
|
|
|
|
|
"""
|
|
|
|
|
Represents a swerve drive style drivetrain.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
|
self.frontLeftLocation = wpimath.Translation2d(0.381, 0.381)
|
|
|
|
|
self.frontRightLocation = wpimath.Translation2d(0.381, -0.381)
|
|
|
|
|
self.backLeftLocation = wpimath.Translation2d(-0.381, 0.381)
|
|
|
|
|
self.backRightLocation = wpimath.Translation2d(-0.381, -0.381)
|
|
|
|
|
|
|
|
|
|
self.frontLeft = swervemodule.SwerveModule(1, 2, 0, 1, 2, 3)
|
|
|
|
|
self.frontRight = swervemodule.SwerveModule(3, 4, 4, 5, 6, 7)
|
|
|
|
|
self.backLeft = swervemodule.SwerveModule(5, 6, 8, 9, 10, 11)
|
|
|
|
|
self.backRight = swervemodule.SwerveModule(7, 8, 12, 13, 14, 15)
|
|
|
|
|
|
|
|
|
|
self.imu = wpilib.OnboardIMU(wpilib.OnboardIMU.MountOrientation.kFlat)
|
|
|
|
|
|
|
|
|
|
self.kinematics = wpimath.SwerveDrive4Kinematics(
|
|
|
|
|
self.frontLeftLocation,
|
|
|
|
|
self.frontRightLocation,
|
|
|
|
|
self.backLeftLocation,
|
|
|
|
|
self.backRightLocation,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.odometry = wpimath.SwerveDrive4Odometry(
|
|
|
|
|
self.kinematics,
|
|
|
|
|
self.imu.getRotation2d(),
|
|
|
|
|
(
|
|
|
|
|
self.frontLeft.getPosition(),
|
|
|
|
|
self.frontRight.getPosition(),
|
|
|
|
|
self.backLeft.getPosition(),
|
|
|
|
|
self.backRight.getPosition(),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
self.imu.resetYaw()
|
|
|
|
|
|
|
|
|
|
def drive(
|
|
|
|
|
self,
|
2026-03-06 14:19:15 -08:00
|
|
|
xVelocity: float,
|
|
|
|
|
yVelocity: float,
|
2026-02-20 18:30:35 -05:00
|
|
|
rot: float,
|
|
|
|
|
fieldRelative: bool,
|
|
|
|
|
periodSeconds: float,
|
|
|
|
|
) -> None:
|
|
|
|
|
"""
|
|
|
|
|
Method to drive the robot using joystick info.
|
2026-03-06 14:19:15 -08:00
|
|
|
:param xVelocity: Velocity of the robot in the x direction (forward).
|
|
|
|
|
:param yVelocity: Velocity of the robot in the y direction (sideways).
|
2026-02-20 18:30:35 -05:00
|
|
|
:param rot: Angular rate of the robot.
|
2026-03-06 14:19:15 -08:00
|
|
|
:param fieldRelative: Whether the provided x and y velocities are relative to the field.
|
2026-02-20 18:30:35 -05:00
|
|
|
:param periodSeconds: Time
|
|
|
|
|
"""
|
2026-03-06 14:19:15 -08:00
|
|
|
robot_velocities = wpimath.ChassisVelocities(xVelocity, yVelocity, rot)
|
2026-02-20 18:30:35 -05:00
|
|
|
if fieldRelative:
|
2026-03-06 14:19:15 -08:00
|
|
|
robot_velocities = robot_velocities.toRobotRelative(self.imu.getRotation2d())
|
2026-02-20 18:30:35 -05:00
|
|
|
|
2026-03-07 00:21:43 -05:00
|
|
|
swerveModuleStates = self.kinematics.toSwerveModuleVelocities(
|
2026-03-06 14:19:15 -08:00
|
|
|
wpimath.ChassisVelocities.discretize(robot_velocities, periodSeconds)
|
2026-02-20 18:30:35 -05:00
|
|
|
)
|
2026-03-06 14:19:15 -08:00
|
|
|
wpimath.SwerveDrive4Kinematics.desaturateWheelVelocities(
|
|
|
|
|
swerveModuleStates, kMaxVelocity
|
2026-02-20 18:30:35 -05:00
|
|
|
)
|
2026-03-07 00:21:43 -05:00
|
|
|
self.frontLeft.setDesiredVelocity(swerveModuleStates[0])
|
|
|
|
|
self.frontRight.setDesiredVelocity(swerveModuleStates[1])
|
|
|
|
|
self.backLeft.setDesiredVelocity(swerveModuleStates[2])
|
|
|
|
|
self.backRight.setDesiredVelocity(swerveModuleStates[3])
|
2026-02-20 18:30:35 -05:00
|
|
|
|
|
|
|
|
def updateOdometry(self) -> None:
|
|
|
|
|
"""Updates the field relative position of the robot."""
|
|
|
|
|
self.odometry.update(
|
|
|
|
|
self.imu.getRotation2d(),
|
|
|
|
|
(
|
|
|
|
|
self.frontLeft.getPosition(),
|
|
|
|
|
self.frontRight.getPosition(),
|
|
|
|
|
self.backLeft.getPosition(),
|
|
|
|
|
self.backRight.getPosition(),
|
|
|
|
|
),
|
|
|
|
|
)
|