S function not accepting Input port Sample time

3 visualizzazioni (ultimi 30 giorni)
All,
I am using a S function for a counter logic which input sample time is converted by Rate transistion block to 20ms (original is 1 ms). When I run this, counter not incrementing for 20ms..
I have this below code snippet written for this in S function.. Any more functions to be added to enable my S function to get the correct Input Sample time?
Any inputs to crack on will be appreciated.
/*************************************************************************************************/
static void mdlInitializeSampleTimes(SimStruct *S)
{
ssSetSampleTime(S, 0, INHERITED_SAMPLE_TIME);
ssSetInputPortSampleTime(S,0,INHERITED_SAMPLE_TIME);
ssSetOffsetTime(S, 0, 0.0);
}
#define MDL_SET_INPUT_PORT_SAMPLE_TIME
static void mdlSetInputPortSampleTime(SimStruct *S,
int_T portIdx,
real_T sampleTime,
real_T offsetTime)
{
ssSetInputPortSampleTime(S,portIdx,sampleTime);
}
/*************************************************************************************************/
  1 Commento
Umar
Umar il 28 Lug 2024
Modificato: Walter Roberson il 28 Lug 2024
Hi Joseph,
By implementing the mdlSetInputPortSampleTime function in your S-function, you can explicitly set the sample time for the input port. Below is an example of how you can modify your S-function code:
#define MDL_SET_INPUT_PORT_SAMPLE_TIME
static void mdlSetInputPortSampleTime(SimStruct *S, int_T portIdx, real_T sampleTime, real_T offsetTime) { ssSetInputPortSampleTime(S, portIdx, sampleTime); }
Hope this helps.

Accedi per commentare.

Risposte (1)

R
R il 28 Lug 2024
To ensure your S-function properly handles the sample times, you need to set the output sample time as well. Additionally, you should make sure that the sample time you set is not inherited but explicitly defined, especially if you are using a rate transition block to change the sample time. Here's an updated version of your code snippet that includes these changes:
/*************************************************************************************************/
static void mdlInitializeSampleTimes(SimStruct *S)
{
// Set the sample time to 20ms (0.02 seconds)
ssSetSampleTime(S, 0, 0.02);
ssSetOffsetTime(S, 0, 0.0);
// Set the input port sample time to 20ms (0.02 seconds)
ssSetInputPortSampleTime(S, 0, 0.02);
ssSetInputPortOffsetTime(S, 0, 0.0);
// Set the output port sample time to 20ms (0.02 seconds)
ssSetOutputPortSampleTime(S, 0, 0.02);
ssSetOutputPortOffsetTime(S, 0, 0.0);
}
#define MDL_SET_INPUT_PORT_SAMPLE_TIME
static void mdlSetInputPortSampleTime(SimStruct *S,
int_T portIdx,
real_T sampleTime,
real_T offsetTime)
{
ssSetInputPortSampleTime(S, portIdx, sampleTime);
ssSetInputPortOffsetTime(S, portIdx, offsetTime);
// Ensure the output port sample time matches the input port sample time
ssSetOutputPortSampleTime(S, 0, sampleTime);
ssSetOutputPortOffsetTime(S, 0, offsetTime);
}
/*************************************************************************************************/
In this code:
  • ssSetSampleTime(S, 0, 0.02); sets the sample time of the S-function to 20ms.
  • ssSetInputPortSampleTime(S, 0, 0.02); and ssSetInputPortOffsetTime(S, 0, 0.0); set the input port sample time and offset time.
  • ssSetOutputPortSampleTime(S, 0, 0.02); and ssSetOutputPortOffsetTime(S, 0, 0.0); set the output port sample time and offset time.
By explicitly setting the sample times, you ensure that the S-function operates at the desired rate, which should help resolve the issue with the counter not incrementing correctly.
Refer to the documentation page at Specify S-Function Sample Times - MATLAB & Simulink (mathworks.com) to know more about the sample-time behaviour of S-Function.

Prodotti


Release

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by