4

How to implement Software in the Loop for control systems design with Modelica (OpenModelica, jModelica) for a plant and C/C++ routine for a controller. What approaches you can suggest? I thought external C Functions would help but seems they have some restrictions such that they should obey reference transparancy property ie return the same values for the same inputs (should not have internal states).

2
  • 1
    How do you intend to implement the scheduler for your controller? Is this a controller that is scheduled at fixed time intervals (e.g. every 16 ms) or is this a controller that might be invoked asynchronously by state events within your plant? Or, do you have in mind that the controller is run continuously (i.e. a continuous mathematical model). I'm pretty sure what you want to do can be achieved in Modelica. But I need to understand exactly what you have in mind. Mar 26, 2012 at 6:43
  • Thank you for your response Michael! Yes I want my controller routine to be executed at regulear interval. And its routine should be provided with process variables from modelica and it should output control actions (signals) which influences model in modelica.
    – aliko
    Mar 26, 2012 at 8:39

1 Answer 1

4

In that case, the issue with calling external C functions does not apply. It is true that Modelica restricts the use of functions in the continuous equations such that the function must return the same value for the same arguments. In those cases, you must find a way to pass the state into the function and have it return the new state (if you want to satisfy this "purity" requirement). This is obviously quite tedious to do with C code (you would have to pass in the state, assign all state variables (probably globals) run your code, then extract the values of all the state variables and return them).

Fortunately for you, you don't need to worry about this. The reason is because your function only needs to be called from within a when clause. If you are triggering your when clause based on time (e.g. using the sample(...) function), I'm pretty sure you are guaranteed that the function will only be invoked once at each time.

Basically, your setup would be something like this:

algorithm
  when sample(0, sample_rate) then
    u := controllerEvaluation(x, y, t);
  end when;

In this way, you can pass time, the necessary states, x, and inputs, y (or u, depending on your perspective), into the controller and you get back the controller command to the plant, u.

In this context, the when clause is a representation of your scheduler and because it doesn't include any state events (like you would have, for example, with a tooth wheel encoder or other asynchronous interrupt) the simulator can schedule all these function invocations without any risk of having to repeat them.

2
  • Thank you Michael for your solution! I'll give it a try!
    – aliko
    Mar 27, 2012 at 13:12
  • It is not guaranteed by the Modelica Language Specification that when sample() invokes the function once at each time. It is likely that it is the case for an ODE representation of the system equations, but it is definitely not guaranteed for a DAE representation of the system equations. A correct implementation would use the sample operator from the cloked/synchronous features of Modelica Language Specification 3.3.
    – DelmeDelmi
    Nov 9, 2016 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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