-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbno055_wrapper.cpp
164 lines (143 loc) · 4.8 KB
/
bno055_wrapper.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "bno055_wrapper.hpp"
#include <stdexcept>
extern "C" {
#include "getbno055.h"
}
BNO055::BNO055(const std::string& i2c_bus, const std::string& address)
: i2c_bus_(i2c_bus), address_(address), initialized_(false) {
get_i2cbus(const_cast<char*>(i2c_bus_.c_str()), const_cast<char*>(address_.c_str()));
initialized_ = true;
}
BNO055::~BNO055() {}
void BNO055::reset() {
if (bno_reset() != 0) {
throw std::runtime_error("Failed to reset BNO055");
}
}
void BNO055::set_mode(opmode_t mode) {
if (::set_mode(mode) != 0) {
throw std::runtime_error("Failed to set mode");
}
}
void BNO055::set_mode_from_string(const std::string& mode) {
opmode_t newmode;
if (mode == "config") newmode = config;
else if (mode == "acconly") newmode = acconly;
else if (mode == "magonly") newmode = magonly;
else if (mode == "gyronly") newmode = gyronly;
else if (mode == "accmag") newmode = accmag;
else if (mode == "accgyro") newmode = accgyro;
else if (mode == "maggyro") newmode = maggyro;
else if (mode == "amg") newmode = amg;
else if (mode == "imu") newmode = imu;
else if (mode == "compass") newmode = compass;
else if (mode == "m4g") newmode = m4g;
else if (mode == "ndof") newmode = ndof;
else if (mode == "ndof_fmc") newmode = ndof_fmc;
else throw std::invalid_argument("Invalid mode");
set_mode(newmode);
}
std::string BNO055::get_mode() const {
int mode = ::get_mode();
switch(mode) {
case config: return "config";
case acconly: return "acconly";
case magonly: return "magonly";
case gyronly: return "gyronly";
case accmag: return "accmag";
case accgyro: return "accgyro";
case maggyro: return "maggyro";
case amg: return "amg";
case imu: return "imu";
case compass: return "compass";
case m4g: return "m4g";
case ndof: return "ndof";
case ndof_fmc: return "ndof_fmc";
default: return "unknown";
}
}
void BNO055::set_power_mode(const std::string& mode) {
power_t newmode;
if (mode == "normal") newmode = normal;
else if (mode == "low") newmode = low;
else if (mode == "suspend") newmode = suspend;
else throw std::invalid_argument("Invalid power mode");
if (::set_power(newmode) != 0) {
throw std::runtime_error("Failed to set power mode");
}
}
std::string BNO055::get_power_mode() const {
int mode = ::get_power();
switch(mode) {
case normal: return "normal";
case low: return "low";
case suspend: return "suspend";
default: return "unknown";
}
}
BNO055::EulerAngles BNO055::get_euler() {
struct bnoeul data;
if (get_eul(&data) != 0) {
throw std::runtime_error("Failed to get euler angles");
}
return {data.eul_head, data.eul_roll, data.eul_pitc};
}
BNO055::Quaternion BNO055::get_quaternion() {
struct bnoqua data;
if (get_qua(&data) != 0) {
throw std::runtime_error("Failed to get quaternion");
}
return {data.quater_w, data.quater_x, data.quater_y, data.quater_z};
}
BNO055::Vector3 BNO055::get_accelerometer() {
struct bnoacc data;
if (get_acc(&data) != 0) {
throw std::runtime_error("Failed to get accelerometer data");
}
return {data.adata_x, data.adata_y, data.adata_z};
}
BNO055::Vector3 BNO055::get_magnetometer() {
struct bnomag data;
if (get_mag(&data) != 0) {
throw std::runtime_error("Failed to get magnetometer data");
}
return {data.mdata_x, data.mdata_y, data.mdata_z};
}
BNO055::Vector3 BNO055::get_gyroscope() {
struct bnogyr data;
if (get_gyr(&data) != 0) {
throw std::runtime_error("Failed to get gyroscope data");
}
return {data.gdata_x, data.gdata_y, data.gdata_z};
}
BNO055::Vector3 BNO055::get_gravity() {
struct bnogra data;
if (get_gra(&data) != 0) {
throw std::runtime_error("Failed to get gravity data");
}
return {data.gravityx, data.gravityy, data.gravityz};
}
BNO055::Vector3 BNO055::get_linear_acceleration() {
struct bnolin data;
if (get_lin(&data) != 0) {
throw std::runtime_error("Failed to get linear acceleration data");
}
return {data.linacc_x, data.linacc_y, data.linacc_z};
}
bool BNO055::is_fully_calibrated() const {
struct bnocal cal;
if (get_calstatus(&cal) != 0) {
throw std::runtime_error("Failed to get calibration status");
}
return cal.scal_st == 3;
}
void BNO055::save_calibration(const std::string& filename) {
if (save_cal(const_cast<char*>(filename.c_str())) != 0) {
throw std::runtime_error("Failed to save calibration");
}
}
void BNO055::load_calibration(const std::string& filename) {
if (load_cal(const_cast<char*>(filename.c_str())) != 0) {
throw std::runtime_error("Failed to load calibration");
}
}