Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

S-function builder / problem

494 views
Skip to first unread message

Daniel

unread,
Jun 5, 2013, 5:31:09 AM6/5/13
to
Halle everyone,

i'm using Simulink for the first time and try to import C-Code into Simulink via S-function builder.

At the moment i think i've done everything right rgarding implementing C-Code and declaring neccessary information within S-function block but nevertheless i have the feeling that the build block isn't working anyway. Both output values "pfCosPhiSet" and "pfQSet" show me permanent the value "0" while all the necessaray input values got some sensible value in order to test the block.

I did the implementation and declaration of the S-function block as followed:

1) New *-mdl opened
2) S-Function Builder Block from library into model taken
3) Within "Data Properties" following input- and output-value defined:

In_1: ucPowerCurrentPercent / Data type: uint8
In_2: ucPowerTargetPercent / Data type: uint8
In_3: fCosPhiTarget / Data type: single
In_4: fCosPhiCurrent / Data type: single
In_5: bCosPhiCurrentValid / Data type: boolean
In_6: fQTarget / Data type: single
In_7: fQCurrent / Data type: single
In_8: bQCurrentValid / Data type: boolean
Out_1: pfCosPhiSet / Data type: single
Out_2: pfQSet / Data type: single

4) Within "Libraries" Source- and Header-Files as followed integrated:

Library/Object/Source files (one per line)
nap_control.c
nap_control_alg.c
Include files and external function declarations
Includes:
#include <math.h>
#include "nap_control.h"
#include "nap_control_alg.h"
#include "types.h"

5) Within "Outputs" two functions (as they are declared in "nap_control.c") as followed integrated:

extern bool_t napControl_GetCosPhi(f32_t *pfCosPhiSet, u8_t ucPowerCurrentPercent, u8_t ucPowerTargetPercent, f32_t fCosPhiTarget, f32_t fCosPhiCurrent, bool_t bCosPhiCurrentValid, f32_t fQCurrent, bool_t bQCurrentValid);

extern bool_t napControl_GetQ(f32_t *pfQSet, u8_t ucPowerCurrentPercent, u8_t ucPowerTargetPercent, f32_t fQTarget, f32_t fQCurrent, bool_t bQCurrentValid);

6) Build S-Function gives following response:

### 'wrap_nap_control.c' created successfully
### 'wrap_nap_control_wrapper.c' created successfully
### 'wrap_nap_control.tlc' created successfully
### S-function wrap_nap_control.mexw32 created successfully

Could it be that the fact, that both output variables ("pfCosPhiSet" and "pfQSet") are pointers (see 5. *pfCosPhiSet / *pfQSet), result in problems within the s-function block. Is it possible to test the ready built block somehow?

MATLAB-Version: R2009b
Compiler: Microsoft Visual C++ 2008 Express
System: Windows 7 (64-bit)

I would be glad to get some answers and maybe some support in order to cope with the described issue.

Thanks in advance,

Daniel

Phil Goddard

unread,
Jun 5, 2013, 10:49:10 AM6/5/13
to
Within mdlOutputs do you actually call the external functions?
The snippet you give just defines them as being extern (which wouldn't normally be done in mdlOutputs anyway).

Phil.

Daniel

unread,
Jun 5, 2013, 11:47:09 AM6/5/13
to
"Phil Goddard" <ph...@goddardconsulting.ca> wrote in message <konj56$kf0$1...@newscl01ah.mathworks.com>...
> Within mdlOutputs do you actually call the external functions?
> The snippet you give just defines them as being extern (which wouldn't normally be done in mdlOutputs anyway).
>
> Phil.

Hello Phil,

thanks for replying. I though within the snippet i wrote down i'm calling the functions of the c code.

So, i gues i have to define them within "Libraries/External function declarations" and call them within "Outputs". Am i right?

Could you explain me the correct way for calling them and is the definition of the external functions done with copying the given snippet into "Libraries/External function declarations" in the same form how it is at the moment?

Whats about the pointers?

Markus

Phil Goddard

unread,
Jun 6, 2013, 3:05:17 PM6/6/13
to

> Could you explain me the correct way for calling them and is the definition of the external functions done with copying the given snippet into "Libraries/External function declarations" in the same form how it is at the moment?
>

It sounds as if you need to spend some time studying C.
Learn what the extern definition does, and how to call external libraries from C-code.

In your case the S-Function acts as a wrapper for converting the C syntax that Simulink expects/uses to that expected/used by your library.
That conversion is done in C and you need to have a good understanding of how to do it before writing the wrapper S-Function.

Phil.

Daniel

unread,
Jun 17, 2013, 5:36:09 AM6/17/13
to
"Phil Goddard" <ph...@goddardconsulting.ca> wrote in message <koqmhd$4ei$1...@newscl01ah.mathworks.com>...
Hello Phil,

as you suggested i went on with studying how the S-Function Builder creates the wrapper S-Function. Calling the needed libraries within S-Function Builder gui seems not to be so difficult for me but i still have a problem defining the external functions from my c-code ("nap_control.c").

My C-Skills are still not that high but i hope you will be so kind to give me short advice in order to be able using S-Function Builder importing existing c-Code.

Kind regards,

Markus

Phil Goddard

unread,
Jun 18, 2013, 12:08:13 PM6/18/13
to
Have a look at the link http://www.mathworks.com/help/simulink/sfg/example-using-s-functions-to-incorporate-legacy-c-code.html for an example that uses an extern defined function.

Phil.

praveen kande

unread,
Dec 22, 2015, 5:10:16 PM12/22/15
to
"Daniel " <realm...@web.de> wrote in message <kon0gt$meo$1...@newscl01ah.mathworks.com>...
Hi Danial,

Did you had any answers to this question??

Thanks.
0 new messages