oculus1
view libovr/Src/Util/Util_MagCalibration.h @ 24:8419d8a13cee
foo
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Fri, 04 Oct 2013 14:50:26 +0300 |
parents | e2f9e4603129 |
children |
line source
1 /************************************************************************************
3 PublicHeader: OVR.h
4 Filename : Util_MagCalibration.h
5 Content : Procedures for calibrating the magnetometer
6 Created : April 16, 2013
7 Authors : Steve LaValle, Andrew Reisse
9 Copyright : Copyright 2013 Oculus VR, Inc. All Rights reserved.
11 Use of this software is subject to the terms of the Oculus license
12 agreement provided at the time of installation or download, or which
13 otherwise accompanies this software in either electronic or hard copy form.
15 *************************************************************************************/
17 #ifndef OVR_Util_MagCalibration_h
18 #define OVR_Util_MagCalibration_h
20 #include "../OVR_SensorFusion.h"
21 #include "../Kernel/OVR_String.h"
22 #include "../Kernel/OVR_Log.h"
24 namespace OVR { namespace Util {
26 class MagCalibration
27 {
28 public:
29 enum MagStatus
30 {
31 Mag_Uninitialized = 0,
32 Mag_AutoCalibrating = 1,
33 Mag_ManuallyCalibrating = 2,
34 Mag_Calibrated = 3
35 };
37 MagCalibration() :
38 Stat(Mag_Uninitialized),
39 MinMagDistance(0.2f), MinQuatDistance(0.5f),
40 SampleCount(0)
41 {
42 MinMagDistanceSq = MinMagDistance * MinMagDistance;
43 MinQuatDistanceSq = MinQuatDistance * MinQuatDistance;
44 MinMagValues = Vector3f(10000.0f,10000.0f,10000.0f);
45 MaxMagValues = Vector3f(-10000.0f,-10000.0f,-10000.0f);
46 MinQuatValues = Quatf(1.0f,1.0f,1.0f,1.0f);
47 MaxQuatValues = Quatf(0.0f,0.0f,0.0f,0.0f);
48 }
50 // Methods that are useful for either auto or manual calibration
51 bool IsUnitialized() const { return Stat == Mag_Uninitialized; }
52 bool IsCalibrated() const { return Stat == Mag_Calibrated; }
53 int NumberOfSamples() const { return SampleCount; }
54 int RequiredSampleCount() const { return 4; }
55 void AbortCalibration()
56 {
57 Stat = Mag_Uninitialized;
58 SampleCount = 0;
59 }
61 void ClearCalibration(SensorFusion& sf)
62 {
63 Stat = Mag_Uninitialized;
64 SampleCount = 0;
65 sf.ClearMagCalibration();
66 };
68 // Methods for automatic magnetometer calibration
69 void BeginAutoCalibration(SensorFusion& sf);
70 unsigned UpdateAutoCalibration(SensorFusion& sf);
71 bool IsAutoCalibrating() const { return Stat == Mag_AutoCalibrating; }
73 // Methods for building a manual (user-guided) calibraton procedure
74 void BeginManualCalibration(SensorFusion& sf);
75 bool IsAcceptableSample(const Quatf& q, const Vector3f& m);
76 bool InsertIfAcceptable(const Quatf& q, const Vector3f& m);
77 // Returns true if successful, requiring that SampleCount = 4
78 bool SetCalibration(SensorFusion& sf);
79 bool IsManuallyCalibrating() const { return Stat == Mag_ManuallyCalibrating; }
81 // This is the minimum acceptable distance (Euclidean) between raw
82 // magnetometer values to be acceptable for usage in calibration.
83 void SetMinMagDistance(float dist)
84 {
85 MinMagDistance = dist;
86 MinMagDistanceSq = MinMagDistance * MinMagDistance;
87 }
89 // The minimum acceptable distance (4D Euclidean) between orientations
90 // to be acceptable for calibration usage.
91 void SetMinQuatDistance(float dist)
92 {
93 MinQuatDistance = dist;
94 MinQuatDistanceSq = MinQuatDistance * MinQuatDistance;
95 }
97 // A result of the calibration, which is the center of a sphere that
98 // roughly approximates the magnetometer data.
99 Vector3f GetMagCenter() const { return MagCenter; }
100 // Retrieves the full magnetometer calibration matrix
101 Matrix4f GetMagCalibration() const;
102 // Retrieves the range of each quaternion term during calibration
103 Quatf GetCalibrationQuatSpread() const { return QuatSpread; }
104 // Retrieves the range of each magnetometer term during calibration
105 Vector3f GetCalibrationMagSpread() const { return MagSpread; }
107 private:
108 // Determine the unique sphere through 4 non-coplanar points
109 Vector3f CalculateSphereCenter(const Vector3f& p1, const Vector3f& p2,
110 const Vector3f& p3, const Vector3f& p4);
112 // Distance from p4 to the nearest point on a plane through p1, p2, p3
113 float PointToPlaneDistance(const Vector3f& p1, const Vector3f& p2,
114 const Vector3f& p3, const Vector3f& p4);
116 Vector3f MagCenter;
117 unsigned Stat;
118 float MinMagDistance;
119 float MinQuatDistance;
120 float MinMagDistanceSq;
121 float MinQuatDistanceSq;
122 // For gathering statistics during calibration
123 Vector3f MinMagValues;
124 Vector3f MaxMagValues;
125 Vector3f MagSpread;
126 Quatf MinQuatValues;
127 Quatf MaxQuatValues;
128 Quatf QuatSpread;
130 unsigned SampleCount;
131 Vector3f MagSamples[4];
132 Quatf QuatSamples[4];
134 };
136 }}
138 #endif