I2C library for Bosch BNO055 sensor
Dependents: Project Campus_Safety_Bot
Fork of BNO055 by
Revision 7:f5880506defe, committed 2017-05-10
- Comitter:
- vargham
- Date:
- Wed May 10 12:12:13 2017 +0000
- Parent:
- 6:1f722ffec323
- Commit message:
- Added I2C object and I2C address arguments to constructor.
Changed in this revision
BNO055.cpp | Show annotated file Show diff for this revision Revisions of this file |
BNO055.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 1f722ffec323 -r f5880506defe BNO055.cpp --- a/BNO055.cpp Tue Sep 22 19:09:45 2015 +0000 +++ b/BNO055.cpp Wed May 10 12:12:13 2017 +0000 @@ -1,16 +1,29 @@ #include "BNO055.h" #include "mbed.h" -BNO055::BNO055(PinName SDA, PinName SCL) : _i2c(SDA,SCL){ +BNO055::BNO055(PinName SDA, PinName SCL, char i2cAddress) : + _i2c(SDA, SCL), + address(i2cAddress), + accel_scale(0.001f), + rate_scale(1.0f/16.0f), + angle_scale(1.0f/16.0f), + temp_scale(1) +{ //Set I2C fast and bring reset line high _i2c.frequency(400000); - address = BNOAddress; - accel_scale = 0.001f; - rate_scale = 1.0f/16.0f; - angle_scale = 1.0f/16.0f; - temp_scale = 1; - } - +} + +BNO055::BNO055(I2C &i2c, char i2cAddress) : + _i2c(i2c), + address(i2cAddress), + accel_scale(0.001f), + rate_scale(1.0f/16.0f), + angle_scale(1.0f/16.0f), + temp_scale(1) +{ + _i2c.frequency(400000); +} + void BNO055::reset(){ //Perform a power-on-reset readchar(BNO055_SYS_TRIGGER_ADDR); @@ -19,15 +32,15 @@ //Wait for the system to come back up again (datasheet says 650ms) wait_ms(675); } - + bool BNO055::check(){ //Check we have communication link with the chip readchar(BNO055_CHIP_ID_ADDR); if (rx != 0xA0) return false; //Grab the chip ID and software versions tx[0] = BNO055_CHIP_ID_ADDR; - _i2c.write(address,tx,1,true); - _i2c.read(address+1,rawdata,7,false); + _i2c.write(address,tx,1,true); + _i2c.read(address+1,rawdata,7,false); ID.id = rawdata[0]; ID.accel = rawdata[1]; ID.mag = rawdata[2]; @@ -37,18 +50,18 @@ ID.bootload = rawdata[6]; setpage(1); tx[0] = BNO055_UNIQUE_ID_ADDR; - _i2c.write(address,tx,1,true); - _i2c.read(address+1,ID.serial,16,false); + _i2c.write(address,tx,1,true); + _i2c.read(address+1,ID.serial,16,false); setpage(0); return true; } - + void BNO055::SetExternalCrystal(bool yn){ // Read the current status from the device - readchar(BNO055_SYS_TRIGGER_ADDR); + readchar(BNO055_SYS_TRIGGER_ADDR); if (yn) rx = rx | 0x80; else rx = rx & 0x7F; - writechar(BNO055_SYS_TRIGGER_ADDR,rx); + writechar(BNO055_SYS_TRIGGER_ADDR,rx); } void BNO055::set_accel_units(char units){ @@ -75,7 +88,7 @@ rate_scale = 1.0f/900.0f; } writechar(BNO055_UNIT_SEL_ADDR,rx); -} +} void BNO055::set_angle_units(char units){ readchar(BNO055_UNIT_SEL_ADDR); @@ -88,7 +101,7 @@ rate_scale = 1.0f/900.0f; } writechar(BNO055_UNIT_SEL_ADDR,rx); -} +} void BNO055::set_temp_units(char units){ readchar(BNO055_UNIT_SEL_ADDR); @@ -101,14 +114,14 @@ temp_scale = 2; } writechar(BNO055_UNIT_SEL_ADDR,rx); -} +} void BNO055::set_orientation(char units){ readchar(BNO055_UNIT_SEL_ADDR); if (units == WINDOWS) rx = rx &0x7F; else rx = rx | units; writechar(BNO055_UNIT_SEL_ADDR,rx); -} +} void BNO055::setmode(char omode){ writechar(BNO055_OPR_MODE_ADDR,omode); @@ -122,8 +135,8 @@ void BNO055::get_accel(void){ tx[0] = BNO055_ACCEL_DATA_X_LSB_ADDR; - _i2c.write(address,tx,1,true); - _i2c.read(address+1,rawdata,6,0); + _i2c.write(address,tx,1,true); + _i2c.read(address+1,rawdata,6,0); accel.rawx = (rawdata[1] << 8 | rawdata[0]); accel.rawy = (rawdata[3] << 8 | rawdata[2]); accel.rawz = (rawdata[5] << 8 | rawdata[4]); @@ -131,11 +144,11 @@ accel.y = float(accel.rawy)*accel_scale; accel.z = float(accel.rawz)*accel_scale; } - + void BNO055::get_gyro(void){ tx[0] = BNO055_GYRO_DATA_X_LSB_ADDR; - _i2c.write(address,tx,1,true); - _i2c.read(address+1,rawdata,6,0); + _i2c.write(address,tx,1,true); + _i2c.read(address+1,rawdata,6,0); gyro.rawx = (rawdata[1] << 8 | rawdata[0]); gyro.rawy = (rawdata[3] << 8 | rawdata[2]); gyro.rawz = (rawdata[5] << 8 | rawdata[4]); @@ -146,8 +159,8 @@ void BNO055::get_mag(void){ tx[0] = BNO055_MAG_DATA_X_LSB_ADDR; - _i2c.write(address,tx,1,true); - _i2c.read(address+1,rawdata,6,0); + _i2c.write(address,tx,1,true); + _i2c.read(address+1,rawdata,6,0); mag.rawx = (rawdata[1] << 8 | rawdata[0]); mag.rawy = (rawdata[3] << 8 | rawdata[2]); mag.rawz = (rawdata[5] << 8 | rawdata[4]); @@ -158,8 +171,8 @@ void BNO055::get_lia(void){ tx[0] = BNO055_LINEAR_ACCEL_DATA_X_LSB_ADDR; - _i2c.write(address,tx,1,true); - _i2c.read(address+1,rawdata,6,0); + _i2c.write(address,tx,1,true); + _i2c.read(address+1,rawdata,6,0); lia.rawx = (rawdata[1] << 8 | rawdata[0]); lia.rawy = (rawdata[3] << 8 | rawdata[2]); lia.rawz = (rawdata[5] << 8 | rawdata[4]); @@ -170,8 +183,8 @@ void BNO055::get_grv(void){ tx[0] = BNO055_GRAVITY_DATA_X_LSB_ADDR; - _i2c.write(address,tx,1,true); - _i2c.read(address+1,rawdata,6,0); + _i2c.write(address,tx,1,true); + _i2c.read(address+1,rawdata,6,0); gravity.rawx = (rawdata[1] << 8 | rawdata[0]); gravity.rawy = (rawdata[3] << 8 | rawdata[2]); gravity.rawz = (rawdata[5] << 8 | rawdata[4]); @@ -182,8 +195,8 @@ void BNO055::get_quat(void){ tx[0] = BNO055_QUATERNION_DATA_W_LSB_ADDR; - _i2c.write(address,tx,1,true); - _i2c.read(address+1,rawdata,8,0); + _i2c.write(address,tx,1,true); + _i2c.read(address+1,rawdata,8,0); quat.raww = (rawdata[1] << 8 | rawdata[0]); quat.rawx = (rawdata[3] << 8 | rawdata[2]); quat.rawy = (rawdata[5] << 8 | rawdata[4]); @@ -196,8 +209,8 @@ void BNO055::get_angles(void){ tx[0] = BNO055_EULER_H_LSB_ADDR; - _i2c.write(address,tx,1,true); - _i2c.read(address+1,rawdata,6,0); + _i2c.write(address,tx,1,true); + _i2c.read(address+1,rawdata,6,0); euler.rawyaw = (rawdata[1] << 8 | rawdata[0]); euler.rawroll = (rawdata[3] << 8 | rawdata[2]); euler.rawpitch = (rawdata[5] << 8 | rawdata[4]); @@ -222,8 +235,8 @@ setmode(OPERATION_MODE_CONFIG); wait_ms(20); tx[0] = ACCEL_OFFSET_X_LSB_ADDR; - _i2c.write(address,tx,1,true); - _i2c.read(address,calibration,22,false); + _i2c.write(address,tx,1,true); + _i2c.read(address,calibration,22,false); setmode(tempmode); wait_ms(10); } @@ -233,15 +246,15 @@ setmode(OPERATION_MODE_CONFIG); wait_ms(20); tx[0] = ACCEL_OFFSET_X_LSB_ADDR; - _i2c.write(address,tx,1,true); - _i2c.write(address,calibration,22,false); + _i2c.write(address,tx,1,true); + _i2c.write(address,calibration,22,false); setmode(tempmode); wait_ms(10); } void BNO055::set_mapping(char orient){ switch (orient){ - case 0: + case 0: writechar(BNO055_AXIS_MAP_CONFIG_ADDR,0x21); writechar(BNO055_AXIS_MAP_SIGN_ADDR,0x04); break; @@ -277,4 +290,4 @@ writechar(BNO055_AXIS_MAP_CONFIG_ADDR,0x24); writechar(BNO055_AXIS_MAP_SIGN_ADDR,0x00); } -} \ No newline at end of file +}
diff -r 1f722ffec323 -r f5880506defe BNO055.h --- a/BNO055.h Tue Sep 22 19:09:45 2015 +0000 +++ b/BNO055.h Wed May 10 12:12:13 2017 +0000 @@ -5,45 +5,45 @@ // #define BNOAddress (0x28 << 1) //Register definitions -/* Page id register definition */ +/* Page id register definition */ #define BNO055_PAGE_ID_ADDR 0x07 -/* PAGE0 REGISTER DEFINITION START*/ -#define BNO055_CHIP_ID_ADDR 0x00 +/* PAGE0 REGISTER DEFINITION START*/ +#define BNO055_CHIP_ID_ADDR 0x00 #define BNO055_ACCEL_REV_ID_ADDR 0x01 #define BNO055_MAG_REV_ID_ADDR 0x02 #define BNO055_GYRO_REV_ID_ADDR 0x03 #define BNO055_SW_REV_ID_LSB_ADDR 0x04 #define BNO055_SW_REV_ID_MSB_ADDR 0x05 #define BNO055_BL_REV_ID_ADDR 0x06 -/* Accel data register */ +/* Accel data register */ #define BNO055_ACCEL_DATA_X_LSB_ADDR 0x08 #define BNO055_ACCEL_DATA_X_MSB_ADDR 0x09 #define BNO055_ACCEL_DATA_Y_LSB_ADDR 0x0A #define BNO055_ACCEL_DATA_Y_MSB_ADDR 0x0B -#define BNO055_ACCEL_DATA_Z_LSB_ADDR 0x0C +#define BNO055_ACCEL_DATA_Z_LSB_ADDR 0x0C #define BNO055_ACCEL_DATA_Z_MSB_ADDR 0x0D -/* Mag data register */ +/* Mag data register */ #define BNO055_MAG_DATA_X_LSB_ADDR 0x0E #define BNO055_MAG_DATA_X_MSB_ADDR 0x0F #define BNO055_MAG_DATA_Y_LSB_ADDR 0x10 #define BNO055_MAG_DATA_Y_MSB_ADDR 0x11 #define BNO055_MAG_DATA_Z_LSB_ADDR 0x12 #define BNO055_MAG_DATA_Z_MSB_ADDR 0x13 -/* Gyro data registers */ +/* Gyro data registers */ #define BNO055_GYRO_DATA_X_LSB_ADDR 0x14 #define BNO055_GYRO_DATA_X_MSB_ADDR 0x15 #define BNO055_GYRO_DATA_Y_LSB_ADDR 0x16 #define BNO055_GYRO_DATA_Y_MSB_ADDR 0x17 #define BNO055_GYRO_DATA_Z_LSB_ADDR 0x18 #define BNO055_GYRO_DATA_Z_MSB_ADDR 0x19 -/* Euler data registers */ +/* Euler data registers */ #define BNO055_EULER_H_LSB_ADDR 0x1A #define BNO055_EULER_H_MSB_ADDR 0x1B #define BNO055_EULER_R_LSB_ADDR 0x1C #define BNO055_EULER_R_MSB_ADDR 0x1D #define BNO055_EULER_P_LSB_ADDR 0x1E #define BNO055_EULER_P_MSB_ADDR 0x1F -/* Quaternion data registers */ +/* Quaternion data registers */ #define BNO055_QUATERNION_DATA_W_LSB_ADDR 0x20 #define BNO055_QUATERNION_DATA_W_MSB_ADDR 0x21 #define BNO055_QUATERNION_DATA_X_LSB_ADDR 0x22 @@ -52,66 +52,66 @@ #define BNO055_QUATERNION_DATA_Y_MSB_ADDR 0x25 #define BNO055_QUATERNION_DATA_Z_LSB_ADDR 0x26 #define BNO055_QUATERNION_DATA_Z_MSB_ADDR 0x27 -/* Linear acceleration data registers */ +/* Linear acceleration data registers */ #define BNO055_LINEAR_ACCEL_DATA_X_LSB_ADDR 0x28 #define BNO055_LINEAR_ACCEL_DATA_X_MSB_ADDR 0x29 #define BNO055_LINEAR_ACCEL_DATA_Y_LSB_ADDR 0x2A #define BNO055_LINEAR_ACCEL_DATA_Y_MSB_ADDR 0x2B #define BNO055_LINEAR_ACCEL_DATA_Z_LSB_ADDR 0x2C #define BNO055_LINEAR_ACCEL_DATA_Z_MSB_ADDR 0x2D -/* Gravity data registers */ +/* Gravity data registers */ #define BNO055_GRAVITY_DATA_X_LSB_ADDR 0x2E #define BNO055_GRAVITY_DATA_X_MSB_ADDR 0x2F #define BNO055_GRAVITY_DATA_Y_LSB_ADDR 0x30 #define BNO055_GRAVITY_DATA_Y_MSB_ADDR 0x31 #define BNO055_GRAVITY_DATA_Z_LSB_ADDR 0x32 #define BNO055_GRAVITY_DATA_Z_MSB_ADDR 0x33 -/* Temperature data register */ -#define BNO055_TEMP_ADDR 0x34 -/* Status registers */ -#define BNO055_CALIB_STAT_ADDR 0x35 -#define BNO055_SELFTEST_RESULT_ADDR 0x36 -#define BNO055_INTR_STAT_ADDR 0x37 -#define BNO055_SYS_CLK_STAT_ADDR 0x38 -#define BNO055_SYS_STAT_ADDR 0x39 -#define BNO055_SYS_ERR_ADDR 0x3A -/* Unit selection register */ -#define BNO055_UNIT_SEL_ADDR 0x3B -#define BNO055_DATA_SELECT_ADDR 0x3C -/* Mode registers */ -#define BNO055_OPR_MODE_ADDR 0x3D -#define BNO055_PWR_MODE_ADDR 0x3E -#define BNO055_SYS_TRIGGER_ADDR 0x3F -#define BNO055_TEMP_SOURCE_ADDR 0x40 -/* Axis remap registers */ -#define BNO055_AXIS_MAP_CONFIG_ADDR 0x41 -#define BNO055_AXIS_MAP_SIGN_ADDR 0x42 -/* Accelerometer Offset registers */ -#define ACCEL_OFFSET_X_LSB_ADDR 0x55 -#define ACCEL_OFFSET_X_MSB_ADDR 0x56 +/* Temperature data register */ +#define BNO055_TEMP_ADDR 0x34 +/* Status registers */ +#define BNO055_CALIB_STAT_ADDR 0x35 +#define BNO055_SELFTEST_RESULT_ADDR 0x36 +#define BNO055_INTR_STAT_ADDR 0x37 +#define BNO055_SYS_CLK_STAT_ADDR 0x38 +#define BNO055_SYS_STAT_ADDR 0x39 +#define BNO055_SYS_ERR_ADDR 0x3A +/* Unit selection register */ +#define BNO055_UNIT_SEL_ADDR 0x3B +#define BNO055_DATA_SELECT_ADDR 0x3C +/* Mode registers */ +#define BNO055_OPR_MODE_ADDR 0x3D +#define BNO055_PWR_MODE_ADDR 0x3E +#define BNO055_SYS_TRIGGER_ADDR 0x3F +#define BNO055_TEMP_SOURCE_ADDR 0x40 +/* Axis remap registers */ +#define BNO055_AXIS_MAP_CONFIG_ADDR 0x41 +#define BNO055_AXIS_MAP_SIGN_ADDR 0x42 +/* Accelerometer Offset registers */ +#define ACCEL_OFFSET_X_LSB_ADDR 0x55 +#define ACCEL_OFFSET_X_MSB_ADDR 0x56 #define ACCEL_OFFSET_Y_LSB_ADDR 0x57 -#define ACCEL_OFFSET_Y_MSB_ADDR 0x58 -#define ACCEL_OFFSET_Z_LSB_ADDR 0x59 -#define ACCEL_OFFSET_Z_MSB_ADDR 0x5A -/* Magnetometer Offset registers */ +#define ACCEL_OFFSET_Y_MSB_ADDR 0x58 +#define ACCEL_OFFSET_Z_LSB_ADDR 0x59 +#define ACCEL_OFFSET_Z_MSB_ADDR 0x5A +/* Magnetometer Offset registers */ #define MAG_OFFSET_X_LSB_ADDR 0x5B #define MAG_OFFSET_X_MSB_ADDR 0x5C #define MAG_OFFSET_Y_LSB_ADDR 0x5D #define MAG_OFFSET_Y_MSB_ADDR 0x5E #define MAG_OFFSET_Z_LSB_ADDR 0x5F #define MAG_OFFSET_Z_MSB_ADDR 0x60 -/* Gyroscope Offset registers*/ +/* Gyroscope Offset registers*/ #define GYRO_OFFSET_X_LSB_ADDR 0x61 #define GYRO_OFFSET_X_MSB_ADDR 0x62 #define GYRO_OFFSET_Y_LSB_ADDR 0x63 #define GYRO_OFFSET_Y_MSB_ADDR 0x64 #define GYRO_OFFSET_Z_LSB_ADDR 0x65 #define GYRO_OFFSET_Z_MSB_ADDR 0x66 -/* Radius registers */ -#define ACCEL_RADIUS_LSB_ADDR 0x67 -#define ACCEL_RADIUS_MSB_ADDR 0x68 -#define MAG_RADIUS_LSB_ADDR 0x69 -#define MAG_RADIUS_MSB_ADDR 0x6A +/* Radius registers */ +#define ACCEL_RADIUS_LSB_ADDR 0x67 +#define ACCEL_RADIUS_MSB_ADDR 0x68 +#define MAG_RADIUS_LSB_ADDR 0x69 +#define MAG_RADIUS_MSB_ADDR 0x6A /* Page 1 registers */ #define BNO055_UNIQUE_ID_ADDR 0x50 @@ -129,35 +129,35 @@ #define ANDROID 0x80 //Definitions for power mode -#define POWER_MODE_NORMAL 0x00 -#define POWER_MODE_LOWPOWER 0x01 -#define POWER_MODE_SUSPEND 0x02 +#define POWER_MODE_NORMAL 0x00 +#define POWER_MODE_LOWPOWER 0x01 +#define POWER_MODE_SUSPEND 0x02 //Definitions for operating mode -#define OPERATION_MODE_CONFIG 0x00 -#define OPERATION_MODE_ACCONLY 0x01 -#define OPERATION_MODE_MAGONLY 0x02 -#define OPERATION_MODE_GYRONLY 0x03 -#define OPERATION_MODE_ACCMAG 0x04 -#define OPERATION_MODE_ACCGYRO 0x05 -#define OPERATION_MODE_MAGGYRO 0x06 -#define OPERATION_MODE_AMG 0x07 -#define OPERATION_MODE_IMUPLUS 0x08 -#define OPERATION_MODE_COMPASS 0x09 -#define OPERATION_MODE_M4G 0x0A -#define OPERATION_MODE_NDOF_FMC_OFF 0x0B -#define OPERATION_MODE_NDOF 0x0C +#define OPERATION_MODE_CONFIG 0x00 +#define OPERATION_MODE_ACCONLY 0x01 +#define OPERATION_MODE_MAGONLY 0x02 +#define OPERATION_MODE_GYRONLY 0x03 +#define OPERATION_MODE_ACCMAG 0x04 +#define OPERATION_MODE_ACCGYRO 0x05 +#define OPERATION_MODE_MAGGYRO 0x06 +#define OPERATION_MODE_AMG 0x07 +#define OPERATION_MODE_IMUPLUS 0x08 +#define OPERATION_MODE_COMPASS 0x09 +#define OPERATION_MODE_M4G 0x0A +#define OPERATION_MODE_NDOF_FMC_OFF 0x0B +#define OPERATION_MODE_NDOF 0x0C typedef struct values{ int16_t rawx,rawy,rawz; float x,y,z; }values; - + typedef struct angles{ int16_t rawroll,rawpitch,rawyaw; float roll, pitch, yaw; } angles; - + typedef struct quaternion{ int16_t raww,rawx,rawy,rawz; float w,x,y,z; @@ -174,25 +174,28 @@ }chip; /** Class for operating Bosch BNO055 sensor over I2C **/ -class BNO055 -{ -public: +class BNO055 +{ +public: /** Create BNO055 instance **/ - BNO055(PinName SDA, PinName SCL); - + BNO055(PinName SDA, PinName SCL, char i2cAddress = BNOAddress); + + /** Create BNO055 instance **/ + BNO055(I2C &i2c, char i2cAddress = BNOAddress); + /** Perform a power-on reset of the BNO055 **/ void reset(); -/** Check that the BNO055 is connected and download the software details +/** Check that the BNO055 is connected and download the software details and serial number of chip and store in ID structure **/ bool check(); /** Turn the external timing crystal on/off **/ void SetExternalCrystal(bool yn); -/** Set the operation mode of the sensor **/ +/** Set the operation mode of the sensor **/ void setmode(char mode); /** Set the power mode of the sensor **/ void setpowermode(char mode); - + /** Set the output units from the accelerometer, either MPERSPERS or MILLIG **/ void set_accel_units(char units); /** Set the output units from the gyroscope, either DEG_PER_SEC or RAD_PER_SEC **/ @@ -201,12 +204,12 @@ void set_angle_units(char units); /** Set the output units from the temperature sensor, either CENTIGRADE or FAHRENHEIT **/ void set_temp_units(char units); -/** Set the data output format to either WINDOWS or ANDROID **/ +/** Set the data output format to either WINDOWS or ANDROID **/ void set_orientation(char units); /** Set the mapping of the exes/directions as per page 25 of datasheet range 0-7, any value outside this will set the orientation to P1 (default at power up) **/ void set_mapping(char orient); - + /** Get the current values from the accelerometer **/ void get_accel(void); /** Get the current values from the gyroscope **/ @@ -228,9 +231,9 @@ void get_calib(void); /** Read the offset and radius values into the calibration array**/ void read_calibration_data(void); -/** Write the contents of the calibration array into the registers **/ +/** Write the contents of the calibration array into the registers **/ void write_calibration_data(void); - + /** Structures containing 3-axis data for acceleration, rate of turn and magnetic field. x,y,z are the scale floating point values and rawx, rawy, rawz are the int16_t values read from the sensors **/ @@ -239,7 +242,7 @@ and rawyaw, rawroll & rollpitch as the int16_t values loaded from the registers **/ angles euler; /** Quaternion values as w,x,y,z (scaled floating point) and raww etc... as int16_t loaded from the - registers **/ + registers **/ quaternion quat; /** Current contents of calibration status register **/ @@ -250,9 +253,9 @@ chip ID; /** Current temperature **/ int temperature; - + private: - + I2C _i2c; char rx,tx[2],address; //I2C variables char rawdata[22]; //Temporary array for input data values @@ -260,7 +263,7 @@ char pwr_mode; float accel_scale,rate_scale,angle_scale; int temp_scale; - + void readchar(char location){ tx[0] = location; _i2c.write(address,tx,1,true); @@ -277,4 +280,4 @@ writechar(BNO055_PAGE_ID_ADDR,value); } }; -#endif \ No newline at end of file +#endif