-
Notifications
You must be signed in to change notification settings - Fork 633
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] Add a class to calculate PD gains using LQR #7825
Draft
TheComputer314
wants to merge
2
commits into
wpilibsuite:main
Choose a base branch
from
TheComputer314:LQR
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+125
−0
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
125 changes: 125 additions & 0 deletions
125
wpimath/src/main/java/edu/wpi/first/math/controller/FeedbackAnalysis.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
// Copyright (c) FIRST and other WPILib contributors. | ||
// Open Source Software; you can modify and/or share it under the terms of | ||
// the WPILib BSD license file in the root directory of this project. | ||
|
||
package edu.wpi.first.math.controller; | ||
|
||
import edu.wpi.first.math.Matrix; | ||
import edu.wpi.first.math.Nat; | ||
import edu.wpi.first.math.VecBuilder; | ||
import edu.wpi.first.math.system.LinearSystem; | ||
|
||
/** | ||
* A calculator for generating optimal PD gains given known control characteristics. | ||
* | ||
* <p>Implementation is taken from SysID. | ||
*/ | ||
public class FeedbackAnalysis { | ||
public record FeedbackGains(double kP, double kD) {} | ||
|
||
public static FeedbackGains getPositionGains( | ||
double kV, | ||
double kA, | ||
double maxControlEffort, | ||
double positionTolerance, | ||
double velocityTolerance, | ||
double controllerPeriodSeconds, | ||
double measurementDelaySeconds) { | ||
if (!Double.isFinite(kV) || !Double.isFinite(kA)) { | ||
throw new IllegalArgumentException("kV and kA must be finite numbers!"); | ||
} | ||
if (kV < 0) { | ||
throw new IllegalArgumentException("kV must be greater than or equal to zero!"); | ||
} | ||
if (kA < 0) { | ||
throw new IllegalArgumentException("kA must be greater than or equal to zero!"); | ||
} | ||
|
||
// If acceleration requires no effort, velocity becomes an input for position | ||
// control. We choose an appropriate model in this case to avoid numerical | ||
// instabilities in the LQR. | ||
if (kA >= 1e-7) { | ||
// Create a position system from our feedforward gains. | ||
var system = | ||
new LinearSystem<>( | ||
new Matrix<>(Nat.N2(), Nat.N2(), new double[] {0.0, 1.0, 0.0, -kV / kA}), | ||
new Matrix<>(Nat.N2(), Nat.N1(), new double[] {0.0, 1.0 / kA}), | ||
new Matrix<>(Nat.N1(), Nat.N2(), new double[] {1.0, 0.0}), | ||
new Matrix<>(Nat.N1(), Nat.N1(), new double[] {0.0})); | ||
// Create an LQR with 2 states to control -- position and velocity. | ||
var controller = | ||
new LinearQuadraticRegulator<>( | ||
system, | ||
VecBuilder.fill(positionTolerance, velocityTolerance), | ||
VecBuilder.fill(maxControlEffort), | ||
controllerPeriodSeconds); | ||
// Compensate for any latency from sensor measurements, filtering, etc. | ||
controller.latencyCompensate(system, controllerPeriodSeconds, measurementDelaySeconds); | ||
|
||
return new FeedbackGains(controller.getK().get(0, 0), controller.getK().get(0, 1)); | ||
} | ||
|
||
// This is our special model to avoid instabilities in the LQR. | ||
var system = | ||
new LinearSystem<>( | ||
new Matrix<>(Nat.N1(), Nat.N1(), new double[] {0.0}), | ||
new Matrix<>(Nat.N1(), Nat.N1(), new double[] {1.0}), | ||
new Matrix<>(Nat.N1(), Nat.N1(), new double[] {1.0}), | ||
new Matrix<>(Nat.N1(), Nat.N1(), new double[] {0.0})); | ||
// Create an LQR with one state -- position. | ||
var controller = | ||
new LinearQuadraticRegulator<>( | ||
system, | ||
VecBuilder.fill(positionTolerance), | ||
VecBuilder.fill(maxControlEffort), | ||
controllerPeriodSeconds); | ||
// Compensate for any latency from sensor measurements, filtering, etc. | ||
controller.latencyCompensate(system, controllerPeriodSeconds, measurementDelaySeconds); | ||
|
||
return new FeedbackGains(kV * controller.getK().get(0, 0), 0); | ||
} | ||
|
||
public static double getVelocityPGain( | ||
double kV, | ||
double kA, | ||
double maxControlEffort, | ||
double tolerance, | ||
double controllerPeriodSeconds, | ||
double measurementDelaySeconds) { | ||
if (!Double.isFinite(kV) || !Double.isFinite(kA)) { | ||
throw new IllegalArgumentException("kV and kA must be finite numbers!"); | ||
} | ||
if (kV < 0) { | ||
throw new IllegalArgumentException("kV must be greater than or equal to 0!"); | ||
} | ||
if (kA < 0) { | ||
throw new IllegalArgumentException("kA must be greater than or equal to 0!"); | ||
} | ||
|
||
// If acceleration for velocity control requires no effort, the feedback | ||
// control gains approach zero. We special-case it here because numerical | ||
// instabilities arise in LQR otherwise. | ||
if (kA <= 1e-7) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Like you do here |
||
return 0; | ||
} | ||
|
||
// Create a velocity system from our feedforward gains. | ||
var system = | ||
new LinearSystem<>( | ||
new Matrix<>(Nat.N1(), Nat.N1(), new double[] {-kV / kA}), | ||
new Matrix<>(Nat.N1(), Nat.N1(), new double[] {1.0 / kA}), | ||
new Matrix<>(Nat.N1(), Nat.N1(), new double[] {1.0}), | ||
new Matrix<>(Nat.N1(), Nat.N1(), new double[] {0.0})); | ||
// Create an LQR controller with 1 state -- velocity. | ||
var controller = | ||
new LinearQuadraticRegulator<>( | ||
system, | ||
VecBuilder.fill(tolerance), | ||
VecBuilder.fill(maxControlEffort), | ||
controllerPeriodSeconds); | ||
// Compensate for any latency from sensor measurements, filtering, etc. | ||
controller.latencyCompensate(system, controllerPeriodSeconds, measurementDelaySeconds); | ||
|
||
return controller.getK().get(0, 0); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the comment above describes the special case, why not put the special case in this if block?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IDK man that's what the comments in SysID do
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I deffo do agree that's kinda confusing.
That's a problem for later me.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment refers to the whole if statement. It's a style thing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahhh ok.