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!