S- function using C strange result on the first step of simulation

3 次查看(过去 30 天)
Hi!
I am using the s-function block with c code when I open the simulation for the first time the result is good, when I run the simulation for the second time on the first step of the simulation the result is wrong.
C_code:
float motor_speed = 0.0;
int old_position = 0;
float motor_speed (int new_position)
{
int delta_position = 0;
delta_position = (new_position - old_position);
motor_speed = (float)delta_position;
old_position = new_position;
return motor_speed;
}
S-function Implementation:
def = legacy_code('initialize')
def.SampleTime = 'inherited'
def.SFunctionName = 'SpeedEstimator'
def.OutputFcnSpec = 'single y1 = motor_pos_spd_qep(int32 u1)'
def.SourceFiles = {'Speed_Estimator.c'}
def.HeaderFiles = {'Speed_Estimator.h'}
legacy_code('sfcn_cmex_generate', def)
legacy_code('compile', def)
Thanks!

回答(1 个)

Ramtej
Ramtej 2023-10-17
Hi Graure,
Based on the information provided, it seems that you are encountering initialization issues with your S-function and assuming that there are no memory leaks in other parts of your code.
A possible workaround would be to declare a function to initialize and reset states, and declare this function in Legacy Code Tool Function Specifications.
You can refer to "Declaring Legacy Code Tool Function Specifications" section in the below documentation for further instructions.
Hope this resolves your query!

类别

Help CenterFile Exchange 中查找有关 Configure and View Diagnostics 的更多信息

标签

产品


版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by