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-12-07 16:37:54 -05:00
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
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/rotation/Encoder.hpp>
|
|
|
|
|
#include <wpi/math/controller/SimpleMotorFeedforward.hpp>
|
|
|
|
|
#include <wpi/math/trajectory/TrapezoidProfile.hpp>
|
|
|
|
|
#include <wpi/system/Timer.hpp>
|
|
|
|
|
#include <wpi/units/length.hpp>
|
2019-12-07 16:37:54 -05:00
|
|
|
|
2025-11-07 19:56:21 -05:00
|
|
|
#include "Constants.hpp"
|
|
|
|
|
#include "ExampleSmartMotorController.hpp"
|
2019-12-07 16:37:54 -05:00
|
|
|
|
2023-09-14 23:56:48 -04:00
|
|
|
class DriveSubsystem : public frc2::SubsystemBase {
|
2019-12-07 16:37:54 -05:00
|
|
|
public:
|
|
|
|
|
DriveSubsystem();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Will be called periodically whenever the CommandScheduler runs.
|
|
|
|
|
*/
|
|
|
|
|
void Periodic() override;
|
|
|
|
|
|
|
|
|
|
// Subsystem methods go here.
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Attempts to follow the given drive states using offboard PID.
|
|
|
|
|
*
|
2024-08-14 00:01:17 -04:00
|
|
|
* @param currentLeft The current left wheel state.
|
|
|
|
|
* @param currentRight The current right wheel state.
|
|
|
|
|
* @param nextLeft The next left wheel state.
|
|
|
|
|
* @param nextRight The next right wheel state.
|
2019-12-07 16:37:54 -05:00
|
|
|
*/
|
2024-08-14 00:01:17 -04:00
|
|
|
void SetDriveStates(frc::TrapezoidProfile<units::meters>::State currentLeft,
|
|
|
|
|
frc::TrapezoidProfile<units::meters>::State currentRight,
|
|
|
|
|
frc::TrapezoidProfile<units::meters>::State nextLeft,
|
|
|
|
|
frc::TrapezoidProfile<units::meters>::State nextRight);
|
2019-12-07 16:37:54 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Drives the robot using arcade controls.
|
|
|
|
|
*
|
|
|
|
|
* @param fwd the commanded forward movement
|
|
|
|
|
* @param rot the commanded rotation
|
|
|
|
|
*/
|
|
|
|
|
void ArcadeDrive(double fwd, double rot);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Resets the drive encoders to currently read a position of 0.
|
|
|
|
|
*/
|
|
|
|
|
void ResetEncoders();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets the distance of the left encoder.
|
|
|
|
|
*
|
|
|
|
|
* @return the average of the TWO encoder readings
|
|
|
|
|
*/
|
|
|
|
|
units::meter_t GetLeftEncoderDistance();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Gets the distance of the right encoder.
|
|
|
|
|
*
|
|
|
|
|
* @return the average of the TWO encoder readings
|
|
|
|
|
*/
|
|
|
|
|
units::meter_t GetRightEncoderDistance();
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the max output of the drive. Useful for scaling the drive to drive
|
|
|
|
|
* more slowly.
|
|
|
|
|
*
|
|
|
|
|
* @param maxOutput the maximum output to which the drive will be constrained
|
|
|
|
|
*/
|
|
|
|
|
void SetMaxOutput(double maxOutput);
|
|
|
|
|
|
2024-08-14 00:01:17 -04:00
|
|
|
/**
|
|
|
|
|
* Creates a command to drive forward a specified distance using a motion
|
|
|
|
|
* profile.
|
|
|
|
|
*
|
|
|
|
|
* @param distance The distance to drive forward.
|
|
|
|
|
* @return A command.
|
|
|
|
|
*/
|
|
|
|
|
frc2::CommandPtr ProfiledDriveDistance(units::meter_t distance);
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates a command to drive forward a specified distance using a motion
|
|
|
|
|
* profile without resetting the encoders.
|
|
|
|
|
*
|
|
|
|
|
* @param distance The distance to drive forward.
|
|
|
|
|
* @return A command.
|
|
|
|
|
*/
|
|
|
|
|
frc2::CommandPtr DynamicProfiledDriveDistance(units::meter_t distance);
|
|
|
|
|
|
2019-12-07 16:37:54 -05:00
|
|
|
private:
|
2024-08-14 00:01:17 -04:00
|
|
|
frc::TrapezoidProfile<units::meters> m_profile{
|
|
|
|
|
{DriveConstants::kMaxSpeed, DriveConstants::kMaxAcceleration}};
|
|
|
|
|
frc::Timer m_timer;
|
|
|
|
|
units::meter_t m_initialLeftDistance;
|
|
|
|
|
units::meter_t m_initialRightDistance;
|
2019-12-07 16:37:54 -05:00
|
|
|
// Components (e.g. motor controllers and sensors) should generally be
|
|
|
|
|
// declared private and exposed only through public methods.
|
|
|
|
|
|
|
|
|
|
// The motor controllers
|
2020-06-19 23:06:34 -07:00
|
|
|
ExampleSmartMotorController m_leftLeader;
|
|
|
|
|
ExampleSmartMotorController m_leftFollower;
|
|
|
|
|
ExampleSmartMotorController m_rightLeader;
|
|
|
|
|
ExampleSmartMotorController m_rightFollower;
|
2019-12-07 16:37:54 -05:00
|
|
|
|
|
|
|
|
// A feedforward component for the drive
|
|
|
|
|
frc::SimpleMotorFeedforward<units::meters> m_feedforward;
|
|
|
|
|
|
|
|
|
|
// The robot's drive
|
2024-01-01 13:37:51 -08:00
|
|
|
frc::DifferentialDrive m_drive{
|
|
|
|
|
[&](double output) { m_leftLeader.Set(output); },
|
|
|
|
|
[&](double output) { m_rightLeader.Set(output); }};
|
2019-12-07 16:37:54 -05:00
|
|
|
};
|