forked from avin2/SensorKinect
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathXnDataProcessor.cpp
More file actions
193 lines (155 loc) · 7.36 KB
/
XnDataProcessor.cpp
File metadata and controls
193 lines (155 loc) · 7.36 KB
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
/*****************************************************************************
* *
* PrimeSense Sensor 5.0 Alpha *
* Copyright (C) 2010 PrimeSense Ltd. *
* *
* This file is part of PrimeSense Common. *
* *
* PrimeSense Sensor is free software: you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as published *
* by the Free Software Foundation, either version 3 of the License, or *
* (at your option) any later version. *
* *
* PrimeSense Sensor 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 Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public License *
* along with PrimeSense Sensor. If not, see <http://www.gnu.org/licenses/>. *
* *
*****************************************************************************/
//---------------------------------------------------------------------------
// Includes
//---------------------------------------------------------------------------
#include "XnDataProcessor.h"
#include <XnProfiling.h>
#include "XnSensor.h"
//---------------------------------------------------------------------------
// Code
//---------------------------------------------------------------------------
XnDataProcessor::XnDataProcessor(XnDevicePrivateData* pDevicePrivateData, const XnChar* csName) :
m_pDevicePrivateData(pDevicePrivateData),
m_csName(csName),
m_nLastPacketID(0),
m_nBytesReceived(0)
{
m_TimeStampData.csStreamName = csName;
m_TimeStampData.bFirst = TRUE;
}
XnDataProcessor::~XnDataProcessor()
{}
XnStatus XnDataProcessor::Init()
{
return (XN_STATUS_OK);
}
void XnDataProcessor::ProcessData(const XnSensorProtocolResponseHeader* pHeader, const XnUChar* pData, XnUInt32 nDataOffset, XnUInt32 nDataSize)
{
XN_PROFILING_START_SECTION("XnDataProcessor::ProcessData")
// count these bytes
m_nBytesReceived += nDataSize;
// check if we start a new packet
if (nDataOffset == 0)
{
// make sure no packet was lost
if (pHeader->nReserve != m_nLastPacketID+1 && pHeader->nReserve != 0)
{
xnLogWarning(XN_MASK_SENSOR_PROTOCOL, "%s: Expected %x, got %x", m_csName, m_nLastPacketID+1, pHeader->nReserve);
OnPacketLost();
}
m_nLastPacketID = pHeader->nReserve;
// log packet arrival
XnUInt64 nNow;
xnOSGetHighResTimeStamp(&nNow);
xnDumpWriteString(m_pDevicePrivateData->MiniPacketsDump, "%llu,0x%hx,0x%hx,0x%hx,%u\n", nNow, pHeader->nType, pHeader->nReserve, pHeader->nBufSize, pHeader->nTimeStamp);
}
ProcessPacketChunk(pHeader, pData, nDataOffset, nDataSize);
XN_PROFILING_END_SECTION
}
void XnDataProcessor::OnPacketLost()
{}
XnUInt64 XnDataProcessor::GetTimeStamp(XnUInt32 nDeviceTimeStamp)
{
const XnUInt64 nWrapPoint = ((XnUInt64)XN_MAX_UINT32) + 1;
XnUInt64 nResultInTicks;
XnUInt64 nNow;
xnOSGetHighResTimeStamp(&nNow);
XnChar csDumpComment[200] = "";
XnBool bCheckSanity = TRUE;
// we register the first TS calculated as time-zero. Every stream's TS data will be
// synchronized with it
if (m_pDevicePrivateData->nGlobalReferenceTS == 0)
{
xnOSEnterCriticalSection(&m_pDevicePrivateData->hEndPointsCS);
if (m_pDevicePrivateData->nGlobalReferenceTS == 0)
{
m_pDevicePrivateData->nGlobalReferenceTS = nDeviceTimeStamp;
m_pDevicePrivateData->nGlobalReferenceOSTime = nNow;
}
xnOSLeaveCriticalSection(&m_pDevicePrivateData->hEndPointsCS);
}
if (m_TimeStampData.bFirst)
{
// check how much OS time passed since global reference was taken
XnUInt64 nOSTime = nNow - m_pDevicePrivateData->nGlobalReferenceOSTime;
// check how many full wrap-arounds occurred (according to OS time)
XnFloat fWrapAroundInMicroseconds = nWrapPoint / (XnDouble)m_pDevicePrivateData->fDeviceFrequency;
XnUInt32 nWraps = nOSTime / fWrapAroundInMicroseconds; // floor
// now check, if current timestamp is less than global, then we have one more wrap-around
// (make sure it's significant - we allow up to 10 ms before - otherwise it could just be a
// matter of race-condition)
if (m_pDevicePrivateData->nGlobalReferenceTS > nDeviceTimeStamp &&
nOSTime > XN_SENSOR_TIMESTAMP_SANITY_DIFF*1000)
{
++nWraps;
}
m_TimeStampData.nReferenceTS = m_pDevicePrivateData->nGlobalReferenceTS;
m_TimeStampData.nTotalTicksAtReferenceTS = nWrapPoint * nWraps;
m_TimeStampData.nLastDeviceTS = 0;
m_TimeStampData.bFirst = FALSE;
nResultInTicks = 0;
bCheckSanity = FALSE; // no need.
sprintf(csDumpComment, "Init. Total Ticks in Ref TS: %llu", m_TimeStampData.nTotalTicksAtReferenceTS);
}
if (nDeviceTimeStamp > m_TimeStampData.nLastDeviceTS) // this is the normal case
{
nResultInTicks = m_TimeStampData.nTotalTicksAtReferenceTS + nDeviceTimeStamp - m_TimeStampData.nReferenceTS;
}
else // wrap around occurred
{
// add the passed time to the reference time
m_TimeStampData.nTotalTicksAtReferenceTS += (nWrapPoint + nDeviceTimeStamp - m_TimeStampData.nReferenceTS);
// mark reference timestamp
m_TimeStampData.nReferenceTS = nDeviceTimeStamp;
sprintf(csDumpComment, "Wrap around. Refernce TS: %u / TotalTicksAtReference: %llu", m_TimeStampData.nReferenceTS, m_TimeStampData.nTotalTicksAtReferenceTS);
nResultInTicks = m_TimeStampData.nTotalTicksAtReferenceTS;
}
m_TimeStampData.nLastDeviceTS = nDeviceTimeStamp;
// calculate result in microseconds
// NOTE: Intel compiler does too much optimization, and we loose up to 5 milliseconds. We perform
// the entire calculation in XnDouble as a workaround
XnDouble dResultTimeMicroSeconds = (XnDouble)nResultInTicks / (XnDouble)m_pDevicePrivateData->fDeviceFrequency;
XnUInt64 nResultTimeMilliSeconds = (XnUInt64)(dResultTimeMicroSeconds / 1000.0);
XnBool bIsSane = TRUE;
// perform sanity check
if (bCheckSanity && (nResultTimeMilliSeconds > (m_TimeStampData.nLastResultTime + XN_SENSOR_TIMESTAMP_SANITY_DIFF*1000)))
{
bIsSane = FALSE;
sprintf(csDumpComment, "%s,Didn't pass sanity. Will try to re-sync.", csDumpComment);
}
// calc result
XnUInt64 nResult = (m_pDevicePrivateData->pSensor->IsHighResTimestamps() ? (XnUInt64)dResultTimeMicroSeconds : nResultTimeMilliSeconds);
// dump it
xnDumpWriteString(m_pDevicePrivateData->TimestampsDump, "%llu,%s,%u,%llu,%s\n", nNow, m_TimeStampData.csStreamName, nDeviceTimeStamp, nResult, csDumpComment);
if (bIsSane)
{
m_TimeStampData.nLastResultTime = nResultTimeMilliSeconds;
return (nResult);
}
else
{
// sanity failed. We lost sync. restart
m_TimeStampData.bFirst = TRUE;
return GetTimeStamp(nDeviceTimeStamp);
}
}