MATLAB Answers

S-function is eliminated through code optimization

조회 수: 2(최근 30일)
Jonas
Jonas 2021년 6월 9일
편집: Jonas 2021년 6월 9일
Hi all
I want to create an S-function by using the S-function Builder to generate a wrapper function which calls external code. The example purpose is to read a pressure sensor. It accepts an index input for which pressure sensor 1 through 8 we want to get the pressure from, and will output the pressure:
  • Input: uint8 index
  • Output: single pressure_volts
When I create such an S-function using the S-function Builder, and connect a Constant block with the index of the pressure sensor, the wrapper function is optimized away by code generation.
I have made an example below, which can also be found in the attachment (R2020b):
  • The function 'A' is the function with an input and an output, of which the input is connected to a Constant block with value '3'.
  • The function 'B' is a function with just an output.
  • The function 'C' is a function with just an input.
When we go look in the generated code, we only find back the wrapper functions for functions 'B' and 'C' in the step-function:
* Model step function */
void test_codegen_sfunctions_step(void)
{
get_pressure_B_Outputs_wrapper(&test_codegen_sfunctions_Y.output_B);
get_pressure_C_Outputs_wrapper(&test_codegen_sfunctions_U.input_C);
}
The 'output_A' the 'A'-function is connected to, is initialized to 0 and never updated again:
/* Model initialize function */
void test_codegen_sfunctions_initialize(void)
{
(...)
test_codegen_sfunctions_Y.output_A = 0.0F;
}
This means the wrapper function for function 'A' is never called.
How do I force Embedded Coder to properly generate code from this S-function, and not optimize it away? Of course, the code generator may inline the Constant value '3' on the location where it calls the wrapper function, but it may not negate the call completely.
Thanks in advance!
Jonas

채택된 답변

Jonas
Jonas 2021년 6월 9일
편집: Jonas 2021년 6월 9일
One solution is to create a Simulink.Parameter object with the value of the index, and Storage Class Const. Then use the parameter as input to the S-function. The wrapper function will not be eliminated and the named constant object is provided as an input.
However, this employs creating additional variables which encumbers the visibility of which index is called.

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by