forked from patrickpoirier51/POC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MAVLINK-POC-Vl53l0X.ino
348 lines (261 loc) · 11.8 KB
/
MAVLINK-POC-Vl53l0X.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// 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))
*/
#include "mavlink/common/mavlink.h" // Mavlink interface
#include "mavlink/common/mavlink_msg_distance_sensor.h"
#include <Wire.h>
#include <VL53L0X.h>
const int idle = 200;
/*
Valid values are (even numbers only):
Pre: 12 to 18 (initialized to 14 by default)
Final: 8 to 14 (initialized to 10 by default)
*/
const int PreRng = 18;
const int PostRng = 14;
const int Scale = 10;
#define bRate 115200
#define XSHUT_pin4 6
#define XSHUT_pin3 5
#define XSHUT_pin2 4 //skip pin #3 : upward looking
#define XSHUT_pin1 2
//ADDRESS_DEFAULT 0b0101001 or 41
//#define Sensor1_newAddress 41 not required address change
#define Sensor2_newAddress 42
#define Sensor3_newAddress 43
#define Sensor4_newAddress 44
#define Sensor5_newAddress 45
VL53L0X Sensor1;
VL53L0X Sensor2;
VL53L0X Sensor3;
VL53L0X Sensor4;
void setup()
{ /*WARNING*/
//Shutdown pins of VL53L0X ACTIVE-LOW-ONLY NO TOLERANT TO 5V will fry them
pinMode(XSHUT_pin1, OUTPUT);
digitalWrite(XSHUT_pin1, LOW);
pinMode(XSHUT_pin2, OUTPUT);
digitalWrite(XSHUT_pin2, LOW);
pinMode(XSHUT_pin3, OUTPUT);
digitalWrite(XSHUT_pin3, LOW);
pinMode(XSHUT_pin4, OUTPUT);
digitalWrite(XSHUT_pin4, LOW);
Serial.begin(bRate);
Wire.begin();
//Change address of sensor and power up next one
// Sensor6.setAddress(SensorXX_newAddress);//For power-up procedure t-boot max 1.2ms "Datasheet: 2.9 Power sequence"
pinMode(XSHUT_pin4, INPUT);
delay(10);
Sensor4.setAddress(Sensor4_newAddress);
pinMode(XSHUT_pin3, INPUT);;
delay(10);
Sensor3.setAddress(Sensor3_newAddress);
pinMode(XSHUT_pin2, INPUT);
delay(10);
Sensor2.setAddress(Sensor2_newAddress);
pinMode(XSHUT_pin1, INPUT);
delay(10);
//ADDRESS_DEFAULT 0b0101001 or 41
Sensor1.init();
Sensor2.init();
Sensor3.init();
Sensor4.init();
Sensor1.setTimeout(idle);
Sensor2.setTimeout(idle);
Sensor3.setTimeout(idle);
Sensor4.setTimeout(idle);
// lower the return signal rate limit (default is 0.25 MCPS)
Sensor1.setSignalRateLimit(0.25);
Sensor2.setSignalRateLimit(0.25);
Sensor3.setSignalRateLimit(0.25);
Sensor4.setSignalRateLimit(0.25);
// increase laser pulse periods (defaults are 14 and 10 PCLKs)
Sensor1.setVcselPulsePeriod(VL53L0X::VcselPeriodPreRange, PreRng);
Sensor1.setVcselPulsePeriod(VL53L0X::VcselPeriodFinalRange, PostRng);
Sensor2.setVcselPulsePeriod(VL53L0X::VcselPeriodPreRange, PreRng);
Sensor2.setVcselPulsePeriod(VL53L0X::VcselPeriodFinalRange, PostRng);
Sensor3.setVcselPulsePeriod(VL53L0X::VcselPeriodPreRange, PreRng);
Sensor3.setVcselPulsePeriod(VL53L0X::VcselPeriodFinalRange, PostRng);
Sensor4.setVcselPulsePeriod(VL53L0X::VcselPeriodPreRange, PreRng);
Sensor4.setVcselPulsePeriod(VL53L0X::VcselPeriodFinalRange, PostRng);
}
void loop() {
command_heartbeat();
command_distance_1();
command_distance_2();
command_distance_3();
command_distance_4();
}
void command_heartbeat() {
//< ID 1 for this system
int sysid = 100;
//< The component sending the message.
int compid = MAV_COMP_ID_PATHPLANNER;
// Define the system type, in this case ground control station
uint8_t system_type =MAV_TYPE_GCS;
uint8_t autopilot_type = MAV_AUTOPILOT_INVALID;
uint8_t system_mode = 0;
uint32_t custom_mode = 0;
uint8_t system_state = 0;
// Initialize the required buffers
mavlink_message_t msg;
uint8_t buf[MAVLINK_MAX_PACKET_LEN];
// Pack the message
mavlink_msg_heartbeat_pack(sysid,compid, &msg, system_type, autopilot_type, system_mode, custom_mode, system_state);
// Copy the message to the send buffer
uint16_t len = mavlink_msg_to_send_buffer(buf, &msg);
// Send the message
//delay(1);
Serial.write(buf, len);
}
void command_distance_1() {
// READ THE DISTANCE SENSOR
float Sensor1Smooth = Sensor1.readRangeSingleMillimeters();
float dist1 = Sensor1Smooth / Scale;
//MAVLINK DISTANCE MESSAGE
int sysid = 1;
//< The component sending the message.
int compid = 158;
uint32_t time_boot_ms = 0; /*< Time since system boot*/
uint16_t min_distance = 5; /*< Minimum distance the sensor can measure in centimeters*/
uint16_t max_distance = 190; /*< Maximum distance the sensor can measure in centimeters*/
uint16_t current_distance = dist1; /*< Current distance reading*/
uint8_t type = 0; /*< Type from MAV_DISTANCE_SENSOR enum.*/
uint8_t id = 1; /*< Onboard ID of the sensor*/
uint8_t orientation = 0; /*(0=forward, each increment is 45degrees more in clockwise direction), 24 (upwards) or 25 (downwards)*/
// Consumed within ArduPilot by the proximity class
uint8_t covariance = 0; /*< Measurement covariance in centimeters, 0 for unknown / invalid readings*/
float horizontal_fov =0;
float vertical_fov =0;
uint8_t signal_quality =0;
const float quaternion =0;
// Initialize the required buffers
mavlink_message_t msg;
uint8_t buf[MAVLINK_MAX_PACKET_LEN];
// Pack the message
mavlink_msg_distance_sensor_pack(sysid,compid,&msg,time_boot_ms,min_distance,max_distance,current_distance,type,id,orientation,covariance,horizontal_fov,vertical_fov,signal_quality,quaternion);
// Copy the message to the send buffer
uint16_t len = mavlink_msg_to_send_buffer(buf, &msg);
// Send the message (.write sends as bytes)
//delay(1);
Serial.write(buf, len);
}
void command_distance_2() {
// READ THE DISTANCE SENSOR
float Sensor2Smooth = Sensor2.readRangeSingleMillimeters();
float dist2 = Sensor2Smooth / Scale;
//MAVLINK DISTANCE MESSAGE
int sysid = 1;
//< The component sending the message.
int compid = 158;
uint32_t time_boot_ms = 0; /*< Time since system boot*/
uint16_t min_distance = 5; /*< Minimum distance the sensor can measure in centimeters*/
uint16_t max_distance = 190; /*< Maximum distance the sensor can measure in centimeters*/
uint16_t current_distance = dist2; /*< Current distance reading*/
uint8_t type = 0; /*< Type from MAV_DISTANCE_SENSOR enum.*/
uint8_t id = 2; /*< Onboard ID of the sensor*/
uint8_t orientation = 2; /*(0=forward, each increment is 45degrees more in clockwise direction), 24 (upwards) or 25 (downwards)*/
// Consumed within ArduPilot by the proximity class
uint8_t covariance = 0; /*< Measurement covariance in centimeters, 0 for unknown / invalid readings*/
float horizontal_fov =0;
float vertical_fov =0;
uint8_t signal_quality =0;
const float quaternion =0;
// Initialize the required buffers
mavlink_message_t msg;
uint8_t buf[MAVLINK_MAX_PACKET_LEN];
// Pack the message
mavlink_msg_distance_sensor_pack(sysid,compid,&msg,time_boot_ms,min_distance,max_distance,current_distance,type,id,orientation,covariance,horizontal_fov,vertical_fov,signal_quality,quaternion);
// Copy the message to the send buffer
uint16_t len = mavlink_msg_to_send_buffer(buf, &msg);
// Send the message (.write sends as bytes)
//delay(1);
Serial.write(buf, len);
}
void command_distance_3() {
// READ THE DISTANCE SENSOR
float Sensor3Smooth = Sensor3.readRangeSingleMillimeters();
float dist3 = Sensor3Smooth / Scale;
//MAVLINK DISTANCE MESSAGE
int sysid = 1;
//< The component sending the message.
int compid = 158;
uint32_t time_boot_ms = 0; /*< Time since system boot*/
uint16_t min_distance = 5; /*< Minimum distance the sensor can measure in centimeters*/
uint16_t max_distance = 190; /*< Maximum distance the sensor can measure in centimeters*/
uint16_t current_distance = dist3; /*< Current distance reading*/
uint8_t type = 0; /*< Type from MAV_DISTANCE_SENSOR enum.*/
uint8_t id = 3; /*< Onboard ID of the sensor*/
uint8_t orientation = 4; /*(0=forward, each increment is 45degrees more in clockwise direction), 24 (upwards) or 25 (downwards)*/
// Consumed within ArduPilot by the proximity class
uint8_t covariance = 0; /*< Measurement covariance in centimeters, 0 for unknown / invalid readings*/
float horizontal_fov =0;
float vertical_fov =0;
uint8_t signal_quality =0;
const float quaternion =0;
// Initialize the required buffers
mavlink_message_t msg;
uint8_t buf[MAVLINK_MAX_PACKET_LEN];
// Pack the message
mavlink_msg_distance_sensor_pack(sysid,compid,&msg,time_boot_ms,min_distance,max_distance,current_distance,type,id,orientation,covariance,horizontal_fov,vertical_fov,signal_quality,quaternion);
// Copy the message to the send buffer
uint16_t len = mavlink_msg_to_send_buffer(buf, &msg);
// Send the message (.write sends as bytes)
//delay(1);
Serial.write(buf, len);
}
void command_distance_4() {
// READ THE DISTANCE SENSOR
float Sensor4Smooth = Sensor4.readRangeSingleMillimeters();
float dist4 = Sensor4Smooth / Scale;
//MAVLINK DISTANCE MESSAGE
int sysid = 1;
//< The component sending the message.
int compid = 158;
uint32_t time_boot_ms = 0; /*< Time since system boot*/
uint16_t min_distance = 5; /*< Minimum distance the sensor can measure in centimeters*/
uint16_t max_distance = 190; /*< Maximum distance the sensor can measure in centimeters*/
uint16_t current_distance = dist4; /*< Current distance reading*/
uint8_t type = 0; /*< Type from MAV_DISTANCE_SENSOR enum.*/
uint8_t id = 4; /*< Onboard ID of the sensor*/
uint8_t orientation = 6; /*(0=forward, each increment is 45degrees more in clockwise direction), 24 (upwards) or 25 (downwards)*/
// Consumed within ArduPilot by the proximity class
uint8_t covariance = 0; /*< Measurement covariance in centimeters, 0 for unknown / invalid readings*/
float horizontal_fov =0;
float vertical_fov =0;
uint8_t signal_quality =0;
const float quaternion =0;
// Initialize the required buffers
mavlink_message_t msg;
uint8_t buf[MAVLINK_MAX_PACKET_LEN];
// Pack the message
mavlink_msg_distance_sensor_pack(sysid,compid,&msg,time_boot_ms,min_distance,max_distance,current_distance,type,id,orientation,covariance,horizontal_fov,vertical_fov,signal_quality,quaternion);
// Copy the message to the send buffer
uint16_t len = mavlink_msg_to_send_buffer(buf, &msg);
// Send the message (.write sends as bytes)
//delay(1);
Serial.write(buf, len);
}