WPILibC++ 2023.4.3
ElevatorFeedforward.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 <wpi/MathExtras.h>
8
9#include "units/length.h"
10#include "units/time.h"
11#include "units/voltage.h"
12
13namespace frc {
14/**
15 * A helper class that computes feedforward outputs for a simple elevator
16 * (modeled as a motor acting against the force of gravity).
17 */
19 public:
20 using Distance = units::meters;
21 using Velocity =
26 using ka_unit =
28
30
31 /**
32 * Creates a new ElevatorFeedforward with the specified gains.
33 *
34 * @param kS The static gain, in volts.
35 * @param kG The gravity gain, in volts.
36 * @param kV The velocity gain, in volt seconds per distance.
37 * @param kA The acceleration gain, in volt secondsĀ² per distance.
38 */
40 units::volt_t kS, units::volt_t kG, units::unit_t<kv_unit> kV,
42 : kS(kS), kG(kG), kV(kV), kA(kA) {}
43
44 /**
45 * Calculates the feedforward from the gains and setpoints.
46 *
47 * @param velocity The velocity setpoint, in distance per second.
48 * @param acceleration The acceleration setpoint, in distance per secondĀ².
49 * @return The computed feedforward, in volts.
50 */
51 constexpr units::volt_t Calculate(units::unit_t<Velocity> velocity,
52 units::unit_t<Acceleration> acceleration =
54 return kS * wpi::sgn(velocity) + kG + kV * velocity + kA * acceleration;
55 }
56
57 // Rearranging the main equation from the calculate() method yields the
58 // formulas for the methods below:
59
60 /**
61 * Calculates the maximum achievable velocity given a maximum voltage supply
62 * and an acceleration. Useful for ensuring that velocity and
63 * acceleration constraints for a trapezoidal profile are simultaneously
64 * achievable - enter the acceleration constraint, and this will give you
65 * a simultaneously-achievable velocity constraint.
66 *
67 * @param maxVoltage The maximum voltage that can be supplied to the elevator.
68 * @param acceleration The acceleration of the elevator.
69 * @return The maximum possible velocity at the given acceleration.
70 */
72 units::volt_t maxVoltage, units::unit_t<Acceleration> acceleration) {
73 // Assume max velocity is positive
74 return (maxVoltage - kS - kG - kA * acceleration) / kV;
75 }
76
77 /**
78 * Calculates the minimum achievable velocity given a maximum voltage supply
79 * and an acceleration. Useful for ensuring that velocity and
80 * acceleration constraints for a trapezoidal profile are simultaneously
81 * achievable - enter the acceleration constraint, and this will give you
82 * a simultaneously-achievable velocity constraint.
83 *
84 * @param maxVoltage The maximum voltage that can be supplied to the elevator.
85 * @param acceleration The acceleration of the elevator.
86 * @return The minimum possible velocity at the given acceleration.
87 */
89 units::volt_t maxVoltage, units::unit_t<Acceleration> acceleration) {
90 // Assume min velocity is negative, ks flips sign
91 return (-maxVoltage + kS - kG - kA * acceleration) / kV;
92 }
93
94 /**
95 * Calculates the maximum achievable acceleration given a maximum voltage
96 * supply and a velocity. Useful for ensuring that velocity and
97 * acceleration constraints for a trapezoidal profile are simultaneously
98 * achievable - enter the velocity constraint, and this will give you
99 * a simultaneously-achievable acceleration constraint.
100 *
101 * @param maxVoltage The maximum voltage that can be supplied to the elevator.
102 * @param velocity The velocity of the elevator.
103 * @return The maximum possible acceleration at the given velocity.
104 */
106 units::volt_t maxVoltage, units::unit_t<Velocity> velocity) {
107 return (maxVoltage - kS * wpi::sgn(velocity) - kG - kV * velocity) / kA;
108 }
109
110 /**
111 * Calculates the minimum achievable acceleration given a maximum voltage
112 * supply and a velocity. Useful for ensuring that velocity and
113 * acceleration constraints for a trapezoidal profile are simultaneously
114 * achievable - enter the velocity constraint, and this will give you
115 * a simultaneously-achievable acceleration constraint.
116 *
117 * @param maxVoltage The maximum voltage that can be supplied to the elevator.
118 * @param velocity The velocity of the elevator.
119 * @return The minimum possible acceleration at the given velocity.
120 */
122 units::volt_t maxVoltage, units::unit_t<Velocity> velocity) {
123 return MaxAchievableAcceleration(-maxVoltage, velocity);
124 }
125
126 units::volt_t kS{0};
127 units::volt_t kG{0};
130};
131} // namespace frc
A helper class that computes feedforward outputs for a simple elevator (modeled as a motor acting aga...
Definition: ElevatorFeedforward.h:18
constexpr units::unit_t< Velocity > MinAchievableVelocity(units::volt_t maxVoltage, units::unit_t< Acceleration > acceleration)
Calculates the minimum achievable velocity given a maximum voltage supply and an acceleration.
Definition: ElevatorFeedforward.h:88
units::unit_t< kv_unit > kV
Definition: ElevatorFeedforward.h:128
units::meters Distance
Definition: ElevatorFeedforward.h:20
constexpr units::unit_t< Acceleration > MaxAchievableAcceleration(units::volt_t maxVoltage, units::unit_t< Velocity > velocity)
Calculates the maximum achievable acceleration given a maximum voltage supply and a velocity.
Definition: ElevatorFeedforward.h:105
units::volt_t kS
Definition: ElevatorFeedforward.h:126
constexpr ElevatorFeedforward(units::volt_t kS, units::volt_t kG, units::unit_t< kv_unit > kV, units::unit_t< ka_unit > kA=units::unit_t< ka_unit >(0))
Creates a new ElevatorFeedforward with the specified gains.
Definition: ElevatorFeedforward.h:39
units::compound_unit< Distance, units::inverse< units::seconds > > Velocity
Definition: ElevatorFeedforward.h:22
units::volt_t kG
Definition: ElevatorFeedforward.h:127
constexpr units::unit_t< Velocity > MaxAchievableVelocity(units::volt_t maxVoltage, units::unit_t< Acceleration > acceleration)
Calculates the maximum achievable velocity given a maximum voltage supply and an acceleration.
Definition: ElevatorFeedforward.h:71
constexpr units::unit_t< Acceleration > MinAchievableAcceleration(units::volt_t maxVoltage, units::unit_t< Velocity > velocity)
Calculates the minimum achievable acceleration given a maximum voltage supply and a velocity.
Definition: ElevatorFeedforward.h:121
units::unit_t< ka_unit > kA
Definition: ElevatorFeedforward.h:129
constexpr units::volt_t Calculate(units::unit_t< Velocity > velocity, units::unit_t< Acceleration > acceleration=units::unit_t< Acceleration >(0))
Calculates the feedforward from the gains and setpoints.
Definition: ElevatorFeedforward.h:51
units::compound_unit< units::volts, units::inverse< Velocity > > kv_unit
Definition: ElevatorFeedforward.h:25
units::compound_unit< Velocity, units::inverse< units::seconds > > Acceleration
Definition: ElevatorFeedforward.h:24
units::compound_unit< units::volts, units::inverse< Acceleration > > ka_unit
Definition: ElevatorFeedforward.h:27
typename units::detail::compound_impl< U, Us... >::type compound_unit
Represents a unit type made up from other units.
Definition: base.h:1434
Definition: AprilTagFieldLayout.h:22
constexpr int sgn(T val)
Definition: MathExtras.h:936