simple_mpc.cpp
Go to the documentation of this file.
00001 
00035 #include <acado_toolkit.hpp>                    // Include the ACADO toolkit
00036 #include <acado/utils/matlab_acado_utils.hpp>   // Include specific Matlab utils
00037 
00038 USING_NAMESPACE_ACADO                           // Open the namespace
00039 
00040 void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[] )  // Start the MEX function. Do NOT change the header of this function.
00041  { 
00042     clearAllStaticCounters( );                  // Clear software counters
00043  
00044     
00045     
00046     // INTRODUCE THE VARIABLES:
00047     // -------------------------
00048         DifferentialState xB;
00049         DifferentialState xW;
00050         DifferentialState vB;
00051         DifferentialState vW;
00052 
00053         Control R;
00054         Control F;
00055 
00056         double mB = 350.0;
00057         double mW = 50.0;
00058         double kS = 20000.0;
00059         double kT = 200000.0;
00060 
00061 
00062     // DEFINE A DIFFERENTIAL EQUATION:
00063     // -------------------------------
00064     DifferentialEquation f;
00065 
00066         f << dot(xB) == vB;
00067         f << dot(xW) == vW;
00068         f << dot(vB) == ( -kS*xB + kS*xW + F ) / mB;
00069         f << dot(vW) == ( -kT*xB - (kT+kS)*xW + kT*R - F ) / mW;
00070 
00071 
00072     // DEFINE LEAST SQUARE FUNCTION:
00073     // -----------------------------
00074     Function h;
00075 
00076     h << xB;
00077     h << xW;
00078         h << vB;
00079     h << vW;
00080 
00081     Matrix Q(4,4);
00082     Q.setIdentity();
00083         Q(0,0) = 10.0;
00084         Q(1,1) = 10.0;
00085 
00086     Vector r(4);
00087     r.setAll( 0.0 );
00088 
00089 
00090     // DEFINE AN OPTIMAL CONTROL PROBLEM:
00091     // ----------------------------------
00092     const double t_start = 0.0;
00093     const double t_end   = 1.0;
00094 
00095     OCP ocp( t_start, t_end, 20 );
00096 
00097     ocp.minimizeLSQ( Q, h, r );
00098 
00099         ocp.subjectTo( f );
00100 
00101         ocp.subjectTo( -500.0 <= F <= 500.0 );
00102         ocp.subjectTo( R == 0.0 );
00103 
00104 
00105 
00106     // SETTING UP THE (SIMULATED) PROCESS:
00107     // -----------------------------------
00108         OutputFcn identity;
00109         DynamicSystem dynamicSystem( f,identity );
00110 
00111         Process process( dynamicSystem,INT_RK45 );
00112 
00113         VariablesGrid disturbance = readFromFile( "simple_mpc_road.txt" );
00114         process.setProcessDisturbance( disturbance );
00115 
00116 
00117     // SETTING UP THE MPC CONTROLLER:
00118     // ------------------------------
00119         RealTimeAlgorithm alg( ocp,0.05 );
00120         alg.set( MAX_NUM_ITERATIONS, 2 );
00121         
00122         StaticReferenceTrajectory zeroReference;
00123 
00124         Controller controller( alg,zeroReference );
00125 
00126 
00127     // SETTING UP THE SIMULATION ENVIRONMENT,  RUN THE EXAMPLE...
00128     // ----------------------------------------------------------
00129         SimulationEnvironment sim( 0.0,3.0,process,controller );
00130 
00131         Vector x0(4);
00132         x0(0) = 0.01;
00133         x0(1) = 0.0;
00134         x0(2) = 0.0;
00135         x0(3) = 0.0;
00136 
00137         sim.init( x0 );
00138         sim.run( );
00139 
00140 
00141     // ...AND PLOT THE RESULTS
00142     // ----------------------------------------------------------
00143         VariablesGrid sampledProcessOutput;
00144         sim.getSampledProcessOutput( sampledProcessOutput );
00145     acadoPlot(sampledProcessOutput);
00146     
00147         VariablesGrid feedbackControl;
00148         sim.getFeedbackControl( feedbackControl );
00149     acadoPlot(feedbackControl);
00150     
00151 
00152           
00153     clearAllStaticCounters( );                  // Clear software counters
00154 } 
00155 


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