v1.0

Dependencies:   BNO055_fusion mbed MODSERIAL dsp

Fork of Bosch_BNO055_Fusion_example by Kenji Arai

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