Skip to content

Commit 6e19bd5

Browse files
authored
Merge pull request #21 from sensebox/tof-distance
use tof instead of ultrasonic
2 parents 57095b2 + 11e29ef commit 6e19bd5

File tree

3 files changed

+44
-2
lines changed

3 files changed

+44
-2
lines changed

senseBox-bike-mcus2/initSensors.ino

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,16 @@ void initUltrasonic() {
2121
// pinMode(ECHO_RIGHT, INPUT);
2222
};
2323

24+
void initVL53L8CX() {
25+
Wire.begin();
26+
Wire.setClock(1000000); //Sensor has max I2C freq of 1MHz
27+
sensor_vl53l8cx_top.begin();
28+
sensor_vl53l8cx_top.init_sensor();
29+
sensor_vl53l8cx_top.vl53l8cx_set_ranging_frequency_hz(30);
30+
sensor_vl53l8cx_top.vl53l8cx_set_resolution(VL53L8CX_RESOLUTION_8X8);
31+
sensor_vl53l8cx_top.vl53l8cx_start_ranging();
32+
}
33+
2434
void initSPS() {
2535
int errorCount = 0;
2636
int16_t ret;

senseBox-bike-mcus2/senseBox-bike-mcus2.ino

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
#include <NMEAGPS.h>
1212
#include <Adafruit_MPU6050.h>
1313
#include <NewPing.h> // http://librarymanager/All#NewPing
14+
#include <vl53l8cx_class.h>
1415

1516
#define TRIGGER_LEFT 1
1617
#define ECHO_LEFT 2
1718
#define MAX_DISTANCE_A 400
1819
NewPing sonarA(TRIGGER_LEFT, ECHO_LEFT, MAX_DISTANCE_A);
20+
VL53L8CX sensor_vl53l8cx_top(&Wire, -1, -1);
1921
static NMEAGPS gps;
2022
// https://github.com/SlashDevin/NeoGPS/blob/master/extras/doc/Data%20Model.md
2123
static gps_fix fix;
@@ -71,7 +73,8 @@ String name;
7173
// todo: give feedback through LED if all is working?
7274
void initsSensors() {
7375
Serial.print("Ultrasonic...");
74-
initUltrasonic();
76+
// initUltrasonic();
77+
initVL53L8CX();
7578
// ATTENTION! SPS Disabled for essen-auf-raedern
7679
// Serial.print("SPS30...");
7780
// initSPS(); Serial.println("done!");
@@ -98,7 +101,7 @@ void initsSensors() {
98101
// set measurements for acceleration, distance, humidity and temperature
99102
void setMeasurements() {
100103
getAccAmplitudes(&sumAccX, &sumAccY, &sumAccZ);
101-
handleDistance();
104+
handleDistanceVL53L8CX();
102105
temp = HDC.readTemperature();
103106
humi = HDC.readHumidity();
104107

senseBox-bike-mcus2/sensorFunctions.ino

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,35 @@ void handleDistance() {
2525
// }
2626
}
2727

28+
/* To give a confidence rating, a target with status 5 is considered as 100% valid.
29+
A status of 6 or 9 can be considered with a confidence value of 50%.
30+
All other statuses are below the 50% confidence level. */
31+
bool validTargetStatus(int status) {
32+
return status == 5 || status == 6 || status == 9;
33+
}
34+
35+
void handleDistanceVL53L8CX() {
36+
VL53L8CX_ResultsData Results;
37+
uint8_t NewDataReady = 0;
38+
uint8_t status;
39+
40+
status = sensor_vl53l8cx_top.vl53l8cx_check_data_ready(&NewDataReady);
41+
42+
if ((!status) && (NewDataReady != 0)) {
43+
sensor_vl53l8cx_top.vl53l8cx_get_ranging_data(&Results);
44+
float min = 1000.0; // larger than what the sensor could possibly see
45+
for(int i = 0; i < VL53L8CX_RESOLUTION_8X8*VL53L8CX_NB_TARGET_PER_ZONE; i++) {
46+
if(validTargetStatus((int)(&Results)->target_status[i])){
47+
float distance = ((&Results)->distance_mm[i])/10;
48+
if(min > distance) {
49+
min = distance;
50+
}
51+
}
52+
}
53+
dist_l = (min==1000.0) ? 400.0 : min; // in theory the sensor can measure up to 4m distance
54+
}
55+
}
56+
2857
void callSPS() {
2958
//struct sps30_measurement m;
3059
char serial[SPS30_MAX_SERIAL_LEN];

0 commit comments

Comments
 (0)