-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconnection_diagnostics.py
More file actions
executable file
·179 lines (148 loc) · 6.08 KB
/
connection_diagnostics.py
File metadata and controls
executable file
·179 lines (148 loc) · 6.08 KB
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
#!/usr/bin/env python3
"""
MAVLink Connection Diagnostics Tool
This script performs diagnostic tests on the MAVLink connection to help
troubleshoot connection issues.
"""
import os
import sys
import time
import socket
import subprocess
from pymavlink import mavutil
# Import configuration
from config import (
DRONE_TCP_ADDRESS,
DRONE_TCP_PORT,
MAVLINK_CONNECTION_STRING
)
def check_network_connectivity(host, port, timeout=5):
"""Check if the host is reachable via ping and if the port is open."""
print(f"\n=== Network Connectivity Check for {host}:{port} ===")
# Check if host is reachable via ping
try:
print(f"Pinging {host}...")
ping_cmd = ["ping", "-c", "3", "-W", str(timeout), host]
ping_result = subprocess.run(ping_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
if ping_result.returncode == 0:
print(f"✓ Host {host} is reachable via ping")
ping_output = ping_result.stdout.strip().split('\n')
for line in ping_output:
if "time=" in line:
print(f" {line}")
else:
print(f"✗ Host {host} is NOT reachable via ping")
print(f" Error: {ping_result.stderr}")
except Exception as e:
print(f"✗ Ping test failed: {e}")
# Check if port is open
try:
print(f"\nChecking if port {port} is open on {host}...")
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.settimeout(timeout)
result = sock.connect_ex((host, int(port)))
if result == 0:
print(f"✓ Port {port} is OPEN on {host}")
else:
print(f"✗ Port {port} is CLOSED on {host} (Error code: {result})")
sock.close()
except Exception as e:
print(f"✗ Port check failed: {e}")
def test_mavlink_connection(connection_string, timeout=10):
"""Test MAVLink connection by attempting to connect and receive a heartbeat."""
print(f"\n=== MAVLink Connection Test for {connection_string} ===")
try:
print(f"Attempting to connect to {connection_string}...")
start_time = time.time()
# Create the connection
mav = mavutil.mavlink_connection(
connection_string,
autoreconnect=True,
source_system=255,
source_component=0,
retries=3,
timeout=timeout
)
print(f"Connection object created. Waiting for heartbeat (timeout: {timeout}s)...")
# Wait for the first heartbeat
msg = mav.recv_match(type='HEARTBEAT', blocking=True, timeout=timeout)
if msg:
elapsed = time.time() - start_time
print(f"✓ Received heartbeat after {elapsed:.2f} seconds")
print(f" From System: {msg.get_srcSystem()}, Component: {msg.get_srcComponent()}")
print(f" Vehicle Type: {msg.type}, Autopilot: {msg.autopilot}")
print(f" System Status: {msg.system_status}")
print(f" MAVLink Version: {msg.mavlink_version}")
# Set target system and component
mav.target_system = msg.get_srcSystem()
mav.target_component = msg.get_srcComponent()
# Try to receive a few more messages
print("\nListening for additional messages (5 seconds)...")
end_time = time.time() + 5
msg_count = 0
while time.time() < end_time:
msg = mav.recv_match(blocking=False)
if msg:
msg_count += 1
print(f" Received: {msg.get_type()}")
time.sleep(0.1)
print(f"Received {msg_count} additional messages in 5 seconds")
return True
else:
print(f"✗ No heartbeat received within {timeout} seconds")
return False
except Exception as e:
print(f"✗ MAVLink connection test failed: {e}")
return False
finally:
try:
if 'mav' in locals():
mav.close()
print("Connection closed")
except:
pass
def check_system_resources():
"""Check system resources that might affect connection."""
print("\n=== System Resource Check ===")
# Check available memory
try:
with open('/proc/meminfo', 'r') as f:
meminfo = f.read()
for line in meminfo.split('\n'):
if 'MemTotal' in line or 'MemFree' in line or 'MemAvailable' in line:
print(f" {line.strip()}")
except Exception as e:
print(f" Memory check failed: {e}")
# Check CPU load
try:
with open('/proc/loadavg', 'r') as f:
loadavg = f.read().strip()
print(f" Load Average: {loadavg}")
except Exception as e:
print(f" CPU load check failed: {e}")
# Check for any relevant processes
try:
print("\n Checking for relevant processes:")
processes = ["mavproxy", "ardupilot", "sitl", "dronekit"]
for proc in processes:
result = subprocess.run(["pgrep", "-l", proc], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
if result.returncode == 0:
print(f" Found {proc} process(es):")
print(f" {result.stdout.strip()}")
except Exception as e:
print(f" Process check failed: {e}")
def run_diagnostics():
"""Run all diagnostic tests."""
print("=== MAVLink Connection Diagnostics ===")
print(f"Time: {time.strftime('%Y-%m-%d %H:%M:%S')}")
print(f"Connection String: {MAVLINK_CONNECTION_STRING}")
print(f"Drone Address: {DRONE_TCP_ADDRESS}:{DRONE_TCP_PORT}")
# Check network connectivity
check_network_connectivity(DRONE_TCP_ADDRESS, DRONE_TCP_PORT)
# Test MAVLink connection
test_mavlink_connection(MAVLINK_CONNECTION_STRING)
# Check system resources
check_system_resources()
print("\n=== Diagnostics Complete ===")
if __name__ == "__main__":
run_diagnostics()