Files
allwpilib/wpilibNewCommands/src/main/native/include/frc2/command/TrapezoidProfileCommand.h

101 lines
3.4 KiB
C
Raw Normal View History

// 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-10-26 12:58:13 -04:00
#pragma once
2019-10-26 12:58:13 -04:00
#include <functional>
#include <frc/Timer.h>
#include <frc/trajectory/TrapezoidProfile.h>
2019-10-26 12:58:13 -04:00
#include "frc2/command/Command.h"
#include "frc2/command/CommandHelper.h"
#include "frc2/command/Requirements.h"
2019-10-26 12:58:13 -04:00
namespace frc2 {
/**
* A command that runs a TrapezoidProfile. Useful for smoothly controlling
* mechanism motion.
*
* This class is provided by the NewCommands VendorDep
*
2019-10-26 12:58:13 -04:00
* @see TrapezoidProfile
*/
template <class Distance>
2019-10-26 12:58:13 -04:00
class TrapezoidProfileCommand
: public CommandHelper<Command, TrapezoidProfileCommand<Distance>> {
using Distance_t = units::unit_t<Distance>;
using Velocity =
units::compound_unit<Distance, units::inverse<units::seconds>>;
using Velocity_t = units::unit_t<Velocity>;
using State = typename frc::TrapezoidProfile<Distance>::State;
2019-10-26 12:58:13 -04:00
public:
/**
* Creates a new TrapezoidProfileCommand that will execute the given
* TrapezoidalProfile. Output will be piped to the provided consumer function.
*
* @param profile The motion profile to execute.
* @param output The consumer for the profile output.
* @param goal The supplier for the desired state
* @param currentState The current state
* @param requirements The list of requirements.
2019-10-26 12:58:13 -04:00
*/
TrapezoidProfileCommand(frc::TrapezoidProfile<Distance> profile,
std::function<void(State)> output,
std::function<State()> goal,
std::function<State()> currentState,
Requirements requirements = {})
: m_profile(profile),
m_output(output),
m_goal(goal),
m_currentState(currentState) {
this->AddRequirements(requirements);
m_newAPI = true;
}
/**
* Creates a new TrapezoidProfileCommand that will execute the given
* TrapezoidalProfile. Output will be piped to the provided consumer function.
*
* @param profile The motion profile to execute.
* @param output The consumer for the profile output.
* @param requirements The list of requirements.
* @deprecated The new constructor allows you to pass in a supplier for
* desired and current state. This allows you to change goals at runtime.
*/
WPI_DEPRECATED(
"The new constructor allows you to pass in a supplier for desired and "
"current state. This allows you to change goals at runtime.")
TrapezoidProfileCommand(frc::TrapezoidProfile<Distance> profile,
std::function<void(State)> output,
Requirements requirements = {})
: m_profile(profile), m_output(output) {
this->AddRequirements(requirements);
m_newAPI = false;
}
2019-10-26 12:58:13 -04:00
2023-01-29 10:21:07 -05:00
void Initialize() override { m_timer.Restart(); }
2019-10-26 12:58:13 -04:00
void Execute() override {
m_output(m_profile.Calculate(m_timer.Get(), m_goal(), m_currentState()));
}
2019-10-26 12:58:13 -04:00
void End(bool interrupted) override { m_timer.Stop(); }
2019-10-26 12:58:13 -04:00
bool IsFinished() override {
return m_timer.HasElapsed(m_profile.TotalTime());
}
2019-10-26 12:58:13 -04:00
private:
frc::TrapezoidProfile<Distance> m_profile;
std::function<void(State)> m_output;
std::function<State()> m_goal;
std::function<State()> m_currentState;
bool m_newAPI; // TODO: Remove
frc::Timer m_timer;
2019-10-26 12:58:13 -04:00
};
} // namespace frc2