getting_started.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 
00039 int main( )
00040 {
00041     USING_NAMESPACE_ACADO
00042 
00043 
00044     // INTRODUCE THE VARIABLES:
00045     // -------------------------
00046         DifferentialState xB; //Body Position
00047         DifferentialState xW; //Wheel Position
00048         DifferentialState vB; //Body Velocity
00049         DifferentialState vW; //Wheel Velocity
00050 
00051         Control F;
00052         Disturbance R;
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         h << F;
00079 
00080     DMatrix Q(5,5); // LSQ coefficient matrix
00081         Q(0,0) = 10.0;
00082         Q(1,1) = 10.0;
00083         Q(2,2) = 1.0;
00084         Q(3,3) = 1.0;
00085         Q(4,4) = 1.0e-8;
00086 
00087     DVector r(5); // Reference
00088     r.setAll( 0.0 );
00089 
00090 
00091     // DEFINE AN OPTIMAL CONTROL PROBLEM:
00092     // ----------------------------------
00093     const double tStart = 0.0;
00094     const double tEnd   = 1.0;
00095 
00096     OCP ocp( tStart, tEnd, 20 );
00097 
00098     ocp.minimizeLSQ( Q, h, r );
00099 
00100         ocp.subjectTo( f );
00101 
00102         ocp.subjectTo( -200.0 <= F <= 200.0 );
00103         ocp.subjectTo( R == 0.0 );
00104 
00105 
00106     // SETTING UP THE REAL-TIME ALGORITHM:
00107     // -----------------------------------
00108         RealTimeAlgorithm alg( ocp,0.025 );
00109         alg.set( MAX_NUM_ITERATIONS, 1 );
00110         alg.set( PLOT_RESOLUTION, MEDIUM );
00111 
00112         GnuplotWindow window;
00113           window.addSubplot( xB, "Body Position [m]" );
00114           window.addSubplot( xW, "Wheel Position [m]" );
00115           window.addSubplot( vB, "Body Velocity [m/s]" );
00116           window.addSubplot( vW, "Wheel Velocity [m/s]" );
00117           window.addSubplot( F,  "Damping Force [N]" );
00118           window.addSubplot( R,  "Road Excitation [m]" );
00119 
00120         alg << window;
00121 
00122 
00123     // SETUP CONTROLLER AND PERFORM A STEP:
00124     // ------------------------------------
00125         StaticReferenceTrajectory zeroReference( "ref.txt" );
00126 
00127         Controller controller( alg,zeroReference );
00128 
00129         DVector y( 4 );
00130         y.setZero( );
00131         y(0) = 0.01;
00132 
00133         if (controller.init( 0.0,y ) != SUCCESSFUL_RETURN)
00134                 exit( 1 );
00135         if (controller.step( 0.0,y ) != SUCCESSFUL_RETURN)
00136                 exit( 1 );
00137 
00138     return EXIT_SUCCESS;
00139 }
00140 
00141 
00142 


acado
Author(s): Milan Vukov, Rien Quirynen
autogenerated on Thu Aug 27 2015 11:58:24