Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages | Examples

slidingModeControlMRPMW.cpp

Go to the documentation of this file.
00001 //////////////////////////////////////////////////////////////////////////////////////////////////
00002 /*! \file slidingModeControlMRPMW.cpp
00003 *  \brief Implementation of the slidingModeControlMRPMW class.
00004 *  \author $Author: jayhawk_hokie $
00005 *  \version $Revision: 1.2 $
00006 *  \date    $Date: 2007/08/31 15:53:26 $
00007 *//////////////////////////////////////////////////////////////////////////////////////////////////
00008 /*! 
00009 */
00010 //////////////////////////////////////////////////////////////////////////////////////////////////
00011 
00012 
00013 #include "slidingModeControlMRPMW.h"
00014 
00015 /*! /brief Constructor for slidingModeControlMRPMW
00016  *
00017  */
00018 slidingModeControlMRPMW::slidingModeControlMRPMW( )
00019 {
00020 }
00021 
00022 /*! /brief Constructor for slidingModeControlMRPMW
00023  *
00024  *  @param ptr_whorl
00025  */
00026 slidingModeControlMRPMW::slidingModeControlMRPMW( Whorl* ptr_whorl )
00027 {
00028         m_whorl = ptr_whorl;
00029         int retcode = Initialize( );
00030         retcode = 0;
00031 }
00032 
00033 /*! /brief De-Constructor for slidingModeControlMRPMW
00034  *
00035  */
00036 slidingModeControlMRPMW::~slidingModeControlMRPMW( ) 
00037 {
00038 
00039 }
00040 
00041 /*! /brief Initialize slidingModeControlMRPMW
00042  *
00043  */
00044 int slidingModeControlMRPMW::Initialize( ) 
00045 {
00046         m_controlTorque.initialize(3);
00047 
00048         m_uMax = 1.5; // saturation limit for reaction wheels [N-m]
00049 
00050         /* Set the contorl gain matrix */
00051         m_gainMRPError.initialize(3,3);
00052 /*      m_gainMRPError(1,1) = 0.05;
00053         m_gainMRPError(2,2) = 0.05;
00054         m_gainMRPError(3,3) = 0.005;
00055 */      m_gainMRPError(1,1) = 0.05;
00056         m_gainMRPError(2,2) = 0.05;
00057         m_gainMRPError(3,3) = 0.1;
00058 
00059         m_gainMRPRate.initialize(3,3);
00060         // semi-auto gain selection
00061         for ( int i=1; i<4; i++ )
00062         {
00063                 m_gainMRPRate(i,i) = 4*pow( m_gainMRPError(i,i), 0.5 );
00064         }
00065         
00066         /* Get reaction wheel orientation matrix */
00067         A = FindA( );
00068 
00069         /* Get reaction wheel spin inertia */
00070         MOI_sw = FindMOI_sw();
00071 
00072         /* Reference Model */
00073         Quaternion qri( 0.0, 0.0, 0.0, 1.0 );
00074         m_quaternionReference(1) = qri(1);
00075         m_quaternionReference(2) = qri(2);
00076         m_quaternionReference(3) = qri(3);
00077         m_quaternionReference(4) = qri(4);
00078         m_mrpReference = ModifiedRodriguezParameters( qri );
00079         m_angularRateReference(1) = 0;
00080         m_angularRateReference(2) = 0.0;
00081         m_angularRateReference(3) = 0.0; // [rad/s]
00082 
00083         ssfTime time;
00084         time = Now( );
00085         m_previousTime = time.GetSeconds( );
00086         
00087         m_estimateInertiaInv.initialize(3,3);
00088         m_estimateInertiaInv(_(1,3),_(1,3)) = matrixInv3x3( m_whorl->GetMOI( ) );
00089         
00090         m_normEstimateInertiaInv = norm2( m_estimateInertiaInv );
00091 
00092         m_normEstimateInertia = norm2( m_whorl->GetMOI( ) ); 
00093 
00094         m_estimateB.initialize(3,3);
00095         m_estimateB(_(1,3),_(1,3)) = matrixInv3x3( m_whorl->GetMOI( ) );
00096 
00097         m_estimateBinv.initialize(3,3);
00098         m_estimateBinv(_(1,3),_(1,3)) = matrixInv3x3( m_estimateB ); 
00099 
00100         /* Bounding Parameters */
00101 
00102         m_phi.initialize(3);
00103         m_phi(1) = 0.5; // custom parameter
00104         m_phi(2) = 0.5; // custom parameter
00105         m_phi(3) = 0.5; // custom parameter
00106         /*
00107         m_phi(1) = 0.016; // custom parameter
00108         m_phi(2) = 0.016; // custom parameter
00109         m_phi(3) = 0.016; // custom parameter
00110         */
00111 
00112         m_eta.initialize(3);
00113         m_eta(1) = 0.04; // custom parameter
00114         m_eta(2) = 0.04; // custom parameter
00115         m_eta(3) = 0.04; // custom parameter
00116         /*
00117         m_eta(1) = 0.01; // custom parameter
00118         m_eta(2) = 0.01; // custom parameter
00119         m_eta(3) = 0.01; // custom parameter
00120         */
00121 
00122         m_torqueMax = 0.02; // [N-m]
00123         
00124         m_D_I.initialize(3,3); // inertial error bound
00125         m_D_I(1,1) = 0.35, m_D_I(1,2)=0.05, m_D_I(1,3)=0.05;
00126         m_D_I(2,1) = 0.05, m_D_I(2,2)=0.35, m_D_I(2,3)=0.05;
00127         m_D_I(3,1) = 0.05, m_D_I(3,2)=0.05, m_D_I(3,3)=0.35;
00128         m_normD_I = norm2( m_D_I );
00129 
00130         m_D_w.initialize(3,3);
00131         m_D_w(1,1) = 0.1, m_D_w(1,2) = 0.088, m_D_w(1,3) = 0.088;       
00132         m_D_w(2,1) = 0.088, m_D_w(2,2) = 0.1, m_D_w(2,3) = 0.088;       
00133         m_D_w(3,1) = 0.088, m_D_w(3,2) = 0.088, m_D_w(3,3) = 0.1;       
00134 
00135         //m_D_wprime.initialize(3,3); 
00136         //m_D_wprime(_(1,3),_(1,3)) = matrixInv3x3( m_whorl->GetMOI( ) ) * m_D_w * m_whorl->GetMOI( ); 
00137 
00138 //cout << "m_D_wprime: " << m_D_wprime << endl;
00139         
00140         m_D_B.initialize(3,3);
00141         //m_D_B(_(1,3),_(1,3)) = ~m_D_I + m_D_wprime;
00142         m_D_B(_(1,3),_(1,3)) = matrixInv3x3( m_whorl->GetMOI( ) ) * ( m_D_w + m_D_I + m_D_I*m_D_w )*m_whorl->GetMOI( );
00143 
00144         m_normD_B = norm2( m_D_B );
00145 
00146         m_D_D.initialize(3,3);
00147         m_D_D(_(1,3),_(1,3)) = m_D_w;
00148 
00149 //cout << "norm2 D_B: " << m_D_B << endl;
00150         m_normD_D = norm2( m_D_D );
00151 
00152         return( 0 );
00153 }
00154 
00155 int slidingModeControlMRPMW::Run()
00156 {
00157 
00158         Vector angularRate(3);
00159         angularRate = m_whorl->GetOmegaBL( );
00160 /*      angularRate(1) = 0.0021;
00161         angularRate(2) = -0.0004;
00162         angularRate(3) = -0.0032;
00163 */
00164         Vector MRP(3);
00165         MRP = m_whorl->GetMRP( );
00166 /*      MRP(1) = 0.001;
00167         MRP(2) = 0.0130;
00168         MRP(3) = 0.5262;
00169 */      
00170 
00171         ssfTime currentTime;
00172         currentTime = Now( );
00173         ssfTime deltaTime;
00174         deltaTime = ( currentTime.GetSeconds( ) - m_previousTime );
00175         //m_previousTime = currentTime.GetSeconds( ); // update time
00176         // dont' update time, thus provides duration time in seconds.
00177 
00178  
00179         /* Desired State. Reference Model */
00180         //ReferenceModelSC( deltaTime );
00181         ReferenceTrajectory( deltaTime );
00182 
00183         ModifiedRodriguezParameters mrp;
00184         mrp.Set( MRP );
00185         ModifiedRodriguezParameters mrpReference;
00186         mrpReference.Set( m_mrpReference );
00187 
00188         /* Determine Rotation Matirx */
00189         Rotation RBN;
00190         RBN.Set( mrp ); // body relative to inertial frame
00191         Rotation RRN;
00192         RRN.Set( mrpReference ); // reference relative to inertial frame
00193         Rotation RBR; // body relative to reference frame
00194         RBR = RBN*~RRN;
00195 
00196         /* Error */
00197         Vector angularRateError(3);
00198         angularRateError = angularRate - RBR.GetDCM( )*(m_angularRateReference); // angular rate error [rad/s]
00199         ModifiedRodriguezParameters mrpError;
00200         mrpError = mrp - mrpReference; // mrp error
00201         mrpError.Switch( 1 ); // important to activate switch condition
00202         cout << "MRP: " << mrp << endl;
00203         cout << "Rate: " << angularRate << endl;
00204         
00205         /* Set Whorl Error Values */
00206         m_whorl->SetMRPError( mrpError );
00207         m_whorl->SetAngularRateError( angularRateError );
00208 
00209         /* Set Whorl Reference Parameters */
00210         m_whorl->SetReferenceOmegaBL( m_angularRateReference );
00211         m_whorl->SetReferenceQuaternion( m_quaternionReference );
00212 
00213        /* Reaction Wheel Speed */
00214         Vector wheelSpeed(3);
00215         double meastime;
00216 
00217         m_whorl->GetMomentumWheel("REACTION_X")->GetWheelSpeed( wheelSpeed(1), meastime );
00218         m_whorl->GetMomentumWheel("REACTION_Y")->GetWheelSpeed( wheelSpeed(2), meastime );
00219         m_whorl->GetMomentumWheel("REACTION_Z")->GetWheelSpeed( wheelSpeed(3), meastime );
00220 
00221 /*
00222 wheelSpeed(1) = 10;
00223 wheelSpeed(2) = -10;
00224 wheelSpeed(3) = 10;
00225 */
00226         /* Reaction Wheel Angular Momentum */
00227         Vector hs(3);
00228         Vector w(3);
00229         Matrix ws(1,1);
00230         for ( int i=1; i<4; i++ )
00231         {
00232                 w(1) = A(1,i);
00233                 w(2) = A(2,i);
00234                 w(3) = A(3,i);
00235                 ws = ~w*angularRate;
00236                 hs(i) = MOI_sw(i,i) * ( ws(1,1) + wheelSpeed(i) );
00237         }
00238 
00239         Vector estimatef(3);
00240         estimatef = -m_estimateInertiaInv*skew( angularRate )*( m_whorl->GetMOI( )*angularRate + A*hs );
00241 
00242 //      cout << "I^-1: " << m_estimateInertiaInv << endl;
00243 //      cout << "I: " << m_whorl->GetMOI( ) << endl;
00244 
00245 //      cout << "hs: " << hs << endl;
00246 //      cout << "A: " << A << endl;
00247 
00248         ModifiedRodriguezParameters mrpRate;
00249         AttitudeModels MRPRate;
00250         MRPRate.MRPKinematic( mrpError, angularRateError, mrpRate );
00251 
00252         /* Equavalent Control  */
00253         Vector ueq(3);
00254 
00255         ueq = m_estimateBinv*( -estimatef + RBR.GetDCM( )*(m_angularAccelReference)
00256                 - skew( angularRate )*RBR.GetDCM( )*(m_angularRateReference) - m_gainMRPRate*mrpRate 
00257                 - m_gainMRPError*mrpError ); // [N-m]
00258         
00259         //cout << m_estimateBinv << endl;
00260         //cout << estimatef << endl;
00261         //cout << m_gainMRPRate*mrpRate << endl;
00262         //cout << m_gainMRPError*mrpError << endl; // [N-m]
00263         
00264         //cout << "ueq: " << ueq << endl;
00265 
00266         /* Sliding Variable */
00267         Vector s(3);
00268         double dt = 0.5;
00269 
00270         //cerr << "angular error: " << angularRateError << endl;
00271         //cerr << "mrp error: " << mrpError << endl;
00272 
00273         //cout << "MRP Rate Gain: " << m_gainMRPRate << endl;
00274 
00275         //cout << "MRP Error Gain: " << m_gainMRPError << endl;
00276         //cerr << "dt: " << dt << endl;
00277 
00278         s = angularRateError + m_gainMRPRate*mrpError + m_gainMRPError*mrpError*dt;
00279         //cout << "s: " << s << endl;
00280 
00281         /* Saturation Function */
00282         Vector sat(3);
00283         sat = Saturation( s, m_phi );
00284         //cout << "sat: " << sat << endl;
00285         
00286         Vector D_s(3);
00287         D_s = A*hs;
00288         
00289         Vector one(3);
00290         one(1) = 1;
00291         one(2) = 1;
00292         one(3) = 1;
00293         
00294         Vector F(3);
00295 /*
00296         double normRate = norm2( angularRate );
00297 
00298         F =  (m_normEstimateInertiaInv)*( 2*m_normD_I + pow( m_normD_I, 2 ) )* m_normEstimateInertia  * pow( normRate,2 )*one 
00299                 + (m_normEstimateInertiaInv)*( m_normD_I + m_normD_D + pow( m_normD_I, 2 )  ) * normRate * norm2( D_s )*one
00300                 + (m_normEstimateInertiaInv)*( 1 + (m_normD_I) )*m_torqueMax*one; 
00301 */
00302 
00303         F = matrixInv3x3( m_whorl->GetMOI( ) )*Vabs( ( -skew( angularRate )*m_D_I + m_D_I*skew( angularRate ) + m_D_I*skew( angularRate )*m_D_I )*m_whorl->GetMOI( )*angularRate
00304                 + ( -skew( angularRate )*m_D_D + m_D_I*skew( angularRate ) + m_D_I*skew( angularRate )*m_D_D )*D_s
00305                 + ( eye(3) + m_D_I )*m_torqueMax*one  );
00306 
00307 cout << "F: " << F << endl;
00308 //cout << "m_normD_D: " << m_normD_D << endl;
00309 //cout << "normRate: " << normRate << endl;
00310 //cout << "Ds: " << D_s << endl;
00311 //cout << "norm2 Ds: " << norm2( D_s ) << endl;
00312 //cout << "1: " << (m_normEstimateInertiaInv)*( m_normD_I + m_normD_D + pow( m_normD_I, 2 )  ) * normRate * norm2( D_s )*one << endl;
00313 //cout << "2: " << (m_normEstimateInertiaInv)*( 1 + (m_normD_I) )*m_torqueMax*one << endl; 
00314 //cout << "3: " << (m_normEstimateInertiaInv)*( 2*m_normD_I + pow( m_normD_I, 2 ) )* m_normEstimateInertia  * pow( normRate,2 )*one << endl;
00315 
00316         /* Robust Control Gain */
00317         Vector K(3);
00318         K = matrixInv3x3( eye(3) - m_D_B )*( F + m_D_B*(Vabs( estimatef - RBR.GetDCM( )*(m_angularAccelReference)
00319                 + skew( angularRate )*RBR.GetDCM( )*(m_angularRateReference) + m_gainMRPRate*mrpRate
00320                 + m_gainMRPError*mrpError ) ) + m_eta );
00321 
00322 /*cout <<       Vabs( m_D_B*( estimatef - RBR.GetDCM( )*(m_angularAccelReference)
00323                 + skew( angularRate )*RBR.GetDCM( )*(m_angularRateReference) + m_gainMRPRate*mrpRate
00324                 + m_gainMRPError*mrpError ) )   << endl;
00325 */
00326         //cout << "K: " << K << endl;
00327 
00328         Matrix matK(3,3);
00329         matK(1,1) = K(1); 
00330         matK(2,2) = K(2); 
00331         matK(3,3) = K(3); 
00332 cerr << "K: " << ~K << endl;
00333 //cerr << "m_estimateBinv: " << m_estimateBinv << endl;
00334         /* Robust Control  */
00335         Vector urob(3);
00336         urob = m_estimateBinv*matK*sat; // [N-m]
00337 
00338 
00339         //out << "K: " << K << endl;
00340 
00341         //cout << "urob: " << urob << endl;
00342 
00343         /* Sliding Mode Control Law */
00344         m_controlTorque = ( ueq - urob ); // [N-m]
00345         cout << "control before A: " << ~m_controlTorque << endl;
00346         m_controlTorque = ~A*m_controlTorque; // [N-m]
00347 
00348         cout << "A: " << A << endl;
00349         cout << "A inv: " << (~A) << endl;
00350         cout << "eq: " << ~ueq << endl;
00351         cout << "rob: " << ~urob << endl;
00352 
00353         cout << "control after A: " << ~m_controlTorque << endl;
00354 
00355         m_controlTorque = WheelSaturation( m_controlTorque );
00356         
00357         cout << "control after Wheel Sat: " << ~m_controlTorque << endl;
00358         
00359         /* Set the desird control torque in the whorl object */
00360         m_whorl->SetControl( m_controlTorque ); // [N-m]
00361 
00362         /* Set the torque for the momentum wheels to produce */
00363         SetWheelTorque( m_controlTorque ); // [N-m]
00364 
00365         return( 0 );
00366 }
00367 
00368 // Do not change the comments below - they will be added automatically by CVS
00369 /*****************************************************************************
00370 *       $Log: slidingModeControlMRPMW.cpp,v $
00371 *       Revision 1.2  2007/08/31 15:53:26  jayhawk_hokie
00372 *       Modified.
00373 *       
00374 *       Revision 1.1  2007/07/24 09:25:15  jayhawk_hokie
00375 *       Initial Submission.
00376 *       
00377 *       
00378 *       
00379 *
00380 ******************************************************************************/

Generated on Wed Sep 5 12:54:25 2007 for DSACSS Operational Code by  doxygen 1.3.9.1