WPILibC++ 2023.4.3
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#include <initializer_list>
9#include <span>
10
11#include <frc/Timer.h>
13
16
17namespace frc2 {
18/**
19 * A command that runs a TrapezoidProfile. Useful for smoothly controlling
20 * mechanism motion.
21 *
22 * This class is provided by the NewCommands VendorDep
23 *
24 * @see TrapezoidProfile
25 */
26template <class Distance>
28 : public CommandHelper<CommandBase, TrapezoidProfileCommand<Distance>> {
30 using Velocity =
33 using State = typename frc::TrapezoidProfile<Distance>::State;
34
35 public:
36 /**
37 * Creates a new TrapezoidProfileCommand that will execute the given
38 * TrapezoidalProfile. Output will be piped to the provided consumer function.
39 *
40 * @param profile The motion profile to execute.
41 * @param output The consumer for the profile output.
42 * @param requirements The list of requirements.
43 */
45 std::function<void(State)> output,
46 std::initializer_list<Subsystem*> requirements)
47 : m_profile(profile), m_output(output) {
48 this->AddRequirements(requirements);
49 }
50
51 /**
52 * Creates a new TrapezoidProfileCommand that will execute the given
53 * TrapezoidalProfile. Output will be piped to the provided consumer function.
54 *
55 * @param profile The motion profile to execute.
56 * @param output The consumer for the profile output.
57 * @param requirements The list of requirements.
58 */
60 std::function<void(State)> output,
61 std::span<Subsystem* const> requirements = {})
62 : m_profile(profile), m_output(output) {
63 this->AddRequirements(requirements);
64 }
65
66 void Initialize() override { m_timer.Restart(); }
67
68 void Execute() override { m_output(m_profile.Calculate(m_timer.Get())); }
69
70 void End(bool interrupted) override { m_timer.Stop(); }
71
72 bool IsFinished() override {
73 return m_timer.HasElapsed(m_profile.TotalTime());
74 }
75
76 private:
78 std::function<void(State)> m_output;
79
80 frc::Timer m_timer;
81};
82
83} // namespace frc2
void AddRequirements(std::initializer_list< Subsystem * > requirements)
Adds the specified Subsystem requirements to the command.
CRTP implementation to allow polymorphic decorator functions in Command.
Definition: CommandHelper.h:26
A command that runs a TrapezoidProfile.
Definition: TrapezoidProfileCommand.h:28
void Initialize() override
The initial subroutine of a command.
Definition: TrapezoidProfileCommand.h:66
TrapezoidProfileCommand(frc::TrapezoidProfile< Distance > profile, std::function< void(State)> output, std::initializer_list< Subsystem * > requirements)
Creates a new TrapezoidProfileCommand that will execute the given TrapezoidalProfile.
Definition: TrapezoidProfileCommand.h:44
TrapezoidProfileCommand(frc::TrapezoidProfile< Distance > profile, std::function< void(State)> output, std::span< Subsystem *const > requirements={})
Creates a new TrapezoidProfileCommand that will execute the given TrapezoidalProfile.
Definition: TrapezoidProfileCommand.h:59
void Execute() override
The main body of a command.
Definition: TrapezoidProfileCommand.h:68
void End(bool interrupted) override
The action to take when the command ends.
Definition: TrapezoidProfileCommand.h:70
bool IsFinished() override
Whether the command has finished.
Definition: TrapezoidProfileCommand.h:72
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.
Definition: TrapezoidProfile.h:67
A trapezoid-shaped velocity profile.
Definition: TrapezoidProfile.h:42
typename units::detail::compound_impl< U, Us... >::type compound_unit
Represents a unit type made up from other units.
Definition: base.h:1434
Definition: InstantCommand.h:14