dev_dae_simulation.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     // INTRODUCE THE VARIABLES:
00044     // -------------------------
00045     DifferentialState         x;
00046     DifferentialState         l;
00047     AlgebraicState            z;
00048     Control                   u;
00049     DifferentialEquation      f;
00050 //     Disturbance R;
00051 
00052 
00053     // DEFINE A DIFFERENTIAL EQUATION:
00054     // -------------------------------
00055     f << dot(x) == -x + 0.5*x*x + u + 0.5*z  ;
00056     f << dot(l) ==  x*x + 3.0*u*u         ;
00057     f <<      0 ==  z + exp(z) - 1.0 + x     ;
00058 
00059 
00060     // DEFINE AN OPTIMAL CONTROL PROBLEM:
00061     // ----------------------------------
00062     OCP ocp( 0.0, 5.0, 10 );
00063     ocp.minimizeMayerTerm( l );
00064 
00065     ocp.subjectTo( f );
00066 //     ocp.subjectTo( R == 0.0 );
00067 
00068 
00069     // SETTING UP THE (SIMULATED) PROCESS:
00070     // -----------------------------------
00071         OutputFcn identity;
00072         DynamicSystem dynamicSystem( f,identity );
00073 
00074         Process process( dynamicSystem,INT_BDF );
00075 
00076         //VariablesGrid disturbance = readFromFile( "dae_simulation_disturbance.txt" );
00077         //process.setProcessDisturbance( disturbance );
00078 
00079 
00080     // SETTING UP THE MPC CONTROLLER:
00081     // ------------------------------
00082         RealTimeAlgorithm alg( ocp,0.5 );
00083 
00084         StaticReferenceTrajectory zeroReference;
00085         Controller controller( alg,zeroReference );
00086 
00087 
00088     // SETTING UP THE SIMULATION ENVIRONMENT,  RUN THE EXAMPLE...
00089     // ----------------------------------------------------------
00090         SimulationEnvironment sim( 0.0,15.0,process,controller );
00091 
00092         DVector x0(2);
00093         x0(0) = 1;
00094         x0(1) = 0;
00095 
00096         sim.init( x0 );
00097         sim.run( );
00098 
00099 
00100     // ...AND PLOT THE RESULTS
00101     // ----------------------------------------------------------
00102         VariablesGrid diffStates;
00103         sim.getProcessDifferentialStates( diffStates );
00104         diffStates.printToFile( "diffStates.txt" );
00105         diffStates.printToFile( "diffStates.m","DIFFSTATES",PS_MATLAB );
00106 
00107         VariablesGrid sampledProcessOutput;
00108     sim.getSampledProcessOutput( sampledProcessOutput );
00109     sampledProcessOutput.printToFile( "sampledOut.txt" );
00110     sampledProcessOutput.printToFile( "sampledOut.m","OUT",PS_MATLAB );
00111 
00112     VariablesGrid feedbackControl;
00113     sim.getFeedbackControl( feedbackControl );
00114         feedbackControl.printToFile( "controls.txt" );
00115         feedbackControl.printToFile( "controls.m","CONTROL",PS_MATLAB );
00116 
00117         VariablesGrid algStates;
00118         sim.getProcessAlgebraicStates( algStates );
00119         algStates.printToFile( "algStates.txt" );
00120         algStates.printToFile( "algStates.m","ALGSTATES",PS_MATLAB );
00121 
00122 
00123     GnuplotWindow window;
00124                 window.addSubplot( diffStates(0), "DIFFERENTIAL STATE: x" );
00125                 window.addSubplot( diffStates(1), "DIFFERENTIAL STATE: l" );
00126                 window.addSubplot( algStates(0),            "ALGEBRAIC STATE: z"    );
00127                 window.addSubplot( feedbackControl(0),      "CONTRUL: u"            );
00128     window.plot( );
00129 
00130 
00131     return 0;
00132 }
00133 
00134 
00135 


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