-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdrone.py
118 lines (97 loc) · 3.49 KB
/
drone.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
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
#import libraries
from dronekit import *
import time
import math
import pyqrcode
import cv2
from geopy.geocoders import Nominatim
cap = cv2.VideoCapture(0)
detector = cv2.QRCodeDetector()
#connect to vehicle
vehicle = connect('127.0.0.1:14551',baud=921600,wait_ready=True)
loc = Nominatim(user_agent="GetLoc")
Qrpass=input("Enter the Password to generate the QR Code:- ")
#genertaing the password of qrcode
url = pyqrcode.create(Qrpass)
url.svg("secret.svg", scale = 8)
url.png('secret.png', scale = 6)
print("Qr Code Successfully Generated and send to your System")
Qrpass1=input("Enter the Password to generate the QR Code for landing:- ")
#genertaing the password of qrcode
url = pyqrcode.create(Qrpass1)
url.svg("landing.svg", scale = 8)
url.png('landing.png', scale = 6)
print("Qr Code Successfully Generated and send to your System")
#move in the desired location
loc = Nominatim(user_agent="GetLoc")
# entering the location name
add=input("Enter the address:")
getLoc = loc.geocode(add)
# printing address
print(getLoc.address)
# printing latitude and longitude
print("Latitude = ", getLoc.latitude, "\n")
print("Longitude = ", getLoc.longitude)
#takeoff function
def arm_takeoff(height):
#check if drone is ready
while not vehicle.is_armable:
print("Waiting for drone")
time.sleep(1)
#change mode and arm
print("Arming")
vehicle.mode=VehicleMode('GUIDED')
vehicle.armed=True
#check if drone is armed
while not vehicle.armed:
print("Waiting for arm\n")
time.sleep(1)
#takeoff
print("Taking off")
vehicle.simple_takeoff(height)
#report values back every 1s and finally break out
while True:
print('Reached ',vehicle.location.global_relative_frame.alt)
if(vehicle.location.global_relative_frame.alt>=height*0.95):
print("Reached Target Altitude\n")
break
time.sleep(1)
def get_location_metres(original_location, dNorth, dEast):
earth_radius = 6378137.0 #Radius of "spherical" earth
#Coordinate offsets in radians
dLat = dNorth/earth_radius
dLon = dEast/(earth_radius*math.cos(math.pi*original_location.lat/180))
#New position in decimal degrees
newlat = original_location.lat + (dLat * 180/math.pi)
newlon = original_location.lon + (dLon * 180/math.pi)
if type(original_location) is LocationGlobal:
targetlocation=LocationGlobal(newlat, newlon,original_location.alt)
elif type(original_location) is LocationGlobalRelative:
targetlocation=LocationGlobalRelative(newlat, newlon,original_location.alt)
else:
raise Exception("Invalid Location object passed")
return targetlocation;
def get_distance_metres(aLocation1, aLocation2):
dlat = aLocation2.lat - aLocation1.lat
dlong = aLocation2.lon - aLocation1.lon
return math.sqrt((dlat*dlat) + (dlong*dlong)) * 1.113195e5
while True:
_,image=cap.read()
res,,=detector.detectAndDecode(image)
if res:
print(res)
if res==Qrpass:
#takeoff
arm_takeoff(10)
point1 = LocationGlobalRelative(getLoc.latitude, getLoc.longitude,20)
vehicle.simple_goto(point1)
print("Going to target location")
time.sleep(60)
elif res==Qrpass1:
print("Mission Accomplished. Returning to launch")
vehicle.mode=VehicleMode('RTL')
time.sleep(10)
vehicle.close()
cv2.destroyAllWindows()
cv2.imshow('Video',image)
cv2.waitKey(1)