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

MomentumWheel.cpp

Go to the documentation of this file.
00001 //////////////////////////////////////////////////////////////////////////////////////////////////
00002 /*! \file MomentumWheel.cpp
00003 *  \brief Implementation of the MomentumWheel class
00004 *  \author $Author: jayhawk_hokie $
00005 *  \version $Revision: 1.35 $
00006 *  \date    $Date: 2007/07/24 09:39:39 $
00007 *//////////////////////////////////////////////////////////////////////////////////////////////////
00008 /*! 
00009 */
00010 //////////////////////////////////////////////////////////////////////////////////////////////////
00011 
00012 
00013 #include "MomentumWheel.h"
00014 
00015 int MomentumWheel::m_WheelCount = 0;
00016 
00017 //////////////////////////////////////////////////////////////////////
00018 // Construction/Destruction
00019 //////////////////////////////////////////////////////////////////////
00020 
00021 MomentumWheel::MomentumWheel()
00022 {
00023         ++m_WheelCount;
00024 }
00025 
00026 
00027 MomentumWheel::MomentumWheel( TiXmlHandle handle, string wheel ) 
00028 {
00029         Initialize( handle, wheel );
00030 }
00031 
00032 MomentumWheel::~MomentumWheel()
00033 {
00034         Deinitialize( );
00035 }
00036 
00037 //////////////////////////////////////////////////////////////////////
00038 //  Facilitators
00039 /////////////////////////////////////////////////////////////////////
00040 
00041 int MomentumWheel::Initialize( TiXmlHandle handle, string wheel ) 
00042 {
00043         ++m_WheelCount;
00044 
00045         m_WheelAxis.initialize(3);
00046 
00047         m_exitConditionData = 1;
00048         
00049         int daisy_chain;
00050         int wheel_address;
00051         int response;
00052         double torque_limit_low; 
00053         double torque_limit_high;
00054         double axialInertia;
00055         double BiasSpeed;
00056         
00057         Vector wheel_axis(3);
00058         
00059         string port;
00060 
00061         TiXmlHandle wheelHandle = handle.FirstChild("HARDWARE_PROPERTIES").FirstChild("WHEELS").FirstChild(wheel);
00062 
00063         response = wheelHandle.Child("DAISY_CHAIN",0).Element() -> QueryIntAttribute("value", &daisy_chain);
00064         checkResponse(response);
00065 
00066         response = wheelHandle.Child("DCADDR", 0).Element() -> QueryIntAttribute("value", &wheel_address);
00067         checkResponse(response);
00068 
00069         response = wheelHandle.Child("LIMIT_LOW", 0).Element() -> QueryDoubleAttribute("value", &torque_limit_low);
00070         checkResponse(response);
00071 
00072         response = wheelHandle.Child("LIMIT_HIGH", 0).Element() -> QueryDoubleAttribute("value", &torque_limit_high);
00073         checkResponse(response);
00074 
00075         response = wheelHandle.Child("INERTIA", 0).Element() -> QueryDoubleAttribute("value", &axialInertia);
00076         checkResponse(response);
00077 
00078         response = wheelHandle.Child("BIAS_SPEED", 0).Element() -> QueryDoubleAttribute("value", &BiasSpeed);
00079         checkResponse(response);
00080 
00081         for (int i=1; i<4; i++)
00082         {
00083                 response = wheelHandle.Child("AXIS", i-1).Element() -> QueryDoubleAttribute( "value", &wheel_axis(i) );
00084         }
00085 
00086         port = wheelHandle.Child("PORT", 0).Element() -> Attribute("value");
00087 
00088         // Get Wheel Speed Coefficients
00089         Vector Coefficients1(4);
00090         for (int i=1; i<5; i++)
00091         {
00092                 response = wheelHandle.Child("COEFFICIENTS1", i-1).Element() -> QueryDoubleAttribute( "value", &Coefficients1(i) );
00093         }
00094         Vector Coefficients2(3);
00095         for (int i=1; i<4; i++)
00096         {
00097                 response = wheelHandle.Child("COEFFICIENTS2", i-1).Element() -> QueryDoubleAttribute( "value", &Coefficients2(i) );
00098         }
00099         Vector Coefficients3(3);
00100         for (int i=1; i<4; i++)
00101         {
00102                 response = wheelHandle.Child("COEFFICIENTS3", i-1).Element() -> QueryDoubleAttribute( "value", &Coefficients3(i) );
00103         }
00104         Vector Coefficients4(4);
00105         for (int i=1; i<5; i++)
00106         {
00107                 response = wheelHandle.Child("COEFFICIENTS4", i-1).Element() -> QueryDoubleAttribute( "value", &Coefficients4(i) );
00108         }
00109         // Set Wheel Speed Coefficients
00110         SetWheelSpeedCoefficients( Coefficients1, Coefficients2, Coefficients3, Coefficients4 );
00111         
00112         // set local and physical momentum wheel values
00113 
00114         SetChainNumber( daisy_chain );
00115         SetWheelAddress( wheel_address );
00116         SetPort( port );
00117         SetWheelTorqueLimits( torque_limit_low, torque_limit_high );
00118         SetWheelAxis( wheel_axis );
00119         SetAxialInertia( axialInertia );
00120         SetBiasSpeed( BiasSpeed );      
00121         SetWheelName( wheel );
00122 
00123         m_PhysicalMomentumWheel.Initialize( );
00124   
00125         s_mapStringValues["ON"]                 = ON;
00126         s_mapStringValues["OFF"]                = OFF;
00127         // Determine whether to log wheel speed.
00128         m_LogData = wheelHandle.Child( "LOGGING", 0 ).Element() -> Attribute( "value" );
00129 
00130         pthread_mutex_init( &m_DataMutex, NULL );
00131         
00132         switch(  s_mapStringValues[ m_LogData ] )
00133         {
00134                 case ON:
00135                         m_WheelHistory = new WheelHistory( handle, wheel );
00136                         StartWheelSpeedDataLogging( );
00137                         break;
00138                 case OFF:
00139                         break;
00140                 default:
00141                         break;
00142         }
00143 
00144         return( 0 );
00145 }
00146 
00147 int MomentumWheel::Deinitialize( )
00148 {
00149         --m_WheelCount;
00150     
00151         // Delete Pointer       
00152         switch(  s_mapStringValues[ m_LogData ] )
00153         {
00154                 case ON:
00155                         StopWheelSpeedDataLogging( );
00156                         delete m_WheelHistory;
00157                         break;
00158                 case OFF:
00159                         break;
00160                 default:
00161                         break;
00162         }
00163         
00164         //delete m_WheelAxis;
00165 
00166         //m_PhysicalMomentumWheel.Deinitialize( );
00167         return( 0 );
00168 }
00169 
00170 int MomentumWheel::Stop()
00171 {
00172         m_PhysicalMomentumWheel.Stop();
00173         return( 0 );
00174 }
00175 
00176 int MomentumWheel::TorqueCommand( double _power )
00177 {
00178         m_PhysicalMomentumWheel.TorqueCommand( _power );
00179         return( 0 );
00180 }
00181 
00182 
00183 void MomentumWheel::StartSpeedQuery( )
00184 {
00185         m_PhysicalMomentumWheel.StartSpeedQuery( );
00186 }
00187 
00188 void MomentumWheel::StopSpeedQuery( )
00189 {
00190         m_PhysicalMomentumWheel.StopSpeedQuery( );
00191 }
00192 
00193 void MomentumWheel::SetWheelTorque( double wheelTorque )
00194 {
00195         m_PhysicalMomentumWheel.CommandWheelTorque( wheelTorque );
00196 }
00197 
00198 void MomentumWheel::SetWheelSpeed(double wheelOmega)
00199 {
00200         m_PhysicalMomentumWheel.SetWheelSpeed( wheelOmega );
00201         m_PhysicalMomentumWheel.CommandWheelSpeed( );
00202 }
00203 
00204 void MomentumWheel::StartPID( )
00205 {
00206         m_PhysicalMomentumWheel.StartPID( );
00207 }
00208 
00209 void MomentumWheel::StopPID( )
00210 {
00211         m_PhysicalMomentumWheel.StopPID( );
00212 }
00213 
00214 void MomentumWheel::SetSystemTorque( double wheelTorque )
00215 {
00216         m_PhysicalMomentumWheel.CommandSystemTorque( wheelTorque );
00217 }
00218 
00219 
00220 ////////////////////////////////////////////////////////////////
00221 //Mutators
00222 /////////////////////////////////////////////////////////////////
00223 
00224 void MomentumWheel::SetWheelAddress( int wheeladdress )
00225 {
00226         m_PhysicalMomentumWheel.SetWheelAddress( wheeladdress );
00227 }
00228 
00229 void MomentumWheel::SetWheelAxis( const Vector& newWheelAxis ) 
00230 {
00231     m_WheelAxis = newWheelAxis;
00232 }
00233 
00234 void MomentumWheel::SetChainNumber( int daisyChain )
00235 {
00236         m_PhysicalMomentumWheel.SetDaisyChainNumber( daisyChain );
00237 }
00238 
00239 void MomentumWheel::SetPort( string Port )
00240 {
00241         m_PhysicalMomentumWheel.SetDaisyChainPort( Port );
00242 }
00243 
00244 void MomentumWheel::SetBiasSpeed( double BiasSpeed )
00245 {
00246         m_PhysicalMomentumWheel.SetBiasSpeed( BiasSpeed );
00247 }
00248 
00249 void MomentumWheel::SetAxialInertia( double axialInertia )
00250 {
00251         m_PhysicalMomentumWheel.SetAxialInertia( axialInertia );
00252 }
00253 
00254 void MomentumWheel::SetWheelTorqueLimits( double torque_limit_low, double torque_limit_high )
00255 {
00256         m_PhysicalMomentumWheel.SetWheelTorqueLimits( torque_limit_low, torque_limit_high );
00257 }
00258 
00259 void MomentumWheel::SetWheelName( string _wheelName )
00260 {
00261         m_PhysicalMomentumWheel.SetWheelName( _wheelName );
00262 }
00263 
00264 void MomentumWheel::SetWheelSpeedCoefficients( Vector _Coefficients1, Vector _Coefficients2, Vector _Coefficients3, Vector _Coefficients4 )
00265 {
00266         m_PhysicalMomentumWheel.SetWheelSpeedCoefficients( _Coefficients1, _Coefficients2, _Coefficients3, _Coefficients4 );
00267 }
00268 
00269 
00270 /////////////////////////////////////////////////////////////////////
00271 //Inspectors
00272 ////////////////////////////////////////////////////////////////////////
00273 
00274 
00275 void MomentumWheel::GetWheelSpeed( double& Speed, double& measTime ) 
00276 {
00277         m_PhysicalMomentumWheel.GetWheelSpeed( Speed, measTime );
00278 }
00279 
00280 
00281 double MomentumWheel::GetWheelTorque( )
00282 {
00283         return( m_PhysicalMomentumWheel.QueryWheelTorque( ) );
00284 }
00285 
00286 Vector MomentumWheel::GetWheelAxis( ) const 
00287 {
00288         return( m_WheelAxis );
00289 }
00290 
00291 
00292 int MomentumWheel::GetWheelCount( )
00293 {
00294         return( m_WheelCount );
00295 }
00296 
00297 
00298 double MomentumWheel::GetAxialInertia( )
00299 {
00300         return( m_PhysicalMomentumWheel.GetAxialInertia( ) );
00301 }
00302 
00303 string MomentumWheel::GetWheelName( )
00304 {
00305         return( m_PhysicalMomentumWheel.GetWheelName( ) );
00306 }
00307 
00308 int MomentumWheel::GetWheelFD()
00309 {
00310         return( m_PhysicalMomentumWheel.GetWheelFD( ) );
00311 }
00312 
00313 double MomentumWheel::GetAmps()
00314 {
00315         return( m_PhysicalMomentumWheel.GetAmps( ) );
00316 }
00317 
00318 double MomentumWheel::GetVolts()
00319 {
00320         return( m_PhysicalMomentumWheel.GetVolts( ) );
00321 }
00322 
00323 
00324 /////////////////////////////////////////////////////////////////////
00325 // Data Logging
00326 ////////////////////////////////////////////////////////////////////////
00327 
00328 void MomentumWheel::StartWheelSpeedDataLogging( )
00329 {
00330         pthread_create( &m_DataSpeedThreadID, NULL, DataWheelSpeed, (void*) this );
00331         return;
00332 }
00333 
00334 void* DataWheelSpeed( void* arg )
00335 {
00336         MomentumWheel *wheel = (MomentumWheel*) arg;
00337 
00338         // in future should be a variable
00339         int readDelay = 50000; // [usec]
00340 
00341         double measurementTime = 0;
00342         ssfTime measurementTime2 = 0;
00343         double wheelSpeed = 0;
00344 
00345         int exit = 1;
00346 
00347         while( exit )
00348         {
00349                 pthread_testcancel( );
00350                 wheel -> GetWheelSpeed( wheelSpeed, measurementTime );
00351                 measurementTime2= measurementTime;
00352                 
00353                 // Lock Reaction Wheel Parameters
00354                 pthread_mutex_lock(&wheel->m_DataMutex);
00355                         wheel -> m_WheelHistory -> AppendHistory( measurementTime2, wheelSpeed );
00356                 // Unlock Reaction Wheel Parameters
00357                 pthread_mutex_unlock(&wheel->m_DataMutex);
00358 
00359                 pthread_testcancel( );
00360     
00361                 pthread_mutex_unlock( &wheel->m_DataMutex );
00362                         exit = wheel->m_exitConditionData;
00363                 pthread_mutex_unlock( &wheel->m_DataMutex );
00364         
00365                 pthread_testcancel( );
00366         
00367                 usleep( readDelay );
00368         } 
00369       return arg;
00370 }
00371 
00372 void MomentumWheel::StopWheelSpeedDataLogging( )
00373 {
00374         pthread_mutex_unlock( &m_DataMutex );
00375                 m_exitConditionData = 0;
00376         pthread_mutex_unlock( &m_DataMutex );
00377         
00378         pthread_join( m_DataSpeedThreadID, NULL );
00379         return;
00380 }
00381 
00382 
00383 
00384 
00385 // Do not change the comments below - they will be added automatically by CVS
00386 /*****************************************************************************
00387 *       $Log: MomentumWheel.cpp,v $
00388 *       Revision 1.35  2007/07/24 09:39:39  jayhawk_hokie
00389 *       Removed matrix and vector pointers.
00390 *       
00391 *       Revision 1.34  2007/05/30 23:48:43  jayhawk_hokie
00392 *       Modified thread termination.
00393 *       
00394 *       Revision 1.33  2007/03/27 19:07:16  jayhawk_hokie
00395 *       Updated Momentum Wheels.
00396 *       
00397 *       Revision 1.32  2007/02/06 20:48:16  jayhawk_hokie
00398 *       Update for XML parsing for Momentum Wheels.
00399 *       
00400 *       Revision 1.31  2005/02/25 18:40:52  cakinli
00401 *       Created Makefiles and organized include directives to reduce the number of
00402 *       include paths.  Reorganized libraries so that there is now one per source
00403 *       directory.  Each directory is self-contained in terms of its Makefile.
00404 *       The local Makefile in each directory includes src/config.mk, which has all
00405 *       the definitions and general and pattern rules.  So at most, to see what
00406 *       goes into building a target, a person needs to examine the Makefile in
00407 *       that directory, and ../config.mk.
00408 *       
00409 *       Revision 1.30  2004/05/11 20:18:08  bstreetman
00410 *       Vast improvements to torque controller
00411 *       
00412 *       Revision 1.29  2004/04/19 15:00:38  bstreetman
00413 *       Fixed a config and a shutdown bug
00414 *       
00415 *       Revision 1.28  2004/03/04 23:13:08  bstreetman
00416 *       Big changes! new functions and functionality.  FUnctions: GetVolts() -- get motor voltage.  GetAmps() -- ge current used by motors.  GetWheelFD() -- get the wheel comm file descriptor.  SetBiasSpeed() -- Set the bias speed used in momentum wheel mode.  SetSystemTorque() -- old style torque function, used mainly by me.  friend RUnTorqueCOntroller() -- new torque controller, runs in a separate thread.  StartTorqueController() -- spawns torque thread, called internally.  Other Changes:  config parameters -- added BiasSpeed confige parameter, sets bias speed and commands the wheel to go there.  SetWheelSpeed() -- no more PID controller, uses a function created by the data i took.  SetWHeelTorque() -- happens in a better way now based on tons of data and hdot = g.
00417 *       
00418 *       Revision 1.27  2003/12/16 17:20:26  bstreetman
00419 *       A bit o' fall cleaning.  Just look at how pretty it is.
00420 *       
00421 *       Revision 1.26  2003/11/14 20:15:44  bstreetman
00422 *       Apparently I broke the PID controller with the last update, but now it works again.  I also added a StopPID() function to end the thread of the PID controller.  To spin up to 900 rpm (about half speed) use SetWheelSpedd(92).  If you wnat to then switch to torque control sleep for about 12 seconds and run StopPID() and you should be good to go.
00423 *       
00424 *       Revision 1.25  2003/10/17 20:01:34  bstreetman
00425 *       Wheel code updated to reflect hardware changes.  All wheels now considered on separate serial port.  Wheels also changed to read wheel velocity in a separate thread.  Thread is fired off by StartSpeedQuery() function.  Wheel Speed and measurement timestamp are set by the function GetWheelSpeed(speed, time).  See SpeedTest.cpp in the work directory for a sample using the new code.
00426 *       
00427 *       Revision 1.24  2003/09/15 21:44:03  bstreetman
00428 *       Removed hardcoded parameters, ready for pure config file use
00429 *       
00430 *       Revision 1.23  2003/08/18 19:30:43  mavandyk
00431 *       Fixed config parsing.
00432 *       
00433 *       Revision 1.22  2003/08/06 21:18:48  bstreetman
00434 *       Updated to work with config parser
00435 *       
00436 *       Revision 1.21  2003/08/04 18:11:35  bstreetman
00437 *       Updated port locations
00438 *       
00439 *       Revision 1.20  2003/08/01 20:21:20  bstreetman
00440 *       Cleaned and streamlined all code. Reduced warning and outputs. CMG and Momentum Wheels now no longer accept a global integer in initialization.  The file descriptor is now set and held internally.
00441 *       
00442 *       Revision 1.19  2003/07/28 17:54:00  bstreetman
00443 *       Change some pass-by-reference to pass-by-value
00444 *       
00445 *       Revision 1.18  2003/07/21 20:15:30  bstreetman
00446 *       Updates
00447 *       
00448 *       Revision 1.17  2003/07/18 19:51:18  cakinli
00449 *       Fixed file footer.
00450 *       
00451 *       Revision 1.2  2003/06/27 19:52:14  bstreetman
00452 *       Defined all remaining functions and updated their implementations.
00453 *       
00454 *       Revision 1.1.1.1  2003/06/06 18:44:15  simpliciter
00455 *       Initial submission.
00456 *       
00457 *
00458 ******************************************************************************/

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