libvisiontransfer  8.3.0
protocol-sh2-imu-bno080.h
1 /*******************************************************************************
2  * Copyright (c) 2020 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  // FRS Configuration Response: Status/Error field
125  static constexpr uint8_t FRS_WRITE_RESPONSE_STATUS_WORDS_RECEIVED = 0x00;
126  static constexpr uint8_t FRS_WRITE_RESPONSE_STATUS_UNRECOGNIZED_FRS_TYPE = 0x01;
127  static constexpr uint8_t FRS_WRITE_RESPONSE_STATUS_BUSY = 0x02;
128  static constexpr uint8_t FRS_WRITE_RESPONSE_STATUS_WRITE_COMPLETED = 0x03;
129  static constexpr uint8_t FRS_WRITE_RESPONSE_STATUS_WRITE_MODE_READY = 0x04;
130  static constexpr uint8_t FRS_WRITE_RESPONSE_STATUS_WRITE_FAILED = 0x05;
131  static constexpr uint8_t FRS_WRITE_RESPONSE_STATUS_UNEXPECTED_DATA = 0x06;
132  static constexpr uint8_t FRS_WRITE_RESPONSE_STATUS_INVALID_LENGTH = 0x07;
133  static constexpr uint8_t FRS_WRITE_RESPONSE_STATUS_RECORD_VALID = 0x08;
134  static constexpr uint8_t FRS_WRITE_RESPONSE_STATUS_RECORD_INVALID = 0x09;
135  static constexpr uint8_t FRS_WRITE_RESPONSE_STATUS_DEVICE_ERROR__DEPRECATED = 0x0A;
136  static constexpr uint8_t FRS_WRITE_RESPONSE_STATUS_READ_ONLY_RECORD = 0x0B;
137  static constexpr uint8_t FRS_WRITE_RESPONSE_STATUS_CANNOT_WRITE_MEMORY_FULL = 0x0C;
138 };
139 
140 inline uint64_t sh2GetU64(const unsigned char* d) {
141  return d[0] | (d[1] << 8) | (d[2] << 16) | (d[3] << 24)
142  | (static_cast<uint64_t>(d[4]) << 32) | (static_cast<uint64_t>(d[5]) << 40)
143  | (static_cast<uint64_t>(d[6]) << 48) | (static_cast<uint64_t>(d[7]) << 56);
144 }
145 inline uint32_t sh2GetU32(const unsigned char* d) {
146  return d[0] | (d[1] << 8) | (d[2] << 16) | (d[3] << 24);
147 }
148 inline uint16_t sh2GetU16(const unsigned char* d) {
149  return d[0] | (d[1] << 8);
150 }
151 inline uint8_t sh2GetU8(const unsigned char* d) {
152  return d[0];
153 }
154 inline double sh2ConvertFixedQ16(uint16_t v, unsigned char q) {
155  return (double) ((int16_t) v) / (double)(1 << q);
156 }
157 
158 inline int sh2GetSensorReportLength(unsigned int sensorReportID) {
159  switch(sensorReportID) {
160  case SH2Constants::SENSOR_ACCELEROMETER: return 10; //ID 0x01
161  case SH2Constants::SENSOR_GYROSCOPE: return 10; //ID 0x02
162  case SH2Constants::SENSOR_MAGNETOMETER: return 10; //ID 0x03
163  case SH2Constants::SENSOR_LINEAR_ACCELERATION: return 10; //ID 0x04
164  case SH2Constants::SENSOR_ROTATION_VECTOR: return 14; //ID 0x05
165  case SH2Constants::SENSOR_GRAVITY: return 10; //ID 0x06
166  case SH2Constants::SENSOR_GYROSCOPE_UNCALIBRATED: return 16; //ID 0x07
167  case SH2Constants::SENSOR_GAME_ROTATION_VECTOR: return 12; //ID 0x08
168  case SH2Constants::SENSOR_GEOMAGNETIC_ROTATION: return 14; //ID 0x09
169  case SH2Constants::SENSOR_PRESSURE: return 8; //ID 0x0a
170  case SH2Constants::SENSOR_AMBIENT_LIGHT: return 8; //ID 0x0b
171  case SH2Constants::SENSOR_HUMIDITY: return 6; //ID 0x0c
172  case SH2Constants::SENSOR_PROXIMITY: return 6; //ID 0x0d
173  case SH2Constants::SENSOR_TEMPERATURE: return 6; //ID 0x0e
174  case SH2Constants::SENSOR_MAGNETOMETER_UNCALIBRATED: return 16; //ID 0x0f
175  case SH2Constants::SENSOR_TAP_DETECTOR: return 5; //ID 0x10
176  case SH2Constants::SENSOR_STEP_COUNTER: return 12; //ID 0x11
177  case SH2Constants::SENSOR_SIGNIFICANT_MOTION: return 6; //ID 0x12
178  case SH2Constants::SENSOR_STABILITY_CLASSIFIER: return 6; //ID 0x13
179  case SH2Constants::SENSOR_ACCELEROMETER_RAW: return 16; //ID 0x14
180  case SH2Constants::SENSOR_GYROSCOPE_RAW: return 16; //ID 0x15
181  case SH2Constants::SENSOR_MAGNETOMETER_RAW: return 16; //ID 0x16
182  case SH2Constants::SENSOR_STEP_DETECTOR: return 8; //ID 0x18
183  case SH2Constants::SENSOR_SHAKE_DETECTOR: return 6; //ID 0x19
184  case SH2Constants::SENSOR_FLIP_DETECTOR: return 6; //ID 0x1a
185  case SH2Constants::SENSOR_PICKUP_DETECTOR: return 6; //ID 0x1b
186  case SH2Constants::SENSOR_STABILITY_DETECTOR: return 6; //ID 0x1c
187  case SH2Constants::SENSOR_PERSONAL_ACTIVITY_CLASSIFIER: return 16; //ID 0x1e
188  case SH2Constants::SENSOR_SLEEP_DETECTOR: return 6; //ID 0x1f
189  case SH2Constants::SENSOR_TILT_DETECTOR: return 6; //ID 0x20
190  case SH2Constants::SENSOR_POCKET_DETECTOR: return 6; //ID 0x21
191  case SH2Constants::SENSOR_CIRCLE_DETECTOR: return 6; //ID 0x22
192  case SH2Constants::SENSOR_HEART_RATE_MONITOR: return 6; //ID 0x23
193  case SH2Constants::SENSOR_ARVR_STABILIZED_ROTATION_VECTOR: return 14; //ID 0x28
194  case SH2Constants::SENSOR_ARVR_STABILIZED_GAME_ROTATION_VECTOR: return 12; //ID 0x29
195  case SH2Constants::SENSOR_GYRO_INTEGRATED_ROTATION_VECTOR: return 14; //ID 0x2a
196  case SH2Constants::SENSOR_MOTION_REQUEST: return 6; //ID 0x2b
197  default: return -1;
198  }
199 }
200 
201 // The Q point for fixed-point values, i.e. the base 2 exponent for division
202 // for consistency reasons also 0 for N/A / undefined
203 inline int sh2GetSensorQPoint(unsigned int sensorReportID) {
204  switch(sensorReportID) {
205  case SH2Constants::SENSOR_ACCELEROMETER: return 8; //ID 0x01
206  case SH2Constants::SENSOR_GYROSCOPE: return 9; //ID 0x02
207  case SH2Constants::SENSOR_MAGNETOMETER: return 4; //ID 0x03
208  case SH2Constants::SENSOR_LINEAR_ACCELERATION: return 8; //ID 0x04
209  case SH2Constants::SENSOR_ROTATION_VECTOR: return 14; //ID 0x05 // but 12 for accuracy fields
210  case SH2Constants::SENSOR_GRAVITY: return 8; //ID 0x06
211  case SH2Constants::SENSOR_GYROSCOPE_UNCALIBRATED: return 9; //ID 0x07
212  case SH2Constants::SENSOR_GAME_ROTATION_VECTOR: return 14; //ID 0x08
213  case SH2Constants::SENSOR_GEOMAGNETIC_ROTATION: return 14; //ID 0x09
214  case SH2Constants::SENSOR_PRESSURE: return 20; //ID 0x0a
215  case SH2Constants::SENSOR_AMBIENT_LIGHT: return 8; //ID 0x0b
216  case SH2Constants::SENSOR_HUMIDITY: return 8; //ID 0x0c
217  case SH2Constants::SENSOR_PROXIMITY: return 4; //ID 0x0d
218  case SH2Constants::SENSOR_TEMPERATURE: return 7; //ID 0x0e
219  case SH2Constants::SENSOR_MAGNETOMETER_UNCALIBRATED: return 4; //ID 0x0f
220  case SH2Constants::SENSOR_TAP_DETECTOR: return 0; //ID 0x10
221  case SH2Constants::SENSOR_STEP_COUNTER: return 0; //ID 0x11
222  case SH2Constants::SENSOR_SIGNIFICANT_MOTION: return 0; //ID 0x12
223  case SH2Constants::SENSOR_STABILITY_CLASSIFIER: return 0; //ID 0x13
224  case SH2Constants::SENSOR_ACCELEROMETER_RAW: return 0; //ID 0x14
225  case SH2Constants::SENSOR_GYROSCOPE_RAW: return 0; //ID 0x15
226  case SH2Constants::SENSOR_MAGNETOMETER_RAW: return 0; //ID 0x16
227  case SH2Constants::SENSOR_STEP_DETECTOR: return 0; //ID 0x18
228  case SH2Constants::SENSOR_SHAKE_DETECTOR: return 0; //ID 0x19
229  case SH2Constants::SENSOR_FLIP_DETECTOR: return 0; //ID 0x1a
230  case SH2Constants::SENSOR_PICKUP_DETECTOR: return 0; //ID 0x1b
231  case SH2Constants::SENSOR_STABILITY_DETECTOR: return 0; //ID 0x1c
232  case SH2Constants::SENSOR_PERSONAL_ACTIVITY_CLASSIFIER: return 0; //ID 0x1e
233  case SH2Constants::SENSOR_SLEEP_DETECTOR: return 0; //ID 0x1f
234  case SH2Constants::SENSOR_TILT_DETECTOR: return 0; //ID 0x20
235  case SH2Constants::SENSOR_POCKET_DETECTOR: return 0; //ID 0x21
236  case SH2Constants::SENSOR_CIRCLE_DETECTOR: return 0; //ID 0x22
237  case SH2Constants::SENSOR_HEART_RATE_MONITOR: return 0; //ID 0x23
238  case SH2Constants::SENSOR_ARVR_STABILIZED_ROTATION_VECTOR: return 14; //ID 0x28
239  case SH2Constants::SENSOR_ARVR_STABILIZED_GAME_ROTATION_VECTOR: return 14; //ID 0x29
240  case SH2Constants::SENSOR_GYRO_INTEGRATED_ROTATION_VECTOR: return 14; //ID 0x2a // but 10 for angular velocity
241  case SH2Constants::SENSOR_MOTION_REQUEST: return 0; //ID 0x2b
242  default: return 0;
243  }
244 }
245 
246 inline const char* sh2GetSensorName(unsigned int sensorReportID) {
247  switch(sensorReportID) {
248  case SH2Constants::SENSOR_ACCELEROMETER: return "Accelerometer";
249  case SH2Constants::SENSOR_GYROSCOPE: return "Gyroscope";
250  case SH2Constants::SENSOR_MAGNETOMETER: return "Magnetometer";
251  case SH2Constants::SENSOR_LINEAR_ACCELERATION: return "Linear Acceleration";
252  case SH2Constants::SENSOR_ROTATION_VECTOR: return "Rotation Vector";
253  case SH2Constants::SENSOR_GRAVITY: return "Gravity";
254  case SH2Constants::SENSOR_GYROSCOPE_UNCALIBRATED: return "Gyroscope Uncalibrated";
255  case SH2Constants::SENSOR_GAME_ROTATION_VECTOR: return "Game Rotation Vector";
256  case SH2Constants::SENSOR_GEOMAGNETIC_ROTATION: return "Geomagnetic Rotation";
257  case SH2Constants::SENSOR_PRESSURE: return "Pressure";
258  case SH2Constants::SENSOR_AMBIENT_LIGHT: return "Ambient Light";
259  case SH2Constants::SENSOR_HUMIDITY: return "Humidity";
260  case SH2Constants::SENSOR_PROXIMITY: return "Proximity";
261  case SH2Constants::SENSOR_TEMPERATURE: return "Temperature";
262  case SH2Constants::SENSOR_MAGNETOMETER_UNCALIBRATED: return "Magnetometer Uncalibrated";
263  case SH2Constants::SENSOR_TAP_DETECTOR: return "Tap Detector";
264  case SH2Constants::SENSOR_STEP_COUNTER: return "Step Counter";
265  case SH2Constants::SENSOR_SIGNIFICANT_MOTION: return "Significant Motion";
266  case SH2Constants::SENSOR_STABILITY_CLASSIFIER: return "Stability Classifier";
267  case SH2Constants::SENSOR_ACCELEROMETER_RAW: return "Accelerometer Raw";
268  case SH2Constants::SENSOR_GYROSCOPE_RAW: return "Gyroscope Raw";
269  case SH2Constants::SENSOR_MAGNETOMETER_RAW: return "Magnetometer Raw";
270  case SH2Constants::SENSOR_STEP_DETECTOR: return "Step Detector";
271  case SH2Constants::SENSOR_SHAKE_DETECTOR: return "Shake Detector";
272  case SH2Constants::SENSOR_FLIP_DETECTOR: return "Flip Detector";
273  case SH2Constants::SENSOR_PICKUP_DETECTOR: return "Pickup Detector";
274  case SH2Constants::SENSOR_STABILITY_DETECTOR: return "Stability Detector";
275  case SH2Constants::SENSOR_PERSONAL_ACTIVITY_CLASSIFIER: return "Personal Activity Classifier";
276  case SH2Constants::SENSOR_SLEEP_DETECTOR: return "Sleep Detector";
277  case SH2Constants::SENSOR_TILT_DETECTOR: return "Tilt Detector";
278  case SH2Constants::SENSOR_POCKET_DETECTOR: return "Pocket Detector";
279  case SH2Constants::SENSOR_CIRCLE_DETECTOR: return "Circle Detector";
280  case SH2Constants::SENSOR_HEART_RATE_MONITOR: return "Heart Rate Monitor";
281  case SH2Constants::SENSOR_ARVR_STABILIZED_ROTATION_VECTOR: return "ARVR-Stabilized Rotation Vector";
282  case SH2Constants::SENSOR_ARVR_STABILIZED_GAME_ROTATION_VECTOR: return "ARVR-Stabilized Game Rotation Vector";
283  case SH2Constants::SENSOR_GYRO_INTEGRATED_ROTATION_VECTOR: return "Gyro-Integrated Rotation Vector";
284  case SH2Constants::SENSOR_MOTION_REQUEST: return "Motion Request";
285  default: return "UNKNOWN";
286  }
287 }
288 
289 // Convenience function to return the appropriate unit string, if applicable
290 inline const char* sh2GetSensorUnit(unsigned int sensorReportID) {
291  switch(sensorReportID) {
292  case SH2Constants::SENSOR_ACCELEROMETER: //ID 0x01
293  case SH2Constants::SENSOR_LINEAR_ACCELERATION: //ID 0x04
294  case SH2Constants::SENSOR_GRAVITY: return "m/s²"; //ID 0x06
295  case SH2Constants::SENSOR_GYROSCOPE: //ID 0x02
296  case SH2Constants::SENSOR_GYROSCOPE_UNCALIBRATED: return "rad/s"; //ID 0x07
297  case SH2Constants::SENSOR_MAGNETOMETER: //ID 0x03
298  case SH2Constants::SENSOR_MAGNETOMETER_UNCALIBRATED: return "μT"; //ID 0x0f
299  case SH2Constants::SENSOR_PRESSURE: return "hPa"; //ID 0x0a
300  case SH2Constants::SENSOR_AMBIENT_LIGHT: return "lx"; //ID 0x0b "cd/m²"
301  case SH2Constants::SENSOR_HUMIDITY: return "%"; //ID 0x0c
302  case SH2Constants::SENSOR_PROXIMITY: return "cm"; //ID 0x0d
303  case SH2Constants::SENSOR_TEMPERATURE: return "°C"; //ID 0x0e
304  default: return "";
305  }
306 }
307 
308 inline const char* sh2GetCommandName(unsigned int cmdID) {
309  static const char* cmdNames[] = {"Reserved", "Errors", "Counter", "Tare", "Initialize",
310  "Reserved", "Save DCD", "ME Calibration", "Reserved", "Periodic DCD Save", "Get Oscillator Type",
311  "Clear DCD and Reset", "Calibration", "Bootloader", "Interactive Calibration"};
312  if (cmdID < sizeof(cmdNames)) return cmdNames[cmdID];
313  else return "Unknown";
314 }
315 
316 #pragma pack(push,1) // Packed struct definitions from SH-2, co-opted for transfer
317 
318 // Common prefix for all SH-2 cargos. SHTP headers irrelevant and not represented.
320 private:
321  uint8_t cargoLength[2];
322  uint8_t channel;
323  uint8_t sequenceNumber;
324  uint8_t reportType;
325 public:
326  inline uint16_t getCargoLength() const { return sh2GetU16(cargoLength) & 0x7fff; } // mask out subtransfer bit
327  inline uint8_t getChannel() const { return channel; }
328  inline uint8_t getSequenceNumber() const { return sequenceNumber; }
329  inline uint8_t getReportType() const { return reportType; }
330 };
331 
332 // Our own custom extension for sending the raw interrupt timestamp (report 0xFF, never reported by SH2)
334 private:
335  SH2CargoBase base;
336  uint8_t usecSinceEpoch[8]; // 64-bit microsecond count
337 public:
338  inline uint64_t getUSecSinceEpoch() const { return (uint64_t) sh2GetU64(usecSinceEpoch); }
339 };
340 
341 // A Timestamp Rebase (0xFA), reporting additional sensor delay offset since last Timebase
343 private:
344  SH2CargoBase base;
345  uint8_t rebaseTime[4];
346 public:
347  inline long getRebaseTime() const { return (int32_t) sh2GetU32(rebaseTime); }
348 };
349 
350 // A Time Base report with a batch transfer (0xFB)
351 // It may be followed by any amount of sensor reports, below.
352 // Refer to the base.getCargoLength() value and the known
353 // record sizes for parsing them.
355 private:
356  SH2CargoBase base;
357  uint8_t timeBase_100uSec[4];
358 public:
359  inline long getTimeBase() const { return 100l * sh2GetU32(timeBase_100uSec); }
360 };
361 
362 // Common base prefix for all sensor reports
364 private:
365  uint8_t sensorID;
366  uint8_t sequenceNumber;
367  uint8_t statusAndDelayMSB;
368  uint8_t delayLSB;
369 public:
370  inline unsigned int getStatus() const { return statusAndDelayMSB & 0x03; }
371  inline unsigned int getDelay() const { return ((statusAndDelayMSB & 0xfc) << 6) | delayLSB; }
372 };
373 
374 // 10-byte reports with individual Q scaling for non-raw 3D sensors
376 private:
377  SH2SensorReportBase base;
378  uint8_t xAxis[2];
379  uint8_t yAxis[2];
380  uint8_t zAxis[2];
381 public:
382  inline double getX() const { return sh2ConvertFixedQ16(sh2GetU16(xAxis), 8); } // Accel Q: shift 8 bits
383  inline double getY() const { return sh2ConvertFixedQ16(sh2GetU16(yAxis), 8); }
384  inline double getZ() const { return sh2ConvertFixedQ16(sh2GetU16(zAxis), 8); }
385 };
387 private:
388  SH2SensorReportBase base;
389  uint8_t xAxis[2];
390  uint8_t yAxis[2];
391  uint8_t zAxis[2];
392 public:
393  inline double getX() const { return sh2ConvertFixedQ16(sh2GetU16(xAxis), 4); } // Magn Q: shift 4 bits
394  inline double getY() const { return sh2ConvertFixedQ16(sh2GetU16(yAxis), 4); }
395  inline double getZ() const { return sh2ConvertFixedQ16(sh2GetU16(zAxis), 4); }
396 };
398 private:
399  SH2SensorReportBase base;
400  uint8_t xAxis[2];
401  uint8_t yAxis[2];
402  uint8_t zAxis[2];
403 public:
404  inline double getX() const { return sh2ConvertFixedQ16(sh2GetU16(xAxis), 9); } // Gyro Q: shift 9 bits
405  inline double getY() const { return sh2ConvertFixedQ16(sh2GetU16(yAxis), 9); }
406  inline double getZ() const { return sh2ConvertFixedQ16(sh2GetU16(zAxis), 9); }
407 };
408 
409 // 14-byte orientation (quaternion) data. i,j,k,real are also known as x,y,z,w
411 private:
412  SH2SensorReportBase base;
413  uint8_t quatI[2];
414  uint8_t quatJ[2];
415  uint8_t quatK[2];
416  uint8_t quatReal[2];
417  uint8_t accuracy[2];
418 public:
419  inline double getI() const { return sh2ConvertFixedQ16(sh2GetU16(quatI), 14); } // Quaternion data: shift 14 bits
420  inline double getJ() const { return sh2ConvertFixedQ16(sh2GetU16(quatJ), 14); }
421  inline double getK() const { return sh2ConvertFixedQ16(sh2GetU16(quatK), 14); }
422  inline double getReal() const { return sh2ConvertFixedQ16(sh2GetU16(quatReal), 14); }
423  inline double getAccuracy() const { return sh2ConvertFixedQ16(sh2GetU16(accuracy), 12); } // Accuracy: shift 12
424 };
425 
426 // 6-byte 1D sensor (pressure, ambient light, humidity, proximity, temperature,
427 // 16-byte data for *raw* accelerometer, gyro, magnetometer
429 private:
430  SH2SensorReportBase base;
431  uint8_t xAxisRaw[2];
432  uint8_t yAxisRaw[2];
433  uint8_t zAxisRaw[2];
434  uint8_t temperature_forGyro[2];
435  uint8_t timestamp[4];
436 };
437 
438 #pragma pack(pop) // End of common sensor data / transport packed struct definitions
439 
440 }} // namespaces
441 
442 #endif
443 
Nerian Vision Technologies