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

조회 수: 7 (최근 30일)
Bogdan Bodnarescu
Bogdan Bodnarescu 2022년 6월 14일
답변: Karan Singh 2023년 11월 8일
Hello,
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.

답변 (1개)

Karan Singh
Karan Singh 2023년 11월 8일
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
enumeration
STOP (0)
CLOSED (1)
end
end
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

카테고리

Help CenterFile Exchange에서 Naming Conventions에 대해 자세히 알아보기

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by