Safe Cast Function cannot be avoided in Generated Code

5 次查看(过去 30 天)
Hello all,
I am using MATLAB 2018b and I have run into a problem with Safe Cast Function in Generated Code.
In my Simulink model I have a Stateflow state machine that uses Simulink Functions whose input data type is a Simulink.defineIntEnumType. So whenever the Simulink Functions are called, their inputs are preprocessed by a Safe Cast function like the following below:
static uint8 <Simulink_model_name>_safe_cast_to_<Enum_type>(uint8 input)
Due to the amount of Simulink Function calls, the generated code becomes pretty much inefficient.
Looking into Embedder Coder user guide and MATLAB help, it is suggested to disable the block parameter: Saturate on integer overflow in order to prevent these safe cast functions from being included in the generated code.
However, the problem is that although I have disabled this parameter in the block which contains the Simulink Function calls, the safe cast functions are still generated in the resulting code.
Is there any other related parameter that I need to configure or any other Enum-related default settings that can be overridden to avoid safe cast functions?
Thank you very much for your support, any feedback would be greatly appreciated.

回答(2 个)

Nicolas Schoonbroodt
Hi Mario,
Just to be sure, can you confirm my understanding of the problem? From the problem description it looks like you have somewhere in your Stateflow chart a line that must looks like
someData = simulink_function(some_input_arg);
With two alternatives for "some_input_arg".
  1. it's a value. (for example 10, or uint8(42) or something similar)
  2. it's another data that exists in the chart (computed in the chart, or an input of the chart, something like that)(so some_input_arg is someVariable)
If what you have is the first case, instead of writing directly 10 or uint(42), what you should write is something like NameOfTheEnum.EnumValue (if the enum is called ON_OFF and you want the value ON, you write ON_OFF.ON)
If it's the second case, then you should specify the datatype of someVariable to be of the enum type in your chart. To do that, you have two possibilities:
  • using the model explorer (ctrl+h), navigate to your chart in the hierarchy on the left, and specify the data type.
  • Using the symbol pane and the property inspector.
If this fixes the problem, you can find potential data type problems in Stateflow charts by using the model advisor with the check hisf_0015 I think. (I believe that you need Simulink Check to have access to hisf_0015)

Sneha Patne
Sneha Patne 2021-1-9
Hello Mario,
I ran into same issue while passing enum as an argument to an external C function call from the stateflow.
If the Action Language is set to "C" in chart property, pass EnumValue as an argument to a C function.
EX: function (ON), considering enum of above answer.
With this option, even if Saturate on integer overflow is enabled, Safe Cast Function function will not generate in code.
But, if the Action Language is set to "MATLAB" in chart property, pass NameOfTheEnum.EnumValue as an argument to a C function.
EX: function (ON_OFF.ON);
The second option suggested by Nicolas which will also work. In this case, signal needs to be created and defined in sldd or .m file and resolve the siganl before passing to stateflow and assign enum value using 'Enumerated Constant" block. But it's unecessory step if new variable is not required.
I hope this helps.

类别

Help CenterFile Exchange 中查找有关 Simulink Functions 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by