Hi, I'm trying to build a simulink model working with S-function written in C. However, when I run the Simulink model, the scope is just a straight line, but it's suppose to be a sine wave. I used Legacy Code Toolbox to generate my S-function. My header file is just
int Sine_value(double amplitude, double vitesse_ang, double phase);
and my main file is
#include "Sine_value.h"
#include <math.h>
int Sine_value(double amplitude,
double vitesse_ang,
double phase,
)
{
int t[101];
int i;
int y0[101]
for (i = 0; i <= 100 ; i++) {
t[i] = i;
y0[i] = amplitude * sin(vitesse_ang * t[i] + phase);
}
return *y0;
}
To compile these file into the S-function, i used all these commands from the Legacy Code Toolbox :
def = legacy_code('initialize')
def.SFunctionName = 'S_function1'
def.OutputFcnSpec = 'y0 = Sine_value(double u1, double u2, double u3)'
def.HeaderFiles = {'Sine_value.h'}
def.SourceFiles = {'main.c'}
legacy_code('sfcn_cmex_generate', def)
legacy_code('compile', def)
I also attached all my files required.

 採用された回答

Fangjun Jiang
Fangjun Jiang 2022 年 6 月 30 日
編集済み: Fangjun Jiang 2022 年 6 月 30 日

0 投票

I understand this might be just an exercise to learn Legacy Code Toolbox and S-function, but there are a couple of things wrong
  1. I would think it would be wrong to specify the returned sine value to be an integer
  2. Once the s-function is compiled and built, it will be executed at every simulation step. With this in mind, you would need the C function just calculate one value, y=sin(x), and leave the rest to Simulink. Calculating the full cycle of sine wave in C makes it impossible to be used to build the s-function.

4 件のコメント

Pierre-Olivier Desrosiers
Pierre-Olivier Desrosiers 2022 年 6 月 30 日
Hi Fangjun, thanks for the answer. However, I've got some follow-up questions.
1) Instead of returning an integer, what should I return as a type ?
2) As i understood, I just need to return one value, because after each step, it will "save" a new value ? My question is : How can I get ALL the output values to plot them ?
Fangjun Jiang
Fangjun Jiang 2022 年 6 月 30 日
  1. Double
  2. Make a simple Simulink model, Clock block, feed to a Sine Wave block and then feed to a Scope block. Run simulation and you will see the sine wave. You can also log the data and plot the wave in MATLAB.
  3. Develop your s-function and then replace the Sine Wave block.
Pierre-Olivier Desrosiers
Pierre-Olivier Desrosiers 2022 年 7 月 2 日
Hi Fangjun, I had a last question. How do I call my time constant in my C function so my simulink model take the step time parameters to calculate the sine wave ?

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeSimulink Coder についてさらに検索

製品

リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by