simulation_environment/getting_started_classical.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 
81  // SETTING UP THE MPC CONTROLLER:
82  // ------------------------------
83  PIDcontroller pid( 4,1,0.01 );
84 
85  DVector pWeights( 4 );
86  pWeights(0) = 1000.0;
87  pWeights(1) = -1000.0;
88  pWeights(2) = 1000.0;
89  pWeights(3) = -1000.0;
90 
91  DVector dWeights( 4 );
92  dWeights(0) = 0.0;
93  dWeights(1) = 0.0;
94  dWeights(2) = 20.0;
95  dWeights(3) = -20.0;
96 
97  pid.setProportionalWeights( pWeights );
98  pid.setDerivativeWeights( dWeights );
99 
100  pid.setControlLowerLimit( 0,-200.0 );
101  pid.setControlUpperLimit( 0, 200.0 );
102 
103 
104 // DMatrix K( 1,4 );
105 // K(0,0) = -3.349222044080232e+04;
106 // K(0,1) = -3.806600292165519e+03;
107 // K(0,2) = 9.999999999999985e+02;
108 // K(0,3) = -1.040810121403324e+03;
109 //
110 // LinearStateFeedback lqr( K,0.025 );
111 //
112 // lqr.setControlLowerLimit( 0,-200.0 );
113 // lqr.setControlUpperLimit( 0, 200.0 );
114 
115 
116  StaticReferenceTrajectory zeroReference;
117 
118  Controller controller( pid,zeroReference );
119 // Controller controller( lqr,zeroReference );
120 
121 
122  // SETTING UP THE SIMULATION ENVIRONMENT, RUN THE EXAMPLE...
123  // ----------------------------------------------------------
124  SimulationEnvironment sim( 0.0,2.5,process,controller );
125 
126  DVector x0(4);
127  x0.setZero();
128 
129  if (sim.init( x0 ) != SUCCESSFUL_RETURN)
130  exit( EXIT_FAILURE );
131  if (sim.run( ) != SUCCESSFUL_RETURN)
132  exit( EXIT_FAILURE );
133 
134  // ...AND PLOT THE RESULTS
135  // ----------------------------------------------------------
136  VariablesGrid diffStates;
137  sim.getProcessDifferentialStates( diffStates );
138 
139  VariablesGrid feedbackControl;
140  sim.getFeedbackControl( feedbackControl );
141 
142  GnuplotWindow window;
143  window.addSubplot( diffStates(0), "Body Position [m]" );
144  window.addSubplot( diffStates(1), "Wheel Position [m]" );
145  window.addSubplot( diffStates(2), "Body Velocity [m/s]" );
146  window.addSubplot( diffStates(3), "Wheel Velocity [m/s]" );
147  window.addSubplot( feedbackControl, "Damping Force [N]" );
148  window.addSubplot( disturbance, "Road Excitation [m]" );
149  window.plot( );
150 
151  return EXIT_SUCCESS;
152 }
153 
154 
155 
Calculates the control inputs of the Process based on the Process outputs.
Definition: controller.hpp:71
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 setDerivativeWeights(const DVector &_dWeights)
returnValue getFeedbackControl(Curve &_feedbackControl) const
returnValue setProportionalWeights(const DVector &_pWeights)
returnValue addSubplot(PlotWindowSubplot &_subplot)
returnValue setControlLowerLimit(uint idx, double _lowerLimit)
returnValue init(const DVector &x0_, const DVector &p_=emptyConstVector)
Derived & setZero(Index size)
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
Allows to run closed-loop simulations of dynamic systems.
Simulates the process to be controlled based on a dynamic model.
Definition: process.hpp:71
returnValue setControlUpperLimit(uint idx, double _upperLimit)
Provides an interface to Gnuplot for plotting algorithmic outputs.
Implements a PID control law to be used within a Controller.
#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