How to generate Defines in the code for Stateflow Parameter/Constant

2 Ansichten (letzte 30 Tage)
I have a Stateflow where I have inputs that are basically uint8 data or boolean data.
Then I have conditions like Input1 == STOP or Input2 == CLOSED
I tried to define STOP, CLOSED both as a Stateflow Constant, or as a Parameter inside the the sldd file.
But in the code even if I configure in the Code Mapping for example STOP to be a Define, the code generator will replace it with its value directly.
I set also to generate Macros for Inlined Parameters, but Embedded Coder seems to have a mind of its own.
The only way to have defines in the Stateflow generated code is to define Simulink Enumerated type for the inputs, and use these types in the conditions in the Stateflow.

Antworten (1)

Karan Singh
Karan Singh am 8 Nov. 2023
Hi Bogdan,
What you're observing is indeed the expected behaviour of MATLAB's Embedded Coder. Embedded Coder is designed to replace constants and parameters with their actual values directly in the generated code to improve execution efficiency. This is because accessing a "#define" macro or a constant variable can potentially be slower than using the actual value directly.
The use of enumerated types, as you've found, is indeed one way to get around this issue. Here's an example of how to define and use an enumerated type:
1. Define the enumerated type in MATLAB:
classdef Status < Simulink.IntEnumType
STOP (0)
2. Use the enumerated type in Stateflow:
if (Input1 == Status.STOP) ...
if (Input2 == Status.CLOSED) ...
This way, "Status.STOP" and "Status.CLOSED" will appear as "STOP" and "CLOSED" in the generated code.
If maintaining the "#define" macros in the generated code is critical for your application, you might want to consider using a different code generation tool or manually post-processing the generated code to replace the constant values with their corresponding macros.
Attached below are some documentation links that you may find helpful:
Hope this helps!
Karan Singh Khati




Community Treasure Hunt

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

Start Hunting!

Translated by