periodic_tracking.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 
44  // INTRODUCE THE VARIABLES:
45  // ------------------------
47  Control u;
48  Disturbance w;
49 
50 
51  // DEFINE A DIFFERENTIAL EQUATION:
52  // -------------------------------
54 
55  f << dot(x) == -2.0*x + u;
56  f2 << dot(x) == -2.0*x + u + 0.1*w; //- 0.000000000001*x*x
57 
58 
59  // DEFINE LEAST SQUARE FUNCTION:
60  // -----------------------------
61  Function h;
62 
63  h << x;
64  h << u;
65 
66  DMatrix Q(2,2);
67  Q.setIdentity();
68 
69  DVector r(2);
70  r.setAll( 0.0 );
71 
72 
73  // DEFINE AN OPTIMAL CONTROL PROBLEM:
74  // ----------------------------------
75  const double t_start = 0.0;
76  const double t_end = 7.0;
77 
78  OCP ocp ( t_start, t_end, 14 );
79  ocp.minimizeLSQ( Q, h, r );
80  ocp.subjectTo ( f );
81 
82  ocp.subjectTo( -1.0 <= u <= 2.0 );
83  //ocp.subjectTo( w == 0.0 );
84 
85 
86  // SETTING UP THE (SIMULATED) PROCESS:
87  // -----------------------------------
88  OutputFcn identity;
89  DynamicSystem dynamicSystem( f2,identity );
90  Process process( dynamicSystem,INT_RK45 );
91 
92 
93  VariablesGrid disturbance; disturbance.read( "my_disturbance.txt" );
94 
95 // GnuplotWindow window2;
96 // window2.addSubplot( disturbance, "my disturbance" );
97 // window2.plot();
98 
99  if (process.setProcessDisturbance( disturbance ) != SUCCESSFUL_RETURN)
100  exit( EXIT_FAILURE );
101 
102  // SETUP OF THE ALGORITHM AND THE TUNING OPTIONS:
103  // ----------------------------------------------
104  double samplingTime = 0.5;
105  RealTimeAlgorithm algorithm( ocp,samplingTime );
106 
107 // // algorithm.set( HESSIAN_APPROXIMATION, BLOCK_BFGS_UPDATE );
108  algorithm.set( HESSIAN_APPROXIMATION, GAUSS_NEWTON );
109 //
110 // // algorithm.set( ABSOLUTE_TOLERANCE , 1e-7 );
111 // // algorithm.set( INTEGRATOR_TOLERANCE, 1e-9 );
112 //
113 // algorithm.set( KKT_TOLERANCE, 1e-4 );
114 
115  algorithm.set( MAX_NUM_ITERATIONS,1 );
116  algorithm.set( USE_REALTIME_SHIFTS, YES );
117 // algorithm.set( USE_REALTIME_ITERATIONS,NO );
118 // algorithm.set( TERMINATE_AT_CONVERGENCE,YES );
119 
120 // algorithm.set( PRINTLEVEL,HIGH );
121 
122 
123  DVector x0(1);
124  x0(0) = 1.0;
125 
126 // // algorithm.solve( x0 );
127 //
128 // GnuplotWindow window1;
129 // window1.addSubplot( x, "DIFFERENTIAL STATE: x" );
130 // window1.addSubplot( u, "CONTROL: u" );
131 // window1.plot();
132 //
133 // return 0;
134 
135 
136  // SETTING UP THE NMPC CONTROLLER:
137  // -------------------------------
138 
139  VariablesGrid myReference; myReference.read( "my_reference.txt" );
140  PeriodicReferenceTrajectory reference( myReference );
141 
142 // GnuplotWindow window3;
143 // window3.addSubplot( myReference(1), "my reference" );
144 // window3.plot();
145 
146  Controller controller( algorithm,reference );
147 
148  // SETTING UP THE SIMULATION ENVIRONMENT, RUN THE EXAMPLE...
149  // ----------------------------------------------------------
150  double simulationStart = 0.0;
151  double simulationEnd = 15.0;
152 
153  SimulationEnvironment sim( simulationStart, simulationEnd, process, controller );
154 
155  if (sim.init( x0 ) != SUCCESSFUL_RETURN)
156  exit( EXIT_FAILURE );
157  if (sim.run( ) != SUCCESSFUL_RETURN)
158  exit( EXIT_FAILURE );
159 
160 
161  // ...AND PLOT THE RESULTS
162  // ----------------------------------------------------------
163  VariablesGrid sampledProcessOutput;
164  sim.getSampledProcessOutput( sampledProcessOutput );
165 
166  VariablesGrid feedbackControl;
167  sim.getFeedbackControl( feedbackControl );
168 
169  GnuplotWindow window;
170  window.addSubplot( sampledProcessOutput(0), "DIFFERENTIAL STATE: x" );
171  window.addSubplot( feedbackControl(0), "CONTROL: u" );
172  window.plot();
173 
174  return EXIT_SUCCESS;
175 }
176 
Calculates the control inputs of the Process based on the Process outputs.
Definition: controller.hpp:71
Allows to setup and evaluate a general function based on SymbolicExpressions.
Definition: function_.hpp:59
Allows to define a static periodic reference trajectory that the ControlLaw aims to track...
Allows to setup and evaluate output functions based on SymbolicExpressions.
Definition: output_fcn.hpp:55
virtual returnValue plot(PlotFrequency _frequency=PLOT_IN_ANY_CASE)
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
User-interface to formulate and solve model predictive control problems.
returnValue subjectTo(const DifferentialEquation &differentialEquation_)
Definition: ocp.cpp:153
returnValue addSubplot(PlotWindowSubplot &_subplot)
returnValue getSampledProcessOutput(VariablesGrid &_sampledProcessOutput)
returnValue set(OptionsName name, int value)
Definition: options.cpp:126
returnValue minimizeLSQ(const DMatrix &S, const Function &h, const DVector &r)
Definition: ocp.cpp:244
returnValue init(const DVector &x0_, const DVector &p_=emptyConstVector)
#define YES
Definition: acado_types.hpp:51
Data class for defining optimal control problems.
Definition: ocp.hpp:89
Expression dot(const Expression &arg)
returnValue read(std::istream &stream)
const double t_end
returnValue setProcessDisturbance(const Curve &_processDisturbance)
Definition: process.cpp:289
const double t_start
void setAll(const T &_value)
Definition: vector.hpp:160
Allows to run closed-loop simulations of dynamic systems.
Simulates the process to be controlled based on a dynamic model.
Definition: process.hpp:71
Provides an interface to Gnuplot for plotting algorithmic outputs.
int main()
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:58