summaryrefslogtreecommitdiffhomepage
path: root/ftnoir_tracker_hatire/ftnoir_tracker_hat.cpp
blob: 99baf75be26ba9d61706fdedb48fd8a743b0da5e (plain)
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
/* code by Furax49, awaiting copyright information */

#include "ftnoir_tracker_hat.h"
#include "facetracknoir/global-settings.h"
#include <QMessageBox>
#include <QDebug>

FTNoIR_Tracker::FTNoIR_Tracker() :
    SerialPort(nullptr),
    stop(false)
{
	TrackerSettings settings;
	settings.load_ini();
	applysettings(settings);

    //ListErrInf  = new QList<QString>();

	datagram.reserve(30);
    qDebug() << "FTNoIR_Tracker::Initialize() Open SerialPort";
    SerialPort = new QSerialPort(sSerialPortName);
    if (SerialPort->open(QIODevice::ReadWrite | QIODevice::Unbuffered  ) == true) {
        SerialPort->flush();
        SerialPort->setBaudRate(115200);
        SerialPort->setParity(QSerialPort::NoParity);
        SerialPort->setDataBits(QSerialPort::Data8);
        SerialPort->setStopBits(QSerialPort::OneStop);
        SerialPort->setFlowControl(QSerialPort::NoFlowControl);
        //SerialPort->setTimeout(waitTimeout);
        //SerialPort->setQueryMode(QextSerialPort::EventDriven); //Polling
        SerialPort->putChar('S');
    }
    else {
        QMessageBox::warning(0,"FaceTrackNoIR Error", "Unable to open SerialPort",QMessageBox::Ok,QMessageBox::NoButton);
        delete SerialPort;
        SerialPort = NULL;
    }
}

FTNoIR_Tracker::~FTNoIR_Tracker()
{
    stop = true;
    wait();
	if (SerialPort!=NULL) {
		if (SerialPort->isOpen() ) {
			SerialPort->putChar('s'); //Send STOP to Arduino
			SerialPort->close();
		}
		delete SerialPort;
		SerialPort=NULL;
	}

}

//send CENTER to Arduino
void FTNoIR_Tracker::notifyCenter() {
	if (SerialPort!=NULL) {
		if (SerialPort->isOpen() ) {
			SerialPort->putChar('C');
		}
	}
}



//send CENTER to Arduino
void FTNoIR_Tracker::center() {
	if (SerialPort!=NULL) {
		if (SerialPort->isOpen() ) {
			SerialPort->putChar('C');
		}
	}
}

//send RESET to Arduino
void FTNoIR_Tracker::reset() {
	if (SerialPort!=NULL) {
		if (SerialPort->isOpen() ) {
			SerialPort->putChar('R');
		}
	}
}

//send command  to Arduino
void FTNoIR_Tracker::sendcmd(QString* cmd) {
    QMutexLocker locker(&lock);
	QByteArray bytes;
	if (SerialPort!=NULL) {
		if (SerialPort->isOpen() ) {
            bytes.append(cmd->toLatin1());
            SerialPort->write(bytes);
		}
	}
}

// return FPS and last status
void FTNoIR_Tracker::get_info(QString*, int* tps ){
    QMutexLocker locker(&lock);
	*tps=HAT.Code;
#if 0
	if (ListErrInf->size()>0)  {
		*info=ListErrInf->takeFirst();
	} else {
		*info= QString();
	}
#endif
}


/** QThread run @override **/
void FTNoIR_Tracker::run() {
    if (!SerialPort)
        return;
    while (!stop)
    {
        if (SerialPort->bytesAvailable()>=30)
        {
            QMutexLocker locker(&lock);
			datagram.clear();
			datagram=SerialPort->read(30);
            QDataStream datastream(datagram);
            datastream >> ArduinoData;
            if (ArduinoData.Begin==0xAAAA && ArduinoData.End==0x5555 )
            {
                if (ArduinoData.Code <= 1000)
                {
					HAT=ArduinoData;
                }
			} else { 
				SerialPort->read(1);
			}
		}
		usleep(10000);
	}
}

void FTNoIR_Tracker::StartTracker( QFrame* )
{
	start( QThread::TimeCriticalPriority );
	return;
}

bool FTNoIR_Tracker::GiveHeadPoseData(double* data)
{
    QMutexLocker locker(&lock);

    const bool inversions[] = {
        bInvertX, bInvertY, bInvertZ, bInvertYaw, bInvertPitch, bInvertRoll
    };

    const bool enablement[] = {
        bEnableX, bEnableY, bEnableZ, bEnableYaw, bEnablePitch, bEnableRoll
    };

    const int axes[] = {
        iXAxis, iYAxis, iZAxis, iYawAxis, iPitchAxis, iRollAxis
    };

    for (int i = 0; i < 6; i++)
    {
        if (enablement[i])
            data[i] = HAT.Gyro[axes[i]] * (inversions[i] ? -1 : 1);
    }

    return true;
}

void FTNoIR_Tracker::applysettings(const TrackerSettings& settings){
	qDebug()<<"Tracker:: Applying settings";

    QMutexLocker locker(&lock);
	sSerialPortName= settings.SerialPortName;

	bEnableRoll = settings.EnableRoll;
	bEnablePitch = settings.EnablePitch;
	bEnableYaw = settings.EnableYaw;
	bEnableX = settings.EnableX;
	bEnableY = settings.EnableY;
	bEnableZ = settings.EnableZ;

	bInvertRoll = settings.InvertRoll;
	bInvertPitch = settings.InvertPitch;
	bInvertYaw = settings.InvertYaw;
	bInvertX = settings.InvertX;
	bInvertY = settings.InvertY;
	bInvertZ = settings.InvertZ;


    iRollAxis= settings.RollAxis;
    iPitchAxis= settings.PitchAxis;
    iYawAxis= settings.YawAxis;
    iXAxis= settings.XAxis;
    iYAxis= settings.YAxis;
    iZAxis= settings.ZAxis;
}

extern "C" FTNOIR_TRACKER_BASE_EXPORT ITracker* CALLING_CONVENTION GetConstructor()
{
	return new FTNoIR_Tracker;
}