2019-11-19 15:38:42 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
/* Copyright (c) 2019 FIRST. All Rights Reserved. */
|
|
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in the root directory of */
|
|
|
|
|
/* the project. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "frc2/command/SubsystemBase.h"
|
|
|
|
|
#include "frc/trajectory/TrapezoidProfile.h"
|
|
|
|
|
|
|
|
|
|
#include <units/units.h>
|
|
|
|
|
|
|
|
|
|
namespace frc2 {
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*/
|
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-19 15:38:42 -05:00
|
|
|
public:
|
|
|
|
|
/**
|
|
|
|
|
* Creates a new TrapezoidProfileSubsystem.
|
|
|
|
|
*
|
|
|
|
|
* @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.
|
|
|
|
|
* @param period The period of the main robot loop, in seconds.
|
|
|
|
|
*/
|
|
|
|
|
TrapezoidProfileSubsystem(Constraints constraints,
|
2019-11-20 23:11:46 -05:00
|
|
|
Distance_t position,
|
2019-11-19 15:38:42 -05:00
|
|
|
units::second_t period = 20_ms)
|
|
|
|
|
: m_constraints(constraints),
|
2019-11-20 23:11:46 -05:00
|
|
|
m_state{position, Velocity_t(0)},
|
2019-11-19 15:38:42 -05:00
|
|
|
m_period(period) {}
|
|
|
|
|
|
|
|
|
|
void Periodic() override {
|
2019-11-20 23:11:46 -05:00
|
|
|
auto profile = frc::TrapezoidProfile<Distance>(m_constraints, GetGoal(), m_state);
|
2019-11-19 15:38:42 -05:00
|
|
|
m_state = profile.Calculate(m_period);
|
|
|
|
|
UseState(m_state);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Users should override this to return the goal state for the subsystem's motion profile.
|
|
|
|
|
*
|
|
|
|
|
* @return The goal state for the subsystem's motion profile.
|
|
|
|
|
*/
|
|
|
|
|
virtual State GetGoal() = 0;
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Users should override this to consume the current state of the motion profile.
|
|
|
|
|
*
|
|
|
|
|
* @param state The current state of the motion profile.
|
|
|
|
|
*/
|
|
|
|
|
virtual void UseState(State state) = 0;
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
Constraints m_constraints;
|
|
|
|
|
State m_state;
|
|
|
|
|
units::second_t m_period;
|
|
|
|
|
};
|
|
|
|
|
}
|