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] Add a class to calculate PD gains using LQR #7825

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

TheComputer314
Copy link
Contributor

@TheComputer314 TheComputer314 commented Feb 25, 2025

Implementation is stolen from SysID.

TODO:

  • Give it a nicer API shape
  • Documenting

@github-actions github-actions bot added the component: wpimath Math library label Feb 25, 2025
@TheComputer314
Copy link
Contributor Author

I'm not the happiest with the API shape, (big block of doubles is not a fun parameter list) will prolly change later

// 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) {
Copy link
Contributor

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?

Copy link
Contributor Author

@TheComputer314 TheComputer314 Feb 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Contributor Author

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like you do here

@calcmogul
Copy link
Member

calcmogul commented Feb 25, 2025

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.

@calcmogul
Copy link
Member

calcmogul commented Feb 25, 2025

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.

@TheComputer314
Copy link
Contributor Author

TheComputer314 commented Feb 25, 2025

I assume the main reason teams don't use the state-space API directly is because Java makes anything math-related difficult to write.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants