I2C library for Bosch BNO055 sensor

Dependents:   Project Campus_Safety_Bot

Fork of BNO055 by Dave Turner

Committer:
vargham
Date:
Wed May 10 12:12:13 2017 +0000
Revision:
7:f5880506defe
Parent:
6:1f722ffec323
Added I2C object and I2C address arguments to constructor.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
StressedDave 0:24f23c36dd24 1 #include "BNO055.h"
StressedDave 0:24f23c36dd24 2 #include "mbed.h"
StressedDave 0:24f23c36dd24 3
vargham 7:f5880506defe 4 BNO055::BNO055(PinName SDA, PinName SCL, char i2cAddress) :
vargham 7:f5880506defe 5 _i2c(SDA, SCL),
vargham 7:f5880506defe 6 address(i2cAddress),
vargham 7:f5880506defe 7 accel_scale(0.001f),
vargham 7:f5880506defe 8 rate_scale(1.0f/16.0f),
vargham 7:f5880506defe 9 angle_scale(1.0f/16.0f),
vargham 7:f5880506defe 10 temp_scale(1)
vargham 7:f5880506defe 11 {
StressedDave 0:24f23c36dd24 12 //Set I2C fast and bring reset line high
StressedDave 5:beaa2bff7ff0 13 _i2c.frequency(400000);
vargham 7:f5880506defe 14 }
vargham 7:f5880506defe 15
vargham 7:f5880506defe 16 BNO055::BNO055(I2C &i2c, char i2cAddress) :
vargham 7:f5880506defe 17 _i2c(i2c),
vargham 7:f5880506defe 18 address(i2cAddress),
vargham 7:f5880506defe 19 accel_scale(0.001f),
vargham 7:f5880506defe 20 rate_scale(1.0f/16.0f),
vargham 7:f5880506defe 21 angle_scale(1.0f/16.0f),
vargham 7:f5880506defe 22 temp_scale(1)
vargham 7:f5880506defe 23 {
vargham 7:f5880506defe 24 _i2c.frequency(400000);
vargham 7:f5880506defe 25 }
vargham 7:f5880506defe 26
StressedDave 0:24f23c36dd24 27 void BNO055::reset(){
StressedDave 0:24f23c36dd24 28 //Perform a power-on-reset
StressedDave 0:24f23c36dd24 29 readchar(BNO055_SYS_TRIGGER_ADDR);
StressedDave 0:24f23c36dd24 30 rx = rx | 0x20;
StressedDave 0:24f23c36dd24 31 writechar(BNO055_SYS_TRIGGER_ADDR,rx);
StressedDave 0:24f23c36dd24 32 //Wait for the system to come back up again (datasheet says 650ms)
StressedDave 0:24f23c36dd24 33 wait_ms(675);
StressedDave 0:24f23c36dd24 34 }
vargham 7:f5880506defe 35
StressedDave 0:24f23c36dd24 36 bool BNO055::check(){
StressedDave 0:24f23c36dd24 37 //Check we have communication link with the chip
StressedDave 0:24f23c36dd24 38 readchar(BNO055_CHIP_ID_ADDR);
StressedDave 0:24f23c36dd24 39 if (rx != 0xA0) return false;
StressedDave 0:24f23c36dd24 40 //Grab the chip ID and software versions
StressedDave 0:24f23c36dd24 41 tx[0] = BNO055_CHIP_ID_ADDR;
vargham 7:f5880506defe 42 _i2c.write(address,tx,1,true);
vargham 7:f5880506defe 43 _i2c.read(address+1,rawdata,7,false);
StressedDave 0:24f23c36dd24 44 ID.id = rawdata[0];
StressedDave 0:24f23c36dd24 45 ID.accel = rawdata[1];
StressedDave 0:24f23c36dd24 46 ID.mag = rawdata[2];
StressedDave 0:24f23c36dd24 47 ID.gyro = rawdata[3];
StressedDave 0:24f23c36dd24 48 ID.sw[0] = rawdata[4];
StressedDave 0:24f23c36dd24 49 ID.sw[1] = rawdata[5];
StressedDave 0:24f23c36dd24 50 ID.bootload = rawdata[6];
StressedDave 0:24f23c36dd24 51 setpage(1);
StressedDave 0:24f23c36dd24 52 tx[0] = BNO055_UNIQUE_ID_ADDR;
vargham 7:f5880506defe 53 _i2c.write(address,tx,1,true);
vargham 7:f5880506defe 54 _i2c.read(address+1,ID.serial,16,false);
StressedDave 0:24f23c36dd24 55 setpage(0);
StressedDave 0:24f23c36dd24 56 return true;
StressedDave 0:24f23c36dd24 57 }
vargham 7:f5880506defe 58
StressedDave 0:24f23c36dd24 59 void BNO055::SetExternalCrystal(bool yn){
StressedDave 0:24f23c36dd24 60 // Read the current status from the device
vargham 7:f5880506defe 61 readchar(BNO055_SYS_TRIGGER_ADDR);
StressedDave 0:24f23c36dd24 62 if (yn) rx = rx | 0x80;
StressedDave 0:24f23c36dd24 63 else rx = rx & 0x7F;
vargham 7:f5880506defe 64 writechar(BNO055_SYS_TRIGGER_ADDR,rx);
StressedDave 0:24f23c36dd24 65 }
StressedDave 0:24f23c36dd24 66
StressedDave 0:24f23c36dd24 67 void BNO055::set_accel_units(char units){
StressedDave 0:24f23c36dd24 68 readchar(BNO055_UNIT_SEL_ADDR);
StressedDave 0:24f23c36dd24 69 if(units == MPERSPERS){
StressedDave 0:24f23c36dd24 70 rx = rx & 0xFE;
StressedDave 0:24f23c36dd24 71 accel_scale = 0.01f;
StressedDave 0:24f23c36dd24 72 }
StressedDave 0:24f23c36dd24 73 else {
StressedDave 0:24f23c36dd24 74 rx = rx | units;
StressedDave 6:1f722ffec323 75 accel_scale = 0.001f;
StressedDave 0:24f23c36dd24 76 }
StressedDave 0:24f23c36dd24 77 writechar(BNO055_UNIT_SEL_ADDR,rx);
StressedDave 0:24f23c36dd24 78 }
StressedDave 0:24f23c36dd24 79
StressedDave 0:24f23c36dd24 80 void BNO055::set_anglerate_units(char units){
StressedDave 0:24f23c36dd24 81 readchar(BNO055_UNIT_SEL_ADDR);
StressedDave 0:24f23c36dd24 82 if (units == DEG_PER_SEC){
StressedDave 0:24f23c36dd24 83 rx = rx & 0xFD;
StressedDave 0:24f23c36dd24 84 rate_scale = 1.0f/16.0f;
StressedDave 0:24f23c36dd24 85 }
StressedDave 0:24f23c36dd24 86 else {
StressedDave 0:24f23c36dd24 87 rx = rx | units;
StressedDave 0:24f23c36dd24 88 rate_scale = 1.0f/900.0f;
StressedDave 0:24f23c36dd24 89 }
StressedDave 0:24f23c36dd24 90 writechar(BNO055_UNIT_SEL_ADDR,rx);
vargham 7:f5880506defe 91 }
StressedDave 0:24f23c36dd24 92
StressedDave 0:24f23c36dd24 93 void BNO055::set_angle_units(char units){
StressedDave 0:24f23c36dd24 94 readchar(BNO055_UNIT_SEL_ADDR);
StressedDave 0:24f23c36dd24 95 if (units == DEGREES){
StressedDave 0:24f23c36dd24 96 rx = rx & 0xFB;
StressedDave 0:24f23c36dd24 97 angle_scale = 1.0f/16.0f;
StressedDave 0:24f23c36dd24 98 }
StressedDave 0:24f23c36dd24 99 else {
StressedDave 0:24f23c36dd24 100 rx = rx | units;
StressedDave 0:24f23c36dd24 101 rate_scale = 1.0f/900.0f;
StressedDave 0:24f23c36dd24 102 }
StressedDave 0:24f23c36dd24 103 writechar(BNO055_UNIT_SEL_ADDR,rx);
vargham 7:f5880506defe 104 }
StressedDave 0:24f23c36dd24 105
StressedDave 0:24f23c36dd24 106 void BNO055::set_temp_units(char units){
StressedDave 0:24f23c36dd24 107 readchar(BNO055_UNIT_SEL_ADDR);
StressedDave 0:24f23c36dd24 108 if (units == CENTIGRADE){
StressedDave 1:2c3322a8d417 109 rx = rx & 0xEF;
StressedDave 1:2c3322a8d417 110 temp_scale = 1;
StressedDave 0:24f23c36dd24 111 }
StressedDave 0:24f23c36dd24 112 else {
StressedDave 0:24f23c36dd24 113 rx = rx | units;
StressedDave 1:2c3322a8d417 114 temp_scale = 2;
StressedDave 0:24f23c36dd24 115 }
StressedDave 0:24f23c36dd24 116 writechar(BNO055_UNIT_SEL_ADDR,rx);
vargham 7:f5880506defe 117 }
StressedDave 0:24f23c36dd24 118
StressedDave 1:2c3322a8d417 119 void BNO055::set_orientation(char units){
StressedDave 1:2c3322a8d417 120 readchar(BNO055_UNIT_SEL_ADDR);
StressedDave 1:2c3322a8d417 121 if (units == WINDOWS) rx = rx &0x7F;
StressedDave 1:2c3322a8d417 122 else rx = rx | units;
StressedDave 1:2c3322a8d417 123 writechar(BNO055_UNIT_SEL_ADDR,rx);
vargham 7:f5880506defe 124 }
StressedDave 1:2c3322a8d417 125
StressedDave 0:24f23c36dd24 126 void BNO055::setmode(char omode){
StressedDave 0:24f23c36dd24 127 writechar(BNO055_OPR_MODE_ADDR,omode);
StressedDave 0:24f23c36dd24 128 op_mode = omode;
StressedDave 0:24f23c36dd24 129 }
StressedDave 0:24f23c36dd24 130
StressedDave 0:24f23c36dd24 131 void BNO055::setpowermode(char pmode){
StressedDave 0:24f23c36dd24 132 writechar(BNO055_PWR_MODE_ADDR,pmode);
StressedDave 0:24f23c36dd24 133 pwr_mode = pmode;
StressedDave 0:24f23c36dd24 134 }
StressedDave 0:24f23c36dd24 135
StressedDave 0:24f23c36dd24 136 void BNO055::get_accel(void){
StressedDave 0:24f23c36dd24 137 tx[0] = BNO055_ACCEL_DATA_X_LSB_ADDR;
vargham 7:f5880506defe 138 _i2c.write(address,tx,1,true);
vargham 7:f5880506defe 139 _i2c.read(address+1,rawdata,6,0);
StressedDave 0:24f23c36dd24 140 accel.rawx = (rawdata[1] << 8 | rawdata[0]);
StressedDave 0:24f23c36dd24 141 accel.rawy = (rawdata[3] << 8 | rawdata[2]);
StressedDave 0:24f23c36dd24 142 accel.rawz = (rawdata[5] << 8 | rawdata[4]);
StressedDave 0:24f23c36dd24 143 accel.x = float(accel.rawx)*accel_scale;
StressedDave 0:24f23c36dd24 144 accel.y = float(accel.rawy)*accel_scale;
StressedDave 0:24f23c36dd24 145 accel.z = float(accel.rawz)*accel_scale;
StressedDave 0:24f23c36dd24 146 }
vargham 7:f5880506defe 147
StressedDave 0:24f23c36dd24 148 void BNO055::get_gyro(void){
StressedDave 0:24f23c36dd24 149 tx[0] = BNO055_GYRO_DATA_X_LSB_ADDR;
vargham 7:f5880506defe 150 _i2c.write(address,tx,1,true);
vargham 7:f5880506defe 151 _i2c.read(address+1,rawdata,6,0);
StressedDave 0:24f23c36dd24 152 gyro.rawx = (rawdata[1] << 8 | rawdata[0]);
StressedDave 0:24f23c36dd24 153 gyro.rawy = (rawdata[3] << 8 | rawdata[2]);
StressedDave 0:24f23c36dd24 154 gyro.rawz = (rawdata[5] << 8 | rawdata[4]);
StressedDave 0:24f23c36dd24 155 gyro.x = float(gyro.rawx)*rate_scale;
StressedDave 0:24f23c36dd24 156 gyro.y = float(gyro.rawy)*rate_scale;
StressedDave 0:24f23c36dd24 157 gyro.z = float(gyro.rawz)*rate_scale;
StressedDave 0:24f23c36dd24 158 }
StressedDave 0:24f23c36dd24 159
StressedDave 0:24f23c36dd24 160 void BNO055::get_mag(void){
StressedDave 0:24f23c36dd24 161 tx[0] = BNO055_MAG_DATA_X_LSB_ADDR;
vargham 7:f5880506defe 162 _i2c.write(address,tx,1,true);
vargham 7:f5880506defe 163 _i2c.read(address+1,rawdata,6,0);
StressedDave 0:24f23c36dd24 164 mag.rawx = (rawdata[1] << 8 | rawdata[0]);
StressedDave 0:24f23c36dd24 165 mag.rawy = (rawdata[3] << 8 | rawdata[2]);
StressedDave 0:24f23c36dd24 166 mag.rawz = (rawdata[5] << 8 | rawdata[4]);
StressedDave 0:24f23c36dd24 167 mag.x = float(mag.rawx);
StressedDave 0:24f23c36dd24 168 mag.y = float(mag.rawy);
StressedDave 0:24f23c36dd24 169 mag.z = float(mag.rawz);
StressedDave 0:24f23c36dd24 170 }
StressedDave 0:24f23c36dd24 171
StressedDave 0:24f23c36dd24 172 void BNO055::get_lia(void){
StressedDave 0:24f23c36dd24 173 tx[0] = BNO055_LINEAR_ACCEL_DATA_X_LSB_ADDR;
vargham 7:f5880506defe 174 _i2c.write(address,tx,1,true);
vargham 7:f5880506defe 175 _i2c.read(address+1,rawdata,6,0);
StressedDave 0:24f23c36dd24 176 lia.rawx = (rawdata[1] << 8 | rawdata[0]);
StressedDave 0:24f23c36dd24 177 lia.rawy = (rawdata[3] << 8 | rawdata[2]);
StressedDave 0:24f23c36dd24 178 lia.rawz = (rawdata[5] << 8 | rawdata[4]);
StressedDave 0:24f23c36dd24 179 lia.x = float(lia.rawx)*accel_scale;
StressedDave 0:24f23c36dd24 180 lia.y = float(lia.rawy)*accel_scale;
StressedDave 0:24f23c36dd24 181 lia.z = float(lia.rawz)*accel_scale;
StressedDave 0:24f23c36dd24 182 }
StressedDave 0:24f23c36dd24 183
StressedDave 0:24f23c36dd24 184 void BNO055::get_grv(void){
StressedDave 0:24f23c36dd24 185 tx[0] = BNO055_GRAVITY_DATA_X_LSB_ADDR;
vargham 7:f5880506defe 186 _i2c.write(address,tx,1,true);
vargham 7:f5880506defe 187 _i2c.read(address+1,rawdata,6,0);
StressedDave 0:24f23c36dd24 188 gravity.rawx = (rawdata[1] << 8 | rawdata[0]);
StressedDave 0:24f23c36dd24 189 gravity.rawy = (rawdata[3] << 8 | rawdata[2]);
StressedDave 0:24f23c36dd24 190 gravity.rawz = (rawdata[5] << 8 | rawdata[4]);
StressedDave 0:24f23c36dd24 191 gravity.x = float(gravity.rawx)*accel_scale;
StressedDave 0:24f23c36dd24 192 gravity.y = float(gravity.rawy)*accel_scale;
StressedDave 0:24f23c36dd24 193 gravity.z = float(gravity.rawz)*accel_scale;
StressedDave 0:24f23c36dd24 194 }
StressedDave 0:24f23c36dd24 195
StressedDave 1:2c3322a8d417 196 void BNO055::get_quat(void){
StressedDave 1:2c3322a8d417 197 tx[0] = BNO055_QUATERNION_DATA_W_LSB_ADDR;
vargham 7:f5880506defe 198 _i2c.write(address,tx,1,true);
vargham 7:f5880506defe 199 _i2c.read(address+1,rawdata,8,0);
StressedDave 1:2c3322a8d417 200 quat.raww = (rawdata[1] << 8 | rawdata[0]);
StressedDave 1:2c3322a8d417 201 quat.rawx = (rawdata[3] << 8 | rawdata[2]);
StressedDave 1:2c3322a8d417 202 quat.rawy = (rawdata[5] << 8 | rawdata[4]);
StressedDave 1:2c3322a8d417 203 quat.rawz = (rawdata[7] << 8 | rawdata[6]);
StressedDave 1:2c3322a8d417 204 quat.w = float(quat.raww)/16384.0f;
StressedDave 1:2c3322a8d417 205 quat.x = float(quat.rawx)/16384.0f;
StressedDave 1:2c3322a8d417 206 quat.y = float(quat.rawy)/16384.0f;
StressedDave 1:2c3322a8d417 207 quat.z = float(quat.rawz)/16384.0f;
StressedDave 1:2c3322a8d417 208 }
StressedDave 1:2c3322a8d417 209
StressedDave 2:695c6e5d239a 210 void BNO055::get_angles(void){
StressedDave 2:695c6e5d239a 211 tx[0] = BNO055_EULER_H_LSB_ADDR;
vargham 7:f5880506defe 212 _i2c.write(address,tx,1,true);
vargham 7:f5880506defe 213 _i2c.read(address+1,rawdata,6,0);
StressedDave 3:1db1628eb8b2 214 euler.rawyaw = (rawdata[1] << 8 | rawdata[0]);
StressedDave 3:1db1628eb8b2 215 euler.rawroll = (rawdata[3] << 8 | rawdata[2]);
StressedDave 3:1db1628eb8b2 216 euler.rawpitch = (rawdata[5] << 8 | rawdata[4]);
StressedDave 3:1db1628eb8b2 217 euler.yaw = float(euler.rawyaw)*angle_scale;
StressedDave 3:1db1628eb8b2 218 euler.roll = float(euler.rawroll)*angle_scale;
StressedDave 3:1db1628eb8b2 219 euler.pitch = float(euler.rawpitch)*angle_scale;
StressedDave 2:695c6e5d239a 220 }
StressedDave 2:695c6e5d239a 221
StressedDave 2:695c6e5d239a 222
StressedDave 1:2c3322a8d417 223 void BNO055::get_temp(void){
StressedDave 1:2c3322a8d417 224 readchar(BNO055_TEMP_ADDR);
StressedDave 1:2c3322a8d417 225 temperature = rx / temp_scale;
StressedDave 1:2c3322a8d417 226 }
StressedDave 1:2c3322a8d417 227
StressedDave 0:24f23c36dd24 228 void BNO055::get_calib(void){
StressedDave 0:24f23c36dd24 229 readchar(BNO055_CALIB_STAT_ADDR);
StressedDave 0:24f23c36dd24 230 calib = rx;
StressedDave 1:2c3322a8d417 231 }
StressedDave 1:2c3322a8d417 232
StressedDave 1:2c3322a8d417 233 void BNO055::read_calibration_data(void){
StressedDave 1:2c3322a8d417 234 char tempmode = op_mode;
StressedDave 1:2c3322a8d417 235 setmode(OPERATION_MODE_CONFIG);
StressedDave 1:2c3322a8d417 236 wait_ms(20);
StressedDave 1:2c3322a8d417 237 tx[0] = ACCEL_OFFSET_X_LSB_ADDR;
vargham 7:f5880506defe 238 _i2c.write(address,tx,1,true);
vargham 7:f5880506defe 239 _i2c.read(address,calibration,22,false);
StressedDave 1:2c3322a8d417 240 setmode(tempmode);
StressedDave 1:2c3322a8d417 241 wait_ms(10);
StressedDave 1:2c3322a8d417 242 }
StressedDave 1:2c3322a8d417 243
StressedDave 1:2c3322a8d417 244 void BNO055::write_calibration_data(void){
StressedDave 1:2c3322a8d417 245 char tempmode = op_mode;
StressedDave 1:2c3322a8d417 246 setmode(OPERATION_MODE_CONFIG);
StressedDave 1:2c3322a8d417 247 wait_ms(20);
StressedDave 1:2c3322a8d417 248 tx[0] = ACCEL_OFFSET_X_LSB_ADDR;
vargham 7:f5880506defe 249 _i2c.write(address,tx,1,true);
vargham 7:f5880506defe 250 _i2c.write(address,calibration,22,false);
StressedDave 1:2c3322a8d417 251 setmode(tempmode);
StressedDave 1:2c3322a8d417 252 wait_ms(10);
StressedDave 1:2c3322a8d417 253 }
StressedDave 1:2c3322a8d417 254
StressedDave 1:2c3322a8d417 255 void BNO055::set_mapping(char orient){
StressedDave 1:2c3322a8d417 256 switch (orient){
vargham 7:f5880506defe 257 case 0:
StressedDave 1:2c3322a8d417 258 writechar(BNO055_AXIS_MAP_CONFIG_ADDR,0x21);
StressedDave 1:2c3322a8d417 259 writechar(BNO055_AXIS_MAP_SIGN_ADDR,0x04);
StressedDave 1:2c3322a8d417 260 break;
StressedDave 1:2c3322a8d417 261 case 1:
StressedDave 1:2c3322a8d417 262 writechar(BNO055_AXIS_MAP_CONFIG_ADDR,0x24);
StressedDave 1:2c3322a8d417 263 writechar(BNO055_AXIS_MAP_SIGN_ADDR,0x00);
StressedDave 1:2c3322a8d417 264 break;
StressedDave 1:2c3322a8d417 265 case 2:
StressedDave 1:2c3322a8d417 266 writechar(BNO055_AXIS_MAP_CONFIG_ADDR,0x24);
StressedDave 1:2c3322a8d417 267 writechar(BNO055_AXIS_MAP_SIGN_ADDR,0x00);
StressedDave 1:2c3322a8d417 268 break;
StressedDave 1:2c3322a8d417 269 case 3:
StressedDave 1:2c3322a8d417 270 writechar(BNO055_AXIS_MAP_CONFIG_ADDR,0x21);
StressedDave 1:2c3322a8d417 271 writechar(BNO055_AXIS_MAP_SIGN_ADDR,0x02);
StressedDave 1:2c3322a8d417 272 break;
StressedDave 1:2c3322a8d417 273 case 4:
StressedDave 1:2c3322a8d417 274 writechar(BNO055_AXIS_MAP_CONFIG_ADDR,0x24);
StressedDave 1:2c3322a8d417 275 writechar(BNO055_AXIS_MAP_SIGN_ADDR,0x03);
StressedDave 1:2c3322a8d417 276 break;
StressedDave 1:2c3322a8d417 277 case 5:
StressedDave 1:2c3322a8d417 278 writechar(BNO055_AXIS_MAP_CONFIG_ADDR,0x21);
StressedDave 1:2c3322a8d417 279 writechar(BNO055_AXIS_MAP_SIGN_ADDR,0x01);
StressedDave 1:2c3322a8d417 280 break;
StressedDave 1:2c3322a8d417 281 case 6:
StressedDave 1:2c3322a8d417 282 writechar(BNO055_AXIS_MAP_CONFIG_ADDR,0x21);
StressedDave 1:2c3322a8d417 283 writechar(BNO055_AXIS_MAP_SIGN_ADDR,0x07);
StressedDave 1:2c3322a8d417 284 break;
StressedDave 1:2c3322a8d417 285 case 7:
StressedDave 1:2c3322a8d417 286 writechar(BNO055_AXIS_MAP_CONFIG_ADDR,0x24);
StressedDave 1:2c3322a8d417 287 writechar(BNO055_AXIS_MAP_SIGN_ADDR,0x05);
StressedDave 1:2c3322a8d417 288 break;
StressedDave 1:2c3322a8d417 289 default:
StressedDave 1:2c3322a8d417 290 writechar(BNO055_AXIS_MAP_CONFIG_ADDR,0x24);
StressedDave 1:2c3322a8d417 291 writechar(BNO055_AXIS_MAP_SIGN_ADDR,0x00);
StressedDave 1:2c3322a8d417 292 }
vargham 7:f5880506defe 293 }