From 4acd072afb9cc76231b1d666d933f3dbbb4b3baa Mon Sep 17 00:00:00 2001 From: Wim Vriend Date: Fri, 4 Feb 2011 15:07:16 +0000 Subject: Testing Client/Server configuration. git-svn-id: svn+ssh://svn.code.sf.net/p/facetracknoir/code@47 19e81ba0-9b1a-49c3-bd6c-561e1906d5fb --- FTNoIR_Tracker_UDP/FTNoIR_Tracker_UDP.vcproj | 4 +- FTNoIR_Tracker_UDP/ftnoir_tracker_udp.cpp | 117 ++++++++++++++++++++------- 2 files changed, 88 insertions(+), 33 deletions(-) (limited to 'FTNoIR_Tracker_UDP') diff --git a/FTNoIR_Tracker_UDP/FTNoIR_Tracker_UDP.vcproj b/FTNoIR_Tracker_UDP/FTNoIR_Tracker_UDP.vcproj index d5df6fc0..55e6d66b 100644 --- a/FTNoIR_Tracker_UDP/FTNoIR_Tracker_UDP.vcproj +++ b/FTNoIR_Tracker_UDP/FTNoIR_Tracker_UDP.vcproj @@ -38,7 +38,7 @@ /> #include +#include #include "Windows.h" +#include "math.h" class FTNoIR_Tracker_UDP : public ITracker, QThread { @@ -14,6 +16,8 @@ public: void StartTracker(); void GiveHeadPoseData(THeadPoseData *data); + bool setParameterValue(const int index, const float newvalue); + protected: void run(); // qthread override run method @@ -31,6 +35,16 @@ private: int srcPort; // Source port-number THeadPoseData newHeadPose; // Structure with new headpose + + //parameter list for the filter-function(s) + enum + { + kPortAddress=0, // Index in QList + kNumFilterParameters // Indicate number of parameters used + }; + QList> parameterRange; + QList parameterValueAsFloat; + }; FTNoIR_Tracker_UDP::FTNoIR_Tracker_UDP() @@ -42,12 +56,38 @@ FTNoIR_Tracker_UDP::FTNoIR_Tracker_UDP() m_StopThread = CreateEvent(0, TRUE, FALSE, 0); m_WaitThread = CreateEvent(0, TRUE, FALSE, 0); - newHeadPose.x = 1.0f; - newHeadPose.y = 2.0f; - newHeadPose.z = 3.0f; - newHeadPose.yaw = 4.0f; - newHeadPose.pitch = 5.0f; - newHeadPose.roll = 6.0f; + //allocate memory for the parameters + parameterValueAsFloat.clear(); + parameterRange.clear(); + + // Add the parameters to the list + parameterRange.append(std::pair(1000.0f,9999.0f)); + parameterValueAsFloat.append(0.0f); + setParameterValue(kPortAddress,5551.0f); + + newHeadPose.x = 0.0f; + newHeadPose.y = 0.0f; + newHeadPose.z = 0.0f; + newHeadPose.yaw = 0.0f; + newHeadPose.pitch = 0.0f; + newHeadPose.roll = 0.0f; + + // + // Create UDP-sockets if they don't exist already. + // They must be created here, because they must be in the new thread (FTNoIR_Tracker_UDP::run()) + // + if (inSocket == 0) { + qDebug() << "FTNoIR_Tracker_UDP::run() creating insocket"; + inSocket = new QUdpSocket(); + // Connect the inSocket to the port, to receive messages + + if (!inSocket->bind(QHostAddress::Any, (int) parameterValueAsFloat[kPortAddress], QUdpSocket::ShareAddress )) { + QMessageBox::warning(0,"FaceTrackNoIR Error", "Unable to bind UDP-port",QMessageBox::Ok,QMessageBox::NoButton); + delete inSocket; + inSocket = 0; + } + } + } FTNoIR_Tracker_UDP::~FTNoIR_Tracker_UDP() @@ -82,17 +122,6 @@ int no_bytes; QHostAddress sender; quint16 senderPort; - // - // Create UDP-sockets if they don't exist already. - // They must be created here, because they must be in the new thread (FTNoIR_Tracker_UDP::run()) - // - if (inSocket == 0) { - qDebug() << "FTNoIR_Tracker_UDP::run() creating insocket"; - inSocket = new QUdpSocket(); - // Connect the inSocket to the port, to receive messages - inSocket->bind(QHostAddress::Any, destPort+1); - } - // // Read the data that was received. // @@ -107,18 +136,23 @@ quint16 senderPort; return; } - while (inSocket->hasPendingDatagrams()) { + if (inSocket != 0) { + while (inSocket->hasPendingDatagrams()) { - QByteArray datagram; - datagram.resize(inSocket->pendingDatagramSize()); + QByteArray datagram; + datagram.resize(inSocket->pendingDatagramSize()); - inSocket->readDatagram( (char * ) &newHeadPose, sizeof(newHeadPose), &sender, &senderPort); + inSocket->readDatagram( (char * ) &newHeadPose, sizeof(newHeadPose), &sender, &senderPort); + } + } + else { + qDebug() << "FTNoIR_Tracker_UDP::run() insocket not ready: exit run()"; + return; } //for lower cpu load - usleep(5000); + usleep(10000); yieldCurrentThread(); - } } @@ -140,14 +174,6 @@ void FTNoIR_Tracker_UDP::StartTracker() void FTNoIR_Tracker_UDP::GiveHeadPoseData(THeadPoseData *data) { - - //newHeadPose.x += 1.0f; - //newHeadPose.y += 2.0f; - //newHeadPose.z += 3.0f; - //newHeadPose.yaw += 4.0f; - //newHeadPose.pitch += 5.0f; - //newHeadPose.roll += 6.0f; - data->x = newHeadPose.x; data->y = newHeadPose.y; data->z = newHeadPose.z; @@ -157,6 +183,35 @@ void FTNoIR_Tracker_UDP::GiveHeadPoseData(THeadPoseData *data) return; } +bool FTNoIR_Tracker_UDP::setParameterValue(const int index, const float newvalue) +{ + if ((index >= 0) && (index < parameterValueAsFloat.size())) + { + // + // Limit the new value, using the defined range. + // + if (newvalue < parameterRange[index].first) { + parameterValueAsFloat[index] = parameterRange[index].first; + } + else { + if (newvalue > parameterRange[index].second) { + parameterValueAsFloat[index] = parameterRange[index].second; + } + else { + parameterValueAsFloat[index] = newvalue; + } + } + +// updateParameterString(index); + return true; + } + else + { + return false; + } +}; + + //////////////////////////////////////////////////////////////////////////////// // Factory function that creates instances if the Tracker object. -- cgit v1.2.3