fourtankNMPC.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_optimal_control.hpp>
00035 #include <acado_toolkit.hpp>
00036 #include <acado_gnuplot.hpp>
00037 
00038 USING_NAMESPACE_ACADO
00039 
00040 int main( )
00041 {
00042     // INTRODUCE THE VARIABLES:
00043     // -------------------------
00044 
00045     DifferentialState     h1,h2,h3,h4;
00046     Control                   u1,u2;
00047     // Parameter                        T;
00048     // Disturbance               w;
00049     // Parameter               p,q;
00050     const double t_start = 0.0;
00051     const double samplingTime=30.0;
00052     double A1=380.1327, A2=380.1327, A3=380.1327, A4=380.1327, a1=1.2272, a2=1.2272, a3=1.2272, a4=1.2272,g=981.0,gamma1=0.45, gamma2=0.4;
00053     double x_ss1=20.0, x_ss2=15.0, u_max1=400.0, u_max2=400.0, x_max1=40.0, x_max2=40.0, x_max3=40.0, x_max4=40.0;
00054     DifferentialEquation      f;
00055     int T=10;
00056     // 
00057      const double t_end   = t_start+T*samplingTime;
00058    
00059     // DEFINE A DIFFERENTIAL EQUATION:
00060     // -------------------------------
00061 
00062     // f << -dot(x) -x*x + p + u*u + w;
00063     f << dot(h1) == (1.0/A1)*(gamma1*u1+a3*sqrt(2.0*g*h3)-a1*sqrt(2.0*g*h1));
00064     f << dot(h2)== (1.0/A2)*(gamma2*u2+a4*sqrt(2.0*g*h4)-a2*sqrt(2.0*g*h2));
00065     f << dot(h3)==(1.0/A3)*((1.0-gamma2)*u2-a3*sqrt(2.0*g*h3));
00066     f << dot(h4)==(1.0/A4)*((1.0-gamma1)*u1-a4*sqrt(2.0*g*h4));
00067 
00068 
00069     // DEFINE THE LEAST SQUARE FUNCTION
00070 
00071     Function h;
00072     h << h1;
00073     h << h2;
00074     DMatrix Q(2,2);
00075     Q.setIdentity();
00076 
00077     DVector r(2);
00078     r(0)=x_ss1;
00079     r(1)=x_ss2;
00080 
00081     // DEFINE AN OPTIMAL CONTROL PROBLEM:
00082     // ----------------------------------
00083     
00084     OCP ocp(t_start,t_end,T);
00085     ocp.minimizeLSQ(Q,h,r);
00086 
00087     ocp.subjectTo(f);
00088     //ocp.subjectTo( AT_START, x1 == 0.0 );
00089     //ocp.subjectTo( AT_START, x2 == 0.0 );
00090     //ocp.subjectTo( AT_START, x3 == 0.0 );
00091     //ocp.subjectTo( AT_START, x4 == 0.0 );
00092     //ocp.subjectTo( AT_END, x1 == 0.0 );
00093     //ocp.subjectTo( AT_END, x2 == 0.0 );
00094     //ocp.subjectTo( AT_END, x3 == 10.0 );
00095     //ocp.subjectTo( AT_END, x4 == 0.0 );
00096     ocp.subjectTo(  0.0 <= u1 <= u_max1 );
00097     ocp.subjectTo(  0.0 <= u2 <= u_max2 );
00098     ocp.subjectTo(  0.0 <= h1 <= x_max1 );
00099     ocp.subjectTo(  0.0 <= h2 <= x_max2 );
00100     ocp.subjectTo(  0.0 <= h3 <= x_max3 );
00101     ocp.subjectTo(  0.0 <= h4 <= x_max4 );
00102 
00103     // SETTING UP THE PROCESS
00104 
00105     OutputFcn identity;
00106     DynamicSystem dynamicSystem(f,identity);
00107 
00108     GaussianNoise noise( 4,0.0,0.1 );
00109 
00110     Sensor sensor( 4 );
00111     if (sensor.setOutputNoise( noise,samplingTime ) != SUCCESSFUL_RETURN)
00112         exit( EXIT_FAILURE );
00113 
00114     Process process(dynamicSystem,INT_RK45);
00115     //process.setSensor( sensor );
00116 
00117     // SETTING UP THE MPC CONTROLLER
00118 
00119     RealTimeAlgorithm algorithm(ocp,samplingTime);
00120 //     algorithm.set( USE_REALTIME_ITERATIONS, YES );
00121     algorithm.set( MAX_NUM_ITERATIONS, 2 );
00122         algorithm.set(LEVENBERG_MARQUARDT, 1e-5);
00123 
00124     StaticReferenceTrajectory zeroReference;
00125 
00126     Controller controller(algorithm,zeroReference);
00127 
00128     // DEFINE AN OPTIMIZATION ALGORITHM AND SOLVE THE OCP:
00129     // ---------------------------------------------------
00130     //OptimizationAlgorithm algorithm(ocp);
00131 
00132     double simStartTime = 0.0;
00133     double simEndTime = 1200.0;
00134     SimulationEnvironment sim(simStartTime,simEndTime,process,controller);
00135 
00136     DVector x0(4);
00137     x0.setZero( );
00138     x0(0)=0.1;
00139     x0(1)=0.1;
00140     x0(2)=0.1;
00141     x0(3)=0.1;
00142     if (sim.init( x0 ) != SUCCESSFUL_RETURN)
00143         exit( EXIT_FAILURE );
00144     if (sim.run( ) != SUCCESSFUL_RETURN)
00145         exit( EXIT_FAILURE );
00146    
00147     VariablesGrid diffStates;
00148     sim.getProcessDifferentialStates(diffStates);
00149     VariablesGrid feedbackControl;
00150     sim.getFeedbackControl (feedbackControl);
00151 
00152     GnuplotWindow window;
00153         window.addSubplot( diffStates(0), "h1");
00154         window.addSubplot( diffStates(1), "h2");
00155         window.addSubplot( diffStates(2), "h3");
00156         window.addSubplot( diffStates(3), "h4");
00157         window.addSubplot( feedbackControl(0), "F1");
00158         window.addSubplot( feedbackControl(1), "F2");
00159     window.plot();
00160     
00161         diffStates.print("result.txt","diffstates",PS_MATLAB);
00162 
00163     //algorithm.set( HESSIAN_APPROXIMATION, EXACT_HESSIAN );
00164     
00165     //algorithm << window;
00166 
00167     //double t1 = acadoGetTime();
00168     //algorithm.solve();
00169     //printf(" %.16e \n ", acadoGetTime() - t1  );
00170 
00171     return EXIT_SUCCESS;
00172 }


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