active_damping_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 
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  Disturbance 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  h << F;
79 
80  DMatrix Q = zeros<double>(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 t_start = 0.0;
94  const double t_end = 1.0;
95 
96  OCP ocp( t_start, t_end, 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 
107  // SETTING UP THE (SIMULATED) PROCESS:
108  // -----------------------------------
109  OutputFcn identity;
110  DynamicSystem dynamicSystem( f,identity );
111 
112  Process process( dynamicSystem,INT_RK45 );
113 
114  VariablesGrid disturbance; disturbance.read( "road.txt" );
115  if (process.setProcessDisturbance( disturbance ) != SUCCESSFUL_RETURN)
116  return EXIT_FAILURE;
117 
118  // SETTING UP THE MPC CONTROLLER:
119  // ------------------------------
120 
121  double samplingTime = 0.025;
122  RealTimeAlgorithm alg( ocp,samplingTime );
123  alg.set( INTEGRATOR_TYPE, INT_RK78 );
124  //alg.set( MAX_NUM_ITERATIONS, 2 );
126 
127  StaticReferenceTrajectory zeroReference;
128 
129  Controller controller( alg,zeroReference );
130 
131 
132  double startTime = 0.0;
133  double endTime = 2.5;
134 
135  DVector x0(4);
136  x0.setZero();
137 
138  // hand-coding call to
139  // sim.init( x0 )
140 
141  DVector uCon;
142  VariablesGrid ySim;
143 
144  if (controller.init( startTime,x0 ) != SUCCESSFUL_RETURN)
145  exit( EXIT_FAILURE );
146  controller.getU( uCon );
147 
148  if (process.init( startTime,x0,uCon ) != SUCCESSFUL_RETURN)
149  exit( EXIT_FAILURE );
150  process.getY( ySim );
151 
152 
153  // hand-coding call to
154  // sim.run( )
155 
156  double currentTime = startTime;
157  int nSteps = 0;
158 
159  while ( currentTime <= endTime )
160  {
161  printf( "\n*** Simulation Loop No. %d (starting at time %.3f) ***\n",nSteps,currentTime );
162 
163  double t = acadoGetTime();
164  if (controller.feedbackStep( currentTime,ySim.getLastVector() ) != SUCCESSFUL_RETURN)
165  exit( EXIT_FAILURE );
166  printf( "t = %e\n", acadoGetTime()-t );
167  controller.getU( uCon );
168  if (controller.preparationStep( ) != SUCCESSFUL_RETURN)
169  exit( EXIT_FAILURE );
170 
171  if (process.step( currentTime,currentTime+samplingTime,uCon ) != SUCCESSFUL_RETURN)
172  exit( EXIT_FAILURE );
173  process.getY( ySim );
174 
175  currentTime += samplingTime;
176  ++nSteps;
177  }
178 
179 // // SETTING UP THE SIMULATION ENVIRONMENT, RUN THE EXAMPLE...
180 // // ----------------------------------------------------------
181 // SimulationEnvironment sim( 0.0,3.0,process,controller );
182 //
183 // DVector x0(4);
184 // x0.setZero();
185 //
186 // sim.init( x0 );
187 // sim.run( );
188 //
189 //
190 // // ...AND PLOT THE RESULTS
191 // // ----------------------------------------------------------
192 // VariablesGrid diffStates;
193 // sim.getProcessDifferentialStates( diffStates );
194 //
195 // VariablesGrid feedbackControl;
196 // sim.getFeedbackControl( feedbackControl );
197 //
198 // GnuplotWindow window;
199 // window.addSubplot( diffStates(0), "Body Position [m]" );
200 // window.addSubplot( diffStates(1), "Wheel Position [m]" );
201 // window.addSubplot( diffStates(2), "Body Velocity [m/s]" );
202 // window.addSubplot( diffStates(3), "Wheel Velocity [m/s]" );
203 // window.addSubplot( feedbackControl, "Damping Force [N]" );
204 // window.addSubplot( disturbance, "Road Excitation [m]" );
205 // window.plot( );
206 
207  return EXIT_SUCCESS;
208 }
209 
210 
211 
virtual returnValue preparationStep(double nextTime=0.0, const VariablesGrid &_yRef=emptyConstVariablesGrid)
Definition: controller.cpp:474
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
double acadoGetTime()
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
#define YES
Definition: acado_types.hpp:51
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. ...
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
Simulates the process to be controlled based on a dynamic model.
Definition: process.hpp:71
virtual returnValue feedbackStep(double currentTime, const DVector &_y, const VariablesGrid &_yRef=emptyConstVariablesGrid)
Definition: controller.cpp:424
returnValue getU(DVector &_u) const
#define R
int main()
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:27