Given some specific matricies, numA, numB, numP, q and r to calculate LQR gains, one can use simply LQRegulatorGains or solve the same via RiccatiSolve:

Inverse[r].(numB\[Transpose].RiccatiSolve[{numA, numB}, {q, r, matP}] + matP\[Transpose])

Giving the stated matricies some values, gains can be calculated and present the same solution as the aformentions LQRegulatorGains

I am attempting to compile either one of these functions for external use in embeddedhardware to adaptively calculate new gains on the fly but am completely overwhelmed in its generation. As far as I've come to understand not all code can be compiled and there are few and far between examples of higher level functions being compiled.

Is this possible? How would one give Compile matrices as a variable to be updated frequently for the given function?

Test matricies:

{numA, numB, numC, numD}={{{0,1,0},{113.116,-2.45364,0.00808392},{-113.116,2.45364,-0.136792}},{{0},{-15.4569},{261.555}},{{1,0,0},{0,1,0},{0,0,1}},{{0},{0},{0}}}
matP = {{0}, {0}, {0}}
q = DiagonalMatrix[{50, 1, 0.001}];
r = 2 {{1}};

LQRegulatorGains are typically computed offline before deployment and, if need be, some kind of gain scheduling is implemented to handle parameter variations. Also stuff involving matrix operations like Kalman filters and MPC have techniques like sequential Kalman filtering and explicit computations to circumvent doing the matrix computations online.

And a quick google search did not turn up anything promising for matrix operations on AVR microcontrollers. I found this from about 10 years ago,

IMO, you need to try some gain scheduling approach, and if that will not work it needs to be attempted on the RasPi which will slow things down.

| improve this answer | |
  • $\begingroup$ Mmm, would you mind showing an example of gain scheduling with mma for use with Microcontrollerkit? That would be equally as helpful...as a side question but just so I know, is it possible to compile the functions riccatisolve or Lqregulatorgains, but just slow to use, or not possible at all. ? Thanks regardless for the info! :) $\endgroup$ – morbo Aug 17 at 17:53
  • $\begingroup$ You can see a case of gain scheduling using Which and NonlinearStateSpaceModel here which is part of a larger example. $\endgroup$ – Suba Thomas Aug 17 at 18:52
  • $\begingroup$ I don't want to write in absolutes, but as I mentioned RiccatiSolve and LQRegulatorGains are offline techniques that it is practically certain that no effort will be made to make them compilable for deployment to an embedded target. $\endgroup$ – Suba Thomas Aug 17 at 18:52
  • $\begingroup$ thank you for the links and infos. Just to be clear, I didn't mean compiling specifically to microcontrollers, rather, just using those commands in general with compile. If it's not possible to export to embedded systems, I will try with gain scheduleing and attempt to compile those commands for use in a different software instead....assuming it's possible. $\endgroup$ – morbo Aug 18 at 8:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.