v1.0

Dependencies:   BNO055_fusion mbed MODSERIAL dsp

Fork of Bosch_BNO055_Fusion_example by Kenji Arai

Committer:
tommyallen
Date:
Mon Feb 01 00:20:46 2016 +0000
Revision:
7:d4b5e83c7947
Parent:
6:0590c7ff8c34
Child:
8:41d1f9ab3be0
25/1

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