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