crane_mpc3_stepped.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 
00027 
00036 #include <acado_toolkit.hpp>
00037 #include <acado_gnuplot.hpp>
00038 
00039 
00040 int main( ){
00041 
00042     USING_NAMESPACE_ACADO;
00043 
00044     // VARIABLES:
00045     // ------------------------
00046     DifferentialState        x;   // Position of the trolley
00047     DifferentialState        v;   // Velocity of the trolley
00048     DifferentialState      phi;   // excitation angle
00049     DifferentialState     dphi;   // rotational velocity
00050 
00051         Control                                 ax;   // trolley accelaration
00052         Disturbance                      W;   // disturbance
00053 
00054     double L = 1.0 ;              // length
00055         double m = 1.0 ;              // mass
00056         double g = 9.81;              // gravitational constant
00057         double b = 0.2 ;              // friction coefficient
00058 
00059 
00060     // DIFFERENTIAL EQUATION:
00061     // ------------------------
00062     DifferentialEquation     f, fSim;   // The model equations
00063 
00064     f << dot(x) ==  v;
00065     f << dot(v) ==  ax;
00066     f << dot(phi ) == dphi;
00067     f << dot(dphi) == -g/L*sin(phi) -ax/L*cos(phi) - b/(m*L*L)*dphi;
00068 
00069         L = 1.2;                                                        // introduce model plant mismatch
00070         
00071         fSim << dot(x) ==  v;
00072         fSim << dot(v) ==  ax + W;
00073         fSim << dot(phi ) == dphi;
00074         fSim << dot(dphi) == -g/L*sin(phi) -ax/L*cos(phi) - b/(m*L*L)*dphi;
00075         
00076 
00077     // DEFINE LEAST SQUARE FUNCTION:
00078     // -----------------------------
00079     Function h;
00080 
00081     h << x;
00082     h << v;
00083     h << phi;
00084     h << dphi;
00085 
00086     DMatrix Q(4,4); // LSQ coefficient matrix
00087     Q.setIdentity();
00088 
00089     DVector r(4); // Reference
00090 
00091 
00092     // DEFINE AN OPTIMAL CONTROL PROBLEM:
00093     // ----------------------------------
00094     const double t_start = 0.0;
00095     const double t_end   = 5.0;
00096 
00097     OCP ocp( t_start, t_end, 25 );
00098 
00099     ocp.minimizeLSQ( Q, h, r );
00100     ocp.subjectTo( f );
00101     ocp.subjectTo( -5.0 <= ax <= 5.0 );
00102 
00103 
00104     // SETTING UP THE (SIMULATED) PROCESS:
00105     // -----------------------------------
00106         OutputFcn identity;
00107         DynamicSystem dynamicSystem( fSim,identity );
00108 
00109         Process process( dynamicSystem,INT_RK45 );
00110 
00111         VariablesGrid disturbance; disturbance.read( "dist.txt" );
00112         if (process.setProcessDisturbance( disturbance ) != SUCCESSFUL_RETURN)
00113                 exit( EXIT_FAILURE );
00114 
00115     // SETTING UP THE MPC CONTROLLER:
00116     // ------------------------------
00117     double samplingTime = 0.1;
00118         RealTimeAlgorithm alg( ocp, samplingTime );
00119 //      alg.set( USE_REALTIME_ITERATIONS,NO );
00120 //      alg.set( MAX_NUM_ITERATIONS,20 );
00121 
00122         StaticReferenceTrajectory zeroReference;
00123 
00124         Controller controller( alg, zeroReference );
00125         
00126         DVector x0(4);
00127         x0.setZero();
00128         x0(3) = 1.0;
00129 
00130         double startTime =  0.0;
00131         double endTime   = 20.0;
00132 
00133 
00134         DVector uCon;
00135         VariablesGrid ySim;
00136         
00137         if (controller.init( startTime,x0 ) != SUCCESSFUL_RETURN)
00138                 exit( EXIT_FAILURE );
00139         controller.getU( uCon );
00140         
00141         if (process.init( startTime,x0,uCon ) != SUCCESSFUL_RETURN)
00142                 exit( EXIT_FAILURE );
00143         process.getY( ySim );
00144 
00145 
00146         //      hand-coding call to 
00147         //      sim.run( )
00148 
00149         int nSteps = 0;
00150         double currentTime = startTime;
00151 
00152         while ( currentTime <= endTime )
00153         {
00154                 printf( "\n*** Simulation Loop No. %d (starting at time %.3f) ***\n",nSteps,currentTime );
00155         
00156                 if (controller.step( currentTime,ySim.getLastVector() ) != SUCCESSFUL_RETURN)
00157                         exit( EXIT_FAILURE );
00158                 controller.getU( uCon );
00159                 
00160                 if (process.step( currentTime,currentTime+samplingTime,uCon ) != SUCCESSFUL_RETURN)
00161                         exit( EXIT_FAILURE );
00162                 process.getY( ySim );
00163                 
00164                 ++nSteps;
00165                 currentTime = (double)nSteps * samplingTime;
00166         }
00167 
00168     return EXIT_SUCCESS;
00169 }
00170 
00171 /* <<< end tutorial code <<< */


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