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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
|
/********************************************************************************
* FaceTrackNoIR This program is a private project of some enthusiastic *
* gamers from Holland, who don't like to pay much for *
* head-tracking. *
* *
* Copyright (C) 2012 Wim Vriend (Developing) *
* Ron Hendriks (Researching and Testing) *
* Homepage: http://facetracknoir.sourceforge.net/home/default.htm *
* *
* Copyright (C) 2012 FuraX49 (HAT Tracker plugins) *
* Homepage: http://hatire.sourceforge.net *
* *
* *
* 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 "ftnoir_tracker_hat.h"
FTNoIR_Tracker::FTNoIR_Tracker()
{
ComPort = NULL;
HAT.Rot[0]=0;
HAT.Rot[1]=0;
HAT.Rot[2]=0;
HAT.Trans[0]=0;
HAT.Trans[1]=0;
HAT.Trans[2]=0;
// prepare & reserve QByteArray
dataRead.resize(4096);
dataRead.clear();
Begin.append((char) 0xAA);
Begin.append((char) 0xAA);
End.append((char) 0x55);
End.append((char) 0x55);
}
FTNoIR_Tracker::~FTNoIR_Tracker()
{
if (ComPort!=NULL) {
if (ComPort->isOpen() ) {
ComPort->close();
}
delete ComPort;
ComPort=NULL;
}
}
//send CENTER to Arduino
void FTNoIR_Tracker::notifyCenter() {
sendcmd(static_cast<QString>(settings.CmdCenter).toLatin1());
}
//send ZERO to Arduino
bool FTNoIR_Tracker::notifyZeroed() {
sendcmd(static_cast<QString>(settings.CmdZero).toLatin1());
return true;
}
//send RESET to Arduino
void FTNoIR_Tracker::reset() {
sendcmd(static_cast<QString>(settings.CmdReset).toLatin1());
}
// Info SerialPort
void FTNoIR_Tracker::SerialInfo() {
QByteArray Msg;
if (ComPort!=NULL) {
if (ComPort->isOpen() ) {
Msg.append("\r\n");
Msg.append(ComPort->portName());
Msg.append("\r\n");
Msg.append("BAUDRATE :");
Msg.append(QString::number(ComPort->baudRate()));
Msg.append("\r\n");
Msg.append("DataBits :");
Msg.append(QString::number(ComPort->dataBits()));
Msg.append("\r\n");
Msg.append("Parity :");
switch (ComPort->parity()) {
case 0: Msg.append("No parity");
break;
case 2: Msg.append("Even parity");
break;
case 3: Msg.append("Odd parity");
break;
case 4: Msg.append("Space parity");
break;
case 5: Msg.append("Mark parity");
break;
default: Msg.append("Unknown parity");
break;
}
Msg.append("\r\n");
Msg.append("Stop Bits :");
switch (ComPort->stopBits()) {
Msg.append(QString::number(ComPort->stopBits()));
case 1: Msg.append("1 stop bit.");
break;
case 2: Msg.append("2 stop bits.");
break;
case 3: Msg.append("1.5 stop bits.");
break;
default: Msg.append("Unknown number of stop bit.");
break;
}
Msg.append("\r\n");
Msg.append("Flow Control :");
switch (ComPort->flowControl()) {
case 0: Msg.append("No flow control");
break;
case 1: Msg.append("Hardware flow control (RTS/CTS)");
break;
case 2: Msg.append("Software flow control (XON/XOFF)");
break;
default: Msg.append("Unknown flow control");
break;
}
emit sendMsgInfo(Msg);
}
}
}
//send command to Arduino
void FTNoIR_Tracker::sendcmd(const QByteArray &cmd) {
QByteArray Msg;
if (cmd.length()>0) {
if (ComPort->isOpen() )
{
ComPort->write(cmd);
if (!ComPort->waitForBytesWritten(1000)) {
emit sendMsgInfo("TimeOut in writing CMD");
} else {
Msg.append("\r\n");
Msg.append("SEND '");
Msg.append(cmd);
Msg.append("'\r\n");
}
if ( !ComPort->waitForReadyRead(1000)) {
emit sendMsgInfo("TimeOut in response to CMD") ;
} else {
emit sendMsgInfo(Msg);
}
} else {
emit sendMsgInfo("ComPort not open") ;
}
}
}
// return FPS
void FTNoIR_Tracker::get_info( int *tps ){
*tps=frame_cnt;
frame_cnt=0;
}
void FTNoIR_Tracker::SerialRead()
{
QMutexLocker lck(&mutex);
dataRead+=ComPort->readAll();
}
#ifndef OPENTRACK_API
void FTNoIR_Tracker::Initialize( QFrame *videoframe )
{
CptError=0;
dataRead.clear();
frame_cnt=0;
settings.load_ini();
applysettings(settings);
ComPort = new QSerialPort(this);
ComPort->setPortName(sSerialPortName);
if (ComPort->open(QIODevice::ReadWrite ) == true) {
connect(ComPort, SIGNAL(readyRead()), this, SLOT(SerialRead()));
if (
ComPort->setBaudRate((QSerialPort::BaudRate)iBaudRate)
&& ComPort->setDataBits((QSerialPort::DataBits)iDataBits)
&& ComPort->setParity((QSerialPort::Parity)iParity)
&& ComPort->setStopBits((QSerialPort::StopBits)iStopBits)
&& ComPort->setFlowControl((QSerialPort::FlowControl)iFlowControl)
&& ComPort->clear(QSerialPort::AllDirections)
&& ComPort->setDataErrorPolicy(QSerialPort::IgnorePolicy)
) {
// Wait init arduino sequence
for (int i = 1; i <=iDelayInit; i+=50) {
if (ComPort->waitForReadyRead(50)) break;
}
sendcmd(sCmdInit);
// Wait init MPU sequence
for (int i = 1; i <=iDelayStart; i+=50) {
if (ComPort->waitForReadyRead(50)) break;
}
} else {
QMessageBox::warning(0,"FaceTrackNoIR Error", ComPort->errorString(),QMessageBox::Ok,QMessageBox::NoButton);
}
}
else {
QMessageBox::warning(0,"FaceTrackNoIR Error", "Unable to open ComPort",QMessageBox::Ok,QMessageBox::NoButton);
delete ComPort;
ComPort = NULL;
}
return;
}
void FTNoIR_Tracker::StartTracker(HWND parent_window)
{
// Send START cmd to IMU
sendcmd(sCmdStart);
// Wait start MPU sequence
for (int i = 1; i <=iDelaySeq; i+=50) {
if (ComPort->waitForReadyRead(50)) break;
}
return;
}
void FTNoIR_Tracker::StopTracker( bool exit )
{
QByteArray Msg;
if (sCmdStop.length()>0) {
if (ComPort->isOpen() )
{
ComPort->write(sCmdStop);
if (!ComPort->waitForBytesWritten(1000)) {
emit sendMsgInfo("TimeOut in writing CMD");
} else {
Msg.append("\r\n");
Msg.append("SEND '");
Msg.append(sCmdStop);
Msg.append("'\r\n");
}
emit sendMsgInfo(Msg);
}
}
// OK, the thread is not stopped, doing this. That might be dangerous anyway...
//
if (exit || !exit) return;
return;
}
#else
void FTNoIR_Tracker::StartTracker(QFrame*)
{
static const int databits_lookup[] = {
QSerialPort::Data5,
QSerialPort::Data6,
QSerialPort::Data7,
QSerialPort::Data8,
QSerialPort::UnknownDataBits
};
struct Local {
static int idx(int max, int value)
{
if (value < 0)
return 0;
if (max > value)
return value;
return max - 1;
}
};
static const int parity_lookup[] = {
QSerialPort::NoParity,
QSerialPort::EvenParity,
QSerialPort::OddParity,
QSerialPort::SpaceParity,
QSerialPort::MarkParity,
QSerialPort::UnknownParity
};
static const int stopbits_lookup[] = {
QSerialPort::OneStop,
QSerialPort::OneAndHalfStop,
QSerialPort::TwoStop,
QSerialPort::UnknownStopBits
};
static const int flowctl_lookup[] = {
QSerialPort::NoFlowControl,
QSerialPort::HardwareControl,
QSerialPort::SoftwareControl,
};
static const int baudrate_lookup[] = {
QSerialPort::Baud1200,
QSerialPort::Baud2400,
QSerialPort::Baud4800,
QSerialPort::Baud9600,
QSerialPort::Baud19200,
QSerialPort::Baud38400,
QSerialPort::Baud57600,
QSerialPort::Baud115200,
QSerialPort::UnknownBaud
};
CptError=0;
dataRead.clear();
frame_cnt=0;
ComPort = new QSerialPort(this);
{
ComPort->setPortName(QSerialPortInfo::availablePorts().value(settings.SerialPortName).portName());
}
if (ComPort->open(QIODevice::ReadWrite ) == true) {
connect(ComPort, SIGNAL(readyRead()), this, SLOT(SerialRead()));
if (
ComPort->setBaudRate(baudrate_lookup[Local::idx(8, settings.pBaudRate)])
&& ComPort->setDataBits((QSerialPort::DataBits)databits_lookup[Local::idx(4, settings.pDataBits)])
&& ComPort->setParity((QSerialPort::Parity)parity_lookup[Local::idx(5, settings.pParity)])
&& ComPort->setStopBits((QSerialPort::StopBits)stopbits_lookup[Local::idx(3, settings.pStopBits)])
&& ComPort->setFlowControl((QSerialPort::FlowControl)flowctl_lookup[Local::idx(3, settings.pFlowControl)])
&& ComPort->clear(QSerialPort::AllDirections)
&& ComPort->setDataErrorPolicy(QSerialPort::IgnorePolicy)
){
// Wait init arduino sequence
for (int i = 1; i <=settings.DelayInit; i+=50) {
if (ComPort->waitForReadyRead(50)) break;
}
sendcmd(static_cast<QString>(settings.CmdInit).toLatin1());
// Wait init MPU sequence
for (int i = 1; i <=settings.DelayStart; i+=50) {
if (ComPort->waitForReadyRead(50)) break;
}
// Send START cmd to IMU
sendcmd(static_cast<QString>(settings.CmdStart).toLatin1());
// Wait start MPU sequence
for (int i = 1; i <=settings.DelaySeq; i+=50) {
if (ComPort->waitForReadyRead(50)) break;
}
} else {
QMessageBox::warning(0,"FaceTrackNoIR Error", ComPort->errorString(),QMessageBox::Ok,QMessageBox::NoButton);
}
}
else {
QMessageBox::warning(0,"FaceTrackNoIR Error", "Unable to open ComPort",QMessageBox::Ok,QMessageBox::NoButton);
delete ComPort;
ComPort = NULL;
}
return;
}
#endif
//
// Return 6DOF info
//
#ifdef OPENTRACK_API
#define THeadPoseData double
#endif
void FTNoIR_Tracker::GetHeadPoseData(THeadPoseData *data)
{
QMutexLocker lck(&mutex);
while (dataRead.length()>=30) {
if ((dataRead.startsWith(Begin) && ( dataRead.mid(28,2)==End )) ) { // .Begin==0xAAAA .End==0x5555
QDataStream datastream(dataRead.left(30));
if (settings.BigEndian) datastream.setByteOrder(QDataStream::BigEndian );
else datastream.setByteOrder(QDataStream::LittleEndian );
datastream>>ArduinoData;
frame_cnt++;
if (ArduinoData.Code <= 1000) {
HAT=ArduinoData;
} else {
emit sendMsgInfo(dataRead.mid(4,24)) ;
}
dataRead.remove(0,30);
} else {
// resynchro trame
int index = dataRead.indexOf(Begin);
if (index==-1) {
index=dataRead.length();
}
emit sendMsgInfo(dataRead.mid(0,index)) ;
dataRead.remove(0,index);
CptError++;
}
}
if (CptError>50) {
emit sendMsgInfo("Can't find HAT frame") ;
CptError=0;
return;
}
data[frame_cnt] = (long) HAT.Code;
struct Fun {
static int clamp3(int foo)
{
if (foo > 2)
return 2;
if (foo < 0)
return 0;
return foo;
}
};
if (settings.EnableYaw) {
if (settings.InvertYaw) data[Yaw] = (double) HAT.Rot[Fun::clamp3(settings.YawAxe)] * -1.0f;
else data[Yaw] = (double) HAT.Rot[Fun::clamp3(settings.YawAxe)];
}
if (settings.EnablePitch) {
if (settings.InvertPitch) data[Pitch] = (double) HAT.Rot[Fun::clamp3(settings.PitchAxe)] * -1.0f;
else data[Pitch] = (double) HAT.Rot[Fun::clamp3(settings.InvertPitch)];
}
if (settings.EnableRoll) {
if (settings.InvertRoll) data[Roll] = (double) HAT.Rot[Fun::clamp3(settings.RollAxe)] * -1.0f;
else data[Roll] = (double) HAT.Rot[Fun::clamp3(settings.RollAxe)];
}
if (settings.EnableX) {
if (settings.InvertX) data[TX] =(double) HAT.Trans[Fun::clamp3(settings.XAxe)]* -1.0f;
else data[TX] = HAT.Trans[Fun::clamp3(settings.XAxe)];
}
if (settings.EnableY) {
if (settings.InvertY) data[TY] =(double) HAT.Trans[Fun::clamp3(settings.YAxe)]* -1.0f;
else data[TY] = HAT.Trans[Fun::clamp3(settings.YAxe)];
}
if (settings.EnableZ) {
if (settings.InvertZ) data[TZ] = HAT.Trans[Fun::clamp3(settings.ZAxe)]* -1.0f;
else data[TZ] = HAT.Trans[Fun::clamp3(settings.ZAxe)];
}
}
void FTNoIR_Tracker::applysettings(const TrackerSettings& settings){
QMutexLocker lck(&mutex);
settings.b->reload();
}
#ifdef OPENTRACK_API
extern "C" FTNOIR_TRACKER_BASE_EXPORT ITracker* CALLING_CONVENTION GetConstructor()
#else
#pragma comment(linker, "/export:GetTracker=_GetTracker@0")
FTNOIR_TRACKER_BASE_EXPORT ITrackerPtr __stdcall GetTracker()
#endif
{
return new FTNoIR_Tracker;
}
|