crane_mpc3.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 
00118         RealTimeAlgorithm alg( ocp,0.1 );
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 
00127     // SETTING UP THE SIMULATION ENVIRONMENT,  RUN THE EXAMPLE...
00128     // ----------------------------------------------------------
00129         SimulationEnvironment sim( 0.0,20.0,process,controller );
00130 
00131         DVector x0(4);
00132         x0.setZero();
00133         x0(3) = 5.0;
00134 
00135         if (sim.init( x0 ) != SUCCESSFUL_RETURN)
00136                 exit( EXIT_FAILURE );
00137         if (sim.run( ) != SUCCESSFUL_RETURN)
00138                 exit( EXIT_FAILURE );
00139 
00140     // ...AND PLOT THE RESULTS
00141     // ----------------------------------------------------------
00142         VariablesGrid diffStates;
00143         if (sim.getProcessDifferentialStates( diffStates ) != SUCCESSFUL_RETURN)
00144                 exit( EXIT_FAILURE );
00145 
00146         VariablesGrid feedbackControl;
00147         if (sim.getFeedbackControl( feedbackControl ) != SUCCESSFUL_RETURN)
00148                 exit( EXIT_FAILURE );
00149 
00150         GnuplotWindow window;
00151                 window.addSubplot( diffStates(0),   "POSITION OF THE TROLLEY" );
00152                 window.addSubplot( diffStates(1),   "VELOCITY OF THE TROLLEY" );
00153                 window.addSubplot( diffStates(2),   "PHI" );
00154                 window.addSubplot( diffStates(3),   "DPHI" );
00155                 window.addSubplot( feedbackControl, "Accelaration [m/s^2]" );
00156         //      window.addSubplot( disturbance,     "Disturbance [m/s^2]" );
00157         window.plot();
00158         
00159         diffStates.print( "diffStates.txt" );
00160 
00161 
00162     return EXIT_SUCCESS;
00163 }
00164 
00165 /* <<< end tutorial code <<< */


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