Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wpimath] fully discretized ElevatorFF and ArmFF #7024

Open
wants to merge 63 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
22c63dd
discretized calculate overloads in Elevator FF
narmstro2020 Sep 2, 2024
8df1e01
initial simple and elevator cleanup/disc
narmstro2020 Sep 2, 2024
0c9df14
added mutable measure for calculated output
narmstro2020 Sep 2, 2024
b47b679
fix to arm structs
narmstro2020 Sep 2, 2024
6cb02a9
SimpleMotorFF C++ first draft
narmstro2020 Sep 3, 2024
3e6a303
formatting fixes
narmstro2020 Sep 3, 2024
8232f6d
ElevatorFF C++ first draft
narmstro2020 Sep 3, 2024
8c8eb58
minor comment out fix
narmstro2020 Sep 3, 2024
d7ba075
Formatting fixes
github-actions[bot] Sep 3, 2024
f7c33d7
Arm first draft C++
narmstro2020 Sep 3, 2024
782e335
Formatting fixes
github-actions[bot] Sep 3, 2024
4e0d048
removed deprecated tests
narmstro2020 Sep 3, 2024
162e977
fixed armFFTest
narmstro2020 Sep 3, 2024
b043a2e
more fixes to ArmFFTests
narmstro2020 Sep 3, 2024
6831552
modification to controller.proto
narmstro2020 Sep 3, 2024
6adb65c
revert controller.java
narmstro2020 Sep 3, 2024
07dc5e2
tiny formatting issue
narmstro2020 Sep 3, 2024
7dfd21a
Regenerate pregenerated files
github-actions[bot] Sep 3, 2024
aee2ac7
fixed some more documentaiton
narmstro2020 Sep 3, 2024
8467501
Merge branch 'FFCalculates' of https://github.com/narmstro2020/allwpi…
narmstro2020 Sep 3, 2024
e5c321a
fixed a doc issue
narmstro2020 Sep 3, 2024
157ee4b
fixed method for arm
narmstro2020 Sep 3, 2024
78bcbf0
empty commit
narmstro2020 Sep 3, 2024
fe3743b
revert ArmFeedforward method back
narmstro2020 Sep 4, 2024
57648cd
Formatting fixes
github-actions[bot] Sep 4, 2024
053af7f
doc fix
narmstro2020 Sep 4, 2024
6966028
fixed recursive mistake
narmstro2020 Sep 4, 2024
f5a9b29
use continuous equation for single velocity overload
narmstro2020 Sep 4, 2024
219a976
Formatting fixes
github-actions[bot] Sep 4, 2024
87cab16
Merge branch 'main' into FFCalculates
narmstro2020 Sep 7, 2024
5056bd0
fixed based of new Units API
narmstro2020 Sep 7, 2024
51cf78e
unwrapped constant in Frisbeebot example
narmstro2020 Sep 13, 2024
fedb57d
empty
narmstro2020 Sep 13, 2024
a35f8e4
java doc cleanup for Arm
narmstro2020 Sep 13, 2024
faae22a
removal of duplicate code in ArmFeedforward.cpp
narmstro2020 Sep 13, 2024
23627fb
doc fix and duplication fix
narmstro2020 Sep 13, 2024
bab93d6
variable fix
narmstro2020 Sep 13, 2024
0529b7d
Formatting fixes
github-actions[bot] Sep 13, 2024
5043a23
Update wpilibcExamples/src/main/cpp/examples/SysId/include/Constants.h
narmstro2020 Sep 13, 2024
edb85cc
Update wpilibcExamples/src/main/cpp/examples/RapidReactCommandBot/inc…
narmstro2020 Sep 13, 2024
a71696f
Update wpimath/src/main/native/include/frc/controller/ElevatorFeedfor…
narmstro2020 Sep 13, 2024
dda16c5
Update wpimath/src/main/java/edu/wpi/first/math/controller/ElevatorFe…
narmstro2020 Sep 13, 2024
8ce86ee
Update wpimath/src/main/native/include/frc/controller/ArmFeedforward.h
narmstro2020 Sep 13, 2024
8e74de6
fix variable reassignment
narmstro2020 Sep 13, 2024
0075619
Formatting fixes
github-actions[bot] Sep 13, 2024
48d2ea8
fixed comment formatting
narmstro2020 Sep 13, 2024
4a3f773
comment formatting fix.
narmstro2020 Sep 13, 2024
6e8b2b7
another fix to comments.
narmstro2020 Sep 13, 2024
d75021b
more formatting fixes.
narmstro2020 Sep 13, 2024
6e81ff7
added 1 space to math indents
narmstro2020 Sep 13, 2024
e76cd03
Formatting fixes
github-actions[bot] Sep 13, 2024
972882b
put comment back in
narmstro2020 Sep 13, 2024
da3cce7
rearrangement
narmstro2020 Sep 13, 2024
d3360f1
Formatting fixes
github-actions[bot] Sep 13, 2024
abf3d60
Update wpimath/src/main/java/edu/wpi/first/math/controller/ElevatorFe…
narmstro2020 Sep 13, 2024
ff91176
Update wpimath/src/main/java/edu/wpi/first/math/controller/ArmFeedfor…
narmstro2020 Sep 13, 2024
097ea4b
cleaning up math derivation comments
narmstro2020 Sep 14, 2024
bc9ddf4
Formatting fixes
github-actions[bot] Sep 14, 2024
9692d52
fix missing periods
narmstro2020 Sep 14, 2024
89f1c71
Merge branch 'FFCalculates' of https://github.com/narmstro2020/allwpi…
narmstro2020 Sep 14, 2024
a726b8b
derivation moved to algorithms.md
narmstro2020 Sep 14, 2024
52443ac
Formatting fixes
github-actions[bot] Sep 14, 2024
50b3cb7
Merge branch 'wpilibsuite:main' into FFCalculates
narmstro2020 Sep 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -188,72 +188,79 @@ public Voltage calculate(LinearVelocity currentVelocity) {
*/
public Voltage calculate(LinearVelocity currentVelocity, LinearVelocity nextVelocity) {
// For an elevator with the model
// dx/dt = −kᵥ/kₐ x + 1/kₐ u - kg/kₐ - kₛ/kₐ sgn(x),
//

// dx/dt = −kᵥ/kₐ x + 1/kₐ u - kg/kₐ - kₛ/kₐ sgn(x),

// where
narmstro2020 marked this conversation as resolved.
Show resolved Hide resolved
// A = −kᵥ/kₐ
// B = 1/kₐ
// c = -(kg/kₐ + kₛ/kₐ sgn(x))
// A_d = eᴬᵀ
// B_d = A⁻¹(eᴬᵀ - I)B
// dx/dt = Ax + Bu + c
//

// A = −kᵥ/kₐ
// B = 1/kₐ
// c = -(kg/kₐ + kₛ/kₐ sgn(x))
// A_d = eᴬᵀ
// B_d = A⁻¹(eᴬᵀ - I)B
// dx/dt = Ax + Bu + c

// Discretize the affine model.
// dx/dt = Ax + Bu + c
// dx/dt = Ax + B(u + B⁺c)
// xₖ₊₁ = eᴬᵀxₖ + A⁻¹(eᴬᵀ - I)B(uₖ + B⁺cₖ)
// xₖ₊₁ = A_d xₖ + B_d (uₖ + B⁺cₖ)
// xₖ₊₁ = A_d xₖ + B_d uₖ + B_d B⁺cₖ
//

// dx/dt = Ax + Bu + c
narmstro2020 marked this conversation as resolved.
Show resolved Hide resolved
// dx/dt = Ax + B(u + B⁺c)
// xₖ₊₁ = eᴬᵀxₖ + A⁻¹(eᴬᵀ - I)B(uₖ + B⁺cₖ)
// xₖ₊₁ = A_d xₖ + B_d (uₖ + B⁺cₖ)
// xₖ₊₁ = A_d xₖ + B_d uₖ + B_d B⁺cₖ

// Solve for uₖ.
// B_d uₖ = xₖ₊₁ − A_d xₖ − B_d B⁺cₖ
// uₖ = B_d⁺(xₖ₊₁ − A_d xₖ − B_d B⁺cₖ)
// uₖ = B_d⁺(xₖ₊₁ − A_d xₖ) − B⁺cₖ
//

// B_d uₖ = xₖ₊₁ − A_d xₖ − B_d B⁺cₖ
// uₖ = B_d⁺(xₖ₊₁ − A_d xₖ − B_d B⁺cₖ)
// uₖ = B_d⁺(xₖ₊₁ − A_d xₖ) − B⁺cₖ

// Substitute in B assuming sgn(x) is a constant for the duration of the step.
// uₖ = B_d⁺(xₖ₊₁ − A_d xₖ) − kₐ(-(kg/kₐ + kₛ/kₐ sgn(x)))
// uₖ = B_d⁺(xₖ₊₁ − A_d xₖ) + kₐ(kg/kₐ + kₛ/kₐ sgn(x))
// uₖ = B_d⁺(xₖ₊₁ − A_d xₖ) + kg + kₛ sgn(x)

// uₖ = B_d⁺(xₖ₊₁ − A_d xₖ) − kₐ(-(kg/kₐ + kₛ/kₐ sgn(x)))
// uₖ = B_d⁺(xₖ₊₁ − A_d xₖ) + kₐ(kg/kₐ + kₛ/kₐ sgn(x))
// uₖ = B_d⁺(xₖ₊₁ − A_d xₖ) + kg + kₛ sgn(x)
if (ka == 0.0) {
// Simplify the model when kₐ = 0.
//

// Simplify A.
// A = −kᵥ/kₐ

// A = −kᵥ/kₐ
// As kₐ approaches zero, A approaches -∞.
// A = −∞
//
// A = −∞

// Simplify A_d.
//
// A_d = eᴬᵀ
// A_d = exp(−∞)
// A_d = 0
//

// A_d = eᴬᵀ
// A_d = exp(−∞)
// A_d = 0

// Simplify B_d.
narmstro2020 marked this conversation as resolved.
Show resolved Hide resolved
// B_d = A⁻¹(eᴬᵀ - I)B
// B_d = A⁻¹((0) - I)B
// B_d = A⁻¹(-I)B
// B_d = -A⁻¹B
// B_d = -(−kᵥ/kₐ)⁻¹(1/kₐ)
// B_d = (kᵥ/kₐ)⁻¹(1/kₐ)
// B_d = kₐ/kᵥ(1/kₐ)
// B_d = 1/kᵥ
//

// B_d = A⁻¹(eᴬᵀ - I)B
// B_d = A⁻¹((0) - I)B
// B_d = A⁻¹(-I)B
// B_d = -A⁻¹B
// B_d = -(−kᵥ/kₐ)⁻¹(1/kₐ)
// B_d = (kᵥ/kₐ)⁻¹(1/kₐ)
// B_d = kₐ/kᵥ(1/kₐ)
// B_d = 1/kᵥ

// Substitute these into the feedforward equation.
//
// uₖ = B_d⁺(xₖ₊₁ − A_d xₖ) + kg + kₛ sgn(x)
// uₖ = (1/kᵥ)⁺(xₖ₊₁ − (0) xₖ) + kg + kₛ sgn(x)
// uₖ = kᵥxₖ₊₁ + kg + kₛ sgn(x)

// uₖ = B_d⁺(xₖ₊₁ − A_d xₖ) + kg + kₛ sgn(x)
// uₖ = (1/kᵥ)⁺(xₖ₊₁ − (0) xₖ) + kg + kₛ sgn(x)
// uₖ = kᵥxₖ₊₁ + kg + kₛ sgn(x)
output.mut_replace(
kg
+ ks * Math.signum(nextVelocity.in(MetersPerSecond))
+ kv * nextVelocity.in(MetersPerSecond),
Volts);
return output;
} else {
// A = −kᵥ/kₐ
// B = 1/kₐ
// A_d = eᴬᵀ
// B_d = A⁻¹(eᴬᵀ - I)B
// A = −kᵥ/kₐ
// B = 1/kₐ
// A_d = eᴬᵀ
// B_d = A⁻¹(eᴬᵀ - I)B
double A = -kv / ka;
double B = 1.0 / ka;
double A_d = Math.exp(A * m_dt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,67 +188,78 @@ public <U extends Unit> Voltage calculate(
Measure<? extends PerUnit<U, TimeUnit>> currentVelocity,
Measure<? extends PerUnit<U, TimeUnit>> nextVelocity) {
// For a simple DC motor with the model
// dx/dt = −kᵥ/kₐ x + 1/kₐ u - kₛ/kₐ sgn(x),
//

// dx/dt = −kᵥ/kₐ x + 1/kₐ u - kₛ/kₐ sgn(x),

// where
narmstro2020 marked this conversation as resolved.
Show resolved Hide resolved
// A = −kᵥ/kₐ
// B = 1/kₐ
// c = -kₛ/kₐ sgn(x))
// A_d = eᴬᵀ
// B_d = A⁻¹(eᴬᵀ - I)B
// dx/dt = Ax + Bu + c
//

// A = −kᵥ/kₐ
// B = 1/kₐ
// c = -kₛ/kₐ sgn(x))
// A_d = eᴬᵀ
// B_d = A⁻¹(eᴬᵀ - I)B
// dx/dt = Ax + Bu + c

// Discretize the affine model.
// dx/dt = Ax + Bu + c
// dx/dt = Ax + B(u + B⁺c)
// xₖ₊₁ = eᴬᵀxₖ + A⁻¹(eᴬᵀ - I)B(uₖ + B⁺cₖ)
// xₖ₊₁ = A_d xₖ + B_d (uₖ + B⁺cₖ)
// xₖ₊₁ = A_d xₖ + B_d uₖ + B_d B⁺cₖ
//

// dx/dt = Ax + Bu + c
// dx/dt = Ax + B(u + B⁺c)
// xₖ₊₁ = eᴬᵀxₖ + A⁻¹(eᴬᵀ - I)B(uₖ + B⁺cₖ)
// xₖ₊₁ = A_d xₖ + B_d (uₖ + B⁺cₖ)
// xₖ₊₁ = A_d xₖ + B_d uₖ + B_d B⁺cₖ

// Solve for uₖ.
// B_d uₖ = xₖ₊₁ − A_d xₖ − B_d B⁺cₖ
// uₖ = B_d⁺(xₖ₊₁ − A_d xₖ − B_d B⁺cₖ)
// uₖ = B_d⁺(xₖ₊₁ − A_d xₖ) − B⁺cₖ
//

// B_d uₖ = xₖ₊₁ − A_d xₖ − B_d B⁺cₖ
// uₖ = B_d⁺(xₖ₊₁ − A_d xₖ − B_d B⁺cₖ)
// uₖ = B_d⁺(xₖ₊₁ − A_d xₖ) − B⁺cₖ

// Substitute in B assuming sgn(x) is a constant for the duration of the step.
// uₖ = B_d⁺(xₖ₊₁ − A_d xₖ) − kₐ(-(kₛ/kₐ sgn(x)))
// uₖ = B_d⁺(xₖ₊₁ − A_d xₖ) + kₐ(kₛ/kₐ sgn(x))
// uₖ = B_d⁺(xₖ₊₁ − A_d xₖ) + kₛ sgn(x)

// uₖ = B_d⁺(xₖ₊₁ − A_d xₖ) − kₐ(-(kₛ/kₐ sgn(x)))
// uₖ = B_d⁺(xₖ₊₁ − A_d xₖ) + kₐ(kₛ/kₐ sgn(x))
// uₖ = B_d⁺(xₖ₊₁ − A_d xₖ) + kₛ sgn(x)
if (ka == 0.0) {
// Simplify the model when kₐ = 0.
//

// Simplify A.
// A = −kᵥ/kₐ
// As kₐ approaches zero, A approaches -∞.
// A = −∞
//

// A = −kᵥ/kₐ

// As kₐ approaches zero, A approaches -∞.

// A = −∞

// Simplify A_d.
// A_d = eᴬᵀ
// A_d = exp(−∞)
// A_d = 0
//

// A_d = eᴬᵀ
// A_d = exp(−∞)
// A_d = 0

// Simplify B_d.
// B_d = A⁻¹(eᴬᵀ - I)B
// B_d = A⁻¹((0) - I)B
// B_d = A⁻¹(-I)B
// B_d = -A⁻¹B
// B_d = -(−kᵥ/kₐ)⁻¹(1/kₐ)
// B_d = (kᵥ/kₐ)⁻¹(1/kₐ)
// B_d = kₐ/kᵥ(1/kₐ)
// B_d = 1/kᵥ
//

// B_d = A⁻¹(eᴬᵀ - I)B
// B_d = A⁻¹((0) - I)B
// B_d = A⁻¹(-I)B
// B_d = -A⁻¹B
// B_d = -(−kᵥ/kₐ)⁻¹(1/kₐ)
// B_d = (kᵥ/kₐ)⁻¹(1/kₐ)
// B_d = kₐ/kᵥ(1/kₐ)
// B_d = 1/kᵥ

// Substitute these into the feedforward equation.
// uₖ = B_d⁺(xₖ₊₁ − A_d xₖ) + kₛ sgn(x)
// uₖ = (1/kᵥ)⁺(xₖ₊₁ − (0) xₖ) + kₛ sgn(x)
// uₖ = kᵥxₖ₊₁ + kₛ sgn(x)

// uₖ = B_d⁺(xₖ₊₁ − A_d xₖ) + kₛ sgn(x)
// uₖ = (1/kᵥ)⁺(xₖ₊₁ − (0) xₖ) + kₛ sgn(x)
// uₖ = kᵥxₖ₊₁ + kₛ sgn(x)
output.mut_replace(
ks * Math.signum(nextVelocity.magnitude()) + kv * nextVelocity.magnitude(), Volts);
return output;
} else {
// A = −kᵥ/kₐ
// B = 1/kₐ
// A_d = eᴬᵀ
// B_d = A⁻¹(eᴬᵀ - I)B
// A = −kᵥ/kₐ
// B = 1/kₐ
// A_d = eᴬᵀ
// B_d = A⁻¹(eᴬᵀ - I)B
double A = -kv / ka;
double B = 1.0 / ka;
double A_d = Math.exp(A * m_dt);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,26 +94,26 @@ class ElevatorFeedforward {
units::unit_t<Velocity> nextVelocity,
units::second_t dt) const {
// Discretize the affine model.
//

// dx/dt = Ax + Bu + c
// dx/dt = Ax + B(u + B⁺c)
// xₖ₊₁ = eᴬᵀxₖ + A⁻¹(eᴬᵀ - I)B(uₖ + B⁺cₖ)
// xₖ₊₁ = A_d xₖ + B_d (uₖ + B⁺cₖ)
// xₖ₊₁ = A_d xₖ + B_duₖ + B_d B⁺cₖ
//

// Solve for uₖ.
//

// B_duₖ = xₖ₊₁ − A_d xₖ − B_d B⁺cₖ
// uₖ = B_d⁺(xₖ₊₁ − A_d xₖ − B_d B⁺cₖ)
// uₖ = B_d⁺(xₖ₊₁ − A_d xₖ) − B⁺cₖ
//

// For an elevator with the model
//

// dx/dt = -Kv/Ka x + 1/Ka u - Kg/Ka - Ks/Ka sgn(x),
//

// A = -Kv/Ka, B = 1/Ka, and c = -(Kg/Ka + Ks/Ka sgn(x)). Substitute in B
// assuming sgn(x) is a constant for the duration of the step.
//

// uₖ = B_d⁺(xₖ₊₁ − A_d xₖ) − Ka(-(Kg/Ka + Ks/Ka sgn(x)))
// uₖ = B_d⁺(xₖ₊₁ − A_d xₖ) + Ka(Kg/Ka + Ks/Ka sgn(x))
// uₖ = B_d⁺(xₖ₊₁ − A_d xₖ) + Kg + Ks sgn(x)
Expand Down Expand Up @@ -153,57 +153,54 @@ class ElevatorFeedforward {
units::unit_t<Velocity> currentVelocity,
units::unit_t<Velocity> nextVelocity) const {
// For an elevator with the model
//

// dx/dt = −kᵥ/kₐ x + 1/kₐ u - kg/kₐ - kₛ/kₐ sgn(x),
//

// where
narmstro2020 marked this conversation as resolved.
Show resolved Hide resolved
//

// A = −kᵥ/kₐ
// B = 1/kₐ
// c = -(kg/kₐ + kₛ/kₐ sgn(x))
// A_d = eᴬᵀ
// B_d = A⁻¹(eᴬᵀ - I)B
// dx/dt = Ax + Bu + c
//

// Discretize the affine model.
//

// dx/dt = Ax + Bu + c
// dx/dt = Ax + B(u + B⁺c)
// xₖ₊₁ = eᴬᵀxₖ + A⁻¹(eᴬᵀ - I)B(uₖ + B⁺cₖ)
// xₖ₊₁ = A_d xₖ + B_d (uₖ + B⁺cₖ)
// xₖ₊₁ = A_d xₖ + B_d uₖ + B_d B⁺cₖ
//

// Solve for uₖ.
//

// B_d uₖ = xₖ₊₁ − A_d xₖ − B_d B⁺cₖ
// uₖ = B_d⁺(xₖ₊₁ − A_d xₖ − B_d B⁺cₖ)
// uₖ = B_d⁺(xₖ₊₁ − A_d xₖ) − B⁺cₖ
//
// Substitute in B assuming sgn(x) is a constant for the duration of the
// step.
//

// Substitute in B assuming sgn(x) is a constant for the duration of the step.

// uₖ = B_d⁺(xₖ₊₁ − A_d xₖ) − kₐ(-(kg/kₐ + kₛ/kₐ sgn(x)))
// uₖ = B_d⁺(xₖ₊₁ − A_d xₖ) + kₐ(kg/kₐ + kₛ/kₐ sgn(x))
// uₖ = B_d⁺(xₖ₊₁ − A_d xₖ) + kg + kₛ sgn(x)
if (kA == decltype(kA)(0)) {
// Simplify the model when kₐ = 0.
//

// Simplify A.
//

// A = −kᵥ/kₐ
//
// As kₐ approaches zero, A approaches -∞.
//
// A = −∞
//

// Simplify A_d.
//

// A_d = eᴬᵀ
// A_d = std::exp(−∞)
// A_d = exp(−∞)
// A_d = 0
//

// Simplify B_d.
//

// B_d = A⁻¹(eᴬᵀ - I)B
// B_d = A⁻¹((0) - I)B
// B_d = A⁻¹(-I)B
Expand All @@ -212,12 +209,12 @@ class ElevatorFeedforward {
// B_d = (kᵥ/kₐ)⁻¹(1/kₐ)
// B_d = kₐ/kᵥ(1/kₐ)
// B_d = 1/kᵥ
//

// Substitute these into the feedforward equation.
//

// uₖ = B_d⁺(xₖ₊₁ − A_d xₖ) + kg + kₛ sgn(x)
// uₖ = (1/kᵥ)⁺(xₖ₊₁ − (0) xₖ) + kg + kₛ sgn(x)
// uₖ = kᵥxₖ₊₁ + kg + kₛ sgn(x)
// uₖ = kᵥxₖ₊₁ + kg + kₛ sgn(x)
return kS * wpi::sgn(nextVelocity) + kG + kV * nextVelocity;
} else {
// A = −kᵥ/kₐ
Expand Down
Loading
Loading