aboutsummaryrefslogtreecommitdiffstats
path: root/LibOVR/Src/Vision/SensorFusion/Vision_SensorStateReader.cpp
blob: b60b500979f199a3cff19d3f19907d6519b23c22 (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
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
/************************************************************************************

Filename    :   Vision_SensorStateReader.cpp
Content     :   Separate reader component that is able to recover sensor pose
Created     :   June 4, 2014
Authors     :   Chris Taylor

Copyright   :   Copyright 2014 Oculus VR, LLC All Rights reserved.

Licensed under the Oculus VR Rift SDK License Version 3.2 (the "License"); 
you may not use the Oculus VR Rift SDK except in compliance with the License, 
which is provided at the time of installation or download, or which 
otherwise accompanies this software in either electronic or hard copy form.

You may obtain a copy of the License at

http://www.oculusvr.com/licenses/LICENSE-3.2 

Unless required by applicable law or agreed to in writing, the Oculus VR SDK 
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

*************************************************************************************/

#include "Vision_SensorStateReader.h"


namespace OVR { namespace Vision {


//-------------------------------------------------------------------------------------

// This is a "perceptually tuned predictive filter", which means that it is optimized
// for improvements in the VR experience, rather than pure error.  In particular,
// jitter is more perceptible at lower speeds whereas latency is more perceptible
// after a high-speed motion.  Therefore, the prediction interval is dynamically
// adjusted based on speed.  Significant more research is needed to further improve
// this family of filters.
static Pose<double> calcPredictedPose(const PoseState<double>& poseState, double predictionDt)
{
    Pose<double> pose = poseState.ThePose;
    const double linearCoef = 1.0;
    Vector3d angularVelocity = poseState.AngularVelocity;
    double angularSpeed = angularVelocity.Length();

    // This could be tuned so that linear and angular are combined with different coefficients
    double speed = angularSpeed + linearCoef * poseState.LinearVelocity.Length();

    const double slope = 0.2; // The rate at which the dynamic prediction interval varies
    double candidateDt = slope * speed; // TODO: Replace with smoothstep function

    double dynamicDt = predictionDt;

    // Choose the candidate if it is shorter, to improve stability
    if (candidateDt < predictionDt)
    {
        dynamicDt = candidateDt;
    }

    if (angularSpeed > 0.001)
    {
        pose.Rotation = pose.Rotation * Quatd(angularVelocity, angularSpeed * dynamicDt);
    }

    pose.Translation += poseState.LinearVelocity * dynamicDt;

    return pose;
}

PoseState<float> calcPredictedPoseState(const LocklessSensorState& sensorState, double absoluteTime, const Posed& centeredFromWorld)
{
    // Delta time from the last available data
    double pdt = absoluteTime - sensorState.WorldFromImu.TimeInSeconds;
    static const double maxPdt = 0.1;

    // If delta went negative due to synchronization problems between processes or just a lag spike,
    if (pdt < 0)
    {
        pdt = 0;
    }
    else if (pdt > maxPdt)
    {
        pdt = maxPdt;
        static double lastLatWarnTime = 0;
        if (lastLatWarnTime != sensorState.WorldFromImu.TimeInSeconds)
        {
            lastLatWarnTime = sensorState.WorldFromImu.TimeInSeconds;
            LogText("[TrackingStateReader] Prediction interval too high: %f s, clamping at %f s\n", pdt, maxPdt);
        }
    }

    PoseStatef result;
    result = PoseStatef(sensorState.WorldFromImu);
    result.TimeInSeconds = absoluteTime;
    result.ThePose = Posef(centeredFromWorld * calcPredictedPose(sensorState.WorldFromImu, pdt) * sensorState.ImuFromCpf);
    return result;
}

//// TrackingStateReader

// Pre-0.5.0 applications assume that the initial WorldFromCentered
// pose is always identity, because the WorldFromImu pose has a 180-degree flip in Y 
// and a 1-meter offset in Z.  See CAPI_HMDState.cpp
Posed TrackingStateReader::DefaultWorldFromCentered(Quatd::Identity(), Vector3d(0, 0, 0));

// At startup, we want an identity pose when the user is looking along the positive camera Z axis, one meter in front of camera.
// That is a 180 degree rotation about Y, with a -1 meter translation (the inverse of this pose, CenteredFromWorld, is actually used)
// (NOTE: This pose should be the same as SensorFusionFilter::DefaultWorldFromImu)
//
//Posed TrackingStateReader::DefaultWorldFromCentered(Quatd(0, 1, 0, 0), Vector3d(0, 0, -1));

TrackingStateReader::TrackingStateReader() :
    HmdUpdater(nullptr),
    CameraUpdater(nullptr)
{
    CenteredFromWorld = GetDefaultCenteredFromWorld();
}

void TrackingStateReader::SetUpdaters(const CombinedHmdUpdater *hmd, const CameraStateUpdater *camera)
{
    HmdUpdater    = hmd;
    CameraUpdater = camera;
}


// This function centers tracking on the current pose, such that when the
// headset is positioned at the current pose and looking level in the current direction,
// the tracking system pose will be identity.
// In other words, tracking is relative to this centered pose.
//
bool TrackingStateReader::RecenterPose(const Vector3d& neckModelOffset)
{
    if (!HmdUpdater)
        return false;

    const LocklessSensorState lstate = HmdUpdater->SensorState.GetState();
    Posed worldFromCpf = (lstate.WorldFromImu.ThePose * lstate.ImuFromCpf);

    return ComputeCenteredFromWorld(worldFromCpf, neckModelOffset);
}

bool TrackingStateReader::ComputeCenteredFromWorld(const Posed& worldFromCpf, const Vector3d& neckModel)
{
    // Position of CPF in the head rotation center frame
    const Vector3d cpfInRotationCenter = neckModel;

    const Vector3d forward(0, 0, -1);
    const Vector3d up(0, 1, 0);
    Vector3d look = worldFromCpf.Rotate(forward);

    // If the headset is pointed straight up or straight down,
    // it may be face down on a tabletop.  In this case we
    // can't reliably extract a heading angle.
    // We assume straight ahead and return false so caller 
    // knows that recenter may not be reliable.
    bool headingValid = true;
    static const double lookTol = cos(DegreeToRad(20.0));
    if (fabs(look.Dot(up)) >= lookTol)    // fabs(lookBack.Dot(up))
    {
        look = forward;
        headingValid = false;
    }

    // Now compute the orientation of the headset when looking straight ahead:
    // Extract the heading (yaw) component of the pose
    Vector3d centeredLook = Vector3d(look.x, 0, look.z).Normalized();
    Quatd centeredOrientation = Quatd::Align(centeredLook, forward);

    // Compute the position in world space of the head rotation center:
    // we assume the head rotates about this point in space.
    Vector3d headRotationCenter = worldFromCpf.Transform(-cpfInRotationCenter);

    // Now apply the heading rotation to compute the reference position of the CPF
    // relative to the head rotation center.
    Vector3d centeredCpfPos = headRotationCenter + centeredOrientation.Rotate(cpfInRotationCenter);

    // Now compute the centered pose of the CPF.
    Posed worldFromCentered(centeredOrientation, centeredCpfPos);

    // For tracking, we use the inverse of the centered pose
    CenteredFromWorld = worldFromCentered.Inverted();

    return headingValid;
}

bool TrackingStateReader::GetTrackingStateAtTime(double absoluteTime, TrackingState& ss) const
{
    LocklessCameraState cameraState;
    LocklessSensorState sensorState;

    if (CameraUpdater)
        cameraState = CameraUpdater->GetState();
    if (HmdUpdater)
        sensorState = HmdUpdater->SensorState.GetState();

    // Update the status flags
    ss.StatusFlags = cameraState.StatusFlags | sensorState.StatusFlags;

    // If no hardware is connected, override the tracking flags
    if (0 == (ss.StatusFlags & Status_HMDConnected))
    {
        ss.StatusFlags &= ~Status_TrackingMask;
    }
    if (0 == (ss.StatusFlags & Status_PositionConnected))
    {
        ss.StatusFlags &= ~(Status_PositionTracked | Status_CameraPoseTracked);
    }

    // If tracking info is invalid,
    if (0 == (ss.StatusFlags & Status_TrackingMask))
    {
        return false;
    }
    
    ss.HeadPose = calcPredictedPoseState(sensorState, absoluteTime, CenteredFromWorld);

    ss.CameraPose        = Posef(CenteredFromWorld * cameraState.WorldFromCamera);
    ss.LeveledCameraPose = Posef(CenteredFromWorld * Posed(Quatd(), cameraState.WorldFromCamera.Translation));

    ss.RawSensorData = sensorState.RawSensorData;
    return true;
}

bool TrackingStateReader::GetPoseAtTime(double absoluteTime, Posef& transform) const
{
    TrackingState ss;

    if (!GetTrackingStateAtTime(absoluteTime, ss))
    {
        return false;
    }

    transform = ss.HeadPose.ThePose;

    return true;
}

uint32_t TrackingStateReader::GetStatus() const
{
    TrackingState ss;

    if (!GetTrackingStateAtTime(0, ss))
    {
        return 0;
    }

    return ss.StatusFlags;
}


}} // namespace OVR::Vision