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
|
|
|
|
|
|
|
|
#include "Drivetrain.h"
|
|
|
|
|
|
2024-12-15 16:09:34 -08:00
|
|
|
#include <frc/kinematics/ChassisSpeeds.h>
|
|
|
|
|
|
2019-09-08 00:11:49 -04:00
|
|
|
frc::MecanumDriveWheelSpeeds Drivetrain::GetCurrentState() const {
|
2022-08-17 13:42:36 -07:00
|
|
|
return {units::meters_per_second_t{m_frontLeftEncoder.GetRate()},
|
|
|
|
|
units::meters_per_second_t{m_frontRightEncoder.GetRate()},
|
|
|
|
|
units::meters_per_second_t{m_backLeftEncoder.GetRate()},
|
|
|
|
|
units::meters_per_second_t{m_backRightEncoder.GetRate()}};
|
2019-09-08 00:11:49 -04:00
|
|
|
}
|
|
|
|
|
|
2022-10-25 15:28:59 -04:00
|
|
|
frc::MecanumDriveWheelPositions Drivetrain::GetCurrentWheelDistances() const {
|
|
|
|
|
return {units::meter_t{m_frontLeftEncoder.GetDistance()},
|
|
|
|
|
units::meter_t{m_frontRightEncoder.GetDistance()},
|
|
|
|
|
units::meter_t{m_backLeftEncoder.GetDistance()},
|
|
|
|
|
units::meter_t{m_backRightEncoder.GetDistance()}};
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-08 00:11:49 -04:00
|
|
|
void Drivetrain::SetSpeeds(const frc::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
|
|
|
|
2020-01-23 21:07:38 -05:00
|
|
|
m_frontLeftMotor.SetVoltage(units::volt_t{frontLeftOutput} +
|
|
|
|
|
frontLeftFeedforward);
|
|
|
|
|
m_frontRightMotor.SetVoltage(units::volt_t{frontRightOutput} +
|
|
|
|
|
frontRightFeedforward);
|
|
|
|
|
m_backLeftMotor.SetVoltage(units::volt_t{backLeftOutput} +
|
|
|
|
|
backLeftFeedforward);
|
|
|
|
|
m_backRightMotor.SetVoltage(units::volt_t{backRightOutput} +
|
|
|
|
|
backRightFeedforward);
|
2019-09-08 00:11:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Drivetrain::Drive(units::meters_per_second_t xSpeed,
|
|
|
|
|
units::meters_per_second_t ySpeed,
|
2023-07-18 21:19:55 -07:00
|
|
|
units::radians_per_second_t rot, bool fieldRelative,
|
|
|
|
|
units::second_t period) {
|
2024-12-15 16:09:34 -08:00
|
|
|
frc::ChassisSpeeds chassisSpeeds{xSpeed, ySpeed, rot};
|
|
|
|
|
if (fieldRelative) {
|
|
|
|
|
chassisSpeeds = chassisSpeeds.ToRobotRelative(m_gyro.GetRotation2d());
|
|
|
|
|
}
|
|
|
|
|
SetSpeeds(m_kinematics.ToWheelSpeeds(chassisSpeeds.Discretize(period))
|
|
|
|
|
.Desaturate(kMaxSpeed));
|
2019-09-08 00:11:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Drivetrain::UpdateOdometry() {
|
2022-10-25 15:28:59 -04:00
|
|
|
m_odometry.Update(m_gyro.GetRotation2d(), GetCurrentWheelDistances());
|
2019-09-08 00:11:49 -04:00
|
|
|
}
|