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
|
|
|
|
|
|
|
|
|
|
#include <frc/Encoder.h>
|
|
|
|
|
#include <frc/controller/PIDController.h>
|
|
|
|
|
#include <frc/controller/ProfiledPIDController.h>
|
|
|
|
|
#include <frc/geometry/Rotation2d.h>
|
|
|
|
|
#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>
|
2021-05-26 00:09:36 -07:00
|
|
|
#include <wpi/numbers>
|
2019-11-21 19:52:56 -08:00
|
|
|
|
|
|
|
|
#include "Constants.h"
|
|
|
|
|
|
|
|
|
|
class SwerveModule {
|
|
|
|
|
using radians_per_second_squared_t =
|
|
|
|
|
units::compound_unit<units::radians,
|
|
|
|
|
units::inverse<units::squared<units::second>>>;
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
SwerveModule(int driveMotorChannel, int turningMotorChannel,
|
|
|
|
|
const int driveEncoderPorts[2], const int turningEncoderPorts[2],
|
|
|
|
|
bool driveEncoderReversed, bool turningEncoderReversed);
|
|
|
|
|
|
|
|
|
|
frc::SwerveModuleState GetState();
|
|
|
|
|
|
2021-01-10 22:49:46 -08:00
|
|
|
void SetDesiredState(const 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.
|
|
|
|
|
|
|
|
|
|
static constexpr units::radians_per_second_t kModuleMaxAngularVelocity =
|
2021-05-26 00:09:36 -07:00
|
|
|
units::radians_per_second_t(wpi::numbers::pi); // radians per second
|
2019-11-21 19:52:56 -08:00
|
|
|
static constexpr units::unit_t<radians_per_second_squared_t>
|
|
|
|
|
kModuleMaxAngularAcceleration =
|
|
|
|
|
units::unit_t<radians_per_second_squared_t>(
|
2021-05-26 00:09:36 -07:00
|
|
|
wpi::numbers::pi * 2.0); // radians per second squared
|
2019-11-21 19:52:56 -08:00
|
|
|
|
|
|
|
|
frc::Spark m_driveMotor;
|
|
|
|
|
frc::Spark m_turningMotor;
|
|
|
|
|
|
|
|
|
|
frc::Encoder m_driveEncoder;
|
|
|
|
|
frc::Encoder m_turningEncoder;
|
|
|
|
|
|
|
|
|
|
bool m_reverseDriveEncoder;
|
|
|
|
|
bool m_reverseTurningEncoder;
|
|
|
|
|
|
|
|
|
|
frc2::PIDController m_drivePIDController{
|
|
|
|
|
ModuleConstants::kPModuleDriveController, 0, 0};
|
|
|
|
|
frc::ProfiledPIDController<units::radians> m_turningPIDController{
|
|
|
|
|
ModuleConstants::kPModuleTurningController,
|
|
|
|
|
0.0,
|
|
|
|
|
0.0,
|
|
|
|
|
{kModuleMaxAngularVelocity, kModuleMaxAngularAcceleration}};
|
|
|
|
|
};
|