periodic_tracking.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 
00035 #include <acado_toolkit.hpp>
00036 #include <acado_gnuplot.hpp>
00037 
00038 
00039 int main( ){
00040 
00041     USING_NAMESPACE_ACADO
00042 
00043 
00044     // INTRODUCE THE VARIABLES:
00045     // ------------------------
00046     DifferentialState x;
00047     Control           u;
00048     Disturbance       w;
00049 
00050 
00051     // DEFINE A DIFFERENTIAL EQUATION:
00052     // -------------------------------
00053     DifferentialEquation f, f2;
00054 
00055     f  << dot(x) == -2.0*x + u;
00056     f2 << dot(x) == -2.0*x + u + 0.1*w; //- 0.000000000001*x*x
00057 
00058 
00059     // DEFINE LEAST SQUARE FUNCTION:
00060     // -----------------------------
00061     Function h;
00062 
00063     h << x;
00064     h << u;
00065 
00066     DMatrix Q(2,2);
00067     Q.setIdentity();
00068 
00069     DVector r(2);
00070     r.setAll( 0.0 );
00071 
00072 
00073     // DEFINE AN OPTIMAL CONTROL PROBLEM:
00074     // ----------------------------------
00075     const double t_start = 0.0;
00076     const double t_end   = 7.0;
00077 
00078     OCP ocp        ( t_start, t_end, 14 );
00079     ocp.minimizeLSQ( Q, h, r );
00080     ocp.subjectTo  ( f );
00081 
00082     ocp.subjectTo( -1.0 <= u <= 2.0 );
00083     //ocp.subjectTo(  w == 0.0 );
00084 
00085 
00086     // SETTING UP THE (SIMULATED) PROCESS:
00087     // -----------------------------------
00088     OutputFcn identity;
00089     DynamicSystem dynamicSystem( f2,identity );
00090     Process process( dynamicSystem,INT_RK45 );
00091 
00092 
00093         VariablesGrid disturbance; disturbance.read( "my_disturbance.txt" );
00094 
00095 //      GnuplotWindow window2;
00096 //              window2.addSubplot( disturbance, "my disturbance"   );
00097 //      window2.plot();
00098 
00099         if (process.setProcessDisturbance( disturbance ) != SUCCESSFUL_RETURN)
00100                 exit( EXIT_FAILURE );
00101 
00102     // SETUP OF THE ALGORITHM AND THE TUNING OPTIONS:
00103     // ----------------------------------------------
00104         double samplingTime = 0.5;
00105     RealTimeAlgorithm  algorithm( ocp,samplingTime );
00106 
00107 // //  algorithm.set( HESSIAN_APPROXIMATION, BLOCK_BFGS_UPDATE );
00108      algorithm.set( HESSIAN_APPROXIMATION, GAUSS_NEWTON );
00109 // 
00110 // //     algorithm.set( ABSOLUTE_TOLERANCE  , 1e-7 );
00111 // //     algorithm.set( INTEGRATOR_TOLERANCE, 1e-9 );
00112 // 
00113 //     algorithm.set( KKT_TOLERANCE, 1e-4 );
00114 
00115         algorithm.set( MAX_NUM_ITERATIONS,1 );
00116         algorithm.set( USE_REALTIME_SHIFTS, YES );
00117 //      algorithm.set( USE_REALTIME_ITERATIONS,NO );
00118 //      algorithm.set( TERMINATE_AT_CONVERGENCE,YES );
00119 
00120 //      algorithm.set( PRINTLEVEL,HIGH );
00121 
00122 
00123     DVector x0(1);
00124     x0(0)  = 1.0;
00125 
00126 // //   algorithm.solve( x0 );
00127 // 
00128 //     GnuplotWindow window1;
00129 //         window1.addSubplot( x, "DIFFERENTIAL STATE: x" );
00130 //         window1.addSubplot( u, "CONTROL: u" );
00131 //     window1.plot();
00132 // 
00133 //      return 0;
00134 
00135 
00136     // SETTING UP THE NMPC CONTROLLER:
00137     // -------------------------------
00138 
00139     VariablesGrid myReference; myReference.read( "my_reference.txt" );
00140     PeriodicReferenceTrajectory reference( myReference );
00141 
00142 //      GnuplotWindow window3;
00143 //              window3.addSubplot( myReference(1), "my reference"   );
00144 //      window3.plot();
00145         
00146     Controller controller( algorithm,reference );
00147 
00148     // SETTING UP THE SIMULATION ENVIRONMENT,  RUN THE EXAMPLE...
00149     // ----------------------------------------------------------
00150     double simulationStart =  0.0;
00151     double simulationEnd   =  15.0;
00152 
00153     SimulationEnvironment sim( simulationStart, simulationEnd, process, controller );
00154 
00155     if (sim.init( x0 ) != SUCCESSFUL_RETURN)
00156         exit( EXIT_FAILURE );
00157     if (sim.run( ) != SUCCESSFUL_RETURN)
00158         exit( EXIT_FAILURE );
00159 
00160 
00161     // ...AND PLOT THE RESULTS
00162     // ----------------------------------------------------------
00163     VariablesGrid sampledProcessOutput;
00164     sim.getSampledProcessOutput( sampledProcessOutput );
00165 
00166     VariablesGrid feedbackControl;
00167     sim.getFeedbackControl( feedbackControl );
00168 
00169     GnuplotWindow window;
00170         window.addSubplot( sampledProcessOutput(0), "DIFFERENTIAL STATE: x" );
00171         window.addSubplot( feedbackControl(0),      "CONTROL: u" );
00172     window.plot();
00173 
00174     return EXIT_SUCCESS;
00175 }
00176 


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