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
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2022-10-15 16:33:14 -07:00
|
|
|
#include <numbers>
|
|
|
|
|
|
2025-11-08 16:58:51 -08:00
|
|
|
#include "wpi/hardware/imu/OnboardIMU.hpp"
|
|
|
|
|
#include "wpi/hardware/motor/PWMSparkMax.hpp"
|
|
|
|
|
#include "wpi/hardware/rotation/Encoder.hpp"
|
|
|
|
|
#include "wpi/math/controller/PIDController.hpp"
|
|
|
|
|
#include "wpi/math/controller/SimpleMotorFeedforward.hpp"
|
|
|
|
|
#include "wpi/math/geometry/Translation2d.hpp"
|
|
|
|
|
#include "wpi/math/kinematics/MecanumDriveKinematics.hpp"
|
|
|
|
|
#include "wpi/math/kinematics/MecanumDriveOdometry.hpp"
|
2026-03-06 14:19:15 -08:00
|
|
|
#include "wpi/math/kinematics/MecanumDriveWheelVelocities.hpp"
|
2019-09-08 00:11:49 -04:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Represents a mecanum drive style drivetrain.
|
|
|
|
|
*/
|
|
|
|
|
class Drivetrain {
|
|
|
|
|
public:
|
2021-12-26 19:25:26 -08:00
|
|
|
Drivetrain() {
|
2025-10-10 15:44:39 -04:00
|
|
|
m_imu.ResetYaw();
|
2021-12-26 19:25:26 -08:00
|
|
|
// 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.
|
|
|
|
|
m_frontRightMotor.SetInverted(true);
|
|
|
|
|
m_backRightMotor.SetInverted(true);
|
|
|
|
|
}
|
2019-09-08 00:11:49 -04:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::math::MecanumDriveWheelPositions GetCurrentWheelDistances() const;
|
2026-03-06 14:19:15 -08:00
|
|
|
wpi::math::MecanumDriveWheelVelocities GetCurrentWheelVelocities() const;
|
|
|
|
|
void SetVelocities(
|
|
|
|
|
const wpi::math::MecanumDriveWheelVelocities& wheelVelocities);
|
|
|
|
|
void Drive(wpi::units::meters_per_second_t xVelocity,
|
|
|
|
|
wpi::units::meters_per_second_t yVelocity,
|
2025-11-07 20:01:58 -05:00
|
|
|
wpi::units::radians_per_second_t rot, bool fieldRelative,
|
|
|
|
|
wpi::units::second_t period);
|
2019-09-08 00:11:49 -04:00
|
|
|
void UpdateOdometry();
|
|
|
|
|
|
2026-03-06 14:19:15 -08:00
|
|
|
static constexpr wpi::units::meters_per_second_t kMaxVelocity =
|
2019-09-08 00:11:49 -04:00
|
|
|
3.0_mps; // 3 meters per second
|
2026-03-06 14:19:15 -08:00
|
|
|
static constexpr wpi::units::radians_per_second_t kMaxAngularVelocity{
|
2022-10-15 16:33:14 -07:00
|
|
|
std::numbers::pi}; // 1/2 rotation per second
|
2019-09-08 00:11:49 -04:00
|
|
|
|
|
|
|
|
private:
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::PWMSparkMax m_frontLeftMotor{1};
|
|
|
|
|
wpi::PWMSparkMax m_frontRightMotor{2};
|
|
|
|
|
wpi::PWMSparkMax m_backLeftMotor{3};
|
|
|
|
|
wpi::PWMSparkMax m_backRightMotor{4};
|
2019-09-08 00:11:49 -04:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::Encoder m_frontLeftEncoder{0, 1};
|
|
|
|
|
wpi::Encoder m_frontRightEncoder{2, 3};
|
|
|
|
|
wpi::Encoder m_backLeftEncoder{4, 5};
|
|
|
|
|
wpi::Encoder m_backRightEncoder{6, 7};
|
2019-09-08 00:11:49 -04:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::math::Translation2d m_frontLeftLocation{0.381_m, 0.381_m};
|
|
|
|
|
wpi::math::Translation2d m_frontRightLocation{0.381_m, -0.381_m};
|
|
|
|
|
wpi::math::Translation2d m_backLeftLocation{-0.381_m, 0.381_m};
|
|
|
|
|
wpi::math::Translation2d m_backRightLocation{-0.381_m, -0.381_m};
|
2019-09-08 00:11:49 -04:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::math::PIDController m_frontLeftPIDController{1.0, 0.0, 0.0};
|
|
|
|
|
wpi::math::PIDController m_frontRightPIDController{1.0, 0.0, 0.0};
|
|
|
|
|
wpi::math::PIDController m_backLeftPIDController{1.0, 0.0, 0.0};
|
|
|
|
|
wpi::math::PIDController m_backRightPIDController{1.0, 0.0, 0.0};
|
2019-09-08 00:11:49 -04:00
|
|
|
|
2026-03-17 16:45:25 -07:00
|
|
|
wpi::OnboardIMU m_imu{wpi::OnboardIMU::FLAT};
|
2019-09-08 00:11:49 -04:00
|
|
|
|
2025-11-07 20:00:05 -05:00
|
|
|
wpi::math::MecanumDriveKinematics m_kinematics{
|
2019-09-08 00:11:49 -04:00
|
|
|
m_frontLeftLocation, m_frontRightLocation, m_backLeftLocation,
|
|
|
|
|
m_backRightLocation};
|
|
|
|
|
|
2025-11-07 20:01:58 -05:00
|
|
|
wpi::math::MecanumDriveOdometry m_odometry{
|
|
|
|
|
m_kinematics, m_imu.GetRotation2d(), GetCurrentWheelDistances()};
|
2020-01-23 21:07:38 -05:00
|
|
|
|
|
|
|
|
// Gains are for example purposes only - must be determined for your own
|
|
|
|
|
// robot!
|
2025-11-07 20:01:58 -05:00
|
|
|
wpi::math::SimpleMotorFeedforward<wpi::units::meters> m_feedforward{
|
|
|
|
|
1_V, 3_V / 1_mps};
|
2019-09-08 00:11:49 -04:00
|
|
|
};
|