Updating LeafSystem discrete state before publishing output

Viewed 32

I have a LeafSystem (controller) with two output ports, each of which depend on the solution to the same MathematicalProgram. My initial idea was to solve the program and store the solution as a discrete state which the output port callbacks can access and copy appropriately.

My interpretation of the documentation (https://drake.mit.edu/doxygen_cxx/group__discrete__systems.html) and what I see when implementing this, however, is that the output callbacks use the discrete state before the PerStepDiscreteUpdateEvent.

Now for my questions -

  1. Is this behavior that I've described above consistent with how the Simulator handles update events or am I missing something there?
  2. Is there a way to update the discrete state before the output calculation and have the updated state be used in the output?
  3. Is there a different design that would be more appropriate here?
1 Answers

The simple solution to your problem is cache entry.

Declare a cache entry that does your mathematical program work and updates the associated cache entry (it stores the results). When each output port is evaluated, they both "Eval" the cache entry and draw whatever data they need from the stored result. Then, no matter which port is evaluated first, the second one will always benefit from the pre-computation.

You can look at the cache entry notes for more detail.

Related