WPILibC++ 2025.2.1
Loading...
Searching...
No Matches
TrapezoidProfileSubsystem.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
8#include <units/time.h>
9
11
12namespace frc2 {
13/**
14 * A subsystem that generates and runs trapezoidal motion profiles
15 * automatically. The user specifies how to use the current state of the motion
16 * profile by overriding the `UseState` method.
17 *
18 * This class is provided by the NewCommands VendorDep
19 * @deprecated Use a TrapezoidProfile instead
20 */
21template <class Distance>
22class [[deprecated("Use a TrapezoidProfile instead")]] TrapezoidProfileSubsystem
23 : public SubsystemBase {
25 using Velocity =
28 using State = typename frc::TrapezoidProfile<Distance>::State;
29 using Constraints = typename frc::TrapezoidProfile<Distance>::Constraints;
30
31 public:
32 /**
33 * Creates a new TrapezoidProfileSubsystem.
34 *
35 * @param constraints The constraints (maximum velocity and acceleration)
36 * for the profiles.
37 * @param initialPosition The initial position of the controller mechanism
38 * when the subsystem is constructed.
39 * @param period The period of the main robot loop, in seconds.
40 */
41 explicit TrapezoidProfileSubsystem(Constraints constraints,
42 Distance_t initialPosition = Distance_t{0},
43 units::second_t period = 20_ms)
44 : m_profile(constraints),
45 m_state{initialPosition, Velocity_t(0)},
46 m_goal{initialPosition, Velocity_t{0}},
47 m_period(period) {}
48
49 void Periodic() override {
50 m_state = m_profile.Calculate(m_period, m_state, m_goal);
51 if (m_enabled) {
52 UseState(m_state);
53 }
54 }
55
56 /**
57 * Sets the goal state for the subsystem.
58 *
59 * @param goal The goal state for the subsystem's motion profile.
60 */
61 void SetGoal(State goal) { m_goal = goal; }
62
63 /**
64 * Sets the goal state for the subsystem. Goal velocity assumed to be zero.
65 *
66 * @param goal The goal position for the subsystem's motion profile.
67 */
68 void SetGoal(Distance_t goal) { m_goal = State{goal, Velocity_t(0)}; }
69
70 protected:
71 /**
72 * Users should override this to consume the current state of the motion
73 * profile.
74 *
75 * @param state The current state of the motion profile.
76 */
77 virtual void UseState(State state) = 0;
78
79 /**
80 * Enable the TrapezoidProfileSubsystem's output.
81 */
82 void Enable() { m_enabled = true; }
83
84 /**
85 * Disable the TrapezoidProfileSubsystem's output.
86 */
87 void Disable() { m_enabled = false; }
88
89 private:
91 State m_state;
92 State m_goal;
93 units::second_t m_period;
94 bool m_enabled{false};
95};
96} // namespace frc2
A base for subsystems that handles registration in the constructor, and provides a more intuitive met...
Definition SubsystemBase.h:24
A subsystem that generates and runs trapezoidal motion profiles automatically.
Definition TrapezoidProfileSubsystem.h:23
void SetGoal(State goal)
Sets the goal state for the subsystem.
Definition TrapezoidProfileSubsystem.h:61
void Disable()
Disable the TrapezoidProfileSubsystem's output.
Definition TrapezoidProfileSubsystem.h:87
void SetGoal(Distance_t goal)
Sets the goal state for the subsystem.
Definition TrapezoidProfileSubsystem.h:68
TrapezoidProfileSubsystem(Constraints constraints, Distance_t initialPosition=Distance_t{0}, units::second_t period=20_ms)
Creates a new TrapezoidProfileSubsystem.
Definition TrapezoidProfileSubsystem.h:41
void Periodic() override
This method is called periodically by the CommandScheduler.
Definition TrapezoidProfileSubsystem.h:49
void Enable()
Enable the TrapezoidProfileSubsystem's output.
Definition TrapezoidProfileSubsystem.h:82
virtual void UseState(State state)=0
Users should override this to consume the current state of the motion profile.
Profile constraints.
Definition TrapezoidProfile.h:59
Profile state.
Definition TrapezoidProfile.h:96
A trapezoid-shaped velocity profile.
Definition TrapezoidProfile.h:46
typename units::detail::compound_impl< U, Us... >::type compound_unit
Represents a unit type made up from other units.
Definition base.h:1438
Definition FunctionalCommand.h:13