15 #ifndef VISIONTRANSFER_PROTOCOL_SH2_IMU_BNO080 16 #define VISIONTRANSFER_PROTOCOL_SH2_IMU_BNO080 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;
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;
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;
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;
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;
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;
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;
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);
130 inline uint32_t sh2GetU32(
const unsigned char* d) {
131 return d[0] | (d[1] << 8) | (d[2] << 16) | (d[3] << 24);
133 inline uint16_t sh2GetU16(
const unsigned char* d) {
134 return d[0] | (d[1] << 8);
136 inline uint8_t sh2GetU8(
const unsigned char* d) {
139 inline double sh2ConvertFixedQ16(uint16_t v,
unsigned char q) {
140 return (
double) ((int16_t) v) / (double)(1 << q);
143 inline int sh2GetSensorReportLength(
unsigned int sensorReportID) {
144 switch(sensorReportID) {
145 case SH2Constants::SENSOR_ACCELEROMETER:
return 10;
146 case SH2Constants::SENSOR_GYROSCOPE:
return 10;
147 case SH2Constants::SENSOR_MAGNETOMETER:
return 10;
148 case SH2Constants::SENSOR_LINEAR_ACCELERATION:
return 10;
149 case SH2Constants::SENSOR_ROTATION_VECTOR:
return 14;
150 case SH2Constants::SENSOR_GRAVITY:
return 10;
151 case SH2Constants::SENSOR_GYROSCOPE_UNCALIBRATED:
return 16;
152 case SH2Constants::SENSOR_GAME_ROTATION_VECTOR:
return 12;
153 case SH2Constants::SENSOR_GEOMAGNETIC_ROTATION:
return 14;
154 case SH2Constants::SENSOR_PRESSURE:
return 8;
155 case SH2Constants::SENSOR_AMBIENT_LIGHT:
return 8;
156 case SH2Constants::SENSOR_HUMIDITY:
return 6;
157 case SH2Constants::SENSOR_PROXIMITY:
return 6;
158 case SH2Constants::SENSOR_TEMPERATURE:
return 6;
159 case SH2Constants::SENSOR_MAGNETOMETER_UNCALIBRATED:
return 16;
160 case SH2Constants::SENSOR_TAP_DETECTOR:
return 5;
161 case SH2Constants::SENSOR_STEP_COUNTER:
return 12;
162 case SH2Constants::SENSOR_SIGNIFICANT_MOTION:
return 6;
163 case SH2Constants::SENSOR_STABILITY_CLASSIFIER:
return 6;
164 case SH2Constants::SENSOR_ACCELEROMETER_RAW:
return 16;
165 case SH2Constants::SENSOR_GYROSCOPE_RAW:
return 16;
166 case SH2Constants::SENSOR_MAGNETOMETER_RAW:
return 16;
167 case SH2Constants::SENSOR_STEP_DETECTOR:
return 8;
168 case SH2Constants::SENSOR_SHAKE_DETECTOR:
return 6;
169 case SH2Constants::SENSOR_FLIP_DETECTOR:
return 6;
170 case SH2Constants::SENSOR_PICKUP_DETECTOR:
return 6;
171 case SH2Constants::SENSOR_STABILITY_DETECTOR:
return 6;
172 case SH2Constants::SENSOR_PERSONAL_ACTIVITY_CLASSIFIER:
return 16;
173 case SH2Constants::SENSOR_SLEEP_DETECTOR:
return 6;
174 case SH2Constants::SENSOR_TILT_DETECTOR:
return 6;
175 case SH2Constants::SENSOR_POCKET_DETECTOR:
return 6;
176 case SH2Constants::SENSOR_CIRCLE_DETECTOR:
return 6;
177 case SH2Constants::SENSOR_HEART_RATE_MONITOR:
return 6;
178 case SH2Constants::SENSOR_ARVR_STABILIZED_ROTATION_VECTOR:
return 14;
179 case SH2Constants::SENSOR_ARVR_STABILIZED_GAME_ROTATION_VECTOR:
return 12;
180 case SH2Constants::SENSOR_GYRO_INTEGRATED_ROTATION_VECTOR:
return 14;
181 case SH2Constants::SENSOR_MOTION_REQUEST:
return 6;
188 inline int sh2GetSensorQPoint(
unsigned int sensorReportID) {
189 switch(sensorReportID) {
190 case SH2Constants::SENSOR_ACCELEROMETER:
return 8;
191 case SH2Constants::SENSOR_GYROSCOPE:
return 9;
192 case SH2Constants::SENSOR_MAGNETOMETER:
return 4;
193 case SH2Constants::SENSOR_LINEAR_ACCELERATION:
return 8;
194 case SH2Constants::SENSOR_ROTATION_VECTOR:
return 14;
195 case SH2Constants::SENSOR_GRAVITY:
return 8;
196 case SH2Constants::SENSOR_GYROSCOPE_UNCALIBRATED:
return 9;
197 case SH2Constants::SENSOR_GAME_ROTATION_VECTOR:
return 14;
198 case SH2Constants::SENSOR_GEOMAGNETIC_ROTATION:
return 14;
199 case SH2Constants::SENSOR_PRESSURE:
return 20;
200 case SH2Constants::SENSOR_AMBIENT_LIGHT:
return 8;
201 case SH2Constants::SENSOR_HUMIDITY:
return 8;
202 case SH2Constants::SENSOR_PROXIMITY:
return 4;
203 case SH2Constants::SENSOR_TEMPERATURE:
return 7;
204 case SH2Constants::SENSOR_MAGNETOMETER_UNCALIBRATED:
return 4;
205 case SH2Constants::SENSOR_TAP_DETECTOR:
return 0;
206 case SH2Constants::SENSOR_STEP_COUNTER:
return 0;
207 case SH2Constants::SENSOR_SIGNIFICANT_MOTION:
return 0;
208 case SH2Constants::SENSOR_STABILITY_CLASSIFIER:
return 0;
209 case SH2Constants::SENSOR_ACCELEROMETER_RAW:
return 0;
210 case SH2Constants::SENSOR_GYROSCOPE_RAW:
return 0;
211 case SH2Constants::SENSOR_MAGNETOMETER_RAW:
return 0;
212 case SH2Constants::SENSOR_STEP_DETECTOR:
return 0;
213 case SH2Constants::SENSOR_SHAKE_DETECTOR:
return 0;
214 case SH2Constants::SENSOR_FLIP_DETECTOR:
return 0;
215 case SH2Constants::SENSOR_PICKUP_DETECTOR:
return 0;
216 case SH2Constants::SENSOR_STABILITY_DETECTOR:
return 0;
217 case SH2Constants::SENSOR_PERSONAL_ACTIVITY_CLASSIFIER:
return 0;
218 case SH2Constants::SENSOR_SLEEP_DETECTOR:
return 0;
219 case SH2Constants::SENSOR_TILT_DETECTOR:
return 0;
220 case SH2Constants::SENSOR_POCKET_DETECTOR:
return 0;
221 case SH2Constants::SENSOR_CIRCLE_DETECTOR:
return 0;
222 case SH2Constants::SENSOR_HEART_RATE_MONITOR:
return 0;
223 case SH2Constants::SENSOR_ARVR_STABILIZED_ROTATION_VECTOR:
return 14;
224 case SH2Constants::SENSOR_ARVR_STABILIZED_GAME_ROTATION_VECTOR:
return 14;
225 case SH2Constants::SENSOR_GYRO_INTEGRATED_ROTATION_VECTOR:
return 14;
226 case SH2Constants::SENSOR_MOTION_REQUEST:
return 0;
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";
275 inline const char* sh2GetSensorUnit(
unsigned int sensorReportID) {
276 switch(sensorReportID) {
277 case SH2Constants::SENSOR_ACCELEROMETER:
278 case SH2Constants::SENSOR_LINEAR_ACCELERATION:
279 case SH2Constants::SENSOR_GRAVITY:
return "m/s²";
280 case SH2Constants::SENSOR_GYROSCOPE:
281 case SH2Constants::SENSOR_GYROSCOPE_UNCALIBRATED:
return "rad/s";
282 case SH2Constants::SENSOR_MAGNETOMETER:
283 case SH2Constants::SENSOR_MAGNETOMETER_UNCALIBRATED:
return "μT";
284 case SH2Constants::SENSOR_PRESSURE:
return "hPa";
285 case SH2Constants::SENSOR_AMBIENT_LIGHT:
return "lx";
286 case SH2Constants::SENSOR_HUMIDITY:
return "%";
287 case SH2Constants::SENSOR_PROXIMITY:
return "cm";
288 case SH2Constants::SENSOR_TEMPERATURE:
return "°C";
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";
301 #pragma pack(push,1) // Packed struct definitions from SH-2, co-opted for transfer 306 uint8_t cargoLength[2];
308 uint8_t sequenceNumber;
311 inline uint16_t getCargoLength()
const {
return sh2GetU16(cargoLength) & 0x7fff; }
312 inline uint8_t getChannel()
const {
return channel; }
313 inline uint8_t getSequenceNumber()
const {
return sequenceNumber; }
314 inline uint8_t getReportType()
const {
return reportType; }
321 uint8_t usecSinceEpoch[8];
323 inline uint64_t getUSecSinceEpoch()
const {
return (uint64_t) sh2GetU64(usecSinceEpoch); }
330 uint8_t rebaseTime[4];
332 inline long getRebaseTime()
const {
return (int32_t) sh2GetU32(rebaseTime); }
342 uint8_t timeBase_100uSec[4];
344 inline long getTimeBase()
const {
return 100l * sh2GetU32(timeBase_100uSec); }
351 uint8_t sequenceNumber;
352 uint8_t statusAndDelayMSB;
355 inline unsigned int getStatus()
const {
return statusAndDelayMSB & 0x03; }
356 inline unsigned int getDelay()
const {
return ((statusAndDelayMSB & 0xfc) << 6) | delayLSB; }
367 inline double getX()
const {
return sh2ConvertFixedQ16(sh2GetU16(xAxis), 8); }
368 inline double getY()
const {
return sh2ConvertFixedQ16(sh2GetU16(yAxis), 8); }
369 inline double getZ()
const {
return sh2ConvertFixedQ16(sh2GetU16(zAxis), 8); }
378 inline double getX()
const {
return sh2ConvertFixedQ16(sh2GetU16(xAxis), 4); }
379 inline double getY()
const {
return sh2ConvertFixedQ16(sh2GetU16(yAxis), 4); }
380 inline double getZ()
const {
return sh2ConvertFixedQ16(sh2GetU16(zAxis), 4); }
389 inline double getX()
const {
return sh2ConvertFixedQ16(sh2GetU16(xAxis), 9); }
390 inline double getY()
const {
return sh2ConvertFixedQ16(sh2GetU16(yAxis), 9); }
391 inline double getZ()
const {
return sh2ConvertFixedQ16(sh2GetU16(zAxis), 9); }
404 inline double getI()
const {
return sh2ConvertFixedQ16(sh2GetU16(quatI), 14); }
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); }
419 uint8_t temperature_forGyro[2];
420 uint8_t timestamp[4];
423 #pragma pack(pop) // End of common sensor data / transport packed struct definitions