libvisiontransfer  8.0.0
protocol-sh2-imu-bno080.h
1  /*******************************************************************************
2  * Copyright (c) 2019 Nerian Vision GmbH
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *******************************************************************************/
14 
15 #ifndef VISIONTRANSFER_PROTOCOL_SH2_IMU_BNO080
16 #define VISIONTRANSFER_PROTOCOL_SH2_IMU_BNO080
17 
18 #include <cstdint>
19 
20 namespace visiontransfer {
21 namespace internal {
22 
23 struct SH2Constants {
24  static constexpr uint8_t CHANNEL_COMMAND = 0;
25  static constexpr uint8_t CHANNEL_EXECUTABLE = 1;
26  static constexpr uint8_t CHANNEL_CONTROL = 2;
27  static constexpr uint8_t CHANNEL_REPORTS = 3;
28  static constexpr uint8_t CHANNEL_WAKE_REPORTS= 4;
29  static constexpr uint8_t CHANNEL_GYRO = 5;
30 
31  static constexpr uint8_t REPORT_COMMAND_RESPONSE = 0xf1;
32  static constexpr uint8_t REPORT_COMMAND_REQUEST = 0xf2;
33  static constexpr uint8_t REPORT_FRS_READ_RESPONSE = 0xf3;
34  static constexpr uint8_t REPORT_FRS_READ_REQUEST = 0xf4;
35  static constexpr uint8_t REPORT_FRS_WRITE_RESPONSE = 0xf5;
36  static constexpr uint8_t REPORT_FRS_WRITE_DATA = 0xf6;
37  static constexpr uint8_t REPORT_FRS_WRITE_REQUEST = 0xf7;
38  static constexpr uint8_t REPORT_PRODUCT_ID_RESPONSE = 0xf8;
39  static constexpr uint8_t REPORT_PRODUCT_ID_REQUEST = 0xf9;
40  static constexpr uint8_t REPORT_TIMESTAMP_REBASE = 0xfa;
41  static constexpr uint8_t REPORT_BASE_TIMESTAMP_REFERENCE = 0xfb;
42  static constexpr uint8_t REPORT_GET_FEATURE_RESPONSE = 0xfc;
43  static constexpr uint8_t REPORT_SET_FEATURE_COMMAND = 0xfd;
44  static constexpr uint8_t REPORT_GET_FEATURE_REQUEST = 0xfe;
45 
46  // Commands supported by report 0xf2 / 0xf1
47  static constexpr uint8_t COMMAND_REPORT_ERRORS = 0x01;
48  static constexpr uint8_t COMMAND_COUNTS = 0x02;
49  static constexpr uint8_t COMMAND_TARE = 0x03;
50  static constexpr uint8_t COMMAND_INITIALIZE = 0x04;
51  static constexpr uint8_t COMMAND_RESERVED_05 = 0x05;
52  static constexpr uint8_t COMMAND_SAVE_DCD = 0x06;
53  static constexpr uint8_t COMMAND_ME_CALIBRATION = 0x07;
54  static constexpr uint8_t COMMAND_RESERVED_08 = 0x08;
55  static constexpr uint8_t COMMAND_PERIODIC_DCD_SAVE = 0x09;
56  static constexpr uint8_t COMMAND_GET_OSCILLATOR_TYPE = 0x0a;
57  static constexpr uint8_t COMMAND_CLEAR_DCD_AND_RESET = 0x0b;
58  static constexpr uint8_t COMMAND_CALIBRATION = 0x0c;
59  static constexpr uint8_t COMMAND_BOOTLOADER = 0x0d;
60  static constexpr uint8_t COMMAND_INTERACTIVE_CALIBRATION = 0x0e;
61 
62  // Subcommands, for certain commands only
63  // DCD / ME / Bootloader not considered yet, here
64  static constexpr uint8_t SUBCOMMAND_COUNTS__GET_COUNTS = 0x00;
65  static constexpr uint8_t SUBCOMMAND_COUNTS__CLEAR_COUNTS = 0x01;
66  static constexpr uint8_t SUBCOMMAND_TARE__TARE_NOW = 0x00;
67  static constexpr uint8_t SUBCOMMAND_TARE__PERSIST_TARE = 0x01;
68  static constexpr uint8_t SUBCOMMAND_TARE__SET_REORIENTATION = 0x02;
69  static constexpr uint8_t SUBCOMMAND_CALIBRATION__START_CALIBRATION = 0x00;
70  static constexpr uint8_t SUBCOMMAND_CALIBRATION__FINISH_CALIBRATION = 0x01;
71 
72  // What to tare (can be ORed)
73  static constexpr uint8_t TARE_AXIS_X = 1;
74  static constexpr uint8_t TARE_AXIS_Y = 2;
75  static constexpr uint8_t TARE_AXIS_Z = 4;
76 
77  // Reference for tare operation
78  static constexpr uint8_t TARE_BASE_ROTATION_VECTOR = 0;
79  static constexpr uint8_t TARE_BASE_GAMING_ROTATION_VECTOR = 1;
80  static constexpr uint8_t TARE_BASE_GEOMAGNETIC_ROTATION_VECTOR = 2;
81  static constexpr uint8_t TARE_BASE_GYRO_INTEGRATED_ROTATION_VECTOR = 3;
82  static constexpr uint8_t TARE_BASE_ARVR_STABILIZED_ROTATION_VECTOR = 4;
83  static constexpr uint8_t TARE_BASE_ARVR_STABILIZED_GAME_ROTATION_VECTOR= 5;
84 
85  // Sensor types (= sensor input report ID)
86  static constexpr uint8_t SENSOR_ACCELEROMETER = 0x01;
87  static constexpr uint8_t SENSOR_GYROSCOPE = 0x02;
88  static constexpr uint8_t SENSOR_MAGNETOMETER = 0x03;
89  static constexpr uint8_t SENSOR_LINEAR_ACCELERATION = 0x04;
90  static constexpr uint8_t SENSOR_ROTATION_VECTOR = 0x05;
91  static constexpr uint8_t SENSOR_GRAVITY = 0x06;
92  static constexpr uint8_t SENSOR_GYROSCOPE_UNCALIBRATED = 0x07;
93  static constexpr uint8_t SENSOR_GAME_ROTATION_VECTOR = 0x08;
94  static constexpr uint8_t SENSOR_GEOMAGNETIC_ROTATION = 0x09;
95  static constexpr uint8_t SENSOR_PRESSURE = 0x0a;
96  static constexpr uint8_t SENSOR_AMBIENT_LIGHT = 0x0b;
97  static constexpr uint8_t SENSOR_HUMIDITY = 0x0c;
98  static constexpr uint8_t SENSOR_PROXIMITY = 0x0d;
99  static constexpr uint8_t SENSOR_TEMPERATURE = 0x0e;
100  static constexpr uint8_t SENSOR_MAGNETOMETER_UNCALIBRATED = 0x0f;
101  static constexpr uint8_t SENSOR_TAP_DETECTOR = 0x10;
102  static constexpr uint8_t SENSOR_STEP_COUNTER = 0x11;
103  static constexpr uint8_t SENSOR_SIGNIFICANT_MOTION = 0x12;
104  static constexpr uint8_t SENSOR_STABILITY_CLASSIFIER = 0x13;
105  static constexpr uint8_t SENSOR_ACCELEROMETER_RAW = 0x14;
106  static constexpr uint8_t SENSOR_GYROSCOPE_RAW = 0x15;
107  static constexpr uint8_t SENSOR_MAGNETOMETER_RAW = 0x16;
108  static constexpr uint8_t SENSOR_STEP_DETECTOR = 0x18;
109  static constexpr uint8_t SENSOR_SHAKE_DETECTOR = 0x19;
110  static constexpr uint8_t SENSOR_FLIP_DETECTOR = 0x1a;
111  static constexpr uint8_t SENSOR_PICKUP_DETECTOR = 0x1b;
112  static constexpr uint8_t SENSOR_STABILITY_DETECTOR = 0x1c;
113  static constexpr uint8_t SENSOR_PERSONAL_ACTIVITY_CLASSIFIER = 0x1e;
114  static constexpr uint8_t SENSOR_SLEEP_DETECTOR = 0x1f;
115  static constexpr uint8_t SENSOR_TILT_DETECTOR = 0x20;
116  static constexpr uint8_t SENSOR_POCKET_DETECTOR = 0x21;
117  static constexpr uint8_t SENSOR_CIRCLE_DETECTOR = 0x22;
118  static constexpr uint8_t SENSOR_HEART_RATE_MONITOR = 0x23;
119  static constexpr uint8_t SENSOR_ARVR_STABILIZED_ROTATION_VECTOR = 0x28;
120  static constexpr uint8_t SENSOR_ARVR_STABILIZED_GAME_ROTATION_VECTOR= 0x29;
121  static constexpr uint8_t SENSOR_GYRO_INTEGRATED_ROTATION_VECTOR = 0x2a;
122  static constexpr uint8_t SENSOR_MOTION_REQUEST = 0x2b;
123 };
124 
125 inline uint64_t sh2GetU64(const unsigned char* d) {
126  return d[0] | (d[1] << 8) | (d[2] << 16) | (d[3] << 24)
127  | (static_cast<uint64_t>(d[4]) << 32) | (static_cast<uint64_t>(d[5]) << 40)
128  | (static_cast<uint64_t>(d[6]) << 48) | (static_cast<uint64_t>(d[7]) << 56);
129 }
130 inline uint32_t sh2GetU32(const unsigned char* d) {
131  return d[0] | (d[1] << 8) | (d[2] << 16) | (d[3] << 24);
132 }
133 inline uint16_t sh2GetU16(const unsigned char* d) {
134  return d[0] | (d[1] << 8);
135 }
136 inline uint8_t sh2GetU8(const unsigned char* d) {
137  return d[0];
138 }
139 inline double sh2ConvertFixedQ16(uint16_t v, unsigned char q) {
140  return (double) ((int16_t) v) / (double)(1 << q);
141 }
142 
143 inline int sh2GetSensorReportLength(unsigned int sensorReportID) {
144  switch(sensorReportID) {
145  case SH2Constants::SENSOR_ACCELEROMETER: return 10; //ID 0x01
146  case SH2Constants::SENSOR_GYROSCOPE: return 10; //ID 0x02
147  case SH2Constants::SENSOR_MAGNETOMETER: return 10; //ID 0x03
148  case SH2Constants::SENSOR_LINEAR_ACCELERATION: return 10; //ID 0x04
149  case SH2Constants::SENSOR_ROTATION_VECTOR: return 14; //ID 0x05
150  case SH2Constants::SENSOR_GRAVITY: return 10; //ID 0x06
151  case SH2Constants::SENSOR_GYROSCOPE_UNCALIBRATED: return 16; //ID 0x07
152  case SH2Constants::SENSOR_GAME_ROTATION_VECTOR: return 12; //ID 0x08
153  case SH2Constants::SENSOR_GEOMAGNETIC_ROTATION: return 14; //ID 0x09
154  case SH2Constants::SENSOR_PRESSURE: return 8; //ID 0x0a
155  case SH2Constants::SENSOR_AMBIENT_LIGHT: return 8; //ID 0x0b
156  case SH2Constants::SENSOR_HUMIDITY: return 6; //ID 0x0c
157  case SH2Constants::SENSOR_PROXIMITY: return 6; //ID 0x0d
158  case SH2Constants::SENSOR_TEMPERATURE: return 6; //ID 0x0e
159  case SH2Constants::SENSOR_MAGNETOMETER_UNCALIBRATED: return 16; //ID 0x0f
160  case SH2Constants::SENSOR_TAP_DETECTOR: return 5; //ID 0x10
161  case SH2Constants::SENSOR_STEP_COUNTER: return 12; //ID 0x11
162  case SH2Constants::SENSOR_SIGNIFICANT_MOTION: return 6; //ID 0x12
163  case SH2Constants::SENSOR_STABILITY_CLASSIFIER: return 6; //ID 0x13
164  case SH2Constants::SENSOR_ACCELEROMETER_RAW: return 16; //ID 0x14
165  case SH2Constants::SENSOR_GYROSCOPE_RAW: return 16; //ID 0x15
166  case SH2Constants::SENSOR_MAGNETOMETER_RAW: return 16; //ID 0x16
167  case SH2Constants::SENSOR_STEP_DETECTOR: return 8; //ID 0x18
168  case SH2Constants::SENSOR_SHAKE_DETECTOR: return 6; //ID 0x19
169  case SH2Constants::SENSOR_FLIP_DETECTOR: return 6; //ID 0x1a
170  case SH2Constants::SENSOR_PICKUP_DETECTOR: return 6; //ID 0x1b
171  case SH2Constants::SENSOR_STABILITY_DETECTOR: return 6; //ID 0x1c
172  case SH2Constants::SENSOR_PERSONAL_ACTIVITY_CLASSIFIER: return 16; //ID 0x1e
173  case SH2Constants::SENSOR_SLEEP_DETECTOR: return 6; //ID 0x1f
174  case SH2Constants::SENSOR_TILT_DETECTOR: return 6; //ID 0x20
175  case SH2Constants::SENSOR_POCKET_DETECTOR: return 6; //ID 0x21
176  case SH2Constants::SENSOR_CIRCLE_DETECTOR: return 6; //ID 0x22
177  case SH2Constants::SENSOR_HEART_RATE_MONITOR: return 6; //ID 0x23
178  case SH2Constants::SENSOR_ARVR_STABILIZED_ROTATION_VECTOR: return 14; //ID 0x28
179  case SH2Constants::SENSOR_ARVR_STABILIZED_GAME_ROTATION_VECTOR: return 12; //ID 0x29
180  case SH2Constants::SENSOR_GYRO_INTEGRATED_ROTATION_VECTOR: return 14; //ID 0x2a
181  case SH2Constants::SENSOR_MOTION_REQUEST: return 6; //ID 0x2b
182  default: return -1;
183  }
184 }
185 
186 // The Q point for fixed-point values, i.e. the base 2 exponent for division
187 // for consistency reasons also 0 for N/A / undefined
188 inline int sh2GetSensorQPoint(unsigned int sensorReportID) {
189  switch(sensorReportID) {
190  case SH2Constants::SENSOR_ACCELEROMETER: return 8; //ID 0x01
191  case SH2Constants::SENSOR_GYROSCOPE: return 9; //ID 0x02
192  case SH2Constants::SENSOR_MAGNETOMETER: return 4; //ID 0x03
193  case SH2Constants::SENSOR_LINEAR_ACCELERATION: return 8; //ID 0x04
194  case SH2Constants::SENSOR_ROTATION_VECTOR: return 14; //ID 0x05 // but 12 for accuracy fields
195  case SH2Constants::SENSOR_GRAVITY: return 8; //ID 0x06
196  case SH2Constants::SENSOR_GYROSCOPE_UNCALIBRATED: return 9; //ID 0x07
197  case SH2Constants::SENSOR_GAME_ROTATION_VECTOR: return 14; //ID 0x08
198  case SH2Constants::SENSOR_GEOMAGNETIC_ROTATION: return 14; //ID 0x09
199  case SH2Constants::SENSOR_PRESSURE: return 20; //ID 0x0a
200  case SH2Constants::SENSOR_AMBIENT_LIGHT: return 8; //ID 0x0b
201  case SH2Constants::SENSOR_HUMIDITY: return 8; //ID 0x0c
202  case SH2Constants::SENSOR_PROXIMITY: return 4; //ID 0x0d
203  case SH2Constants::SENSOR_TEMPERATURE: return 7; //ID 0x0e
204  case SH2Constants::SENSOR_MAGNETOMETER_UNCALIBRATED: return 4; //ID 0x0f
205  case SH2Constants::SENSOR_TAP_DETECTOR: return 0; //ID 0x10
206  case SH2Constants::SENSOR_STEP_COUNTER: return 0; //ID 0x11
207  case SH2Constants::SENSOR_SIGNIFICANT_MOTION: return 0; //ID 0x12
208  case SH2Constants::SENSOR_STABILITY_CLASSIFIER: return 0; //ID 0x13
209  case SH2Constants::SENSOR_ACCELEROMETER_RAW: return 0; //ID 0x14
210  case SH2Constants::SENSOR_GYROSCOPE_RAW: return 0; //ID 0x15
211  case SH2Constants::SENSOR_MAGNETOMETER_RAW: return 0; //ID 0x16
212  case SH2Constants::SENSOR_STEP_DETECTOR: return 0; //ID 0x18
213  case SH2Constants::SENSOR_SHAKE_DETECTOR: return 0; //ID 0x19
214  case SH2Constants::SENSOR_FLIP_DETECTOR: return 0; //ID 0x1a
215  case SH2Constants::SENSOR_PICKUP_DETECTOR: return 0; //ID 0x1b
216  case SH2Constants::SENSOR_STABILITY_DETECTOR: return 0; //ID 0x1c
217  case SH2Constants::SENSOR_PERSONAL_ACTIVITY_CLASSIFIER: return 0; //ID 0x1e
218  case SH2Constants::SENSOR_SLEEP_DETECTOR: return 0; //ID 0x1f
219  case SH2Constants::SENSOR_TILT_DETECTOR: return 0; //ID 0x20
220  case SH2Constants::SENSOR_POCKET_DETECTOR: return 0; //ID 0x21
221  case SH2Constants::SENSOR_CIRCLE_DETECTOR: return 0; //ID 0x22
222  case SH2Constants::SENSOR_HEART_RATE_MONITOR: return 0; //ID 0x23
223  case SH2Constants::SENSOR_ARVR_STABILIZED_ROTATION_VECTOR: return 14; //ID 0x28
224  case SH2Constants::SENSOR_ARVR_STABILIZED_GAME_ROTATION_VECTOR: return 14; //ID 0x29
225  case SH2Constants::SENSOR_GYRO_INTEGRATED_ROTATION_VECTOR: return 14; //ID 0x2a // but 10 for angular velocity
226  case SH2Constants::SENSOR_MOTION_REQUEST: return 0; //ID 0x2b
227  default: return 0;
228  }
229 }
230 
231 inline const char* sh2GetSensorName(unsigned int sensorReportID) {
232  switch(sensorReportID) {
233  case SH2Constants::SENSOR_ACCELEROMETER: return "Accelerometer";
234  case SH2Constants::SENSOR_GYROSCOPE: return "Gyroscope";
235  case SH2Constants::SENSOR_MAGNETOMETER: return "Magnetometer";
236  case SH2Constants::SENSOR_LINEAR_ACCELERATION: return "Linear Acceleration";
237  case SH2Constants::SENSOR_ROTATION_VECTOR: return "Rotation Vector";
238  case SH2Constants::SENSOR_GRAVITY: return "Gravity";
239  case SH2Constants::SENSOR_GYROSCOPE_UNCALIBRATED: return "Gyroscope Uncalibrated";
240  case SH2Constants::SENSOR_GAME_ROTATION_VECTOR: return "Game Rotation Vector";
241  case SH2Constants::SENSOR_GEOMAGNETIC_ROTATION: return "Geomagnetic Rotation";
242  case SH2Constants::SENSOR_PRESSURE: return "Pressure";
243  case SH2Constants::SENSOR_AMBIENT_LIGHT: return "Ambient Light";
244  case SH2Constants::SENSOR_HUMIDITY: return "Humidity";
245  case SH2Constants::SENSOR_PROXIMITY: return "Proximity";
246  case SH2Constants::SENSOR_TEMPERATURE: return "Temperature";
247  case SH2Constants::SENSOR_MAGNETOMETER_UNCALIBRATED: return "Magnetometer Uncalibrated";
248  case SH2Constants::SENSOR_TAP_DETECTOR: return "Tap Detector";
249  case SH2Constants::SENSOR_STEP_COUNTER: return "Step Counter";
250  case SH2Constants::SENSOR_SIGNIFICANT_MOTION: return "Significant Motion";
251  case SH2Constants::SENSOR_STABILITY_CLASSIFIER: return "Stability Classifier";
252  case SH2Constants::SENSOR_ACCELEROMETER_RAW: return "Accelerometer Raw";
253  case SH2Constants::SENSOR_GYROSCOPE_RAW: return "Gyroscope Raw";
254  case SH2Constants::SENSOR_MAGNETOMETER_RAW: return "Magnetometer Raw";
255  case SH2Constants::SENSOR_STEP_DETECTOR: return "Step Detector";
256  case SH2Constants::SENSOR_SHAKE_DETECTOR: return "Shake Detector";
257  case SH2Constants::SENSOR_FLIP_DETECTOR: return "Flip Detector";
258  case SH2Constants::SENSOR_PICKUP_DETECTOR: return "Pickup Detector";
259  case SH2Constants::SENSOR_STABILITY_DETECTOR: return "Stability Detector";
260  case SH2Constants::SENSOR_PERSONAL_ACTIVITY_CLASSIFIER: return "Personal Activity Classifier";
261  case SH2Constants::SENSOR_SLEEP_DETECTOR: return "Sleep Detector";
262  case SH2Constants::SENSOR_TILT_DETECTOR: return "Tilt Detector";
263  case SH2Constants::SENSOR_POCKET_DETECTOR: return "Pocket Detector";
264  case SH2Constants::SENSOR_CIRCLE_DETECTOR: return "Circle Detector";
265  case SH2Constants::SENSOR_HEART_RATE_MONITOR: return "Heart Rate Monitor";
266  case SH2Constants::SENSOR_ARVR_STABILIZED_ROTATION_VECTOR: return "ARVR-Stabilized Rotation Vector";
267  case SH2Constants::SENSOR_ARVR_STABILIZED_GAME_ROTATION_VECTOR: return "ARVR-Stabilized Game Rotation Vector";
268  case SH2Constants::SENSOR_GYRO_INTEGRATED_ROTATION_VECTOR: return "Gyro-Integrated Rotation Vector";
269  case SH2Constants::SENSOR_MOTION_REQUEST: return "Motion Request";
270  default: return "UNKNOWN";
271  }
272 }
273 
274 // Convenience function to return the appropriate unit string, if applicable
275 inline const char* sh2GetSensorUnit(unsigned int sensorReportID) {
276  switch(sensorReportID) {
277  case SH2Constants::SENSOR_ACCELEROMETER: //ID 0x01
278  case SH2Constants::SENSOR_LINEAR_ACCELERATION: //ID 0x04
279  case SH2Constants::SENSOR_GRAVITY: return "m/s²"; //ID 0x06
280  case SH2Constants::SENSOR_GYROSCOPE: //ID 0x02
281  case SH2Constants::SENSOR_GYROSCOPE_UNCALIBRATED: return "rad/s"; //ID 0x07
282  case SH2Constants::SENSOR_MAGNETOMETER: //ID 0x03
283  case SH2Constants::SENSOR_MAGNETOMETER_UNCALIBRATED: return "μT"; //ID 0x0f
284  case SH2Constants::SENSOR_PRESSURE: return "hPa"; //ID 0x0a
285  case SH2Constants::SENSOR_AMBIENT_LIGHT: return "lx"; //ID 0x0b "cd/m²"
286  case SH2Constants::SENSOR_HUMIDITY: return "%"; //ID 0x0c
287  case SH2Constants::SENSOR_PROXIMITY: return "cm"; //ID 0x0d
288  case SH2Constants::SENSOR_TEMPERATURE: return "°C"; //ID 0x0e
289  default: return "";
290  }
291 }
292 
293 inline const char* sh2GetCommandName(unsigned int cmdID) {
294  static const char* cmdNames[] = {"Reserved", "Errors", "Counter", "Tare", "Initialize",
295  "Reserved", "Save DCD", "ME Calibration", "Reserved", "Periodic DCD Save", "Get Oscillator Type",
296  "Clear DCD and Reset", "Calibration", "Bootloader", "Interactive Calibration"};
297  if (cmdID < sizeof(cmdNames)) return cmdNames[cmdID];
298  else return "Unknown";
299 }
300 
301 #pragma pack(push,1) // Packed struct definitions from SH-2, co-opted for transfer
302 
303 // Common prefix for all SH-2 cargos. SHTP headers irrelevant and not represented.
305 private:
306  uint8_t cargoLength[2];
307  uint8_t channel;
308  uint8_t sequenceNumber;
309  uint8_t reportType;
310 public:
311  inline uint16_t getCargoLength() const { return sh2GetU16(cargoLength) & 0x7fff; } // mask out subtransfer bit
312  inline uint8_t getChannel() const { return channel; }
313  inline uint8_t getSequenceNumber() const { return sequenceNumber; }
314  inline uint8_t getReportType() const { return reportType; }
315 };
316 
317 // Our own custom extension for sending the raw interrupt timestamp (report 0xFF, never reported by SH2)
319 private:
320  SH2CargoBase base;
321  uint8_t usecSinceEpoch[8]; // 64-bit microsecond count
322 public:
323  inline uint64_t getUSecSinceEpoch() const { return (uint64_t) sh2GetU64(usecSinceEpoch); }
324 };
325 
326 // A Timestamp Rebase (0xFA), reporting additional sensor delay offset since last Timebase
328 private:
329  SH2CargoBase base;
330  uint8_t rebaseTime[4];
331 public:
332  inline long getRebaseTime() const { return (int32_t) sh2GetU32(rebaseTime); }
333 };
334 
335 // A Time Base report with a batch transfer (0xFB)
336 // It may be followed by any amount of sensor reports, below.
337 // Refer to the base.getCargoLength() value and the known
338 // record sizes for parsing them.
340 private:
341  SH2CargoBase base;
342  uint8_t timeBase_100uSec[4];
343 public:
344  inline long getTimeBase() const { return 100l * sh2GetU32(timeBase_100uSec); }
345 };
346 
347 // Common base prefix for all sensor reports
349 private:
350  uint8_t sensorID;
351  uint8_t sequenceNumber;
352  uint8_t statusAndDelayMSB;
353  uint8_t delayLSB;
354 public:
355  inline unsigned int getStatus() const { return statusAndDelayMSB & 0x03; }
356  inline unsigned int getDelay() const { return ((statusAndDelayMSB & 0xfc) << 6) | delayLSB; }
357 };
358 
359 // 10-byte reports with individual Q scaling for non-raw 3D sensors
361 private:
362  SH2SensorReportBase base;
363  uint8_t xAxis[2];
364  uint8_t yAxis[2];
365  uint8_t zAxis[2];
366 public:
367  inline double getX() const { return sh2ConvertFixedQ16(sh2GetU16(xAxis), 8); } // Accel Q: shift 8 bits
368  inline double getY() const { return sh2ConvertFixedQ16(sh2GetU16(yAxis), 8); }
369  inline double getZ() const { return sh2ConvertFixedQ16(sh2GetU16(zAxis), 8); }
370 };
372 private:
373  SH2SensorReportBase base;
374  uint8_t xAxis[2];
375  uint8_t yAxis[2];
376  uint8_t zAxis[2];
377 public:
378  inline double getX() const { return sh2ConvertFixedQ16(sh2GetU16(xAxis), 4); } // Magn Q: shift 4 bits
379  inline double getY() const { return sh2ConvertFixedQ16(sh2GetU16(yAxis), 4); }
380  inline double getZ() const { return sh2ConvertFixedQ16(sh2GetU16(zAxis), 4); }
381 };
383 private:
384  SH2SensorReportBase base;
385  uint8_t xAxis[2];
386  uint8_t yAxis[2];
387  uint8_t zAxis[2];
388 public:
389  inline double getX() const { return sh2ConvertFixedQ16(sh2GetU16(xAxis), 9); } // Gyro Q: shift 9 bits
390  inline double getY() const { return sh2ConvertFixedQ16(sh2GetU16(yAxis), 9); }
391  inline double getZ() const { return sh2ConvertFixedQ16(sh2GetU16(zAxis), 9); }
392 };
393 
394 // 14-byte orientation (quaternion) data. i,j,k,real are also known as x,y,z,w
396 private:
397  SH2SensorReportBase base;
398  uint8_t quatI[2];
399  uint8_t quatJ[2];
400  uint8_t quatK[2];
401  uint8_t quatReal[2];
402  uint8_t accuracy[2];
403 public:
404  inline double getI() const { return sh2ConvertFixedQ16(sh2GetU16(quatI), 14); } // Quaternion data: shift 14 bits
405  inline double getJ() const { return sh2ConvertFixedQ16(sh2GetU16(quatJ), 14); }
406  inline double getK() const { return sh2ConvertFixedQ16(sh2GetU16(quatK), 14); }
407  inline double getReal() const { return sh2ConvertFixedQ16(sh2GetU16(quatReal), 14); }
408  inline double getAccuracy() const { return sh2ConvertFixedQ16(sh2GetU16(accuracy), 12); } // Accuracy: shift 12
409 };
410 
411 // 6-byte 1D sensor (pressure, ambient light, humidity, proximity, temperature,
412 // 16-byte data for *raw* accelerometer, gyro, magnetometer
414 private:
415  SH2SensorReportBase base;
416  uint8_t xAxisRaw[2];
417  uint8_t yAxisRaw[2];
418  uint8_t zAxisRaw[2];
419  uint8_t temperature_forGyro[2];
420  uint8_t timestamp[4];
421 };
422 
423 #pragma pack(pop) // End of common sensor data / transport packed struct definitions
424 
425 }} // namespaces
426 
427 #endif
428 
Nerian Vision Technologies