From: Connor McArthur on
Hi,

I am having trouble finding a solution to this problem. All I need to do is define a discrete sample time as an s-function block parameter (much like Simulink's source blocks do, with -1 for inherited sample time). Is there a tutorial or walkthrough for this? My knowledge of C s-functions is intermediate at best.
From: Connor McArthur on
"Connor McArthur" <connor.mcarthur(a)villanova.edu> wrote in message <i2k8eg$ml7$1(a)fred.mathworks.com>...
> Hi,
>
> I am having trouble finding a solution to this problem. All I need to do is define a discrete sample time as an s-function block parameter (much like Simulink's source blocks do, with -1 for inherited sample time). Is there a tutorial or walkthrough for this? My knowledge of C s-functions is intermediate at best.

SOLVED:

I ended up blocking rtOneStep using the device's system clock so that it adhered to the determined fundamental sample time.
From: Phil Goddard on
Assuming you're using a Level-2 m-code S-Function, then in the block "setup" function you need to define the number of parameters that the block will be given, i.e.

block.NumDialogPrms = 1; % or 2, or 3, or 4, or however many

and then when specifying the sample time get the appropriate parameter, i.e. (assuming the sample time is the first parameter, and there's no offset)

block.SampleTimes = [block.DialogPrm(1).Data 0];

One of the best placed to start to learn how to write S-functions is to open up the demos by typing

>> sfundemos

then navigating you way into the appropriate lanuage.
Each demo has a link in it that when double clicked opens up the S-Function code for that demo in the editor.

Phil
From: Phil Goddard on
I've only just seen that you're wanting a C-mex S-Function, not a m-code one as per my previous response.

For a C-mes S-Function you, within mdlInitializeSizes, you specify the number of parameters to expect using

ssSetNumSFcnParams(S, 1); % or 2, or 3 ,.. etc

Then within mdlInitializeSampleTimes get and use the appropriate parameter using something like,

real_T Ts;
Ts = ((real_T*) mxGetPr(ssGetSFcnParam(S,0))); % 1st parameter is the rate
ssSetSampleTime(S, 0, Ts);

you'll most likely want some error checking in there too to ensure that the parameter is a positive scalar numeric and hence is valid to use.

Phil.