simple_mpc.cpp
Go to the documentation of this file.
00001 /*
00002  *    This file is part of ACADO Toolkit.
00003  *
00004  *    ACADO Toolkit -- A Toolkit for Automatic Control and Dynamic Optimization.
00005  *    Copyright (C) 2008-2014 by Boris Houska, Hans Joachim Ferreau,
00006  *    Milan Vukov, Rien Quirynen, KU Leuven.
00007  *    Developed within the Optimization in Engineering Center (OPTEC)
00008  *    under supervision of Moritz Diehl. All rights reserved.
00009  *
00010  *    ACADO Toolkit is free software; you can redistribute it and/or
00011  *    modify it under the terms of the GNU Lesser General Public
00012  *    License as published by the Free Software Foundation; either
00013  *    version 3 of the License, or (at your option) any later version.
00014  *
00015  *    ACADO Toolkit is distributed in the hope that it will be useful,
00016  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  *    Lesser General Public License for more details.
00019  *
00020  *    You should have received a copy of the GNU Lesser General Public
00021  *    License along with ACADO Toolkit; if not, write to the Free Software
00022  *    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00023  *
00024  */
00025 
00026 
00027 
00035 #include <acado_toolkit.hpp>
00036 #include <acado_gnuplot.hpp>
00037 
00038 using namespace std;
00039 
00040 USING_NAMESPACE_ACADO
00041 
00042 int main( )
00043 {
00044     // INTRODUCE THE VARIABLES:
00045     // -------------------------
00046         DifferentialState xB;
00047         DifferentialState xW;
00048         DifferentialState vB;
00049         DifferentialState vW;
00050 
00051         Control R;
00052         Control F;
00053 
00054         double mB = 350.0;
00055         double mW = 50.0;
00056         double kS = 20000.0;
00057         double kT = 200000.0;
00058 
00059 
00060     // DEFINE A DIFFERENTIAL EQUATION:
00061     // -------------------------------
00062     DifferentialEquation f;
00063 
00064         f << dot(xB) == vB;
00065         f << dot(xW) == vW;
00066         f << dot(vB) == ( -kS*xB + kS*xW + F ) / mB;
00067         f << dot(vW) == (  kS*xB - (kT+kS)*xW + kT*R - F ) / mW;
00068 
00069 
00070     // DEFINE LEAST SQUARE FUNCTION:
00071     // -----------------------------
00072     Function h;
00073 
00074     h << xB;
00075     h << xW;
00076         h << vB;
00077     h << vW;
00078 
00079     DMatrix Q(4,4);
00080     Q.setIdentity();
00081         Q(0,0) = 10.0;
00082         Q(1,1) = 10.0;
00083 
00084     DVector r(4);
00085     r.setAll( 0.0 );
00086 
00087 
00088     // DEFINE AN OPTIMAL CONTROL PROBLEM:
00089     // ----------------------------------
00090     const double t_start = 0.0;
00091     const double t_end   = 1.0;
00092 
00093     OCP ocp( t_start, t_end, 20 );
00094 
00095     ocp.minimizeLSQ( Q, h, r );
00096 
00097         ocp.subjectTo( f );
00098 
00099         ocp.subjectTo( -500.0 <= F <= 500.0 );
00100         ocp.subjectTo( R == 0.0 );
00101 
00102 
00103 
00104     // SETTING UP THE (SIMULATED) PROCESS:
00105     // -----------------------------------
00106         OutputFcn identity;
00107         DynamicSystem dynamicSystem( f,identity );
00108 
00109         Process process( dynamicSystem,INT_RK45 );
00110 
00111     // SETTING UP THE MPC CONTROLLER:
00112     // ------------------------------
00113         RealTimeAlgorithm alg( ocp,0.05 );
00114         alg.set( MAX_NUM_ITERATIONS, 2 );
00115         
00116         StaticReferenceTrajectory zeroReference;
00117 
00118         Controller controller( alg,zeroReference );
00119 
00120 
00121     // SETTING UP THE SIMULATION ENVIRONMENT,  RUN THE EXAMPLE...
00122     // ----------------------------------------------------------
00123         SimulationEnvironment sim( 0.0,3.0,process,controller );
00124 
00125         DVector x0(4);
00126         x0(0) = 0.01;
00127         x0(1) = 0.0;
00128         x0(2) = 0.0;
00129         x0(3) = 0.0;
00130 
00131         if (sim.init( x0 ) != SUCCESSFUL_RETURN)
00132                 exit( EXIT_FAILURE );
00133         if (sim.run( ) != SUCCESSFUL_RETURN)
00134                 exit( EXIT_FAILURE );
00135 
00136     // ...AND PLOT THE RESULTS
00137     // ----------------------------------------------------------
00138         VariablesGrid sampledProcessOutput;
00139         sim.getSampledProcessOutput( sampledProcessOutput );
00140 
00141         VariablesGrid feedbackControl;
00142         sim.getFeedbackControl( feedbackControl );
00143 
00144         GnuplotWindow window;
00145         window.addSubplot( sampledProcessOutput(0), "Body Position [m]" );
00146         window.addSubplot( sampledProcessOutput(1), "Wheel Position [m]" );
00147         window.addSubplot( sampledProcessOutput(2), "Body Velocity [m/s]" );
00148         window.addSubplot( sampledProcessOutput(3), "Wheel Velocity [m/s]" );
00149         window.addSubplot( feedbackControl(1),      "Damping Force [N]" );
00150         window.addSubplot( feedbackControl(0),      "Road Excitation [m]" );
00151         window.plot( );
00152 
00153     return EXIT_SUCCESS;
00154 }
00155 
00156 
00157 


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Thu Aug 27 2015 12:00:00