S- function using C strange result on the first step of simulation
조회 수: 1 (최근 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!
댓글 수: 0
답변 (1개)
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.
Integrate C Functions Using Legacy Code Tool: www.mathworks.com/help/simulink/sfg/integrating-existing-c-functions-into-simulink-models-with-the-legacy-code-tool.html
Hope this resolves your query!
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Configure and View Diagnostics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!