How to create a sawtooth signal inside an S-function ?

Hi,
I'm working with C-MEX S-function (C code). I was wondering if it is possible to create a sawtooth signal inside a C-coded S-function and see the signal with the Simulink scope. If it is possible, how to do it ?
Thanks for your help

 採用された回答

TAB
TAB 2012 年 9 月 10 日
編集済み: TAB 2012 年 9 月 10 日

2 投票

A simple demonstration:
  1. Drag a S-function builder block in the model.
  2. Configure S-function having only 1 output (y[0]). No input, No discrete state, No continuous state.
  3. In the output function write code given below
  4. Build the S-function and run the model.
y0[0] = y0[0]+1;
if(y0[0]>UPPER_LIMIT)
{
y0[0] = 0;
}

1 件のコメント

Alice
Alice 2012 年 9 月 10 日
Thank you for your help, it works

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

その他の回答 (1 件)

Alice
Alice 2012 年 9 月 14 日

0 投票

Is it possible to do the same signal inside an S-function without having an output port ? The signal would be compared to the input signal.

3 件のコメント

TAB
TAB 2012 年 9 月 14 日
編集済み: TAB 2012 年 9 月 14 日
How will you get the sawtooth signal output without output port ?
Alice
Alice 2012 年 9 月 17 日
It won't exactly be an output signal. Actually, the sawtooth signal would be created inside the S-function to stay in the block and each point would be compared to the points of the input signal. The output would be the display of on/off leds ont the board I have.
TAB
TAB 2012 年 9 月 17 日
In your S-function builder block
  • 1 output (y[0]) for LED,
  • 1 input (u[0]) for comparing
  • 1 discrete state (xD[0]) for storing Ramp value
  • No continuous state.
In your S-function builder Discrete update field
xD[0] = xD[0] + 1;
if(xD[0]>=UPPER_LIMIT)
{
xD[0] = 0;
}
In your S-function builder Outputs field
if(xD[0]>=u0[0])
{
y0[0] = 1; /*LED ON*/
}
else
{
y0[0] = 0; /*LED OFF*/
}
Please go through the s-function document.

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

カテゴリ

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

製品

質問済み:

2012 年 9 月 10 日

Community Treasure Hunt

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

Start Hunting!

Translated by