sfunction_robot.cpp
Go to the documentation of this file.
1 #define S_FUNCTION_NAME sfunction_robot
2 #define S_FUNCTION_LEVEL 2
3 
4 #define MDL_START
5 
6 extern "C" {
7 
8 #include "acado_common.h"
9 #include "acado_auxiliary_functions.h"
10 #include "simstruc.h"
11 
12 ACADOvariables acadoVariables;
14 
15 static void mdlInitializeSizes (SimStruct *S)
16 {
17  /* Specify the number of continuous and discrete states */
18  ssSetNumContStates(S, 0);
19  ssSetNumDiscStates(S, 0);
20 
21  /* Specify the number of intput ports */
22  if ( !ssSetNumInputPorts(S, 2) )
23  return;
24 
25  /* Specify the number of output ports */
26  if ( !ssSetNumOutputPorts(S, 2) )
27  return;
28 
29  /* Specify the number of parameters */
30  ssSetNumSFcnParams(S, 10);
31  if ( ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S) )
32  return;
33 
34  /* Specify dimension information for the input ports */
35  ssSetInputPortVectorDimension(S, 0, ACADO_NX);
36  ssSetInputPortVectorDimension(S, 1, ACADO_NY);
37 
38  /* Specify dimension information for the output ports */
39  ssSetOutputPortVectorDimension(S, 0, ACADO_NU );
40  ssSetOutputPortVectorDimension(S, 1, 1 );
41 
42  /* Specify the direct feedthrough status */
43  ssSetInputPortDirectFeedThrough(S, 0, 1);
44  ssSetInputPortDirectFeedThrough(S, 1, 1);
45 
46  /* One sample time */
47  ssSetNumSampleTimes(S, 1);
48  }
49 
50 
51 #if defined(MATLAB_MEX_FILE)
52 
53 #define MDL_SET_INPUT_PORT_DIMENSION_INFO
54 #define MDL_SET_OUTPUT_PORT_DIMENSION_INFO
55 
56 static void mdlSetInputPortDimensionInfo(SimStruct *S, int_T port, const DimsInfo_T *dimsInfo)
57 {
58  if ( !ssSetInputPortDimensionInfo(S, port, dimsInfo) )
59  return;
60 }
61 
62 static void mdlSetOutputPortDimensionInfo(SimStruct *S, int_T port, const DimsInfo_T *dimsInfo)
63 {
64  if ( !ssSetOutputPortDimensionInfo(S, port, dimsInfo) )
65  return;
66 }
67 
68  #endif /* MATLAB_MEX_FILE */
69 
70 
71 static void mdlInitializeSampleTimes(SimStruct *S)
72 {
73  double SAMPLINGTIME = mxGetScalar( ssGetSFcnParam(S, 0) );
74 
75  ssSetSampleTime(S, 0, SAMPLINGTIME);
76  ssSetOffsetTime(S, 0, 0.0);
77 }
78 
79 
80 static void mdlStart(SimStruct *S)
81 {
82  int i, j, k;
83 
84  InputRealPtrsType in_ref;
85  double *xInit, *uInit, *Smat, *SNmat, *numSteps, *lbV, *ubV, *lbAV, *ubAV;
86 
87  /* get inputs and perform feedback step */
88  in_ref = ssGetInputPortRealSignalPtrs(S, 1);
89 
90  xInit = mxGetPr( ssGetSFcnParam(S, 1) );
91  uInit = mxGetPr( ssGetSFcnParam(S, 2) );
92 
93  for( i=0; i < ACADO_N+1; ++i ) {
94  for( j=0; j < ACADO_NX; ++j ) acadoVariables.x[i*ACADO_NX+j] = xInit[j];
95  }
96  for( i=0; i < ACADO_N; ++i ) {
97  for( j=0; j < ACADO_NU; ++j ) acadoVariables.u[i*ACADO_NU+j] = uInit[j];
98  }
99 
100  for( i=0; i < ACADO_N; ++i ) {
101  for( j=0; j < ACADO_NY; ++j ) acadoVariables.y[i*ACADO_NY+j] = (double)(*in_ref[j]);
102  }
103  for( i=0; i < ACADO_NYN; ++i ) acadoVariables.yN[i] = (double)(*in_ref[i]);
104 
105  Smat = mxGetPr( ssGetSFcnParam(S, 3) );
106  SNmat = mxGetPr( ssGetSFcnParam(S, 4) );
107  numSteps = mxGetPr( ssGetSFcnParam(S, 5) );
108 
109  for( i = 0; i < (ACADO_NYN); ++i ) {
110  for( j = 0; j < ACADO_NYN; ++j ) {
111  acadoVariables.WN[i*ACADO_NYN+j] = SNmat[i*ACADO_NYN+j];
112  }
113  }
114  for( i = 0; i < (ACADO_NY); ++i ) {
115  for( j = 0; j < ACADO_NY; ++j ) {
116  acadoVariables.W[i*ACADO_NY+j] = Smat[i*ACADO_NY+j];
117  }
118  }
119 
120  lbV = mxGetPr( ssGetSFcnParam(S, 6) );
121  ubV = mxGetPr( ssGetSFcnParam(S, 7) );
122  lbAV = mxGetPr( ssGetSFcnParam(S, 8) );
123  ubAV = mxGetPr( ssGetSFcnParam(S, 9) );
124 
125  for( i=0; i < ACADO_N; ++i ) {
126  for( j=0; j < ACADO_NU; ++j ) acadoVariables.lbValues[i*ACADO_NU+j] = lbV[j];
127  for( j=0; j < ACADO_NU; ++j ) acadoVariables.ubValues[i*ACADO_NU+j] = ubV[j];
128 
129  for( j=0; j < ACADO_NU; ++j ) acadoVariables.lbAValues[i*ACADO_NU+j] = lbAV[j];
130  for( j=0; j < ACADO_NU; ++j ) acadoVariables.ubAValues[i*ACADO_NU+j] = ubAV[j];
131  }
132 
133  acado_preparationStep( );
134 }
135 
136 
137 static void mdlOutputs(SimStruct *S, int_T tid)
138 {
139  int i, j, iter, error;
140  double measurement[ACADO_NX];
141  acado_timer t;
142  double timeFdb, timePrep;
143 
144  InputRealPtrsType in_x, in_ref;
145  real_t *out_u0, *out_kktTol;
146 
147  /* get inputs and perform feedback step */
148  in_x = ssGetInputPortRealSignalPtrs(S, 0);
149  in_ref = ssGetInputPortRealSignalPtrs(S, 1);
150 
151  for( i=0; i < ACADO_NX; ++i ) acadoVariables.x0[i] = (double)(*in_x[i]);
152 
153  for( i=0; i < ACADO_N-1; ++i ) {
154  for( j=0; j < ACADO_NY; ++j ) acadoVariables.y[i*ACADO_NY+j] = acadoVariables.y[ACADO_NY+i*ACADO_NY+j];
155  }
156  for( i=0; i < ACADO_NYN; ++i ) acadoVariables.y[(ACADO_N-1)*ACADO_NY+i] = acadoVariables.yN[i];
157  for( i=0; i < ACADO_NYN; ++i ) acadoVariables.yN[i] = (double)(*in_ref[i]);
158 
159 
160  acado_tic( &t );
161  error = acado_feedbackStep( );
162  timeFdb = acado_toc( &t );
163 
164  acado_tic( &t );
165  acado_preparationStep( );
166  timePrep = acado_toc( &t );
167 
168  printf("NMPC step (error value: %d):\n", error);
169  printf("Timing RTI iteration: %.3g ms + %.3g ms = %.3g ms \n", timeFdb*1e3, timePrep*1e3, (timeFdb+timePrep)*1e3);
170  printf("---------------------------------------------------------------\n");
171 
172  /* return outputs and prepare next iteration */
173  out_u0 = ssGetOutputPortRealSignal(S, 0);
174  out_kktTol = ssGetOutputPortRealSignal(S, 1);
175 
176  for( i=0; i < ACADO_NU; ++i ) out_u0[i] = acadoVariables.u[i];
177  out_kktTol[0] = acado_getKKT( );
178 }
179 
180 
181 static void mdlTerminate(SimStruct *S)
182 {
183 }
184 
185 
186 #ifdef MATLAB_MEX_FILE
187 #include "simulink.c"
188 #else
189 #include "cg_sfun.h"
190 #endif
191 
192 
193 }
static void mdlStart(SimStruct *S)
#define ACADO_NX
Definition: AD_test.c:47
static void mdlInitializeSampleTimes(SimStruct *S)
#define ACADO_NU
Definition: AD_test.c:48
static void mdlInitializeSizes(SimStruct *S)
ACADOworkspace acadoWorkspace
static void mdlTerminate(SimStruct *S)
ACADOvariables acadoVariables
static void mdlOutputs(SimStruct *S, int_T tid)
double real_t
Definition: AD_test.c:10


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:35:04