1 #define S_FUNCTION_NAME sfunction_robot 2 #define S_FUNCTION_LEVEL 2 8 #include "acado_common.h" 9 #include "acado_auxiliary_functions.h" 18 ssSetNumContStates(S, 0);
19 ssSetNumDiscStates(S, 0);
22 if ( !ssSetNumInputPorts(S, 2) )
26 if ( !ssSetNumOutputPorts(S, 2) )
30 ssSetNumSFcnParams(S, 10);
31 if ( ssGetNumSFcnParams(S) != ssGetSFcnParamsCount(S) )
35 ssSetInputPortVectorDimension(S, 0,
ACADO_NX);
36 ssSetInputPortVectorDimension(S, 1, ACADO_NY);
39 ssSetOutputPortVectorDimension(S, 0,
ACADO_NU );
40 ssSetOutputPortVectorDimension(S, 1, 1 );
43 ssSetInputPortDirectFeedThrough(S, 0, 1);
44 ssSetInputPortDirectFeedThrough(S, 1, 1);
47 ssSetNumSampleTimes(S, 1);
51 #if defined(MATLAB_MEX_FILE) 53 #define MDL_SET_INPUT_PORT_DIMENSION_INFO 54 #define MDL_SET_OUTPUT_PORT_DIMENSION_INFO 56 static void mdlSetInputPortDimensionInfo(SimStruct *
S, int_T port,
const DimsInfo_T *dimsInfo)
58 if ( !ssSetInputPortDimensionInfo(S, port, dimsInfo) )
62 static void mdlSetOutputPortDimensionInfo(SimStruct *S, int_T port,
const DimsInfo_T *dimsInfo)
64 if ( !ssSetOutputPortDimensionInfo(S, port, dimsInfo) )
73 double SAMPLINGTIME = mxGetScalar( ssGetSFcnParam(S, 0) );
75 ssSetSampleTime(S, 0, SAMPLINGTIME);
76 ssSetOffsetTime(S, 0, 0.0);
84 InputRealPtrsType in_ref;
85 double *xInit, *uInit, *Smat, *SNmat, *numSteps, *lbV, *ubV, *lbAV, *ubAV;
88 in_ref = ssGetInputPortRealSignalPtrs(S, 1);
90 xInit = mxGetPr( ssGetSFcnParam(S, 1) );
91 uInit = mxGetPr( ssGetSFcnParam(S, 2) );
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];
96 for( i=0; i < ACADO_N; ++i ) {
97 for( j=0; j <
ACADO_NU; ++j ) acadoVariables.u[i*ACADO_NU+j] = uInit[j];
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]);
103 for( i=0; i < ACADO_NYN; ++i ) acadoVariables.yN[i] = (
double)(*in_ref[i]);
105 Smat = mxGetPr( ssGetSFcnParam(S, 3) );
106 SNmat = mxGetPr( ssGetSFcnParam(S, 4) );
107 numSteps = mxGetPr( ssGetSFcnParam(S, 5) );
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];
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];
120 lbV = mxGetPr( ssGetSFcnParam(S, 6) );
121 ubV = mxGetPr( ssGetSFcnParam(S, 7) );
122 lbAV = mxGetPr( ssGetSFcnParam(S, 8) );
123 ubAV = mxGetPr( ssGetSFcnParam(S, 9) );
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];
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];
133 acado_preparationStep( );
139 int i, j, iter, error;
142 double timeFdb, timePrep;
144 InputRealPtrsType in_x, in_ref;
145 real_t *out_u0, *out_kktTol;
148 in_x = ssGetInputPortRealSignalPtrs(S, 0);
149 in_ref = ssGetInputPortRealSignalPtrs(S, 1);
151 for( i=0; i <
ACADO_NX; ++i ) acadoVariables.x0[i] = (
double)(*in_x[i]);
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];
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]);
161 error = acado_feedbackStep( );
162 timeFdb = acado_toc( &t );
165 acado_preparationStep( );
166 timePrep = acado_toc( &t );
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");
173 out_u0 = ssGetOutputPortRealSignal(S, 0);
174 out_kktTol = ssGetOutputPortRealSignal(S, 1);
176 for( i=0; i <
ACADO_NU; ++i ) out_u0[i] = acadoVariables.u[i];
177 out_kktTol[0] = acado_getKKT( );
186 #ifdef MATLAB_MEX_FILE 187 #include "simulink.c" static void mdlStart(SimStruct *S)
static void mdlInitializeSampleTimes(SimStruct *S)
static void mdlInitializeSizes(SimStruct *S)
ACADOworkspace acadoWorkspace
static void mdlTerminate(SimStruct *S)
ACADOvariables acadoVariables
static void mdlOutputs(SimStruct *S, int_T tid)