IGSTK
/tmp/buildd/igstk-4.4.0/Source/igstkAscensionCommandInterpreter.h
Go to the documentation of this file.
00001 /*=========================================================================
00002 
00003   Program:   Image Guided Surgery Software Toolkit
00004   Module:    $RCSfile: igstkAscensionCommandInterpreter.h,v $
00005   Language:  C++
00006   Date:      $Date: 2009-01-30 19:33:49 $
00007   Version:   $Revision: 1.1 $
00008 
00009   Copyright (c) ISC  Insight Software Consortium.  All rights reserved.
00010   See IGSTKCopyright.txt or http://www.igstk.org/copyright.htm for details.
00011 
00012      This software is distributed WITHOUT ANY WARRANTY; without even
00013      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
00014      PURPOSE.  See the above copyright notices for more information.
00015 
00016 =========================================================================
00017 
00018 Copyright (c) 2000-2005 Atamai, Inc.
00019 
00020 Use, modification and redistribution of the software, in source or
00021 binary forms, are permitted provided that the following terms and
00022 conditions are met:
00023 
00024 1) Redistribution of the source code, in verbatim or modified
00025    form, must retain the above copyright notice, this license,
00026    the following disclaimer, and any notices that refer to this
00027    license and/or the following disclaimer.  
00028 
00029 2) Redistribution in binary form must include the above copyright
00030    notice, a copy of this license and the following disclaimer
00031    in the documentation or with other materials provided with the
00032    distribution.
00033 
00034 3) Modified copies of the source code must be clearly marked as such,
00035    and must not be misrepresented as verbatim copies of the source code.
00036 
00037 THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE SOFTWARE "AS IS"
00038 WITHOUT EXPRESSED OR IMPLIED WARRANTY INCLUDING, BUT NOT LIMITED TO,
00039 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00040 PURPOSE.  IN NO EVENT SHALL ANY COPYRIGHT HOLDER OR OTHER PARTY WHO MAY
00041 MODIFY AND/OR REDISTRIBUTE THE SOFTWARE UNDER THE TERMS OF THIS LICENSE
00042 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL OR CONSEQUENTIAL DAMAGES
00043 (INCLUDING, BUT NOT LIMITED TO, LOSS OF DATA OR DATA BECOMING INACCURATE
00044 OR LOSS OF PROFIT OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF
00045 THE USE OR INABILITY TO USE THE SOFTWARE, EVEN IF ADVISED OF THE
00046 POSSIBILITY OF SUCH DAMAGES.
00047 
00048 =======================================================================*/
00049 
00050 #ifndef __igstkAscensionCommandInterpreter_h
00051 #define __igstkAscensionCommandInterpreter_h
00052 
00053 #include "igstkObject.h"
00054 #include "igstkSerialCommunication.h"
00055 
00056 #include <string>
00057 
00058 namespace igstk
00059 {
00060 
00069 enum AscensionCommand
00070 {
00071   FB_POINT               = 'B',
00072   FB_STREAM              = '@',
00073   FB_REPORT_RATE_FULL    = 'Q',
00074   FB_REPORT_RATE_DIV2    = 'R',
00075   FB_REPORT_RATE_DIV8    = 'S',
00076   FB_REPORT_RATE_DIV32   = 'T',
00077 
00078   FB_C_POSITION          = 'V',
00079   FB_C_ANGLES            = 'W',
00080   FB_C_MATRIX            = 'X',
00081   FB_C_POSITION_ANGLES   = 'Y',
00082   FB_C_POSITION_MATRIX   = 'Z',
00083   FB_C_QUATERNION        = '\\',
00084   FB_C_POSITION_QUATERNION = ']',
00085 
00086   FB_BUTTON_MODE         = 'M',
00087   FB_BUTTON_READ         = 'N',
00088 
00089   FB_ANGLE_ALIGN1        = 'J',
00090   FB_REFERENCE_FRAME1    = 'H',
00091   FB_ANGLE_ALIGN2        = 'q',
00092   FB_REFERENCE_FRAME2    = 'r',
00093 
00094   FB_FBB_RESET           = '/',
00095   FB_HEMISPHERE          = 'L',
00096   FB_NEXT_TRANSMITTER    = '0',
00097   FB_RUN                 = 'F',
00098   FB_SLEEP               = 'G',
00099   FB_SYNC                = 'A',
00100   FB_XOFF                = '\x13',
00101   FB_XON                 = '\x11',
00102  
00103   FB_RS232_TO_FBB        = '\xF0',
00104   FB_RS232_TO_FBB_E      = '\xE0',
00105   FB_RS232_TO_FBB_SE     = '\xA0',
00106 
00107   FB_EXAMINE_VALUE       = 'O',
00108   FB_CHANGE_VALUE        = 'P',
00109 };
00110 
00118 enum AscensionDataFormat
00119 {
00120   FB_POSITION            = 'V',
00121   FB_ANGLES              = 'W',
00122   FB_MATRIX              = 'X',
00123   FB_POSITION_ANGLES     = 'Y',
00124   FB_POSITION_MATRIX     = 'Z',
00125   FB_QUATERNION          = '\\',
00126   FB_POSITION_QUATERNION = ']',
00127 };
00128 
00135 enum AscensionStatusBits
00136 {
00137   FB_STATUS_STREAMING   = 0x0001,
00138   FB_STATUS_FORMAT      = 0x001E,
00139   FB_STATUS_SLEEPING    = 0x0020,
00140   FB_STATUS_XOFF        = 0x0040,
00141   FB_STATUS_FACTORY     = 0x0080,
00142   FB_STATUS_NO_SYNC     = 0x0100,
00143   FB_STATUS_CRT_SYNC    = 0x0200,
00144   FB_STATUS_EXPANDED    = 0x0400,  
00145   FB_STATUS_HOST_SYNC   = 0x0800,
00146   FB_STATUS_RUNNING     = 0x1000,
00147   FB_STATUS_ERROR       = 0x2000,
00148   FB_STATUS_INITIALIZED = 0x4000,
00149   FB_STATUS_MASTER      = 0x8000,
00150 };
00151 
00160 enum AscensionBusStatusBits
00161 {
00162   FB_FBB_STATUS_ERT0        = 0x01,
00163   FB_FBB_STATUS_ERT1        = 0x02,
00164   FB_FBB_STATUS_ERT2        = 0x04,
00165   FB_FBB_STATUS_ERT3        = 0x08,
00166   FB_FBB_STATUS_ERT         = 0x10,
00167   FB_FBB_STATUS_SENSOR      = 0x20,
00168   FB_FBB_STATUS_RUNNING     = 0x40,
00169   FB_FBB_STATUS_ACCESSIBLE  = 0x80,
00170 };
00171 
00176 enum AscensionErrorCode
00177 {
00178   FB_NO_ERROR       = 0,     
00179   FB_OPEN_ERROR     = 1,     
00180   FB_COM_ERROR      = 2,     
00181   FB_IO_ERROR       = 3,     
00182   FB_TIMEOUT_ERROR  = 4,     
00183   FB_PARM_ERROR     = 5,     
00184   FB_COMMAND_ERROR  = 6,     
00185   FB_ILLEGAL_ERROR  = 7,     
00186   FB_PHASE_ERROR    = 8,     
00187   FB_RESOURCE_ERROR = 9,     
00188 };
00189 
00197 enum AscensionAddressMode
00198 {
00199   FB_NORMAL           = 0,
00200   FB_EXPANDED         = 1,
00201   FB_SUPER_EXPANDED   = 3,
00202 };
00203 
00210 enum AscensionHemisphere
00211 {
00212   FB_FORWARD = 0x0000,
00213   FB_AFT     = 0x0100,
00214   FB_UPPER   = 0x010C,
00215   FB_LOWER   = 0x000C,
00216   FB_LEFT    = 0x0106,
00217   FB_RIGHT   = 0x0006,
00218 };
00219 
00228 enum AscensionPositionScale
00229 {
00230   FB_STANDARD = 0,
00231   FB_EXTENDED = 1,
00232 };
00233 
00241 enum AscensionParameter
00242 {
00243   FB_STATUS            = 0,     
00244   FB_REVISION          = 1,     
00245   FB_SPEED             = 2,     
00246   FB_POSITION_SCALING  = 3,     
00248   FB_FILTER            = 4,     
00249   FB_ALPHA_MIN         = 5,     
00250   FB_RATE_COUNT        = 6,     
00251   FB_RATE              = 7,     
00253   FB_DATA_READY        = 8,     
00254   FB_DATA_READY_CHAR   = 9,     
00255   FB_ERROR_CODE        = 10,    
00256   FB_ERROR_BEHAVIOR    = 11,    
00258   FB_VM                = 12,    
00259   FB_ALPHA_MAX         = 13,    
00260   FB_ELIMINATION       = 14,    
00261   FB_IDENTIFICATION    = 15,    
00263   FB_ERROR_CODE_EXPANDED = 16,  
00264   FB_REFERENCE_FRAME   = 17,    
00265   FB_TRANSMITTER_MODE  = 18,    
00266   FB_FBB_ADDRESS_MODE  = 19,    
00268   FB_LINE_FILTER       = 20,    
00269   FB_FBB_ADDRESS       = 21,    
00270   FB_P_HEMISPHERE      = 22,    
00271   FB_P_ANGLE_ALIGN2    = 23,    
00273   FB_P_REFERENCE_FRAME2 = 24,   
00274   FB_SERIAL_NUMBER     = 25,    
00275   FB_SENSOR_SERIAL_NUMBER = 26, 
00276   FB_XMTR_SERIAL_NUMBER = 27,   
00278   FB_FBB_DELAY         = 32,    
00279   FB_GROUP_MODE        = 35,    
00281   FB_FBB_STATUS        = 36,    
00283   FB_FBB_AUTOCONFIG    = 50,    
00284 };
00285 
00290 enum AscensionBaudRate
00291 {
00292   FB_2400   = 0,
00293   FB_4800   = 1,
00294   FB_9600   = 2,
00295   FB_19200  = 3,
00296   FB_38400  = 4,
00297   FB_57600  = 5,
00298   FB_115200 = 6,
00299 };
00300 
00301 class AscensionCommandInterpreter : public Object
00302 {
00303 public:
00304 
00306   igstkStandardClassTraitsMacro( AscensionCommandInterpreter, Object )
00307 
00308 public:
00309 
00311   typedef SerialCommunication            CommunicationType;
00312 
00314   void SetCommunication(CommunicationType* communication);
00315   
00317   CommunicationType* GetCommunication();
00318 
00320   void Open();
00321 
00323   void Stop();
00324   void Run();
00325 
00327   void Close();
00328 
00330   void Reset();
00331 
00333   void SetHemisphere(AscensionHemisphere hemisphere);
00334 
00336   void SetFormat(AscensionDataFormat format);
00337 
00339   void SetButtonMode(bool mode);
00340 
00342   void Point();
00343 
00345   void Stream();
00346 
00348   void EndStream();
00349 
00352   void EndStreamIfStreaming();
00353 
00359   void Update();
00360 
00366   unsigned int GetBird();
00367 
00373   void GetPosition(float xyz[3]);
00374 
00379   void GetAngles(float zyx[3]);
00380 
00387   void GetMatrix(float a[9]);
00388 
00395   void GetQuaternion(float q[4]);
00396 
00402   int GetButton();
00403 
00405   void MatrixFromAngles(float a[9], const float zyx[3]);
00406 
00408   void AnglesFromMatrix(float zyx[3], const float a[9]);
00409 
00411   void FBBReset();
00412 
00414   void FBBAutoConfig(unsigned int num);
00415 
00417   void RS232ToFBB(unsigned int bird);
00418 
00420   void SendCommand(AscensionCommand command);
00421 
00423   void SendCommandWords(AscensionCommand command, const short *data);
00424 
00426   void SendCommandBytes(AscensionCommand command, const char *data);
00427 
00429   int ExamineValue(AscensionParameter parm);
00430 
00432   int ExamineValueWords(AscensionParameter parm, short *data);
00433 
00435   int ExamineValueBytes(AscensionParameter parm, char *data);
00436 
00438   void ChangeValue(AscensionParameter parm, int data);
00439 
00441   void ChangeValueWords(AscensionParameter parm, const short *data);
00442 
00444   void ChangeValueBytes(AscensionParameter parm, const char *data);
00445 
00452   AscensionErrorCode GetError();
00453 
00455   const char *GetErrorMessage();
00456 
00462   void ButtonRead(int *val);
00463 
00465   int GetShort(const char *cp);
00466 
00468   void PutShort(char *cp, int val);
00469 
00470 protected:
00471 
00473   AscensionCommandInterpreter();
00474 
00476   virtual ~AscensionCommandInterpreter();
00477 
00479   virtual void PrintSelf( std::ostream& os, itk::Indent indent ) const; 
00480 
00481 private:
00482 
00484   void ReceiveRaw(char *reply, unsigned int len);
00485 
00487   void SendRaw(const char *text, unsigned int len);
00488 
00490   int Unpack(char **cp);
00491 
00493   void SetErrorAndMessage(AscensionErrorCode errorcode, const char *text);
00494 
00495   /* stores last error */
00496   AscensionErrorCode m_Error;
00497 
00498   /* stores text for last error */
00499   std::string m_ErrorText;
00500 
00501   /* revision number (quite important) */
00502   int m_Revision;
00503   
00504   /* max examine/change parameter number for this rev */
00505   AscensionParameter m_MaxParameter;
00506 
00507   /* FBB addressing mode */
00508   AscensionAddressMode m_AddressMode;
00509 
00510   /* birds are in group mode */
00511   bool m_GroupMode;
00512 
00513   /* number of running birds */
00514   unsigned int m_NumberOfBirds;
00515 
00516   /* true if currently stream */
00517   bool m_StreamData;
00518 
00519   /* true if currently point */
00520   bool m_PointData;
00521 
00522   /* stores bird address after RS232_TO_FBB */
00523   unsigned int m_FBBAddress;
00524 
00525   /* the bird which this data set is for */
00526   unsigned int m_CurrentBird;
00527 
00528   /* position scale mode for each bird */
00529   AscensionPositionScale m_PositionScale[128];
00530 
00531   /* button mode for each bird */
00532   bool m_ButtonMode[128];
00533 
00534   /* FB_POSITION, FB_ANGLES, etc for each bird */
00535   AscensionDataFormat m_DataFormat[128];
00536 
00537   /* place where data is stored after being read */
00538   char m_DataBuffer[256];
00539 
00540   /* leftover chars after a phase error */
00541   unsigned int m_PhaseErrorLeftoverBytes;
00542 
00544   CommunicationType::Pointer m_Communication;
00545 };
00546 
00547 } /* end namespace igstk */
00548 
00549 #endif /* __igstkAscensionCommandInterpreter_h */