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!