Some improvements on mrcrsch's BNO055_fusion library

Dependents:   BabaTalp-NRF

Fork of BNO055_fusion by Marcell Rausch

Committer:
gume
Date:
Sat Apr 22 19:24:31 2017 +0000
Revision:
7:7df0c13f0ef9
Parent:
6:eae1a743876b
Add power mode change; Extend mode change with non fusion modes

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 4:9e6fead1e93e 10 * Revised: April 16th, 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 4:9e6fead1e93e 76 el->h = (double)h / 900;
kenjiArai 4:9e6fead1e93e 77 el->p = (double)p / 900;
kenjiArai 4:9e6fead1e93e 78 el->r = (double)r / 900;
kenjiArai 0:86a17116e8be 79 } else {
kenjiArai 4:9e6fead1e93e 80 el->h = (double)h / 16;
kenjiArai 4:9e6fead1e93e 81 el->p = (double)p / 16;
kenjiArai 4:9e6fead1e93e 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 4:9e6fead1e93e 123 la->x = (double)x / 100;
kenjiArai 4:9e6fead1e93e 124 la->y = (double)y / 100;
kenjiArai 4:9e6fead1e93e 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 4:9e6fead1e93e 154 gr->x = (double)x / 100;
kenjiArai 4:9e6fead1e93e 155 gr->y = (double)y / 100;
kenjiArai 4:9e6fead1e93e 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 }
gume 6:eae1a743876b 301 sw_rev_id = (dt[5] << 8) | dt[4];
gume 6:eae1a743876b 302 bootldr_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
gume 7:7df0c13f0ef9 334 /////////////// Change Power mode ///////////////////////
gume 7:7df0c13f0ef9 335 void BNO055::change_power_mode(uint8_t mode)
gume 7:7df0c13f0ef9 336 {
gume 7:7df0c13f0ef9 337 write_reg0(BNO055_PWR_MODE, mode);
gume 7:7df0c13f0ef9 338 }
gume 7:7df0c13f0ef9 339
kenjiArai 0:86a17116e8be 340 /////////////// Change Fusion mode ///////////////////////
kenjiArai 0:86a17116e8be 341 void BNO055::change_fusion_mode(uint8_t mode)
kenjiArai 0:86a17116e8be 342 {
kenjiArai 0:86a17116e8be 343 uint8_t current_mode;
kenjiArai 0:86a17116e8be 344
kenjiArai 0:86a17116e8be 345 select_page(0);
kenjiArai 0:86a17116e8be 346 current_mode = check_operating_mode();
kenjiArai 0:86a17116e8be 347 switch (mode) {
kenjiArai 0:86a17116e8be 348 case CONFIGMODE:
kenjiArai 0:86a17116e8be 349 dt[0] = BNO055_OPR_MODE;
kenjiArai 0:86a17116e8be 350 dt[1] = mode;
kenjiArai 0:86a17116e8be 351 _i2c.write(chip_addr, dt, 2, false);
kenjiArai 0:86a17116e8be 352 wait_ms(19); // wait 19mS
kenjiArai 0:86a17116e8be 353 break;
kenjiArai 0:86a17116e8be 354 case MODE_IMU:
kenjiArai 0:86a17116e8be 355 case MODE_COMPASS:
kenjiArai 0:86a17116e8be 356 case MODE_M4G:
kenjiArai 0:86a17116e8be 357 case MODE_NDOF_FMC_OFF:
kenjiArai 0:86a17116e8be 358 case MODE_NDOF:
kenjiArai 0:86a17116e8be 359 if (current_mode != CONFIGMODE) { // Can we change the mode directry?
kenjiArai 0:86a17116e8be 360 dt[0] = BNO055_OPR_MODE;
kenjiArai 0:86a17116e8be 361 dt[1] = CONFIGMODE;
kenjiArai 0:86a17116e8be 362 _i2c.write(chip_addr, dt, 2, false);
kenjiArai 0:86a17116e8be 363 wait_ms(19); // wait 19mS
kenjiArai 0:86a17116e8be 364 }
kenjiArai 0:86a17116e8be 365 dt[0] = BNO055_OPR_MODE;
kenjiArai 0:86a17116e8be 366 dt[1] = mode;
kenjiArai 0:86a17116e8be 367 _i2c.write(chip_addr, dt, 2, false);
kenjiArai 0:86a17116e8be 368 wait_ms(7); // wait 7mS
kenjiArai 0:86a17116e8be 369 break;
kenjiArai 0:86a17116e8be 370 default:
kenjiArai 0:86a17116e8be 371 break;
kenjiArai 0:86a17116e8be 372 }
kenjiArai 0:86a17116e8be 373 }
kenjiArai 0:86a17116e8be 374
kenjiArai 0:86a17116e8be 375 uint8_t BNO055::check_operating_mode(void)
kenjiArai 0:86a17116e8be 376 {
kenjiArai 0:86a17116e8be 377 select_page(0);
kenjiArai 0:86a17116e8be 378 dt[0] = BNO055_OPR_MODE;
kenjiArai 0:86a17116e8be 379 _i2c.write(chip_addr, dt, 1, true);
kenjiArai 0:86a17116e8be 380 _i2c.read(chip_addr, dt, 1, false);
kenjiArai 0:86a17116e8be 381 return dt[0];
kenjiArai 0:86a17116e8be 382 }
kenjiArai 0:86a17116e8be 383
kenjiArai 0:86a17116e8be 384 /////////////// Set Mouting position /////////////////////
kenjiArai 0:86a17116e8be 385 void BNO055::set_mounting_position(uint8_t position)
kenjiArai 0:86a17116e8be 386 {
kenjiArai 0:86a17116e8be 387 uint8_t remap_config;
kenjiArai 0:86a17116e8be 388 uint8_t remap_sign;
kenjiArai 0:86a17116e8be 389 uint8_t current_mode;
kenjiArai 0:86a17116e8be 390
kenjiArai 0:86a17116e8be 391 current_mode = check_operating_mode();
kenjiArai 0:86a17116e8be 392 change_fusion_mode(CONFIGMODE);
kenjiArai 0:86a17116e8be 393 switch (position) {
kenjiArai 0:86a17116e8be 394 case MT_P0:
kenjiArai 0:86a17116e8be 395 remap_config = 0x21;
kenjiArai 0:86a17116e8be 396 remap_sign = 0x04;
kenjiArai 0:86a17116e8be 397 break;
kenjiArai 0:86a17116e8be 398 case MT_P2:
kenjiArai 0:86a17116e8be 399 remap_config = 0x24;
kenjiArai 0:86a17116e8be 400 remap_sign = 0x06;
kenjiArai 0:86a17116e8be 401 break;
kenjiArai 0:86a17116e8be 402 case MT_P3:
kenjiArai 0:86a17116e8be 403 remap_config = 0x21;
kenjiArai 0:86a17116e8be 404 remap_sign = 0x02;
kenjiArai 0:86a17116e8be 405 break;
kenjiArai 0:86a17116e8be 406 case MT_P4:
kenjiArai 0:86a17116e8be 407 remap_config = 0x24;
kenjiArai 0:86a17116e8be 408 remap_sign = 0x03;
kenjiArai 0:86a17116e8be 409 break;
kenjiArai 0:86a17116e8be 410 case MT_P5:
kenjiArai 0:86a17116e8be 411 remap_config = 0x21;
kenjiArai 0:86a17116e8be 412 remap_sign = 0x01;
kenjiArai 0:86a17116e8be 413 break;
kenjiArai 0:86a17116e8be 414 case MT_P6:
kenjiArai 0:86a17116e8be 415 remap_config = 0x21;
kenjiArai 0:86a17116e8be 416 remap_sign = 0x07;
kenjiArai 0:86a17116e8be 417 break;
kenjiArai 0:86a17116e8be 418 case MT_P7:
kenjiArai 0:86a17116e8be 419 remap_config = 0x24;
kenjiArai 0:86a17116e8be 420 remap_sign = 0x05;
kenjiArai 0:86a17116e8be 421 break;
kenjiArai 0:86a17116e8be 422 case MT_P1:
kenjiArai 0:86a17116e8be 423 default:
kenjiArai 0:86a17116e8be 424 remap_config = 0x24;
kenjiArai 0:86a17116e8be 425 remap_sign = 0x00;
kenjiArai 0:86a17116e8be 426 break;
kenjiArai 0:86a17116e8be 427 }
kenjiArai 0:86a17116e8be 428 dt[0] = BNO055_AXIS_MAP_CONFIG;
kenjiArai 0:86a17116e8be 429 dt[1] = remap_config;
kenjiArai 0:86a17116e8be 430 dt[2] = remap_sign;
kenjiArai 0:86a17116e8be 431 _i2c.write(chip_addr, dt, 3, false);
kenjiArai 0:86a17116e8be 432 change_fusion_mode(current_mode);
kenjiArai 0:86a17116e8be 433 }
kenjiArai 0:86a17116e8be 434
kenjiArai 0:86a17116e8be 435 /////////////// I2C Freq. /////////////////////////////////
kenjiArai 0:86a17116e8be 436 void BNO055::frequency(int hz)
kenjiArai 0:86a17116e8be 437 {
kenjiArai 0:86a17116e8be 438 _i2c.frequency(hz);
kenjiArai 0:86a17116e8be 439 }
kenjiArai 0:86a17116e8be 440
kenjiArai 0:86a17116e8be 441 /////////////// Read/Write specific register //////////////
kenjiArai 0:86a17116e8be 442 uint8_t BNO055::read_reg0(uint8_t addr)
kenjiArai 0:86a17116e8be 443 {
kenjiArai 0:86a17116e8be 444 select_page(0);
kenjiArai 0:86a17116e8be 445 dt[0] = addr;
kenjiArai 0:86a17116e8be 446 _i2c.write(chip_addr, dt, 1, true);
kenjiArai 0:86a17116e8be 447 _i2c.read(chip_addr, dt, 1, false);
kenjiArai 0:86a17116e8be 448 return (uint8_t)dt[0];
kenjiArai 0:86a17116e8be 449 }
kenjiArai 0:86a17116e8be 450
kenjiArai 0:86a17116e8be 451 uint8_t BNO055::write_reg0(uint8_t addr, uint8_t data)
kenjiArai 0:86a17116e8be 452 {
kenjiArai 0:86a17116e8be 453 uint8_t current_mode;
kenjiArai 0:86a17116e8be 454 uint8_t d;
kenjiArai 0:86a17116e8be 455
kenjiArai 0:86a17116e8be 456 current_mode = check_operating_mode();
kenjiArai 0:86a17116e8be 457 change_fusion_mode(CONFIGMODE);
kenjiArai 0:86a17116e8be 458 dt[0] = addr;
kenjiArai 0:86a17116e8be 459 dt[1] = data;
kenjiArai 0:86a17116e8be 460 _i2c.write(chip_addr, dt, 2, false);
kenjiArai 0:86a17116e8be 461 d = dt[0];
kenjiArai 0:86a17116e8be 462 change_fusion_mode(current_mode);
kenjiArai 0:86a17116e8be 463 return d;
kenjiArai 0:86a17116e8be 464 }
kenjiArai 0:86a17116e8be 465
kenjiArai 0:86a17116e8be 466 uint8_t BNO055::read_reg1(uint8_t addr)
kenjiArai 0:86a17116e8be 467 {
kenjiArai 0:86a17116e8be 468 select_page(1);
kenjiArai 0:86a17116e8be 469 dt[0] = addr;
kenjiArai 0:86a17116e8be 470 _i2c.write(chip_addr, dt, 1, true);
kenjiArai 0:86a17116e8be 471 _i2c.read(chip_addr, dt, 1, false);
kenjiArai 0:86a17116e8be 472 return (uint8_t)dt[0];
kenjiArai 0:86a17116e8be 473 }
kenjiArai 0:86a17116e8be 474
kenjiArai 0:86a17116e8be 475 uint8_t BNO055::write_reg1(uint8_t addr, uint8_t data)
kenjiArai 0:86a17116e8be 476 {
kenjiArai 0:86a17116e8be 477 uint8_t current_mode;
kenjiArai 0:86a17116e8be 478 uint8_t d;
kenjiArai 0:86a17116e8be 479
kenjiArai 0:86a17116e8be 480 current_mode = check_operating_mode();
kenjiArai 0:86a17116e8be 481 change_fusion_mode(CONFIGMODE);
kenjiArai 0:86a17116e8be 482 select_page(1);
kenjiArai 0:86a17116e8be 483 dt[0] = addr;
kenjiArai 0:86a17116e8be 484 dt[1] = data;
kenjiArai 0:86a17116e8be 485 _i2c.write(chip_addr, dt, 2, false);
kenjiArai 0:86a17116e8be 486 d = dt[0];
kenjiArai 0:86a17116e8be 487 change_fusion_mode(current_mode);
kenjiArai 0:86a17116e8be 488 return d;
kenjiArai 0:86a17116e8be 489 }
mrcrsch 5:6a08a4c5b1e1 490
mrcrsch 5:6a08a4c5b1e1 491 void BNO055::get_quaternion_float(float *Q){
mrcrsch 5:6a08a4c5b1e1 492 BNO055_QUATERNION_TypeDef intQ;
mrcrsch 5:6a08a4c5b1e1 493 get_quaternion(&intQ);
mrcrsch 5:6a08a4c5b1e1 494
mrcrsch 5:6a08a4c5b1e1 495 Q[0] = (float)(intQ.w / 16384.0);
mrcrsch 5:6a08a4c5b1e1 496 Q[1] = (float)(intQ.x / 16384.0);
mrcrsch 5:6a08a4c5b1e1 497 Q[2] = (float)(intQ.y / 16384.0);
mrcrsch 5:6a08a4c5b1e1 498 Q[3] = (float)(intQ.z / 16384.0);
mrcrsch 5:6a08a4c5b1e1 499 }
mrcrsch 5:6a08a4c5b1e1 500
mrcrsch 5:6a08a4c5b1e1 501
mrcrsch 5:6a08a4c5b1e1 502 void BNO055::configure_accelerometer_range(uint8_t range){
mrcrsch 5:6a08a4c5b1e1 503 uint8_t current_mode;
mrcrsch 5:6a08a4c5b1e1 504
mrcrsch 5:6a08a4c5b1e1 505 current_mode = check_operating_mode();
mrcrsch 5:6a08a4c5b1e1 506 if(current_mode != CONFIGMODE)
mrcrsch 5:6a08a4c5b1e1 507 change_fusion_mode(CONFIGMODE);
mrcrsch 5:6a08a4c5b1e1 508
mrcrsch 5:6a08a4c5b1e1 509 select_page(1);
mrcrsch 5:6a08a4c5b1e1 510 dt[0] = ACCEL_CONFIG;
mrcrsch 5:6a08a4c5b1e1 511 dt[1] = 0<<5 | 3 << 2 | range;
mrcrsch 5:6a08a4c5b1e1 512
mrcrsch 5:6a08a4c5b1e1 513 _i2c.write(chip_addr, dt, 2, false);
mrcrsch 5:6a08a4c5b1e1 514
mrcrsch 5:6a08a4c5b1e1 515 change_fusion_mode(current_mode);
mrcrsch 5:6a08a4c5b1e1 516 }
mrcrsch 5:6a08a4c5b1e1 517
mrcrsch 5:6a08a4c5b1e1 518 void BNO055::get_abs_accel(BNO055_LIN_ACC_TypeDef *la){
mrcrsch 5:6a08a4c5b1e1 519 //http://math.stackexchange.com/questions/40164/how-do-you-rotate-a-vector-by-a-unit-quaternion
mrcrsch 5:6a08a4c5b1e1 520 //http://mathworld.wolfram.com/QuaternionConjugate.html
mrcrsch 5:6a08a4c5b1e1 521
mrcrsch 5:6a08a4c5b1e1 522 float tempQuat[4];
mrcrsch 5:6a08a4c5b1e1 523 float Quat[4];
mrcrsch 5:6a08a4c5b1e1 524
mrcrsch 5:6a08a4c5b1e1 525 get_linear_accel(la);
mrcrsch 5:6a08a4c5b1e1 526 get_quaternion_float(tempQuat);
mrcrsch 5:6a08a4c5b1e1 527
mrcrsch 5:6a08a4c5b1e1 528 for( int i=0; i<4; i++)
mrcrsch 5:6a08a4c5b1e1 529 Quat[i] = tempQuat[i];
mrcrsch 5:6a08a4c5b1e1 530
mrcrsch 5:6a08a4c5b1e1 531 //http://es.mathworks.com/help/aeroblks/quaternionmultiplication.html q=quat, r=la
mrcrsch 5:6a08a4c5b1e1 532 tempQuat[0]=0 -la->x*Quat[1] -la->y*Quat[2] -la->z*Quat[3];
mrcrsch 5:6a08a4c5b1e1 533 tempQuat[1]=0 +la->x*Quat[0] -la->y*Quat[3] +la->z*Quat[2];
mrcrsch 5:6a08a4c5b1e1 534 tempQuat[2]=0 +la->x*Quat[3] +la->y*Quat[0] -la->z*Quat[1];
mrcrsch 5:6a08a4c5b1e1 535 tempQuat[3]=0 -la->x*Quat[2] +la->y*Quat[1] +la->z*Quat[0];
mrcrsch 5:6a08a4c5b1e1 536
mrcrsch 5:6a08a4c5b1e1 537 //q=tempQuat, r=quatConj
mrcrsch 5:6a08a4c5b1e1 538 la->x=Quat[0]*tempQuat[1] -Quat[1]*tempQuat[0] +Quat[2]*tempQuat[3] -Quat[3]*tempQuat[2];
mrcrsch 5:6a08a4c5b1e1 539 la->y=Quat[0]*tempQuat[2] +Quat[1]*tempQuat[3] -Quat[2]*tempQuat[0] +Quat[3]*tempQuat[1];
mrcrsch 5:6a08a4c5b1e1 540 la->z=Quat[0]*tempQuat[3] +Quat[1]*tempQuat[2] -Quat[2]*tempQuat[1] -Quat[3]*tempQuat[0];
mrcrsch 5:6a08a4c5b1e1 541 }