Bike-X  0.8
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
OVR_SensorImpl.h
Go to the documentation of this file.
1 /************************************************************************************
2 
3 Filename : OVR_SensorImpl.h
4 Content : Sensor device specific implementation.
5 Created : March 7, 2013
6 Authors : Lee Cooper
7 
8 Copyright : Copyright 2014 Oculus VR, Inc. All Rights reserved.
9 
10 Licensed under the Oculus VR Rift SDK License Version 3.1 (the "License");
11 you may not use the Oculus VR Rift SDK except in compliance with the License,
12 which is provided at the time of installation or download, or which
13 otherwise accompanies this software in either electronic or hard copy form.
14 
15 You may obtain a copy of the License at
16 
17 http://www.oculusvr.com/licenses/LICENSE-3.1
18 
19 Unless required by applicable law or agreed to in writing, the Oculus VR SDK
20 distributed under the License is distributed on an "AS IS" BASIS,
21 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22 See the License for the specific language governing permissions and
23 limitations under the License.
24 
25 *************************************************************************************/
26 
27 #ifndef OVR_SensorImpl_h
28 #define OVR_SensorImpl_h
29 
30 #include "OVR_HIDDeviceImpl.h"
31 #include "OVR_SensorTimeFilter.h"
32 #include "OVR_Device.h"
33 
34 #ifdef OVR_OS_ANDROID
35 #include "OVR_PhoneSensors.h"
36 #endif
37 
38 namespace OVR {
39 
40 struct TrackerMessage;
41 class ExternalVisitor;
42 
43 //-------------------------------------------------------------------------------------
44 // SensorDeviceFactory enumerates Oculus Sensor devices.
46 {
47 public:
49 
50  // Enumerates devices, creating and destroying relevant objects in manager.
51  virtual void EnumerateDevices(EnumerateVisitor& visitor);
52 
53  virtual bool MatchVendorProduct(UInt16 vendorId, UInt16 productId) const;
54  virtual bool DetectHIDDevice(DeviceManager* pdevMgr, const HIDDeviceDesc& desc);
55 protected:
57 };
58 
59 
60 // Describes a single a Oculus Sensor device and supports creating its instance.
62 {
63 public:
65  : HIDDeviceCreateDesc(factory, Device_Sensor, hidDesc) { }
66 
67  virtual DeviceCreateDesc* Clone() const
68  {
69  return new SensorDeviceCreateDesc(*this);
70  }
71 
72  virtual DeviceBase* NewDeviceInstance();
73 
75  DeviceCreateDesc**) const
76  {
77  if ((other.Type == Device_Sensor) && (pFactory == other.pFactory))
78  {
79  const SensorDeviceCreateDesc& s2 = (const SensorDeviceCreateDesc&) other;
80  if (MatchHIDDevice(s2.HIDDesc))
81  return Match_Found;
82  }
83  return Match_None;
84  }
85 
86  virtual bool MatchHIDDevice(const HIDDeviceDesc& hidDesc) const
87  {
88  // should paths comparison be case insensitive?
89  return ((HIDDesc.Path.CompareNoCase(hidDesc.Path) == 0) &&
90  (HIDDesc.SerialNumber == hidDesc.SerialNumber) &&
91  (HIDDesc.VersionNumber == hidDesc.VersionNumber));
92  }
93 
94  virtual bool GetDeviceInfo(DeviceInfo* info) const;
95 };
96 
97 // A simple stub for notification of a sensor in Boot Loader mode
98 // This descriptor does not support the creation of a device, only the detection
99 // of its existence to warn apps that the sensor device needs firmware.
100 // The Boot Loader descriptor reuses and is created by the Sensor device factory
101 // but in the future may use a dedicated factory
103 {
104 public:
106  : HIDDeviceCreateDesc(factory, Device_BootLoader, hidDesc) { }
107 
108  virtual DeviceCreateDesc* Clone() const
109  {
110  return new BootLoaderDeviceCreateDesc(*this);
111  }
112 
113  // Boot Loader device creation is not allowed
114  virtual DeviceBase* NewDeviceInstance() { return NULL; };
115 
117  DeviceCreateDesc**) const
118  {
119  if ((other.Type == Device_BootLoader) && (pFactory == other.pFactory))
120  {
121  const BootLoaderDeviceCreateDesc& s2 = (const BootLoaderDeviceCreateDesc&) other;
122  if (MatchHIDDevice(s2.HIDDesc))
123  return Match_Found;
124  }
125  return Match_None;
126  }
127 
128  virtual bool MatchHIDDevice(const HIDDeviceDesc& hidDesc) const
129  {
130  // should paths comparison be case insensitive?
131  return ((HIDDesc.Path.CompareNoCase(hidDesc.Path) == 0) &&
132  (HIDDesc.SerialNumber == hidDesc.SerialNumber));
133  }
134 
135  virtual bool GetDeviceInfo(DeviceInfo* info) const
136  {
137  OVR_UNUSED(info);
138  return false;
139  }
140 };
141 
142 
143 //-------------------------------------------------------------------------------------
144 // ***** OVR::SensorDisplayInfoImpl
145 
146 // DisplayInfo obtained from sensor; these values are used to report distortion
147 // settings and other coefficients.
148 // Older SensorDisplayInfo will have all zeros, causing the library to apply hard-coded defaults.
149 // Currently, only resolutions and sizes are used.
151 {
152  enum { PacketSize = 56 };
154 
155  enum
156  {
157  Mask_BaseFmt = 0x0f,
162  };
163 
165 
169  float VCenter;
171  // Currently these values are not well-measured.
173  // TODO: add DistortionEqn
174  // TODO: currently these values are all zeros and the
175  // distortion is hard-coded in HMDDeviceCreateDesc::GetDeviceInfo()
176  float DistortionK[6];
177 
179 
180  void Unpack();
181 };
182 
183 //-------------------------------------------------------------------------------------
184 // ***** OVR::SensorDeviceImpl
185 
186 // Oculus Sensor interface.
187 
188 class SensorDeviceImpl : public HIDDeviceImpl<OVR::SensorDevice>
189 {
190 public:
193 
194 
195  // DeviceCommaon interface
196  virtual bool Initialize(DeviceBase* parent);
197  virtual void Shutdown();
198 
199  virtual void AddMessageHandler(MessageHandler* handler);
200 
201  // HIDDevice::Notifier interface.
202  virtual void OnInputReport(UByte* pData, UInt32 length);
203  virtual double OnTicks(double tickSeconds);
204 
205  // HMD-Mounted sensor has a different coordinate frame.
206  virtual void SetCoordinateFrame(CoordinateFrame coordframe);
207  virtual CoordinateFrame GetCoordinateFrame() const;
208 
209  // SensorDevice interface
210  virtual bool SetRange(const SensorRange& range, bool waitFlag);
211  virtual void GetRange(SensorRange* range) const;
212 
213  virtual void GetFactoryCalibration(Vector3f* AccelOffset, Vector3f* GyroOffset,
214  Matrix4f* AccelMatrix, Matrix4f* GyroMatrix,
215  float* Temperature);
216  virtual void SetOnboardCalibrationEnabled(bool enabled);
217  virtual bool IsMagCalibrated();
218 
219  // Sets report rate (in Hz) of MessageBodyFrame messages (delivered through MessageHandler::OnMessage call).
220  // Currently supported maximum rate is 1000Hz. If the rate is set to 500 or 333 Hz then OnMessage will be
221  // called twice or thrice at the same 'tick'.
222  // If the rate is < 333 then the OnMessage / MessageBodyFrame will be called three
223  // times for each 'tick': the first call will contain averaged values, the second
224  // and third calls will provide with most recent two recorded samples.
225  virtual void SetReportRate(unsigned rateHz);
226  // Returns currently set report rate, in Hz. If 0 - error occurred.
227  // Note, this value may be different from the one provided for SetReportRate. The return
228  // value will contain the actual rate.
229  virtual unsigned GetReportRate() const;
230 
231  bool SetSerialReport(const SerialReport& data);
232  bool GetSerialReport(SerialReport* data);
233 
234  // Hack to create HMD device from sensor display info.
235  static void EnumerateHMDFromSensorDisplayInfo(const SensorDisplayInfoImpl& displayInfo,
237 
238  // These methods actually store data in a JSON file
239  virtual bool SetMagCalibrationReport(const MagCalibrationReport& data);
240  virtual bool GetMagCalibrationReport(MagCalibrationReport* data);
241 
242 protected:
243 
244  virtual void openDevice();
245  void closeDeviceOnError();
246 
248  bool setRange(const SensorRange& range);
249 
250  Void setReportRate(unsigned rateHz);
251 
252  Void setOnboardCalibrationEnabled(bool enabled);
253 
254  bool setSerialReport(const SerialReport& data);
255  bool getSerialReport(SerialReport* data);
256 
257  // Called for decoded messages
258  void onTrackerMessage(TrackerMessage* message);
259  bool decodeTrackerMessage(TrackerMessage* message, UByte* buffer, int size);
260 
261  // Helpers to reduce casting.
262 /*
263  SensorDeviceCreateDesc* getCreateDesc() const
264  { return (SensorDeviceCreateDesc*)pCreateDesc.GetPtr(); }
265 
266  HIDDeviceDesc* getHIDDesc() const
267  { return &getCreateDesc()->HIDDesc; }
268 */
269 
270  // Set if the sensor is located on the HMD.
271  // Older prototype firmware doesn't support changing HW coordinates,
272  // so we track its state.
276 
284 
285  // This tracks wrap around, and should be monotonically increasing.
287 
288  // Current sensor range obtained from device.
291 
292  // IMU calibration obtained from device.
298 
300 
303 
304 #ifdef OVR_OS_ANDROID
305  void replaceWithPhoneMag(Vector3f* val);
306  PhoneSensors* pPhoneSensors;
307 #endif
308 
309 private:
312 };
313 
314 } // namespace OVR
315 
316 #endif // OVR_SensorImpl_h
BootLoaderDeviceCreateDesc(DeviceFactory *factory, const HIDDeviceDesc &hidDesc)
void onTrackerMessage(TrackerMessage *message)
DeviceFactory *const pFactory
virtual DeviceBase * NewDeviceInstance()
bool decodeTrackerMessage(TrackerMessage *message, UByte *buffer, int size)
virtual bool MatchVendorProduct(UInt16 vendorId, UInt16 productId) const
virtual bool Initialize(DeviceBase *parent)
virtual void GetRange(SensorRange *range) const
virtual bool GetDeviceInfo(DeviceInfo *info) const
SensorTimeFilter TimeFilter
bool setSerialReport(const SerialReport &data)
virtual bool GetDeviceInfo(DeviceInfo *info) const
#define NULL
virtual unsigned GetReportRate() const
SensorDeviceImpl(SensorDeviceCreateDesc *createDesc)
virtual bool MatchHIDDevice(const HIDDeviceDesc &hidDesc) const
uint16_t UInt16
Definition: OVR_Types.h:251
virtual void AddMessageHandler(MessageHandler *handler)
virtual bool DetectHIDDevice(DeviceManager *pdevMgr, const HIDDeviceDesc &desc)
bool GetSerialReport(SerialReport *data)
uint32_t UInt32
Definition: OVR_Types.h:253
virtual DeviceBase * NewDeviceInstance()
virtual void SetReportRate(unsigned rateHz)
virtual double OnTicks(double tickSeconds)
#define OVR_UNUSED(a)
static SensorDeviceFactory & GetInstance()
static void EnumerateHMDFromSensorDisplayInfo(const SensorDisplayInfoImpl &displayInfo, DeviceFactory::EnumerateVisitor &visitor)
Void setReportRate(unsigned rateHz)
virtual DeviceCreateDesc * Clone() const
uint8_t UByte
Definition: OVR_Types.h:249
virtual bool SetMagCalibrationReport(const MagCalibrationReport &data)
bool setRange(const SensorRange &range)
virtual void OnInputReport(UByte *pData, UInt32 length)
virtual void SetOnboardCalibrationEnabled(bool enabled)
const DeviceType Type
CoordinateFrame HWCoordinates
DeviceManagerImpl * pManager
virtual void GetFactoryCalibration(Vector3f *AccelOffset, Vector3f *GyroOffset, Matrix4f *AccelMatrix, Matrix4f *GyroMatrix, float *Temperature)
bool getSerialReport(SerialReport *data)
virtual bool IsMagCalibrated()
virtual CoordinateFrame GetCoordinateFrame() const
virtual DeviceCreateDesc * Clone() const
virtual void EnumerateDevices(EnumerateVisitor &visitor)
DeviceManager * getManager() const
virtual void SetCoordinateFrame(CoordinateFrame coordframe)
CoordinateFrame Coordinates
virtual void openDevice()
virtual MatchResult MatchDevice(const DeviceCreateDesc &other, DeviceCreateDesc **) const
Void setCoordinateFrame(CoordinateFrame coordframe)
SensorDeviceCreateDesc(DeviceFactory *factory, const HIDDeviceDesc &hidDesc)
virtual bool SetRange(const SensorRange &range, bool waitFlag)
Void setOnboardCalibrationEnabled(bool enabled)
virtual bool MatchHIDDevice(const HIDDeviceDesc &hidDesc) const
bool SetSerialReport(const SerialReport &data)
static int OVR_STDCALL CompareNoCase(const char *a, const char *b)
Definition: OVR_String.cpp:488
virtual bool GetMagCalibrationReport(MagCalibrationReport *data)
UByte Buffer[PacketSize]
virtual MatchResult MatchDevice(const DeviceCreateDesc &other, DeviceCreateDesc **) const