2020-12-26 14:12:05 -08: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.
|
2019-09-08 00:11:49 -04:00
|
|
|
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "Drivetrain.hpp"
|
2019-09-08 00:11:49 -04:00
|
|
|
|
2025-11-07 19:57:55 -05:00
|
|
|
#include <wpi/math/kinematics/ChassisSpeeds.hpp>
|
2024-12-15 16:09:34 -08:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::math::MecanumDriveWheelSpeeds Drivetrain::GetCurrentState() const {
|
|
|
|
|
return {wpi::units::meters_per_second_t{m_frontLeftEncoder.GetRate()},
|
|
|
|
|
wpi::units::meters_per_second_t{m_frontRightEncoder.GetRate()},
|
|
|
|
|
wpi::units::meters_per_second_t{m_backLeftEncoder.GetRate()},
|
|
|
|
|
wpi::units::meters_per_second_t{m_backRightEncoder.GetRate()}};
|
2019-09-08 00:11:49 -04:00
|
|
|
}
|
|
|
|
|
|
2025-11-07 20:01:58 -05:00
|
|
|
wpi::math::MecanumDriveWheelPositions Drivetrain::GetCurrentWheelDistances()
|
|
|
|
|
const {
|
2025-11-07 20:00:05 -05:00
|
|
|
return {wpi::units::meter_t{m_frontLeftEncoder.GetDistance()},
|
|
|
|
|
wpi::units::meter_t{m_frontRightEncoder.GetDistance()},
|
|
|
|
|
wpi::units::meter_t{m_backLeftEncoder.GetDistance()},
|
|
|
|
|
wpi::units::meter_t{m_backRightEncoder.GetDistance()}};
|
2022-10-25 15:28:59 -04:00
|
|
|
}
|
|
|
|
|
|
2025-11-07 20:01:58 -05:00
|
|
|
void Drivetrain::SetSpeeds(
|
|
|
|
|
const wpi::math::MecanumDriveWheelSpeeds& wheelSpeeds) {
|
2020-01-23 21:07:38 -05:00
|
|
|
const auto frontLeftFeedforward =
|
|
|
|
|
m_feedforward.Calculate(wheelSpeeds.frontLeft);
|
|
|
|
|
const auto frontRightFeedforward =
|
|
|
|
|
m_feedforward.Calculate(wheelSpeeds.frontRight);
|
|
|
|
|
const auto backLeftFeedforward =
|
|
|
|
|
m_feedforward.Calculate(wheelSpeeds.rearLeft);
|
|
|
|
|
const auto backRightFeedforward =
|
|
|
|
|
m_feedforward.Calculate(wheelSpeeds.rearRight);
|
|
|
|
|
|
|
|
|
|
const double frontLeftOutput = m_frontLeftPIDController.Calculate(
|
2021-10-25 08:58:12 -07:00
|
|
|
m_frontLeftEncoder.GetRate(), wheelSpeeds.frontLeft.value());
|
2020-01-23 21:07:38 -05:00
|
|
|
const double frontRightOutput = m_frontRightPIDController.Calculate(
|
2021-10-25 08:58:12 -07:00
|
|
|
m_frontRightEncoder.GetRate(), wheelSpeeds.frontRight.value());
|
2020-01-23 21:07:38 -05:00
|
|
|
const double backLeftOutput = m_backLeftPIDController.Calculate(
|
2021-10-25 08:58:12 -07:00
|
|
|
m_backLeftEncoder.GetRate(), wheelSpeeds.rearLeft.value());
|
2020-01-23 21:07:38 -05:00
|
|
|
const double backRightOutput = m_backRightPIDController.Calculate(
|
2021-10-25 08:58:12 -07:00
|
|
|
m_backRightEncoder.GetRate(), wheelSpeeds.rearRight.value());
|
2019-09-08 00:11:49 -04:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
m_frontLeftMotor.SetVoltage(wpi::units::volt_t{frontLeftOutput} +
|
2020-01-23 21:07:38 -05:00
|
|
|
frontLeftFeedforward);
|
2025-11-07 20:00:05 -05:00
|
|
|
m_frontRightMotor.SetVoltage(wpi::units::volt_t{frontRightOutput} +
|
2020-01-23 21:07:38 -05:00
|
|
|
frontRightFeedforward);
|
2025-11-07 20:00:05 -05:00
|
|
|
m_backLeftMotor.SetVoltage(wpi::units::volt_t{backLeftOutput} +
|
2020-01-23 21:07:38 -05:00
|
|
|
backLeftFeedforward);
|
2025-11-07 20:00:05 -05:00
|
|
|
m_backRightMotor.SetVoltage(wpi::units::volt_t{backRightOutput} +
|
2020-01-23 21:07:38 -05:00
|
|
|
backRightFeedforward);
|
2019-09-08 00:11:49 -04:00
|
|
|
}
|
|
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
void Drivetrain::Drive(wpi::units::meters_per_second_t xSpeed,
|
|
|
|
|
wpi::units::meters_per_second_t ySpeed,
|
|
|
|
|
wpi::units::radians_per_second_t rot, bool fieldRelative,
|
|
|
|
|
wpi::units::second_t period) {
|
|
|
|
|
wpi::math::ChassisSpeeds chassisSpeeds{xSpeed, ySpeed, rot};
|
2024-12-15 16:09:34 -08:00
|
|
|
if (fieldRelative) {
|
2025-10-10 15:44:39 -04:00
|
|
|
chassisSpeeds = chassisSpeeds.ToRobotRelative(m_imu.GetRotation2d());
|
2024-12-15 16:09:34 -08:00
|
|
|
}
|
|
|
|
|
SetSpeeds(m_kinematics.ToWheelSpeeds(chassisSpeeds.Discretize(period))
|
|
|
|
|
.Desaturate(kMaxSpeed));
|
2019-09-08 00:11:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Drivetrain::UpdateOdometry() {
|
2025-10-10 15:44:39 -04:00
|
|
|
m_odometry.Update(m_imu.GetRotation2d(), GetCurrentWheelDistances());
|
2019-09-08 00:11:49 -04:00
|
|
|
}
|