forked from patrickpoirier51/POC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
VL53l1X_TESTER.ino
152 lines (115 loc) · 4.32 KB
/
VL53l1X_TESTER.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Arduino MAVLink http://forum.arduino.cc/index.php?topic=382592.0
// https://github.com/ArduPilot/ardupilot_wiki/blob/master/dev/source/docs/code-overview-object-avoidance.rst
/*
The system id of the message should match the system id of the vehicle
(default is "1" but can be changed using the SYSID_THISMAV parameter).
The component id can be anything but MAV_COMP_ID_PATHPLANNER (195)
or MAV_COMP_ID_PERIPHERAL (158) are probably good choices.
# Define function to send distance_message mavlink message for mavlink based rangefinder, must be >10hz
# http://mavlink.org/messages/common#DISTANCE_SENSOR
def send_distance_message(dist):
msg = vehicle.message_factory.distance_sensor_encode(
0, # time since system boot, not used
1, # min distance cm
10000, # max distance cm
dist, # current distance, must be int
0, # type = 0 MAV_DISTANCE_SENSOR_LASER Laser rangefinder, e.g. LightWare SF02/F or PulsedLight units
0, # onboard id, not used
mavutil.mavlink.MAV_SENSOR_ROTATION_PITCH_270, # must be set to MAV_SENSOR_ROTATION_PITCH_270 for mavlink rangefinder, represents downward facing
0 # covariance, not used
)
vehicle.send_mavlink(msg)
vehicle.flush()
if args.verbose:
log.debug("Sending mavlink distance_message:" +str(dist))
*/
/*
This example shows how to set up and read multiple VL53L1X sensors connected to
the same I2C bus. Each sensor needs to have its XSHUT pin connected to a
different Arduino pin, and you should change sensorCount and the xshutPins array
below to match your setup.
For more information, see ST's application note AN4846 ("Using multiple VL53L0X
in a single design"). The principles described there apply to the VL53L1X as
well.
*/
#include <Wire.h>
#include <VL53L1X.h>
// The number of sensors in your system.
const uint8_t sensorCount = 2;
// The Arduino pin connected to the XSHUT pin of each sensor.
const uint8_t xshutPins[sensorCount] = { 2, 3,};
VL53L1X sensors[sensorCount];
void setup()
{
while (!Serial) {}
Serial.begin(115200);
Wire.begin();
Wire.setClock(400000); // use 400 kHz I2C
// Disable/reset all sensors by driving their XSHUT pins low.
for (uint8_t i = 0; i < sensorCount; i++)
{
pinMode(xshutPins[i], OUTPUT);
digitalWrite(xshutPins[i], LOW);
}
// Enable, initialize, and start each sensor, one by one.
for (uint8_t i = 0; i < sensorCount; i++)
{
// Stop driving this sensor's XSHUT low. This should allow the carrier
// board to pull it high. (We do NOT want to drive XSHUT high since it is
// not level shifted.) Then wait a bit for the sensor to start up.
pinMode(xshutPins[i], INPUT);
delay(10);
sensors[i].setTimeout(500);
if (!sensors[i].init())
{
Serial.print("Failed to detect and initialize sensor ");
Serial.println(i);
while (1);
}
// Each sensor must have its address changed to a unique value other than
// the default of 0x29 (except for the last one, which could be left at
// the default). To make it simple, we'll just count up from 0x2A.
sensors[i].setAddress(0x2A + i);
sensors[i].startContinuous(50);
}
// Serial.println("setup");
}
void loop() {
command_distance_1();
command_distance_2();
//command_distance_3();
//command_distance_4();
//Serial.println("loop");
//delay(200);
}
void command_distance_1() {
if (sensors[0].dataReady()) {
Serial.print(sensors[0].read());
Serial.print('\t');
}
if (sensors[0].timeoutOccurred()) {
Serial.print(" sensor1 timeout");
}
}
void command_distance_2() {
if (sensors[1].dataReady()){
Serial.print(sensors[1].read());
Serial.println('\t');
}
if (sensors[1].timeoutOccurred()) {
Serial.print(" sensor2 timeout");
}
}
// void command_distance_3() {
// Serial.print(sensors[3].read());
// if (sensors[3].timeoutOccurred()) {
// Serial.print(" sensor3 timeout");
// }
// Serial.print('\t');
// }
// void command_distance_4() {
// Serial.print(sensors[4].read());
// if (sensors[4].timeoutOccurred()) {
// Serial.print(" sensor4 timeout");
// }
//}