-
Notifications
You must be signed in to change notification settings - Fork 2
/
GPSroute.py.save
275 lines (215 loc) · 8.23 KB
/
GPSroute.py.save
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
from gps import *
import time, sys
import threading
import math
import ps_drone # Import PS-Drone
class GpsController(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.gpsd = gps(mode=WATCH_ENABLE) #starting the stream of info
self.running = False
def run(self):
self.running = True
while self.running:
# grab EACH set of gpsd info to clear the buffer
self.gpsd.next()
def stopController(self):
self.running = False
@property
def fix(self):
return self.gpsd.fix
@property
def utc(self):
return self.gpsd.utc
@property
def satellites(self):
return self.gpsd.satellites
def getDistanceByCoordinates(latI,longI,latF,longF):
earthRadius = 3958.75
latDiff = math.radians(latF-latI)
lngDiff = math.radians(longF-longI)
a = math.sin(latDiff /2) * math.sin(latDiff /2) + math.cos(math.radians(latI)) * math.cos(math.radians(latF)) * math.sin(lngDiff /2) * math.sin(lngDiff /2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
distance = earthRadius * c
meterConversion = 1609
distance = distance * meterConversion
#print "Distancia: "+distance
return distance
# Teste
#5570 m
#Coliseu
#-12.966275, -38.403925
#Farol de Itapua
#-12.957065, -38.353690
def getDestinoDirection(latI,longI,latA,longA,latF,longF):
return ( longF - longI ) * ( latA - latI ) - ( latF - latI ) * ( longA - longI )
def getCoefAng(latI,longI,latF,longF):
if (longI==longF):
longF = longF + 0.000001
return ( ( latF - latI ) / ( longF - longI ) )
def getAngle(latI,longI,latA,longA,latF,longF):
direction = getDestinoDirection(latI,longI,latA,longA,latF,longF)
sentido = True #Direita = True / Esquerda = False
if (direction == 0):
return 0
elif (direction < 0):
sentido = False
coefAngI = getCoefAng(latI, longI, latA, longA)
coefAngF = getCoefAng(latA, longA, latF, longF)
tangAngulo = ( ( coefAngI - coefAngF ) / ( 1 + ( coefAngI * coefAngF ) ) )
if(tangAngulo < 0):
tangAngulo = tangAngulo*(-1)
angulo = math.degrees(math.atan(tangAngulo))
#se o ponto de destino for mais perto do inicial que o atual
#usar complemento do angulo em 180
distFim = getDistanceByCoordinates(latI,longI,latF,longF)
distAtual = getDistanceByCoordinates(latA,longA,latF,longF)
if(distFim < distAtual):
angulo = 180-angulo
if(angulo>170):
#print "Angulo corrigido de ",angulo," para 170 por limitacao da biblioteca"
angulo = 170 #limitacao de biblioteca descrita na documentacao
if((sentido==False)and(angulo>0)):
angulo = angulo*(-1)
#print "Angulo de curvatura: ",angulo
return angulo
#---------------COORDENADAS DO DESTINO----------------
#CASA
#latDest = -12.893525
#longDest = -38.459263
#IGREJINHA
#latDest = -12.901878
#longDest = -38.457580
#UFBA
#latDest = -13.002755
#longDest = -38.506940
#TESTE
latDest = -13.0013262
longDest = -38.5070985
velocidade = 0.4 #equivalente a 2m/s
# create the controller
gpsc = GpsController()
# start using drone
drone = ps_drone.Drone()
def startRoute():
try:
while True:
if ( len(gpsc.satellites) > 0 ):
#-------LEVANTA VoO-----------------------------------------------------------
drone.takeoff()
print "Levantando voo..."
while drone.NavData["demo"][0][2]: time.sleep(0.1) #Aguarda takeoff acabar
#-------VERIFICA SE POSIcaO INICIAL == DESTINO---------------------------------
latAtual = gpsc.fix.latitude
longAtual = gpsc.fix.longitude
distancia = getDistanceByCoordinates(latAtual,longAtual,latDest,longDest)
if distancia < 2:
drone.stop()
time.sleep(2)
drone.land() #Pousa
print "Chegou no destino"
else:
#----------SE MOVE PARA AJUSTE INICIAL DE ANGULO -------------------------
latAnt = latAtual
longAnt = longAtual
drone.moveForward()
time.sleep(3)
drone.stop()
time.sleep(2)
latAtual = gpsc.fix.latitude
longAtual = gpsc.fix.longitude
angulo = getAngle(latAnt,longAnt,latAtual,longAtual,latDest,longDest)
drone.turnAngle(angulo,1,1)
time.sleep(4)
drone.setSpeed(velocidade)
drone.moveForward()
time.sleep(2)
#---------INICIA LOOP PARA CORREcaO DE ROTA/VELOCIDADE ATe DESTINO
arrived = False
while not arrived:
latAnt = latAtual
longAnt = longAtual
latAtual = gpsc.fix.latitude
longAtual = gpsc.fix.longitude
# Calcula distancia
espera = 2
distancia = getDistanceByCoordinates(latAtual,longAtual,latDest,longDest)
if ((distancia < 2) or (len(gpsc.satellites) == 0)):
drone.stop()
time.sleep(espera)
drone.land() #Pousa
arrived = True
print "Chegou no destino"
else:
# Calcula Velocidade
if distancia < 5:
velocidade = 0.02
print "Velocidade: 2%"
elif distancia < 10:
velocidade = 0.1
print "Velocidade: 10%"
elif distancia < 30:
velocidade = 0.5
print "Velocidade: 50%"
else:
velocidade = 1
print "Velocidade: 100%"
angulo = getAngle(latAnt,longAnt,latAtual,longAtual,latDest,longDest)
#recebe modulo do angulo
moduloAngulo = angulo
if(angulo<0):
moduloAngulo = angulo*(-1)
#calcula variacao da velocidade com base no modulo do angulo
if(moduloAngulo<60):
espera = 1
elif((moduloAngulo>60)and(moduloAngulo<120)):
espera = 2
velocidade = velocidade / 2
print "Velocidade dividida pela metade para curvar corretamente"
elif(moduloAngulo>120):
espera = 3
velocidade = velocidade / 3
print "Velocidade dividida ao terco para curvar corretamente"
drone.turnAngle(angulo,1,1)
drone.setSpeed(velocidade)
drone.moveForward()
time.sleep(espera)
else:
print "Procurando por satelites..."
time.sleep(3)
#Ctrl C
except KeyboardInterrupt:
print "User cancelled"
#Error
except:
print "Unexpected error:", sys.exc_info()[0]
raise
finally:
print "Stopping gps controller"
gpsc.stopController()
print "Pousando drone"
drone.stop()
time.sleep(4)
drone.land() #Pousa
#wait for the tread to finish
gpsc.join()
print "Done"
if __name__ == '__main__':
# start GPS controller
gpsc.start()
##### Suggested clean drone startup sequence #####
# start drone
drone.startup() # Connects to drone a$
drone.reset() # Sets the drone's st$
while (drone.getBattery()[0] == - 1): time.sleep(0.1) # Wait until the dron$
print "Battery: "+str(drone.getBattery()[0])+"% "+str(drone.getBattery()[1]) # G$
drone.useDemoMode(True) # Just give me 15 bas$
time.sleep(0.5) # Give it some time t$
print "Espaco para iniciar"
stop = False
while not stop:
key = drone.getKey()
if key == " ":
if drone.NavData["demo"][0][2] and not drone.NavData["demo"][0][3]:
stop = True
startRoute()