examples/simulation_environment/getting_started.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 
34 #include <acado_toolkit.hpp>
35 #include <acado_gnuplot.hpp>
36 
37 
38 int main( )
39 {
41 
42 
43  // INTRODUCE THE VARIABLES:
44  // -------------------------
45  DifferentialState xB; //Body Position
46  DifferentialState xW; //Wheel Position
47  DifferentialState vB; //Body Velocity
48  DifferentialState vW; //Wheel Velocity
49 
50  Disturbance R;
51  Control F;
52 
53  double mB = 350.0;
54  double mW = 50.0;
55  double kS = 20000.0;
56  double kT = 200000.0;
57 
58 
59  // DEFINE A DIFFERENTIAL EQUATION:
60  // -------------------------------
62 
63  f << dot(xB) == vB;
64  f << dot(xW) == vW;
65  f << dot(vB) == ( -kS*xB + kS*xW + F ) / mB;
66  f << dot(vW) == ( kS*xB - (kT+kS)*xW + kT*R - F ) / mW;
67 
68 
69  // SETTING UP THE (SIMULATED) PROCESS:
70  // -----------------------------------
71  OutputFcn identity;
72  DynamicSystem dynamicSystem( f,identity );
73 
74  Process process( dynamicSystem,INT_RK45 );
75 
76  VariablesGrid disturbance; disturbance.read( "road.txt" );
77  if (process.setProcessDisturbance( disturbance ) != SUCCESSFUL_RETURN)
78  exit( EXIT_FAILURE );
79 
80  // DEFINE AN OPTIMAL CONTROL PROBLEM:
81  // ----------------------------------
82  Function h;
83 
84  h << xB;
85  h << xW;
86  h << vB;
87  h << vW;
88  h << F;
89 
90  DMatrix Q = zeros<double>(5,5); // LSQ coefficient matrix
91  Q(0,0) = 10.0;
92  Q(1,1) = 10.0;
93  Q(2,2) = 1.0;
94  Q(3,3) = 1.0;
95  Q(4,4) = 1.0e-8;
96 
97  DVector r(5); // Reference
98  r.setAll( 0.0 );
99 
100 
101  const double tStart = 0.0;
102  const double tEnd = 1.0;
103 
104  OCP ocp( tStart, tEnd, 20 );
105 
106  ocp.minimizeLSQ( Q, h, r );
107 
108  ocp.subjectTo( f );
109 
110  ocp.subjectTo( -200.0 <= F <= 200.0 );
111  ocp.subjectTo( R == 0.0 );
112 
113 
114  // SETTING UP THE MPC CONTROLLER:
115  // ------------------------------
116  RealTimeAlgorithm alg( ocp,0.05 );
117  alg.set( INTEGRATOR_TYPE, INT_RK78 );
119 // alg.set( MAX_NUM_ITERATIONS, 2 );
120 // alg.set( USE_IMMEDIATE_FEEDBACK,YES );
121 
122  StaticReferenceTrajectory zeroReference;
123 
124  Controller controller( alg,zeroReference );
125 
126 
127  // SETTING UP THE SIMULATION ENVIRONMENT, RUN THE EXAMPLE...
128  // ----------------------------------------------------------
129  SimulationEnvironment sim( 0.0,2.5,process,controller );
130 
131  DVector x0(4);
132  x0.setZero();
133 
134  if (sim.init( x0 ) != SUCCESSFUL_RETURN)
135  exit( EXIT_FAILURE );
136  if (sim.run( ) != SUCCESSFUL_RETURN)
137  exit( EXIT_FAILURE );
138 
139 
140  // ... AND PLOT THE RESULTS
141  // ------------------------
142  VariablesGrid diffStates;
143  sim.getProcessDifferentialStates( diffStates );
144 
145  VariablesGrid feedbackControl;
146  sim.getFeedbackControl( feedbackControl );
147 
148  GnuplotWindow window;
149  window.addSubplot( diffStates(0), "Body Position [m]" );
150  window.addSubplot( diffStates(1), "Wheel Position [m]" );
151  window.addSubplot( diffStates(2), "Body Velocity [m/s]" );
152  window.addSubplot( diffStates(3), "Wheel Velocity [m/s]" );
153  window.addSubplot( feedbackControl, "Damping Force [N]" );
154  window.addSubplot( disturbance, "Road Excitation [m]" );
155  window.plot( );
156 
157  return EXIT_SUCCESS;
158 }
159 
160 
161 
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)
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
User-interface to formulate and solve model predictive control problems.
returnValue subjectTo(const DifferentialEquation &differentialEquation_)
Definition: ocp.cpp:153
returnValue addSubplot(PlotWindowSubplot &_subplot)
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)
Derived & setZero(Index size)
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)
returnValue read(std::istream &stream)
returnValue setProcessDisturbance(const Curve &_processDisturbance)
Definition: process.cpp:289
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:34:39