Go to the documentation of this file.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
00036 #include <acado_toolkit.hpp>
00037 #include <acado_gnuplot.hpp>
00038
00039
00040 int main( ){
00041
00042 USING_NAMESPACE_ACADO;
00043
00044
00045
00046 DifferentialState x;
00047 DifferentialState v;
00048 DifferentialState phi;
00049 DifferentialState dphi;
00050
00051 Control ax;
00052 Disturbance W;
00053
00054 double L = 1.0 ;
00055 double m = 1.0 ;
00056 double g = 9.81;
00057 double b = 0.2 ;
00058
00059
00060
00061
00062 DifferentialEquation f, fSim;
00063
00064 f << dot(x) == v;
00065 f << dot(v) == ax;
00066 f << dot(phi ) == dphi;
00067 f << dot(dphi) == -g/L*sin(phi) -ax/L*cos(phi) - b/(m*L*L)*dphi;
00068
00069 L = 1.2;
00070
00071 fSim << dot(x) == v;
00072 fSim << dot(v) == ax + W;
00073 fSim << dot(phi ) == dphi;
00074 fSim << dot(dphi) == -g/L*sin(phi) -ax/L*cos(phi) - b/(m*L*L)*dphi;
00075
00076
00077
00078
00079 Function h;
00080
00081 h << x;
00082 h << v;
00083 h << phi;
00084 h << dphi;
00085
00086 DMatrix Q(4,4);
00087 Q.setIdentity();
00088
00089 DVector r(4);
00090
00091
00092
00093
00094 const double t_start = 0.0;
00095 const double t_end = 5.0;
00096
00097 OCP ocp( t_start, t_end, 25 );
00098
00099 ocp.minimizeLSQ( Q, h, r );
00100 ocp.subjectTo( f );
00101 ocp.subjectTo( -5.0 <= ax <= 5.0 );
00102
00103
00104
00105
00106 OutputFcn identity;
00107 DynamicSystem dynamicSystem( fSim,identity );
00108
00109 Process process( dynamicSystem,INT_RK45 );
00110
00111 VariablesGrid disturbance; disturbance.read( "dist.txt" );
00112 if (process.setProcessDisturbance( disturbance ) != SUCCESSFUL_RETURN)
00113 exit( EXIT_FAILURE );
00114
00115
00116
00117 double samplingTime = 0.1;
00118 RealTimeAlgorithm alg( ocp, samplingTime );
00119
00120
00121
00122 StaticReferenceTrajectory zeroReference;
00123
00124 Controller controller( alg, zeroReference );
00125
00126 DVector x0(4);
00127 x0.setZero();
00128 x0(3) = 1.0;
00129
00130 double startTime = 0.0;
00131 double endTime = 20.0;
00132
00133
00134 DVector uCon;
00135 VariablesGrid ySim;
00136
00137 if (controller.init( startTime,x0 ) != SUCCESSFUL_RETURN)
00138 exit( EXIT_FAILURE );
00139 controller.getU( uCon );
00140
00141 if (process.init( startTime,x0,uCon ) != SUCCESSFUL_RETURN)
00142 exit( EXIT_FAILURE );
00143 process.getY( ySim );
00144
00145
00146
00147
00148
00149 int nSteps = 0;
00150 double currentTime = startTime;
00151
00152 while ( currentTime <= endTime )
00153 {
00154 printf( "\n*** Simulation Loop No. %d (starting at time %.3f) ***\n",nSteps,currentTime );
00155
00156 if (controller.step( currentTime,ySim.getLastVector() ) != SUCCESSFUL_RETURN)
00157 exit( EXIT_FAILURE );
00158 controller.getU( uCon );
00159
00160 if (process.step( currentTime,currentTime+samplingTime,uCon ) != SUCCESSFUL_RETURN)
00161 exit( EXIT_FAILURE );
00162 process.getY( ySim );
00163
00164 ++nSteps;
00165 currentTime = (double)nSteps * samplingTime;
00166 }
00167
00168 return EXIT_SUCCESS;
00169 }
00170
00171