WPILibC++  2020.3.2
TrapezoidProfileCommand.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2019-2020 FIRST. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #pragma once
9 
10 #include <functional>
11 #include <initializer_list>
12 
13 #include <frc/trajectory/TrapezoidProfile.h>
14 #include <wpi/ArrayRef.h>
15 
16 #include "frc2/Timer.h"
17 #include "frc2/command/CommandBase.h"
18 #include "frc2/command/CommandHelper.h"
19 
20 namespace frc2 {
27 template <class Distance>
29  : public CommandHelper<CommandBase, TrapezoidProfileCommand<Distance>> {
30  using Distance_t = units::unit_t<Distance>;
31  using Velocity =
32  units::compound_unit<Distance, units::inverse<units::seconds>>;
33  using Velocity_t = units::unit_t<Velocity>;
34  using State = typename frc::TrapezoidProfile<Distance>::State;
35 
36  public:
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 
59  std::function<void(State)> output,
60  wpi::ArrayRef<Subsystem*> requirements = {})
61  : m_profile(profile), m_output(output) {
62  this->AddRequirements(requirements);
63  }
64 
65  void Initialize() override {
66  m_timer.Reset();
67  m_timer.Start();
68  }
69 
70  void Execute() override { m_output(m_profile.Calculate(m_timer.Get())); }
71 
72  void End(bool interrupted) override { m_timer.Stop(); }
73 
74  bool IsFinished() override {
75  return m_timer.HasElapsed(m_profile.TotalTime());
76  }
77 
78  private:
80  std::function<void(State)> m_output;
81 
82  Timer m_timer;
83 };
84 
85 } // namespace frc2
frc2::Timer::Get
units::second_t Get() const
Get the current time from the timer.
frc2::Timer::Start
void Start()
Start the timer running.
wpi::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:42
frc2::TrapezoidProfileCommand::Execute
void Execute() override
The main body of a command.
Definition: TrapezoidProfileCommand.h:70
frc::TrapezoidProfile
A trapezoid-shaped velocity profile.
Definition: TrapezoidProfile.h:45
frc2::Timer::Stop
void Stop()
Stop the timer.
frc2::Timer
A wrapper for the frc::Timer class that returns unit-typed values.
Definition: Timer.h:40
frc2::TrapezoidProfileCommand::Initialize
void Initialize() override
The initial subroutine of a command.
Definition: TrapezoidProfileCommand.h:65
frc2::TrapezoidProfileCommand::TrapezoidProfileCommand
TrapezoidProfileCommand(frc::TrapezoidProfile< Distance > profile, std::function< void(State)> output, wpi::ArrayRef< Subsystem * > requirements={})
Creates a new TrapezoidProfileCommand that will execute the given TrapezoidalProfile.
Definition: TrapezoidProfileCommand.h:58
frc2::Timer::Reset
void Reset()
Reset the timer by setting the time to 0.
frc2::TrapezoidProfileCommand::End
void End(bool interrupted) override
The action to take when the command ends.
Definition: TrapezoidProfileCommand.h:72
frc2::TrapezoidProfileCommand
A command that runs a TrapezoidProfile.
Definition: TrapezoidProfileCommand.h:28
frc2::CommandBase::AddRequirements
void AddRequirements(std::initializer_list< Subsystem * > requirements)
Adds the specified requirements to the command.
frc2::CommandHelper
CRTP implementation to allow polymorphic decorator functions in Command.
Definition: CommandHelper.h:26
frc2::TrapezoidProfileCommand::IsFinished
bool IsFinished() override
Whether the command has finished.
Definition: TrapezoidProfileCommand.h:74
frc::TrapezoidProfile::State
Definition: TrapezoidProfile.h:68
frc2::TrapezoidProfileCommand::TrapezoidProfileCommand
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
frc2::Timer::HasElapsed
bool HasElapsed(units::second_t period)
Check if the period specified has passed.