Working multi-sensor TOF setup

Dependencies:   mbed VL53L0X

Committer:
vazbyte
Date:
Wed Feb 27 16:18:33 2019 +0000
Revision:
3:5edb525fae6d
Parent:
2:8758d2437277
increased frequency of measurement and added noise reduction;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jkjk010695 0:c543582b78b1 1 #include "mbed.h"
jkjk010695 0:c543582b78b1 2 #include "VL53L0X.h"
jkjk010695 0:c543582b78b1 3
jkjk010695 0:c543582b78b1 4 #define range1_addr (0x56)
jkjk010695 0:c543582b78b1 5 #define range2_addr (0x60)
vazbyte 1:05a851d3ac3b 6 #define range1_XSHUT p15
vazbyte 1:05a851d3ac3b 7 #define range2_XSHUT p16
vazbyte 1:05a851d3ac3b 8 #define VL53L0_I2C_SDA p30
vazbyte 1:05a851d3ac3b 9 #define VL53L0_I2C_SCL p7
jkjk010695 0:c543582b78b1 10
vazbyte 1:05a851d3ac3b 11 Serial pc(USBTX, USBRX);
vazbyte 1:05a851d3ac3b 12 static DevI2C devI2c(VL53L0_I2C_SDA, VL53L0_I2C_SCL);
vazbyte 2:8758d2437277 13 DigitalOut led1(LED1);
vazbyte 2:8758d2437277 14 DigitalOut led2(LED2);
jkjk010695 0:c543582b78b1 15
jkjk010695 0:c543582b78b1 16 int main()
jkjk010695 0:c543582b78b1 17 {
jkjk010695 0:c543582b78b1 18 /*Contruct the sensors*/
jkjk010695 0:c543582b78b1 19 static DigitalOut shutdown1_pin(range1_XSHUT);
jkjk010695 0:c543582b78b1 20 static VL53L0X range1(&devI2c, &shutdown1_pin, NC);
jkjk010695 0:c543582b78b1 21 static DigitalOut shutdown2_pin(range2_XSHUT);
jkjk010695 0:c543582b78b1 22 static VL53L0X range2(&devI2c, &shutdown2_pin, NC);
jkjk010695 0:c543582b78b1 23 /*Initial all sensors*/
jkjk010695 0:c543582b78b1 24 range1.init_sensor(range1_addr);
jkjk010695 0:c543582b78b1 25 range2.init_sensor(range2_addr);
jkjk010695 0:c543582b78b1 26
jkjk010695 0:c543582b78b1 27 /*Get datas*/
jkjk010695 0:c543582b78b1 28 uint32_t distance1;
jkjk010695 0:c543582b78b1 29 uint32_t distance2;
jkjk010695 0:c543582b78b1 30 int status1;
jkjk010695 0:c543582b78b1 31 int status2;
vazbyte 2:8758d2437277 32
jkjk010695 0:c543582b78b1 33 while(1){
jkjk010695 0:c543582b78b1 34 status1 = range1.get_distance(&distance1);
vazbyte 2:8758d2437277 35 status2 = range2.get_distance(&distance2);
jkjk010695 0:c543582b78b1 36 if (status1 == VL53L0X_ERROR_NONE) {
vazbyte 2:8758d2437277 37 // printf("Range1 [mm]: %6ld\r\n", distance1);
vazbyte 3:5edb525fae6d 38 if (distance1 > 40 && distance1 < 2200) {
vazbyte 2:8758d2437277 39 led1 = 0;
vazbyte 2:8758d2437277 40 }
vazbyte 2:8758d2437277 41 else {
vazbyte 2:8758d2437277 42 led1 = 1;
vazbyte 2:8758d2437277 43 }
jkjk010695 0:c543582b78b1 44 } else {
vazbyte 2:8758d2437277 45 // printf("Range1 [mm]: --\r\n");
vazbyte 2:8758d2437277 46 led1 = 1;
jkjk010695 0:c543582b78b1 47 }
jkjk010695 0:c543582b78b1 48 if (status2 == VL53L0X_ERROR_NONE) {
vazbyte 2:8758d2437277 49 // printf("Range2 [mm]: %6ld\r\n", distance2);
vazbyte 3:5edb525fae6d 50 if (distance2 > 40 && distance2 < 2200) {
vazbyte 2:8758d2437277 51 led2 = 0;
vazbyte 3:5edb525fae6d 52 }
jkjk010695 0:c543582b78b1 53 } else {
vazbyte 2:8758d2437277 54 // printf("Range2 [mm]: --\r\n");
vazbyte 2:8758d2437277 55 led2 = 1;
jkjk010695 0:c543582b78b1 56 }
vazbyte 3:5edb525fae6d 57 wait(0.2);
jkjk010695 0:c543582b78b1 58 }
jkjk010695 0:c543582b78b1 59
jkjk010695 0:c543582b78b1 60 }
jkjk010695 0:c543582b78b1 61