This repository has been archived by the owner on Feb 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathunmanned_aerial_vehicule.py
85 lines (69 loc) · 2.61 KB
/
unmanned_aerial_vehicule.py
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
import threading
import socket
import logging
import json
class UnmannedAerialVehicule(threading.Thread):
TELEMETRY_BUFFER_SIZE = 1024
LISTENING_IP = "0.0.0.0"
LISTENING_PORT = 5008
def __init__(self):
""" Constructor """
threading.Thread.__init__(self)
self.lat = 0
self.alt = 0
self.lon = 0
self.data = None
self.telemetry_socket = None
self.time_boot_ms = 0
self.pitch = 0
self.yaw = 0
self.roll = 0
self.kill = False
self.delta = 0
self.latency = 0
self.ready = True
self.connection_failure = False
# Bind socket
try:
self.telemetry_socket = socket.socket(
socket.AF_INET, socket.SOCK_DGRAM)
self.telemetry_socket.settimeout(10)
self.telemetry_socket.bind(
(self.LISTENING_IP, self.LISTENING_PORT))
self.data, addr = self.telemetry_socket.recvfrom(
self.TELEMETRY_BUFFER_SIZE)
logging.info("UAV Socket binded")
except socket.error:
logging.error(
"UAV connection failed to initialize. Please check MAVProxy or your network connection")
self.ready = False
def run(self):
""" Thread callback. Will fetch UAV GPS coordinates """
try:
while self.ready:
# Get telemetry data
try:
self.data, addr = self.telemetry_socket.recvfrom(
self.TELEMETRY_BUFFER_SIZE)
if self.connection_failure:
self.connection_failure = False
logging.info("Connection regained")
except socket.error:
self.connection_failure = True
logging.error("Connection loss")
# Format into json
telemetry_json = json.loads(self.data)
if float(telemetry_json['packet_id']) == 33:
self.alt = float(telemetry_json['relative_alt']) / 1000
self.lat = float(telemetry_json['lat']) / 10000000
self.lon = float(telemetry_json['lon']) / 10000000
self.latency = telemetry_json['time_boot_ms'] - self.delta
self.delta = telemetry_json['time_boot_ms']
if self.kill:
self.telemetry_socket.close()
break
except KeyboardInterrupt:
self.kill = True
def close(self):
""" Trigger thread closure """
self.kill = True