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-10-26 12:58:13 -04:00
|
|
|
|
2019-11-05 20:52:49 -08:00
|
|
|
#pragma once
|
2019-10-26 12:58:13 -04:00
|
|
|
|
|
|
|
|
#include <functional>
|
2019-11-05 20:52:49 -08:00
|
|
|
#include <initializer_list>
|
2022-10-15 16:33:14 -07:00
|
|
|
#include <span>
|
2019-10-26 12:58:13 -04:00
|
|
|
|
2021-05-28 22:06:59 -07:00
|
|
|
#include <frc/Timer.h>
|
2019-11-05 20:52:49 -08:00
|
|
|
#include <frc/trajectory/TrapezoidProfile.h>
|
2019-10-26 12:58:13 -04:00
|
|
|
|
2023-07-14 01:12:01 -04:00
|
|
|
#include "frc2/command/Command.h"
|
2019-11-05 20:52:49 -08:00
|
|
|
#include "frc2/command/CommandHelper.h"
|
2019-10-26 12:58:13 -04:00
|
|
|
|
|
|
|
|
namespace frc2 {
|
|
|
|
|
/**
|
|
|
|
|
* A command that runs a TrapezoidProfile. Useful for smoothly controlling
|
|
|
|
|
* mechanism motion.
|
|
|
|
|
*
|
2022-01-08 11:11:34 -08:00
|
|
|
* This class is provided by the NewCommands VendorDep
|
|
|
|
|
*
|
2019-10-26 12:58:13 -04:00
|
|
|
* @see TrapezoidProfile
|
|
|
|
|
*/
|
2019-11-20 23:11:46 -05:00
|
|
|
template <class Distance>
|
2019-10-26 12:58:13 -04:00
|
|
|
class TrapezoidProfileCommand
|
2023-07-14 01:12:01 -04:00
|
|
|
: public CommandHelper<Command, TrapezoidProfileCommand<Distance>> {
|
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;
|
2019-11-20 22:44:18 -08:00
|
|
|
|
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.
|
|
|
|
|
*
|
2021-10-14 18:09:38 -07:00
|
|
|
* @param profile The motion profile to execute.
|
|
|
|
|
* @param output The consumer for the profile output.
|
2023-07-19 20:25:10 -04:00
|
|
|
* @param goal The supplier for the desired state
|
|
|
|
|
* @param currentState The current state
|
2021-10-14 18:09:38 -07:00
|
|
|
* @param requirements The list of requirements.
|
2019-10-26 12:58:13 -04:00
|
|
|
*/
|
2023-07-19 20:25:10 -04:00
|
|
|
TrapezoidProfileCommand(frc::TrapezoidProfile<Distance> profile,
|
|
|
|
|
std::function<void(State)> output,
|
|
|
|
|
std::function<State()> goal,
|
|
|
|
|
std::function<State()> currentState,
|
2023-09-17 20:48:39 -07:00
|
|
|
Requirements requirements = {})
|
2023-07-19 20:25:10 -04:00
|
|
|
: m_profile(profile),
|
|
|
|
|
m_output(output),
|
|
|
|
|
m_goal(goal),
|
|
|
|
|
m_currentState(currentState) {
|
|
|
|
|
this->AddRequirements(requirements);
|
|
|
|
|
m_newAPI = true;
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-01 20:09:17 -08:00
|
|
|
/**
|
|
|
|
|
* Creates a new TrapezoidProfileCommand that will execute the given
|
|
|
|
|
* TrapezoidalProfile. Output will be piped to the provided consumer function.
|
|
|
|
|
*
|
2021-10-14 18:09:38 -07:00
|
|
|
* @param profile The motion profile to execute.
|
|
|
|
|
* @param output The consumer for the profile output.
|
|
|
|
|
* @param requirements The list of requirements.
|
2023-07-19 20:25:10 -04:00
|
|
|
* @deprecated The new constructor allows you to pass in a supplier for
|
|
|
|
|
* desired and current state. This allows you to change goals at runtime.
|
2020-01-01 20:09:17 -08:00
|
|
|
*/
|
2023-07-19 20:25:10 -04:00
|
|
|
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.")
|
2020-01-01 20:09:17 -08:00
|
|
|
TrapezoidProfileCommand(frc::TrapezoidProfile<Distance> profile,
|
|
|
|
|
std::function<void(State)> output,
|
2023-09-17 20:48:39 -07:00
|
|
|
Requirements requirements = {})
|
2019-11-20 23:11:46 -05:00
|
|
|
: m_profile(profile), m_output(output) {
|
2019-12-04 23:40:37 -05:00
|
|
|
this->AddRequirements(requirements);
|
2023-07-19 20:25:10 -04:00
|
|
|
m_newAPI = false;
|
2019-11-20 23:11:46 -05:00
|
|
|
}
|
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
|
|
|
|
2023-07-19 20:25:10 -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
|
|
|
|
2019-11-20 23:11:46 -05:00
|
|
|
void End(bool interrupted) override { m_timer.Stop(); }
|
2019-10-26 12:58:13 -04:00
|
|
|
|
2019-11-20 23:11:46 -05:00
|
|
|
bool IsFinished() override {
|
2020-02-08 13:23:29 -05:00
|
|
|
return m_timer.HasElapsed(m_profile.TotalTime());
|
2019-11-20 23:11:46 -05:00
|
|
|
}
|
2019-10-26 12:58:13 -04:00
|
|
|
|
|
|
|
|
private:
|
2019-11-20 23:11:46 -05:00
|
|
|
frc::TrapezoidProfile<Distance> m_profile;
|
|
|
|
|
std::function<void(State)> m_output;
|
2023-07-19 20:25:10 -04:00
|
|
|
std::function<State()> m_goal;
|
|
|
|
|
std::function<State()> m_currentState;
|
|
|
|
|
bool m_newAPI; // TODO: Remove
|
2021-05-28 22:06:59 -07:00
|
|
|
frc::Timer m_timer;
|
2019-10-26 12:58:13 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace frc2
|