Bike-X  0.8
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
OVR_SensorImpl_Common.cpp
Go to the documentation of this file.
1 /************************************************************************************
2 
3 Filename : OVR_SensorImpl_Common.cpp
4 Content : Source common to SensorImpl and Sensor2Impl.
5 Created : January 21, 2014
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 #include "OVR_SensorImpl_Common.h"
28 #include "Kernel/OVR_Alg.h"
29 
30 namespace OVR
31 {
32 
33 void UnpackSensor(const UByte* buffer, SInt32* x, SInt32* y, SInt32* z)
34 {
35  // Sign extending trick
36  // from http://graphics.stanford.edu/~seander/bithacks.html#FixedSignExtend
37  struct {SInt32 x:21;} s;
38 
39  *x = s.x = (buffer[0] << 13) | (buffer[1] << 5) | ((buffer[2] & 0xF8) >> 3);
40  *y = s.x = ((buffer[2] & 0x07) << 18) | (buffer[3] << 10) | (buffer[4] << 2) |
41  ((buffer[5] & 0xC0) >> 6);
42  *z = s.x = ((buffer[5] & 0x3F) << 15) | (buffer[6] << 7) | (buffer[7] >> 1);
43 }
44 
45 void PackSensor(UByte* buffer, SInt32 x, SInt32 y, SInt32 z)
46 {
47  // Pack 3 32 bit integers into 8 bytes
48  buffer[0] = UByte(x >> 13);
49  buffer[1] = UByte(x >> 5);
50  buffer[2] = UByte((x << 3) | ((y >> 18) & 0x07));
51  buffer[3] = UByte(y >> 10);
52  buffer[4] = UByte(y >> 2);
53  buffer[5] = UByte((y << 6) | ((z >> 15) & 0x3F));
54  buffer[6] = UByte(z >> 7);
55  buffer[7] = UByte(z << 1);
56 }
57 
58 UInt16 SelectSensorRampValue(const UInt16* ramp, unsigned count,
59  float val, float factor, const char* label)
60 {
61  UInt16 threshold = (UInt16)(val * factor);
62 
63  for (unsigned i = 0; i<count; i++)
64  {
65  if (ramp[i] >= threshold)
66  return ramp[i];
67  }
68  OVR_DEBUG_LOG(("SensorDevice::SetRange - %s clamped to %0.4f",
69  label, float(ramp[count-1]) / factor));
70  OVR_UNUSED2(factor, label);
71  return ramp[count-1];
72 }
73 
75 {
76  SetSensorRange(r, commandId);
77 }
78 
80 {
81  CommandId = commandId;
83  r.MaxAcceleration, (1.0f / 9.81f), "MaxAcceleration");
85  r.MaxRotationRate, Math<float>::RadToDegreeFactor, "MaxRotationRate");
87  r.MaxMagneticField, 1000.0f, "MaxMagneticField");
88  Pack();
89 }
90 
92 {
93  r->MaxAcceleration = AccelScale * 9.81f;
95  r->MaxMagneticField= MagScale * 0.001f;
96 }
97 
99 {
100  return SensorRange(AccelRangeRamp[sizeof(AccelRangeRamp)/sizeof(AccelRangeRamp[0]) - 1] * 9.81f,
101  GyroRangeRamp[sizeof(GyroRangeRamp)/sizeof(GyroRangeRamp[0]) - 1] *
103  MagRangeRamp[sizeof(MagRangeRamp)/sizeof(MagRangeRamp[0]) - 1] * 0.001f);
104 }
105 
107 {
108  Buffer[0] = 4;
109  Buffer[1] = UByte(CommandId & 0xFF);
110  Buffer[2] = UByte(CommandId >> 8);
111  Buffer[3] = UByte(AccelScale);
112  Buffer[4] = UByte(GyroScale & 0xFF);
113  Buffer[5] = UByte(GyroScale >> 8);
114  Buffer[6] = UByte(MagScale & 0xFF);
115  Buffer[7] = UByte(MagScale >> 8);
116 }
117 
119 {
120  CommandId = Buffer[1] | (UInt16(Buffer[2]) << 8);
121  AccelScale= Buffer[3];
122  GyroScale = Buffer[4] | (UInt16(Buffer[5]) << 8);
123  MagScale = Buffer[6] | (UInt16(Buffer[7]) << 8);
124 }
125 
127  : CommandId(0), Flags(0), PacketInterval(0), SampleRate(0)
128 {
129  memset(Buffer, 0, PacketSize);
130  Buffer[0] = 2;
131 }
132 
133 void SensorConfigImpl::SetSensorCoordinates(bool sensorCoordinates)
134 {
135  Flags = (Flags & ~Flag_SensorCoordinates) | (sensorCoordinates ? Flag_SensorCoordinates : 0);
136 }
137 
139 {
140  return (Flags & Flag_SensorCoordinates) != 0;
141 }
142 
144 {
145  Buffer[0] = 2;
146  Buffer[1] = UByte(CommandId & 0xFF);
147  Buffer[2] = UByte(CommandId >> 8);
148  Buffer[3] = Flags;
150  Buffer[5] = UByte(SampleRate & 0xFF);
151  Buffer[6] = UByte(SampleRate >> 8);
152 }
153 
155 {
156  CommandId = Buffer[1] | (UInt16(Buffer[2]) << 8);
157  Flags = Buffer[3];
158  PacketInterval = Buffer[4];
159  SampleRate = Buffer[5] | (UInt16(Buffer[6]) << 8);
160 }
161 
163  : AccelOffset(), GyroOffset(), AccelMatrix(), GyroMatrix(), Temperature(0)
164 {
165  memset(Buffer, 0, PacketSize);
166  Buffer[0] = 3;
167 }
168 
170 {
171  SInt32 x, y, z;
172 
173  Buffer[0] = 3;
174 
175  x = SInt32(AccelOffset.x * 1e4f);
176  y = SInt32(AccelOffset.y * 1e4f);
177  z = SInt32(AccelOffset.z * 1e4f);
178  PackSensor(Buffer + 3, x, y, z);
179 
180  x = SInt32(GyroOffset.x * 1e4f);
181  y = SInt32(GyroOffset.y * 1e4f);
182  z = SInt32(GyroOffset.z * 1e4f);
183  PackSensor(Buffer + 11, x, y, z);
184 
185  // ignore the scale matrices for now
186 }
187 
189 {
190  static const float sensorMax = (1 << 20) - 1;
191  SInt32 x, y, z;
192 
193  UnpackSensor(Buffer + 3, &x, &y, &z);
194  AccelOffset.y = (float) y * 1e-4f;
195  AccelOffset.z = (float) z * 1e-4f;
196  AccelOffset.x = (float) x * 1e-4f;
197 
198  UnpackSensor(Buffer + 11, &x, &y, &z);
199  GyroOffset.x = (float) x * 1e-4f;
200  GyroOffset.y = (float) y * 1e-4f;
201  GyroOffset.z = (float) z * 1e-4f;
202 
203  for (int i = 0; i < 3; i++)
204  {
205  UnpackSensor(Buffer + 19 + 8 * i, &x, &y, &z);
206  AccelMatrix.M[i][0] = (float) x / sensorMax;
207  AccelMatrix.M[i][1] = (float) y / sensorMax;
208  AccelMatrix.M[i][2] = (float) z / sensorMax;
209  AccelMatrix.M[i][i] += 1.0f;
210  }
211 
212  for (int i = 0; i < 3; i++)
213  {
214  UnpackSensor(Buffer + 43 + 8 * i, &x, &y, &z);
215  GyroMatrix.M[i][0] = (float) x / sensorMax;
216  GyroMatrix.M[i][1] = (float) y / sensorMax;
217  GyroMatrix.M[i][2] = (float) z / sensorMax;
218  GyroMatrix.M[i][i] += 1.0f;
219  }
220 
221  Temperature = (float) Alg::DecodeSInt16(Buffer + 67) / 100.0f;
222 }
223 
225  : CommandId(commandId), KeepAliveIntervalMs(interval)
226 {
227  Pack();
228 }
229 
231 {
232  Buffer[0] = 8;
233  Buffer[1] = UByte(CommandId & 0xFF);
234  Buffer[2] = UByte(CommandId >> 8);
235  Buffer[3] = UByte(KeepAliveIntervalMs & 0xFF);
236  Buffer[4] = UByte(KeepAliveIntervalMs >> 8);
237 }
238 
240 {
241  CommandId = Buffer[1] | (UInt16(Buffer[2]) << 8);
242  KeepAliveIntervalMs= Buffer[3] | (UInt16(Buffer[4]) << 8);
243 }
244 
245 } // namespace OVR
void SetSensorRange(const SensorRange &r, UInt16 commandId=0)
float MaxRotationRate
Definition: OVR_Device.h:469
uint16_t UInt16
Definition: OVR_Types.h:251
float MaxMagneticField
Definition: OVR_Device.h:472
const UInt16 GyroRangeRamp[]
SensorRangeImpl(const SensorRange &r, UInt16 commandId=0)
T DegreeToRad(T rads)
Definition: OVR_Math.h:228
uint8_t UByte
Definition: OVR_Types.h:249
void GetSensorRange(SensorRange *r)
static SensorRange GetMaxSensorRange()
void PackSensor(UByte *buffer, SInt32 x, SInt32 y, SInt32 z)
UByte Buffer[PacketSize]
const UInt16 AccelRangeRamp[]
void SetSensorCoordinates(bool sensorCoordinates)
T M[4][4]
Definition: OVR_Math.h:1197
int32_t SInt32
Definition: OVR_Types.h:252
float MaxAcceleration
Definition: OVR_Device.h:466
__BEGIN_NAMESPACE_STD void void __END_NAMESPACE_STD void __BEGIN_NAMESPACE_STD void * memset(void *__s, int __c, size_t __n) __THROW __nonnull((1))
void UnpackSensor(const UByte *buffer, SInt32 *x, SInt32 *y, SInt32 *z)
SensorKeepAliveImpl(UInt16 interval=0, UInt16 commandId=0)
#define OVR_DEBUG_LOG(args)
Definition: OVR_Log.h:196
UInt16 SelectSensorRampValue(const UInt16 *ramp, unsigned count, float val, float factor, const char *label)
SInt16 DecodeSInt16(const UByte *buffer)
Definition: OVR_Alg.h:991
#define OVR_UNUSED2(a1, a2)
const UInt16 MagRangeRamp[]