diff --git a/AATC_Coordinate.py b/AATC_Coordinate.py index cbcdd52..620b8cf 100644 --- a/AATC_Coordinate.py +++ b/AATC_Coordinate.py @@ -22,3 +22,53 @@ def Print(self): def __str__(self): return str((round(self.x,8),round(self.y,8),round(self.z,8))) + + +def AddCoords(Coord,VectorCoord): #Simulates the drone moving + Coord.x += VectorCoord.x + Coord.y += VectorCoord.y + Coord.z += VectorCoord.z + time.sleep(0.1) + return Coord + +def CalculateVector(Coords,TargetCoords,Speed): + dx = TargetCoords.x- Coords.x + dy = TargetCoords.y- Coords.y + dz = TargetCoords.z- Coords.z + + + yCircumference = 40008000 + xCircumference = 40075160 + #Converts to metres + mdy = dy * yCircumference /360 + mdx = dx * xCircumference * math.cos(toRadian(TargetCoords.y)) /360 + + v = math.sqrt(mdx**2+mdy**2+ dz**2) + ratio = Speed/v + svx = dx*ratio #Gets Speed vectors + svy = dy*ratio + svz = dz*ratio + return AATC_Drone.Coordinate(svx,svy,svz) + +def toRadian(x): + return x*math.pi/180 + +def toDegree(x): + return 180*x/math.pi + + +def DeltaCoordToMetres(aCoord,bCoord): + #Formula for dx and dy from : https://stackoverflow.com/questions/3024404/transform-longitude-latitude-into-meters + dx = abs(aCoord.x - bCoord.x) + dy = abs(aCoord.y - bCoord.y) # in degrees + dz = abs(aCoord.z - bCoord.z) + + + yCircumference = 40008000 + xCircumference = 40075160 + + mdy = dy * yCircumference /360 + mdx = dx * xCircumference * math.cos(toRadian(aCoord.y)) /360 + + Distance = math.sqrt(mdx**2 + mdy**2 + dz**2) + return Distance diff --git a/AATC_Crypto.py b/AATC_Crypto.py index c3e59bf..cbe7b11 100644 --- a/AATC_Crypto.py +++ b/AATC_Crypto.py @@ -1,8 +1,9 @@ #AATC crypto module # Diffie-Hellman from 'pip install diffiehellman ' -import diffiehellman.diffiehellman as DH -import codecs,recvall,ast +#import diffiehellman.diffiehellman as DH +import codecs,recvall,ast,binascii,os from Crypto.Cipher import AES +from Crypto.PublicKey import RSA class Crypter: """ @@ -29,7 +30,7 @@ def __init__(self, con, mode = "CLIENT",AutoGenerate = True): def SetMode(self,mode): self.mode = mode - def GenerateKey(self,key_size = 256): + def GenerateKey(self,key_size = 2048): print("Generating encryption keys. Please stand by...") #Generating keys takes a long time and have found no way to shorted key length if self.mode == "SERVER": self.ServerGenerateKey() @@ -40,51 +41,109 @@ def GenerateKey(self,key_size = 256): print("Encryption keys generated",self.shared_key) - def ServerGenerateKey(self): - DifHel = DH.DiffieHellman() + + def ClientGenerateKey(self,RSA_KeySize,AES_KeySize): + RSAKey = RSA.generate(RSA_KeySize) + + privateKey = RSAKey.exportKey("DER") + publicKey = RSAKey.publicKey().exportKey("DER") + + self.Send(("ExchangeKey",(publicKey,AES_KeySize))) #Format ("ExchangeKey",("a1b2c3.....",)) + data = self.Recv() + Sucess,Message = data[0],data[1] #Format (Sucess,("a2c3d4...")) + if Sucess == False: + raise Exception("Error occured while exchanging keys") + + self.Send(("Exit",())) + data = self.Recv() + if data[0] == False: + raise Exception("Server failed to commit to exit") + + RSAPrivateObject = RSA.importKey(privateKey) + self.AESKey = RSAPrivateObject.decrypt(Message) + self.AES = AES.new(self.AESKey) + + + + def ServerGenerateKey(self): Exit = False - while not Exit : + while not Exit: data = self.Recv() Command, Arguments = data[0],data[1] - if Command == "PublicKey": - try: - DifHel.generate_public_key() - DifHel.generate_shared_secret(Arguments[0]) - self.Send(("PublicKey",(DifHel.public_key,))) - except: - self.Send((False,())) - + if Command == "ExchangeKey": + publicKey,AES_KeySize = Arguments[0],Arguments[1] + if AES_KeySize not in [16,24,32]: + AES_KeySize = 32 #If key size is not valid set size to default of 32 + + self.AESKey = binascii.b2a_hex(os.urandom(AES_KeySize)) # Here to allow regeneration of AES key while still in loop if required. + + PublicKeyObject = RSA.importKey(publicKey) + EncryptedAESKey = PublicKeyObject.encrypt(self.AESKey,"x")[0] + self.Send((True,(self.AESKey,))) + elif Command == "Exit": self.Send((True,())) Exit = True - self.shared_key = DifHel.shared_key[0:32] - self.AES = AES.new(self.shared_key) - - - - - def ClientGenerateKey(self,key_size): - DifHel = DH.DiffieHellman() - DifHel.generate_public_key() + else: + self.Send((False,("Command does not exist",))) + self.AES = AES.new(self.AESKey) + + - self.Send(("PublicKey",(DifHel.public_key,))) - data = self.Recv() - if data[0] == False: - raise Exception("Error occured while transmitting public key") - DifHel.generate_shared_secret(data[1][0]) - self.Send(("Exit",())) - data = self.Recv() - if data[0] == False: - raise Exception("Server failed to commit to exit") - self.shared_key = DifHel.shared_key[0:32] - self.AES = AES.new(self.shared_key) + + +## def DHServerGenerateKey(self): +## DifHel = DH.DiffieHellman() +## +## +## Exit = False +## while not Exit : +## data = self.Recv() +## Command, Arguments = data[0],data[1] +## +## if Command == "PublicKey": +## try: +## DifHel.generate_public_key() +## DifHel.generate_shared_secret(Arguments[0]) +## self.Send(("PublicKey",(DifHel.public_key,))) +## except: +## self.Send((False,())) +## +## elif Command == "Exit": +## self.Send((True,())) +## Exit = True +## +## self.shared_key = DifHel.shared_key[0:32] +## self.AES = AES.new(self.shared_key) +## +## +## +## +## def DHClientGenerateKey(self,key_size): +## DifHel = DH.DiffieHellman() +## DifHel.generate_public_key() +## +## +## self.Send(("PublicKey",(DifHel.public_key,))) +## data = self.Recv() +## if data[0] == False: +## raise Exception("Error occured while transmitting public key") +## DifHel.generate_shared_secret(data[1][0]) +## +## self.Send(("Exit",())) +## data = self.Recv() +## if data[0] == False: +## raise Exception("Server failed to commit to exit") +## +## self.shared_key = DifHel.shared_key[0:32] +## self.AES = AES.new(self.shared_key) @@ -110,6 +169,5 @@ def Recv(self): data = ast.literal_eval(codecs.decode(data)) # (Command,Arguments) return data - #return data[0],data[1],data[2] except Exception as e: print("Error in Cryptor while receiving ",e) diff --git a/AATC_DB.py b/AATC_DB.py index a18a1c6..8a5cdfb 100644 --- a/AATC_DB.py +++ b/AATC_DB.py @@ -84,13 +84,8 @@ def DroneCheckCredentials(self,DroneID,DronePassword): return False,"Incorrect Drone Credentials",-1 def DroneGetDroneInfo(self,DroneID,DronePassword): - self.cur.execute("SELECT 1 FROM DroneCredentials WHERE DroneID = %s AND DronePassword = %s",(DroneID,DronePassword)) - DroneIDFetch = self.cur.fetchall() - if DroneIDFetch != (): - self.cur.execute("SELECT * FROM Drone WHERE DroneID = %s",(DroneID,)) - return True,str(self.Table_Headers("Drone")),self.cur.fetchall() - else: - return False,"Incorrect Drone Credentials",[] + self.cur.execute("SELECT * FROM Drone WHERE DroneID = %s",(DroneID,)) + return True,str(self.Table_Headers("Drone")),self.cur.fetchall() diff --git a/AATC_Drone.py b/AATC_Drone.py index 77ab1d6..53c480e 100644 --- a/AATC_Drone.py +++ b/AATC_Drone.py @@ -62,8 +62,8 @@ def UpdateDroneStatus(self,LastCoords,LastBattery): ############################ - def DroneGetDroneInfo(self,DroneID,DronePassword): - self.Send("DroneGetDroneInfo",(DroneID,DronePassword)) + def DroneGetDroneInfo(self,DroneID): + self.Send("DroneGetDroneInfo",(DroneID,)) Sucess,Message,DroneInfo = self.Recv() return Sucess,Message,DroneInfo diff --git a/AATC_Drone_Logic.py b/AATC_Drone_Logic.py index 8b33f9f..2791cdc 100644 --- a/AATC_Drone_Logic.py +++ b/AATC_Drone_Logic.py @@ -1,91 +1,157 @@ -import AATC_Drone,threading,queue,math,time - -def LaunchDroneLogic(DRONEID,DRONEPASSWORD,FlightQueue,StatusQueue,SLEEP_TIME = 30): - Exit = False - while not Exit: - try: - D = AATC_Drone.CreateDroneInterface() - LoginSucess,Message = D.Login(DRONEID,DRONEPASSWORD) - print(LoginSucess,Message) - if LoginSucess: +import AATC_Drone,threading,queue,math,time,AATC_GPIO + + +class DroneLogicSystem: + def __init__(DroneID,DronePassword,FlightQueue,StatusQueue,GPIO_Queue,Sleep_Time = 60): + self.DroneID = DroneID + self.DronePassword = DronePassword + self.FlightQueue = FlightQueue + self.StatusQueue = StatusQueue + self.GPIO_Queue = GPIO_Queue + self.Sleep_Time = Sleep_Time + + def Main(self): + Exit = False + InFlight = False + while not Exit: + try: + self.D = AATC_Drone.CreateDroneInterface() + LoginSucess,Message = self.D.Login(self.DroneID,self.DronePassword) - - AvailableFlight = False - while not AvailableFlight: - - if not StatusQueue.empty(): - Status = StatusQueue.get() - StatusQueue.task_done() - D.UpdateDroneStatus(Status["Coords"],Status["Battery"])#dfjsafkdsajdfjs - - Sucess,Message,FlightID = D.CheckForFlight() - AvailableFlight = Sucess - time.sleep(SLEEP_TIME) #At the end to pause to wait so that if the server is still writing waypoints it can finish. + if LoginSucess: + if not InFlight: + self.GPIO_Queue.put(("GREEN","Function",(AATC_GPIO.Pattern,( [(21,1,5),(21,0,1)],)))) #Let the Thread for the GREEN LED blink on pin 21 at 0.5 Hz for 1 cycle repeatedly until stopped + self.CheckForFlight() + else: + self.GPIO_Queue.put(("GREEN","Function",(AATC_GPIO.Blink,(21,0.5,1,True)))) #Let the Thread for the GREEN LED blink on pin 21 at 0.5 Hz for 1 cycle repeatedly until stopped + self.RunFlight() + + InFlight = False #Once RunFlight has completed sucessfully go back to checking for flights. Will only complete once finished, if crashes will not pass here. + + else: + self.GPIO_Queue.put(("RED","Function",(AATC_GPIO.Blink,(11,1,10,False)))) #Let the Thread for RED LED blink on pin 11 at 1Hz 10 times and not repeat. + print("Login failure",Message) + time.sleep(self.Sleep_Time) #To prevent spamming server - - FlightID = FlightID[0][0] - print("Obtaining flight and drone information. FlightID :",FlightID) - DSucess,DroneMessage,DroneData = D.DroneGetDroneInfo(DRONEID,DRONEPASSWORD) - FSucess,FlightMessage,FlightData = D.GetFlight(FlightID) - WSucess,WaypointsMessage,FlightWaypointsData = D.GetFlightWaypoints(FlightID) - if not (DSucess and FSucess and WSucess): - raise Exception("FlightData or FlightWaypointData was not sucessfully obtained") + except Exception as e: + print("Error occured in DroneLogic Main",e) + self.GPIO_Queue.put(("RED","Function",(AATC_GPIO.Blink,(11,3,30,False)))) #Let the Thread for RED LED blink on pin 11 at 3Hz 30 times and not repeat. + time.sleep(self.Sleep_Time) #To prevent spamming server - DroneInfo = AATC_Drone.MakeDroneInfo(DroneMessage, DroneData) - Flight = AATC_Drone.GetFlightObject(FlightMessage,FlightData) - Waypoints = AATC_Drone.GetWaypointObjects(WaypointsMessage,FlightWaypointsData) - FlightQueue.put((False,(DroneInfo,Flight,Waypoints))) + + + + def CheckForFlight(self): + AvailableFlight = False + while not AvailableFlight: + + if not self.StatusQueue.empty(): #Updates status + Status = self.StatusQueue.get() + self.StatusQueue.task_done() + self.D.UpdateDroneStatus(Status["Coords"],Status["Battery"])#dfjsafkdsajdfjs - except Exception as e: - print("Error occured in Drone Logic",str(e)) - AvailableFlight = False + Sucess,Message,FlightID = self.D.CheckForFlight() + AvailableFlight = Sucess + + time.sleep(self.Sleep_Time) #At the end to pause to wait so that if the server is still writing waypoints it can finish. + + + FlightID = FlightID[0][0] + print("Obtaining flight and drone information. FlightID :",FlightID) + DroneInfo, Flight, Waypoints = GetAllFlightInfo(self.D,self.DroneID,FlightID) + + FlightQueue.put((False,(DroneInfo,Flight,Waypoints))) - if AvailableFlight: + def RunFlight(self): + complete = False + while not complete: #While drone not at target location + while self.StatusQueue.empty(): + time.sleep(self.Sleep_Time) + + Status = self.StatusQueue.get() + self.StatusQueue.task_done() + + Sucess,Message = self.D.UpdateDroneStatus(Status["Coords"],Status["Battery"]) + if "MarkComplete" in Status: + complete = True + print("Flight ",Status["MarkComplete"]," complete") + Sucess,Message = self.D.MarkFlightComplete(Status["MarkComplete"],1) + print("Sucess",Sucess," Message:",Message) + time.sleep(self.Sleep_Time) - complete = False - crashed = False - while not complete: #While drone not at target location - try: - if crashed: - D = AATC_Drone.CreateDroneInterface() - LoginSucess,Message = D.Login(DRONEID,DRONEPASSWORD) - crashed = False - while StatusQueue.empty(): - time.sleep(SLEEP_TIME) - Status = StatusQueue.get() - StatusQueue.task_done() - Sucess,Message = D.UpdateDroneStatus(Status["Coords"],Status["Battery"]) - if "MarkComplete" in Status: - complete = True - print("Flight ",Status["MarkComplete"]," complete") - Sucess,Message = D.MarkFlightComplete(Status["MarkComplete"],1) - print("Sucess",Sucess," Message:",Message) - time.sleep(SLEEP_TIME) - except Exception as e: - print("Error in drone logic flight stage",e) - crashed = True - -def toRadian(x): - return x*math.pi/180 -def CalculateVector(Coords,TargetCoords,Speed): - dx = TargetCoords.x- Coords.x - dy = TargetCoords.y- Coords.y - dz = TargetCoords.z- Coords.z +##def LaunchDroneLogic(DRONEID,DRONEPASSWORD,FlightQueue,StatusQueue,GPIO_Queue,SLEEP_TIME = 60): +## Exit = False +## while not Exit: +## try: +## D = AATC_Drone.CreateDroneInterface() +## LoginSucess,Message = D.Login(DRONEID,DRONEPASSWORD) +## print(LoginSucess,Message) +## if LoginSucess: +## +## AvailableFlight = False +## while not AvailableFlight: +## +## if not StatusQueue.empty(): +## Status = StatusQueue.get() +## StatusQueue.task_done() +## D.UpdateDroneStatus(Status["Coords"],Status["Battery"])#dfjsafkdsajdfjs +## +## Sucess,Message,FlightID = D.CheckForFlight() +## AvailableFlight = Sucess +## time.sleep(SLEEP_TIME) #At the end to pause to wait so that if the server is still writing waypoints it can finish. +## +## +## FlightID = FlightID[0][0] +## print("Obtaining flight and drone information. FlightID :",FlightID) +## DroneInfo, Flight, Waypoints = GetAllFlightInfo(D,DRONEID,FlightID) +## +## FlightQueue.put((False,(DroneInfo,Flight,Waypoints))) +## +## except Exception as e: +## print("Error occured in Drone Logic",str(e)) +## AvailableFlight = False +## +## if AvailableFlight: +## +## complete = False +## crashed = False +## while not complete: #While drone not at target location +## try: +## if crashed: +## D = AATC_Drone.CreateDroneInterface() +## LoginSucess,Message = D.Login(DRONEID,DRONEPASSWORD) +## crashed = False +## while StatusQueue.empty(): +## time.sleep(SLEEP_TIME) +## Status = StatusQueue.get() +## StatusQueue.task_done() +## Sucess,Message = D.UpdateDroneStatus(Status["Coords"],Status["Battery"]) +## if "MarkComplete" in Status: +## complete = True +## print("Flight ",Status["MarkComplete"]," complete") +## Sucess,Message = D.MarkFlightComplete(Status["MarkComplete"],1) +## print("Sucess",Sucess," Message:",Message) +## time.sleep(SLEEP_TIME) +## except Exception as e: +## print("Error in drone logic flight stage",e) +## crashed = True + + +def GetAllFlightInfo(D,DRONEID,FlightID): #Gets all drone flight information and packages the information into objects + DSucess,DroneMessage,DroneData = D.DroneGetDroneInfo(DRONEID) + FSucess,FlightMessage,FlightData = D.GetFlight(FlightID) + WSucess,WaypointsMessage,FlightWaypointsData = D.GetFlightWaypoints(FlightID) + if not (DSucess and FSucess and WSucess): + raise Exception("FlightData or FlightWaypointData was not sucessfully obtained") + + DroneInfo = AATC_Drone.MakeDroneInfo(DroneMessage, DroneData) + Flight = AATC_Drone.GetFlightObject(FlightMessage,FlightData) + Waypoints = AATC_Drone.GetWaypointObjects(WaypointsMessage,FlightWaypointsData) + + return DroneInfo,Flight,Waypoints - yCircumference = 40008000 - xCircumference = 40075160 - #Converts to metres - mdy = dy * yCircumference /360 - mdx = dx * xCircumference * math.cos(toRadian(TargetCoords.y)) /360 - - v = math.sqrt(mdx**2+mdy**2+ dz**2) - ratio = Speed/v - svx = dx*ratio #Gets Speed vectors - svy = dy*ratio - svz = dz*ratio - return AATC_Drone.Coordinate(svx,svy,svz) def AtWaypoint(Coords,WaypointCoords,xSize,ySize,zSize): x,y,z = False,False,False @@ -97,13 +163,6 @@ def AtWaypoint(Coords,WaypointCoords,xSize,ySize,zSize): z = True return all([x,y,z]) -def AddCoords(Coord,VectorCoord): #Simulates the drone moving - Coord.x += VectorCoord.x - Coord.y += VectorCoord.y - Coord.z += VectorCoord.z - time.sleep(0.1) - return Coord - def PutStatus(StatusQueue,Coords,Battery,MarkComplete = None,EmptyOverride = False): if StatusQueue.empty() or EmptyOverride: #Prevents adding a thousands of items into the queue, with the sending delayed by server speed. This method will cause the reported position to lag behind by a constant amount rather than getting progressivly further behind. @@ -127,26 +186,28 @@ def DroneHardware(FlightQueue,StatusQueue): while not AtWaypoint(Coords,Flight.StartCoord,xSize,ySize,zSize): - VectorCoord = CalculateVector(Coords,Flight.StartCoord,DroneInfo.DroneSpeed) - Coords = AddCoords(Coords,VectorCoord) + VectorCoord = AATC_Coordinate.CalculateVector(Coords,Flight.StartCoord,DroneInfo.DroneSpeed) + Coords = AATC_Coordinate.AddCoords(Coords,VectorCoord) PutStatus(StatusQueue,Coords,Battery) - Coords.Print() + print("Reached Start Coordinate") for point in Waypoints: while not AtWaypoint(Coords,point.Coord,xSize,ySize,zSize): - VectorCoord = CalculateVector(Coords,point.Coord,DroneInfo.DroneSpeed) - Coords = AddCoords(Coords,VectorCoord) + VectorCoord = AATC_Coordinate.CalculateVector(Coords,point.Coord,DroneInfo.DroneSpeed) + Coords = AATC_Coordinate.AddCoords(Coords,VectorCoord) PutStatus(StatusQueue,Coords,Battery) - Coords.Print() #probably too verbose + print("Reached Waypoint",point.WaypointNumber) while not AtWaypoint(Coords,Flight.EndCoord,xSize,ySize,zSize): - VectorCoord = CalculateVector(Coords,Flight.EndCoord,DroneInfo.DroneSpeed) - Coords = AddCoords(Coords,VectorCoord) + VectorCoord = AATC_Coordinate.CalculateVector(Coords,Flight.EndCoord,DroneInfo.DroneSpeed) + Coords = AATC_Coordinate.AddCoords(Coords,VectorCoord) PutStatus(StatusQueue,Coords,Battery) - Coords.Print() + print("Reached End Coordinate") + + PutStatus(StatusQueue,Coords,Battery,Flight.FlightID,EmptyOverride = True) # Updates Status and marks flight as complete. FlightQueue.task_done() @@ -154,8 +215,12 @@ def DroneHardware(FlightQueue,StatusQueue): def Startup(DroneID,DronePassword): FlightQueue = queue.Queue() StatusQueue = queue.Queue() + GPIO_Queue = AATC_GPIO.Create_Controller() + + droneLogicObject = DroneLogicSystem(DroneID,DronePassword,FlightQueue,StatusQueue,GPIO_Queue) + HardwareThread = threading.Thread(target = DroneHardware,args = (FlightQueue,StatusQueue)) - LogicThread = threading.Thread(target = LaunchDroneLogic, args = (DroneID,DronePassword,FlightQueue,StatusQueue)) + LogicThread = threading.Thread(target = droneLogicObject.Main) HardwareThread.start() LogicThread.start() diff --git a/AATC_GPIO.py b/AATC_GPIO.py index 9043ac1..51aacc9 100644 --- a/AATC_GPIO.py +++ b/AATC_GPIO.py @@ -1,5 +1,8 @@ import threading,multiprocessing,queue,time,random -#import RPi.GPIO as GPIO +try: + import RPi.GPIO as GPIO +except: + print("No RPi.GPIO module available. Features depending on this will not work/crash") ##GPIO.setmode(GPIO.BOARD) @@ -14,9 +17,9 @@ def GPIO_Thread(Thread_Name,GPIO_Queue): FuncArgs = () while not Exit: try: - FuncReset = Function(Thread_Name,*FuncArgs) #calls the function passed to the thread + Repeat = Function(Thread_Name,*FuncArgs) #calls the function passed to the thread - if FuncReset: + if Repeat: Function,FuncArgs = BlankFunction,() #Resets commands. Allows function to exit itself. if not GPIO_Queue.empty(): @@ -173,7 +176,7 @@ def Command(Thread_Name,arg1,arg2...): ... ... end of function code - return True/False # return FuncReset value. If True, function will not be repeated. If False, will continue until new command arrives + return True/False # return Repeat value. If True, function will be repeated. If False will exit. """ @@ -182,24 +185,51 @@ def Command(Thread_Name,arg1,arg2...): def BlankFunction(Thread_Name): time.sleep(0.2) - return False + return True def DisplayName(Thread_Name,Sleep_Time): print("Message from Thread",Thread_Name,". Sleeping for time",Sleep_Time) time.sleep(Sleep_Time) - return False + return True -def BlinkTest(Thread_Name,pin,frequency,cycles): #prints demonstration of blinking pin in text, Frequency in Hz, cycles = repeats till check for new instruction +def BlinkTest(Thread_Name,pin,frequency,cycles=1,repeat = False): #prints demonstration of blinking pin in text, Frequency in Hz, cycles = repeats till check for new instruction pauseTime = 1/(frequency*2) for x in range(cycles): print("Activating blink pin",pin, "Cycle:",x) time.sleep(pauseTime) print("Deactivating blink pin",pin, "Cycle:",x) time.sleep(pauseTime) - return True + + return repeat + + + + +##################General GPIO functions + +def Blink(Thread_Name,pin,frequency,cycles = 1,repeat= False): + pauseTime = 1/(frequency*2) + GPIO.setup(pin,GPIO.OUT) + + for x in range(cycles): + #On + GPIO.output(pin,1) + time.sleep(pauseTime) + + #Off + GPIO.output(pin,0) + time.sleep(pauseTime) + + return repeat + + + + + + -def Pattern(Thread_Name, Pattern ,ReferenceTime=1): +def Pattern(Thread_Name, Pattern ,ReferenceTime=1,repeat = True): try: GPIO.setmode(GPIO.BOARD) GPIO.setup(11, GPIO.OUT) #red @@ -218,7 +248,7 @@ def Pattern(Thread_Name, Pattern ,ReferenceTime=1): print("Exception in PatternTest",e) finally: GPIO.cleanup() - return False + return repeat def PatternGenerator(PinSet=[11,13,21],StateSet = [0,1] ,Length = 50 ,MinTime = 0.1 ,MaxTime = 1 , RoundingTime = 2): Pattern = [] diff --git a/AATC_Server_002.py b/AATC_Server_002.py index 1ecfd2c..78607d1 100644 --- a/AATC_Server_002.py +++ b/AATC_Server_002.py @@ -16,27 +16,8 @@ def CoordLessThanOrEqual(Coord1,Coord2):# True if Coord1 <= Coord2 BoolList.append(False) return all(BoolList) -def toRadian(x): - return x*math.pi/180 + -def toDegree(x): - return 180*x/math.pi - -def DeltaCoordToMetres(aCoord,bCoord): - #Formula for dx and dy from : https://stackoverflow.com/questions/3024404/transform-longitude-latitude-into-meters - dx = abs(aCoord.x - bCoord.x) - dy = abs(aCoord.y - bCoord.y) # in degrees - dz = abs(aCoord.z - bCoord.z) - - - yCircumference = 40008000 - xCircumference = 40075160 - - mdy = dy * yCircumference /360 - mdx = dx * xCircumference * math.cos(toRadian(aCoord.y)) /360 - - Distance = math.sqrt(mdx**2 + mdy**2 + dz**2) - return Distance @@ -482,7 +463,7 @@ def AddFlight(self,Arguments): TotalDistance = 0 for x in range(len(CoordList)): if x != 0: #If not first Coord add the difference in distance to time etc - Distance = DeltaCoordToMetres(CoordList[x]["Coords"],CoordList[x-1]["Coords"]) #Gets distance in metres + Distance = AATC_Coordinate.DeltaCoordToMetres(CoordList[x]["Coords"],CoordList[x-1]["Coords"]) #Gets distance in metres TotalDistance += Distance DeltaTime = Distance/DroneSpeed Time = Time + DeltaTime @@ -490,6 +471,8 @@ def AddFlight(self,Arguments): EndTime = Time # Time at which it would probably complete it + + #Adding Flight to Database self.DB.AddFlight(self.UserID,DroneID,HighPoints[0],HighPoints[len(HighPoints)-1],StartTime,EndTime,EndTime,TotalDistance,XOffset,YOffset,ZOffset) @@ -1012,8 +995,8 @@ def UpdateDroneStatus(self,Arguments): ######################################################## def DroneGetDroneInfo(self,Arguments): - DroneID,DronePassword = Arguments[0],Arguments[1] - Sucess,Message,Data = self.DB.DroneGetDroneInfo(DroneID,DronePassword) + DroneID = Arguments[0] + Sucess,Message,Data = self.DB.DroneGetDroneInfo(self.DroneID) return Sucess,Message,Data ##########################################################