vrecko
virtual reality framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
LHeadTracker.h
Go to the documentation of this file.
1 /***************************************************************************
2  lheadtracker.h - description
3  -------------------
4  begin : Mon Jun 3 2002
5  copyright : (C) 2002 by Jan Flasar
6  email : flasar@fi.muni.cz
7  ***************************************************************************/
8 
9 /***************************************************************************
10  * *
11  * This program is free software; you can redistribute it and/or modify *
12  * it under the terms of the GNU General Public License as published by *
13  * the Free Software Foundation; either version 2 of the License, or *
14  * (at your option) any later version. *
15  * *
16  ***************************************************************************/
17 
18 #ifndef LHEADTRACKER_H
19 #define LHEADTRACKER_H
20 
21 #include <vrecko/IOPins.h>
22 #include <vrecko/Device.h>
23 #include <osg/Vec3>
24 #include <osg/Vec4>
25 #include <osg/Matrix>
26 #include <osg/Quat>
27 #include <setjmp.h>
28 
29 #if defined(WIN32)
30 #include <windows.h>
31 #endif
32 
33 
34 namespace vreckoDP_base {
35 
36 #define HTI_CONNECTBYTE (char)0x87 /* Open the connection */
37 #define HTI_CLOSEBYTE (char)0x88 /* Close the connection */
38 
39 //#define HTI_DEF_PORT 7777 /* Default TCP port */
40 //#define HTI_DEF_SERIAL "/dev/ttyC0" /* Default serial port */
41 
42 #define HTD_COMMAND_LEN 2
43 #define HTD_CMD_RESET "*R"
44 #define HTD_CMD_DIAG "*\005"
45 #define HTD_CMD_REPORT "*d"
46 #define HTD_CMD_DEMAND "*D"
47 
48 #define HTD_PACKET_LEN 16
49 
50 #define HTI_MAX_NUM 1048575 /* max. returned positon */
51 #define HTI_2MAX_NUM 2097150 /* 2*HTI_MAX_NUM */
52 #define HTI_MIN_POS_DIFF 20 /* min. position difference */
53 #define HTI_MIN_ROT_DIFF 80 /* min. rotation difference */
54 #define HTI_POS_C 0.0005
55 #define HTI_ROT_C 40.0
56 
57 // class level defines
58 #define SLAVE_MODE 1
59 #define MASTER_MODE 2
60 
61 // command type defines
62 #define CMD_TYPE_SLAVE_XMITTER_TYPE 1 // slave transmitter type
63 #define CMD_TYPE_FILTER_POINT 7 // filter point count
64 #define CMD_TYPE_RCVR_DIMENSIONS 8 // set receiver dimensions
65 
66 #define htd_flush( port_fd ) ioctl( port_fd, TCFLSH, 2 )
67 #define if_err( param ) if( ( param ) < 0 )
68 
69 
70 typedef enum {
71  HTD_REC_POS_OUT = 0, /* receiver out of active area */
72  HTD_REC_POS_ACT, /* receiver in active area */
73  HTD_REC_POS_FRI /* receiver in fringe area */
75 
76 typedef enum {
79  ANGLES //EULERS_ANGLES ???
81 
82 
83 class DP_EXPORT LHeadTracker : public vrecko::Device {
84  public:
85  LHeadTracker();
86  LHeadTracker(const char *port_name);
87  LHeadTracker(const char *port_name1, const char *port_name2);
88  ~LHeadTracker();
89 
90  virtual int openDevice(void);
91  int closeDevice(void);
92 
93  void update(void);
94 
95  bool loadXMLParameters(XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *pParametersNode);
96  XERCES_CPP_NAMESPACE_QUALIFIER DOMNode *saveXMLParameters(XERCES_CPP_NAMESPACE_QUALIFIER DOMDocument *);
97 
98  int getRotate(float *p_rotate);
99  osg::Vec3 &getRotate(void);
100  void setRotationType(rotation_type _type) {rot_type = _type;};
101  osg::Vec3 &getRotateA(void);
102  osg::Matrix &getRotateM(void);
103  osg::Quat &getRotateQ(void);
104  int getTranslate(float *p_trans);
105  osg::Vec3 &getTranslate(void);
106  void printPos(void);
107 
108  void setInitialPosition(osg::Vec4 &_pos);
109  void setInitialRotation(osg::Vec4 &_rot);
110  void setInitialPosition(osg::Vec4 &_pos1, osg::Vec4 &_pos2);
111  void setInitialRotation(osg::Vec4 &_rot1, osg::Vec4 &_rot2);
112 
113  void demandReport(void);
114  void setBaseT( osg::Vec4 b1, osg::Vec4 b2, osg::Vec4 b3, osg::Vec4 orig );
115  void setBaseR( osg::Vec4 b1, osg::Vec4 b2, osg::Vec4 b3, osg::Vec4 orig );
116  void resetBase( osg::Vec4 b1, osg::Vec4 b2, osg::Vec4 b3, osg::Vec4 orig );
117  void setSlaveTransmitterType(unsigned char transmitter_type);
118 
119  protected:
120  int port_fd, port_fd_dual, _port_fd;
121  std::string _port_name_dual;
122  int actual_p; // 0 | 1 | ...
123 
124  int scanDevice(void);
125 #if defined(WIN32)
126  HANDLE hCom, hCom_dual, _hCom; // handle for COM port
127  HANDLE hComxx;
128 
129  bool openPort(const char *pcCommPort, HANDLE *temp_hCom);
130 #else
131  bool openPort(const char *_pname, int *temp_port_fd);
132 #endif
133  bool scanDeviceX(void);
134 
135  int diagnostic(void);
136  int getRecData(long int *x, long int *y, long int *z, long int *pi, long int *ya, long int *ro, char *packet);
137  void delay(int msec);
138  int lhtRead(char *buf, int n);
139  void reset(void);
140  int singleReport(char *reply);
141  void printBuffer(const char *buf, int length);
142 
143  osg::Vec4 getEuler(osg::Matrix& _mat);
144 
145 // private:
146  bool rec_out[2]; /* receiver out of active area */
147  osg::Vec4 pos[2], rot[2];
148  osg::Vec3 ret_pos, ret_rot;
149  osg::Quat rotq[2], rotq_diff[2];
151  osg::Matrix ret_rotm;
152  osg::Quat ret_rotq;
153  osg::Vec4 base1, base2, base3, origin; /* HT coordinate system */
154 
155  char tx_buf[HTD_COMMAND_LEN]; /* tx buffer */
156  char rx_buf[2*HTD_PACKET_LEN]; /* rx buffer */
157  int req_len, read_len;
158 
159  long int absX[2], absY[2], absZ[2]; /* absolute values of position */
160  long int absPI[2], absYA[2], absRO[2]; /* absolute values of rotation */
161  osg::Vec4 pos_diff[2]; /* initial_position - first_relative_postion */
162  osg::Vec4 rot_diff[2]; /* -- '' -- rotation */
163 
164  long int last_absPI[2], last_absYA[2], last_absRO[2]; /* last absolute rotation */
165  osg::Vec4 last_rot[2]; /* last rotation */
166 
167  osg::Matrix tr_matrix, tr_matrix2; /* transformation matrix for relative position evaluation */
168 
169  bool first_report[2]; /* first report from active area */
170 
171  osg::Vec4 get_rel_pos( long int x, long int y, long int z );
172  bool checkDiff(const long int &x, const long int &y, const long int &z,
173  const long int &n_pi, const long int &n_ya, const long int &n_ro);
174 
175  jmp_buf tenv;
176 
177  osg::Vec3 translate_mapping;
178 
180  DECLARE_OUTPUT(Orientation, vrecko::MessageQuat);
181 };
182 
183 }
184 
185 #endif