-
Notifications
You must be signed in to change notification settings - Fork 0
/
wiiheadtracking.cpp
164 lines (135 loc) · 5.05 KB
/
wiiheadtracking.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// wiimote head tracking for linux
// by Steven Thomas Snyder, [email protected]
/*
(C) copyright 2008, Steven Snyder, All Rights Reserved
LICENSING INFORMATION:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <GL/glut.h>
#include <GL/glu.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <cwiid.h> /* cwiid wii remote library */
#include "trackingsettings.h"
#include "wiiheadtracking.h"
#define COORD_X 0
#define COORD_Y 1
#define COORD_Z 2
// Math functions
// =================================================================================
// calculate the distance between (x1,y1) and (x2,y2)
double calc_distance(double x1, double y1, double x2, double y2)
{
double xdiff = x1-x2;
double ydiff = y1-y2;
double distsqr = (xdiff*xdiff)+(ydiff*ydiff);
return sqrt(distsqr);
}
// generate a random coordinate in the square formed by corners (0,0) and (min,max)
GLfloat randCoord(float min, float max)
{
float random = ((float)rand()/(float)RAND_MAX) * (max-min) + min;
return random;
}
// TrackWiimote
// ==================================================================================
TrackWiimote::TrackWiimote(){
m_connect_attempts = 0;
m_max_attempts = 10;
m_bt_address = *BDADDR_ANY;
m_wiimote = NULL;
}
int TrackWiimote::updateState()
{
return cwiid_get_state(this->m_wiimote, &(this->m_state));
}
int TrackWiimote::connect()
{
printf("Wiimote: Press 1 and 2 simultaneously to connect the Wiimote.\n");
this->m_wiimote = cwiid_connect(&(this->m_bt_address), CWIID_FLAG_CONTINUOUS|CWIID_FLAG_NONBLOCK);
if (!this->m_wiimote)
{
printf("Unable to connect to Wiimote.\n");
return 0;
}
printf("Established connection to Wiimote!\nInitializing...\n");
if (cwiid_command(this->m_wiimote, CWIID_CMD_LED, CWIID_LED1_ON|CWIID_LED4_ON)!=0)
{
printf("Failed attempting to enable LED1 and LED4.\n");
return 0;
}
if (cwiid_command(this->m_wiimote, CWIID_CMD_RPT_MODE,CWIID_RPT_IR|CWIID_RPT_ACC|CWIID_RPT_BTN)!=0)
{
printf("Failed to enable Wii functions.\n");
return 0;
}
if (cwiid_get_state(this->m_wiimote, &(this->m_state))!=0)
{
printf("Failed to get Wii remote state.\n");
return 0;
}
printf("Initialization complete!\n");
return 1;
}
int TrackWiimote::updateIRPositions(double ir_positions[][2], int ir_sizes[])
{
/* returns 1 if 2 or more positions are valid, otherwise returns 0 or -1 */
int ret = -1;
if (this->m_wiimote) /* if the wiimote is still connected */
{
int i;
for (i=0; i<4; i++)
{
if (this->m_state.ir_src[i].valid)
{
ir_positions[i][COORD_X] = ((this->m_state.ir_src[i].pos[CWIID_X] * HRANGE
/ (double)CWIID_IR_X_MAX) -(HRANGE/2));
ir_positions[i][COORD_Y] = (this->m_state.ir_src[i].pos[CWIID_Y] * VRANGE
/ (double)CWIID_IR_Y_MAX) -(VRANGE/2);
if (this->m_state.ir_src[i].size != -1)
ir_sizes[i] = this->m_state.ir_src[i].size+1;
ret++;
}
}
return ret;
}
return 0;
}
/* update the camera position using ir points camerapt_1 and camerapt_2 */
int TrackWiimote::updateHeadPos(head_info_t *headinfo, double ir_positions[][2], int ir_sizes[], int pt1, int pt2)
{
double cur_pt_dist = calc_distance(ir_positions[pt1][COORD_X],
ir_positions[pt1][COORD_Y],
ir_positions[pt2][COORD_X],
ir_positions[pt2][COORD_Y]);
double x = (ir_positions[pt1][COORD_X] + ir_positions[pt2][COORD_X])/2;
double y = (ir_positions[pt1][COORD_Y] + ir_positions[pt2][COORD_Y])/2;
double z_scale = (headinfo->ini_point_dist/cur_pt_dist);
if (z_scale > 100 || cur_pt_dist < 0.01 || cur_pt_dist > 100.0)
return 0;
headinfo->cur_wii_to_real = z_scale * headinfo->ini_wii_to_real; // is this needed?
headinfo->eye_pos[COORD_X] = x * headinfo->cur_wii_to_real;
headinfo->eye_pos[COORD_Y] = y * headinfo->cur_wii_to_real;
headinfo->eye_pos[COORD_Z] = z_scale * headinfo->ini_pos[COORD_Z];
return 1;
}
void TrackWiimote::calibrate(head_info_t *headinfo, double ir_positions[][2], int ir_sizes[], int pt1, int pt2){
double cur_pt_dist = calc_distance(ir_positions[pt1][COORD_X],
ir_positions[pt1][COORD_Y],
ir_positions[pt2][COORD_X],
ir_positions[pt2][COORD_Y]);
printf("Old distance between IR sources: %.2fcm. New: %.2fcm.\n",headinfo->ini_point_dist,cur_pt_dist);
headinfo->ini_point_dist = cur_pt_dist;
printf("Assuming your head is %.2f units (usually cm) from the wiimote, calibration is complete.\n",headinfo->ini_pos[COORD_Z]);
}