Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#define S_FUNCTION_LEVEL 2
#define S_FUNCTION_NAME sfun_time
#define TIME_SCALE_FACTOR(S) ssGetSFcnParam(S,0)
/*
* Need to include simstruc.h for the definition of the SimStruct and
* its associated macro definitions.
*/
#include "simstruc.h"
/*
* Include the standard ANSI C header for handling time functions:
* ---------------------------------------------------------------
*/
#include <time.h>
static void mdlInitializeSizes(SimStruct *S)
{
ssSetNumSFcnParams(S, 1); /* Number of expected parameters */
if (ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S)) return;
ssSetNumContStates(S, 0);
ssSetNumDiscStates(S, 0);
if (!ssSetNumInputPorts(S, 0)) return;
if (!ssSetNumOutputPorts(S, 0)) return;
ssSetNumSampleTimes(S, 1);
ssSetNumRWork(S, 1);
ssSetNumIWork(S, 0);
ssSetNumPWork(S, 0);
ssSetNumModes(S, 0);
ssSetNumNonsampledZCs(S, 0);
ssSetOptions(S, 0);
}
#define MDL_INITIALIZE_SAMPLE_TIMES
static void mdlInitializeSampleTimes(SimStruct *S)
{
ssSetSampleTime(S, 0, CONTINUOUS_SAMPLE_TIME);
ssSetOffsetTime(S, 0, 0.0);
}
#define MDL_START
static void mdlStart(SimStruct *S)
{
ssSetRWorkValue(S,0,ssGetTStart(S));
}
static void mdlOutputs(SimStruct *S, int_T tid)
{
real_T t_previousSimTime = ssGetRWorkValue(S,0);
const real_T *scaleFactor = mxGetPr(TIME_SCALE_FACTOR(S));
time_T t_SimTime = ssGetT(S);
real_T t_diff = 0.0;
real_T dt;
real_T t_current;
real_T t_0;
real_T t_previous;
real_T t_elapsed;
/* Desired Delta time */
dt = (t_SimTime - t_previousSimTime) * (scaleFactor[0]);
/* Get clock time at the beginning of this step*/
t_previous = (real_T)clock()/CLOCKS_PER_SEC;
t_0 = (real_T)clock()/CLOCKS_PER_SEC;
/* Wait to reach the desired time */
while (t_diff<dt){
t_current = (real_T) clock()/CLOCKS_PER_SEC;
/* Look for wrapup */
if (t_current<t_previous){
t_elapsed = t_previous - t_0;
t_0 = (real_T) clock()/CLOCKS_PER_SEC - t_elapsed;
}
t_diff = t_current - t_0;
t_previous = t_current;
}
/* Store current time to be used in next time step*/
ssSetRWorkValue(S, 0, t_SimTime);
}
static void mdlTerminate(SimStruct *S)
{
UNUSED_ARG(S); /* unused input argument */
}
/*
* Required S-function trailer:
* ----------------------------
*/
#ifdef MATLAB_MEX_FILE /* Is this file being compiled as a MEX-file? */
#include "simulink.c" /* MEX-file interface mechanism */
#else
#include "cg_sfun.h" /* Code generation registration function */
#endif