Fork of Alexander Lill's BNO055_fusion library

Committer:
kenjiArai
Date:
Fri Apr 10 11:18:12 2015 +0000
Revision:
3:0ad6f85b178f
Parent:
2:0f225b686cd5
Child:
4:9e6fead1e93e
improved select_page() function.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenjiArai 0:86a17116e8be 1 /*
kenjiArai 0:86a17116e8be 2 * mbed library program
kenjiArai 0:86a17116e8be 3 * BNO055 Intelligent 9-axis absolute orientation sensor
kenjiArai 0:86a17116e8be 4 * by Bosch Sensortec
kenjiArai 0:86a17116e8be 5 *
kenjiArai 0:86a17116e8be 6 * Copyright (c) 2015 Kenji Arai / JH1PJL
kenjiArai 0:86a17116e8be 7 * http://www.page.sannet.ne.jp/kenjia/index.html
kenjiArai 0:86a17116e8be 8 * http://mbed.org/users/kenjiArai/
kenjiArai 0:86a17116e8be 9 * Created: March 30th, 2015
kenjiArai 3:0ad6f85b178f 10 * Revised: April 10th, 2015
kenjiArai 0:86a17116e8be 11 *
kenjiArai 0:86a17116e8be 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
kenjiArai 0:86a17116e8be 13 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
kenjiArai 0:86a17116e8be 14 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
kenjiArai 0:86a17116e8be 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
kenjiArai 0:86a17116e8be 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
kenjiArai 0:86a17116e8be 17 */
kenjiArai 0:86a17116e8be 18
kenjiArai 0:86a17116e8be 19 #include "mbed.h"
kenjiArai 0:86a17116e8be 20 #include "BNO055.h"
kenjiArai 0:86a17116e8be 21
kenjiArai 1:cb7e19c0a702 22 BNO055::BNO055 (PinName p_sda, PinName p_scl, PinName p_reset, uint8_t addr, uint8_t mode):
kenjiArai 1:cb7e19c0a702 23 _i2c(p_sda, p_scl), _res(p_reset)
kenjiArai 0:86a17116e8be 24 {
kenjiArai 0:86a17116e8be 25 chip_addr = addr;
kenjiArai 0:86a17116e8be 26 chip_mode = mode;
kenjiArai 0:86a17116e8be 27 initialize ();
kenjiArai 0:86a17116e8be 28 }
kenjiArai 0:86a17116e8be 29
kenjiArai 1:cb7e19c0a702 30 BNO055::BNO055 (PinName p_sda, PinName p_scl, PinName p_reset) :
kenjiArai 1:cb7e19c0a702 31 _i2c(p_sda, p_scl), _res(p_reset)
kenjiArai 0:86a17116e8be 32 {
kenjiArai 0:86a17116e8be 33 chip_addr = BNO055_G_CHIP_ADDR;
kenjiArai 0:86a17116e8be 34 chip_mode = MODE_NDOF;
kenjiArai 0:86a17116e8be 35 initialize ();
kenjiArai 0:86a17116e8be 36 }
kenjiArai 0:86a17116e8be 37
kenjiArai 1:cb7e19c0a702 38 BNO055::BNO055 (I2C& p_i2c, PinName p_reset, uint8_t addr, uint8_t mode) :
kenjiArai 1:cb7e19c0a702 39 _i2c(p_i2c), _res(p_reset)
kenjiArai 0:86a17116e8be 40 {
kenjiArai 0:86a17116e8be 41 chip_addr = addr;
kenjiArai 0:86a17116e8be 42 chip_mode = mode;
kenjiArai 0:86a17116e8be 43 initialize ();
kenjiArai 0:86a17116e8be 44 }
kenjiArai 0:86a17116e8be 45
kenjiArai 1:cb7e19c0a702 46 BNO055::BNO055 (I2C& p_i2c, PinName p_reset) :
kenjiArai 1:cb7e19c0a702 47 _i2c(p_i2c), _res(p_reset)
kenjiArai 0:86a17116e8be 48 {
kenjiArai 0:86a17116e8be 49 chip_addr = BNO055_G_CHIP_ADDR;
kenjiArai 0:86a17116e8be 50 chip_mode = MODE_NDOF;
kenjiArai 0:86a17116e8be 51 initialize ();
kenjiArai 0:86a17116e8be 52 }
kenjiArai 0:86a17116e8be 53
kenjiArai 0:86a17116e8be 54 /////////////// Read data & normalize /////////////////////
kenjiArai 0:86a17116e8be 55 void BNO055::get_Euler_Angles(BNO055_EULER_TypeDef *el)
kenjiArai 0:86a17116e8be 56 {
kenjiArai 0:86a17116e8be 57 uint8_t deg_or_rad;
kenjiArai 0:86a17116e8be 58 int16_t h,p,r;
kenjiArai 0:86a17116e8be 59
kenjiArai 0:86a17116e8be 60 select_page(0);
kenjiArai 0:86a17116e8be 61 dt[0] = BNO055_UNIT_SEL;
kenjiArai 0:86a17116e8be 62 _i2c.write(chip_addr, dt, 1, true);
kenjiArai 0:86a17116e8be 63 _i2c.read(chip_addr, dt, 1, false);
kenjiArai 0:86a17116e8be 64 if (dt[0] & 0x04) {
kenjiArai 0:86a17116e8be 65 deg_or_rad = 1; // Radian
kenjiArai 0:86a17116e8be 66 } else {
kenjiArai 0:86a17116e8be 67 deg_or_rad = 0; // Degree
kenjiArai 0:86a17116e8be 68 }
kenjiArai 0:86a17116e8be 69 dt[0] = BNO055_EULER_H_LSB;
kenjiArai 0:86a17116e8be 70 _i2c.write(chip_addr, dt, 1, true);
kenjiArai 0:86a17116e8be 71 _i2c.read(chip_addr, dt, 6, false);
kenjiArai 0:86a17116e8be 72 h = dt[1] << 8 | dt[0];
kenjiArai 0:86a17116e8be 73 p = dt[3] << 8 | dt[2];
kenjiArai 0:86a17116e8be 74 r = dt[5] << 8 | dt[4];
kenjiArai 0:86a17116e8be 75 if (deg_or_rad) {
kenjiArai 0:86a17116e8be 76 el->h = (double)(h / 900);
kenjiArai 0:86a17116e8be 77 el->p = (double)(p / 900);
kenjiArai 0:86a17116e8be 78 el->r = (double)(r / 900);
kenjiArai 0:86a17116e8be 79 } else {
kenjiArai 0:86a17116e8be 80 el->h = (double)(h / 16);
kenjiArai 0:86a17116e8be 81 el->p = (double)(p / 16);
kenjiArai 0:86a17116e8be 82 el->r = (double)(r / 16);
kenjiArai 0:86a17116e8be 83 }
kenjiArai 0:86a17116e8be 84 }
kenjiArai 0:86a17116e8be 85
kenjiArai 0:86a17116e8be 86 void BNO055::get_quaternion(BNO055_QUATERNION_TypeDef *qua)
kenjiArai 0:86a17116e8be 87 {
kenjiArai 0:86a17116e8be 88 select_page(0);
kenjiArai 0:86a17116e8be 89 dt[0] = BNO055_QUATERNION_W_LSB;
kenjiArai 0:86a17116e8be 90 _i2c.write(chip_addr, dt, 1, true);
kenjiArai 0:86a17116e8be 91 _i2c.read(chip_addr, dt, 8, false);
kenjiArai 0:86a17116e8be 92 qua->w = dt[1] << 8 | dt[0];
kenjiArai 0:86a17116e8be 93 qua->x = dt[3] << 8 | dt[2];
kenjiArai 0:86a17116e8be 94 qua->y = dt[5] << 8 | dt[4];
kenjiArai 0:86a17116e8be 95 qua->z = dt[7] << 8 | dt[6];
kenjiArai 0:86a17116e8be 96 }
kenjiArai 0:86a17116e8be 97
kenjiArai 0:86a17116e8be 98 void BNO055::get_linear_accel(BNO055_LIN_ACC_TypeDef *la)
kenjiArai 0:86a17116e8be 99 {
kenjiArai 0:86a17116e8be 100 uint8_t ms2_or_mg;
kenjiArai 0:86a17116e8be 101 int16_t x,y,z;
kenjiArai 0:86a17116e8be 102
kenjiArai 0:86a17116e8be 103 select_page(0);
kenjiArai 0:86a17116e8be 104 dt[0] = BNO055_UNIT_SEL;
kenjiArai 0:86a17116e8be 105 _i2c.write(chip_addr, dt, 1, true);
kenjiArai 0:86a17116e8be 106 _i2c.read(chip_addr, dt, 1, false);
kenjiArai 0:86a17116e8be 107 if (dt[0] & 0x01) {
kenjiArai 0:86a17116e8be 108 ms2_or_mg = 1; // mg
kenjiArai 0:86a17116e8be 109 } else {
kenjiArai 0:86a17116e8be 110 ms2_or_mg = 0; // m/s*s
kenjiArai 0:86a17116e8be 111 }
kenjiArai 0:86a17116e8be 112 dt[0] = BNO055_LINEAR_ACC_X_LSB;
kenjiArai 0:86a17116e8be 113 _i2c.write(chip_addr, dt, 1, true);
kenjiArai 0:86a17116e8be 114 _i2c.read(chip_addr, dt, 6, false);
kenjiArai 0:86a17116e8be 115 x = dt[1] << 8 | dt[0];
kenjiArai 0:86a17116e8be 116 y = dt[3] << 8 | dt[2];
kenjiArai 0:86a17116e8be 117 z = dt[5] << 8 | dt[4];
kenjiArai 0:86a17116e8be 118 if (ms2_or_mg) {
kenjiArai 0:86a17116e8be 119 la->x = (double)x;
kenjiArai 0:86a17116e8be 120 la->y = (double)y;
kenjiArai 0:86a17116e8be 121 la->z = (double)z;
kenjiArai 0:86a17116e8be 122 } else {
kenjiArai 0:86a17116e8be 123 la->x = (double)(x / 100);
kenjiArai 0:86a17116e8be 124 la->y = (double)(y / 100);
kenjiArai 0:86a17116e8be 125 la->z = (double)(z / 100);
kenjiArai 0:86a17116e8be 126 }
kenjiArai 0:86a17116e8be 127 }
kenjiArai 0:86a17116e8be 128
kenjiArai 0:86a17116e8be 129 void BNO055::get_gravity(BNO055_GRAVITY_TypeDef *gr)
kenjiArai 0:86a17116e8be 130 {
kenjiArai 0:86a17116e8be 131 uint8_t ms2_or_mg;
kenjiArai 0:86a17116e8be 132 int16_t x,y,z;
kenjiArai 0:86a17116e8be 133
kenjiArai 0:86a17116e8be 134 select_page(0);
kenjiArai 0:86a17116e8be 135 dt[0] = BNO055_UNIT_SEL;
kenjiArai 0:86a17116e8be 136 _i2c.write(chip_addr, dt, 1, true);
kenjiArai 0:86a17116e8be 137 _i2c.read(chip_addr, dt, 1, false);
kenjiArai 0:86a17116e8be 138 if (dt[0] & 0x01) {
kenjiArai 0:86a17116e8be 139 ms2_or_mg = 1; // mg
kenjiArai 0:86a17116e8be 140 } else {
kenjiArai 0:86a17116e8be 141 ms2_or_mg = 0; // m/s*s
kenjiArai 0:86a17116e8be 142 }
kenjiArai 0:86a17116e8be 143 dt[0] = BNO055_GRAVITY_X_LSB;
kenjiArai 0:86a17116e8be 144 _i2c.write(chip_addr, dt, 1, true);
kenjiArai 0:86a17116e8be 145 _i2c.read(chip_addr, dt, 6, false);
kenjiArai 0:86a17116e8be 146 x = dt[1] << 8 | dt[0];
kenjiArai 0:86a17116e8be 147 y = dt[3] << 8 | dt[2];
kenjiArai 0:86a17116e8be 148 z = dt[5] << 8 | dt[4];
kenjiArai 0:86a17116e8be 149 if (ms2_or_mg) {
kenjiArai 0:86a17116e8be 150 gr->x = (double)x;
kenjiArai 0:86a17116e8be 151 gr->y = (double)y;
kenjiArai 0:86a17116e8be 152 gr->z = (double)z;
kenjiArai 0:86a17116e8be 153 } else {
kenjiArai 0:86a17116e8be 154 gr->x = (double)(x / 100);
kenjiArai 0:86a17116e8be 155 gr->y = (double)(y / 100);
kenjiArai 0:86a17116e8be 156 gr->z = (double)(z / 100);
kenjiArai 0:86a17116e8be 157 }
kenjiArai 0:86a17116e8be 158 }
kenjiArai 0:86a17116e8be 159
kenjiArai 0:86a17116e8be 160 void BNO055::get_chip_temperature(BNO055_TEMPERATURE_TypeDef *tmp)
kenjiArai 0:86a17116e8be 161 {
kenjiArai 0:86a17116e8be 162 uint8_t c_or_f;
kenjiArai 0:86a17116e8be 163
kenjiArai 0:86a17116e8be 164 select_page(0);
kenjiArai 0:86a17116e8be 165 dt[0] = BNO055_UNIT_SEL;
kenjiArai 0:86a17116e8be 166 _i2c.write(chip_addr, dt, 1, true);
kenjiArai 0:86a17116e8be 167 _i2c.read(chip_addr, dt, 1, false);
kenjiArai 0:86a17116e8be 168 if (dt[0] & 0x10) {
kenjiArai 0:86a17116e8be 169 c_or_f = 1; // Fahrenheit
kenjiArai 0:86a17116e8be 170 } else {
kenjiArai 0:86a17116e8be 171 c_or_f = 0; // degrees Celsius
kenjiArai 0:86a17116e8be 172 }
kenjiArai 0:86a17116e8be 173 dt[0] = BNO055_TEMP_SOURCE;
kenjiArai 0:86a17116e8be 174 dt[1] = 0;
kenjiArai 0:86a17116e8be 175 _i2c.write(chip_addr, dt, 2, false);
kenjiArai 0:86a17116e8be 176 wait_ms(1); // Do I need to wait?
kenjiArai 0:86a17116e8be 177 dt[0] = BNO055_TEMP;
kenjiArai 0:86a17116e8be 178 _i2c.write(chip_addr, dt, 1, true);
kenjiArai 0:86a17116e8be 179 _i2c.read(chip_addr, dt, 1, false);
kenjiArai 0:86a17116e8be 180 if (c_or_f) {
kenjiArai 0:86a17116e8be 181 tmp->acc_chip = (int8_t)dt[0] * 2;
kenjiArai 0:86a17116e8be 182 } else {
kenjiArai 0:86a17116e8be 183 tmp->acc_chip = (int8_t)dt[0];
kenjiArai 0:86a17116e8be 184 }
kenjiArai 0:86a17116e8be 185 dt[0] = BNO055_TEMP_SOURCE;
kenjiArai 0:86a17116e8be 186 dt[1] = 1;
kenjiArai 0:86a17116e8be 187 _i2c.write(chip_addr, dt, 2, false);
kenjiArai 0:86a17116e8be 188 wait_ms(1); // Do I need to wait?
kenjiArai 0:86a17116e8be 189 dt[0] = BNO055_TEMP;
kenjiArai 0:86a17116e8be 190 _i2c.write(chip_addr, dt, 1, true);
kenjiArai 0:86a17116e8be 191 _i2c.read(chip_addr, dt, 1, false);
kenjiArai 0:86a17116e8be 192 if (c_or_f) {
kenjiArai 0:86a17116e8be 193 tmp->gyr_chip = (int8_t)dt[0] * 2;
kenjiArai 0:86a17116e8be 194 } else {
kenjiArai 0:86a17116e8be 195 tmp->gyr_chip = (int8_t)dt[0];
kenjiArai 0:86a17116e8be 196 }
kenjiArai 0:86a17116e8be 197 }
kenjiArai 0:86a17116e8be 198
kenjiArai 0:86a17116e8be 199 /////////////// Initialize ////////////////////////////////
kenjiArai 0:86a17116e8be 200 void BNO055::initialize (void)
kenjiArai 0:86a17116e8be 201 {
kenjiArai 1:cb7e19c0a702 202 #if defined(TARGET_STM32L152RE)
kenjiArai 1:cb7e19c0a702 203 _i2c.frequency(100000);
kenjiArai 1:cb7e19c0a702 204 #else
kenjiArai 1:cb7e19c0a702 205 _i2c.frequency(400000);
kenjiArai 1:cb7e19c0a702 206 #endif
kenjiArai 3:0ad6f85b178f 207 page_flag = 0xff;
kenjiArai 3:0ad6f85b178f 208 select_page(0);
kenjiArai 0:86a17116e8be 209 // Check Acc & Mag & Gyro are available of not
kenjiArai 0:86a17116e8be 210 check_id();
kenjiArai 0:86a17116e8be 211 // Set initial data
kenjiArai 0:86a17116e8be 212 set_initial_dt_to_regs();
kenjiArai 0:86a17116e8be 213 // Unit selection
kenjiArai 0:86a17116e8be 214 unit_selection();
kenjiArai 0:86a17116e8be 215 // Set fusion mode
kenjiArai 0:86a17116e8be 216 change_fusion_mode(chip_mode);
kenjiArai 0:86a17116e8be 217 }
kenjiArai 0:86a17116e8be 218
kenjiArai 0:86a17116e8be 219 void BNO055::unit_selection(void)
kenjiArai 0:86a17116e8be 220 {
kenjiArai 0:86a17116e8be 221 select_page(0);
kenjiArai 0:86a17116e8be 222 dt[0] = BNO055_UNIT_SEL;
kenjiArai 0:86a17116e8be 223 dt[1] = UNIT_ORI_WIN + UNIT_ACC_MSS + UNIT_GYR_DPS + UNIT_EULER_DEG + UNIT_TEMP_C;
kenjiArai 0:86a17116e8be 224 _i2c.write(chip_addr, dt, 2, false);
kenjiArai 0:86a17116e8be 225 }
kenjiArai 0:86a17116e8be 226
kenjiArai 0:86a17116e8be 227 uint8_t BNO055::select_page(uint8_t page)
kenjiArai 0:86a17116e8be 228 {
kenjiArai 3:0ad6f85b178f 229 if (page != page_flag){
kenjiArai 3:0ad6f85b178f 230 dt[0] = BNO055_PAGE_ID;
kenjiArai 3:0ad6f85b178f 231 if (page == 1) {
kenjiArai 3:0ad6f85b178f 232 dt[1] = 1; // select page 1
kenjiArai 3:0ad6f85b178f 233 } else {
kenjiArai 3:0ad6f85b178f 234 dt[1] = 0; // select page 0
kenjiArai 3:0ad6f85b178f 235 }
kenjiArai 3:0ad6f85b178f 236 _i2c.write(chip_addr, dt, 2, false);
kenjiArai 3:0ad6f85b178f 237 dt[0] = BNO055_PAGE_ID;
kenjiArai 3:0ad6f85b178f 238 _i2c.write(chip_addr, dt, 1, true);
kenjiArai 3:0ad6f85b178f 239 _i2c.read(chip_addr, dt, 1, false);
kenjiArai 3:0ad6f85b178f 240 page_flag = dt[0];
kenjiArai 0:86a17116e8be 241 }
kenjiArai 3:0ad6f85b178f 242 return page_flag;
kenjiArai 0:86a17116e8be 243 }
kenjiArai 0:86a17116e8be 244
kenjiArai 1:cb7e19c0a702 245 uint8_t BNO055::reset(void)
kenjiArai 1:cb7e19c0a702 246 {
kenjiArai 1:cb7e19c0a702 247 _res = 0;
kenjiArai 1:cb7e19c0a702 248 wait_ms(1); // Reset 1mS
kenjiArai 1:cb7e19c0a702 249 _res = 1;
kenjiArai 1:cb7e19c0a702 250 wait(0.7); // Need to wait at least 650mS
kenjiArai 1:cb7e19c0a702 251 #if defined(TARGET_STM32L152RE)
kenjiArai 1:cb7e19c0a702 252 _i2c.frequency(400000);
kenjiArai 1:cb7e19c0a702 253 #else
kenjiArai 1:cb7e19c0a702 254 _i2c.frequency(400000);
kenjiArai 1:cb7e19c0a702 255 #endif
kenjiArai 1:cb7e19c0a702 256 _i2c.stop();
kenjiArai 3:0ad6f85b178f 257 page_flag = 0xff;
kenjiArai 3:0ad6f85b178f 258 select_page(0);
kenjiArai 1:cb7e19c0a702 259 check_id();
kenjiArai 1:cb7e19c0a702 260 if (chip_id != I_AM_BNO055_CHIP){
kenjiArai 1:cb7e19c0a702 261 return 1;
kenjiArai 1:cb7e19c0a702 262 } else {
kenjiArai 1:cb7e19c0a702 263 initialize();
kenjiArai 1:cb7e19c0a702 264 return 0;
kenjiArai 1:cb7e19c0a702 265 }
kenjiArai 1:cb7e19c0a702 266 }
kenjiArai 1:cb7e19c0a702 267
kenjiArai 0:86a17116e8be 268 ////// Set initialize data to related registers ///////////
kenjiArai 0:86a17116e8be 269 void BNO055::set_initial_dt_to_regs(void)
kenjiArai 0:86a17116e8be 270 {
kenjiArai 0:86a17116e8be 271 // select_page(0);
kenjiArai 0:86a17116e8be 272 // current setting is only used default values
kenjiArai 0:86a17116e8be 273 }
kenjiArai 0:86a17116e8be 274
kenjiArai 0:86a17116e8be 275 /////////////// Check Who am I? ///////////////////////////
kenjiArai 0:86a17116e8be 276 void BNO055::check_id(void)
kenjiArai 0:86a17116e8be 277 {
kenjiArai 0:86a17116e8be 278 select_page(0);
kenjiArai 0:86a17116e8be 279 // ID
kenjiArai 0:86a17116e8be 280 dt[0] = BNO055_CHIP_ID;
kenjiArai 0:86a17116e8be 281 _i2c.write(chip_addr, dt, 1, true);
kenjiArai 0:86a17116e8be 282 _i2c.read(chip_addr, dt, 7, false);
kenjiArai 0:86a17116e8be 283 chip_id = dt[0];
kenjiArai 0:86a17116e8be 284 if (chip_id == I_AM_BNO055_CHIP) {
kenjiArai 3:0ad6f85b178f 285 ready_flag = 1;
kenjiArai 0:86a17116e8be 286 } else {
kenjiArai 3:0ad6f85b178f 287 ready_flag = 0;
kenjiArai 0:86a17116e8be 288 }
kenjiArai 0:86a17116e8be 289 acc_id = dt[1];
kenjiArai 0:86a17116e8be 290 if (acc_id == I_AM_BNO055_ACC) {
kenjiArai 3:0ad6f85b178f 291 ready_flag |= 2;
kenjiArai 0:86a17116e8be 292 }
kenjiArai 0:86a17116e8be 293 mag_id = dt[2];
kenjiArai 0:86a17116e8be 294 if (mag_id == I_AM_BNO055_MAG) {
kenjiArai 3:0ad6f85b178f 295 ready_flag |= 4;
kenjiArai 0:86a17116e8be 296 }
kenjiArai 0:86a17116e8be 297 gyr_id = dt[3];
kenjiArai 0:86a17116e8be 298 if (mag_id == I_AM_BNO055_MAG) {
kenjiArai 3:0ad6f85b178f 299 ready_flag |= 8;
kenjiArai 0:86a17116e8be 300 }
kenjiArai 0:86a17116e8be 301 bootldr_rev_id = dt[5]<< 8 | dt[4];
kenjiArai 0:86a17116e8be 302 sw_rev_id = dt[6];
kenjiArai 0:86a17116e8be 303 }
kenjiArai 0:86a17116e8be 304
kenjiArai 0:86a17116e8be 305 void BNO055::read_id_inf(BNO055_ID_INF_TypeDef *id)
kenjiArai 0:86a17116e8be 306 {
kenjiArai 0:86a17116e8be 307 id->chip_id = chip_id;
kenjiArai 0:86a17116e8be 308 id->acc_id = acc_id;
kenjiArai 0:86a17116e8be 309 id->mag_id = mag_id;
kenjiArai 0:86a17116e8be 310 id->gyr_id = gyr_id;
kenjiArai 0:86a17116e8be 311 id->bootldr_rev_id = bootldr_rev_id;
kenjiArai 0:86a17116e8be 312 id->sw_rev_id = sw_rev_id;
kenjiArai 0:86a17116e8be 313 }
kenjiArai 0:86a17116e8be 314
kenjiArai 0:86a17116e8be 315 /////////////// Check chip ready or not //////////////////
kenjiArai 2:0f225b686cd5 316 uint8_t BNO055::chip_ready(void)
kenjiArai 0:86a17116e8be 317 {
kenjiArai 3:0ad6f85b178f 318 if (ready_flag == 0x0f) {
kenjiArai 0:86a17116e8be 319 return 1;
kenjiArai 0:86a17116e8be 320 }
kenjiArai 0:86a17116e8be 321 return 0;
kenjiArai 0:86a17116e8be 322 }
kenjiArai 0:86a17116e8be 323
kenjiArai 2:0f225b686cd5 324 /////////////// Read Calibration status //////////////////
kenjiArai 2:0f225b686cd5 325 uint8_t BNO055::read_calib_status(void)
kenjiArai 2:0f225b686cd5 326 {
kenjiArai 2:0f225b686cd5 327 select_page(0);
kenjiArai 2:0f225b686cd5 328 dt[0] = BNO055_CALIB_STAT;
kenjiArai 2:0f225b686cd5 329 _i2c.write(chip_addr, dt, 1, true);
kenjiArai 2:0f225b686cd5 330 _i2c.read(chip_addr, dt, 1, false);
kenjiArai 2:0f225b686cd5 331 return dt[0];
kenjiArai 2:0f225b686cd5 332 }
kenjiArai 2:0f225b686cd5 333
kenjiArai 0:86a17116e8be 334 /////////////// Change Fusion mode ///////////////////////
kenjiArai 0:86a17116e8be 335 void BNO055::change_fusion_mode(uint8_t mode)
kenjiArai 0:86a17116e8be 336 {
kenjiArai 0:86a17116e8be 337 uint8_t current_mode;
kenjiArai 0:86a17116e8be 338
kenjiArai 0:86a17116e8be 339 select_page(0);
kenjiArai 0:86a17116e8be 340 current_mode = check_operating_mode();
kenjiArai 0:86a17116e8be 341 switch (mode) {
kenjiArai 0:86a17116e8be 342 case CONFIGMODE:
kenjiArai 0:86a17116e8be 343 dt[0] = BNO055_OPR_MODE;
kenjiArai 0:86a17116e8be 344 dt[1] = mode;
kenjiArai 0:86a17116e8be 345 _i2c.write(chip_addr, dt, 2, false);
kenjiArai 0:86a17116e8be 346 wait_ms(19); // wait 19mS
kenjiArai 0:86a17116e8be 347 break;
kenjiArai 0:86a17116e8be 348 case MODE_IMU:
kenjiArai 0:86a17116e8be 349 case MODE_COMPASS:
kenjiArai 0:86a17116e8be 350 case MODE_M4G:
kenjiArai 0:86a17116e8be 351 case MODE_NDOF_FMC_OFF:
kenjiArai 0:86a17116e8be 352 case MODE_NDOF:
kenjiArai 0:86a17116e8be 353 if (current_mode != CONFIGMODE) { // Can we change the mode directry?
kenjiArai 0:86a17116e8be 354 dt[0] = BNO055_OPR_MODE;
kenjiArai 0:86a17116e8be 355 dt[1] = CONFIGMODE;
kenjiArai 0:86a17116e8be 356 _i2c.write(chip_addr, dt, 2, false);
kenjiArai 0:86a17116e8be 357 wait_ms(19); // wait 19mS
kenjiArai 0:86a17116e8be 358 }
kenjiArai 0:86a17116e8be 359 dt[0] = BNO055_OPR_MODE;
kenjiArai 0:86a17116e8be 360 dt[1] = mode;
kenjiArai 0:86a17116e8be 361 _i2c.write(chip_addr, dt, 2, false);
kenjiArai 0:86a17116e8be 362 wait_ms(7); // wait 7mS
kenjiArai 0:86a17116e8be 363 break;
kenjiArai 0:86a17116e8be 364 default:
kenjiArai 0:86a17116e8be 365 break;
kenjiArai 0:86a17116e8be 366 }
kenjiArai 0:86a17116e8be 367 }
kenjiArai 0:86a17116e8be 368
kenjiArai 0:86a17116e8be 369 uint8_t BNO055::check_operating_mode(void)
kenjiArai 0:86a17116e8be 370 {
kenjiArai 0:86a17116e8be 371 select_page(0);
kenjiArai 0:86a17116e8be 372 dt[0] = BNO055_OPR_MODE;
kenjiArai 0:86a17116e8be 373 _i2c.write(chip_addr, dt, 1, true);
kenjiArai 0:86a17116e8be 374 _i2c.read(chip_addr, dt, 1, false);
kenjiArai 0:86a17116e8be 375 return dt[0];
kenjiArai 0:86a17116e8be 376 }
kenjiArai 0:86a17116e8be 377
kenjiArai 0:86a17116e8be 378 /////////////// Set Mouting position /////////////////////
kenjiArai 0:86a17116e8be 379 void BNO055::set_mounting_position(uint8_t position)
kenjiArai 0:86a17116e8be 380 {
kenjiArai 0:86a17116e8be 381 uint8_t remap_config;
kenjiArai 0:86a17116e8be 382 uint8_t remap_sign;
kenjiArai 0:86a17116e8be 383 uint8_t current_mode;
kenjiArai 0:86a17116e8be 384
kenjiArai 0:86a17116e8be 385 current_mode = check_operating_mode();
kenjiArai 0:86a17116e8be 386 change_fusion_mode(CONFIGMODE);
kenjiArai 0:86a17116e8be 387 switch (position) {
kenjiArai 0:86a17116e8be 388 case MT_P0:
kenjiArai 0:86a17116e8be 389 remap_config = 0x21;
kenjiArai 0:86a17116e8be 390 remap_sign = 0x04;
kenjiArai 0:86a17116e8be 391 break;
kenjiArai 0:86a17116e8be 392 case MT_P2:
kenjiArai 0:86a17116e8be 393 remap_config = 0x24;
kenjiArai 0:86a17116e8be 394 remap_sign = 0x06;
kenjiArai 0:86a17116e8be 395 break;
kenjiArai 0:86a17116e8be 396 case MT_P3:
kenjiArai 0:86a17116e8be 397 remap_config = 0x21;
kenjiArai 0:86a17116e8be 398 remap_sign = 0x02;
kenjiArai 0:86a17116e8be 399 break;
kenjiArai 0:86a17116e8be 400 case MT_P4:
kenjiArai 0:86a17116e8be 401 remap_config = 0x24;
kenjiArai 0:86a17116e8be 402 remap_sign = 0x03;
kenjiArai 0:86a17116e8be 403 break;
kenjiArai 0:86a17116e8be 404 case MT_P5:
kenjiArai 0:86a17116e8be 405 remap_config = 0x21;
kenjiArai 0:86a17116e8be 406 remap_sign = 0x01;
kenjiArai 0:86a17116e8be 407 break;
kenjiArai 0:86a17116e8be 408 case MT_P6:
kenjiArai 0:86a17116e8be 409 remap_config = 0x21;
kenjiArai 0:86a17116e8be 410 remap_sign = 0x07;
kenjiArai 0:86a17116e8be 411 break;
kenjiArai 0:86a17116e8be 412 case MT_P7:
kenjiArai 0:86a17116e8be 413 remap_config = 0x24;
kenjiArai 0:86a17116e8be 414 remap_sign = 0x05;
kenjiArai 0:86a17116e8be 415 break;
kenjiArai 0:86a17116e8be 416 case MT_P1:
kenjiArai 0:86a17116e8be 417 default:
kenjiArai 0:86a17116e8be 418 remap_config = 0x24;
kenjiArai 0:86a17116e8be 419 remap_sign = 0x00;
kenjiArai 0:86a17116e8be 420 break;
kenjiArai 0:86a17116e8be 421 }
kenjiArai 0:86a17116e8be 422 dt[0] = BNO055_AXIS_MAP_CONFIG;
kenjiArai 0:86a17116e8be 423 dt[1] = remap_config;
kenjiArai 0:86a17116e8be 424 dt[2] = remap_sign;
kenjiArai 0:86a17116e8be 425 _i2c.write(chip_addr, dt, 3, false);
kenjiArai 0:86a17116e8be 426 change_fusion_mode(current_mode);
kenjiArai 0:86a17116e8be 427 }
kenjiArai 0:86a17116e8be 428
kenjiArai 0:86a17116e8be 429 /////////////// I2C Freq. /////////////////////////////////
kenjiArai 0:86a17116e8be 430 void BNO055::frequency(int hz)
kenjiArai 0:86a17116e8be 431 {
kenjiArai 0:86a17116e8be 432 _i2c.frequency(hz);
kenjiArai 0:86a17116e8be 433 }
kenjiArai 0:86a17116e8be 434
kenjiArai 0:86a17116e8be 435 /////////////// Read/Write specific register //////////////
kenjiArai 0:86a17116e8be 436 uint8_t BNO055::read_reg0(uint8_t addr)
kenjiArai 0:86a17116e8be 437 {
kenjiArai 0:86a17116e8be 438 select_page(0);
kenjiArai 0:86a17116e8be 439 dt[0] = addr;
kenjiArai 0:86a17116e8be 440 _i2c.write(chip_addr, dt, 1, true);
kenjiArai 0:86a17116e8be 441 _i2c.read(chip_addr, dt, 1, false);
kenjiArai 0:86a17116e8be 442 return (uint8_t)dt[0];
kenjiArai 0:86a17116e8be 443 }
kenjiArai 0:86a17116e8be 444
kenjiArai 0:86a17116e8be 445 uint8_t BNO055::write_reg0(uint8_t addr, uint8_t data)
kenjiArai 0:86a17116e8be 446 {
kenjiArai 0:86a17116e8be 447 uint8_t current_mode;
kenjiArai 0:86a17116e8be 448 uint8_t d;
kenjiArai 0:86a17116e8be 449
kenjiArai 0:86a17116e8be 450 current_mode = check_operating_mode();
kenjiArai 0:86a17116e8be 451 change_fusion_mode(CONFIGMODE);
kenjiArai 0:86a17116e8be 452 dt[0] = addr;
kenjiArai 0:86a17116e8be 453 dt[1] = data;
kenjiArai 0:86a17116e8be 454 _i2c.write(chip_addr, dt, 2, false);
kenjiArai 0:86a17116e8be 455 d = dt[0];
kenjiArai 0:86a17116e8be 456 change_fusion_mode(current_mode);
kenjiArai 0:86a17116e8be 457 return d;
kenjiArai 0:86a17116e8be 458 }
kenjiArai 0:86a17116e8be 459
kenjiArai 0:86a17116e8be 460 uint8_t BNO055::read_reg1(uint8_t addr)
kenjiArai 0:86a17116e8be 461 {
kenjiArai 0:86a17116e8be 462 select_page(1);
kenjiArai 0:86a17116e8be 463 dt[0] = addr;
kenjiArai 0:86a17116e8be 464 _i2c.write(chip_addr, dt, 1, true);
kenjiArai 0:86a17116e8be 465 _i2c.read(chip_addr, dt, 1, false);
kenjiArai 0:86a17116e8be 466 return (uint8_t)dt[0];
kenjiArai 0:86a17116e8be 467 }
kenjiArai 0:86a17116e8be 468
kenjiArai 0:86a17116e8be 469 uint8_t BNO055::write_reg1(uint8_t addr, uint8_t data)
kenjiArai 0:86a17116e8be 470 {
kenjiArai 0:86a17116e8be 471 uint8_t current_mode;
kenjiArai 0:86a17116e8be 472 uint8_t d;
kenjiArai 0:86a17116e8be 473
kenjiArai 0:86a17116e8be 474 current_mode = check_operating_mode();
kenjiArai 0:86a17116e8be 475 change_fusion_mode(CONFIGMODE);
kenjiArai 0:86a17116e8be 476 select_page(1);
kenjiArai 0:86a17116e8be 477 dt[0] = addr;
kenjiArai 0:86a17116e8be 478 dt[1] = data;
kenjiArai 0:86a17116e8be 479 _i2c.write(chip_addr, dt, 2, false);
kenjiArai 0:86a17116e8be 480 d = dt[0];
kenjiArai 0:86a17116e8be 481 change_fusion_mode(current_mode);
kenjiArai 0:86a17116e8be 482 return d;
kenjiArai 0:86a17116e8be 483 }