test

Dependencies:   mbed

Fork of BNO055_HelloWorld by Dave Turner

Committer:
SES01
Date:
Tue May 22 11:29:05 2018 +0000
Revision:
1:b178e785986d
test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SES01 1:b178e785986d 1 #ifndef BNO055_H
SES01 1:b178e785986d 2 #define BNO055_H
SES01 1:b178e785986d 3
SES01 1:b178e785986d 4 #include "mbed.h"
SES01 1:b178e785986d 5 //
SES01 1:b178e785986d 6 #define BNOAddress (0x28 << 1)
SES01 1:b178e785986d 7 //Register definitions
SES01 1:b178e785986d 8 /* Page id register definition */
SES01 1:b178e785986d 9 #define BNO055_PAGE_ID_ADDR 0x07
SES01 1:b178e785986d 10 /* PAGE0 REGISTER DEFINITION START*/
SES01 1:b178e785986d 11 #define BNO055_CHIP_ID_ADDR 0x00
SES01 1:b178e785986d 12 #define BNO055_ACCEL_REV_ID_ADDR 0x01
SES01 1:b178e785986d 13 #define BNO055_MAG_REV_ID_ADDR 0x02
SES01 1:b178e785986d 14 #define BNO055_GYRO_REV_ID_ADDR 0x03
SES01 1:b178e785986d 15 #define BNO055_SW_REV_ID_LSB_ADDR 0x04
SES01 1:b178e785986d 16 #define BNO055_SW_REV_ID_MSB_ADDR 0x05
SES01 1:b178e785986d 17 #define BNO055_BL_REV_ID_ADDR 0x06
SES01 1:b178e785986d 18 /* Accel data register */
SES01 1:b178e785986d 19 #define BNO055_ACCEL_DATA_X_LSB_ADDR 0x08
SES01 1:b178e785986d 20 #define BNO055_ACCEL_DATA_X_MSB_ADDR 0x09
SES01 1:b178e785986d 21 #define BNO055_ACCEL_DATA_Y_LSB_ADDR 0x0A
SES01 1:b178e785986d 22 #define BNO055_ACCEL_DATA_Y_MSB_ADDR 0x0B
SES01 1:b178e785986d 23 #define BNO055_ACCEL_DATA_Z_LSB_ADDR 0x0C
SES01 1:b178e785986d 24 #define BNO055_ACCEL_DATA_Z_MSB_ADDR 0x0D
SES01 1:b178e785986d 25 /* Mag data register */
SES01 1:b178e785986d 26 #define BNO055_MAG_DATA_X_LSB_ADDR 0x0E
SES01 1:b178e785986d 27 #define BNO055_MAG_DATA_X_MSB_ADDR 0x0F
SES01 1:b178e785986d 28 #define BNO055_MAG_DATA_Y_LSB_ADDR 0x10
SES01 1:b178e785986d 29 #define BNO055_MAG_DATA_Y_MSB_ADDR 0x11
SES01 1:b178e785986d 30 #define BNO055_MAG_DATA_Z_LSB_ADDR 0x12
SES01 1:b178e785986d 31 #define BNO055_MAG_DATA_Z_MSB_ADDR 0x13
SES01 1:b178e785986d 32 /* Gyro data registers */
SES01 1:b178e785986d 33 #define BNO055_GYRO_DATA_X_LSB_ADDR 0x14
SES01 1:b178e785986d 34 #define BNO055_GYRO_DATA_X_MSB_ADDR 0x15
SES01 1:b178e785986d 35 #define BNO055_GYRO_DATA_Y_LSB_ADDR 0x16
SES01 1:b178e785986d 36 #define BNO055_GYRO_DATA_Y_MSB_ADDR 0x17
SES01 1:b178e785986d 37 #define BNO055_GYRO_DATA_Z_LSB_ADDR 0x18
SES01 1:b178e785986d 38 #define BNO055_GYRO_DATA_Z_MSB_ADDR 0x19
SES01 1:b178e785986d 39 /* Euler data registers */
SES01 1:b178e785986d 40 #define BNO055_EULER_H_LSB_ADDR 0x1A
SES01 1:b178e785986d 41 #define BNO055_EULER_H_MSB_ADDR 0x1B
SES01 1:b178e785986d 42 #define BNO055_EULER_R_LSB_ADDR 0x1C
SES01 1:b178e785986d 43 #define BNO055_EULER_R_MSB_ADDR 0x1D
SES01 1:b178e785986d 44 #define BNO055_EULER_P_LSB_ADDR 0x1E
SES01 1:b178e785986d 45 #define BNO055_EULER_P_MSB_ADDR 0x1F
SES01 1:b178e785986d 46 /* Quaternion data registers */
SES01 1:b178e785986d 47 #define BNO055_QUATERNION_DATA_W_LSB_ADDR 0x20
SES01 1:b178e785986d 48 #define BNO055_QUATERNION_DATA_W_MSB_ADDR 0x21
SES01 1:b178e785986d 49 #define BNO055_QUATERNION_DATA_X_LSB_ADDR 0x22
SES01 1:b178e785986d 50 #define BNO055_QUATERNION_DATA_X_MSB_ADDR 0x23
SES01 1:b178e785986d 51 #define BNO055_QUATERNION_DATA_Y_LSB_ADDR 0x24
SES01 1:b178e785986d 52 #define BNO055_QUATERNION_DATA_Y_MSB_ADDR 0x25
SES01 1:b178e785986d 53 #define BNO055_QUATERNION_DATA_Z_LSB_ADDR 0x26
SES01 1:b178e785986d 54 #define BNO055_QUATERNION_DATA_Z_MSB_ADDR 0x27
SES01 1:b178e785986d 55 /* Linear acceleration data registers */
SES01 1:b178e785986d 56 #define BNO055_LINEAR_ACCEL_DATA_X_LSB_ADDR 0x28
SES01 1:b178e785986d 57 #define BNO055_LINEAR_ACCEL_DATA_X_MSB_ADDR 0x29
SES01 1:b178e785986d 58 #define BNO055_LINEAR_ACCEL_DATA_Y_LSB_ADDR 0x2A
SES01 1:b178e785986d 59 #define BNO055_LINEAR_ACCEL_DATA_Y_MSB_ADDR 0x2B
SES01 1:b178e785986d 60 #define BNO055_LINEAR_ACCEL_DATA_Z_LSB_ADDR 0x2C
SES01 1:b178e785986d 61 #define BNO055_LINEAR_ACCEL_DATA_Z_MSB_ADDR 0x2D
SES01 1:b178e785986d 62 /* Gravity data registers */
SES01 1:b178e785986d 63 #define BNO055_GRAVITY_DATA_X_LSB_ADDR 0x2E
SES01 1:b178e785986d 64 #define BNO055_GRAVITY_DATA_X_MSB_ADDR 0x2F
SES01 1:b178e785986d 65 #define BNO055_GRAVITY_DATA_Y_LSB_ADDR 0x30
SES01 1:b178e785986d 66 #define BNO055_GRAVITY_DATA_Y_MSB_ADDR 0x31
SES01 1:b178e785986d 67 #define BNO055_GRAVITY_DATA_Z_LSB_ADDR 0x32
SES01 1:b178e785986d 68 #define BNO055_GRAVITY_DATA_Z_MSB_ADDR 0x33
SES01 1:b178e785986d 69 /* Temperature data register */
SES01 1:b178e785986d 70 #define BNO055_TEMP_ADDR 0x34
SES01 1:b178e785986d 71 /* Status registers */
SES01 1:b178e785986d 72 #define BNO055_CALIB_STAT_ADDR 0x35
SES01 1:b178e785986d 73 #define BNO055_SELFTEST_RESULT_ADDR 0x36
SES01 1:b178e785986d 74 #define BNO055_INTR_STAT_ADDR 0x37
SES01 1:b178e785986d 75 #define BNO055_SYS_CLK_STAT_ADDR 0x38
SES01 1:b178e785986d 76 #define BNO055_SYS_STAT_ADDR 0x39
SES01 1:b178e785986d 77 #define BNO055_SYS_ERR_ADDR 0x3A
SES01 1:b178e785986d 78 /* Unit selection register */
SES01 1:b178e785986d 79 #define BNO055_UNIT_SEL_ADDR 0x3B
SES01 1:b178e785986d 80 #define BNO055_DATA_SELECT_ADDR 0x3C
SES01 1:b178e785986d 81 /* Mode registers */
SES01 1:b178e785986d 82 #define BNO055_OPR_MODE_ADDR 0x3D
SES01 1:b178e785986d 83 #define BNO055_PWR_MODE_ADDR 0x3E
SES01 1:b178e785986d 84 #define BNO055_SYS_TRIGGER_ADDR 0x3F
SES01 1:b178e785986d 85 #define BNO055_TEMP_SOURCE_ADDR 0x40
SES01 1:b178e785986d 86 /* Axis remap registers */
SES01 1:b178e785986d 87 #define BNO055_AXIS_MAP_CONFIG_ADDR 0x41
SES01 1:b178e785986d 88 #define BNO055_AXIS_MAP_SIGN_ADDR 0x42
SES01 1:b178e785986d 89 /* Accelerometer Offset registers */
SES01 1:b178e785986d 90 #define ACCEL_OFFSET_X_LSB_ADDR 0x55
SES01 1:b178e785986d 91 #define ACCEL_OFFSET_X_MSB_ADDR 0x56
SES01 1:b178e785986d 92 #define ACCEL_OFFSET_Y_LSB_ADDR 0x57
SES01 1:b178e785986d 93 #define ACCEL_OFFSET_Y_MSB_ADDR 0x58
SES01 1:b178e785986d 94 #define ACCEL_OFFSET_Z_LSB_ADDR 0x59
SES01 1:b178e785986d 95 #define ACCEL_OFFSET_Z_MSB_ADDR 0x5A
SES01 1:b178e785986d 96 /* Magnetometer Offset registers */
SES01 1:b178e785986d 97 #define MAG_OFFSET_X_LSB_ADDR 0x5B
SES01 1:b178e785986d 98 #define MAG_OFFSET_X_MSB_ADDR 0x5C
SES01 1:b178e785986d 99 #define MAG_OFFSET_Y_LSB_ADDR 0x5D
SES01 1:b178e785986d 100 #define MAG_OFFSET_Y_MSB_ADDR 0x5E
SES01 1:b178e785986d 101 #define MAG_OFFSET_Z_LSB_ADDR 0x5F
SES01 1:b178e785986d 102 #define MAG_OFFSET_Z_MSB_ADDR 0x60
SES01 1:b178e785986d 103 /* Gyroscope Offset registers*/
SES01 1:b178e785986d 104 #define GYRO_OFFSET_X_LSB_ADDR 0x61
SES01 1:b178e785986d 105 #define GYRO_OFFSET_X_MSB_ADDR 0x62
SES01 1:b178e785986d 106 #define GYRO_OFFSET_Y_LSB_ADDR 0x63
SES01 1:b178e785986d 107 #define GYRO_OFFSET_Y_MSB_ADDR 0x64
SES01 1:b178e785986d 108 #define GYRO_OFFSET_Z_LSB_ADDR 0x65
SES01 1:b178e785986d 109 #define GYRO_OFFSET_Z_MSB_ADDR 0x66
SES01 1:b178e785986d 110 /* Radius registers */
SES01 1:b178e785986d 111 #define ACCEL_RADIUS_LSB_ADDR 0x67
SES01 1:b178e785986d 112 #define ACCEL_RADIUS_MSB_ADDR 0x68
SES01 1:b178e785986d 113 #define MAG_RADIUS_LSB_ADDR 0x69
SES01 1:b178e785986d 114 #define MAG_RADIUS_MSB_ADDR 0x6A
SES01 1:b178e785986d 115
SES01 1:b178e785986d 116 /* Page 1 registers */
SES01 1:b178e785986d 117 #define BNO055_UNIQUE_ID_ADDR 0x50
SES01 1:b178e785986d 118
SES01 1:b178e785986d 119 //Definitions for unit selection
SES01 1:b178e785986d 120 #define MPERSPERS 0x00
SES01 1:b178e785986d 121 #define MILLIG 0x01
SES01 1:b178e785986d 122 #define DEG_PER_SEC 0x00
SES01 1:b178e785986d 123 #define RAD_PER_SEC 0x02
SES01 1:b178e785986d 124 #define DEGREES 0x00
SES01 1:b178e785986d 125 #define RADIANS 0x04
SES01 1:b178e785986d 126 #define CENTIGRADE 0x00
SES01 1:b178e785986d 127 #define FAHRENHEIT 0x10
SES01 1:b178e785986d 128 #define WINDOWS 0x00
SES01 1:b178e785986d 129 #define ANDROID 0x80
SES01 1:b178e785986d 130
SES01 1:b178e785986d 131 //Definitions for power mode
SES01 1:b178e785986d 132 #define POWER_MODE_NORMAL 0x00
SES01 1:b178e785986d 133 #define POWER_MODE_LOWPOWER 0x01
SES01 1:b178e785986d 134 #define POWER_MODE_SUSPEND 0x02
SES01 1:b178e785986d 135
SES01 1:b178e785986d 136 //Definitions for operating mode
SES01 1:b178e785986d 137 #define OPERATION_MODE_CONFIG 0x00
SES01 1:b178e785986d 138 #define OPERATION_MODE_ACCONLY 0x01
SES01 1:b178e785986d 139 #define OPERATION_MODE_MAGONLY 0x02
SES01 1:b178e785986d 140 #define OPERATION_MODE_GYRONLY 0x03
SES01 1:b178e785986d 141 #define OPERATION_MODE_ACCMAG 0x04
SES01 1:b178e785986d 142 #define OPERATION_MODE_ACCGYRO 0x05
SES01 1:b178e785986d 143 #define OPERATION_MODE_MAGGYRO 0x06
SES01 1:b178e785986d 144 #define OPERATION_MODE_AMG 0x07
SES01 1:b178e785986d 145 #define OPERATION_MODE_IMUPLUS 0x08
SES01 1:b178e785986d 146 #define OPERATION_MODE_COMPASS 0x09
SES01 1:b178e785986d 147 #define OPERATION_MODE_M4G 0x0A
SES01 1:b178e785986d 148 #define OPERATION_MODE_NDOF_FMC_OFF 0x0B
SES01 1:b178e785986d 149 #define OPERATION_MODE_NDOF 0x0C
SES01 1:b178e785986d 150
SES01 1:b178e785986d 151 typedef struct values{
SES01 1:b178e785986d 152 int16_t rawx,rawy,rawz;
SES01 1:b178e785986d 153 float x,y,z;
SES01 1:b178e785986d 154 }values;
SES01 1:b178e785986d 155
SES01 1:b178e785986d 156 typedef struct angles{
SES01 1:b178e785986d 157 int16_t rawroll,rawpitch,rawyaw;
SES01 1:b178e785986d 158 float roll, pitch, yaw;
SES01 1:b178e785986d 159 } angles;
SES01 1:b178e785986d 160
SES01 1:b178e785986d 161 typedef struct quaternion{
SES01 1:b178e785986d 162 int16_t raww,rawx,rawy,rawz;
SES01 1:b178e785986d 163 float w,x,y,z;
SES01 1:b178e785986d 164 }quaternion;
SES01 1:b178e785986d 165
SES01 1:b178e785986d 166 typedef struct chip{
SES01 1:b178e785986d 167 char id;
SES01 1:b178e785986d 168 char accel;
SES01 1:b178e785986d 169 char gyro;
SES01 1:b178e785986d 170 char mag;
SES01 1:b178e785986d 171 char sw[2];
SES01 1:b178e785986d 172 char bootload;
SES01 1:b178e785986d 173 char serial[16];
SES01 1:b178e785986d 174 }chip;
SES01 1:b178e785986d 175
SES01 1:b178e785986d 176 /** Class for operating Bosch BNO055 sensor over I2C **/
SES01 1:b178e785986d 177 class BNO055
SES01 1:b178e785986d 178 {
SES01 1:b178e785986d 179 public:
SES01 1:b178e785986d 180
SES01 1:b178e785986d 181 /** Create BNO055 instance **/
SES01 1:b178e785986d 182 BNO055(PinName SDA, PinName SCL);
SES01 1:b178e785986d 183
SES01 1:b178e785986d 184 /** Perform a power-on reset of the BNO055 **/
SES01 1:b178e785986d 185 void reset();
SES01 1:b178e785986d 186 /** Check that the BNO055 is connected and download the software details
SES01 1:b178e785986d 187 and serial number of chip and store in ID structure **/
SES01 1:b178e785986d 188 bool check();
SES01 1:b178e785986d 189 /** Turn the external timing crystal on/off **/
SES01 1:b178e785986d 190 void SetExternalCrystal(bool yn);
SES01 1:b178e785986d 191 /** Set the operation mode of the sensor **/
SES01 1:b178e785986d 192 void setmode(char mode);
SES01 1:b178e785986d 193 /** Set the power mode of the sensor **/
SES01 1:b178e785986d 194 void setpowermode(char mode);
SES01 1:b178e785986d 195
SES01 1:b178e785986d 196 /** Set the output units from the accelerometer, either MPERSPERS or MILLIG **/
SES01 1:b178e785986d 197 void set_accel_units(char units);
SES01 1:b178e785986d 198 /** Set the output units from the gyroscope, either DEG_PER_SEC or RAD_PER_SEC **/
SES01 1:b178e785986d 199 void set_anglerate_units(char units);
SES01 1:b178e785986d 200 /** Set the output units from the IMU, either DEGREES or RADIANS **/
SES01 1:b178e785986d 201 void set_angle_units(char units);
SES01 1:b178e785986d 202 /** Set the output units from the temperature sensor, either CENTIGRADE or FAHRENHEIT **/
SES01 1:b178e785986d 203 void set_temp_units(char units);
SES01 1:b178e785986d 204 /** Set the data output format to either WINDOWS or ANDROID **/
SES01 1:b178e785986d 205 void set_orientation(char units);
SES01 1:b178e785986d 206 /** Set the mapping of the exes/directions as per page 25 of datasheet
SES01 1:b178e785986d 207 range 0-7, any value outside this will set the orientation to P1 (default at power up) **/
SES01 1:b178e785986d 208 void set_mapping(char orient);
SES01 1:b178e785986d 209
SES01 1:b178e785986d 210 /** Get the current values from the accelerometer **/
SES01 1:b178e785986d 211 void get_accel(void);
SES01 1:b178e785986d 212 /** Get the current values from the gyroscope **/
SES01 1:b178e785986d 213 void get_gyro(void);
SES01 1:b178e785986d 214 /** Get the current values from the magnetometer **/
SES01 1:b178e785986d 215 void get_mag(void);
SES01 1:b178e785986d 216 /** Get the corrected linear acceleration **/
SES01 1:b178e785986d 217 void get_lia(void);
SES01 1:b178e785986d 218 /** Get the current gravity vector **/
SES01 1:b178e785986d 219 void get_grv(void);
SES01 1:b178e785986d 220 /** Get the output quaternion **/
SES01 1:b178e785986d 221 void get_quat(void);
SES01 1:b178e785986d 222 /** Get the current Euler angles **/
SES01 1:b178e785986d 223 void get_angles(void);
SES01 1:b178e785986d 224 /** Get the current temperature **/
SES01 1:b178e785986d 225 void get_temp(void);
SES01 1:b178e785986d 226
SES01 1:b178e785986d 227 /** Read the calibration status register and store the result in the calib variable **/
SES01 1:b178e785986d 228 void get_calib(void);
SES01 1:b178e785986d 229 /** Read the offset and radius values into the calibration array**/
SES01 1:b178e785986d 230 void read_calibration_data(void);
SES01 1:b178e785986d 231 /** Write the contents of the calibration array into the registers **/
SES01 1:b178e785986d 232 void write_calibration_data(void);
SES01 1:b178e785986d 233
SES01 1:b178e785986d 234 /** Structures containing 3-axis data for acceleration, rate of turn and magnetic field.
SES01 1:b178e785986d 235 x,y,z are the scale floating point values and
SES01 1:b178e785986d 236 rawx, rawy, rawz are the int16_t values read from the sensors **/
SES01 1:b178e785986d 237 values accel,gyro,mag,lia,gravity;
SES01 1:b178e785986d 238 /** Stucture containing the Euler angles as yaw, pitch, roll as scaled floating point
SES01 1:b178e785986d 239 and rawyaw, rawroll & rollpitch as the int16_t values loaded from the registers **/
SES01 1:b178e785986d 240 angles euler;
SES01 1:b178e785986d 241 /** Quaternion values as w,x,y,z (scaled floating point) and raww etc... as int16_t loaded from the
SES01 1:b178e785986d 242 registers **/
SES01 1:b178e785986d 243 quaternion quat;
SES01 1:b178e785986d 244
SES01 1:b178e785986d 245 /** Current contents of calibration status register **/
SES01 1:b178e785986d 246 char calib;
SES01 1:b178e785986d 247 /** Contents of the 22 registers containing offset and radius values used as calibration by the sensor **/
SES01 1:b178e785986d 248 char calibration[22];
SES01 1:b178e785986d 249 /** Structure containing sensor numbers, software version and chip UID **/
SES01 1:b178e785986d 250 chip ID;
SES01 1:b178e785986d 251 /** Current temperature **/
SES01 1:b178e785986d 252 int temperature;
SES01 1:b178e785986d 253
SES01 1:b178e785986d 254 private:
SES01 1:b178e785986d 255
SES01 1:b178e785986d 256 I2C _i2c;
SES01 1:b178e785986d 257 char rx,tx[2],address; //I2C variables
SES01 1:b178e785986d 258 char rawdata[22]; //Temporary array for input data values
SES01 1:b178e785986d 259 char op_mode;
SES01 1:b178e785986d 260 char pwr_mode;
SES01 1:b178e785986d 261 float accel_scale,rate_scale,angle_scale;
SES01 1:b178e785986d 262 int temp_scale;
SES01 1:b178e785986d 263
SES01 1:b178e785986d 264 void readchar(char location){
SES01 1:b178e785986d 265 tx[0] = location;
SES01 1:b178e785986d 266 _i2c.write(address,tx,1,true);
SES01 1:b178e785986d 267 _i2c.read(address,&rx,1,false);
SES01 1:b178e785986d 268 }
SES01 1:b178e785986d 269
SES01 1:b178e785986d 270 void writechar(char location, char value){
SES01 1:b178e785986d 271 tx[0] = location;
SES01 1:b178e785986d 272 tx[1] = value;
SES01 1:b178e785986d 273 _i2c.write(address,tx,2);
SES01 1:b178e785986d 274 }
SES01 1:b178e785986d 275
SES01 1:b178e785986d 276 void setpage(char value){
SES01 1:b178e785986d 277 writechar(BNO055_PAGE_ID_ADDR,value);
SES01 1:b178e785986d 278 }
SES01 1:b178e785986d 279 };
SES01 1:b178e785986d 280 #endif