How to generate code using embedded coder for custom simulink blocks and custom simulink inports/outports without taking them on root canvas?

조회 수: 13 (최근 30일)
Refer attached model (Iteration_01.slx) which contain following parts:
1. Custom Simulink Blocks (created for accessing hardware status) (Attached AnalogInputLib.slx file of the libary used).
2. Custom Simulink Inports/Outports (Blue ports are inports and Orange ports are outports generated using script and inport's (Blue) value is updated via BSW and outport's (orange) value is transferred to BSW using Embedded Coder's generated code in BSW sofware of respective Hardware.
1. Custom Simulink Blocks: Here custom simulink blocks were used on root canvas and in a subsystem. While compiling this simulink model for code generation, the code for the model on the root canvas was generated but code for the subsystem blocks was not generated as the blocks in the subsystem are refered as virtual blocks.
2. Custom Simulink Inports/Outports: Here custom simulink inports/outports were used on root canvas and in a subsystem. While compiling this simulink model for code generation, the code for the model on the root canvas was generated but code for the subsystem ports was not generated as the ports in the subsystem are refered as virtual blocks.
Is there anyway we can generate code for these custom simulink blocks and custom simulink inports/outports without taking them on root canvas?

답변 (1개)

Purvaja
Purvaja 2025년 6월 11일
Hi @Tejas,
As you mentioned in your question, since the subsystems and inports/outports are treated as virtual blocks, Embedded Coder does not generate code for them. To tackle this, we need to make these blocks non-virtual and enable function packaging as reusable functions for subsystems from the custom library.
I tried this on a dummy model. This model consists of:
  • A Simulink root model called (RootModel.slx) with a subsystem called “Subsystem”
  • A custom library (Library.slx) with a MATLAB Function block encapsulated inside a subsystem named MyLibBlock
The MyLibBlock is used inside the “Subsystem” block in the root model.
RootModel.slx -
Library.slx -
MyLibBlock consists of a Matlab function block with some code, having inputs and outputs.
We can observe in any block parameters of a subsystem that: To enable code generation, we must convert it into non-virtual subsystem by using “Treat as atomic parameter”.
Now, for generating code of library used in root model, two parameters need to be changed in the main library block :
  1. Block Parameters (Subsystem) ->Main-> enable “Treat as atomic Unit”
  2. Block Parameters (Subsystem) ->Code Generation -> Function Packaging -> Reuseable Function.
After generating the report using Embedded Coder, I was able to see the logic written in the MATLAB Function block inside my library block reflected in the generated C code. This confirms that code was successfully generated for the custom block used inside the subsystem.
You can refer to these resources to get an idea for wider picture:
  1. Embedded coder: https://www.mathworks.com/help/ecoder/index.html
  2. Non-virtual and virtual blocks in Simulink: https://www.mathworks.com/help/simulink/ug/nonvirtual-and-virtual-blocks.html
  3. Generate Code and Executables for Individual Subsystems: https://www.mathworks.com/help/rtw/ug/generate-code-and-executables-for-an-individual-subsystem.html
  4. Generate Subsystem Code as Separate Function and Files: https://www.mathworks.com/help/rtw/ug/generate-separate-code-and-files-for-subsystems-in-a-model.html
Hope this helps you! If you're still facing issues, I recommend experimenting with different parameter combinations for non-virtual subsystems and custom inports/outports.

카테고리

Help CenterFile Exchange에서 Deployment, Integration, and Supported Hardware에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by