Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: Orion_newPCB_test_LV Orion_PCB_test_Faulhaber_gr41_wptcmd_V1
Fork of BNO055 by
BNO055.h
00001 #ifndef BNO055_H 00002 #define BNO055_H 00003 00004 #include "mbed.h" 00005 // 00006 //#define BNOAddress (0x28 << 1) 00007 #define BNOAddress (0x29 << 1) 00008 //Register definitions 00009 /* Page id register definition */ 00010 #define BNO055_PAGE_ID_ADDR 0x07 00011 /* PAGE0 REGISTER DEFINITION START*/ 00012 #define BNO055_CHIP_ID_ADDR 0x00 00013 #define BNO055_ACCEL_REV_ID_ADDR 0x01 00014 #define BNO055_MAG_REV_ID_ADDR 0x02 00015 #define BNO055_GYRO_REV_ID_ADDR 0x03 00016 #define BNO055_SW_REV_ID_LSB_ADDR 0x04 00017 #define BNO055_SW_REV_ID_MSB_ADDR 0x05 00018 #define BNO055_BL_REV_ID_ADDR 0x06 00019 /* Accel data register */ 00020 #define BNO055_ACCEL_DATA_X_LSB_ADDR 0x08 00021 #define BNO055_ACCEL_DATA_X_MSB_ADDR 0x09 00022 #define BNO055_ACCEL_DATA_Y_LSB_ADDR 0x0A 00023 #define BNO055_ACCEL_DATA_Y_MSB_ADDR 0x0B 00024 #define BNO055_ACCEL_DATA_Z_LSB_ADDR 0x0C 00025 #define BNO055_ACCEL_DATA_Z_MSB_ADDR 0x0D 00026 /* Mag data register */ 00027 #define BNO055_MAG_DATA_X_LSB_ADDR 0x0E 00028 #define BNO055_MAG_DATA_X_MSB_ADDR 0x0F 00029 #define BNO055_MAG_DATA_Y_LSB_ADDR 0x10 00030 #define BNO055_MAG_DATA_Y_MSB_ADDR 0x11 00031 #define BNO055_MAG_DATA_Z_LSB_ADDR 0x12 00032 #define BNO055_MAG_DATA_Z_MSB_ADDR 0x13 00033 /* Gyro data registers */ 00034 #define BNO055_GYRO_DATA_X_LSB_ADDR 0x14 00035 #define BNO055_GYRO_DATA_X_MSB_ADDR 0x15 00036 #define BNO055_GYRO_DATA_Y_LSB_ADDR 0x16 00037 #define BNO055_GYRO_DATA_Y_MSB_ADDR 0x17 00038 #define BNO055_GYRO_DATA_Z_LSB_ADDR 0x18 00039 #define BNO055_GYRO_DATA_Z_MSB_ADDR 0x19 00040 /* Euler data registers */ 00041 #define BNO055_EULER_H_LSB_ADDR 0x1A 00042 #define BNO055_EULER_H_MSB_ADDR 0x1B 00043 #define BNO055_EULER_R_LSB_ADDR 0x1C 00044 #define BNO055_EULER_R_MSB_ADDR 0x1D 00045 #define BNO055_EULER_P_LSB_ADDR 0x1E 00046 #define BNO055_EULER_P_MSB_ADDR 0x1F 00047 /* Quaternion data registers */ 00048 #define BNO055_QUATERNION_DATA_W_LSB_ADDR 0x20 00049 #define BNO055_QUATERNION_DATA_W_MSB_ADDR 0x21 00050 #define BNO055_QUATERNION_DATA_X_LSB_ADDR 0x22 00051 #define BNO055_QUATERNION_DATA_X_MSB_ADDR 0x23 00052 #define BNO055_QUATERNION_DATA_Y_LSB_ADDR 0x24 00053 #define BNO055_QUATERNION_DATA_Y_MSB_ADDR 0x25 00054 #define BNO055_QUATERNION_DATA_Z_LSB_ADDR 0x26 00055 #define BNO055_QUATERNION_DATA_Z_MSB_ADDR 0x27 00056 /* Linear acceleration data registers */ 00057 #define BNO055_LINEAR_ACCEL_DATA_X_LSB_ADDR 0x28 00058 #define BNO055_LINEAR_ACCEL_DATA_X_MSB_ADDR 0x29 00059 #define BNO055_LINEAR_ACCEL_DATA_Y_LSB_ADDR 0x2A 00060 #define BNO055_LINEAR_ACCEL_DATA_Y_MSB_ADDR 0x2B 00061 #define BNO055_LINEAR_ACCEL_DATA_Z_LSB_ADDR 0x2C 00062 #define BNO055_LINEAR_ACCEL_DATA_Z_MSB_ADDR 0x2D 00063 /* Gravity data registers */ 00064 #define BNO055_GRAVITY_DATA_X_LSB_ADDR 0x2E 00065 #define BNO055_GRAVITY_DATA_X_MSB_ADDR 0x2F 00066 #define BNO055_GRAVITY_DATA_Y_LSB_ADDR 0x30 00067 #define BNO055_GRAVITY_DATA_Y_MSB_ADDR 0x31 00068 #define BNO055_GRAVITY_DATA_Z_LSB_ADDR 0x32 00069 #define BNO055_GRAVITY_DATA_Z_MSB_ADDR 0x33 00070 /* Temperature data register */ 00071 #define BNO055_TEMP_ADDR 0x34 00072 /* Status registers */ 00073 #define BNO055_CALIB_STAT_ADDR 0x35 00074 #define BNO055_SELFTEST_RESULT_ADDR 0x36 00075 #define BNO055_INTR_STAT_ADDR 0x37 00076 #define BNO055_SYS_CLK_STAT_ADDR 0x38 00077 #define BNO055_SYS_STAT_ADDR 0x39 00078 #define BNO055_SYS_ERR_ADDR 0x3A 00079 /* Unit selection register */ 00080 #define BNO055_UNIT_SEL_ADDR 0x3B 00081 #define BNO055_DATA_SELECT_ADDR 0x3C 00082 /* Mode registers */ 00083 #define BNO055_OPR_MODE_ADDR 0x3D 00084 #define BNO055_PWR_MODE_ADDR 0x3E 00085 #define BNO055_SYS_TRIGGER_ADDR 0x3F 00086 #define BNO055_TEMP_SOURCE_ADDR 0x40 00087 /* Axis remap registers */ 00088 #define BNO055_AXIS_MAP_CONFIG_ADDR 0x41 00089 #define BNO055_AXIS_MAP_SIGN_ADDR 0x42 00090 /* Accelerometer Offset registers */ 00091 #define ACCEL_OFFSET_X_LSB_ADDR 0x55 00092 #define ACCEL_OFFSET_X_MSB_ADDR 0x56 00093 #define ACCEL_OFFSET_Y_LSB_ADDR 0x57 00094 #define ACCEL_OFFSET_Y_MSB_ADDR 0x58 00095 #define ACCEL_OFFSET_Z_LSB_ADDR 0x59 00096 #define ACCEL_OFFSET_Z_MSB_ADDR 0x5A 00097 /* Magnetometer Offset registers */ 00098 #define MAG_OFFSET_X_LSB_ADDR 0x5B 00099 #define MAG_OFFSET_X_MSB_ADDR 0x5C 00100 #define MAG_OFFSET_Y_LSB_ADDR 0x5D 00101 #define MAG_OFFSET_Y_MSB_ADDR 0x5E 00102 #define MAG_OFFSET_Z_LSB_ADDR 0x5F 00103 #define MAG_OFFSET_Z_MSB_ADDR 0x60 00104 /* Gyroscope Offset registers*/ 00105 #define GYRO_OFFSET_X_LSB_ADDR 0x61 00106 #define GYRO_OFFSET_X_MSB_ADDR 0x62 00107 #define GYRO_OFFSET_Y_LSB_ADDR 0x63 00108 #define GYRO_OFFSET_Y_MSB_ADDR 0x64 00109 #define GYRO_OFFSET_Z_LSB_ADDR 0x65 00110 #define GYRO_OFFSET_Z_MSB_ADDR 0x66 00111 /* Radius registers */ 00112 #define ACCEL_RADIUS_LSB_ADDR 0x67 00113 #define ACCEL_RADIUS_MSB_ADDR 0x68 00114 #define MAG_RADIUS_LSB_ADDR 0x69 00115 #define MAG_RADIUS_MSB_ADDR 0x6A 00116 00117 /* Page 1 registers */ 00118 #define BNO055_UNIQUE_ID_ADDR 0x50 00119 00120 //Definitions for unit selection 00121 #define MPERSPERS 0x00 00122 #define MILLIG 0x01 00123 #define DEG_PER_SEC 0x00 00124 #define RAD_PER_SEC 0x02 00125 #define DEGREES 0x00 00126 #define RADIANS 0x04 00127 #define CENTIGRADE 0x00 00128 #define FAHRENHEIT 0x10 00129 #define WINDOWS 0x00 00130 #define ANDROID 0x80 00131 00132 //Definitions for power mode 00133 #define POWER_MODE_NORMAL 0x00 00134 #define POWER_MODE_LOWPOWER 0x01 00135 #define POWER_MODE_SUSPEND 0x02 00136 00137 //Definitions for operating mode 00138 #define OPERATION_MODE_CONFIG 0x00 00139 #define OPERATION_MODE_ACCONLY 0x01 00140 #define OPERATION_MODE_MAGONLY 0x02 00141 #define OPERATION_MODE_GYRONLY 0x03 00142 #define OPERATION_MODE_ACCMAG 0x04 00143 #define OPERATION_MODE_ACCGYRO 0x05 00144 #define OPERATION_MODE_MAGGYRO 0x06 00145 #define OPERATION_MODE_AMG 0x07 00146 #define OPERATION_MODE_IMUPLUS 0x08 00147 #define OPERATION_MODE_COMPASS 0x09 00148 #define OPERATION_MODE_M4G 0x0A 00149 #define OPERATION_MODE_NDOF_FMC_OFF 0x0B 00150 #define OPERATION_MODE_NDOF 0x0C 00151 00152 typedef struct values{ 00153 int16_t rawx,rawy,rawz; 00154 float x,y,z; 00155 }values; 00156 00157 typedef struct angles{ 00158 int16_t rawroll,rawpitch,rawyaw; 00159 float roll, pitch, yaw; 00160 } angles; 00161 00162 typedef struct quaternion{ 00163 int16_t raww,rawx,rawy,rawz; 00164 float w,x,y,z; 00165 }quaternion; 00166 00167 typedef struct chip{ 00168 char id; 00169 char accel; 00170 char gyro; 00171 char mag; 00172 char sw[2]; 00173 char bootload; 00174 char serial[16]; 00175 }chip; 00176 00177 /** Class for operating Bosch BNO055 sensor over I2C **/ 00178 class BNO055 00179 { 00180 public: 00181 00182 /** Create BNO055 instance **/ 00183 BNO055(PinName SDA, PinName SCL); 00184 00185 /** Perform a power-on reset of the BNO055 **/ 00186 void reset(); 00187 /** Check that the BNO055 is connected and download the software details 00188 and serial number of chip and store in ID structure **/ 00189 bool check(); 00190 /** Turn the external timing crystal on/off **/ 00191 void SetExternalCrystal(bool yn); 00192 /** Set the operation mode of the sensor **/ 00193 void setmode(char mode); 00194 /** Set the power mode of the sensor **/ 00195 void setpowermode(char mode); 00196 00197 /** Set the output units from the accelerometer, either MPERSPERS or MILLIG **/ 00198 void set_accel_units(char units); 00199 /** Set the output units from the gyroscope, either DEG_PER_SEC or RAD_PER_SEC **/ 00200 void set_anglerate_units(char units); 00201 /** Set the output units from the IMU, either DEGREES or RADIANS **/ 00202 void set_angle_units(char units); 00203 /** Set the output units from the temperature sensor, either CENTIGRADE or FAHRENHEIT **/ 00204 void set_temp_units(char units); 00205 /** Set the data output format to either WINDOWS or ANDROID **/ 00206 void set_orientation(char units); 00207 /** Set the mapping of the exes/directions as per page 25 of datasheet 00208 range 0-7, any value outside this will set the orientation to P1 (default at power up) **/ 00209 void set_mapping(char orient); 00210 00211 /** Get the current values from the accelerometer **/ 00212 void get_accel(void); 00213 /** Get the current values from the gyroscope **/ 00214 void get_gyro(void); 00215 /** Get the current values from the magnetometer **/ 00216 void get_mag(void); 00217 /** Get the corrected linear acceleration **/ 00218 void get_lia(void); 00219 /** Get the current gravity vector **/ 00220 void get_grv(void); 00221 /** Get the output quaternion **/ 00222 void get_quat(void); 00223 /** Get the current Euler angles **/ 00224 void get_angles(void); 00225 /** Get the current temperature **/ 00226 void get_temp(void); 00227 00228 /** Read the calibration status register and store the result in the calib variable **/ 00229 void get_calib(void); 00230 /** Read the offset and radius values into the calibration array**/ 00231 void read_calibration_data(void); 00232 /** Write the contents of the calibration array into the registers **/ 00233 void write_calibration_data(void); 00234 00235 /** Structures containing 3-axis data for acceleration, rate of turn and magnetic field. 00236 x,y,z are the scale floating point values and 00237 rawx, rawy, rawz are the int16_t values read from the sensors **/ 00238 values accel,gyro,mag,lia,gravity; 00239 /** Stucture containing the Euler angles as yaw, pitch, roll as scaled floating point 00240 and rawyaw, rawroll & rollpitch as the int16_t values loaded from the registers **/ 00241 angles euler; 00242 /** Quaternion values as w,x,y,z (scaled floating point) and raww etc... as int16_t loaded from the 00243 registers **/ 00244 quaternion quat; 00245 00246 /** Current contents of calibration status register **/ 00247 char calib; 00248 /** Contents of the 22 registers containing offset and radius values used as calibration by the sensor **/ 00249 char calibration[22]; 00250 /** Structure containing sensor numbers, software version and chip UID **/ 00251 chip ID; 00252 /** Current temperature **/ 00253 int temperature; 00254 00255 private: 00256 00257 I2C _i2c; 00258 char rx,tx[2],address; //I2C variables 00259 char rawdata[22]; //Temporary array for input data values 00260 char op_mode; 00261 char pwr_mode; 00262 float accel_scale,rate_scale,angle_scale; 00263 int temp_scale; 00264 00265 void readchar(char location){ 00266 tx[0] = location; 00267 _i2c.write(address,tx,1,true); 00268 _i2c.read(address,&rx,1,false); 00269 } 00270 00271 void writechar(char location, char value){ 00272 tx[0] = location; 00273 tx[1] = value; 00274 _i2c.write(address,tx,2); 00275 } 00276 00277 void setpage(char value){ 00278 writechar(BNO055_PAGE_ID_ADDR,value); 00279 } 00280 }; 00281 #endif
Generated on Sat Jul 16 2022 21:37:38 by
1.7.2
