dev_powerkite_on.cpp
Go to the documentation of this file.
00001 /*
00002  *    This file is part of ACADO Toolkit.
00003  *
00004  *    ACADO Toolkit -- A Toolkit for Automatic Control and Dynamic Optimization.
00005  *    Copyright (C) 2008-2014 by Boris Houska, Hans Joachim Ferreau,
00006  *    Milan Vukov, Rien Quirynen, KU Leuven.
00007  *    Developed within the Optimization in Engineering Center (OPTEC)
00008  *    under supervision of Moritz Diehl. All rights reserved.
00009  *
00010  *    ACADO Toolkit is free software; you can redistribute it and/or
00011  *    modify it under the terms of the GNU Lesser General Public
00012  *    License as published by the Free Software Foundation; either
00013  *    version 3 of the License, or (at your option) any later version.
00014  *
00015  *    ACADO Toolkit is distributed in the hope that it will be useful,
00016  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  *    Lesser General Public License for more details.
00019  *
00020  *    You should have received a copy of the GNU Lesser General Public
00021  *    License along with ACADO Toolkit; if not, write to the Free Software
00022  *    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
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 // DIFFERENTIAL STATES :
00043 // -------------------------
00044    DifferentialState      r;      //  the length r of the cable
00045    DifferentialState    phi;      //  the angle phi
00046    DifferentialState  theta;      //  the angle theta (grosser wert- naeher am boden/kleiner wert - weiter oben)
00047 // -------------------------      //  -------------------------------------------
00048    DifferentialState     dr;      //  first  derivative of r0    with respect to t
00049    DifferentialState   dphi;      //  first  derivative of phi   with respect to t
00050    DifferentialState dtheta;      //  first  derivative of theta with respect to t
00051 // -------------------------      //  -------------------------------------------
00052    DifferentialState      n;      //  winding number(rauslassen)
00053 // -------------------------      //  -------------------------------------------
00054    DifferentialState    Psi;      //  the roll angle Psi
00055    DifferentialState     CL;      //  the aerodynamic lift coefficient
00056 // -------------------------      //  -------------------------------------------
00057    DifferentialState      W;      //  integral over the power at the generator
00058                                   //  ( = ENERGY )(rauslassen)
00059 
00060 
00061    // MEASUREMENT FUNCTION :
00062 // -------------------------
00063    Function                   model_response         ;    // the measurement function
00064 
00065 
00066 // CONTROL :
00067 // -------------------------
00068    Control             ddr0;      //  second derivative of r0    with respect to t
00069    Control             dPsi;      //  first  derivative of Psi   with respect to t
00070    Control              dCL;      //  first  derivative of CL    with respect to t
00071 
00072 
00073    Disturbance       w_extra;
00074 
00075 
00076 // PARAMETERS :
00077 // ------------------------
00078                                      //  PARAMETERS OF THE KITE :
00079                                      //  -----------------------------
00080    double         mk =  2000.00;      //  mass of the kite               //  [ kg    ](maybe change to 5000kg)
00081    double          A =  500.00;      //  effective area                 //  [ m^2   ]
00082    double          V =  720.00;      //  volume                         //  [ m^3   ]
00083    double        cd0 =    0.04;      //  aerodynamic drag coefficient   //  [       ]
00084                                      //  ( cd0: without downwash )
00085    double          K =    0.04;      //  induced drag constant          //  [       ]
00086 
00087 
00088                                      //   PHYSICAL CONSTANTS :
00089                                      //  -----------------------------
00090    double          g =    9.81;      //  gravitational constant         //  [ m /s^2]
00091    double        rho =    1.23;      //  density of the air             //  [ kg/m^3]
00092 
00093                                      //  PARAMETERS OF THE CABLE :
00094                                      //  -----------------------------
00095    double       rhoc = 1450.00;      //  density of the cable           //  [ kg/m^3]
00096    double         cc =    1.00;      //  frictional constant            //  [       ]
00097    double         dc = 0.05614;      //  diameter                       //  [ m     ]
00098 
00099 
00100                                      //  PARAMETERS OF THE WIND :
00101                                      //  -----------------------------
00102    double         w0 =   10.00;      //  wind velocity at altitude h0   //  [ m/s   ]
00103    double         h0 =  100.00;      //  the altitude h0                //  [ m     ]
00104    double         hr =    0.10;      //  roughness length               //  [ m     ]
00105 
00106 
00107 
00108 // OTHER VARIABLES :
00109 // ------------------------
00110 
00111    double AQ                       ;      //  cross sectional area
00112 
00113    IntermediateState     mc;      //  mass of the cable
00114    IntermediateState     m ;      //  effective inertial mass
00115    IntermediateState     m_;      //  effective gravitational mass
00116 
00117    IntermediateState     Cg;
00118 
00119    IntermediateState     dm;      //  first  derivative of m     with respect to t
00120 
00121 
00122 // DEFINITION OF PI :
00123 // ------------------------
00124 
00125    double PI = 3.1415926535897932;
00126 
00127 
00128 // ORIENTATION AND FORCES :
00129 // ------------------------
00130 
00131    IntermediateState h               ;      //  altitude of the kite
00132    IntermediateState w               ;      //  the wind at altitude h
00133    IntermediateState we          [ 3];      //  effective wind vector
00134    IntermediateState nwe             ;      //  norm of the effective wind vector
00135    IntermediateState nwep            ;      //  -
00136    IntermediateState ewep        [ 3];      //  projection of ewe
00137    IntermediateState eta             ;      //  angle eta: cf. [2]
00138    IntermediateState et          [ 3];      //  unit vector from the left to the right wing tip                          
00139    IntermediateState Caer            ;
00140    IntermediateState Cf              ;      //  simple constants
00141    IntermediateState CD              ;      //  the aerodynamic drag coefficient
00142    IntermediateState Fg          [ 3];      //  the gravitational force
00143    IntermediateState Faer        [ 3];      //  the aerodynamic force
00144    IntermediateState Ff          [ 3];      //  the frictional force
00145    IntermediateState F           [ 3];      //  the total force
00146 
00147 
00148 // TERMS ON RIGHT-HAND-SIDE
00149 // OF THE DIFFERENTIAL
00150 // EQUATIONS              :
00151 // ------------------------
00152 
00153    IntermediateState a_pseudo    [ 3];      //  the pseudo accelaration
00154    IntermediateState dn              ;      //  the time derivate of the kite's winding number
00155    IntermediateState ddr             ;      //  second derivative of s     with respect to t
00156    IntermediateState ddphi           ;      //  second derivative of phi   with respect to t
00157    IntermediateState ddtheta         ;      //  second derivative of theta with respect to t
00158    IntermediateState power           ;      //  the power at the generator
00159 // ------------------------        ------                   //  ----------------------------------------------
00160    IntermediateState regularisation  ;      //  regularisation of controls
00161 
00162 
00163 
00164 //                        MODEL EQUATIONS :
00165 // ===============================================================
00166 
00167 
00168 
00169 // SPRING CONSTANT OF THE CABLE :
00170 // ---------------------------------------------------------------
00171 
00172    AQ      =  PI*dc*dc/4.0                                       ;
00173 
00174 // THE EFECTIVE MASS' :
00175 // ---------------------------------------------------------------
00176 
00177    mc      =  rhoc*AQ*r        ;   // mass of the cable
00178    m       =  mk + mc     / 3.0;   // effective inertial mass
00179    m_      =  mk + mc     / 2.0;   // effective gravitational mass
00180 // -----------------------------   // ----------------------------
00181    dm      =  (rhoc*AQ/ 3.0)*dr;   // time derivative of the mass
00182 
00183 
00184 // WIND SHEAR MODEL :
00185 // ---------------------------------------------------------------
00186 // for startup +60m
00187    h       =  r*cos(theta)+60.0                                       ;
00188 //    h       =  r*cos(theta)                                       ;
00189    w       =  log(h/hr) / log(h0/hr) *(w0+w_extra)                    ;
00190 
00191 
00192 // EFFECTIVE WIND IN THE KITE`S SYSTEM :
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 // CALCULATION OF THE KITE`S TRANSVERSAL AXIS :
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 // ewep[0] =  0.0                                                ;
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 // CONSTANTS FOR SEVERAL FORCES :
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 // THE DRAG-COEFFICIENT :
00231 // ---------------------------------------------------------------
00232 
00233    CD      =  cd0 + K*CL*CL                                      ;
00234 
00235 
00236 
00237 // SUM OF GRAVITATIONAL AND LIFTING FORCE :
00238 // ---------------------------------------------------------------
00239 
00240    Fg  [0] =  Cg  *  cos(theta)                                  ;
00241 // Fg  [1] =  Cg  *  0.0                                         ;
00242    Fg  [2] =  Cg  *  sin(theta)                                  ;
00243 
00244 
00245 // SUM OF THE AERODYNAMIC FORCES :
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 // THE FRICTION OF THE CABLE :
00254 // ---------------------------------------------------------------
00255 
00256 // Ff  [0] =  Cf  *  cc* we[0]                                   ;
00257    Ff  [1] =  Cf  *  cc* we[1]                                   ;
00258    Ff  [2] =  Cf  *  cc* we[2]                                   ;
00259 
00260 
00261 
00262 // THE TOTAL FORCE :
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 // THE PSEUDO ACCELERATION:
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 // THE EQUATIONS OF MOTION:
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 // THE DERIVATIVE OF THE WINDING NUMBER :
00302 // ---------------------------------------------------------------
00303 
00304    dn           =  (        dphi*ddtheta - dtheta*ddphi     ) /
00305                    (2.0*PI*(dphi*dphi    + dtheta*dtheta)   )      ;
00306 
00307 
00308 
00309 // THE POWER AT THE GENERATOR :
00310 // ---------------------------------------------------------------
00311 
00312    power        =   m*ddr*dr                                     ;
00313 
00314 
00315 
00316 // REGULARISATION TERMS :
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 // REFERENCE TRAJECTORY:
00333 // ---------------------------------------------------------------
00334         VariablesGrid myReference; myReference.read( "ref_w_zeros.txt" );// read the measurements
00335         PeriodicReferenceTrajectory reference( myReference );
00336 
00337 
00338 // THE "RIGHT-HAND-SIDE" OF THE ODE:
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                             ;    // the state r is measured
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     // DEFINE AN OPTIMAL CONTROL PROBLEM:
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 )   ;    // fit h to the data    
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 // SETTING UP THE (SIMULATED) PROCESS:
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     // SETUP OF THE ALGORITHM AND THE TUNING OPTIONS:
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 //      algorithm.set( USE_IMMEDIATE_FEEDBACK, YES );
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     // SETTING UP THE NMPC CONTROLLER:
00463     // -------------------------------
00464 
00465         Controller controller( algorithm, reference );
00466 
00467     // SETTING UP THE SIMULATION ENVIRONMENT,  RUN THE EXAMPLE...
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     // ...AND PLOT THE RESULTS
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 


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Thu Aug 27 2015 11:58:05