Debian Codegraph @

p5glove/libp5glove/p5glove.c

raw symbols SCIP
1 /*
2 * $Id$
3 *
4 * Copyright (c) 2003 Jason McMullan <[email protected]>
5 * Windows patch (c) 2004 Ross Bencina <[email protected]>
6 * MacOSX patch (c) 2004 Tim Kreger <[email protected]>
7 *
8 * USB P5 Data Glove support
9 */
10
11 /*
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 */
26
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include <stdio.h>
32 #include <stdlib.h> /* for calloc */
33 #include <stdint.h>
34 #include <string.h> /* for memcpy */
35 #include <math.h>
36
37 #include "usb_hid.h"
38 #include <errno.h>
39 #include "p5glove.h"
40
41 #define DEBUG
42 #ifdef DEBUG
43 #define DPRINTF(fmt,args...) fprintf(stderr,"p5: " fmt , ##args )
44 #else
45 #define DPRINTF(fmt,args...) do { } while (0)
46 #endif
47
48 struct p5glove {
49 USBHID usb;
50
51 char name[128];
52
53 /* Calibration data from Report 12 & 6 */
54 struct p5glove_cal {
55 int id; /* Device id */
56 struct {
57 double v; /* in Radians */
58 double h; /* in Radians */
59 } head[2];
60 double head_dist; /* in Meters */
61 double led[10][3];
62 } cal;
63
64 struct p5glove_data {
65 struct timeval sampled; /* Time sampled at */
66 uint32_t valid; /* Which data items are valid (mask) */
67
68 int buttons; /* Button bitmask */
69 int finger[5]; /* Finger clench values (0-63) */
70
71 /* Raw glove data */
72 struct p5glove_ir {
73 int visible; /* Was the sensor visible? */
74 int v1,v2,h;
75 } ir[8]; /* IR Sensors values. (-511 - 511) */
76
77 /* Computed from p5g_process_sample
78 */
79 double position[3]; /* Position */
80
81 /* Rotation information */
82 struct {
83 double axis[3]; /* Rotation axis (normalized) */
84 double angle; /* In degrees */
85 } rotation;
86 } data,prev;
87 };
88
89 static uint32_t get_bits(uint8_t *data,int pos,int len)
90 {
91 int index = (pos>>3);
92 uint32_t value = 0;
93
94 pos &= 0x7;
95 if (pos != 0) {
96 value |= (data[index] & (0xff >> pos)) << (len-(8-pos));
97 len -= (8-pos);
98 index++;
99 }
100
101 for (; len >= 8; len-=8) {
102 value |= data[index] << (len-8);
103 index++;
104 }
105
106 if (len > 0)
107 value |= (data[index] & (0xff << (8-len))) >> (8-len);
108
109 return value;
110 }
111
112 static int32_t get_bits_signed(uint8_t *data,int pos,int len)
113 {
114 uint32_t value,mask=0;
115
116 value = get_bits(data,pos,len);
117 if (value & (1 << (len-1)))
118 mask = 0xffffffff << (len - 32);
119 return value | mask;
120 }
121
122 static void p5g_unpack_sample(struct p5glove *p5, uint8_t data[24])
123 {
124 unsigned char tmp[24];
125 int visible=1;
126 int i;
127
128 /* Decode data.
129 * Format (nibbles from LSB to MSB)
130 * 11111111111111112222222222222222
131 * 0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF
132 * 01ddddddddBCCCCVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVxxx
133 * 0 1 2 3 4 5 6 7 8 9 A B C D E F 0 1 2 3 4 5 6 7
134 * 1 1 1 1 1 1 1 1
135 * (bytes from LSB to MSB)
136 *
137 * d - Packed 6-bit bend sensor data (Index, Middle, Ring, Pinky, Thumb)
138 * B - Button data. A=1, B=2, C=4
139 * C - IR sensor report index
140 * V - Packed 30 bit signed IR info (10 bits X, 10 bits Y, 10 bits Z), x4
141 */
142
143 if (data[0] != 1) {
144 printf("Odd. Sample [0]=%d\n",data[0]);
145 return;
146 }
147
148 for (i=0; i < 5; i++) {
149 int value;
150
151 switch (i) {
152 case 0: value = data[1] >> 2; break;
153 case 1: value = ((data[1] & 0x3) << 4) | (data[2] >> 4); break;
154 case 2: value = ((data[2] & 0xF) << 2) | (data[3] >> 6); break;
155 case 3: value = (data[3] & 0x3F); break;
156 case 4: value = data[4] >> 2; break;
157 default: value = 0; break;
158 }
159
160 p5->data.finger[i]=get_bits(data,8+6*i,6);
161 }
162
163 p5->data.buttons=get_bits(data,40,4);
164
165 /* Clear visibility */
166 for (i=0 ; i <8; i++)
167 p5->data.ir[i].visible=0;
168
169 for (i=0; i < 4; i++) {
170 int axis;
171 int value;
172
173 axis = get_bits(data,44+4*i,4);
174 if (axis & 0x8)
175 continue;
176
177 p5->data.ir[axis].v2=get_bits_signed(data,60+(i*30),10);
178 p5->data.ir[axis].v1=get_bits_signed(data,60+(i*30)+10,10);
179 p5->data.ir[axis].h=get_bits_signed(data,60+(i*30)+20,10);
180 p5->data.ir[axis].visible=visible++;
181 }
182 }
183
184 void p5g_process_led(P5Glove p5,int led,double pos[3])
185 {
186 double v1,v2,h,d,zprime;
187
188 v1 = atan(p5->data.ir[led].v1/512.0)+p5->cal.head[0].v;
189 v2 = atan(p5->data.ir[led].v2/512.0)+p5->cal.head[1].v;
190 h = atan(p5->data.ir[led].h/512.0);
191 d = p5->cal.head_dist;
192
193 zprime = d/(tan(v1)-tan(v2));
194
195 pos[0]=-sin(h)*zprime;
196 pos[1]=tan(v1)*zprime-d/2.0; /* Places 0 in the center */
197 pos[2]=cos(h)*zprime-d*2.0; /* A reasonable Z center */
198 }
199
200 int p5glove_reference_led(P5Glove p5, int led, double pos[3])
201 {
202 if (led < 0 || led > 7)
203 return -EINVAL;
204
205 memcpy(pos,p5->cal.led[led],sizeof(double)*3);
206 return 0;
207 }
208
209 #ifdef CHECK_ACUTE_TRIANGLES
210 static int p5glove_check_triangle(P5Glove p5,int led1,int led2,int led3)
211 {
212 double c;
213 int led[3];
214 int i;
215
216 led[0]=led1;
217 led[1]=led2;
218 led[2]=led3;
219
220 for (i=0; i < 3; i++) {
221 c=p5glove_angle(p5->cal.led[led[i]],
222 p5->cal.led[led[(i+1)%3]],
223 p5->cal.led[led[(i+2)%3]])*180.0/M_PI;
224
225 /* At too acute/obtune an angle? */
226 DPRINTF("Angle %d-%d-%d is %.4lf\n",led[i],led[(i+1)%3],led[(i+2)%3],c);
227 if (c < 7.0)
228 return -EINVAL;
229 }
230
231 return 0;
232 }
233 #endif
234
235 /* Determine the 'best' leds to use form the data.
236 * Return the # of good leds
237 */
238 static int p5g_best_leds(P5Glove p5,int led[4],double pos[4][3])
239 {
240 int leds;
241 int i,j;
242 int error[4]={0,0,0,0};
243
244 for (i=0; i < 4; i++)
245 led[i]=-1;
246
247 /* Get the position from the 'best led' */
248 for (i = 0 ; i < 8; i++)
249 if (p5->data.ir[i].visible)
250 led[p5->data.ir[i].visible-1]=i;
251
252 /* Hmm. No 'best' led.
253 */
254 if (led[0] < 0)
255 return -ENOENT;
256 p5g_process_led(p5,led[0],pos[0]);
257
258 if (led[1] < 0)
259 return 1;
260 p5g_process_led(p5,led[1],pos[1]);
261
262 if (led[2] < 0)
263 return 2;
264
265 p5g_process_led(p5,led[2],pos[2]);
266
267 /* If we have more than 2, we need to verify the data */
268 if (led[3] < 0)
269 leds=3;
270 else {
271 p5g_process_led(p5,led[3],pos[3]);
272 leds=4;
273 }
274
275 /* For each led-led pair, determine the error % versus the
276 * reference distances
277 */
278 DPRINTF("Distances: %d - %d - %d - %d\n",led[0],led[1],led[2],led[3]);
279 for (i=0; i < leds; i++) {
280 for (j=i+1 ;j < leds; j++) {
281 double ref_dist = p5glove_dist(p5->cal.led[led[i]],p5->cal.led[led[j]]);
282 double dist = p5glove_dist(pos[i],pos[j]);
283 double err = fabs(dist-ref_dist)/ref_dist;
284
285 DPRINTF("%d - %d: %.4lf ",led[i],led[j],ref_dist);
286 DPRINTF("(%.4lf) [%.4lf]\n",led[i],led[j],dist,err);
287
288 /* Max 9% error margin on distance */
289 if (err > 0.20) {
290 error[i]++;
291 error[j]++;
292 }
293 }
294 }
295
296 /* Mark as dead anything with an error index of leds-1 */
297 j=leds-1;
298 for (i=0; i < leds; i++) {
299 if (error[i] != j)
300 continue;
301
302 DPRINTF("Dead: Led %d\n",led[i]);
303 leds--;
304 if (i != leds) {
305 memcpy(&error[i],&error[i+1],sizeof(int)*(leds-i));
306 memcpy(&led[i],&led[i+1],sizeof(double)*(leds-i));
307 memcpy(pos[i],pos[i+1],sizeof(double)*3*(leds-i));
308 }
309 i--;
310 if (leds == 1)
311 return leds;
312 }
313
314 #ifdef CHECK_ACUTE_TRIANGLES
315 /* Not enough left for rotation. Darn. */
316 if (leds < 3)
317 return leds;
318
319 /* Check all triangles for obtusity */
320 if (leds == 3) {
321 int err;
322 err=p5glove_check_triangle(p5,led[0],led[1],led[2]);
323 if (err < 0)
324 return 1;
325 return 3;
326 }
327
328 /* Leds == 4 */
329 for (i=0; i < leds; i++)
330 error[i]=0;
331
332 for (i=0; i < leds; i++) {
333 int err;
334 int l[3];
335
336 err = p5glove_check_triangle(p5,led[i],led[(i+1)%4],led[(i+2)%4]);
337 if (err < 0) {
338 error[i]++;
339 error[(i+1)%4]++;
340 error[(i+2)%4]++;
341 }
342 }
343
344 for (i=0; i < leds; i++)
345 DPRINTF("%d: error=%d\n",led[i],error[i]);
346
347
348 /* Mark as dead anything with an error index of leds-1 */
349 j=leds-1;
350 for (i=0; i < leds; i++) {
351 if (error[i] != j)
352 continue;
353
354 DPRINTF("Dead: Led %d\n",led[i]);
355 leds--;
356 if (i != leds) {
357 memcpy(&error[i],&error[i+1],sizeof(int)*(leds-i));
358 memcpy(&led[i],&led[i+1],sizeof(double)*(leds-i));
359 memcpy(pos[i],pos[i+1],sizeof(double)*3*(leds-i));
360 }
361 i--;
362 if (leds == 1)
363 return leds;
364 }
365 #endif /* CHECK_ACUTE_TRIANGES */
366
367 return leds;
368 }
369
370 /* Calculate delta, then store in 'prev'
371 */
372 static int p5g_delta(P5Glove p5,int have_pos, int have_rot)
373 {
374 int mask=0;
375 int i;
376
377 if (p5->prev.buttons != p5->data.buttons)
378 mask |= P5GLOVE_DELTA_BUTTONS;
379
380 for (i=0; i < 5; i++)
381 if (p5->prev.finger[i] != p5->data.finger[i]) {
382 mask |= P5GLOVE_DELTA_FINGERS;
383 break;
384 }
385
386 if (have_pos && (! memcmp(p5->data.position,p5->prev.position,sizeof(double)*3)))
387 have_pos=0;
388
389 if (have_rot && (! memcmp(&p5->data.rotation,&p5->prev.rotation,sizeof(p5->data.rotation))))
390 have_rot=0;
391
392 memcpy(&p5->prev,&p5->data,sizeof(p5->data));
393 return mask |
394 (have_pos ? P5GLOVE_DELTA_POSITION : 0) |
395 (have_rot ? P5GLOVE_DELTA_ROTATION : 0);
396 }
397
398
399 static int p5g_process_sample(P5Glove p5)
400 {
401 int i,j,leds;
402 int led[4] = {-1,-1,-1,-1};
403 double pos[4][3];
404 double pos_plane[3],ref_plane[3];
405 double rot_matrix[4][4];
406 double c,s,t;
407 double x,y,z;
408 const double zero[3]={0.0,0.0,0.0};
409 const double y_up[3]={0.0,1.0,0.0};
410
411 /* Get the best leds from the data */
412 leds=p5g_best_leds(p5,led,pos);
413 if (leds < 1)
414 return p5g_delta(p5,0,0);
415
416 p5g_process_led(p5,led[0],pos[0]);
417
418 /* Less than 3 leds. Can't calculate rotation info.
419 */
420 if (leds < 3)
421 return p5g_delta(p5,1,0);
422
423 /* Calculate reference normal of the leds */
424 p5glove_plane(p5->cal.led[led[0]],
425 p5->cal.led[led[1]],
426 p5->cal.led[led[2]],ref_plane);
427
428 /* Calculate actual normal of the leds */
429 p5glove_plane(pos[0],pos[1],pos[2],pos_plane);
430
431 /* Calculate cos(theta) from the dot product of the ref and actual */
432 c=p5glove_dot(ref_plane,pos_plane);
433 p5->data.rotation.angle=acos(c)*180.0/M_PI;
434 /* Calculate the rotation axis normal */
435 p5glove_plane(ref_plane,zero,pos_plane,p5->data.rotation.axis);
436
437 DPRINTF("Position: %d - %d - %d\n",led[0],led[1],led[2]);
438 DPRINTF("Position: [%.4lf, %.4lf,%.4lf]\n",pos[0][0],pos[0][1],pos[0][2]);
439 DPRINTF("Reference normal: [%.4lf, %.4lf, %.4lf]\n",ref_plane[0],ref_plane[1],ref_plane[2]);
440 DPRINTF("Position normal: [%.4lf, %.4lf, %.4lf]\n",pos_plane[0],pos_plane[1],pos_plane[2]);
441 DPRINTF("Reference angle: %.4lf\n",p5glove_dot(ref_plane,y_up)*180.0/M_PI);
442 DPRINTF("Normal up angle: %.4lf\n",p5glove_dot(pos_plane,y_up)*180.0/M_PI);
443 DPRINTF("Rotation angle: %.4lf\n",p5->data.rotation.angle);
444 DPRINTF("Rotation axis : [%.4lf, %.4lf, %.4lf]\n",p5->data.rotation.axis[0],p5->data.rotation.axis[1],p5->data.rotation.axis[2]);
445
446 c=cos(p5->data.rotation.angle*M_PI/180.0);
447 s=sin(p5->data.rotation.angle*M_PI/180.0);
448 t=1.0-c;
449
450 /* Calculate transformation matrix, in column major order */
451 x=p5->data.rotation.axis[0];
452 y=p5->data.rotation.axis[1];
453 z=p5->data.rotation.axis[2];
454
455 /* Column 0 */
456 rot_matrix[0][0]=t*x*x+c;
457 rot_matrix[0][1]=t*x*y-s*z;
458 rot_matrix[0][2]=t*x*z+s*y;
459 rot_matrix[0][3]=0;
460
461 /* Column 1 */
462 rot_matrix[1][0]=t*x*y+s*z;
463 rot_matrix[1][1]=t*y*y+c;
464 rot_matrix[1][2]=t*y*z-s*x;
465 rot_matrix[1][3]=0;
466
467 /* Column 2 */
468 rot_matrix[2][0]=t*x*z-s*y;
469 rot_matrix[2][1]=t*y*z+s*x;
470 rot_matrix[2][2]=t*z*z+c;
471 rot_matrix[2][3]=0;
472
473 /* Column 3 */
474 rot_matrix[3][0]=0;
475 rot_matrix[3][1]=0;
476 rot_matrix[3][2]=0;
477 rot_matrix[3][3]=1;
478
479 /* Now, apply the rotation angle to the position */
480 p5glove_vec_mat(p5->cal.led[led[0]],rot_matrix,pos[1]);
481
482 /* Then simply subtract the difference from the
483 * reference position to get the *real* point */
484 for (i=0; i<3; i++)
485 p5->data.position[i] = pos[0][i] - pos[1][i];
486
487 /* There! The position is corrected, the rotation matrix is complete - we're done! */
488 return p5g_delta(p5,1,1);
489 }
490
491 static int p5g_parse_report6(struct p5glove *p5,int8_t *buff)
492 {
493 /* We are given 32nds of a degree, so: */
494 #define REPORT6_TO_RAD(x) ((x)/32.0*M_PI/180.0)
495 p5->cal.head[0].v += REPORT6_TO_RAD(buff[1]);
496 p5->cal.head[0].h += REPORT6_TO_RAD(buff[2]);
497 p5->cal.head[1].v += REPORT6_TO_RAD(buff[3]);
498 p5->cal.head[1].h += REPORT6_TO_RAD(buff[4]);
499
500 /* The following is in 10ths of an inch. Convert to meters */
501 #define REPORT6_TO_METERS(x) ((x)/393.70039)
502 p5->cal.head_dist = REPORT6_TO_METERS(buff[5]);
503
504 return 0;
505 }
506
507 static int p5g_parse_report12(struct p5glove *p5,uint8_t *buff)
508 {
509 int led,axis;
510
511 p5->cal.id = get_bits(buff,8,8);
512 switch (p5->cal.id) {
513 case 0:
514 p5->cal.head[0].v += 17.0 * (M_PI/180.0);
515 p5->cal.head[1].v += -10.0 * (M_PI/180.0);
516 break;
517 default: fprintf(stderr,"libp5glove: unknown glove type %d\n",p5->cal.id);
518 return -EINVAL;
519 }
520 /* The following is in 100ths of an inch. Convert to meters */
521 #define REPORT12_TO_METERS(x) ((x)/3937.0039)
522 for (led = 0; led < 10; led++) {
523 for (axis = 0; axis < 3; axis++) {
524 int16_t val;
525 val = get_bits(buff,16+(48*led)+(16*axis),16);
526 p5->cal.led[led][axis]=REPORT12_TO_METERS(val);
527 if (axis == 0)
528 p5->cal.led[led][axis] *= -1.0;
529 }
530 }
531
532 #ifdef DEBUG_CAL
533 for (led=0; led < 12; led++) {
534 DPRINTF("%d %.4lf %.4lf %.4lf\n",led,
535 p5->cal.led[led][0],
536 p5->cal.led[led][1],
537 p5->cal.led[led][2]);
538 }
539 #endif
540
541 return 0;
542 }
543
544 static int p5glove_calibrate(struct p5glove *p5)
545 {
546 int8_t report6_buff[6];
547 int8_t report12_buff[255];
548 int err;
549
550 memset(&p5->cal,0,sizeof(p5->cal));
551 report12_buff[0]=12;
552 err=GetUSBHIDFeature(p5->usb,report12_buff,sizeof(report12_buff));
553 if (err < 0) goto end;
554
555 err=p5g_parse_report12(p5,report12_buff);
556 if (err < 0) goto end;
557
558 report6_buff[0]=6;
559 err=GetUSBHIDFeature(p5->usb,report6_buff,sizeof(report6_buff));
560 if (err < 0) goto end;
561
562 err=p5g_parse_report6(p5,report6_buff);
563
564 #ifdef DEBUG_CALIB
565 DPRINTF("Cal results:\n");
566 { int i; for (i=0;i<2;i++) {
567 DPRINTF("\tHead %d.v = %.2f deg\n",i,p5->cal.head[i].v * 180.0/M_PI);
568 DPRINTF("\tHead %d.h = %.2f deg\n",i,p5->cal.head[i].h * 180.0/M_PI);} }
569 DPRINTF("\tHead Distance = %.2f meters\n",p5->cal.head_dist);
570 #endif
571
572 end:
573 return err;
574 }
575
576 /*********** Public API **************/
577
578 P5Glove p5glove_open(int glove_number)
579 {
580 struct p5glove *p5 = NULL;
581 USBHID usb;
582 int err;
583
584 usb = OpenUSBHID (
585 glove_number, /* nth matching device */
586 0x0d7f, /* vendor id */
587 0x0100, /* product id */
588 0, /* version number (not used) */
589 SELECT_VENDOR_ID_FLAG | SELECT_PRODUCT_ID_FLAG ); /* selection flags */
590 if ( usb == INVALID_USBHID_VALUE )
591 return NULL;
592
593 p5=calloc(1,sizeof(*p5));
594 p5->usb=usb;
595 err = p5glove_calibrate(p5);
596 if (err < 0) {
597 if (err == -EBUSY) {
598 fprintf(stderr,"libp5glove: Can't get feature reports.\n");
599 fprintf(stderr,"libp5glove: If you have the 'usbmouse' module loaded, please unload it.\n");
600 }
601 free(p5);
602 CloseUSBHID(usb);
603 return NULL;
604 }
605
606 return p5;
607 }
608
609 void p5glove_close(P5Glove p5)
610 {
611 if (p5->usb != NULL) {
612 CloseUSBHID(p5->usb);
613 }
614 p5->usb=NULL;
615 free(p5);
616 }
617
618 int p5glove_sample(P5Glove p5,int timeout)
619 {
620 int err;
621 uint8_t data[24];
622
623 if ( ReadUSBHID( p5->usb, data, 24 ) == 24 && data[0]==1) {
624 p5g_unpack_sample(p5, data);
625 err=p5g_process_sample(p5);
626 } else {
627 errno = EACCES;
628 err = -1;
629 }
630
631 return err;
632 }
633
634 int p5glove_get_buttons(P5Glove p5,uint32_t *buttons)
635 {
636 *buttons = p5->data.buttons;
637 return 0;
638 }
639
640 int p5glove_get_finger(P5Glove p5,int finger,double *clench)
641 {
642 *clench = p5->data.finger[finger]/63.0;
643 return 0;
644 }
645
646 int p5glove_get_position(P5Glove p5,double pos[3])
647 {
648 memcpy(pos,p5->data.position,sizeof(double)*3);
649 return 0;
650 }
651
652 int p5glove_get_rotation(P5Glove p5,double *angle,double axis[3])
653 {
654 memcpy(axis,p5->data.rotation.axis,sizeof(double)*3);
655 *angle = p5->data.rotation.angle;
656 return 0;
657 }
658
659 void p5glove_begin_calibration(P5Glove p5)
660 {
661 char report[2] = { 0x01, 0x01 };
662 SetUSBHIDFeature( p5->usb, report, 2 );
663 }
664
665 void p5glove_end_calibration(P5Glove p5)
666 {
667 char report[2] = { 0x01, 0x00 };
668 SetUSBHIDFeature( p5->usb, report, 2 );
669 }
670
671 int p5glove_get_mouse_mode(P5Glove p5)
672 {
673 char report[2] = { 0x05, 0x00 };
674 GetUSBHIDFeature( p5->usb, report, 2 );
675
676 return (report[1] == 0x01)? 1 : 0;
677 }
678
679 void p5glove_mouse_mode_on(P5Glove p5)
680 {
681 char report[2] = { 0x05, 0x01 };
682 SetUSBHIDFeature( p5->usb, report, 2 );
683 }
684
685 void p5glove_mouse_mode_off(P5Glove p5)
686 {
687 char report[2] = { 0x05, 0xFF };
688 SetUSBHIDFeature( p5->usb, report, 2 );
689 }
690