v1.0

Dependencies:   BNO055_fusion mbed MODSERIAL dsp

Fork of Bosch_BNO055_Fusion_example by Kenji Arai

Committer:
tommyallen
Date:
Thu Feb 04 15:48:15 2016 +0000
Revision:
9:7be40e42e578
Parent:
8:41d1f9ab3be0
Child:
10:acbb851070c2
04/02/16;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kenjiArai 0:31451519d283 1 /*
kenjiArai 0:31451519d283 2 * mbed Application program for the mbed Nucleo F401
kenjiArai 0:31451519d283 3 * BNO055 Intelligent 9-axis absolute orientation sensor
kenjiArai 0:31451519d283 4 * by Bosch Sensortec
kenjiArai 0:31451519d283 5 *
kenjiArai 0:31451519d283 6 * Copyright (c) 2015 Kenji Arai / JH1PJL
kenjiArai 0:31451519d283 7 * http://www.page.sannet.ne.jp/kenjia/index.html
kenjiArai 0:31451519d283 8 * http://mbed.org/users/kenjiArai/
kenjiArai 0:31451519d283 9 * Created: March 30th, 2015
kenjiArai 5:9594519c9462 10 * Revised: April 16th, 2015
kenjiArai 0:31451519d283 11 *
kenjiArai 0:31451519d283 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
kenjiArai 0:31451519d283 13 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
kenjiArai 0:31451519d283 14 * AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
kenjiArai 0:31451519d283 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
kenjiArai 0:31451519d283 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
kenjiArai 0:31451519d283 17 */
kenjiArai 0:31451519d283 18
kenjiArai 0:31451519d283 19 // Include ---------------------------------------------------------------------------------------
kenjiArai 0:31451519d283 20 #include "mbed.h"
kenjiArai 0:31451519d283 21 #include "BNO055.h"
tommyallen 7:d4b5e83c7947 22 #include "MODSERIAL.h"
tommyallen 9:7be40e42e578 23 #include "arm_math.h"
kenjiArai 0:31451519d283 24
kenjiArai 0:31451519d283 25 // Definition ------------------------------------------------------------------------------------
kenjiArai 3:f5b5c4d795ce 26 #define NUM_LOOP 100
kenjiArai 3:f5b5c4d795ce 27
kenjiArai 0:31451519d283 28 // Object ----------------------------------------------------------------------------------------
tommyallen 7:d4b5e83c7947 29 MODSERIAL pc(USBTX,USBRX);
tommyallen 7:d4b5e83c7947 30
tommyallen 8:41d1f9ab3be0 31 volatile bool RAW = true;
tommyallen 8:41d1f9ab3be0 32 volatile bool FUS = false;
tommyallen 7:d4b5e83c7947 33 bool printed = false;
tommyallen 9:7be40e42e578 34 int Time;
tommyallen 9:7be40e42e578 35 int TimeStamp;
tommyallen 6:0590c7ff8c34 36
tommyallen 8:41d1f9ab3be0 37 //string sdata = "";
tommyallen 8:41d1f9ab3be0 38
tommyallen 7:d4b5e83c7947 39 DigitalOut led1(LED1);
tommyallen 7:d4b5e83c7947 40 DigitalOut led2(LED2);
tommyallen 7:d4b5e83c7947 41 DigitalOut led3(LED3);
tommyallen 7:d4b5e83c7947 42 DigitalOut led4(LED4);
kenjiArai 2:cf77282aea7b 43 DigitalOut pwr_onoff(p30);
tommyallen 7:d4b5e83c7947 44
kenjiArai 2:cf77282aea7b 45 I2C i2c(p28, p27); // SDA, SCL
kenjiArai 2:cf77282aea7b 46 BNO055 imu(i2c, p29); // Reset =D7, addr = BNO055_G_CHIP_ADDR, mode = MODE_NDOF <- as default
tommyallen 7:d4b5e83c7947 47 LocalFileSystem local("local"); // Create the local filesystem under the name "local"
tommyallen 6:0590c7ff8c34 48
kenjiArai 3:f5b5c4d795ce 49 Timer t;
kenjiArai 0:31451519d283 50
kenjiArai 0:31451519d283 51 // RAM -------------------------------------------------------------------------------------------
kenjiArai 0:31451519d283 52 BNO055_ID_INF_TypeDef bno055_id_inf;
kenjiArai 0:31451519d283 53 BNO055_EULER_TypeDef euler_angles;
kenjiArai 0:31451519d283 54 BNO055_QUATERNION_TypeDef quaternion;
kenjiArai 0:31451519d283 55 BNO055_LIN_ACC_TypeDef linear_acc;
kenjiArai 0:31451519d283 56 BNO055_GRAVITY_TypeDef gravity;
kenjiArai 0:31451519d283 57 BNO055_TEMPERATURE_TypeDef chip_temp;
kenjiArai 0:31451519d283 58
kenjiArai 0:31451519d283 59 // ROM / Constant data ---------------------------------------------------------------------------
kenjiArai 0:31451519d283 60
kenjiArai 0:31451519d283 61 // Function prototypes ---------------------------------------------------------------------------
kenjiArai 0:31451519d283 62
tommyallen 7:d4b5e83c7947 63
tommyallen 7:d4b5e83c7947 64 //-------------------------------------------------------------------------------------------------
tommyallen 7:d4b5e83c7947 65 // Serial Interrupt
tommyallen 7:d4b5e83c7947 66 //-------------------------------------------------------------------------------------------------
tommyallen 7:d4b5e83c7947 67 void rxCallback(MODSERIAL_IRQ_INFO *q)
tommyallen 7:d4b5e83c7947 68 {
tommyallen 7:d4b5e83c7947 69 MODSERIAL *serial = q->serial;
tommyallen 8:41d1f9ab3be0 70 if ( serial->rxGetLastChar() == 'f') {
tommyallen 8:41d1f9ab3be0 71 imu.write_reg0(0x3d, 0x0c);
tommyallen 8:41d1f9ab3be0 72 RAW = false;
tommyallen 8:41d1f9ab3be0 73 FUS = true;
tommyallen 8:41d1f9ab3be0 74 pc.printf("\r\nSWITCHING TO FUSION DATA!\r\n");
tommyallen 8:41d1f9ab3be0 75 }
tommyallen 8:41d1f9ab3be0 76 if ( serial->rxGetLastChar() == 'r') {
tommyallen 8:41d1f9ab3be0 77 imu.write_reg0(0x3d, 0x07);
tommyallen 8:41d1f9ab3be0 78 RAW = true;
tommyallen 8:41d1f9ab3be0 79 FUS = false;
tommyallen 8:41d1f9ab3be0 80 pc.printf("\r\nSWITCHING TO RAW DATA!\r\n");
tommyallen 7:d4b5e83c7947 81 }
tommyallen 7:d4b5e83c7947 82 }
tommyallen 7:d4b5e83c7947 83
kenjiArai 0:31451519d283 84 //-------------------------------------------------------------------------------------------------
kenjiArai 0:31451519d283 85 // Control Program
kenjiArai 3:f5b5c4d795ce 86 //-------------------------------------------------------------------------------------------------
kenjiArai 4:6d1118089a36 87 // Calibration
kenjiArai 4:6d1118089a36 88 // Please refer BNO055 Data sheet 3.10 Calibration & 3.6.4 Sensor calibration data
tommyallen 7:d4b5e83c7947 89 void bno055_calbration(void)
tommyallen 7:d4b5e83c7947 90 {
kenjiArai 4:6d1118089a36 91 uint8_t d;
tommyallen 7:d4b5e83c7947 92 pc.printf("------ Enter BNO055 Manual Calibration Mode ------\r\n");
kenjiArai 4:6d1118089a36 93 //---------- Gyroscope Caliblation ------------------------------------------------------------
kenjiArai 4:6d1118089a36 94 // (a) Place the device in a single stable position for a period of few seconds to allow the
kenjiArai 4:6d1118089a36 95 // gyroscope to calibrate
kenjiArai 4:6d1118089a36 96 pc.printf("Step1) Please wait few seconds\r\n");
kenjiArai 4:6d1118089a36 97 t.start();
tommyallen 7:d4b5e83c7947 98 while (t.read() < 10) {
kenjiArai 4:6d1118089a36 99 d = imu.read_calib_status();
tommyallen 6:0590c7ff8c34 100 pc.printf("calibrating = 0x%x target = 0x30(at least)\r\n", d);
tommyallen 7:d4b5e83c7947 101 if ((d & 0x30) == 0x30) {
kenjiArai 4:6d1118089a36 102 break;
kenjiArai 4:6d1118089a36 103 }
kenjiArai 4:6d1118089a36 104 wait(1.0);
kenjiArai 4:6d1118089a36 105 }
kenjiArai 4:6d1118089a36 106 pc.printf("-> Step1) is done\r\n\r\n");
kenjiArai 4:6d1118089a36 107 //---------- Magnetometer Caliblation ---------------------------------------------------------
kenjiArai 4:6d1118089a36 108 // (a) Make some random movements (for example: writing the number ‘8’ on air) until the
kenjiArai 4:6d1118089a36 109 // CALIB_STAT register indicates fully calibrated.
kenjiArai 4:6d1118089a36 110 // (b) It takes more calibration movements to get the magnetometer calibrated than in the
kenjiArai 4:6d1118089a36 111 // NDOF mode.
kenjiArai 4:6d1118089a36 112 pc.printf("Step2) random moving (try to change the BNO055 axis)\r\n");
kenjiArai 4:6d1118089a36 113 t.start();
tommyallen 7:d4b5e83c7947 114 while (t.read() < 30) {
kenjiArai 4:6d1118089a36 115 d = imu.read_calib_status();
tommyallen 6:0590c7ff8c34 116 pc.printf("calibrating, = 0x%x target = 0x33(at least)\r\n", d);
tommyallen 7:d4b5e83c7947 117 if ((d & 0x03) == 0x03) {
kenjiArai 4:6d1118089a36 118 break;
kenjiArai 4:6d1118089a36 119 }
tommyallen 7:d4b5e83c7947 120 wait(1.0);
kenjiArai 4:6d1118089a36 121 }
kenjiArai 4:6d1118089a36 122 pc.printf("-> Step2) is done\r\n\r\n");
kenjiArai 4:6d1118089a36 123 //---------- Magnetometer Caliblation ---------------------------------------------------------
kenjiArai 4:6d1118089a36 124 // a) Place the device in 6 different stable positions for a period of few seconds
kenjiArai 4:6d1118089a36 125 // to allow the accelerometer to calibrate.
kenjiArai 4:6d1118089a36 126 // b) Make sure that there is slow movement between 2 stable positions
kenjiArai 4:6d1118089a36 127 // The 6 stable positions could be in any direction, but make sure that the device is
kenjiArai 4:6d1118089a36 128 // lying at least once perpendicular to the x, y and z axis.
kenjiArai 4:6d1118089a36 129 pc.printf("Step3) Change rotation each X,Y,Z axis KEEP SLOWLY!!");
kenjiArai 4:6d1118089a36 130 pc.printf(" Each 90deg stay a 5 sec and set at least 6 position.\r\n");
kenjiArai 4:6d1118089a36 131 pc.printf(" e.g. (1)ACC:X0,Y0,Z-9,(2)ACC:X9,Y0,Z0,(3)ACC:X0,Y0,Z9,");
kenjiArai 4:6d1118089a36 132 pc.printf("(4)ACC:X-9,Y0,Z0,(5)ACC:X0,Y-9,Z0,(6)ACC:X0,Y9,Z0,\r\n");
kenjiArai 4:6d1118089a36 133 pc.printf(" If you will give up, hit any key.\r\n", d);
kenjiArai 4:6d1118089a36 134 t.stop();
tommyallen 7:d4b5e83c7947 135 while (true) {
kenjiArai 4:6d1118089a36 136 d = imu.read_calib_status();
kenjiArai 4:6d1118089a36 137 imu.get_gravity(&gravity);
tommyallen 6:0590c7ff8c34 138 pc.printf("calibrating = 0x%x target = 0xff ACC:X %4.1f, Y %4.1f, Z %4.1f\r\n",
tommyallen 7:d4b5e83c7947 139 d, gravity.x, gravity.y, gravity.z);
tommyallen 7:d4b5e83c7947 140 if (d == 0xff) {
tommyallen 7:d4b5e83c7947 141 break;
tommyallen 7:d4b5e83c7947 142 }
tommyallen 7:d4b5e83c7947 143 if (pc.readable()) {
tommyallen 7:d4b5e83c7947 144 break;
tommyallen 7:d4b5e83c7947 145 }
kenjiArai 4:6d1118089a36 146 wait(1.0);
kenjiArai 4:6d1118089a36 147 }
tommyallen 7:d4b5e83c7947 148 if (imu.read_calib_status() == 0xff) {
kenjiArai 4:6d1118089a36 149 pc.printf("-> All of Calibration steps are done successfully!\r\n\r\n");
kenjiArai 4:6d1118089a36 150 } else {
kenjiArai 4:6d1118089a36 151 pc.printf("-> Calibration steps are suspended!\r\n\r\n");
kenjiArai 4:6d1118089a36 152 }
kenjiArai 4:6d1118089a36 153 t.stop();
kenjiArai 4:6d1118089a36 154 }
tommyallen 7:d4b5e83c7947 155
tommyallen 8:41d1f9ab3be0 156 //-------------------------------------------------------------------------------------------------
tommyallen 8:41d1f9ab3be0 157 //-------------------------------------------------------------------------------------------------
tommyallen 8:41d1f9ab3be0 158 //-------------------------------------------------------------------------------------------------
tommyallen 8:41d1f9ab3be0 159 //-------------------------------------------------------------------------------------------------
tommyallen 8:41d1f9ab3be0 160 // Main Program
tommyallen 8:41d1f9ab3be0 161 //-------------------------------------------------------------------------------------------------
tommyallen 8:41d1f9ab3be0 162 //-------------------------------------------------------------------------------------------------
tommyallen 8:41d1f9ab3be0 163 //-------------------------------------------------------------------------------------------------
tommyallen 8:41d1f9ab3be0 164 //-------------------------------------------------------------------------------------------------
tommyallen 7:d4b5e83c7947 165 int main()
tommyallen 7:d4b5e83c7947 166 {
tommyallen 8:41d1f9ab3be0 167 pc.baud(115200);
tommyallen 7:d4b5e83c7947 168 pc.attach(&rxCallback, MODSERIAL::RxIrq);
tommyallen 7:d4b5e83c7947 169 pc.printf("BNO055 Hello World\r\n\r\n");
kenjiArai 4:6d1118089a36 170 imu.set_mounting_position(MT_P6);
kenjiArai 4:6d1118089a36 171 pwr_onoff = 0;
tommyallen 7:d4b5e83c7947 172 pc.printf("\r\n\r\nIf pc terminal soft is ready, please hit any key!\r\n");
kenjiArai 4:6d1118089a36 173 char c = pc.getc();
tommyallen 7:d4b5e83c7947 174 pc.printf("Bosch Sensortec BNO055 test program on " __DATE__ "/" __TIME__ "\r\n");
kenjiArai 4:6d1118089a36 175 // Is BNO055 avairable?
tommyallen 7:d4b5e83c7947 176 if (imu.chip_ready() == 0) {
kenjiArai 4:6d1118089a36 177 do {
tommyallen 7:d4b5e83c7947 178 pc.printf("Bosch BNO055 is NOT avirable!!\r\n Reset\r\n");
kenjiArai 4:6d1118089a36 179 pwr_onoff = 1; // Power off
kenjiArai 4:6d1118089a36 180 wait(0.1);
kenjiArai 4:6d1118089a36 181 pwr_onoff = 0; // Power on
kenjiArai 4:6d1118089a36 182 wait(0.02);
kenjiArai 4:6d1118089a36 183 } while(imu.reset());
kenjiArai 4:6d1118089a36 184 }
kenjiArai 4:6d1118089a36 185 pc.printf("Bosch BNO055 is available now!!\r\n");
kenjiArai 4:6d1118089a36 186 pc.printf("AXIS_REMAP_CONFIG:0x%02x, AXIS_REMAP_SIGN:0x%02x\r\n",
tommyallen 7:d4b5e83c7947 187 imu.read_reg0(BNO055_AXIS_MAP_CONFIG), imu.read_reg0(BNO055_AXIS_MAP_SIGN));
kenjiArai 4:6d1118089a36 188 imu.read_id_inf(&bno055_id_inf);
kenjiArai 4:6d1118089a36 189 pc.printf("CHIP ID:0x%02x, ACC ID:0x%02x, MAG ID:0x%02x, GYR ID:0x%02x, ",
tommyallen 7:d4b5e83c7947 190 bno055_id_inf.chip_id, bno055_id_inf.acc_id, bno055_id_inf.mag_id, bno055_id_inf.gyr_id);
kenjiArai 4:6d1118089a36 191 pc.printf("SW REV:0x%04x, BL REV:0x%02x\r\n",
tommyallen 7:d4b5e83c7947 192 bno055_id_inf.sw_rev_id, bno055_id_inf.bootldr_rev_id);
kenjiArai 4:6d1118089a36 193 pc.printf("If you would like to calibrate the BNO055, please hit 'y' (No: any other key)\r\n");
kenjiArai 4:6d1118089a36 194 c = pc.getc();
tommyallen 7:d4b5e83c7947 195 if (c == 'y') {
kenjiArai 4:6d1118089a36 196 bno055_calbration();
kenjiArai 4:6d1118089a36 197 }
kenjiArai 4:6d1118089a36 198 t.start();
tommyallen 8:41d1f9ab3be0 199 imu.write_reg0(0x3d, 0x07); // Default FUS
tommyallen 7:d4b5e83c7947 200
tommyallen 8:41d1f9ab3be0 201 while (1) {
tommyallen 8:41d1f9ab3be0 202 while (RAW == true) {
tommyallen 9:7be40e42e578 203 Time = t.read_ms();
tommyallen 9:7be40e42e578 204 if (Time > TimeStamp + 9) {
tommyallen 9:7be40e42e578 205 pc.printf("RAW,xLSB:,%d,xMSB:,%d,yLSB:%d,yMSB:,%d,zLSB:,%d,zMSB:,%d,"
tommyallen 9:7be40e42e578 206 ,imu.read_reg0(0x08),imu.read_reg0(0x09)
tommyallen 9:7be40e42e578 207 ,imu.read_reg0(0x0A),imu.read_reg0(0x0B)
tommyallen 9:7be40e42e578 208 ,imu.read_reg0(0x0C),imu.read_reg0(0x0D));
tommyallen 9:7be40e42e578 209 pc.printf("Time:,%d,/",t.read_ms());
tommyallen 9:7be40e42e578 210 TimeStamp = Time;
tommyallen 9:7be40e42e578 211 }
tommyallen 8:41d1f9ab3be0 212 }
tommyallen 8:41d1f9ab3be0 213 while (RAW == false) {
tommyallen 9:7be40e42e578 214 Time = t.read_ms();
tommyallen 9:7be40e42e578 215 if (Time > TimeStamp + 9) {
tommyallen 9:7be40e42e578 216 imu.get_linear_accel(&linear_acc);
tommyallen 9:7be40e42e578 217 pc.printf("FUS,x:,%+6.1f,y:,%+6.1f,z:,%+6.1f,Total:,%+6.1f,"
tommyallen 9:7be40e42e578 218 ,linear_acc.x ,linear_acc.y ,linear_acc.z
tommyallen 9:7be40e42e578 219 ,t.read_ms());
tommyallen 9:7be40e42e578 220 pc.printf("Time:,%d,/",t.read_ms());
tommyallen 9:7be40e42e578 221 TimeStamp = Time;
tommyallen 9:7be40e42e578 222 }
tommyallen 8:41d1f9ab3be0 223 }
tommyallen 8:41d1f9ab3be0 224 }
kenjiArai 4:6d1118089a36 225 }
kenjiArai 4:6d1118089a36 226
tommyallen 8:41d1f9ab3be0 227 // imu.get_Euler_Angles(&euler_angles);
tommyallen 8:41d1f9ab3be0 228 // pc.printf("X,%+6.1f,Y,%+6.1f,Z,%+6.1f,",
tommyallen 8:41d1f9ab3be0 229 // euler_angles.h, euler_angles.r, euler_angles.p);
tommyallen 8:41d1f9ab3be0 230 // imu.get_linear_accel(&linear_acc);
tommyallen 8:41d1f9ab3be0 231 // pc.printf("Acc:,X,%+6.1f,Y,%+6.1f,Z,%+6.1f,",
tommyallen 8:41d1f9ab3be0 232 // linear_acc.x, linear_acc.y, linear_acc.z);
tommyallen 8:41d1f9ab3be0 233 //
tommyallen 8:41d1f9ab3be0 234 // imu.get_gravity(&gravity);
tommyallen 8:41d1f9ab3be0 235 // pc.printf("Gravity,X,%+6.1f,Y,%+6.1f,Z,%+6.1f,",
tommyallen 8:41d1f9ab3be0 236 // gravity.x, gravity.y, gravity.z);
tommyallen 8:41d1f9ab3be0 237 //
tommyallen 8:41d1f9ab3be0 238 // pc.printf("Time:,%d,\r\n",t.read_ms());
tommyallen 6:0590c7ff8c34 239 // imu.get_Euler_Angles(&euler_angles);
tommyallen 6:0590c7ff8c34 240 // pc.printf("[E],Y,%+6.1f,R,%+6.1f,P,%+6.1f,",
tommyallen 6:0590c7ff8c34 241 // euler_angles.h, euler_angles.r, euler_angles.p);
tommyallen 6:0590c7ff8c34 242 // imu.get_quaternion(&quaternion);
tommyallen 6:0590c7ff8c34 243 // pc.printf("[Q],W,%d,X,%d,Y,%d,Z,%d,",
tommyallen 6:0590c7ff8c34 244 // quaternion.w, quaternion.x, quaternion.y, quaternion.z);
tommyallen 6:0590c7ff8c34 245 // imu.get_linear_accel(&linear_acc);
tommyallen 6:0590c7ff8c34 246 // pc.printf("[L],X,%+6.1f,Y,%+6.1f,Z,%+6.1f,",
tommyallen 6:0590c7ff8c34 247 // linear_acc.x, linear_acc.y, linear_acc.z);
tommyallen 6:0590c7ff8c34 248 // imu.get_gravity(&gravity);
tommyallen 6:0590c7ff8c34 249 // pc.printf("[G],X,%+6.1f,Y,%+6.1f,Z,%+6.1f,",
tommyallen 6:0590c7ff8c34 250 // gravity.x, gravity.y, gravity.z);
tommyallen 6:0590c7ff8c34 251 // imu.get_chip_temperature(&chip_temp);
tommyallen 6:0590c7ff8c34 252 // pc.printf("[T],%+d,%+d,",
tommyallen 6:0590c7ff8c34 253 // chip_temp.acc_chip, chip_temp.gyr_chip);
tommyallen 6:0590c7ff8c34 254 // pc.printf("[S],0x%x,[M],%d\r\n",
tommyallen 6:0590c7ff8c34 255 // imu.read_calib_status(), t.read_ms());
tommyallen 7:d4b5e83c7947 256 // pc.printf("Words\r\n");
kenjiArai 0:31451519d283 257