WPILibC++ 2024.3.2
TrapezoidProfileCommand.h
Go to the documentation of this file.
1// Copyright (c) FIRST and other WPILib contributors.
2// Open Source Software; you can modify and/or share it under the terms of
3// the WPILib BSD license file in the root directory of this project.
4
5#pragma once
6
7#include <functional>
8
9#include <frc/Timer.h>
11
15
16namespace frc2 {
17/**
18 * A command that runs a TrapezoidProfile. Useful for smoothly controlling
19 * mechanism motion.
20 *
21 * This class is provided by the NewCommands VendorDep
22 *
23 * @see TrapezoidProfile
24 */
25template <class Distance>
27 : public CommandHelper<Command, TrapezoidProfileCommand<Distance>> {
29 using Velocity =
32 using State = typename frc::TrapezoidProfile<Distance>::State;
33
34 public:
35 /**
36 * Creates a new TrapezoidProfileCommand that will execute the given
37 * TrapezoidalProfile. Output will be piped to the provided consumer function.
38 *
39 * @param profile The motion profile to execute.
40 * @param output The consumer for the profile output.
41 * @param goal The supplier for the desired state
42 * @param currentState The current state
43 * @param requirements The list of requirements.
44 */
46 std::function<void(State)> output,
47 std::function<State()> goal,
48 std::function<State()> currentState,
49 Requirements requirements = {})
50 : m_profile(profile),
51 m_output(output),
52 m_goal(goal),
53 m_currentState(currentState) {
54 this->AddRequirements(requirements);
55 m_newAPI = true;
56 }
57
58 /**
59 * Creates a new TrapezoidProfileCommand that will execute the given
60 * TrapezoidalProfile. Output will be piped to the provided consumer function.
61 *
62 * @param profile The motion profile to execute.
63 * @param output The consumer for the profile output.
64 * @param requirements The list of requirements.
65 * @deprecated The new constructor allows you to pass in a supplier for
66 * desired and current state. This allows you to change goals at runtime.
67 */
68 WPI_DEPRECATED(
69 "The new constructor allows you to pass in a supplier for desired and "
70 "current state. This allows you to change goals at runtime.")
71 TrapezoidProfileCommand(frc::TrapezoidProfile<Distance> profile,
72 std::function<void(State)> output,
73 Requirements requirements = {})
74 : m_profile(profile), m_output(output) {
75 this->AddRequirements(requirements);
76 m_newAPI = false;
77 }
78
79 void Initialize() override { m_timer.Restart(); }
80
81 void Execute() override {
82 m_output(m_profile.Calculate(m_timer.Get(), m_currentState(), m_goal()));
83 }
84
85 void End(bool interrupted) override { m_timer.Stop(); }
86
87 bool IsFinished() override {
88 return m_timer.HasElapsed(m_profile.TotalTime());
89 }
90
91 private:
93 std::function<void(State)> m_output;
94 std::function<State()> m_goal;
95 std::function<State()> m_currentState;
96 bool m_newAPI; // TODO: Remove
97 frc::Timer m_timer;
98};
99
100} // namespace frc2
CRTP implementation to allow polymorphic decorator functions in Command.
Definition: CommandHelper.h:27
Represents requirements for a command, which is a set of (pointers to) subsystems.
Definition: Requirements.h:20
A command that runs a TrapezoidProfile.
Definition: TrapezoidProfileCommand.h:27
void Initialize() override
Definition: TrapezoidProfileCommand.h:79
void Execute() override
Definition: TrapezoidProfileCommand.h:81
void End(bool interrupted) override
Definition: TrapezoidProfileCommand.h:85
TrapezoidProfileCommand(frc::TrapezoidProfile< Distance > profile, std::function< void(State)> output, std::function< State()> goal, std::function< State()> currentState, Requirements requirements={})
Creates a new TrapezoidProfileCommand that will execute the given TrapezoidalProfile.
Definition: TrapezoidProfileCommand.h:45
bool IsFinished() override
Definition: TrapezoidProfileCommand.h:87
A timer class.
Definition: Timer.h:36
void Restart()
Restart the timer by stopping the timer, if it is not already stopped, resetting the accumulated time...
units::second_t Get() const
Get the current time from the timer.
bool HasElapsed(units::second_t period) const
Check if the period specified has passed.
void Stop()
Stop the timer.
Profile state.
Definition: TrapezoidProfile.h:90
A trapezoid-shaped velocity profile.
Definition: TrapezoidProfile.h:45
typename units::detail::compound_impl< U, Us... >::type compound_unit
Represents a unit type made up from other units.
Definition: base.h:1434
Definition: TrapezoidProfileSubsystem.h:12
Definition: AprilTagPoseEstimator.h:15
Definition: array.h:89