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

Controller.cpp

Go to the documentation of this file.
00001 //////////////////////////////////////////////////////////////////////////////////////////////////
00002 /*! \file Controller.cpp
00003 *  \brief Implementation of the Controller class.
00004 *  \author $Author: jayhawk_hokie $
00005 *  \version $Revision: 1.10 $
00006 *  \date    $Date: 2007/08/31 15:53:26 $
00007 *//////////////////////////////////////////////////////////////////////////////////////////////////
00008 /*! 
00009 */
00010 //////////////////////////////////////////////////////////////////////////////////////////////////
00011 
00012 #include "Controller.h"
00013 
00014 Controller::Controller( ) 
00015 { 
00016         m_quaternionReference.initialize(4);
00017         m_angularRateReference.initialize(3); 
00018         m_angularAccelReference.initialize(3); 
00019         m_mrpReference.initialize(3);
00020 } 
00021 
00022 Controller::Controller(Whorl* ptr_whorl)
00023 {
00024         m_whorl = ptr_whorl;
00025         A = FindA();
00026         MOI_sw = FindMOI_sw();
00027 
00028         m_quaternionReference.initialize(4);
00029         m_angularRateReference.initialize(3); 
00030         m_angularAccelReference.initialize(3); 
00031         m_mrpReference.initialize(3);
00032 
00033 }
00034 
00035 Controller::~Controller( )
00036 {
00037 }
00038 
00039 Matrix Controller::FindA()
00040 {
00041         
00042         static int wheel_num = m_whorl->GetMomentumWheel("REACTION_X")->GetWheelCount();
00043         Matrix temp(wheel_num,3);
00044         string wheel("REACTION_");
00045         string axis;
00046         string name;
00047         for (int ii =1;ii<=wheel_num;ii++)
00048         {
00049                 // TEMP - FIND A BETTER WAY TO INCREMENT AXIS
00050                 if (ii==1) axis = "X";
00051                 if (ii==2) axis = "Y";
00052                 if (ii==3) axis = "Z";          
00053                 
00054                 //sprintf(number,"%d",ii);
00055                 string name =wheel+axis;
00056                 MomentumWheel *whl = m_whorl->GetMomentumWheel(name);
00057                 Vector tempVect = whl->GetWheelAxis();
00058                 temp(_,ii)=tempVect;
00059         }                       
00060         return temp;
00061 }
00062 
00063 Matrix Controller::FindMOI_sw()
00064 {
00065         
00066         static int wheel_num = m_whorl->GetMomentumWheel("REACTION_X")->GetWheelCount();
00067         Matrix temp(wheel_num,wheel_num);
00068         string wheel("REACTION_");
00069         string axis;
00070         string name;
00071         for (int ii =1;ii<=wheel_num;ii++)
00072         {
00073                 if (ii==1) axis="X";
00074                 if (ii==2) axis="Y";
00075                 if (ii==3) axis="Z";
00076 
00077                 //sprintf(number,"%d",ii);
00078                 string name =wheel+axis;
00079                 temp(ii,ii)=m_whorl->GetMomentumWheel(name)->GetAxialInertia();
00080         }                       
00081         return temp;
00082 }
00083 
00084 int Controller::SetWheelTorque(Vector Torque)
00085 {
00086         static int wheel_num = m_whorl->GetMomentumWheel("REACTION_X")->GetWheelCount();
00087         string wheel("REACTION_");
00088         string axis;
00089         string name;
00090         for (int ii =1;ii<=wheel_num;ii++)
00091         {
00092                 if (ii==1) axis="X";
00093                 if (ii==2) axis="Y";
00094                 if (ii==3) axis="Z";
00095                 
00096                 //sprintf(number,"%d",ii);
00097                 string name = wheel+axis;
00098                 m_whorl->GetMomentumWheel(name)->SetWheelTorque(Torque(ii));
00099         }
00100         return 0;
00101 }
00102 
00103 
00104 int Controller::SetThrusterTorque(char Thruster_set, int Number_of_Pulses, double Pulse_legth)
00105 {
00106         return 0;
00107 }
00108 
00109 void Controller::ReferenceModelSC( ssfTime deltaTime )
00110 {
00111         // Setup an integrator and any special parameters
00112         RungeKuttaIntegrator myIntegrator;
00113         myIntegrator.SetNumSteps(10);
00114 
00115         // Integration times
00116         vector<ssfTime> integrationTimes;
00117         ssfTime begin(0);
00118         ssfTime end( begin + deltaTime.GetSeconds( ) );
00119         integrationTimes.push_back(begin);
00120         integrationTimes.push_back(end);
00121 
00122         // Create the initial attitude state
00123         Vector state(7);
00124         state( _(1,4) ) = m_quaternionReference;
00125         state( _(5,7) ) = m_angularRateReference; 
00126         
00127         SpecificFunctor AttitudeForcesFunctor( &NullFunctor );
00128 
00129         // Create the matrix of parameters needed for the RHS - MOI
00130         Matrix Parameters(3,3);
00131         Parameters = m_whorl->GetMOI( );
00132 
00133         // Integrate over the desired time interval
00134         Matrix history = myIntegrator.Integrate(
00135                              integrationTimes,           // seconds
00136                              &AttituteDynamics_QuaternionAngVel,
00137                              state,
00138                              NULL,
00139                              NULL,
00140                              Parameters,
00141                              AttitudeForcesFunctor
00142                          );
00143 
00144         m_quaternionReference = ~history( 11, _(2,5) );
00145         m_angularRateReference = ~history( 11, _(6,8) ); 
00146 
00147         Quaternion quaternion( m_quaternionReference );
00148         ModifiedRodriguezParameters mrpReference( quaternion );
00149         mrpReference.Switch( 1 ); // force switching if greater than 180 deg.
00150         m_mrpReference = mrpReference;
00151 
00152         state( _(1,4) ) = m_quaternionReference;
00153         state( _(5,7) ) = m_angularRateReference; 
00154         Vector derivative = AttituteDynamics_QuaternionAngVel( end, state, NULL, NULL, Parameters, AttitudeForcesFunctor );
00155         m_angularAccelReference = derivative( _(5,7) );
00156         
00157         return;
00158 }
00159 
00160 // Torque function that will be called each timestep
00161 Vector NullFunctor(const ssfTime& _pSSFTime, const OrbitState& _pOrbitState, const AttitudeState& _pAttitudeState)
00162 {
00163         return Vector(3);
00164 }
00165 
00166 
00167 void Controller::ReferenceTrajectory( ssfTime deltaTime )
00168 {
00169         // For Wheel Bias Ramp up speed.
00170         if ( deltaTime.GetSeconds( ) < 25 )
00171         {
00172                 deltaTime = 0;
00173         }
00174         else
00175         { 
00176                 deltaTime = deltaTime-25;
00177         }
00178 
00179         double time = deltaTime.GetSeconds( ); // [s]
00180 
00181         // Setup an integrator and any special parameters
00182         RungeKuttaIntegrator myIntegrator;
00183         myIntegrator.SetNumSteps(10);
00184 
00185         // Integration times
00186         vector<ssfTime> integrationTimes;
00187         ssfTime begin(deltaTime);
00188         ssfTime end( begin + 0.5 );
00189         integrationTimes.push_back(begin);
00190         integrationTimes.push_back(end);
00191 
00192         // Create the initial attitude state
00193         Vector state(4);
00194         state( _(1,4) ) = m_quaternionReference;
00195         
00196         SpecificFunctor AttitudeForcesFunctor( &NullFunctor );
00197 
00198         // Create the matrix of parameters needed for the RHS - MOI
00199         Matrix Parameters(3,3);
00200         Parameters = m_whorl->GetMOI( );
00201         // Integrate over the desired time interval
00202         Matrix history = myIntegrator.Integrate(
00203                              integrationTimes,           // seconds
00204                              &AttitudeTrajectory,
00205                              state,
00206                              NULL,
00207                              NULL,
00208                              Parameters,
00209                              AttitudeForcesFunctor
00210                          );
00211         
00212         m_quaternionReference = ~history( 11, _(2,5) );
00213         //m_angularRateReference = ~history( 11, _(6,8) ); 
00214 
00215         double omega = 0.025; // [rad/s]
00216         double C1 = 30;
00217         double C2 = -0.1; 
00218         m_angularRateReference(1) = 0; //sin( omega*time ); 
00219         m_angularRateReference(2) = 0; //sin( omega*time ); 
00220         m_angularRateReference(3) = exp( -time/C1 )*sin( omega*time ) + C2; 
00221 
00222         Quaternion quaternion( m_quaternionReference );
00223         ModifiedRodriguezParameters mrpReference( quaternion );
00224         mrpReference.Switch( 1 ); // force switching if greater than 180 deg.
00225         m_mrpReference = mrpReference;
00226 
00227         //Vector derivative = AttitudeTrajectory( end, state, NULL, NULL, Parameters, AttitudeForcesFunctor );
00228         //m_angularAccelReference = derivative( _(5,7) );
00229         m_angularAccelReference(1) = 0;  
00230         m_angularAccelReference(2) = 0;  
00231         m_angularAccelReference(3) = -exp( -time/C1 )*sin( omega*time )/C1 + omega*exp( -time/C1 )*cos( omega*time ); 
00232         
00233         return;
00234 }
00235 
00236 
00237 Vector AttitudeTrajectory(const ssfTime &_time, const Vector& _state, Orbit *_Orbit, Attitude *_Attitude, const Matrix &_parameters, const Functor &_torqueFuncPtr)
00238 {
00239         static Vector stateDot(4);
00240 
00241         static Vector _quaternionRate(4);
00242 
00243         static Quaternion q;
00244         q = _state( _(1,4) );
00245 
00246         static Quaternion qRate;
00247 
00248         double time = _time.GetSeconds( );
00249         
00250         double omega = 0.025; // [rad/s]
00251         double C1 = 30;
00252         double C2 = -0.1; 
00253         static Vector angularRate(3);
00254         angularRate(1) = 0; //sin( omega*time ); 
00255         angularRate(2) = 0; //sin( omega*time ); 
00256         angularRate(3) = exp( -time/C1 )*sin( omega*time ) + C2; 
00257         /*
00258         angularAccel(1) = 0;  
00259         angularAccel(2) = 0;  
00260         angularAccel(3) = -exp( -time/C1 )*sin( omega*time )/C1 + omega*exp( -time/C1 )*cos( omega*time ); 
00261         */
00262 
00263         AttitudeModels Rate;
00264         Rate.QuaternionKinematic( q, angularRate, qRate );
00265 
00266         stateDot( _(1,4) ) = qRate;
00267 
00268 
00269         return (stateDot);
00270 }
00271 
00272 
00273 Vector Controller::Saturation( Vector _s, Vector _phi )
00274 {
00275         Vector _sat(3);
00276 
00277         for( int iterator = 1; iterator < 4; iterator++ )
00278         {
00279                 if( fabs( _s( iterator ) ) <= _phi( iterator ) )
00280                 {
00281                         _sat( iterator ) = _s( iterator )/_phi( iterator );
00282                 }
00283                 else
00284                 {
00285                         _sat( iterator ) = SignSingle( _s( iterator ) );
00286                 }
00287         }
00288         return( _sat );
00289 }
00290 
00291 Vector Controller::Sign( Vector _s )
00292 {
00293         Vector _sign(3);
00294         
00295         for( int iterator = 1; iterator < 4; iterator++ )
00296         {
00297                 _sign( iterator ) = SignSingle( _s( iterator ) );
00298         }
00299         return( _sign );
00300 }
00301 
00302 
00303 Vector Controller::WheelSaturation( Vector _u )
00304 {
00305         Vector _uSat(3);
00306 
00307         for( int iterator = 1; iterator < 4; iterator++ )
00308         {
00309                 if( fabs( _u( iterator ) ) <= m_uMax )
00310                 {
00311                         _uSat( iterator ) = _u( iterator );
00312                 }
00313                 else
00314                 {
00315                         _uSat( iterator ) = m_uMax*SignSingle( _u( iterator ) );
00316                 }
00317         }
00318         return( _uSat );
00319 }
00320 
00321 
00322 
00323 double Controller::SignSingle( double _s )
00324 {
00325         if( _s >= 0 )
00326         {
00327                 _s = 1;
00328         }
00329         else
00330         {
00331                 _s = -1;
00332         }
00333 
00334         return( _s );
00335 }
00336 
00337 // Do not change the comments below - they will be added automatically by CVS
00338 /*****************************************************************************
00339 *       $Log: Controller.cpp,v $
00340 *       Revision 1.10  2007/08/31 15:53:26  jayhawk_hokie
00341 *       Modified.
00342 *       
00343 *       Revision 1.9  2007/07/24 09:26:11  jayhawk_hokie
00344 *       Added attitude reference models.
00345 *       
00346 *       Revision 1.8  2007/05/23 22:18:10  jayhawk_hokie
00347 *       Implemented spacecraft reference model.
00348 *       
00349 *       Revision 1.7  2007/03/27 19:42:05  jayhawk_hokie
00350 *       Removed unused variables.
00351 *       
00352 *       Revision 1.6  2007/02/09 14:34:33  jayhawk_hokie
00353 *       Added XML wheel parsing technique.
00354 *       
00355 *       Revision 1.5  2003/08/13 23:16:42  mavandyk
00356 *       Altered structure as outline in meeting.
00357 *       
00358 *       Revision 1.4  2003/08/01 11:19:00  mavandyk
00359 *       Changed the class structure, and fixed bugs.
00360 *       
00361 *       Revision 1.3  2003/07/07 14:11:24  simpliciter
00362 *       Removed GetWhorlObject() calls following Whorl pointers.
00363 *       
00364 *       
00365 *
00366 ******************************************************************************/

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