WPILibC++ 2027.0.0-alpha-5
Loading...
Searching...
No Matches
ArmFeedforward.hpp
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 "wpi/units/angle.hpp"
9#include "wpi/units/angular_velocity.hpp"
10#include "wpi/units/math.hpp"
11#include "wpi/units/voltage.hpp"
14
15namespace wpi::math {
16/**
17 * A helper class that computes feedforward outputs for a simple arm (modeled as
18 * a motor acting against the force of gravity on a beam suspended at an angle).
19 */
21 public:
22 using Angle = wpi::units::radians;
23 using Velocity = wpi::units::radians_per_second;
25 wpi::units::compound_unit<wpi::units::radians_per_second,
26 wpi::units::inverse<wpi::units::second>>;
27 using kv_unit = wpi::units::compound_unit<
28 wpi::units::volts, wpi::units::inverse<wpi::units::radians_per_second>>;
29 using ka_unit = wpi::units::compound_unit<wpi::units::volts,
30 wpi::units::inverse<Acceleration>>;
31
32 /**
33 * Creates a new ArmFeedforward with the specified gains.
34 *
35 * @param kS The static gain, in volts.
36 * @param kG The gravity gain, in volts.
37 * @param kV The velocity gain, in volt seconds per radian.
38 * @param kA The acceleration gain, in volt seconds² per radian.
39 * @param dt The period in seconds.
40 * @throws IllegalArgumentException for kv &lt; zero.
41 * @throws IllegalArgumentException for ka &lt; zero.
42 * @throws IllegalArgumentException for period &le; zero.
43 */
44 constexpr ArmFeedforward(
45 wpi::units::volt_t kS, wpi::units::volt_t kG,
46 wpi::units::unit_t<kv_unit> kV,
47 wpi::units::unit_t<ka_unit> kA = wpi::units::unit_t<ka_unit>(0),
48 wpi::units::second_t dt = 20_ms)
49 : kS(kS), kG(kG), kV(kV), kA(kA), m_dt(dt) {
50 if (kV.value() < 0) {
51 wpi::math::MathSharedStore::ReportError(
52 "kV must be a non-negative number, got {}!", kV.value());
53 this->kV = wpi::units::unit_t<kv_unit>{0};
55 }
56 if (kA.value() < 0) {
57 wpi::math::MathSharedStore::ReportError(
58 "kA must be a non-negative number, got {}!", kA.value());
59 this->kA = wpi::units::unit_t<ka_unit>{0};
61 }
62 if (dt <= 0_ms) {
64 "period must be a positive number, got {}!", dt.value());
65 this->m_dt = 20_ms;
66 wpi::math::MathSharedStore::ReportWarning("period defaulted to 20 ms.");
67 }
68 }
69
70 /**
71 * Calculates the feedforward from the gains and setpoint assuming discrete
72 * control. Use this method when the velocity does not change.
73 *
74 * @param currentAngle The current angle. This angle should be measured from
75 * the horizontal (i.e. if the provided angle is 0, the arm should be parallel
76 * to the floor). If your encoder does not follow this convention, an offset
77 * should be added.
78 * @param currentVelocity The current velocity.
79 * @return The computed feedforward in volts.
80 */
81 constexpr wpi::units::volt_t Calculate(
82 wpi::units::unit_t<Angle> currentAngle,
83 wpi::units::unit_t<Velocity> currentVelocity) const {
84 return kS * wpi::util::sgn(currentVelocity) +
85 kG * wpi::units::math::cos(currentAngle) + kV * currentVelocity;
86 }
87
88 /**
89 * Calculates the feedforward from the gains and setpoints assuming discrete
90 * control.
91 *
92 * @param currentAngle The current angle. This angle should be measured from
93 * the horizontal (i.e. if the provided angle is 0, the arm should be parallel
94 * to the floor). If your encoder does not follow this convention, an offset
95 * should be added.
96 * @param currentVelocity The current velocity.
97 * @param nextVelocity The next velocity.
98 * @return The computed feedforward in volts.
99 */
100 wpi::units::volt_t Calculate(wpi::units::unit_t<Angle> currentAngle,
101 wpi::units::unit_t<Velocity> currentVelocity,
102 wpi::units::unit_t<Velocity> nextVelocity) const;
103
104 // Rearranging the main equation from the calculate() method yields the
105 // formulas for the methods below:
106
107 /**
108 * Calculates the maximum achievable velocity given a maximum voltage supply,
109 * a position, and an acceleration. Useful for ensuring that velocity and
110 * acceleration constraints for a trapezoidal profile are simultaneously
111 * achievable - enter the acceleration constraint, and this will give you
112 * a simultaneously-achievable velocity constraint.
113 *
114 * @param maxVoltage The maximum voltage that can be supplied to the arm.
115 * @param angle The angle of the arm. This angle should be measured
116 * from the horizontal (i.e. if the provided angle is 0,
117 * the arm should be parallel to the floor). If your
118 * encoder does not follow this convention, an offset
119 * should be added.
120 * @param acceleration The acceleration of the arm.
121 * @return The maximum possible velocity at the given acceleration and angle.
122 */
123 constexpr wpi::units::unit_t<Velocity> MaxAchievableVelocity(
124 wpi::units::volt_t maxVoltage, wpi::units::unit_t<Angle> angle,
125 wpi::units::unit_t<Acceleration> acceleration) {
126 // Assume max velocity is positive
127 return (maxVoltage - kS - kG * wpi::units::math::cos(angle) -
128 kA * acceleration) /
129 kV;
130 }
131
132 /**
133 * Calculates the minimum achievable velocity given a maximum voltage supply,
134 * a position, and an acceleration. Useful for ensuring that velocity and
135 * acceleration constraints for a trapezoidal profile are simultaneously
136 * achievable - enter the acceleration constraint, and this will give you
137 * a simultaneously-achievable velocity constraint.
138 *
139 * @param maxVoltage The maximum voltage that can be supplied to the arm.
140 * @param angle The angle of the arm. This angle should be measured
141 * from the horizontal (i.e. if the provided angle is 0,
142 * the arm should be parallel to the floor). If your
143 * encoder does not follow this convention, an offset
144 * should be added.
145 * @param acceleration The acceleration of the arm.
146 * @return The minimum possible velocity at the given acceleration and angle.
147 */
148 constexpr wpi::units::unit_t<Velocity> MinAchievableVelocity(
149 wpi::units::volt_t maxVoltage, wpi::units::unit_t<Angle> angle,
150 wpi::units::unit_t<Acceleration> acceleration) {
151 // Assume min velocity is negative, ks flips sign
152 return (-maxVoltage + kS - kG * wpi::units::math::cos(angle) -
153 kA * acceleration) /
154 kV;
155 }
156
157 /**
158 * Calculates the maximum achievable acceleration given a maximum voltage
159 * supply, a position, and a velocity. Useful for ensuring that velocity and
160 * acceleration constraints for a trapezoidal profile are simultaneously
161 * achievable - enter the velocity constraint, and this will give you
162 * a simultaneously-achievable acceleration constraint.
163 *
164 * @param maxVoltage The maximum voltage that can be supplied to the arm.
165 * @param angle The angle of the arm. This angle should be measured
166 * from the horizontal (i.e. if the provided angle is 0,
167 * the arm should be parallel to the floor). If your
168 * encoder does not follow this convention, an offset
169 * should be added.
170 * @param velocity The velocity of the arm.
171 * @return The maximum possible acceleration at the given velocity and angle.
172 */
173 constexpr wpi::units::unit_t<Acceleration> MaxAchievableAcceleration(
174 wpi::units::volt_t maxVoltage, wpi::units::unit_t<Angle> angle,
175 wpi::units::unit_t<Velocity> velocity) {
176 return (maxVoltage - kS * wpi::util::sgn(velocity) -
177 kG * wpi::units::math::cos(angle) - kV * velocity) /
178 kA;
179 }
180
181 /**
182 * Calculates the minimum achievable acceleration given a maximum voltage
183 * supply, a position, and a velocity. Useful for ensuring that velocity and
184 * acceleration constraints for a trapezoidal profile are simultaneously
185 * achievable - enter the velocity constraint, and this will give you
186 * a simultaneously-achievable acceleration constraint.
187 *
188 * @param maxVoltage The maximum voltage that can be supplied to the arm.
189 * @param angle The angle of the arm. This angle should be measured
190 * from the horizontal (i.e. if the provided angle is 0,
191 * the arm should be parallel to the floor). If your
192 * encoder does not follow this convention, an offset
193 * should be added.
194 * @param velocity The velocity of the arm.
195 * @return The minimum possible acceleration at the given velocity and angle.
196 */
197 constexpr wpi::units::unit_t<Acceleration> MinAchievableAcceleration(
198 wpi::units::volt_t maxVoltage, wpi::units::unit_t<Angle> angle,
199 wpi::units::unit_t<Velocity> velocity) {
200 return MaxAchievableAcceleration(-maxVoltage, angle, velocity);
201 }
202
203 /**
204 * Sets the static gain.
205 *
206 * @param kS The static gain.
207 */
208 constexpr void SetKs(wpi::units::volt_t kS) { this->kS = kS; }
209
210 /**
211 * Sets the gravity gain.
212 *
213 * @param kG The gravity gain.
214 */
215 constexpr void SetKg(wpi::units::volt_t kG) { this->kG = kG; }
216
217 /**
218 * Sets the velocity gain.
219 *
220 * @param kV The velocity gain.
221 */
222 constexpr void SetKv(wpi::units::unit_t<kv_unit> kV) { this->kV = kV; }
223
224 /**
225 * Sets the acceleration gain.
226 *
227 * @param kA The acceleration gain.
228 */
229 constexpr void SetKa(wpi::units::unit_t<ka_unit> kA) { this->kA = kA; }
230
231 /**
232 * Returns the static gain.
233 *
234 * @return The static gain.
235 */
236 constexpr wpi::units::volt_t GetKs() const { return kS; }
237
238 /**
239 * Returns the gravity gain.
240 *
241 * @return The gravity gain.
242 */
243 constexpr wpi::units::volt_t GetKg() const { return kG; }
244
245 /**
246 * Returns the velocity gain.
247 *
248 * @return The velocity gain.
249 */
250 constexpr wpi::units::unit_t<kv_unit> GetKv() const { return kV; }
251
252 /**
253 * Returns the acceleration gain.
254 *
255 * @return The acceleration gain.
256 */
257 constexpr wpi::units::unit_t<ka_unit> GetKa() const { return kA; }
258
259 private:
260 /// The static gain, in volts.
261 wpi::units::volt_t kS;
262
263 /// The gravity gain, in volts.
264 wpi::units::volt_t kG;
265
266 /// The velocity gain, in V/(rad/s)volt seconds per radian.
267 wpi::units::unit_t<kv_unit> kV;
268
269 /// The acceleration gain, in V/(rad/s²).
270 wpi::units::unit_t<ka_unit> kA;
271
272 /** The period. */
273 wpi::units::second_t m_dt;
274};
275} // namespace wpi::math
276
#define WPILIB_DLLEXPORT
Definition SymbolExports.hpp:36
constexpr wpi::units::volt_t GetKg() const
Returns the gravity gain.
Definition ArmFeedforward.hpp:243
constexpr void SetKs(wpi::units::volt_t kS)
Sets the static gain.
Definition ArmFeedforward.hpp:208
constexpr wpi::units::unit_t< Velocity > MaxAchievableVelocity(wpi::units::volt_t maxVoltage, wpi::units::unit_t< Angle > angle, wpi::units::unit_t< Acceleration > acceleration)
Calculates the maximum achievable velocity given a maximum voltage supply, a position,...
Definition ArmFeedforward.hpp:123
wpi::units::radians_per_second Velocity
Definition ArmFeedforward.hpp:23
constexpr wpi::units::unit_t< ka_unit > GetKa() const
Returns the acceleration gain.
Definition ArmFeedforward.hpp:257
constexpr wpi::units::unit_t< Acceleration > MinAchievableAcceleration(wpi::units::volt_t maxVoltage, wpi::units::unit_t< Angle > angle, wpi::units::unit_t< Velocity > velocity)
Calculates the minimum achievable acceleration given a maximum voltage supply, a position,...
Definition ArmFeedforward.hpp:197
constexpr wpi::units::unit_t< Acceleration > MaxAchievableAcceleration(wpi::units::volt_t maxVoltage, wpi::units::unit_t< Angle > angle, wpi::units::unit_t< Velocity > velocity)
Calculates the maximum achievable acceleration given a maximum voltage supply, a position,...
Definition ArmFeedforward.hpp:173
constexpr wpi::units::volt_t Calculate(wpi::units::unit_t< Angle > currentAngle, wpi::units::unit_t< Velocity > currentVelocity) const
Calculates the feedforward from the gains and setpoint assuming discrete control.
Definition ArmFeedforward.hpp:81
constexpr wpi::units::volt_t GetKs() const
Returns the static gain.
Definition ArmFeedforward.hpp:236
wpi::units::radians Angle
Definition ArmFeedforward.hpp:22
constexpr void SetKa(wpi::units::unit_t< ka_unit > kA)
Sets the acceleration gain.
Definition ArmFeedforward.hpp:229
constexpr wpi::units::unit_t< Velocity > MinAchievableVelocity(wpi::units::volt_t maxVoltage, wpi::units::unit_t< Angle > angle, wpi::units::unit_t< Acceleration > acceleration)
Calculates the minimum achievable velocity given a maximum voltage supply, a position,...
Definition ArmFeedforward.hpp:148
constexpr wpi::units::unit_t< kv_unit > GetKv() const
Returns the velocity gain.
Definition ArmFeedforward.hpp:250
wpi::units::compound_unit< wpi::units::volts, wpi::units::inverse< wpi::units::radians_per_second > > kv_unit
Definition ArmFeedforward.hpp:27
constexpr ArmFeedforward(wpi::units::volt_t kS, wpi::units::volt_t kG, wpi::units::unit_t< kv_unit > kV, wpi::units::unit_t< ka_unit > kA=wpi::units::unit_t< ka_unit >(0), wpi::units::second_t dt=20_ms)
Creates a new ArmFeedforward with the specified gains.
Definition ArmFeedforward.hpp:44
wpi::units::volt_t Calculate(wpi::units::unit_t< Angle > currentAngle, wpi::units::unit_t< Velocity > currentVelocity, wpi::units::unit_t< Velocity > nextVelocity) const
Calculates the feedforward from the gains and setpoints assuming discrete control.
wpi::units::compound_unit< wpi::units::volts, wpi::units::inverse< Acceleration > > ka_unit
Definition ArmFeedforward.hpp:29
constexpr void SetKg(wpi::units::volt_t kG)
Sets the gravity gain.
Definition ArmFeedforward.hpp:215
constexpr void SetKv(wpi::units::unit_t< kv_unit > kV)
Sets the velocity gain.
Definition ArmFeedforward.hpp:222
wpi::units::compound_unit< wpi::units::radians_per_second, wpi::units::inverse< wpi::units::second > > Acceleration
Definition ArmFeedforward.hpp:24
static void ReportError(const S &format, Args &&... args)
Definition MathShared.hpp:48
static void ReportWarning(const S &format, Args &&... args)
Definition MathShared.hpp:57
Definition LinearSystem.hpp:20
constexpr int sgn(T val)
Definition MathExtras.hpp:766