forked from patrickpoirier51/POC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MAVLINK-POC-VL53L1x.ino
172 lines (131 loc) · 5.27 KB
/
MAVLINK-POC-VL53L1x.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
#include <Wire.h>
#include <VL53L1X.h>
#include "mavlink/common/mavlink.h" // Mavlink interface
#include "mavlink/common/mavlink_msg_distance_sensor.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
for (uint8_t i = 0; i < sensorCount; i++)
{
pinMode(xshutPins[i], OUTPUT);
digitalWrite(xshutPins[i], LOW);
}
for (uint8_t i = 0; i < sensorCount; i++)
{
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);
}
sensors[i].setAddress(0x2A + i);
sensors[i].startContinuous(50);
}
// Serial.println("setup");
}
void loop() {
command_heartbeat();
command_distance_1();
command_distance_2();
//command_distance_3();
//command_distance_4();
//Serial.println("loop");
delay(200);
}
void command_heartbeat() {
//< ID 1 for this system
int sysid = 1;
//< The component sending the message.
int compid = 196;
// 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 = sensors[0].read();
float dist1 = Sensor1Smooth / 10;
//MAVLINK DISTANCE MESSAGE
int sysid = 1;
//< The component sending the message.
int compid = 196;
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 = 400; /*< 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 = sensors[1].read();
float dist2 = Sensor2Smooth / 10;
//MAVLINK DISTANCE MESSAGE
int sysid = 1;
//< The component sending the message.
int compid = 196;
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 = 400; /*< 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);
}