00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include "MomentumWheel.h"
00014
00015 int MomentumWheel::m_WheelCount = 0;
00016
00017
00018
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
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
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
00110 SetWheelSpeedCoefficients( Coefficients1, Coefficients2, Coefficients3, Coefficients4 );
00111
00112
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
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
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
00165
00166
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
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
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
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
00339 int readDelay = 50000;
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
00354 pthread_mutex_lock(&wheel->m_DataMutex);
00355 wheel -> m_WheelHistory -> AppendHistory( measurementTime2, wheelSpeed );
00356
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
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448
00449
00450
00451
00452
00453
00454
00455
00456
00457
00458