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-19 15:38:42 -05:00
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2019-11-20 22:44:18 -08:00
|
|
|
#include <frc/trajectory/TrapezoidProfile.h>
|
2020-06-29 22:25:09 -07:00
|
|
|
#include <units/time.h>
|
2019-11-19 15:38:42 -05:00
|
|
|
|
2019-11-20 22:44:18 -08:00
|
|
|
#include "frc2/command/SubsystemBase.h"
|
|
|
|
|
|
2019-11-19 15:38:42 -05:00
|
|
|
namespace frc2 {
|
|
|
|
|
/**
|
2019-11-20 22:44:18 -08:00
|
|
|
* A subsystem that generates and runs trapezoidal motion profiles
|
|
|
|
|
* automatically. The user specifies how to use the current state of the motion
|
|
|
|
|
* profile by overriding the `UseState` method.
|
2022-01-08 11:11:34 -08:00
|
|
|
*
|
|
|
|
|
* This class is provided by the NewCommands VendorDep
|
2019-11-19 15:38:42 -05:00
|
|
|
*/
|
2019-11-20 23:11:46 -05:00
|
|
|
template <class Distance>
|
2019-11-19 15:38:42 -05:00
|
|
|
class TrapezoidProfileSubsystem : public SubsystemBase {
|
2019-11-20 23:11:46 -05:00
|
|
|
using Distance_t = units::unit_t<Distance>;
|
|
|
|
|
using Velocity =
|
|
|
|
|
units::compound_unit<Distance, units::inverse<units::seconds>>;
|
|
|
|
|
using Velocity_t = units::unit_t<Velocity>;
|
2019-11-21 00:46:33 -05:00
|
|
|
using State = typename frc::TrapezoidProfile<Distance>::State;
|
|
|
|
|
using Constraints = typename frc::TrapezoidProfile<Distance>::Constraints;
|
2019-11-20 22:44:18 -08:00
|
|
|
|
2019-11-19 15:38:42 -05:00
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* Creates a new TrapezoidProfileSubsystem.
|
|
|
|
|
*
|
2019-11-20 22:44:18 -08:00
|
|
|
* @param constraints The constraints (maximum velocity and acceleration)
|
|
|
|
|
* for the profiles.
|
|
|
|
|
* @param initialPosition The initial position of the controller mechanism
|
|
|
|
|
* when the subsystem is constructed.
|
2019-11-19 15:38:42 -05:00
|
|
|
* @param period The period of the main robot loop, in seconds.
|
|
|
|
|
*/
|
2019-12-23 02:09:25 -05:00
|
|
|
explicit TrapezoidProfileSubsystem(Constraints constraints,
|
|
|
|
|
Distance_t initialPosition = Distance_t{0},
|
|
|
|
|
units::second_t period = 20_ms)
|
2019-11-20 22:44:18 -08:00
|
|
|
: m_constraints(constraints),
|
2019-12-23 02:09:25 -05:00
|
|
|
m_state{initialPosition, Velocity_t(0)},
|
|
|
|
|
m_goal{initialPosition, Velocity_t{0}},
|
2019-11-20 22:44:18 -08:00
|
|
|
m_period(period) {}
|
2019-11-19 15:38:42 -05:00
|
|
|
|
|
|
|
|
void Periodic() override {
|
2019-11-20 22:44:18 -08:00
|
|
|
auto profile =
|
2019-11-26 00:46:47 -05:00
|
|
|
frc::TrapezoidProfile<Distance>(m_constraints, m_goal, m_state);
|
2019-11-19 15:38:42 -05:00
|
|
|
m_state = profile.Calculate(m_period);
|
2019-12-23 02:09:25 -05:00
|
|
|
if (m_enabled) {
|
|
|
|
|
UseState(m_state);
|
|
|
|
|
}
|
2019-11-19 15:38:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2019-11-26 00:46:47 -05:00
|
|
|
* Sets the goal state for the subsystem.
|
2019-11-19 15:38:42 -05:00
|
|
|
*
|
2019-11-26 00:46:47 -05:00
|
|
|
* @param goal The goal state for the subsystem's motion profile.
|
2019-11-19 15:38:42 -05:00
|
|
|
*/
|
2019-11-26 00:46:47 -05:00
|
|
|
void SetGoal(State goal) { m_goal = goal; }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Sets the goal state for the subsystem. Goal velocity assumed to be zero.
|
|
|
|
|
*
|
|
|
|
|
* @param goal The goal position for the subsystem's motion profile.
|
|
|
|
|
*/
|
|
|
|
|
void SetGoal(Distance_t goal) { m_goal = State{goal, Velocity_t(0)}; }
|
2019-11-19 15:38:42 -05:00
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
/**
|
2019-11-20 22:44:18 -08:00
|
|
|
* Users should override this to consume the current state of the motion
|
|
|
|
|
* profile.
|
2019-11-19 15:38:42 -05:00
|
|
|
*
|
|
|
|
|
* @param state The current state of the motion profile.
|
|
|
|
|
*/
|
|
|
|
|
virtual void UseState(State state) = 0;
|
|
|
|
|
|
2019-12-23 02:09:25 -05:00
|
|
|
/**
|
|
|
|
|
* Enable the TrapezoidProfileSubsystem's output.
|
|
|
|
|
*/
|
|
|
|
|
void Enable() { m_enabled = true; }
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Disable the TrapezoidProfileSubsystem's output.
|
|
|
|
|
*/
|
|
|
|
|
void Disable() { m_enabled = false; }
|
|
|
|
|
|
2019-11-19 15:38:42 -05:00
|
|
|
private:
|
|
|
|
|
Constraints m_constraints;
|
|
|
|
|
State m_state;
|
2019-11-26 00:46:47 -05:00
|
|
|
State m_goal;
|
2019-11-19 15:38:42 -05:00
|
|
|
units::second_t m_period;
|
2019-12-23 02:09:25 -05:00
|
|
|
bool m_enabled{false};
|
2019-11-19 15:38:42 -05:00
|
|
|
};
|
2019-11-20 22:44:18 -08:00
|
|
|
} // namespace frc2
|