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 plant constructor to Feedforward classes #7144

Closed
wants to merge 12 commits into from

Conversation

narmstro2020
Copy link
Contributor

So I'm having a little difficulty naming and placing these utilities.

Here is the primary use case. One can create a DCMotorSim, ElevatorSim, etc. via LinearSystemId's physical methods. If you wish to test out code using the simulator with feedback and feedforward controllers you would need to determine gains. One could manually tune and in a real system one can gain help from sysId, but I thought it would be handy to have a means of determining estimated gains from the plants themselves not just as a way of testing code without a robot (in the off season, etc) but also as a potential teaching tool for the concepts of feedback and feedforward. Most of the logic here is derived from sysId. I suppose that these utilities could be used as sanity checks, but the primary use case in my mind would be to help the usability of the simulators.

I haven't really done much to this other than propose the basic concept. Documentation is needed, C++ is needed. All of which I will do, but I want to start this as a draft first.

@narmstro2020
Copy link
Contributor Author

/format

Copy link
Member

@calcmogul calcmogul left a comment

Choose a reason for hiding this comment

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

We already have a class for autocomputing feedforward and feedback: LinearPlantInversionFeedforward and LinearQuadraticRegulator respectively. We have classes that wrap them like ElevatorFeedforward, ArmFeedforward, and LTVUnicycleController.

wpimath's oringally intended design had users create a model from physical constants or feedforward gains via a LinearSystemId factory function. The feedforward, feedback, and physics simulation classes consumed this model. In the following years, we've added overloads that take feedforward gains directly, but that bloated the interface. We started with one way to do things, and now we have two. This PR adds a third. We should decide on one cohesive design instead.

Synthesizing specific feedforward gains feels like a step backwards, because LinearSystemId already gives you a more general model that the existing classes can consume. Creating a LinearSystem, synthesizing feedforward gains from it, then passing those gains to a physics sim class that creates another LinearSystem internally feels redundant.

@narmstro2020
Copy link
Contributor Author

narmstro2020 commented Oct 1, 2024

We started with one way to do things, and now we have two. This PR adds a third. We should decide on one cohesive design instead.

I may have made the use case muddled. Apologies for that. I'm not sure this is adding a third? All this would do inherently is extract out kv, and ka from the Sim classes (also calculating kg for gravity systems). Now if the plant was made from the kv, ka overloads that would be silly, but if the plant was created from the physical overaloads then it would serve a purpose to then make a FF controller and a FB controller for testing/sim/teaching purpose.

I suppose one could add methods to the sim class to handle extracting kv, ka, kg, but having feedback gains method would then be needed.

I'm okay scrapping this and/or rewriting, but do you have any thought as to what to do?

EDIT: Or I suppose methods to grab kv, ka out of LinearPlantInversionFeedforward and LQR would be a possibility? I'd like to make the footprint as small as possible.

@calcmogul
Copy link
Member

calcmogul commented Oct 1, 2024

Again, you don't need the feedforward constants to construct a sim physics class. They already take a LinearSystem, so the extra step is unnecessary.

@narmstro2020
Copy link
Contributor Author

Again, you don't need the feedforward constants to construct a sim class. They already take a LinearSystem.

I agree. I'm talking about grabbing the kv, ka constants to create a Feedforward controller. Unless there is a better method for use when running a Sim class that I don't know about.

@calcmogul
Copy link
Member

calcmogul commented Oct 1, 2024

We have LinearPlantInversionFeedforward for that. Just add a LinearSystem constructor overload to the wrapper feedforward classes.

@narmstro2020
Copy link
Contributor Author

narmstro2020 commented Oct 1, 2024

We have LinearPlantInversionFeedforward for that. Just add a LinearSystem constructor overload to the wrapper feedforward classes.

Well I can that. Thanks.

Should I make methods to get kp, kd from LQR? Or is there another possiblity?

@narmstro2020
Copy link
Contributor Author

narmstro2020 commented Oct 1, 2024

@calcmogul Like this for starters?

I would like to do this for Flywheel as well, but wanted to check with you first. Plus the Flywheel will have type erasure issues.

@narmstro2020
Copy link
Contributor Author

/format

@calcmogul
Copy link
Member

calcmogul commented Oct 1, 2024

Should I make methods to get kp, kd from LQR?

No, because k_p and k_d only make sense for a 2x1 (2 states, one input) system, and LQR supports any dimensionality. In the 2x1 special case, users can extract k_p via getK(0, 0) and k_d via getK(0, 1). frc-docs could document that idiom.

A PIDController constructor overload that takes an LQR<2, 1> doesn't sit well with me because you might as well just use the LQR instance you already have for control (granted, the code for that is much cleaner in C++ than in Java). It also feels like interface bloat for what was otherwise a clean separation; it's odd that such an advanced class gets mixed into a simple one's interface.

// B_d = A⁻¹(eᴬᵀ - I)B
// A = −kᵥ/kₐ
// B = 1/kₐ
// A_d = eᴬᵀ
Copy link
Member

Choose a reason for hiding this comment

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

Are these from a different PR? The original indentation was fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This was spawned from the main branch.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It looks like the formatter "fixed" it

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do you want me to go ahead with fixing the indents.

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'll have the wait on this till the Elevator and Arm FF's are merged. One that will save time on the indents and the period will be part of the constructors at that point.

@narmstro2020
Copy link
Contributor Author

/format

@narmstro2020
Copy link
Contributor Author

@calcmogul Should I close this and reopen now that the FF classes have been updated or continue from this PR

@narmstro2020 narmstro2020 reopened this Oct 23, 2024
@narmstro2020
Copy link
Contributor Author

/format

@narmstro2020 narmstro2020 changed the title [wpimath] Simulation gains utility [wpimath] Add plant factory to Feedforward classes Oct 23, 2024
@narmstro2020 narmstro2020 marked this pull request as ready for review October 23, 2024 22:52
@narmstro2020 narmstro2020 requested a review from a team as a code owner October 23, 2024 22:52
@narmstro2020 narmstro2020 changed the title [wpimath] Add plant factory to Feedforward classes [wpimath] Add plant constructor to Feedforward classes Oct 23, 2024
@narmstro2020
Copy link
Contributor Author

I'm going to close this PR for now. I might open up a similar one later, but I haven't had a chance to retool this at all recently.

@narmstro2020 narmstro2020 deleted the SysIdUtil branch December 26, 2024 20:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants