Simulink code interface to external C++ application

1 view (last 30 days)
I have a fairly complex Simulink model with multiple referenced models that I am trying to interface with external C++ code. I am using 2014a and generating code using Simulink Coder. Below are two important requirements I would like to satisfy while doing this:
  1. Have the ability to create multiple instances of the model in my external C++ code
  2. Have the ability to overwrite data that is input to the model and have it reflected to the external application.
Given these requirements, at the top level what is the best method to interface to external IO, datastores or IO ports?
From my understanding using global datastores (or Simulink.Signal objects) and by specifying the appropriate storage class I may be able to satisfy 2 above, but in doing so I would have to specify signal names and this would not allow me to satisfy 1.
If I use IO port blocks at top level, I may be able to satisfy 1 but not 2.

Accepted Answer

Jon Boerner
Jon Boerner on 20 Oct 2014
Hi Anup,
I think that the exact behavior you are looking for may be limited to the Embedded Coder product.
For Simulink Coder, here is what you can do.
  1. Go to the Configuration Parameters for the the model
  2. Go to the Code Generation pane
  3. Change the Language option to "C++"
  4. Go the the Code Generation > Interface pane
  5. Make sure Code Interface > Code interface packaging is set to "C++ class"
Now if you generate code you should see that you have a class with a private member modelname_P. You could create as many instances of the model as you wanted, and there would be no interaction between them. Using this member you could modify the parameters inside of it (such as the value for a Gain block, or the value of a constant block), if it was not private.
Having Embedded Coder and using the Embedded Coder System Target File "ert.tlc" allows you to modify that property to be public, and edit it exactly as you have mentioned.
You could use storage classes as you mentioned to make the parameters accessible, but they would turn into global variables that would be used by multiple instances of the model, meaning you could not make the models independent.
I suppose if everything was guaranteed to be single-threaded, you could make the parameters global and change their values before interacting with each model, but this would be fragile. For example, if you had a gain G that you wanted to update and made global, and had different values for different models (G1, G2, and so on) you could do:
G = G1
Initialize model 1
G = G2
Initialize model 2
while(1)
G = G1
model 1 step
G = G2
model 2 step
end
It is not a very good solution though. Really, the way to go is with Embedded Coder.
  1 Comment
Anup
Anup on 21 Oct 2014
Jon, Thanks for the detailed answer. Yes I do have the Code interface packaging set to C++ and am getting the code output in desired format. However, earlier my constraint was #2 which has now been removed due to a redesign. So I should be able to use the input/output port interface and make instances of the model without having any global data or managing access to that global data. Thanks for confirming my suspicion though that it would not have been a good solution using the global data. Also for pointing out that embedded coder would allow further customizations. Regards, Anup.

Sign in to comment.

More Answers (0)

Categories

Find more on Deployment, Integration, and Supported Hardware in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!