00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00034
00035 #include <acado_toolkit.hpp>
00036 #include <acado_gnuplot.hpp>
00037
00038 USING_NAMESPACE_ACADO
00039
00040 int main( )
00041 {
00042
00043
00044
00045 DifferentialState h1,h2,h3,h4;
00046 Control u1,u2;
00047
00048
00049
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
00060
00061
00062
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
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
00082
00083
00084 OCP ocp(t_start,t_end,T);
00085 ocp.minimizeLSQ(Q,h,r);
00086
00087 ocp.subjectTo(f);
00088
00089
00090
00091
00092
00093
00094
00095
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
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
00116
00117
00118
00119 RealTimeAlgorithm algorithm(ocp,samplingTime);
00120
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
00129
00130
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
00164
00165
00166
00167
00168
00169
00170
00171 return EXIT_SUCCESS;
00172 }