BNO08X Driver 0.1.0
API reference
Loading...
Searching...
No Matches
bno08x_records.hpp
Go to the documentation of this file.
1
13#ifndef BNO08X_RECORDS_HPP
14#define BNO08X_RECORDS_HPP
15
16#include <array>
17#include <cstddef>
18#include <cstdint>
19
20#include "bno08x_detail.hpp"
21#include "bno08x_registers.hpp"
22#include "bno08x_types.hpp"
23
24namespace bno08x {
25
27struct Quat30 {
28 int32_t x{0};
29 int32_t y{0};
30 int32_t z{0};
31 int32_t w{0};
33 friend constexpr bool operator==(const Quat30& lhs, const Quat30& rhs) noexcept = default;
34};
35
40 uint8_t stable_duration_s{0};
41 uint8_t delta_accel_mg{0};
43 friend constexpr bool operator==(const PowerManagement& lhs,
44 const PowerManagement& rhs) noexcept = default;
45};
46
49 int32_t scaling_q30{0};
50 int32_t max_rotation_q29{0};
51 int32_t max_error_q29{0};
52 int32_t stability_q29{0};
54 friend constexpr bool operator==(const ArvrStabilization& lhs,
55 const ArvrStabilization& rhs) noexcept = default;
56};
57
61 uint32_t step_threshold{0};
63 friend constexpr bool operator==(const SignificantMotionConfig& lhs,
64 const SignificantMotionConfig& rhs) noexcept = default;
65};
66
69 uint32_t min_time_us{0};
70 uint32_t max_time_us{0};
71 int32_t threshold_q26{0};
72 uint32_t count{0};
73 bool enable_x{false};
74 bool enable_y{false};
75 bool enable_z{false};
77 friend constexpr bool operator==(const ShakeConfig& lhs,
78 const ShakeConfig& rhs) noexcept = default;
79};
80
83 int32_t offset{0};
85 int32_t scale_q15{0};
87 friend constexpr bool operator==(const EnvCalibration& lhs,
88 const EnvCalibration& rhs) noexcept = default;
89};
90
94 0x0207};
95 uint32_t sync_interval_us{10000};
96 int32_t max_error_q29{0};
97 int32_t prediction_q10{0};
98 int32_t alpha_q20{0};
99 int32_t beta_q20{0};
100 int32_t gamma_q20{0};
102 friend constexpr bool operator==(const GyroRvConfig& lhs,
103 const GyroRvConfig& rhs) noexcept = default;
104};
105
107inline constexpr std::size_t kOrientationWords = 4;
108inline constexpr std::size_t kPowerMgmtWords = 3;
109inline constexpr std::size_t kArvrWords = 4;
110inline constexpr std::size_t kSigMotionWords = 2;
111inline constexpr std::size_t kShakeWords = 5;
112inline constexpr std::size_t kEnvCalWords = 2;
113inline constexpr std::size_t kGyroRvConfigWords = 7;
114
115namespace detail {
117[[nodiscard]] constexpr uint32_t W(int32_t v) noexcept {
118 return static_cast<uint32_t>(v);
119}
121[[nodiscard]] constexpr int32_t I(uint32_t w) noexcept {
122 return static_cast<int32_t>(static_cast<int64_t>(w ^ 0x8000'0000U) - 0x8000'0000LL);
123}
124} // namespace detail
125
128[[nodiscard]] constexpr std::array<uint32_t, kOrientationWords> EncodeOrientation(
129 const Quat30& q) noexcept {
130 return {detail::W(q.x), detail::W(q.y), detail::W(q.z), detail::W(q.w)};
131}
133[[nodiscard]] constexpr Quat30 DecodeOrientation(
134 const std::array<uint32_t, kOrientationWords>& w) noexcept {
135 return Quat30{
136 .x = detail::I(w[0]), .y = detail::I(w[1]), .z = detail::I(w[2]), .w = detail::I(w[3])};
137}
139[[nodiscard]] constexpr std::array<uint32_t, kPowerMgmtWords> EncodePowerManagement(
140 const PowerManagement& p) noexcept {
141 const uint32_t packed =
142 static_cast<uint32_t>(p.stable_duration_s) | (static_cast<uint32_t>(p.delta_accel_mg) << 8U);
143 return {detail::W(p.delta_orientation_q28), detail::W(p.stable_threshold_q25), packed};
144}
147 const std::array<uint32_t, kPowerMgmtWords>& w) noexcept {
148 return PowerManagement{.delta_orientation_q28 = detail::I(w[0]),
149 .stable_threshold_q25 = detail::I(w[1]),
150 .stable_duration_s = static_cast<uint8_t>(w[2] & 0xFFU),
151 .delta_accel_mg = static_cast<uint8_t>((w[2] >> 8U) & 0xFFU)};
152}
154[[nodiscard]] constexpr std::array<uint32_t, kArvrWords> EncodeArvr(
155 const ArvrStabilization& a) noexcept {
156 return {detail::W(a.scaling_q30), detail::W(a.max_rotation_q29), detail::W(a.max_error_q29),
157 detail::W(a.stability_q29)};
158}
160[[nodiscard]] constexpr ArvrStabilization DecodeArvr(
161 const std::array<uint32_t, kArvrWords>& w) noexcept {
162 return ArvrStabilization{.scaling_q30 = detail::I(w[0]),
163 .max_rotation_q29 = detail::I(w[1]),
164 .max_error_q29 = detail::I(w[2]),
165 .stability_q29 = detail::I(w[3])};
166}
168[[nodiscard]] constexpr std::array<uint32_t, kSigMotionWords> EncodeSignificantMotion(
169 const SignificantMotionConfig& s) noexcept {
170 return {detail::W(s.accel_threshold_q24), s.step_threshold};
171}
174 const std::array<uint32_t, kSigMotionWords>& w) noexcept {
175 return SignificantMotionConfig{.accel_threshold_q24 = detail::I(w[0]), .step_threshold = w[1]};
176}
179[[nodiscard]] constexpr std::array<uint32_t, kShakeWords> EncodeShake(
180 const ShakeConfig& s) noexcept {
181 const uint32_t flags = (s.enable_x ? 1U : 0U) | (s.enable_y ? 2U : 0U) | (s.enable_z ? 4U : 0U);
182 return {s.min_time_us, s.max_time_us, detail::W(s.threshold_q26), s.count, flags};
183}
185[[nodiscard]] constexpr ShakeConfig DecodeShake(
186 const std::array<uint32_t, kShakeWords>& w) noexcept {
187 return ShakeConfig{.min_time_us = w[0],
188 .max_time_us = w[1],
189 .threshold_q26 = detail::I(w[2]),
190 .count = w[3],
191 .enable_x = (w[4] & 1U) != 0U,
192 .enable_y = (w[4] & 2U) != 0U,
193 .enable_z = (w[4] & 4U) != 0U};
194}
196[[nodiscard]] constexpr std::array<uint32_t, kEnvCalWords> EncodeEnvCalibration(
197 const EnvCalibration& e) noexcept {
198 return {detail::W(e.offset), detail::W(e.scale_q15)};
199}
201[[nodiscard]] constexpr EnvCalibration DecodeEnvCalibration(
202 const std::array<uint32_t, kEnvCalWords>& w) noexcept {
203 return EnvCalibration{.offset = detail::I(w[0]), .scale_q15 = detail::I(w[1])};
204}
206[[nodiscard]] constexpr std::array<uint32_t, kGyroRvConfigWords> EncodeGyroRvConfig(
207 const GyroRvConfig& g) noexcept {
208 return {g.reference_type, g.sync_interval_us,
209 detail::W(g.max_error_q29), detail::W(g.prediction_q10),
210 detail::W(g.alpha_q20), detail::W(g.beta_q20),
211 detail::W(g.gamma_q20)};
212}
214[[nodiscard]] constexpr GyroRvConfig DecodeGyroRvConfig(
215 const std::array<uint32_t, kGyroRvConfigWords>& w) noexcept {
216 return GyroRvConfig{.reference_type = w[0],
217 .sync_interval_us = w[1],
218 .max_error_q29 = detail::I(w[2]),
219 .prediction_q10 = detail::I(w[3]),
220 .alpha_q20 = detail::I(w[4]),
221 .beta_q20 = detail::I(w[5]),
222 .gamma_q20 = detail::I(w[6])};
223}
224
225namespace detail {
226
228constexpr int32_t MountQ30(int8_t code) noexcept {
229 constexpr int32_t kOne = 1'073'741'824;
230 constexpr int32_t kHalf = 536'870'912;
231 constexpr int32_t kRoot = 759'250'125;
232 const int32_t mag = (code < 0) ? -code : code;
233 int32_t v = 0;
234 if (mag == 1) {
235 v = kOne;
236 } else if (mag == 2) {
237 v = kHalf;
238 } else if (mag == 3) {
239 v = kRoot;
240 } else {
241 // zero
242 }
243 return (code < 0) ? -v : v;
244}
245
247inline constexpr std::array<std::array<int8_t, 4>, 24> kMountCodes{{
248 {{1, 0, 0, 0}}, {{3, 0, 0, 3}}, {{0, 0, 0, 1}}, {{3, 0, 0, -3}}, {{0, 0, -1, 0}},
249 {{0, -3, -3, 0}}, {{0, -1, 0, 0}}, {{0, -3, 3, 0}}, {{0, 0, -3, 3}}, {{2, -2, -2, 2}},
250 {{3, -3, 0, 0}}, {{2, -2, 2, -2}}, {{-3, -3, 0, 0}}, {{-2, -2, -2, -2}}, {{0, 0, -3, -3}},
251 {{2, 2, -2, -2}}, {{-2, -2, -2, 2}}, {{0, -3, 0, 3}}, {{2, -2, 2, 2}}, {{-3, 0, -3, 0}},
252 {{2, 2, -2, 2}}, {{0, -3, 0, -3}}, {{2, -2, -2, -2}}, {{3, 0, -3, 0}},
253}};
254
255} // namespace detail
256
258inline constexpr std::size_t kMountingCount = 24;
259
266[[nodiscard]] constexpr Quat30 MountingQuaternion(std::size_t row) noexcept {
267 if (row >= kMountingCount) {
268 return Quat30{};
269 }
270 const auto& c = detail::kMountCodes[row];
271 return Quat30{.x = detail::MountQ30(c[1]),
272 .y = detail::MountQ30(c[2]),
273 .z = detail::MountQ30(c[3]),
274 .w = detail::MountQ30(c[0])};
275}
276
278enum class CalStep : uint8_t {
279 EnableSensors = 0,
281 MovePoses = 1,
282 SetDown = 2,
283 RotateAxes = 3,
284 WaitAccuracy = 4,
285 Save = 5,
286 Done = 6,
287};
288
294public:
296 [[nodiscard]] CalStep Step() const noexcept {
297 return step_;
298 }
301 void Advance() noexcept {
302 const uint8_t s = detail::ToUnderlying(step_);
303 if ((step_ == CalStep::WaitAccuracy) && !accurate_) {
304 return;
305 }
306 if (step_ != CalStep::Done) {
307 step_ = static_cast<CalStep>(s + 1U); // cast of a value proven below `Done`
308 }
309 }
312 void Observe(Accuracy a) noexcept {
313 accurate_ = (a == Accuracy::Medium) || (a == Accuracy::High);
314 if ((step_ == CalStep::WaitAccuracy) && accurate_) {
315 step_ = CalStep::Save;
316 }
317 }
319 [[nodiscard]] bool ReadyToSave() const noexcept {
320 return accurate_;
321 }
323 void Reset() noexcept {
325 accurate_ = false;
326 }
328 [[nodiscard]] static constexpr uint32_t MinRateHz() noexcept {
329 return 50;
330 }
331
332private:
334 bool accurate_{false};
335};
336
337} // namespace bno08x
338
339#endif // BNO08X_RECORDS_HPP
Compile-time helpers of the BNO08X driver: typed integer literals, exact hook matching,...
BNO08x protocol constants: SHTP channels, SH-2 report, command and record identifiers,...
Public types of the BNO08x driver: errors, Result<T>, configuration, events and the integer unit help...
A state object that turns the calibration procedure into steps. It sends nothing: it follows what you...
Definition bno08x_records.hpp:293
static constexpr uint32_t MinRateHz() noexcept
The slowest report rate the procedure allows, in Hz (DS p38).
Definition bno08x_records.hpp:328
bool ReadyToSave() const noexcept
Definition bno08x_records.hpp:319
void Observe(Accuracy a) noexcept
Tell the helper the latest magnetometer accuracy (the status of a magnetic field sample).
Definition bno08x_records.hpp:312
CalStep Step() const noexcept
Definition bno08x_records.hpp:296
void Advance() noexcept
The user finished the current step; go to the next one (WaitAccuracy needs Observe()).
Definition bno08x_records.hpp:301
void Reset() noexcept
Start again.
Definition bno08x_records.hpp:323
Accuracy
Accuracy of a sensor sample (status bits 1:0 of every input report, RM S6.5.1).
Definition bno08x_types.hpp:196
@ High
High accuracy.
@ Medium
Medium accuracy.
The BNO08x driver: everything it declares lives here; internals in detail.
Definition bno08x.hpp:36
constexpr std::size_t kEnvCalWords
Environmental calibration records.
Definition bno08x_records.hpp:112
constexpr std::size_t kSigMotionWords
Record 0xC274.
Definition bno08x_records.hpp:110
constexpr std::array< uint32_t, kEnvCalWords > EncodeEnvCalibration(const EnvCalibration &e) noexcept
Words of an environmental calibration record.
Definition bno08x_records.hpp:196
constexpr std::size_t kArvrWords
Records 0x3E2D and 0x3E2E.
Definition bno08x_records.hpp:109
constexpr std::array< uint32_t, kSigMotionWords > EncodeSignificantMotion(const SignificantMotionConfig &s) noexcept
Words of record 0xC274.
Definition bno08x_records.hpp:168
constexpr std::array< uint32_t, kOrientationWords > EncodeOrientation(const Quat30 &q) noexcept
Words of an orientation record (x, y, z, w; RM S4.1).
Definition bno08x_records.hpp:128
constexpr ShakeConfig DecodeShake(const std::array< uint32_t, kShakeWords > &w) noexcept
The record 0x7D7D of its words.
Definition bno08x_records.hpp:185
constexpr std::size_t kPowerMgmtWords
Record 0xD3E2.
Definition bno08x_records.hpp:108
constexpr std::array< uint32_t, kShakeWords > EncodeShake(const ShakeConfig &s) noexcept
Words of record 0x7D7D (the enable word has bit 0 X, bit 1 Y, bit 2 Z).
Definition bno08x_records.hpp:179
constexpr std::size_t kShakeWords
Record 0x7D7D.
Definition bno08x_records.hpp:111
constexpr GyroRvConfig DecodeGyroRvConfig(const std::array< uint32_t, kGyroRvConfigWords > &w) noexcept
The record 0xA1A2 of its words.
Definition bno08x_records.hpp:214
constexpr EnvCalibration DecodeEnvCalibration(const std::array< uint32_t, kEnvCalWords > &w) noexcept
The environmental calibration of its words.
Definition bno08x_records.hpp:201
constexpr std::array< uint32_t, kArvrWords > EncodeArvr(const ArvrStabilization &a) noexcept
Words of an AR/VR stabilisation record.
Definition bno08x_records.hpp:154
constexpr std::array< uint32_t, kGyroRvConfigWords > EncodeGyroRvConfig(const GyroRvConfig &g) noexcept
Words of record 0xA1A2.
Definition bno08x_records.hpp:206
constexpr Quat30 MountingQuaternion(std::size_t row) noexcept
The mapping quaternion of row row of DS Figure 4-3 (p41), the system orientation for a device mounted...
Definition bno08x_records.hpp:266
constexpr SignificantMotionConfig DecodeSignificantMotion(const std::array< uint32_t, kSigMotionWords > &w) noexcept
The record 0xC274 of its words.
Definition bno08x_records.hpp:173
constexpr std::size_t kOrientationWords
Words of each typed record.
Definition bno08x_records.hpp:107
constexpr ArvrStabilization DecodeArvr(const std::array< uint32_t, kArvrWords > &w) noexcept
The AR/VR record of its words.
Definition bno08x_records.hpp:160
constexpr Quat30 DecodeOrientation(const std::array< uint32_t, kOrientationWords > &w) noexcept
The quaternion of an orientation record.
Definition bno08x_records.hpp:133
constexpr std::array< uint32_t, kPowerMgmtWords > EncodePowerManagement(const PowerManagement &p) noexcept
Words of record 0xD3E2.
Definition bno08x_records.hpp:139
constexpr std::size_t kMountingCount
Number of mounting rows in DS Figure 4-3.
Definition bno08x_records.hpp:258
CalStep
Steps of the calibration procedure (CAL 1000-4044, DS S3.1).
Definition bno08x_records.hpp:278
@ MovePoses
Move the device through 4 to 6 orientations, about 1 s each.
@ RotateAxes
Rotate about 180 degrees around each axis, about 2 s each.
@ SetDown
Set the device down for 2 to 3 s so the gyroscope calibrates.
@ Save
Save the DCD.
@ WaitAccuracy
Wait until the magnetometer accuracy reads medium or high.
@ Done
The calibration is stored.
constexpr PowerManagement DecodePowerManagement(const std::array< uint32_t, kPowerMgmtWords > &w) noexcept
The record 0xD3E2 of its words.
Definition bno08x_records.hpp:146
constexpr std::size_t kGyroRvConfigWords
Record 0xA1A2.
Definition bno08x_records.hpp:113
AR/VR stabilisation records 0x3E2D and 0x3E2E (RM S4.2).
Definition bno08x_records.hpp:48
int32_t stability_q29
Definition bno08x_records.hpp:52
int32_t max_error_q29
Maximum error, Q29.
Definition bno08x_records.hpp:51
int32_t max_rotation_q29
Maximum rotation, Q29.
Definition bno08x_records.hpp:50
friend constexpr bool operator==(const ArvrStabilization &lhs, const ArvrStabilization &rhs) noexcept=default
Equality of all fields, as a hidden friend.
int32_t scaling_q30
Scaling, Q30.
Definition bno08x_records.hpp:49
Environmental sensor calibration records 0x39AF, 0x4D20, 0x1AC9, 0x39B1, 0x4DA2.
Definition bno08x_records.hpp:82
int32_t scale_q15
Definition bno08x_records.hpp:85
int32_t offset
Definition bno08x_records.hpp:83
friend constexpr bool operator==(const EnvCalibration &lhs, const EnvCalibration &rhs) noexcept=default
Equality of all fields, as a hidden friend.
Gyro-integrated rotation vector configuration record 0xA1A2 (RM S4.3.12).
Definition bno08x_records.hpp:92
int32_t alpha_q20
Alpha, Q20.
Definition bno08x_records.hpp:98
int32_t prediction_q10
Prediction amount in seconds, Q10.
Definition bno08x_records.hpp:97
uint32_t sync_interval_us
Synchronisation interval.
Definition bno08x_records.hpp:95
int32_t gamma_q20
Definition bno08x_records.hpp:100
int32_t beta_q20
Beta, Q20.
Definition bno08x_records.hpp:99
friend constexpr bool operator==(const GyroRvConfig &lhs, const GyroRvConfig &rhs) noexcept=default
Equality of all fields, as a hidden friend.
uint32_t reference_type
Reference data type: 0x0207 game rotation vector, 0x0204 rotation vector.
Definition bno08x_records.hpp:93
int32_t max_error_q29
Maximum error, Q29 radians.
Definition bno08x_records.hpp:96
MotionEngine power management and stability classifier record 0xD3E2 (RM S4.3.1).
Definition bno08x_records.hpp:37
uint8_t delta_accel_mg
Definition bno08x_records.hpp:41
friend constexpr bool operator==(const PowerManagement &lhs, const PowerManagement &rhs) noexcept=default
Equality of all fields, as a hidden friend.
int32_t stable_threshold_q25
Stability: angular rate threshold in rad/s, Q25.
Definition bno08x_records.hpp:39
int32_t delta_orientation_q28
Stability: orientation change in radians, Q28.
Definition bno08x_records.hpp:38
uint8_t stable_duration_s
Stability: seconds the device must be still.
Definition bno08x_records.hpp:40
A unit quaternion in Q30 (the orientation records, RM S4.1).
Definition bno08x_records.hpp:27
friend constexpr bool operator==(const Quat30 &lhs, const Quat30 &rhs) noexcept=default
Equality of all fields, as a hidden friend.
int32_t y
Y component, Q30.
Definition bno08x_records.hpp:29
int32_t z
Z component, Q30.
Definition bno08x_records.hpp:30
int32_t x
X component, Q30.
Definition bno08x_records.hpp:28
int32_t w
Definition bno08x_records.hpp:31
Shake detector configuration record 0x7D7D.
Definition bno08x_records.hpp:68
int32_t threshold_q26
Acceleration threshold in m/s2, Q26.
Definition bno08x_records.hpp:71
bool enable_y
Detect on Y.
Definition bno08x_records.hpp:74
friend constexpr bool operator==(const ShakeConfig &lhs, const ShakeConfig &rhs) noexcept=default
Equality of all fields, as a hidden friend.
bool enable_x
Detect on X.
Definition bno08x_records.hpp:73
bool enable_z
Definition bno08x_records.hpp:75
uint32_t count
Shakes needed.
Definition bno08x_records.hpp:72
uint32_t max_time_us
Longest shake.
Definition bno08x_records.hpp:70
uint32_t min_time_us
Shortest shake.
Definition bno08x_records.hpp:69
Significant motion detector configuration record 0xC274.
Definition bno08x_records.hpp:59
uint32_t step_threshold
Definition bno08x_records.hpp:61
friend constexpr bool operator==(const SignificantMotionConfig &lhs, const SignificantMotionConfig &rhs) noexcept=default
Equality of all fields, as a hidden friend.
int32_t accel_threshold_q24
Acceleration threshold in m/s2, Q24.
Definition bno08x_records.hpp:60