A library of the 9-Axis Sensor BNO055 from Bosch Sensortec.

Requires the I2C-Master library - Link

Committer:
joelvonrotz
Date:
Thu Jul 11 11:09:06 2019 +0000
Revision:
4:f581a8e97aec
Parent:
0:a2a71c38065e
Child:
5:e6cb34d9531d
updated main page

Who changed what in which revision?

UserRevisionLine numberNew contents of line
joelvonrotz 0:a2a71c38065e 1 /**
joelvonrotz 0:a2a71c38065e 2 * @brief Declaration of the BNO055 class
joelvonrotz 0:a2a71c38065e 3 *
joelvonrotz 0:a2a71c38065e 4 * @file bno055.h
joelvonrotz 0:a2a71c38065e 5 * @author Joel von Rotz
joelvonrotz 0:a2a71c38065e 6 * @date 18.07.2018
joelvonrotz 0:a2a71c38065e 7 */
joelvonrotz 0:a2a71c38065e 8 #ifndef BNO055_H
joelvonrotz 0:a2a71c38065e 9 #define BNO055_H
joelvonrotz 0:a2a71c38065e 10
joelvonrotz 0:a2a71c38065e 11 #include "mbed.h"
joelvonrotz 0:a2a71c38065e 12 #include "bno055_config.h"
joelvonrotz 0:a2a71c38065e 13 #include "bno055_registermap.h"
joelvonrotz 0:a2a71c38065e 14 #include "bno055_definitions.h"
joelvonrotz 0:a2a71c38065e 15 #include "i2c_master.h"
joelvonrotz 0:a2a71c38065e 16
joelvonrotz 0:a2a71c38065e 17
joelvonrotz 0:a2a71c38065e 18 /**
joelvonrotz 4:f581a8e97aec 19 * @brief This is a library for the sensor BNO055, a 9 axis sensor by <a href="https://www.bosch-sensortec.com/">Bosch Sensortec</a>, written by <a href="https://os.mbed.com/users/joelvonrotz/">Joel von Rotz</a>
joelvonrotz 0:a2a71c38065e 20 * <img src="https://ae01.alicdn.com/kf/HTB1n3aPNpXXXXaFXVXXq6xXFXXXi/BNO055-gyroscope-Absolute-Orientation-9-Axis-Sensor.jpg_640x640.jpg" alt="Image of a flying Potato here!">
joelvonrotz 0:a2a71c38065e 21 * Currently, this library is compatible with the mbed LPC1768 only and probably won't change in the future, unless somebody takes their time to port the library to other microcontroller (would very much appreciate it though).
joelvonrotz 0:a2a71c38065e 22 * \n\n
joelvonrotz 4:f581a8e97aec 23 * <center><a href="https://os.mbed.com/users/joelvonrotz/code/i2c-master/">This Library requires The <strong>TWI_Master</strong> Library to work</a></center>
joelvonrotz 0:a2a71c38065e 24 * \n\n
joelvonrotz 0:a2a71c38065e 25 * <h2>Example Programm Without Debug</h2>
joelvonrotz 0:a2a71c38065e 26 * \n\n
joelvonrotz 0:a2a71c38065e 27 * @code
joelvonrotz 4:f581a8e97aec 28 * #include "mbed.h"
joelvonrotz 4:f581a8e97aec 29 * #include "i2c_master.h"
joelvonrotz 4:f581a8e97aec 30 * #include "bno055.h"
joelvonrotz 4:f581a8e97aec 31 *
joelvonrotz 4:f581a8e97aec 32 * Serial pc(USBTX,USBRX,115200);
joelvonrotz 4:f581a8e97aec 33 *
joelvonrotz 4:f581a8e97aec 34 * I2C_Master i2c_master(p9, p10);
joelvonrotz 4:f581a8e97aec 35 * DigitalOut bno055_reset(p11);
joelvonrotz 4:f581a8e97aec 36 * BNO055 bno055(0x28, //7-bit slave address
joelvonrotz 4:f581a8e97aec 37 * i2c_master, //reference of the i2c-master object
joelvonrotz 4:f581a8e97aec 38 * bno055_reset, //reference of the reset-pin connected with the mbed
joelvonrotz 4:f581a8e97aec 39 * true); //enable external 32kHz oscillator
joelvonrotz 4:f581a8e97aec 40 *
joelvonrotz 4:f581a8e97aec 41 *
joelvonrotz 4:f581a8e97aec 42 * int main() {
joelvonrotz 4:f581a8e97aec 43 * bno055.setOperationMode(OPERATION_MODE_NDOF);
joelvonrotz 4:f581a8e97aec 44 *
joelvonrotz 4:f581a8e97aec 45 * while(1) {
joelvonrotz 4:f581a8e97aec 46 * bno055.getEulerDegrees();
joelvonrotz 4:f581a8e97aec 47 * pc.printf("x: %4.3f y: %4.3f z: %4.3f\n\r", bno055.euler.x,bno055.euler.y,bno055.euler.z);
joelvonrotz 4:f581a8e97aec 48 * wait_ms(100);
joelvonrotz 4:f581a8e97aec 49 * }
joelvonrotz 4:f581a8e97aec 50 * }
joelvonrotz 0:a2a71c38065e 51 * @endcode
joelvonrotz 0:a2a71c38065e 52 * \n\n
joelvonrotz 0:a2a71c38065e 53 * <h2>Example Programm With Debug</h2>
joelvonrotz 0:a2a71c38065e 54 * \n\n
joelvonrotz 0:a2a71c38065e 55 * @code
joelvonrotz 4:f581a8e97aec 56 * #define DEBUGGING_ENABLED
joelvonrotz 4:f581a8e97aec 57 *
joelvonrotz 4:f581a8e97aec 58 * #include "mbed.h"
joelvonrotz 4:f581a8e97aec 59 * #include "i2c_master.h"
joelvonrotz 4:f581a8e97aec 60 * #include "bno055.h"
joelvonrotz 4:f581a8e97aec 61 *
joelvonrotz 4:f581a8e97aec 62 * Serial pc(USBTX,USBRX,115200);
joelvonrotz 4:f581a8e97aec 63 *
joelvonrotz 4:f581a8e97aec 64 * I2C_Master i2c_master(p9, p10);
joelvonrotz 4:f581a8e97aec 65 * DigitalOut bno055_reset(p11);
joelvonrotz 4:f581a8e97aec 66 * BNO055 bno055(0x28, //7-bit slave address
joelvonrotz 4:f581a8e97aec 67 * i2c_master, //reference of the i2c-master object
joelvonrotz 4:f581a8e97aec 68 * bno055_reset, //reference of the reset-pin connected with the mbed
joelvonrotz 4:f581a8e97aec 69 * true, //enable external 32kHz oscillator
joelvonrotz 4:f581a8e97aec 70 * pc); //debugging Serial object
joelvonrotz 4:f581a8e97aec 71 *
joelvonrotz 4:f581a8e97aec 72 * int main() {
joelvonrotz 4:f581a8e97aec 73 * bno055.setOperationMode(OPERATION_MODE_NDOF);
joelvonrotz 4:f581a8e97aec 74 *
joelvonrotz 4:f581a8e97aec 75 * while(1) {
joelvonrotz 4:f581a8e97aec 76 * bno055.getEulerDegrees();
joelvonrotz 4:f581a8e97aec 77 * pc.printf("x: %4.3f y: %4.3f z: %4.3f\n\r", bno055.euler.x,bno055.euler.y,bno055.euler.z);
joelvonrotz 4:f581a8e97aec 78 * wait_ms(100);
joelvonrotz 4:f581a8e97aec 79 * }
joelvonrotz 4:f581a8e97aec 80 * }
joelvonrotz 0:a2a71c38065e 81 * @endcode
joelvonrotz 0:a2a71c38065e 82 */
joelvonrotz 0:a2a71c38065e 83 class BNO055
joelvonrotz 0:a2a71c38065e 84 {
joelvonrotz 0:a2a71c38065e 85 public:
joelvonrotz 0:a2a71c38065e 86 #ifdef DEBUGGING_ENABLED
joelvonrotz 0:a2a71c38065e 87 BNO055(uint8_t slave_address, I2C_Master& i2c_master, DigitalOut& ResetPin, bool external_clk, Serial& DEBUG_SERIAL);
joelvonrotz 0:a2a71c38065e 88 #else
joelvonrotz 0:a2a71c38065e 89 BNO055(uint8_t slave_address, I2C_Master& i2c_master, DigitalOut& ResetPin, bool external_clk);
joelvonrotz 0:a2a71c38065e 90 #endif
joelvonrotz 0:a2a71c38065e 91 //=---------------------------------------
joelvonrotz 0:a2a71c38065e 92 struct bno055_format_s
joelvonrotz 0:a2a71c38065e 93 {
joelvonrotz 0:a2a71c38065e 94 uint8_t units;
joelvonrotz 0:a2a71c38065e 95 bno055_orientation_t orientation;
joelvonrotz 0:a2a71c38065e 96 bno055_temperature_t temperature;
joelvonrotz 0:a2a71c38065e 97 bno055_euler_t euler;
joelvonrotz 0:a2a71c38065e 98 bno055_gyro_t gyroscope;
joelvonrotz 0:a2a71c38065e 99 bno055_acceleration_t acceleration;
joelvonrotz 0:a2a71c38065e 100 };
joelvonrotz 0:a2a71c38065e 101
joelvonrotz 0:a2a71c38065e 102 struct bno055_id_s
joelvonrotz 0:a2a71c38065e 103 {
joelvonrotz 0:a2a71c38065e 104 uint8_t chip;
joelvonrotz 0:a2a71c38065e 105 uint8_t accel;
joelvonrotz 0:a2a71c38065e 106 uint8_t gyro;
joelvonrotz 0:a2a71c38065e 107 uint8_t magneto;
joelvonrotz 0:a2a71c38065e 108 uint8_t bl_rev;
joelvonrotz 0:a2a71c38065e 109 uint16_t sw_rev;
joelvonrotz 0:a2a71c38065e 110 };
joelvonrotz 0:a2a71c38065e 111
joelvonrotz 0:a2a71c38065e 112 struct bno055_mode_s
joelvonrotz 0:a2a71c38065e 113 {
joelvonrotz 0:a2a71c38065e 114 uint8_t power;
joelvonrotz 0:a2a71c38065e 115 uint8_t operation;
joelvonrotz 0:a2a71c38065e 116 };
joelvonrotz 0:a2a71c38065e 117
joelvonrotz 0:a2a71c38065e 118 struct bno055_system_s
joelvonrotz 0:a2a71c38065e 119 {
joelvonrotz 0:a2a71c38065e 120 uint8_t status;
joelvonrotz 0:a2a71c38065e 121 uint8_t error;
joelvonrotz 0:a2a71c38065e 122 };
joelvonrotz 0:a2a71c38065e 123
joelvonrotz 0:a2a71c38065e 124 struct bno055_axis_s
joelvonrotz 0:a2a71c38065e 125 {
joelvonrotz 0:a2a71c38065e 126 uint8_t map;
joelvonrotz 0:a2a71c38065e 127 uint8_t sign;
joelvonrotz 0:a2a71c38065e 128 };
joelvonrotz 0:a2a71c38065e 129
joelvonrotz 0:a2a71c38065e 130 struct bno055_interrupt_s
joelvonrotz 0:a2a71c38065e 131 {
joelvonrotz 0:a2a71c38065e 132 uint8_t status;
joelvonrotz 0:a2a71c38065e 133 struct bno055_gyro_interrupt_s
joelvonrotz 0:a2a71c38065e 134 {
joelvonrotz 0:a2a71c38065e 135 bool high_rate;
joelvonrotz 0:a2a71c38065e 136 bool any_motion;
joelvonrotz 0:a2a71c38065e 137 }gyroscope;
joelvonrotz 0:a2a71c38065e 138
joelvonrotz 0:a2a71c38065e 139 struct bno055_accel_interrupt_s
joelvonrotz 0:a2a71c38065e 140 {
joelvonrotz 0:a2a71c38065e 141 bool no_motion;
joelvonrotz 0:a2a71c38065e 142 bool any_motion;
joelvonrotz 0:a2a71c38065e 143 bool high_g;
joelvonrotz 0:a2a71c38065e 144 }acceleration;
joelvonrotz 0:a2a71c38065e 145 };
joelvonrotz 0:a2a71c38065e 146
joelvonrotz 0:a2a71c38065e 147 struct bno055_quaternion_s
joelvonrotz 0:a2a71c38065e 148 {
joelvonrotz 0:a2a71c38065e 149 float w;
joelvonrotz 0:a2a71c38065e 150 float x;
joelvonrotz 0:a2a71c38065e 151 float y;
joelvonrotz 0:a2a71c38065e 152 float z;
joelvonrotz 0:a2a71c38065e 153 };
joelvonrotz 0:a2a71c38065e 154
joelvonrotz 0:a2a71c38065e 155 struct bno055_data_s
joelvonrotz 0:a2a71c38065e 156 {
joelvonrotz 0:a2a71c38065e 157 float x;
joelvonrotz 0:a2a71c38065e 158 float y;
joelvonrotz 0:a2a71c38065e 159 float z;
joelvonrotz 0:a2a71c38065e 160 };
joelvonrotz 0:a2a71c38065e 161
joelvonrotz 0:a2a71c38065e 162 struct bno055_calibration_s
joelvonrotz 0:a2a71c38065e 163 {
joelvonrotz 0:a2a71c38065e 164 uint8_t system;
joelvonrotz 0:a2a71c38065e 165 uint8_t gyro;
joelvonrotz 0:a2a71c38065e 166 uint8_t acceleration;
joelvonrotz 0:a2a71c38065e 167 uint8_t magneto;
joelvonrotz 0:a2a71c38065e 168 uint8_t status;
joelvonrotz 0:a2a71c38065e 169 };
joelvonrotz 0:a2a71c38065e 170
joelvonrotz 0:a2a71c38065e 171 //=---------------------------------------
joelvonrotz 0:a2a71c38065e 172
joelvonrotz 0:a2a71c38065e 173
joelvonrotz 0:a2a71c38065e 174 float temperature;
joelvonrotz 0:a2a71c38065e 175 uint16_t orientation;
joelvonrotz 0:a2a71c38065e 176
joelvonrotz 0:a2a71c38065e 177 bno055_format_s format;
joelvonrotz 0:a2a71c38065e 178 bno055_id_s id;
joelvonrotz 0:a2a71c38065e 179 bno055_mode_s mode;
joelvonrotz 0:a2a71c38065e 180 bno055_system_s system;
joelvonrotz 0:a2a71c38065e 181 bno055_axis_s axis;
joelvonrotz 0:a2a71c38065e 182
joelvonrotz 0:a2a71c38065e 183 bno055_data_s gravity_vector;
joelvonrotz 0:a2a71c38065e 184 bno055_data_s linear_accel;
joelvonrotz 0:a2a71c38065e 185 bno055_data_s accel;
joelvonrotz 0:a2a71c38065e 186 bno055_data_s magneto;
joelvonrotz 0:a2a71c38065e 187 bno055_data_s gyro;
joelvonrotz 0:a2a71c38065e 188 bno055_data_s euler;
joelvonrotz 0:a2a71c38065e 189
joelvonrotz 0:a2a71c38065e 190 bno055_quaternion_s quaternion;
joelvonrotz 0:a2a71c38065e 191 bno055_calibration_s calibration;
joelvonrotz 0:a2a71c38065e 192
joelvonrotz 0:a2a71c38065e 193 bno055_interrupt_s interrupt;
joelvonrotz 0:a2a71c38065e 194
joelvonrotz 0:a2a71c38065e 195 //=---------------------------------------
joelvonrotz 0:a2a71c38065e 196
joelvonrotz 0:a2a71c38065e 197 void setUnitFormat(bno055_orientation_t new_orientation_format = WINDOWS,
joelvonrotz 0:a2a71c38065e 198 bno055_temperature_t new_temperature_format = CELSIUS,
joelvonrotz 0:a2a71c38065e 199 bno055_euler_t new_euler_format = DEGREES,
joelvonrotz 0:a2a71c38065e 200 bno055_gyro_t new_gyroscope_format = DEGREE_PER_SEC,
joelvonrotz 0:a2a71c38065e 201 bno055_acceleration_t new_acceleration_format = ACCELERATION
joelvonrotz 0:a2a71c38065e 202 );
joelvonrotz 0:a2a71c38065e 203
joelvonrotz 0:a2a71c38065e 204 void getUnitFormat(void);
joelvonrotz 0:a2a71c38065e 205 void getIDs(void);
joelvonrotz 0:a2a71c38065e 206 void setPowerMode(bno055_powermode_t new_power_mode = POWER_NORMAL);
joelvonrotz 0:a2a71c38065e 207 uint8_t getPowerMode(void);
joelvonrotz 0:a2a71c38065e 208 void setOperationMode(bno055_opr_mode_t new_opr_mode = OPERATION_MODE_CONFIGMODE);
joelvonrotz 0:a2a71c38065e 209 uint8_t getOperationMode(void);
joelvonrotz 0:a2a71c38065e 210 void setPage(bno055_page_t new_page = PAGE_0);
joelvonrotz 0:a2a71c38065e 211 uint8_t getPage(void);
joelvonrotz 0:a2a71c38065e 212 uint8_t getSystemStatus(void);
joelvonrotz 0:a2a71c38065e 213 uint8_t getSystemError(void);
joelvonrotz 0:a2a71c38065e 214 void useExternalOscillator(bool enabled);
joelvonrotz 0:a2a71c38065e 215 void resetSW(void);
joelvonrotz 0:a2a71c38065e 216 void resetHW(void);
joelvonrotz 0:a2a71c38065e 217 void assignAxis( bno055_axis_t x_axis = X_AXIS,
joelvonrotz 0:a2a71c38065e 218 bno055_axis_t y_axis = Y_AXIS,
joelvonrotz 0:a2a71c38065e 219 bno055_axis_t z_axis = Z_AXIS);
joelvonrotz 0:a2a71c38065e 220 void setAxisSign(bno055_axis_sign_t x_sign = POSITIVE,
joelvonrotz 0:a2a71c38065e 221 bno055_axis_sign_t y_sign = POSITIVE,
joelvonrotz 0:a2a71c38065e 222 bno055_axis_sign_t z_sign = POSITIVE);
joelvonrotz 0:a2a71c38065e 223 void setOrientation(bno055_remap_options_t orientation_placement = REMAP_OPTION_P1);
joelvonrotz 0:a2a71c38065e 224 void getOrientation(void);
joelvonrotz 0:a2a71c38065e 225 void getCalibrationStatus(void);
joelvonrotz 0:a2a71c38065e 226
joelvonrotz 0:a2a71c38065e 227 void getInterruptFlag(void);
joelvonrotz 0:a2a71c38065e 228 void setEnableInterrupts(bno055_enable_t accel_no_motion,
joelvonrotz 0:a2a71c38065e 229 bno055_enable_t accel_any_motion,
joelvonrotz 0:a2a71c38065e 230 bno055_enable_t accel_high_g,
joelvonrotz 0:a2a71c38065e 231 bno055_enable_t gyro_high_rate,
joelvonrotz 0:a2a71c38065e 232 bno055_enable_t gyro_any_motion
joelvonrotz 0:a2a71c38065e 233 );
joelvonrotz 0:a2a71c38065e 234 uint8_t getEnabledInterrupts(void);
joelvonrotz 0:a2a71c38065e 235
joelvonrotz 0:a2a71c38065e 236 void setInterruptMask(bno055_enable_t accel_no_motion,
joelvonrotz 0:a2a71c38065e 237 bno055_enable_t accel_any_motion,
joelvonrotz 0:a2a71c38065e 238 bno055_enable_t accel_high_g,
joelvonrotz 0:a2a71c38065e 239 bno055_enable_t gyro_high_rate,
joelvonrotz 0:a2a71c38065e 240 bno055_enable_t gyro_any_motion
joelvonrotz 0:a2a71c38065e 241 );
joelvonrotz 0:a2a71c38065e 242 uint8_t getInterruptMask(void);
joelvonrotz 0:a2a71c38065e 243
joelvonrotz 0:a2a71c38065e 244 void configAccelerationInterrupt(bno055_config_int_axis_t high_axis,
joelvonrotz 0:a2a71c38065e 245 bno055_config_int_axis_t am_nm_axis,
joelvonrotz 0:a2a71c38065e 246 bno055_any_motion_sample_t am_dur
joelvonrotz 0:a2a71c38065e 247 );
joelvonrotz 0:a2a71c38065e 248 void configGyroscopeInterrupt(bno055_config_int_axis_t hg_z_axis,
joelvonrotz 0:a2a71c38065e 249 bno055_config_int_axis_t hg_y_axis,
joelvonrotz 0:a2a71c38065e 250 bool high_rate_unfilt,
joelvonrotz 0:a2a71c38065e 251 bool any_motion_unfilt
joelvonrotz 0:a2a71c38065e 252 );
joelvonrotz 0:a2a71c38065e 253
joelvonrotz 0:a2a71c38065e 254 void getAcceleration(void);
joelvonrotz 0:a2a71c38065e 255 void getMagnetometer(void);
joelvonrotz 0:a2a71c38065e 256 void getGyroscope(void);
joelvonrotz 0:a2a71c38065e 257 void getEulerDegrees(void);
joelvonrotz 0:a2a71c38065e 258 void getQuaternion(void);
joelvonrotz 0:a2a71c38065e 259 void getLinearAcceleration(void);
joelvonrotz 0:a2a71c38065e 260 void getGravityVector(void);
joelvonrotz 0:a2a71c38065e 261 void getTemperature(void);
joelvonrotz 0:a2a71c38065e 262
joelvonrotz 0:a2a71c38065e 263 private:
joelvonrotz 0:a2a71c38065e 264 I2C_Master& m_i2c_master;
joelvonrotz 0:a2a71c38065e 265 DigitalOut& m_ResetPin;
joelvonrotz 0:a2a71c38065e 266
joelvonrotz 0:a2a71c38065e 267 //DEBUG SERIAL
joelvonrotz 0:a2a71c38065e 268 #ifdef DEBUGGING_ENABLED
joelvonrotz 0:a2a71c38065e 269 Serial& m_DEBUG_SERIAL;
joelvonrotz 0:a2a71c38065e 270 #endif
joelvonrotz 0:a2a71c38065e 271
joelvonrotz 0:a2a71c38065e 272 uint8_t m_bno055_address;
joelvonrotz 0:a2a71c38065e 273
joelvonrotz 0:a2a71c38065e 274 uint8_t page;
joelvonrotz 0:a2a71c38065e 275 uint8_t arrayIndex;
joelvonrotz 0:a2a71c38065e 276 float sensor_data_converted;
joelvonrotz 0:a2a71c38065e 277 uint8_t register_data[8];
joelvonrotz 0:a2a71c38065e 278 int16_t sensor_data[4];
joelvonrotz 0:a2a71c38065e 279
joelvonrotz 0:a2a71c38065e 280 void get(bno055_reg_t address, bno055_data_s& data, const float format, bool is_euler);
joelvonrotz 0:a2a71c38065e 281 void get(bno055_reg_t address, bno055_quaternion_s& data, const float format);
joelvonrotz 0:a2a71c38065e 282
joelvonrotz 0:a2a71c38065e 283
joelvonrotz 0:a2a71c38065e 284 };
joelvonrotz 0:a2a71c38065e 285
joelvonrotz 0:a2a71c38065e 286 #endif /* BNO055_H */
joelvonrotz 0:a2a71c38065e 287
joelvonrotz 0:a2a71c38065e 288