getting_started_classical.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 
00034 #include <acado_toolkit.hpp>
00035 #include <acado_gnuplot.hpp>
00036 
00037 
00038 int main( )
00039 {
00040     USING_NAMESPACE_ACADO
00041 
00042 
00043     // INTRODUCE THE VARIABLES:
00044     // -------------------------
00045         DifferentialState xB; //Body Position
00046         DifferentialState xW; //Wheel Position
00047         DifferentialState vB; //Body Velocity
00048         DifferentialState vW; //Wheel Velocity
00049 
00050         Disturbance R;
00051         Control F;
00052 
00053         double mB = 350.0;
00054         double mW = 50.0;
00055         double kS = 20000.0;
00056         double kT = 200000.0;
00057 
00058 
00059     // DEFINE A DIFFERENTIAL EQUATION:
00060     // -------------------------------
00061     DifferentialEquation f;
00062 
00063         f << dot(xB) == vB;
00064         f << dot(xW) == vW;
00065         f << dot(vB) == ( -kS*xB + kS*xW + F ) / mB;
00066         f << dot(vW) == (  kS*xB - (kT+kS)*xW + kT*R - F ) / mW;
00067 
00068 
00069     // SETTING UP THE (SIMULATED) PROCESS:
00070     // -----------------------------------
00071         OutputFcn identity;
00072         DynamicSystem dynamicSystem( f,identity );
00073 
00074         Process process( dynamicSystem,INT_RK45 );
00075 
00076         VariablesGrid disturbance; disturbance.read( "road.txt" );
00077         if (process.setProcessDisturbance( disturbance ) != SUCCESSFUL_RETURN)
00078                 exit( EXIT_FAILURE );
00079 
00080 
00081     // SETTING UP THE MPC CONTROLLER:
00082     // ------------------------------
00083         PIDcontroller pid( 4,1,0.01 );
00084 
00085         DVector pWeights( 4 );
00086         pWeights(0) = 1000.0;
00087         pWeights(1) = -1000.0;
00088         pWeights(2) = 1000.0;
00089         pWeights(3) = -1000.0;
00090 
00091         DVector dWeights( 4 );
00092         dWeights(0) = 0.0;
00093         dWeights(1) = 0.0;
00094         dWeights(2) = 20.0;
00095         dWeights(3) = -20.0;
00096 
00097         pid.setProportionalWeights( pWeights );
00098         pid.setDerivativeWeights( dWeights );
00099 
00100         pid.setControlLowerLimit( 0,-200.0 );
00101         pid.setControlUpperLimit( 0, 200.0 );
00102 
00103 
00104 //      DMatrix K( 1,4 );
00105 //      K(0,0) = -3.349222044080232e+04;
00106 //      K(0,1) = -3.806600292165519e+03;
00107 //      K(0,2) =  9.999999999999985e+02;
00108 //      K(0,3) = -1.040810121403324e+03;
00109 // 
00110 //      LinearStateFeedback lqr( K,0.025 );
00111 // 
00112 //      lqr.setControlLowerLimit( 0,-200.0 );
00113 //      lqr.setControlUpperLimit( 0, 200.0 );
00114 
00115 
00116         StaticReferenceTrajectory zeroReference;
00117 
00118         Controller controller( pid,zeroReference );
00119 //      Controller controller( lqr,zeroReference );
00120 
00121 
00122     // SETTING UP THE SIMULATION ENVIRONMENT,  RUN THE EXAMPLE...
00123     // ----------------------------------------------------------
00124         SimulationEnvironment sim( 0.0,2.5,process,controller );
00125 
00126         DVector x0(4);
00127         x0.setZero();
00128 
00129         if (sim.init( x0 ) != SUCCESSFUL_RETURN)
00130                 exit( EXIT_FAILURE );
00131         if (sim.run( ) != SUCCESSFUL_RETURN)
00132                 exit( EXIT_FAILURE );
00133 
00134     // ...AND PLOT THE RESULTS
00135     // ----------------------------------------------------------
00136         VariablesGrid diffStates;
00137         sim.getProcessDifferentialStates( diffStates );
00138 
00139         VariablesGrid feedbackControl;
00140         sim.getFeedbackControl( feedbackControl );
00141 
00142         GnuplotWindow window;
00143         window.addSubplot( diffStates(0),   "Body Position [m]" );
00144         window.addSubplot( diffStates(1),   "Wheel Position [m]" );
00145         window.addSubplot( diffStates(2),   "Body Velocity [m/s]" );
00146         window.addSubplot( diffStates(3),   "Wheel Velocity [m/s]" );
00147         window.addSubplot( feedbackControl, "Damping Force [N]" );
00148         window.addSubplot( disturbance,     "Road Excitation [m]" );
00149         window.plot( );
00150 
00151     return EXIT_SUCCESS;
00152 }
00153 
00154 
00155 


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