examples/controller/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 
35 #include <acado_toolkit.hpp>
36 #include <acado_gnuplot.hpp>
37 
38 
39 int main( )
40 {
42 
43 
44  // INTRODUCE THE VARIABLES:
45  // -------------------------
46  DifferentialState xB; //Body Position
47  DifferentialState xW; //Wheel Position
48  DifferentialState vB; //Body Velocity
49  DifferentialState vW; //Wheel Velocity
50 
51  Control F;
52  Disturbance R;
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  h << F;
79 
80  DMatrix Q(5,5); // LSQ coefficient matrix
81  Q(0,0) = 10.0;
82  Q(1,1) = 10.0;
83  Q(2,2) = 1.0;
84  Q(3,3) = 1.0;
85  Q(4,4) = 1.0e-8;
86 
87  DVector r(5); // Reference
88  r.setAll( 0.0 );
89 
90 
91  // DEFINE AN OPTIMAL CONTROL PROBLEM:
92  // ----------------------------------
93  const double tStart = 0.0;
94  const double tEnd = 1.0;
95 
96  OCP ocp( tStart, tEnd, 20 );
97 
98  ocp.minimizeLSQ( Q, h, r );
99 
100  ocp.subjectTo( f );
101 
102  ocp.subjectTo( -200.0 <= F <= 200.0 );
103  ocp.subjectTo( R == 0.0 );
104 
105 
106  // SETTING UP THE REAL-TIME ALGORITHM:
107  // -----------------------------------
108  RealTimeAlgorithm alg( ocp,0.025 );
109  alg.set( MAX_NUM_ITERATIONS, 1 );
110  alg.set( PLOT_RESOLUTION, MEDIUM );
111 
112  GnuplotWindow window;
113  window.addSubplot( xB, "Body Position [m]" );
114  window.addSubplot( xW, "Wheel Position [m]" );
115  window.addSubplot( vB, "Body Velocity [m/s]" );
116  window.addSubplot( vW, "Wheel Velocity [m/s]" );
117  window.addSubplot( F, "Damping Force [N]" );
118  window.addSubplot( R, "Road Excitation [m]" );
119 
120  alg << window;
121 
122 
123  // SETUP CONTROLLER AND PERFORM A STEP:
124  // ------------------------------------
125  StaticReferenceTrajectory zeroReference( "ref.txt" );
126 
127  Controller controller( alg,zeroReference );
128 
129  DVector y( 4 );
130  y.setZero( );
131  y(0) = 0.01;
132 
133  if (controller.init( 0.0,y ) != SUCCESSFUL_RETURN)
134  exit( 1 );
135  if (controller.step( 0.0,y ) != SUCCESSFUL_RETURN)
136  exit( 1 );
137 
138  return EXIT_SUCCESS;
139 }
140 
141 
142 
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
#define USING_NAMESPACE_ACADO
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
virtual returnValue step(double currentTime, const DVector &_y, const VariablesGrid &_yRef=emptyConstVariablesGrid)
Definition: controller.cpp:338
virtual returnValue init(double startTime=0.0, const DVector &_x0=emptyConstVector, const DVector &_p=emptyConstVector, const VariablesGrid &_yRef=emptyConstVariablesGrid)
Definition: controller.cpp:270
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)
void setAll(const T &_value)
Definition: vector.hpp:160
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