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

조회 수: 1 (최근 30일)
Graure Iulian-Robert
Graure Iulian-Robert 2022년 8월 29일
답변: Ramtej 2023년 10월 17일
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