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-11-21 19:52:56 -08:00
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2022-10-15 16:33:14 -07:00
|
|
|
#include <numbers>
|
|
|
|
|
|
2019-11-21 19:52:56 -08:00
|
|
|
#include <frc/Encoder.h>
|
|
|
|
|
#include <frc/controller/PIDController.h>
|
|
|
|
|
#include <frc/controller/ProfiledPIDController.h>
|
|
|
|
|
#include <frc/geometry/Rotation2d.h>
|
2022-10-25 15:28:36 -04:00
|
|
|
#include <frc/kinematics/SwerveModulePosition.h>
|
2019-11-21 19:52:56 -08:00
|
|
|
#include <frc/kinematics/SwerveModuleState.h>
|
2021-04-17 11:27:16 -07:00
|
|
|
#include <frc/motorcontrol/Spark.h>
|
2019-11-21 19:52:56 -08:00
|
|
|
#include <frc/trajectory/TrapezoidProfile.h>
|
|
|
|
|
|
|
|
|
|
#include "Constants.h"
|
|
|
|
|
|
|
|
|
|
class SwerveModule {
|
|
|
|
|
public:
|
|
|
|
|
SwerveModule(int driveMotorChannel, int turningMotorChannel,
|
|
|
|
|
const int driveEncoderPorts[2], const int turningEncoderPorts[2],
|
|
|
|
|
bool driveEncoderReversed, bool turningEncoderReversed);
|
|
|
|
|
|
|
|
|
|
frc::SwerveModuleState GetState();
|
|
|
|
|
|
2022-10-25 15:28:36 -04:00
|
|
|
frc::SwerveModulePosition GetPosition();
|
|
|
|
|
|
2024-09-30 15:23:30 -04:00
|
|
|
void SetDesiredState(frc::SwerveModuleState& state);
|
2019-11-21 19:52:56 -08:00
|
|
|
|
|
|
|
|
void ResetEncoders();
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
// We have to use meters here instead of radians due to the fact that
|
|
|
|
|
// ProfiledPIDController's constraints only take in meters per second and
|
|
|
|
|
// meters per second squared.
|
|
|
|
|
|
2022-08-17 13:42:36 -07:00
|
|
|
static constexpr auto kModuleMaxAngularVelocity =
|
2022-10-15 16:33:14 -07:00
|
|
|
units::radians_per_second_t{std::numbers::pi};
|
2022-08-17 13:42:36 -07:00
|
|
|
static constexpr auto kModuleMaxAngularAcceleration =
|
2022-10-15 16:33:14 -07:00
|
|
|
units::radians_per_second_squared_t{std::numbers::pi * 2.0};
|
2019-11-21 19:52:56 -08:00
|
|
|
|
|
|
|
|
frc::Spark m_driveMotor;
|
|
|
|
|
frc::Spark m_turningMotor;
|
|
|
|
|
|
|
|
|
|
frc::Encoder m_driveEncoder;
|
|
|
|
|
frc::Encoder m_turningEncoder;
|
|
|
|
|
|
2023-09-15 19:57:31 -07:00
|
|
|
frc::PIDController m_drivePIDController{
|
2019-11-21 19:52:56 -08:00
|
|
|
ModuleConstants::kPModuleDriveController, 0, 0};
|
|
|
|
|
frc::ProfiledPIDController<units::radians> m_turningPIDController{
|
|
|
|
|
ModuleConstants::kPModuleTurningController,
|
|
|
|
|
0.0,
|
|
|
|
|
0.0,
|
|
|
|
|
{kModuleMaxAngularVelocity, kModuleMaxAngularAcceleration}};
|
|
|
|
|
};
|