2022-11-28 10:41:25 -05: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.
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <functional>
|
|
|
|
|
|
2025-11-07 19:57:55 -05:00
|
|
|
#include <wpi/commands2/CommandPtr.hpp>
|
|
|
|
|
#include <wpi/commands2/SubsystemBase.hpp>
|
|
|
|
|
#include <wpi/drive/DifferentialDrive.hpp>
|
|
|
|
|
#include <wpi/hardware/imu/OnboardIMU.hpp>
|
|
|
|
|
#include <wpi/hardware/motor/PWMSparkMax.hpp>
|
|
|
|
|
#include <wpi/hardware/rotation/Encoder.hpp>
|
|
|
|
|
#include <wpi/math/controller/ProfiledPIDController.hpp>
|
|
|
|
|
#include <wpi/math/controller/SimpleMotorFeedforward.hpp>
|
|
|
|
|
#include <wpi/units/angle.hpp>
|
|
|
|
|
#include <wpi/units/length.hpp>
|
2022-11-28 10:41:25 -05:00
|
|
|
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "Constants.hpp"
|
2022-11-28 10:41:25 -05:00
|
|
|
|
2023-09-14 23:56:48 -04:00
|
|
|
class Drive : public frc2::SubsystemBase {
|
2022-11-28 10:41:25 -05:00
|
|
|
public:
|
|
|
|
|
Drive();
|
|
|
|
|
/**
|
|
|
|
|
* Returns a command that drives the robot with arcade controls.
|
|
|
|
|
*
|
|
|
|
|
* @param fwd the commanded forward movement
|
|
|
|
|
* @param rot the commanded rotation
|
|
|
|
|
*/
|
2023-05-31 22:10:53 -07:00
|
|
|
frc2::CommandPtr ArcadeDriveCommand(std::function<double()> fwd,
|
|
|
|
|
std::function<double()> rot);
|
2022-11-28 10:41:25 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a command that drives the robot forward a specified distance at a
|
|
|
|
|
* specified speed.
|
|
|
|
|
*
|
|
|
|
|
* @param distance The distance to drive forward in meters
|
|
|
|
|
* @param speed The fraction of max speed at which to drive
|
|
|
|
|
*/
|
2023-05-31 22:10:53 -07:00
|
|
|
frc2::CommandPtr DriveDistanceCommand(units::meter_t distance, double speed);
|
2022-11-28 10:41:25 -05:00
|
|
|
|
2024-10-11 11:43:24 -04:00
|
|
|
/**
|
|
|
|
|
* Returns a command that turns to robot to the specified angle using a motion
|
|
|
|
|
* profile and PID controller.
|
|
|
|
|
*
|
|
|
|
|
* @param angle The angle to turn to
|
|
|
|
|
*/
|
|
|
|
|
frc2::CommandPtr TurnToAngleCommand(units::degree_t angle);
|
|
|
|
|
|
2022-11-28 10:41:25 -05:00
|
|
|
private:
|
|
|
|
|
frc::PWMSparkMax m_leftLeader{DriveConstants::kLeftMotor1Port};
|
|
|
|
|
frc::PWMSparkMax m_leftFollower{DriveConstants::kLeftMotor2Port};
|
|
|
|
|
frc::PWMSparkMax m_rightLeader{DriveConstants::kRightMotor1Port};
|
|
|
|
|
frc::PWMSparkMax m_rightFollower{DriveConstants::kRightMotor2Port};
|
|
|
|
|
|
2024-01-01 13:37:51 -08:00
|
|
|
frc::DifferentialDrive m_drive{
|
|
|
|
|
[&](double output) { m_leftLeader.Set(output); },
|
|
|
|
|
[&](double output) { m_rightLeader.Set(output); }};
|
2022-11-28 10:41:25 -05:00
|
|
|
|
|
|
|
|
frc::Encoder m_leftEncoder{DriveConstants::kLeftEncoderPorts[0],
|
|
|
|
|
DriveConstants::kLeftEncoderPorts[1],
|
|
|
|
|
DriveConstants::kLeftEncoderReversed};
|
|
|
|
|
frc::Encoder m_rightEncoder{DriveConstants::kRightEncoderPorts[0],
|
|
|
|
|
DriveConstants::kRightEncoderPorts[1],
|
|
|
|
|
DriveConstants::kRightEncoderReversed};
|
2024-10-11 11:43:24 -04:00
|
|
|
|
2025-10-10 15:44:39 -04:00
|
|
|
frc::OnboardIMU m_imu{frc::OnboardIMU::kFlat};
|
2024-10-11 11:43:24 -04:00
|
|
|
|
|
|
|
|
frc::ProfiledPIDController<units::radians> m_controller{
|
|
|
|
|
DriveConstants::kTurnP,
|
|
|
|
|
DriveConstants::kTurnI,
|
|
|
|
|
DriveConstants::kTurnD,
|
|
|
|
|
{DriveConstants::kMaxTurnRate, DriveConstants::kMaxTurnAcceleration}};
|
|
|
|
|
frc::SimpleMotorFeedforward<units::radians> m_feedforward{
|
|
|
|
|
DriveConstants::ks, DriveConstants::kv, DriveConstants::ka};
|
2022-11-28 10:41:25 -05:00
|
|
|
};
|