dev_dae_simulation.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ACADO Toolkit.
3  *
4  * ACADO Toolkit -- A Toolkit for Automatic Control and Dynamic Optimization.
5  * Copyright (C) 2008-2014 by Boris Houska, Hans Joachim Ferreau,
6  * Milan Vukov, Rien Quirynen, KU Leuven.
7  * Developed within the Optimization in Engineering Center (OPTEC)
8  * under supervision of Moritz Diehl. All rights reserved.
9  *
10  * ACADO Toolkit is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Lesser General Public
12  * License as published by the Free Software Foundation; either
13  * version 3 of the License, or (at your option) any later version.
14  *
15  * ACADO Toolkit is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with ACADO Toolkit; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23  *
24  */
25 
26 
27 
35 #include <acado_toolkit.hpp>
36 #include <acado_gnuplot.hpp>
37 
38 
39 int main( ){
40 
42 
43  // INTRODUCE THE VARIABLES:
44  // -------------------------
48  Control u;
50 // Disturbance R;
51 
52 
53  // DEFINE A DIFFERENTIAL EQUATION:
54  // -------------------------------
55  f << dot(x) == -x + 0.5*x*x + u + 0.5*z ;
56  f << dot(l) == x*x + 3.0*u*u ;
57  f << 0 == z + exp(z) - 1.0 + x ;
58 
59 
60  // DEFINE AN OPTIMAL CONTROL PROBLEM:
61  // ----------------------------------
62  OCP ocp( 0.0, 5.0, 10 );
63  ocp.minimizeMayerTerm( l );
64 
65  ocp.subjectTo( f );
66 // ocp.subjectTo( R == 0.0 );
67 
68 
69  // SETTING UP THE (SIMULATED) PROCESS:
70  // -----------------------------------
71  OutputFcn identity;
72  DynamicSystem dynamicSystem( f,identity );
73 
74  Process process( dynamicSystem,INT_BDF );
75 
76  //VariablesGrid disturbance = readFromFile( "dae_simulation_disturbance.txt" );
77  //process.setProcessDisturbance( disturbance );
78 
79 
80  // SETTING UP THE MPC CONTROLLER:
81  // ------------------------------
82  RealTimeAlgorithm alg( ocp,0.5 );
83 
84  StaticReferenceTrajectory zeroReference;
85  Controller controller( alg,zeroReference );
86 
87 
88  // SETTING UP THE SIMULATION ENVIRONMENT, RUN THE EXAMPLE...
89  // ----------------------------------------------------------
90  SimulationEnvironment sim( 0.0,15.0,process,controller );
91 
92  DVector x0(2);
93  x0(0) = 1;
94  x0(1) = 0;
95 
96  sim.init( x0 );
97  sim.run( );
98 
99 
100  // ...AND PLOT THE RESULTS
101  // ----------------------------------------------------------
102  VariablesGrid diffStates;
103  sim.getProcessDifferentialStates( diffStates );
104  diffStates.printToFile( "diffStates.txt" );
105  diffStates.printToFile( "diffStates.m","DIFFSTATES",PS_MATLAB );
106 
107  VariablesGrid sampledProcessOutput;
108  sim.getSampledProcessOutput( sampledProcessOutput );
109  sampledProcessOutput.printToFile( "sampledOut.txt" );
110  sampledProcessOutput.printToFile( "sampledOut.m","OUT",PS_MATLAB );
111 
112  VariablesGrid feedbackControl;
113  sim.getFeedbackControl( feedbackControl );
114  feedbackControl.printToFile( "controls.txt" );
115  feedbackControl.printToFile( "controls.m","CONTROL",PS_MATLAB );
116 
117  VariablesGrid algStates;
118  sim.getProcessAlgebraicStates( algStates );
119  algStates.printToFile( "algStates.txt" );
120  algStates.printToFile( "algStates.m","ALGSTATES",PS_MATLAB );
121 
122 
123  GnuplotWindow window;
124  window.addSubplot( diffStates(0), "DIFFERENTIAL STATE: x" );
125  window.addSubplot( diffStates(1), "DIFFERENTIAL STATE: l" );
126  window.addSubplot( algStates(0), "ALGEBRAIC STATE: z" );
127  window.addSubplot( feedbackControl(0), "CONTRUL: u" );
128  window.plot( );
129 
130 
131  return 0;
132 }
133 
134 
135 
Calculates the control inputs of the Process based on the Process outputs.
Definition: controller.hpp:71
Allows to setup and evaluate output functions based on SymbolicExpressions.
Definition: output_fcn.hpp:55
virtual returnValue plot(PlotFrequency _frequency=PLOT_IN_ANY_CASE)
returnValue getProcessDifferentialStates(VariablesGrid &_diffStates)
Stores a DifferentialEquation together with an OutputFcn.
#define USING_NAMESPACE_ACADO
Provides a time grid consisting of vector-valued optimization variables at each grid point...
returnValue getFeedbackControl(Curve &_feedbackControl) const
returnValue getProcessAlgebraicStates(VariablesGrid &_algStates)
User-interface to formulate and solve model predictive control problems.
returnValue subjectTo(const DifferentialEquation &differentialEquation_)
Definition: ocp.cpp:153
returnValue minimizeMayerTerm(const Expression &arg)
Definition: ocp.cpp:238
returnValue addSubplot(PlotWindowSubplot &_subplot)
returnValue getSampledProcessOutput(VariablesGrid &_sampledProcessOutput)
returnValue init(const DVector &x0_, const DVector &p_=emptyConstVector)
Data class for defining optimal control problems.
Definition: ocp.hpp:89
Allows to define a static reference trajectory that the ControlLaw aims to track. ...
Expression dot(const Expression &arg)
Allows to run closed-loop simulations of dynamic systems.
Simulates the process to be controlled based on a dynamic model.
Definition: process.hpp:71
int main()
IntermediateState exp(const Expression &arg)
Provides an interface to Gnuplot for plotting algorithmic outputs.
Allows to setup and evaluate differential equations (ODEs and DAEs) based on SymbolicExpressions.


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Mon Jun 10 2019 12:34:32