Loop software with Modelica
How to Implement Software in Loop for Control System Design with Modelica (OpenModelica, jModelica) for Installation and C / C ++ for Controller. What can you offer? I thought external C functions would help, but they seem to have some limitations, so they must obey the referential transparency properties, i.e. Return the same values for the same input (must not have internal states).
source to share
In this case, the problem with calls to external C functions does not apply. It is true that Modelica restricts the use of functions in continuous equations so that the function must return the same value for the same arguments. In these cases, you must find a way to pass the state to the function and return the new state to it (if you want to satisfy this "cleanliness" requirement). This is obviously quite tedious for C code (you would have to go into state, assign all state variables (possibly global) to run your code, and then extract the values of all state variables and return them).
Lucky for you, you don't need to worry about this. The reason is that your function should only be called from within a when clause. If you run a when clause based on time (for example, using the sample (...) function), I'm sure you're guaranteed that the function will only be called once every time.
Basically, your setup would be something like this:
algorithm when sample(0, sample_rate) then u := controllerEvaluation(x, y, t); end when;
This way, you can pass the time, the required states, x and inputs, y (or u, depending on your perspective), to the controller, and you will return the controller command to the factory, u.
In this context, the when clause is a view of your scheduler and because it does not include any state events (like you would, for example, with a cogwheel encoder or some other asynchronous interrupt), the simulator can schedule all of these function calls without the risk of repeating them ...
source to share