examples/getting_started/simple_mpc.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 using namespace std;
39 
41 
42 int main( )
43 {
44  // INTRODUCE THE VARIABLES:
45  // -------------------------
50 
51  Control R;
52  Control F;
53 
54  double mB = 350.0;
55  double mW = 50.0;
56  double kS = 20000.0;
57  double kT = 200000.0;
58 
59 
60  // DEFINE A DIFFERENTIAL EQUATION:
61  // -------------------------------
63 
64  f << dot(xB) == vB;
65  f << dot(xW) == vW;
66  f << dot(vB) == ( -kS*xB + kS*xW + F ) / mB;
67  f << dot(vW) == ( kS*xB - (kT+kS)*xW + kT*R - F ) / mW;
68 
69 
70  // DEFINE LEAST SQUARE FUNCTION:
71  // -----------------------------
72  Function h;
73 
74  h << xB;
75  h << xW;
76  h << vB;
77  h << vW;
78 
79  DMatrix Q(4,4);
80  Q.setIdentity();
81  Q(0,0) = 10.0;
82  Q(1,1) = 10.0;
83 
84  DVector r(4);
85  r.setAll( 0.0 );
86 
87 
88  // DEFINE AN OPTIMAL CONTROL PROBLEM:
89  // ----------------------------------
90  const double t_start = 0.0;
91  const double t_end = 1.0;
92 
93  OCP ocp( t_start, t_end, 20 );
94 
95  ocp.minimizeLSQ( Q, h, r );
96 
97  ocp.subjectTo( f );
98 
99  ocp.subjectTo( -500.0 <= F <= 500.0 );
100  ocp.subjectTo( R == 0.0 );
101 
102 
103 
104  // SETTING UP THE (SIMULATED) PROCESS:
105  // -----------------------------------
106  OutputFcn identity;
107  DynamicSystem dynamicSystem( f,identity );
108 
109  Process process( dynamicSystem,INT_RK45 );
110 
111  // SETTING UP THE MPC CONTROLLER:
112  // ------------------------------
113  RealTimeAlgorithm alg( ocp,0.05 );
114  alg.set( MAX_NUM_ITERATIONS, 2 );
115 
116  StaticReferenceTrajectory zeroReference;
117 
118  Controller controller( alg,zeroReference );
119 
120 
121  // SETTING UP THE SIMULATION ENVIRONMENT, RUN THE EXAMPLE...
122  // ----------------------------------------------------------
123  SimulationEnvironment sim( 0.0,3.0,process,controller );
124 
125  DVector x0(4);
126  x0(0) = 0.01;
127  x0(1) = 0.0;
128  x0(2) = 0.0;
129  x0(3) = 0.0;
130 
131  if (sim.init( x0 ) != SUCCESSFUL_RETURN)
132  exit( EXIT_FAILURE );
133  if (sim.run( ) != SUCCESSFUL_RETURN)
134  exit( EXIT_FAILURE );
135 
136  // ...AND PLOT THE RESULTS
137  // ----------------------------------------------------------
138  VariablesGrid sampledProcessOutput;
139  sim.getSampledProcessOutput( sampledProcessOutput );
140 
141  VariablesGrid feedbackControl;
142  sim.getFeedbackControl( feedbackControl );
143 
144  GnuplotWindow window;
145  window.addSubplot( sampledProcessOutput(0), "Body Position [m]" );
146  window.addSubplot( sampledProcessOutput(1), "Wheel Position [m]" );
147  window.addSubplot( sampledProcessOutput(2), "Body Velocity [m/s]" );
148  window.addSubplot( sampledProcessOutput(3), "Wheel Velocity [m/s]" );
149  window.addSubplot( feedbackControl(1), "Damping Force [N]" );
150  window.addSubplot( feedbackControl(0), "Road Excitation [m]" );
151  window.plot( );
152 
153  return EXIT_SUCCESS;
154 }
155 
156 
157 
USING_NAMESPACE_ACADO int main()
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 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)
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)
const double t_end
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.
#define R
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:35:04