00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00034 #include <acado_toolkit.hpp>
00035 #include <acado_gnuplot.hpp>
00036
00037 USING_NAMESPACE_ACADO
00038
00039 int main( )
00040 {
00041
00042
00043
00044 DifferentialState r;
00045 DifferentialState phi;
00046 DifferentialState theta;
00047
00048 DifferentialState dr;
00049 DifferentialState dphi;
00050 DifferentialState dtheta;
00051
00052 DifferentialState n;
00053
00054 DifferentialState Psi;
00055 DifferentialState CL;
00056
00057 DifferentialState W;
00058
00059
00060
00061
00062
00063 Function model_response ;
00064
00065
00066
00067
00068 Control ddr0;
00069 Control dPsi;
00070 Control dCL;
00071
00072
00073 Disturbance w_extra;
00074
00075
00076
00077
00078
00079
00080 double mk = 2000.00;
00081 double A = 500.00;
00082 double V = 720.00;
00083 double cd0 = 0.04;
00084
00085 double K = 0.04;
00086
00087
00088
00089
00090 double g = 9.81;
00091 double rho = 1.23;
00092
00093
00094
00095 double rhoc = 1450.00;
00096 double cc = 1.00;
00097 double dc = 0.05614;
00098
00099
00100
00101
00102 double w0 = 10.00;
00103 double h0 = 100.00;
00104 double hr = 0.10;
00105
00106
00107
00108
00109
00110
00111 double AQ ;
00112
00113 IntermediateState mc;
00114 IntermediateState m ;
00115 IntermediateState m_;
00116
00117 IntermediateState Cg;
00118
00119 IntermediateState dm;
00120
00121
00122
00123
00124
00125 double PI = 3.1415926535897932;
00126
00127
00128
00129
00130
00131 IntermediateState h ;
00132 IntermediateState w ;
00133 IntermediateState we [ 3];
00134 IntermediateState nwe ;
00135 IntermediateState nwep ;
00136 IntermediateState ewep [ 3];
00137 IntermediateState eta ;
00138 IntermediateState et [ 3];
00139 IntermediateState Caer ;
00140 IntermediateState Cf ;
00141 IntermediateState CD ;
00142 IntermediateState Fg [ 3];
00143 IntermediateState Faer [ 3];
00144 IntermediateState Ff [ 3];
00145 IntermediateState F [ 3];
00146
00147
00148
00149
00150
00151
00152
00153 IntermediateState a_pseudo [ 3];
00154 IntermediateState dn ;
00155 IntermediateState ddr ;
00156 IntermediateState ddphi ;
00157 IntermediateState ddtheta ;
00158 IntermediateState power ;
00159
00160 IntermediateState regularisation ;
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172 AQ = PI*dc*dc/4.0 ;
00173
00174
00175
00176
00177 mc = rhoc*AQ*r ;
00178 m = mk + mc / 3.0;
00179 m_ = mk + mc / 2.0;
00180
00181 dm = (rhoc*AQ/ 3.0)*dr;
00182
00183
00184
00185
00186
00187 h = r*cos(theta)+60.0 ;
00188
00189 w = log(h/hr) / log(h0/hr) *(w0+w_extra) ;
00190
00191
00192
00193
00194
00195 we[0] = w*sin(theta)*cos(phi) - dr ;
00196 we[1] = -w*sin(phi) - r*sin(theta)*dphi ;
00197 we[2] = -w*cos(theta)*cos(phi) + r *dtheta;
00198
00199
00200
00201
00202
00203 nwep = pow( we[1]*we[1] + we[2]*we[2], 0.5 );
00204 nwe = pow( we[0]*we[0] + we[1]*we[1] + we[2]*we[2], 0.5 );
00205 eta = asin( we[0]*tan(Psi)/ nwep ) ;
00206
00207
00208
00209
00210 ewep[1] = we[1] / nwep ;
00211 ewep[2] = we[2] / nwep ;
00212
00213
00214
00215 et [0] = sin(Psi) ;
00216 et [1] = (-cos(Psi)*sin(eta))*ewep[1] - (cos(Psi)*cos(eta))*ewep[2];
00217 et [2] = (-cos(Psi)*sin(eta))*ewep[2] + (cos(Psi)*cos(eta))*ewep[1];
00218
00219
00220
00221
00222
00223
00224
00225 Cg = (V*rho-m_)*g ;
00226 Caer = (rho*A/2.0 )*nwe ;
00227 Cf = (rho*dc/8.0)*r*nwe ;
00228
00229
00230
00231
00232
00233 CD = cd0 + K*CL*CL ;
00234
00235
00236
00237
00238
00239
00240 Fg [0] = Cg * cos(theta) ;
00241
00242 Fg [2] = Cg * sin(theta) ;
00243
00244
00245
00246
00247
00248 Faer[0] = Caer*( CL*(we[1]*et[2]-we[2]*et[1]) + CD*we[0] ) ;
00249 Faer[1] = Caer*( CL*(we[2]*et[0]-we[0]*et[2]) + CD*we[1] ) ;
00250 Faer[2] = Caer*( CL*(we[0]*et[1]-we[1]*et[0]) + CD*we[2] ) ;
00251
00252
00253
00254
00255
00256
00257 Ff [1] = Cf * cc* we[1] ;
00258 Ff [2] = Cf * cc* we[2] ;
00259
00260
00261
00262
00263
00264
00265 F [0] = Fg[0] + Faer[0] ;
00266 F [1] = Faer[1] + Ff[1] ;
00267 F [2] = Fg[2] + Faer[2] + Ff[2] ;
00268
00269
00270
00271
00272
00273
00274 a_pseudo [0] = - ddr0
00275 + r*( dtheta*dtheta
00276 + sin(theta)*sin(theta)*dphi *dphi )
00277 - dm/m*dr ;
00278
00279 a_pseudo [1] = - 2.0*cos(theta)/sin(theta)*dphi*dtheta
00280 - 2.0*dr/r*dphi
00281 - dm/m*dphi ;
00282
00283 a_pseudo [2] = cos(theta)*sin(theta)*dphi*dphi
00284 - 2.0*dr/r*dtheta
00285 - dm/m*dtheta ;
00286
00287
00288
00289
00290
00291
00292
00293 ddr = F[0]/m + a_pseudo[0] ;
00294 ddphi = F[1]/(m*r*sin(theta)) + a_pseudo[1] ;
00295 ddtheta = -F[2]/(m*r ) + a_pseudo[2] ;
00296
00297
00298
00299
00300
00301
00302
00303
00304 dn = ( dphi*ddtheta - dtheta*ddphi ) /
00305 (2.0*PI*(dphi*dphi + dtheta*dtheta) ) ;
00306
00307
00308
00309
00310
00311
00312 power = m*ddr*dr ;
00313
00314
00315
00316
00317
00318
00319
00320 regularisation = 5.0e2 * ddr0 * ddr0
00321 + 1.0e8 * dPsi * dPsi
00322 + 1.0e5 * dCL * dCL
00323 + 2.5e5 * dn * dn
00324 + 2.5e7 * ddphi * ddphi;
00325 + 2.5e7 * ddtheta * ddtheta;
00326 + 2.5e6 * dtheta * dtheta;
00327
00328
00329
00330
00331
00332
00333
00334 VariablesGrid myReference; myReference.read( "ref_w_zeros.txt" );
00335 PeriodicReferenceTrajectory reference( myReference );
00336
00337
00338
00339
00340 DifferentialEquation f;
00341
00342 f << dot(r) == dr ;
00343 f << dot(phi) == dphi ;
00344 f << dot(theta) == dtheta ;
00345 f << dot(dr) == ddr0 ;
00346 f << dot(dphi) == ddphi ;
00347 f << dot(dtheta) == ddtheta ;
00348 f << dot(n) == dn ;
00349 f << dot(Psi) == dPsi ;
00350 f << dot(CL) == dCL ;
00351 f << dot(W) == (-power + regularisation)*1.0e-6;
00352
00353
00354 model_response << r ;
00355 model_response << phi ;
00356 model_response << theta;
00357 model_response << dr ;
00358 model_response << dphi ;
00359 model_response << dtheta;
00360 model_response << ddr0;
00361 model_response << dPsi;
00362 model_response << dCL ;
00363
00364
00365 DVector x_scal(9);
00366
00367 x_scal(0) = 60.0;
00368 x_scal(1) = 1.0e-1;
00369 x_scal(2) = 1.0e-1;
00370 x_scal(3) = 40.0;
00371 x_scal(4) = 1.0e-1;
00372 x_scal(5) = 1.0e-1;
00373 x_scal(6) = 60.0;
00374 x_scal(7) = 1.5e-1;
00375 x_scal(8) = 2.5;
00376
00377
00378 DMatrix Q(9,9);
00379 Q.setIdentity();
00380 DMatrix Q_end(9,9);
00381 Q_end.setIdentity();
00382 int i;
00383 for( i = 0; i < 6; i++ ){
00384 Q(i,i) = (1.0e-1/x_scal(i))*(1.0e-1/x_scal(i));
00385 Q_end(i,i) = (5.0e-1/x_scal(i))*(5.0e-1/x_scal(i));
00386 }
00387 for( i = 6; i < 9; i++ ){
00388 Q(i,i) = (1.0e-1/x_scal(i))*(1.0e-1/x_scal(i));
00389 Q_end(i,i) = (5.0e-1/x_scal(i))*(5.0e-1/x_scal(i));
00390 }
00391
00392 DVector measurements(9);
00393 measurements.setAll( 0.0 );
00394
00395
00396
00397
00398 const double t_start = 0.0;
00399 const double t_end = 10.0;
00400 OCP ocp( t_start, t_end, 10 );
00401 ocp.minimizeLSQ( Q,model_response, measurements ) ;
00402 ocp.minimizeLSQEndTerm( Q_end,model_response, measurements ) ;
00403 ocp.subjectTo( f );
00404
00405
00406 ocp.subjectTo( -0.34 <= phi <= 0.34 );
00407 ocp.subjectTo( 0.85 <= theta <= 1.45 );
00408 ocp.subjectTo( -40.0 <= dr <= 10.0 );
00409 ocp.subjectTo( -0.29 <= Psi <= 0.29 );
00410 ocp.subjectTo( 0.1 <= CL <= 1.50 );
00411 ocp.subjectTo( -0.7 <= n <= 0.90 );
00412 ocp.subjectTo( -25.0 <= ddr0 <= 25.0 );
00413 ocp.subjectTo( -0.065 <= dPsi <= 0.065 );
00414 ocp.subjectTo( -3.5 <= dCL <= 3.5 );
00415 ocp.subjectTo( -60.0 <= cos(theta)*r );
00416 ocp.subjectTo( w_extra == 0.0 );
00417
00418
00419
00420
00421 OutputFcn identity;
00422 DynamicSystem dynamicSystem( f,identity );
00423 Process process( dynamicSystem,INT_RK45 );
00424
00425
00426 VariablesGrid disturbance; disturbance.read( "my_wind_disturbance_controlsfree.txt" );
00427 if (process.setProcessDisturbance( disturbance ) != SUCCESSFUL_RETURN)
00428 exit( EXIT_FAILURE );
00429
00430
00431
00432 double samplingTime = 1.0;
00433 RealTimeAlgorithm algorithm( ocp, samplingTime );
00434 if (algorithm.initializeDifferentialStates("p_s_ref.txt" ) != SUCCESSFUL_RETURN)
00435 exit( EXIT_FAILURE );
00436 if (algorithm.initializeControls ("p_c_ref.txt" ) != SUCCESSFUL_RETURN)
00437 exit( EXIT_FAILURE );
00438
00439 algorithm.set( MAX_NUM_ITERATIONS, 2 );
00440 algorithm.set( KKT_TOLERANCE , 1e-4 );
00441 algorithm.set( HESSIAN_APPROXIMATION,GAUSS_NEWTON);
00442 algorithm.set( INTEGRATOR_TOLERANCE, 1e-6 );
00443 algorithm.set( GLOBALIZATION_STRATEGY,GS_FULLSTEP );
00444
00445 algorithm.set( USE_REALTIME_SHIFTS, YES );
00446 algorithm.set(LEVENBERG_MARQUARDT, 1e-5);
00447
00448
00449 DVector x0(10);
00450 x0(0) = 1.8264164528775887e+03;
00451 x0(1) = -5.1770453309520573e-03;
00452 x0(2) = 1.2706440287266794e+00;
00453 x0(3) = 2.1977888424944396e+00;
00454 x0(4) = 3.1840786108641383e-03;
00455 x0(5) = -3.8281200674676448e-02;
00456 x0(6) = 0.0000000000000000e+00;
00457 x0(7) = -1.0372313936413566e-02;
00458 x0(8) = 1.4999999999999616e+00;
00459 x0(9) = 0.0000000000000000e+00;
00460
00461
00462
00463
00464
00465 Controller controller( algorithm, reference );
00466
00467
00468
00469 double simulationStart = 0.0;
00470 double simulationEnd = 10.0;
00471
00472 SimulationEnvironment sim( simulationStart, simulationEnd, process, controller );
00473
00474 if (sim.init( x0 ) != SUCCESSFUL_RETURN)
00475 exit( EXIT_FAILURE );
00476 if (sim.run( ) != SUCCESSFUL_RETURN)
00477 exit( EXIT_FAILURE );
00478
00479
00480
00481
00482 VariablesGrid diffStates;
00483 sim.getProcessDifferentialStates( diffStates );
00484 diffStates.print( "diffStates.txt" );
00485 diffStates.print( "diffStates.m","DIFFSTATES",PS_MATLAB );
00486
00487 VariablesGrid interStates;
00488 sim.getProcessIntermediateStates( interStates );
00489 interStates.print( "interStates.txt" );
00490 interStates.print( "interStates.m","INTERSTATES",PS_MATLAB );
00491
00492 VariablesGrid sampledProcessOutput;
00493 sim.getSampledProcessOutput( sampledProcessOutput );
00494 sampledProcessOutput.print( "sampledOut.txt" );
00495 sampledProcessOutput.print( "sampledOut.m","OUT",PS_MATLAB );
00496
00497 VariablesGrid feedbackControl;
00498 sim.getFeedbackControl( feedbackControl );
00499 feedbackControl.print( "controls.txt" );
00500 feedbackControl.print( "controls.m","CONTROL",PS_MATLAB );
00501
00502 GnuplotWindow window;
00503 window.addSubplot( sampledProcessOutput(0), "DIFFERENTIAL STATE: r" );
00504 window.addSubplot( sampledProcessOutput(1), "DIFFERENTIAL STATE: phi" );
00505 window.addSubplot( sampledProcessOutput(2), "DIFFERENTIAL STATE: theta" );
00506 window.addSubplot( sampledProcessOutput(3), "DIFFERENTIAL STATE: dr" );
00507 window.addSubplot( sampledProcessOutput(4), "DIFFERENTIAL STATE: dphi" );
00508 window.addSubplot( sampledProcessOutput(5), "DIFFERENTIAL STATE: dtheta" );
00509 window.addSubplot( sampledProcessOutput(7), "DIFFERENTIAL STATE: Psi" );
00510 window.addSubplot( sampledProcessOutput(8), "DIFFERENTIAL STATE: CL" );
00511 window.addSubplot( sampledProcessOutput(9), "DIFFERENTIAL STATE: W" );
00512
00513 window.addSubplot( feedbackControl(0), "CONTROL 1 DDR0" );
00514 window.addSubplot( feedbackControl(1), "CONTROL 1 DPSI" );
00515 window.addSubplot( feedbackControl(2), "CONTROL 1 DCL" );
00516 window.plot( );
00517
00518 GnuplotWindow window2;
00519 window2.addSubplot( interStates(1) );
00520 window2.plot();
00521
00522 return 0;
00523 }
00524