Project

General

Profile

5DT Data Glove plugin  1.0
GloveObject.cpp
1 //###################################################################################
2 //# Glove Object #
3 //# Used To Handle a 5DT Glove #
4 //# Author : #
5 //# Aymeric SUTEAU #
6 //# LISA - ANGERS #
7 //###################################################################################
8 
9 
10 /* HEADER INCLUDE */
11 #include "GloveObject.h"
12 #include "GloveCalibration.h"
13 #include "GlovePlugin.h"
14 #include <string>
15 
16 /*------------------------------- CALLBACK FUNCTION -------------------------------*/
17 void GloveCallback(LPVOID param)
18 {
19  // Lower the message rate to
20  // Cast void pointer to GloveObject type before using
21  GloveObject *pGloveObject = (GloveObject*) param;
22  // If we retrieve a valid pointer, we call the function which will update the sensors values
23  if (pGloveObject)
24  pGloveObject->UpdateGlove();
25  // Hack to lower the message rate
26  // Sleep(10);
27 }
28 
29 
30 /*-------------------------- CONSTRUCTOR AND DESTRUCTOR ---------------------------*/
32 {
33  pGlove = 0;
34  calibration = 0;
35  iUSBPort = 0;
36  iType = FD_GLOVENONE;
37  NbrOfSensors = -1;
38  gestureIndex = -1;
39  bCalibrationDone = true;
40  pSensorValues = new float[NUMBEROFSENSOR];
41  for (int i = 0; i < NUMBEROFSENSOR; i++)
42  {
43  *(pSensorValues + i) = 0.0f;
44  }
45 }
46 
48 {
49  // Destroy dynamic table containing data glove serial number
50  //delete [] szSerialNumber;
51 
52  // Delete the callback function previously added (in order to stop the acquisition of sensors values)
53  fdSetCallback(pGlove, NULL, NULL);
54 
55  // close the usb port
56  Close();
57 
58  if (pSensorValues)
59  {
60  delete[] pSensorValues;
61  pSensorValues = 0;
62  }
63  if (calibration)
64  {
65  delete calibration;
66  calibration = 0;
67  }
68 }
69 
70 
71 /*------------------------------ GETTERS AND SETTERS ------------------------------*/
72 
74 {
75  return sUSBPort;
76 }
77 
78 int GloveObject::GetTypeID()
79 {
80  return iType;
81 }
82 std::string GloveObject::GetTypeName()
83 {
84  return sType;
85 }
86 
87 int GloveObject::GetHandIndex()
88 {
89  return iHand;
90 }
91 
92 std::string GloveObject::GetHandName()
93 {
94  return sHand;
95 }
96 
97 int GloveObject::GetNbrOfSensors()
98 {
99  return NbrOfSensors;
100 }
101 
102 std::string GloveObject::GetSerialNumber()
103 {
104  return serialNumber;
105 }
106 
107 int GloveObject::GetUSBPortIndex()
108 {
109  return iUSBPort;
110 }
111 
112 float* GloveObject::GetSensorValues()
113 {
114  return pSensorValues;
115 }
116 
117 fdGlove* GloveObject::GetGlove()
118 {
119  return pGlove;
120 }
121 
122 /*----- Return gesture index between 0 and 15 (thumb not handled) -----*/
123 int GloveObject::GetGestureIndex()
124 {
125  return this->gestureIndex;
126 }
127 
128 void GloveObject::SetGestureIndex(int driverGesture)
129 {
130  // We get from driver
131  // Gesture : 0 closed except for thumb
132  // Thumb : 16 closed, 32 half closed, 0 open
133  // -1 for an unrecognized gesture
134 
135  // We want : 0 for closed , 1 for open in binary
136  // And beginning with the thumb
137 
138  if (driverGesture != -1)
139  {
140 
141  driverGesture = driverGesture<<1;
142 
143  // Thumb closed or half closed
144  if (driverGesture & 0x60) // 0x60=0x30<<1=0b00110000<<1 meaning 48=16+32 fingers (both are thumb)
145  gestureIndex = driverGesture & (0x1E); // 0x1E=0x0F<<1=0b00011110 meaning that the thumb will be set to 0 and others fingers will not be changed
146  // Thumb open
147  else
148  gestureIndex = driverGesture | 0x01; // we have already moved the fingers so we just set the thumb at 1
149  }
150 }
151 
152 /*-------------------------------- ADDITIONAL FUNCTIONS ---------------------------------*/
153 /*----- Initialize glove object -----*/
154 bool GloveObject::Init()
155 {
156  // Check for any available USB glove
157  unsigned short aPID[5];
158  int nNumFound = 5, nChosen = 0;
159  char portName[5] = "USB";
160  bool bInitOk = false;
161  //szSerialNumber = new char[12];
162 
163  // Scan USB ports
164  fdScanUSB(aPID, nNumFound);
165  // DEBUG : MMechostr(MSKDEBUG, "-> Init ...gloves found : %d\n", nNumFound);
166  for (int i = 0; i < nNumFound; i++)
167  {
168  // Update the name of current USB port
169  sprintf(portName, "USB%d", i);
170 
171  // Try to open the first device of the list, else try to open the next one
172  pGlove = fdOpen(portName);
173  if (pGlove == 0)
174  {
175  // Initialization failed : data glove already opened
176  MMechostr(MSKDEBUG, "-> Init ...failed : data glove n.%d already opened\n", i);
177  }
178  else
179  {
180  // Set USB port and index
181  sUSBPort = portName;
182  iUSBPort = i;
183  MMechostr(MSKDEBUG, "-> Init ...successful\n");
184  bInitOk = true;
185  break;
186  }
187  }
188 
189  // If no gloves have been found, we must exit the function
190  if (!bInitOk)
191  {
192  MMechostr(MSKDEBUG, "-> Init ...failed : couldn't open any data glove\n");
193  return false;
194  }
195 
196  // Retrieve the data glove serial number
197  char tmpserial[20];
198  fdGetSerialNumber(pGlove, tmpserial);
199  MMechostr(MSKDEBUG, "Glove 5DT serial : %s", tmpserial);
200  serialNumber = tmpserial;
201 
202  // Disable auto calibration (enabled by default)
203  // fdSetAutoCalibrate(pGlove, false);
204 
205  // Set Glove Type and Hand Type
206  iType = fdGetGloveType(pGlove);
207  switch (iType)
208  {
209  // Not a Glove : we consider that the initialization of the device was a failure
210  case FD_GLOVENONE:
211  sType="None";
212  return false;
213  break;
214 
215  // Get Glove Type
216  case FD_GLOVE7:
217  sType="5DT Glove";
218  break;
219 
220  case FD_GLOVE7W:
221  sType="5DT Wireless Glove";
222  break;
223 
224  case FD_GLOVE16:
225  sType="16DT Glove";
226  break;
227 
228  case FD_GLOVE16W:
229  sType="16DT Wireless Glove";
230  break;
231 
232  case FD_GLOVE5U:
233  sType="DG5 Ultra Series";
234  break;
235 
236  case FD_GLOVE5UW:
237  sType="DG5 Wireless Ultra Series";
238  break;
239 
240  case FD_GLOVE5U_USB:
241  sType="DG5 Ultra USB";
242  break;
243 
244  case FD_GLOVE14U:
245  sType="DG14 Ultra Series";
246  break;
247 
248  case FD_GLOVE14UW:
249  sType="DG14 Wireless Ultra Series";
250  break;
251 
252  case FD_GLOVE14U_USB:
253  sType="DG14 Ultra USB";
254  break;
255  }
256  iHand=fdGetGloveHand(pGlove);
257  if (iHand == FD_HAND_RIGHT)
258  sHand = "Right";
259  else
260  sHand = "Left";
261 
262  // Set Number of Glove Sensors
263  NbrOfSensors = fdGetNumSensors(pGlove);
264 
265  // Set the update function callback (once the calibration is done)
266  fdSetCallback(pGlove, (void*)GloveCallback, this);
267  return true;
268 }
269 
270 /*----- Calibtration function ------*/
271 void GloveObject::StartCalibration()
272 {
273  if (calibration == 0)
274  {
275  //TODO : destroy calib
276  calibration = new GloveCalibration(this);
277  calibration->start();
278  }
279  else if (!calibration->GetIsRunning())
280  {
281  calibration->start();
282  }
283  else
284  {
285  //TODO : MSKDEBUG to change
286  MMechostr(MSKDEBUG, "Calibration already running");
287  }
288 }
289 
290 /*----- To be called by glove callback function -----*/
291 void GloveObject::UpdateGlove()
292 {
293  // If and only if a valid pointer to the glove device is available
294  // And we add a hack to lower the message rate
295  if (pGlove && timer.GetMilliseconds() > 10)
296  {
297  fdGetSensorScaledAll(pGlove, pSensorValues); // Update sensors values
298 
299  // Retrieve the current gesture according to the gesture mode
300  // And check if its value has changed and send it to the event manager
301  int oldGesture = gestureIndex;
302  SetGestureIndex(fdGetGestureA(pGlove));
303  if (gestureIndex != oldGesture)
304  {
305  // Notify Scol window that a new hand gesture has been made
306  OBJpostEvent(GLOVE_HAND_CB, (int)this, (LPARAM)gestureIndex);
307  }
308 
309  //Create a message buffer to post the raw data
310  float* rawMsg = new float[NUMBEROFSENSOR];
311  for (int i = 0; i < NUMBEROFSENSOR; i++)
312  {
313  rawMsg[i] = pSensorValues[i];
314  }
315 
316  //Send sensor values
317  OBJpostEvent(GLOVE_NEWDATA_CB, (int)this, (LPARAM)rawMsg);
318  timer.Reset();
319  }
320 }
321 
322 
323 /*----- Close glove -----*/
324 int GloveObject::Close()
325 {
326  int iErrorCode = fdClose(pGlove); // Free the glove device and communication port
327  pGlove = 0; // Make sure the pointer is reset
328  return iErrorCode;
329 }