-
Notifications
You must be signed in to change notification settings - Fork 630
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
base: main
Are you sure you want to change the base?
Conversation
I'm not the happiest with the API shape, (big block of |
// 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) { |
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.
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.
// 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 comment
The reason will be displayed to describe this comment to others. Learn more.
Like you do here
I'm not a fan of this API either. wpimath's design intent is to provide composable functions you can mix-and-match instead of big interfaces that automate plumbing for specific use cases. The latter is less flexible and maintainable in the long term (e.g., PIDSubsystem, PIDCommand, RamseteCommand, ProfiledPIDController). The code in this PR can be shortened considerably by using a LinearSystemId factory function and making LQR take lists instead of vectors. At least in C++, you could shorten this enough that it's not really worth having the convenience function. With all that said, I'd rather see a more general class like LTVController. /**
* Constructor.
*
* @param dynamics function
* @param state tolerance vector
* @param input tolerance vector
* @param period (defaulted to 20 ms)
* @param measurement delay (defaulted to 0 ms)
*/
/**
* Returns the next output of the linearized LQR.
*
* @param measurement vector
* @param reference vector
* @returns control input vector
*/ I assume the main reason teams don't use the state-space API directly is because Java makes anything math-related difficult to write. |
We probably don't need a C++ version, since it's really short: auto system = frc::LinearSystemId::IdentifyPositionSystem<units::meters>(Kv, Ka);
frc::LinearQuadraticRegulator<2, 1> controller{system, {qp, qv}, {r}, period};
controller.LatencyCompensate(system, period, measurementDelay);
return {controller.K(0, 0), controller.K(0, 1)}; If your kₐ value is super small, or zero because you didn't measure it, you probably shouldn't be using LQR. |
That's exactly it. I wanted to use LQR stuff in my own team code, but the verbosity of math in Java made it just easier to write a helper class that eventually ended up as this PR. Java sucks. |
Implementation is stolen from SysID.
TODO: