-
Notifications
You must be signed in to change notification settings - Fork 0
/
speedcontroller.c
71 lines (56 loc) · 1.98 KB
/
speedcontroller.c
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
#define S_FUNCTION_LEVEL 2
#define S_FUNCTION_NAME speedcontroller
#include "simstruc.h"
#include <math.h>
#define U(element) (*uPtrs[element]) /*Pointer to Input Port0*/
static void mdlInitializeSizes(SimStruct *S){
ssSetNumDiscStates(S, 3);
if (!ssSetNumInputPorts(S, 1)) return;
ssSetInputPortWidth(S, 0, 4);
ssSetInputPortDirectFeedThrough(S, 0, 1);
ssSetInputPortOverWritable(S, 0, 1);
if (!ssSetNumOutputPorts(S, 1)) return;
ssSetOutputPortWidth(S, 0, 1);
ssSetNumSampleTimes(S, 1);
ssSetOptions(S, (SS_OPTION_EXCEPTION_FREE_CODE
| SS_OPTION_DISCRETE_VALUED_OUTPUT));}
static void mdlInitializeSampleTimes(SimStruct *S){
ssSetSampleTime(S, 0, 1e-3);
ssSetOffsetTime(S, 0, 0.0);}
#define MDL_INITIALIZE_CONDITIONS 25
static void mdlInitializeConditions(SimStruct *S){
real_T *X0 = ssGetRealDiscStates(S);
int_T nXStates = ssGetNumDiscStates(S);
InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0);
int_T i;
/* initialize the states to 0.0 */
for (i=0; i < nXStates; i++) {
X0[i] = 0.0; } }
static void mdlOutputs(SimStruct *S, int_T tid){
real_T *Y = ssGetOutputPortRealSignal(S,0);
real_T *X = ssGetRealDiscStates(S);
real_T Tref;
Tref = X[1];
Y[0] = Tref; }
#define MDL_UPDATE
static void mdlUpdate(SimStruct *S, int_T tid) {
real_T *X = ssGetRealDiscStates(S);
InputRealPtrsType uPtrs = ssGetInputPortRealSignalPtrs(S,0);
real_T dt = 1e-3;
real_T Tmax = 7.0;
real_T Tref, Wref, Wr, xWr_new, xWr_prv, Kspi, Kspp;
Kspp = U(0); Kspi = U(1); Wref = U(2);
Wr = U(3); xWr_prv = X[0];
xWr_new = xWr_prv + dt * ( Wref - Wr );
Tref = Kspi * xWr_new - Kspp * Wr ;
if( Tref >= Tmax ){Tref = Tmax; xWr_new = xWr_prv;}
if( Tref <= -Tmax ){Tref = -Tmax; xWr_new = xWr_prv;}
X[0] = xWr_new; X[1] = Tref;}
static void mdlTerminate(SimStruct *S)
{ } /*Keep this function empty since no memory is allocated*/
#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