forked from venthur/python-ardrone
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_connection.py
More file actions
executable file
·168 lines (125 loc) · 4.73 KB
/
test_connection.py
File metadata and controls
executable file
·168 lines (125 loc) · 4.73 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
AR.Drone 2.0 Connection Test
This script tests the AR.Drone library connection capabilities:
- Mock mode (always works)
- Live mode (requires actual drone)
- Port conflict detection and resolution
"""
import sys
import time
from libardrone import ARDrone2
def test_mock_mode():
"""Test mock mode functionality."""
print("=" * 50)
print("Testing Mock Mode")
print("=" * 50)
try:
with ARDrone2(mock=True, hd_video=True) as drone:
print("✓ Mock drone created and connected")
# Test basic functionality
time.sleep(0.5)
# Test flight controls
drone.takeoff()
print("✓ Takeoff command sent")
drone.move(roll=0.1, pitch=0.1, yaw=0.1, gaz=0.1)
print("✓ Movement command sent")
drone.hover()
print("✓ Hover command sent")
drone.land()
print("✓ Land command sent")
# Test data access
navdata = drone.get_navdata()
print(f"✓ Navigation data: {navdata is not None}")
image = drone.get_image()
print(f"✓ Image data: {image is not None}")
if image is not None:
print(f" Image shape: {image.shape}")
battery = drone.get_battery()
print(f"✓ Battery: {battery}%")
altitude = drone.get_altitude()
print(f"✓ Altitude: {altitude}mm")
print("✓ Mock mode test completed successfully")
return True
except Exception as e:
print(f"✗ Mock mode test failed: {e}")
return False
def test_live_mode():
"""Test live mode functionality."""
print("\n" + "=" * 50)
print("Testing Live Mode (requires actual drone)")
print("=" * 50)
try:
drone = ARDrone2(mock=False, hd_video=True)
print("✓ Live drone object created")
# Try to connect
print("Attempting to connect to AR.Drone at 192.168.1.1...")
drone.connect()
print("✓ Connected to AR.Drone successfully!")
print("✓ Live mode test completed successfully")
# Test basic status
print(f"✓ Video ready: {drone.is_video_ready()}")
print(f"✓ Flying: {drone.is_flying()}")
print(f"✓ Emergency: {drone.is_emergency()}")
drone.disconnect()
return True
except Exception as e:
print(f"✗ Live mode test failed: {e}")
print(" This is expected if no AR.Drone is connected")
return False
def test_port_cleanup():
"""Test port cleanup functionality."""
print("\n" + "=" * 50)
print("Testing Port Management")
print("=" * 50)
import subprocess
try:
# Check for existing processes
result = subprocess.run(['python3', 'cleanup_ports.py'],
capture_output=True, text=True, timeout=10)
if result.returncode == 0:
print("✓ Port cleanup utility works")
if "No processes found" in result.stdout:
print("✓ No port conflicts detected")
else:
print("✓ Port cleanup completed")
return True
else:
print(f"✗ Port cleanup failed: {result.stderr}")
return False
except Exception as e:
print(f"✗ Port cleanup test failed: {e}")
return False
def main():
"""Run all tests."""
print("AR.Drone 2.0 Library Test Suite")
print("================================")
print()
results = []
# Test mock mode
results.append(("Mock Mode", test_mock_mode()))
# Test port management
results.append(("Port Management", test_port_cleanup()))
# Test live mode (will likely fail without hardware)
results.append(("Live Mode", test_live_mode()))
# Summary
print("\n" + "=" * 50)
print("Test Results Summary")
print("=" * 50)
passed = 0
for test_name, result in results:
status = "PASS" if result else "FAIL"
print(f"{test_name:<20}: {status}")
if result:
passed += 1
print(f"\nPassed: {passed}/{len(results)} tests")
if passed >= 2: # Mock mode and port management should always pass
print("\n✓ AR.Drone 2.0 library is working correctly!")
print(" Live mode failure is expected without hardware.")
else:
print("\n✗ Some tests failed. Check the output above.")
return 1
return 0
if __name__ == '__main__':
sys.exit(main())