Ultrasound sensor and IMU combined

Dependencies:   HCSR04 X_NUCLEO_6180XA1 mbed

Fork of HelloWorld_6180XA1 by ST

Committer:
EmbeddedSam
Date:
Wed May 09 14:43:53 2018 +0000
Revision:
50:453855ed9372
Parent:
48:c577e2af2a10
initial commit;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Davidroid 47:04733a0905ba 1 /*
EmbeddedSam 50:453855ed9372 2 Ultrasound and IMU Demo
EmbeddedSam 50:453855ed9372 3 Sam Walsh 2018
Davidroid 47:04733a0905ba 4 */
Davidroid 47:04733a0905ba 5
Davidroid 47:04733a0905ba 6
Davidroid 47:04733a0905ba 7 /* Includes ------------------------------------------------------------------*/
Davidroid 47:04733a0905ba 8
gallonm 0:83c628a58feb 9 #include "mbed.h"
gallonm 4:ccd62fd7e137 10 #include <string.h>
gallonm 4:ccd62fd7e137 11 #include <stdlib.h>
gallonm 4:ccd62fd7e137 12 #include <stdio.h>
gallonm 8:4c05f7a5bb60 13 #include <assert.h>
EmbeddedSam 48:c577e2af2a10 14 #include "hcsr04.h"
EmbeddedSam 50:453855ed9372 15 #include "BNO055.h"
gallonm 8:4c05f7a5bb60 16
Davidroid 47:04733a0905ba 17
Davidroid 47:04733a0905ba 18 /* Definitions ---------------------------------------------------------------*/
gallonm 20:b2e0b41a0e6b 19
Davidroid 47:04733a0905ba 20 /* Variables -----------------------------------------------------------------*/
Davidroid 47:04733a0905ba 21
EmbeddedSam 50:453855ed9372 22 HCSR04 UltrasonicSensor(D7,D8); //Trigger,Echo
EmbeddedSam 50:453855ed9372 23 Serial pc(USBTX, USBRX); // tx, rx
EmbeddedSam 50:453855ed9372 24 I2C i2c(D4, D5); // SDA, SCL
EmbeddedSam 50:453855ed9372 25 BNO055 imu(i2c, D6); // Reset =D6, addr = BNO055_G_CHIP_ADDR, mode = MODE_NDOF <- as default
EmbeddedSam 48:c577e2af2a10 26
EmbeddedSam 50:453855ed9372 27 Timer t;
EmbeddedSam 48:c577e2af2a10 28
EmbeddedSam 50:453855ed9372 29 // RAM -------------------------------------------------------------------------------------------
EmbeddedSam 50:453855ed9372 30 BNO055_ID_INF_TypeDef bno055_id_inf;
EmbeddedSam 50:453855ed9372 31 BNO055_EULER_TypeDef euler_angles;
EmbeddedSam 50:453855ed9372 32 BNO055_QUATERNION_TypeDef quaternion;
EmbeddedSam 50:453855ed9372 33 BNO055_LIN_ACC_TypeDef linear_acc;
EmbeddedSam 50:453855ed9372 34 BNO055_GRAVITY_TypeDef gravity;
EmbeddedSam 50:453855ed9372 35 BNO055_TEMPERATURE_TypeDef chip_temp;
EmbeddedSam 48:c577e2af2a10 36
EmbeddedSam 50:453855ed9372 37 float distance;
Davidroid 47:04733a0905ba 38
Davidroid 47:04733a0905ba 39 /* Functions -----------------------------------------------------------------*/
licio.mapelli@st.com 32:724d2afb0ca2 40
mapellil 35:8b4a5cc0fb1f 41 /*=================================== Main ==================================
mapellil 44:3c68d5aa842e 42 Prints on the serial over USB the measured distance and lux.
mapellil 44:3c68d5aa842e 43 The measures are run in single shot polling mode.
mapellil 35:8b4a5cc0fb1f 44 =============================================================================*/
mapellil 35:8b4a5cc0fb1f 45 int main()
mapellil 43:f03152407731 46 {
EmbeddedSam 48:c577e2af2a10 47 pc.baud(921600);
EmbeddedSam 50:453855ed9372 48 pc.printf ("Hello World \n\r");
EmbeddedSam 50:453855ed9372 49 imu.set_mounting_position(MT_P6);
EmbeddedSam 50:453855ed9372 50 // Is BNO055 avairable?
EmbeddedSam 50:453855ed9372 51 if (imu.chip_ready() == 0){
EmbeddedSam 50:453855ed9372 52 do {
EmbeddedSam 50:453855ed9372 53 pc.printf("Bosch BNO055 is NOT avirable!!\r\n Reset\r\n");
EmbeddedSam 50:453855ed9372 54 } while(imu.reset());
EmbeddedSam 50:453855ed9372 55 }
EmbeddedSam 50:453855ed9372 56 pc.printf("Bosch BNO055 is available now!!\r\n");
EmbeddedSam 50:453855ed9372 57 pc.printf("AXIS_REMAP_CONFIG:0x%02x, AXIS_REMAP_SIGN:0x%02x\r\n",
EmbeddedSam 50:453855ed9372 58 imu.read_reg0(BNO055_AXIS_MAP_CONFIG), imu.read_reg0(BNO055_AXIS_MAP_SIGN));
EmbeddedSam 50:453855ed9372 59 imu.read_id_inf(&bno055_id_inf);
EmbeddedSam 50:453855ed9372 60 pc.printf("CHIP ID:0x%02x, ACC ID:0x%02x, MAG ID:0x%02x, GYR ID:0x%02x, ",
EmbeddedSam 50:453855ed9372 61 bno055_id_inf.chip_id, bno055_id_inf.acc_id, bno055_id_inf.mag_id, bno055_id_inf.gyr_id);
EmbeddedSam 50:453855ed9372 62 pc.printf("SW REV:0x%04x, BL REV:0x%02x\r\n",
EmbeddedSam 50:453855ed9372 63 bno055_id_inf.sw_rev_id, bno055_id_inf.bootldr_rev_id);
Davidroid 47:04733a0905ba 64
EmbeddedSam 50:453855ed9372 65 pc.printf("[E]:Euler Angles[deg],[Q]:Quaternion[],[L]:Linear accel[m/s*s],");
EmbeddedSam 50:453855ed9372 66 pc.printf("[G]:Gravity vector[m/s*s],[T]:Chip temperature,Acc,Gyr[degC],[S]:Status,[M]:time[mS]\r\n");
EmbeddedSam 50:453855ed9372 67 t.start();
EmbeddedSam 50:453855ed9372 68 while(1) {
EmbeddedSam 50:453855ed9372 69 pc.printf ("Ultrasound: %d \t", (int)distance);
EmbeddedSam 50:453855ed9372 70 distance = UltrasonicSensor.distance();
EmbeddedSam 50:453855ed9372 71 imu.get_Euler_Angles(&euler_angles);
EmbeddedSam 50:453855ed9372 72 pc.printf("[E],Y,%+6.1f,R,%+6.1f,P,%+6.1f,",
EmbeddedSam 50:453855ed9372 73 euler_angles.h, euler_angles.r, euler_angles.p);
EmbeddedSam 50:453855ed9372 74 imu.get_quaternion(&quaternion);
EmbeddedSam 50:453855ed9372 75 pc.printf("[Q],W,%d,X,%d,Y,%d,Z,%d,",
EmbeddedSam 50:453855ed9372 76 quaternion.w, quaternion.x, quaternion.y, quaternion.z);
EmbeddedSam 50:453855ed9372 77 imu.get_linear_accel(&linear_acc);
EmbeddedSam 50:453855ed9372 78 pc.printf("[L],X,%+6.1f,Y,%+6.1f,Z,%+6.1f,",
EmbeddedSam 50:453855ed9372 79 linear_acc.x, linear_acc.y, linear_acc.z);
EmbeddedSam 50:453855ed9372 80 imu.get_gravity(&gravity);
EmbeddedSam 50:453855ed9372 81 pc.printf("[G],X,%+6.1f,Y,%+6.1f,Z,%+6.1f,",
EmbeddedSam 50:453855ed9372 82 gravity.x, gravity.y, gravity.z);
EmbeddedSam 50:453855ed9372 83 imu.get_chip_temperature(&chip_temp);
EmbeddedSam 50:453855ed9372 84 pc.printf("[T],%+d,%+d,",
EmbeddedSam 50:453855ed9372 85 chip_temp.acc_chip, chip_temp.gyr_chip);
EmbeddedSam 50:453855ed9372 86 pc.printf("[S],0x%x,[M],%d\r\n",
EmbeddedSam 50:453855ed9372 87 imu.read_calib_status(), t.read_ms());
EmbeddedSam 50:453855ed9372 88 }
EmbeddedSam 50:453855ed9372 89 }
Davidroid 47:04733a0905ba 90
EmbeddedSam 50:453855ed9372 91
EmbeddedSam 50:453855ed9372 92 //while(1){
EmbeddedSam 50:453855ed9372 93
EmbeddedSam 50:453855ed9372 94 // wait(0.5);
EmbeddedSam 50:453855ed9372 95 // }
EmbeddedSam 50:453855ed9372 96 //}