crane_mpc3_stepped.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 
36 #include <acado_toolkit.hpp>
37 #include <acado_gnuplot.hpp>
38 
39 
40 int main( ){
41 
43 
44  // VARIABLES:
45  // ------------------------
46  DifferentialState x; // Position of the trolley
47  DifferentialState v; // Velocity of the trolley
48  DifferentialState phi; // excitation angle
49  DifferentialState dphi; // rotational velocity
50 
51  Control ax; // trolley accelaration
52  Disturbance W; // disturbance
53 
54  double L = 1.0 ; // length
55  double m = 1.0 ; // mass
56  double g = 9.81; // gravitational constant
57  double b = 0.2 ; // friction coefficient
58 
59 
60  // DIFFERENTIAL EQUATION:
61  // ------------------------
62  DifferentialEquation f, fSim; // The model equations
63 
64  f << dot(x) == v;
65  f << dot(v) == ax;
66  f << dot(phi ) == dphi;
67  f << dot(dphi) == -g/L*sin(phi) -ax/L*cos(phi) - b/(m*L*L)*dphi;
68 
69  L = 1.2; // introduce model plant mismatch
70 
71  fSim << dot(x) == v;
72  fSim << dot(v) == ax + W;
73  fSim << dot(phi ) == dphi;
74  fSim << dot(dphi) == -g/L*sin(phi) -ax/L*cos(phi) - b/(m*L*L)*dphi;
75 
76 
77  // DEFINE LEAST SQUARE FUNCTION:
78  // -----------------------------
79  Function h;
80 
81  h << x;
82  h << v;
83  h << phi;
84  h << dphi;
85 
86  DMatrix Q(4,4); // LSQ coefficient matrix
87  Q.setIdentity();
88 
89  DVector r(4); // Reference
90 
91 
92  // DEFINE AN OPTIMAL CONTROL PROBLEM:
93  // ----------------------------------
94  const double t_start = 0.0;
95  const double t_end = 5.0;
96 
97  OCP ocp( t_start, t_end, 25 );
98 
99  ocp.minimizeLSQ( Q, h, r );
100  ocp.subjectTo( f );
101  ocp.subjectTo( -5.0 <= ax <= 5.0 );
102 
103 
104  // SETTING UP THE (SIMULATED) PROCESS:
105  // -----------------------------------
106  OutputFcn identity;
107  DynamicSystem dynamicSystem( fSim,identity );
108 
109  Process process( dynamicSystem,INT_RK45 );
110 
111  VariablesGrid disturbance; disturbance.read( "dist.txt" );
112  if (process.setProcessDisturbance( disturbance ) != SUCCESSFUL_RETURN)
113  exit( EXIT_FAILURE );
114 
115  // SETTING UP THE MPC CONTROLLER:
116  // ------------------------------
117  double samplingTime = 0.1;
118  RealTimeAlgorithm alg( ocp, samplingTime );
119 // alg.set( USE_REALTIME_ITERATIONS,NO );
120 // alg.set( MAX_NUM_ITERATIONS,20 );
121 
122  StaticReferenceTrajectory zeroReference;
123 
124  Controller controller( alg, zeroReference );
125 
126  DVector x0(4);
127  x0.setZero();
128  x0(3) = 1.0;
129 
130  double startTime = 0.0;
131  double endTime = 20.0;
132 
133 
134  DVector uCon;
135  VariablesGrid ySim;
136 
137  if (controller.init( startTime,x0 ) != SUCCESSFUL_RETURN)
138  exit( EXIT_FAILURE );
139  controller.getU( uCon );
140 
141  if (process.init( startTime,x0,uCon ) != SUCCESSFUL_RETURN)
142  exit( EXIT_FAILURE );
143  process.getY( ySim );
144 
145 
146  // hand-coding call to
147  // sim.run( )
148 
149  int nSteps = 0;
150  double currentTime = startTime;
151 
152  while ( currentTime <= endTime )
153  {
154  printf( "\n*** Simulation Loop No. %d (starting at time %.3f) ***\n",nSteps,currentTime );
155 
156  if (controller.step( currentTime,ySim.getLastVector() ) != SUCCESSFUL_RETURN)
157  exit( EXIT_FAILURE );
158  controller.getU( uCon );
159 
160  if (process.step( currentTime,currentTime+samplingTime,uCon ) != SUCCESSFUL_RETURN)
161  exit( EXIT_FAILURE );
162  process.getY( ySim );
163 
164  ++nSteps;
165  currentTime = (double)nSteps * samplingTime;
166  }
167 
168  return EXIT_SUCCESS;
169 }
170 
171 /* <<< end tutorial code <<< */
USING_NAMESPACE_ACADO IntermediateState sin(const Expression &arg)
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
returnValue getY(VariablesGrid &_y) const
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...
User-interface to formulate and solve model predictive control problems.
virtual returnValue step(const VariablesGrid &_u, const VariablesGrid &_p=emptyVariablesGrid)
Definition: process.cpp:508
returnValue subjectTo(const DifferentialEquation &differentialEquation_)
Definition: ocp.cpp:153
IntermediateState cos(const Expression &arg)
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
DVector getLastVector() const
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. ...
#define v
Expression dot(const Expression &arg)
#define L
int main()
returnValue read(std::istream &stream)
const double t_end
returnValue setProcessDisturbance(const Curve &_processDisturbance)
Definition: process.cpp:289
const double t_start
Simulates the process to be controlled based on a dynamic model.
Definition: process.hpp:71
returnValue getU(DVector &_u) const
Allows to setup and evaluate differential equations (ODEs and DAEs) based on SymbolicExpressions.
virtual returnValue init(double _startTime=0.0, const DVector &_xStart=emptyConstVector, const DVector &_uStart=emptyConstVector, const DVector &_pStart=emptyConstVector)
Definition: process.cpp:353


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