00001 /************************************************************************************************/ 00002 //SubInclude DSACSSROOT src Base ; 00003 /*! \file PhysicalMomentumWheel.h 00004 * \brief The PhysicalMomentumWheel class provides the interface for h/w momentum wheels. 00005 * \author $Author: jayhawk_hokie $ 00006 * \version $Revision: 1.35 $ 00007 * \date $Date: 2007/07/24 09:39:39 $ 00008 ************************************************************************************************/ 00009 /*! 00010 * 00011 ************************************************************************************************/ 00012 00013 #ifndef __SSSL_PHYSICALMOMENTUMWHEEL_H__ 00014 #define __SSSL_PHYSICALMOMENTUMWHEEL_H__ 00015 00016 #include <iostream> 00017 #include <sstream> 00018 #include <fstream> 00019 00020 00021 // Standard System Header Files 00022 #include <pthread.h> 00023 #include <string> 00024 #include <sstream> 00025 #include <fstream> 00026 #include <sys/time.h> 00027 #include <sys/types.h> 00028 #include <sys/stat.h> 00029 #include <sys/fcntl.h> 00030 #include <sys/ioctl.h> 00031 #include <asm/ioctls.h> 00032 #include <bits/types.h> 00033 00034 // DSACSS Header Files 00035 #include <Base/Actuator.h> 00036 #include <utils/Time.h> 00037 extern "C" 00038 { 00039 #include <Hardware/satctl.h> 00040 } 00041 extern "C" 00042 { 00043 #include <Hardware/common.h> 00044 } 00045 00046 // Open-SESSAME Header Files 00047 #include <matrix/Matrix.h> 00048 00049 using namespace std; 00050 using namespace O_SESSAME; 00051 00052 /*! Class to interact with actual harware momentum wheels */ 00053 class PhysicalMomentumWheel : public Actuator 00054 { 00055 public: 00056 ///////////////////////////////////////////////////// 00057 // Contructors/Deconstructors 00058 //////////////////////////////////////////////////// 00059 00060 /*! \brief Default Constructor 00061 */ 00062 PhysicalMomentumWheel( ); 00063 00064 /*! \brief Default Deconstructor 00065 */ 00066 virtual ~PhysicalMomentumWheel( ); 00067 00068 ////////////////////////////////////////////////////// 00069 // Facilitators 00070 ///////////////////////////////////////////////////// 00071 00072 /*! \brief Initialize the wheel and its chain 00073 */ 00074 int Initialize( ); 00075 00076 /*! \brief Deinitialize the wheel and its chain. 00077 */ 00078 int Deinitialize( ); 00079 00080 /*! \brief Total System Reset for wheel motor. 00081 */ 00082 int Reset( ); 00083 00084 int ChangeBaud( ); 00085 00086 /*! \brief Set ECHO to on. Echo input data back out main channel 00087 */ 00088 int EchoOn( ); 00089 00090 /*! \brief Set ECHO to off. Do not echo input data back out main channel 00091 */ 00092 int EchoOff( ); 00093 00094 /*! \brief Enable torque mode for the wheel motor. 00095 */ 00096 int EnableTorqueMode( ); 00097 00098 /*! \brief Enable torque mode for the wheel motor. 00099 * @param _power is the voltage to be commanded in Torque Mode 00100 */ 00101 int TorqueCommand( double _power ); 00102 00103 /*! \brief Stop the wheel. The function resets the settings for the wheel motor. 00104 * Not a good method, but works. 00105 */ 00106 int Stop( ); 00107 00108 /*! \brief Start the speed reading routine of the motor in a separate thread 00109 * Each wheel's speed query needs to be started individually. The performance occurs 00110 * when the spart calls for each wheel are spread evenly. 00111 */ 00112 void StartSpeedQuery( ); 00113 00114 /*! \brief Friend function split in a new thread that queries the wheel speed repeatedly. It begins running in the 00115 * StartSpeedQuery function. Velocity by Position: This function determines the velocity of the wheel by differentiating 00116 * the position reported by the wheel. The function asks the wheel for its 00117 * position and then gets the time of day at which the measurement is reported. 00118 * The code then delays for a small time and asks for the position and gets 00119 * the time again. The velocity is then calculated by dividing the change 00120 * in position byt he change in time. The return value is the velocity in rev/sec. 00121 * 00122 * @param arg the argument is set to be a "this" pointer cast as a void pointer 00123 */ 00124 friend void* QueryWheelSpeed( void* arg ); 00125 00126 /*! \brief Stop the speed reading routine of the motor. This function does not need to be called a t the end of the program. 00127 * The thread will be closed automatically when the program ends. 00128 */ 00129 void StopSpeedQuery( ); 00130 00131 /*! \brief Command a torque to the wheel 00132 * @param _wheelTorque double representing the desired torque output in N-m 00133 */ 00134 void CommandWheelTorque( double _wheelTorque ); 00135 00136 /*! \brief Start the torque controller. It is called in the CommandWheelTorque function. 00137 */ 00138 void StartTorqueController( ); 00139 00140 /*! Friend function split in a new thread that controls the torque applied. It begins running in the 00141 * StartTorqueController function. Determine reaction wheel speed to provide the desired torque requested from controller. 00142 * @param arg the argument is set to be a "this" pointer cast as a void pointer 00143 */ 00144 friend void* RunTorqueController( void* arg ); 00145 00146 /*! Friend function split in a new thread that controls the torque applied. It begins running in the 00147 * StartTorqueController function. Determine reaction wheel speed to provide the desired torque requested from controller. 00148 * @param arg the argument is set to be a "this" pointer cast as a void pointer 00149 */ 00150 friend void* RunTorqueControllerRev( void* arg ); 00151 00152 /*! \brief End the Torque Controller Thread. 00153 */ 00154 void StopTorqueController( ); 00155 00156 /*! \brief Set wheel speed. 00157 * @param desiredWheelSpeed 00158 */ 00159 void SetWheelSpeed( double desiredWheelSpeed ); 00160 00161 /*! \brief Command a speed to the wheel. 00162 * Retrieves wheelspeed from double m_DesiredWheelSpeed. 00163 */ 00164 void CommandWheelSpeed( ); 00165 00166 /*! \brief Start the PID controller in a separate thread 00167 */ 00168 void StartPID( ); 00169 00170 /*! \brief Friend function that is spawned in a new thread in order to run the PID controller. It is called 00171 * internally by the CommandWheelSpeed() function. 00172 * PID controller to set momentum wheel angular velocity. Runs in a separate thread created by 00173 * by the CommandWheelSpeed() function. THis function is a friend to the PhyicalMomentumWheel class. 00174 * Once created it will run indefinitely until either a new speed is requested or the end of the program 00175 * is reached. 00176 * 00177 * This controller has gain scheduling based on the magnitude of the velocity change desired. It 00178 * also places limits on large changes in torque and limits the upper value of torque in order 00179 * to protect the motor and its components. 00180 * 00181 * @param arg the argument is set to be a "this" pointer cast as a void pointer 00182 */ 00183 friend void* RunPIDController(void* arg); 00184 00185 /*! \brief End the PID Control Thread (will be called by the deconstructor) 00186 */ 00187 void StopPID( ); 00188 00189 00190 /*! \brief Old-style Command a torque to the wheel 00191 * @param wheelTorque double representing the desired torque output in N-m 00192 */ 00193 void CommandSystemTorque( double wheelTorque ); 00194 00195 ///////////////////////////////////////////////// 00196 // Mutators 00197 //////////////////////////////////////////////// 00198 00199 /*! \brief Set the maximun and minimum torques to be applied to the wheel 00200 * @param minWheelTorque The minimum torque that should be applied to the wheel in N-m 00201 * @param maxWheelTorque The maaximum torque that shoul be applied to the wheel in N-m 00202 */ 00203 void SetWheelTorqueLimits( const double& minWheelTorque, const double& maxWheelTorque ); 00204 00205 /*! \brief Set the maximum change between the current and desired torque values 00206 * @param maxTorqueStep The maximun allowed cahnge in torque in N-m 00207 */ 00208 void SetMaxTorqueStep( const double maxTorqueStep ); 00209 00210 /*! \brief Set the address of the wheel in the daisy chain 00211 * @param WheelAddress integer representing the address of the wheel 00212 */ 00213 void SetWheelAddress( int WheelAddress ); 00214 00215 /*! \brief Set a daisy chain number to distinguish between two or more daisy chains 00216 * @param DaisyChainNumber 00217 */ 00218 void SetDaisyChainNumber( int DaisyChainNumber ); 00219 00220 /*! \brief Set the port to which the wheel's chain is connected 00221 * @param DaisyChainPort The string perpresenting the port loaction, S0 for serial 1, USB1 for usb port 2, etc 00222 */ 00223 void SetDaisyChainPort( string DaisyChainPort ); 00224 00225 /*! \brief Set the bais speed of the wheel for a momentum wheel. 00226 * @param BiasSpeed [rad/s] 00227 */ 00228 void SetBiasSpeed( double BiasSpeed ); 00229 00230 /*! \brief Set the axial inertia of the wheel 00231 * @param _inertia the spin axis ineria in kg-m^2 00232 */ 00233 void SetAxialInertia( double _inertia ); 00234 00235 /*! \brief Set the momentum wheel string name 00236 * @param _wheelName A string naming the wheel 00237 */ 00238 void SetWheelName( string _wheelName ); 00239 00240 /*! \brief Set the momentum wheel speed coefficients. The wheel speed coefficients are determined from the characterization of each wheel motor. 00241 * @param _Coefficients1 a 4x1 vector of constants 00242 * @param _Coefficients2 a 3x1 vector of constants 00243 * @param _Coefficients3 a 3x1 vector of constants 00244 * @param _Coefficients4 a 4x1 vector of constants 00245 */ 00246 void SetWheelSpeedCoefficients( Vector _Coefficients1, Vector _Coefficients2, Vector _Coefficients3, Vector _Coefficients4 ); 00247 00248 ///////////////////////////////////////////////// 00249 // Inspectors 00250 ///////////////////////////////////////////////// 00251 00252 /*! /brief Returns the most current speed reported by the wheel, a double in rad/sec 00253 * @param Speed double set to the current wheel speed 00254 * @param measTime double set to the number of seconds since unix epoch when the velocity waas measured 00255 */ 00256 void GetWheelSpeed( double& Speed, double& measTime ); 00257 00258 /*! \brief Returns a double representing the current torque to the wheel in N-m 00259 */ 00260 double QueryWheelTorque( ); 00261 00262 /*! \brief Get wheel inertia 00263 * @param _inertia is the axial inertia kg-m^2 00264 */ 00265 void GetAxialInertia( double& _inertia ); 00266 00267 /*! \brief Returns a type double of wheel inertia kg-m^2*/ 00268 double GetAxialInertia( ); 00269 00270 /*! \brief Returns a type string of the wheel name */ 00271 string GetWheelName( ); 00272 00273 /*! \brief Sets the parameters to the min and max torque limits 00274 * @param minWheelTorque Set to the minimum allowed wheel torque in N-m 00275 * @param maxWheelTorque Set to the maximum allowed wheel torque in N-m 00276 */ 00277 void GetWheelTorqueLimits( double& minWheelTorque, double& maxWheelTorque ); 00278 00279 /*! \brief Sets the parameter maxTorqueStep to the maximum allowed torque step in N-m 00280 * @param maxTorqueStep 00281 */ 00282 void GetMaxTorqueStep( double& maxTorqueStep ); 00283 00284 /*! \brief Get the Wheel file descriptor 00285 */ 00286 int GetWheelFD( ); 00287 00288 /*! \brief Get the amps reported by the motor 00289 */ 00290 double GetAmps( ); 00291 00292 /*! \brief Get the voltage the motor 00293 */ 00294 double GetVolts( ); 00295 00296 protected: 00297 00298 00299 private: 00300 /*! \brief Member double that holds the current angular wheel speed in rad/sec */ 00301 double m_CurrentWheelSpeed; 00302 00303 /*! \brief Memeber double holds the time of velcotiy measurement in seconds from unix epoch */ 00304 double m_VelocityTime; 00305 00306 /*! \brief Member double that holds the desired angular wheel speed in rad/sec */ 00307 double m_DesiredWheelSpeed; 00308 00309 /*! \brief Member double that holds the maximum output torque in N-m */ 00310 double m_MaxWheelTorque; 00311 00312 /*! \brief Member double that holds the minimum output torque in N-m */ 00313 double m_MinWheelTorque; 00314 00315 /*! \brief Member double that holds the current ouput torque in N-m */ 00316 double m_CurrentWheelTorque; 00317 00318 /*! \brief Desired wheel Torque*/ 00319 double m_DesiredWheelTorque; 00320 00321 /*! \brief Member double that holds the axial Inertia in kg-m^2*/ 00322 double m_AxialInertia; 00323 00324 /*! \brief Member integer Bias speed set flag */ 00325 int m_BiasFlag; 00326 00327 /*! \brief Momentum Wheel bias speed */ 00328 double m_BiasSpeed; 00329 00330 /*! \brief Member integer that holds the address of the wheel on its daisy chain */ 00331 int m_WheelAddress; 00332 00333 /*! \brief Member integer that holds which daisy chain the wheel is on */ 00334 int m_DaisyChainNumber; 00335 00336 /*! \brief Member integer that holds the file descriptor for all wheels */ 00337 int m_fd; 00338 00339 /*! \brief Member string that hold which port the wheel is connected to */ 00340 string m_DaisyChainPort; 00341 00342 /*! \brief Member double that hold minimum volts of the reaction wheel motor */ 00343 double m_MinVolts; 00344 00345 /*! \brief Member double that hold maximum current of the reaction wheel motor */ 00346 double m_MaxAmps; 00347 00348 /*! \brief Member double that hold maximum current of the reaction wheel motor */ 00349 string m_WheelName; 00350 00351 ///////////////////////////// 00352 // Threads 00353 //////////////////////////// 00354 00355 /*! \brief Member pthread id for splitting off PID control */ 00356 pthread_t m_PIDThreadID; 00357 00358 /*! \brief Member pthread for splitting off speed queries */ 00359 pthread_t m_SpeedThreadID; 00360 00361 /*! \brief Member pthread for Torque Controller */ 00362 pthread_t m_TorqueThreadID; 00363 00364 /*! \brief Memeber mutex lock variable*/ 00365 pthread_mutex_t m_Mutex; 00366 00367 int m_exitConditionSpeed; 00368 00369 int m_exitConditionTorque; 00370 00371 ///////////////////////// 00372 //PID control holders 00373 //////////////////////// 00374 /*! \brief Member double that holds the last angular velocity of the wheel, used in PID controller */ 00375 double LAST_VEL; 00376 00377 /*! \brief Member double that holds the last applied wheel torque, used in PID controller */ 00378 double LAST_TOR; 00379 00380 /*! \brief Member double that holds the last velocity error, used by the PID controller */ 00381 double LAST_ERR; 00382 00383 /*! \brief Member double that holds the last voltage, used by PID controller */ 00384 double LAST_VOLT; 00385 00386 /*! \brief Member double that holds the largest permissable torque change in N-m */ 00387 double m_MaxTorqueStep; 00388 00389 ///////////////////////////// 00390 // Torque Controller Holders 00391 //////////////////////////// 00392 00393 /*! \brief Member integer Torque Control flag, used in Torque Controller */ 00394 int m_TorqueControlFlag; 00395 00396 /*! \brief Delta Time to produce Desired Control Torque, used in Torque Controller [s] */ 00397 double m_TorqueControllerDeltaTime; 00398 00399 /*! \brief Proportional Torque Gain (to tune function to supply torques closer to actual N-m) */ 00400 double m_TorqueControllerTorqueGain; 00401 00402 /*! \brief Negative Time Gain */ 00403 double m_TorqueControllerNegativeTimeGain; 00404 00405 /*! \brief Steady State Gain [rad/s]*/ 00406 double m_TorqueControllerSteadyStateGain; 00407 00408 /*! \brief Member integer Torque Control Wheel Speed flag, used in Torque Controller */ 00409 int m_TorqueControllerDesiredWheelSpeedFlag; 00410 00411 ///////////////////////////// 00412 // Wheel Speed Coefficients 00413 //////////////////////////// 00414 00415 Vector m_Coefficients1; 00416 Vector m_Coefficients2; 00417 Vector m_Coefficients3; 00418 Vector m_Coefficients4; 00419 00420 00421 }; 00422 00423 #endif 00424 // Do not change the comments below - they will be added automatically by CVS 00425 /***************************************************************************** 00426 * $Log: PhysicalMomentumWheel.h,v $ 00427 * Revision 1.35 2007/07/24 09:39:39 jayhawk_hokie 00428 * Removed matrix and vector pointers. 00429 * 00430 * Revision 1.34 2007/05/30 23:48:43 jayhawk_hokie 00431 * Modified thread termination. 00432 * 00433 * Revision 1.33 2007/05/29 18:57:41 jayhawk_hokie 00434 * *** empty log message *** 00435 * 00436 * Revision 1.32 2007/04/09 14:01:31 jayhawk_hokie 00437 * Added capability in Torque Controller to reduce wheel speed. 00438 * 00439 * Revision 1.31 2007/03/27 19:07:16 jayhawk_hokie 00440 * Updated Momentum Wheels. 00441 * 00442 * Revision 1.30 2007/02/06 20:56:23 jayhawk_hokie 00443 * Added SetAxialInertia function. 00444 * 00445 * Revision 1.29 2005/02/25 18:40:52 cakinli 00446 * Created Makefiles and organized include directives to reduce the number of 00447 * include paths. Reorganized libraries so that there is now one per source 00448 * directory. Each directory is self-contained in terms of its Makefile. 00449 * The local Makefile in each directory includes src/config.mk, which has all 00450 * the definitions and general and pattern rules. So at most, to see what 00451 * goes into building a target, a person needs to examine the Makefile in 00452 * that directory, and ../config.mk. 00453 * 00454 * Revision 1.28 2004/05/11 20:18:09 bstreetman 00455 * Vast improvements to torque controller 00456 * 00457 * Revision 1.27 2004/04/19 15:00:41 bstreetman 00458 * Fixed a config and a shutdown bug 00459 * 00460 * Revision 1.26 2004/03/18 18:08:17 bstreetman 00461 * Further Fixes on the torque controller 00462 * 00463 * Revision 1.25 2004/03/15 21:58:31 bstreetman 00464 * Fixed a multible multi-thread issue. 00465 * 00466 * Revision 1.24 2004/03/04 23:13:09 bstreetman 00467 * 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. 00468 * 00469 * Revision 1.23 2003/12/16 17:20:26 bstreetman 00470 * A bit o' fall cleaning. Just look at how pretty it is. 00471 * 00472 * Revision 1.22 2003/11/14 20:15:44 bstreetman 00473 * 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. 00474 * 00475 * Revision 1.21 2003/10/17 20:01:34 bstreetman 00476 * 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. 00477 * 00478 * Revision 1.20 2003/09/15 21:44:03 bstreetman 00479 * Removed hardcoded parameters, ready for pure config file use 00480 * 00481 * Revision 1.19 2003/08/18 21:30:20 bstreetman 00482 * PID controller now functions properly in multiple threads. RunPIDController() now a non-member friend funciton 00483 * 00484 * Revision 1.17 2003/08/04 21:25:23 bstreetman 00485 * Now with doxygen-style comments 00486 * 00487 * Revision 1.16 2003/08/01 20:21:20 bstreetman 00488 * 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. 00489 * 00490 * Revision 1.15 2003/07/28 21:08:00 bstreetman 00491 * Now with PID controller, gains not perfect yet. RUns fine, just slow getting the wheels up to speed 00492 * 00493 * Revision 1.14 2003/07/21 20:15:30 bstreetman 00494 * Updates 00495 * 00496 * Revision 1.13 2003/07/21 18:28:28 cskelton 00497 * Current copies 00498 * 00499 * Revision 1.12 2003/07/09 20:24:33 cskelton 00500 * Corrected Initialization and Deconstruction 00501 * 00502 * Revision 1.11 2003/07/07 19:34:08 cskelton 00503 * Removed virtual from functions 00504 * 00505 * Revision 1.10 2003/07/04 16:01:31 nicmcp 00506 * took out the Axial inertia and added it to Momentumwheel.h 00507 * 00508 * Revision 1.9 2003/07/02 22:02:12 bstreetman 00509 * Improve functionality and made to work with the c hardware code. 00510 * 00511 * Revision 1.8 2003/07/01 19:56:09 cskelton 00512 * Added Hardware software support 00513 * 00514 * Revision 1.7 2003/07/01 19:19:43 cskelton 00515 * Debugged to compile 00516 * 00517 * Revision 1.6 2003/07/01 16:00:23 bstreetman 00518 * Added CommandWheelSpeed function 00519 * 00520 * Revision 1.5 2003/06/30 21:04:57 cskelton 00521 * INserted actual functions 00522 * 00523 * Revision 1.4 2003/06/30 18:47:12 cskelton 00524 * Updated Functions 00525 * 00526 * Revision 1.3 2003/06/30 15:33:21 cskelton 00527 * Now PMW dervived from Actuators 00528 * 00529 * Revision 1.2 2003/06/26 20:53:28 bstreetman 00530 * Changed inputs of CommandWheelToque func 00531 * 00532 * Revision 1.1 2003/06/19 21:31:55 simpliciter 00533 * Initial submission, upgrades from Andy's generic code. 00534 * 00535 * Revision 1.1 2003/06/13 15:24:33 simpliciter 00536 * Initial submission. 00537 * 00538 * 00539 ******************************************************************************/