Skip to content

Commit

Permalink
Merge pull request #20 from Scratchcat1/Cleaning
Browse files Browse the repository at this point in the history
Cleaning
  • Loading branch information
Scratchcat1 authored Nov 7, 2017
2 parents b46b68b + 7884490 commit b671c35
Show file tree
Hide file tree
Showing 10 changed files with 271 additions and 141 deletions.
19 changes: 11 additions & 8 deletions AATC_AStar.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import os,pickle,heapq,time,math,hashlib
import os,pickle,time,math,hashlib
from AATC_Coordinate import *
try:
try:
import PriorityQueue.PriorityQueueC as PriorityQueue
except:
print("PriotityQueueC not available, defaulting to pure python")
import PriorityQueue.PriorityQueue
import PriorityQueue.PriorityQueue as PriorityQueue

except:
print("AStarPQ not available")
Expand Down Expand Up @@ -232,7 +232,7 @@ def Find_NodeID(self,x,y,z):
return NodeID

def Obj_Find_NodeID(self,Obj):
x,y,z = Obj.Coords.x,Obj.Coords.y,Obj.Coords.z
x,y,z = Obj.Coords.Get_X(),Obj.Coords.Get_Y(),Obj.Coords.Get_Z()
NodeID = self.Find_NodeID(x,y,z)
return NodeID

Expand Down Expand Up @@ -340,12 +340,15 @@ def Get_Coords(self):
def Get_Cost(self):
return self.Cost

def Set_Cost(self,cost):
self.Cost = cost

def EstimateDistance(Node,Target,xSize,ySize,zSize):
Node_Coords = Node.Get_Coords()
Target_Coords = Target.Get_Coords()
return abs(Node_Coords.Get_X()-Target_Coords.Get_X())/xSize+abs(Node_Coords.Get_Y()-Target_Coords.Get_Y())/ySize+abs(Node_Coords.Get_Z()-Target_Coords.Get_Z())/zSize

def AStarPQ(graph,start,target,xSize=1,ySize=1,zSize = 1): # Set all g to node_count + 1
def AStarPQ(graph,start,target): # Set all g to node_count + 1
StartTime = time.time()

xSize,ySize,zSize = graph.Get_Size()
Expand Down Expand Up @@ -387,9 +390,9 @@ def AStarPQ(graph,start,target,xSize=1,ySize=1,zSize = 1): # Set all g to node
cameFrom[NodeID] = current
g[NodeID] = tScore
fp.remove((f[NodeID],NodeID))
x = g[NodeID] + EstimateDistance(NewNode,graph.GetNode(target),xSize,ySize,zSize)
f[NodeID] = x
fp.put((x,NodeID))
fTemp = g[NodeID] + EstimateDistance(NewNode,graph.GetNode(target),xSize,ySize,zSize)
f[NodeID] = fTemp
fp.put((fTemp,NodeID))

f.pop(current) #These values will not be refered to again since the current NodeID has been moved to the closed set . This therefore reduces memory usage very slightly
g.pop(current)
Expand All @@ -404,7 +407,7 @@ def AStarPQ(graph,start,target,xSize=1,ySize=1,zSize = 1): # Set all g to node
print(FindPath(cameFrom,current))
return None

def AStar2(graph,start,target,xSize=1,ySize=1,zSize = 1): # Set all g to node_count + 1
def AStar2(graph,start,target): # Set all g to node_count + 1
StartTime = time.time()

xSize,ySize,zSize = graph.Get_Size()
Expand Down
89 changes: 64 additions & 25 deletions AATC_Coordinate.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,78 @@

class Coordinate:
def __init__(self,x,y,z=0,xSize=0,ySize=0,zSize=0):
self.x = x
self.y = y
self.z = z
self.xSize = xSize
self.ySize = ySize
self.zSize = zSize
self._x = x
self._y = y
self._z = z
self._xSize = xSize
self._ySize = ySize
self._zSize = zSize

def Get_X(self):
return self.x
return self._x
def Get_Y(self):
return self.y
return self._y
def Get_Z(self):
return self.z
return self._z

def Set_X(self,x):
self._x = x
def Set_Y(self,y):
self._y = y
def Set_Z(self,z):
self._z = z

def Get_XSize(self):
return self._xSize
def Get_YSize(self):
return self._ySize
def Get_ZSize(self):
return self._zSize

def Set_XSize(self,x):
self._xSize = x
def Set_YSize(self,y):
self._ySize = y
def Set_ZSize(self,z):
self._zSize = z


def Print(self):
print("Coordinate:")
print("X: {:<8} xSize:{:<8}".format(round(self.x,7),self.xSize))
print("Y: {:<8} ySize:{:<8}".format(round(self.y,7),self.ySize))
print("Z: {:<8} zSize:{:<8}".format(round(self.z,7),self.zSize))
print("X: {:<8} xSize:{:<8}".format(round(self._x,7),self._xSize))
print("Y: {:<8} ySize:{:<8}".format(round(self._y,7),self._ySize))
print("Z: {:<8} zSize:{:<8}".format(round(self._z,7),self._zSize))

def __str__(self):
return str((round(self.x,8),round(self.y,8),round(self.z,8)))
return str((round(self._x,8),round(self._y,8),round(self._z,8)))

def getTuple(self):
return (self._x,self._y,self._z)

def copy(self):
return Coordinate(self.x,self.y,self.z,self.xSize,self.ySize,self.zSize)
return Coordinate(self._x,self._y,self._z,self._xSize,self._ySize,self._zSize)


def AddCoords(Coord,VectorCoord): #Simulates the drone moving
Coord.x += VectorCoord.x
Coord.y += VectorCoord.y
Coord.z += VectorCoord.z
x = Coord.Get_X() + VectorCoord.Get_X()
y = Coord.Get_Y() + VectorCoord.Get_Y()
z = Coord.Get_Z() + VectorCoord.Get_Z()
Coord.Set_X(x)
Coord.Set_Y(y)
Coord.Set_Z(z)
return Coord

def CalculateVector(Coords,TargetCoords,Speed):
dx = TargetCoords.x- Coords.x
dy = TargetCoords.y- Coords.y
dz = TargetCoords.z- Coords.z
dx = TargetCoords.Get_X()- Coords.Get_X()
dy = TargetCoords.Get_Y()- Coords.Get_Y()
dz = TargetCoords.Get_Z()- Coords.Get_Z()


yCircumference = 40008000
xCircumference = 40075160
#Converts to metres
mdy = dy * yCircumference /360
mdx = dx * xCircumference * math.cos(toRadian(TargetCoords.y)) /360
mdx = dx * xCircumference * math.cos(toRadian(TargetCoords.Get_Y())) /360

v = math.sqrt(mdx**2+mdy**2+ dz**2)
ratio = Speed/v
Expand All @@ -61,18 +89,29 @@ def toDegree(x):
return 180*x/math.pi


def AtWaypoint(Coords,WaypointCoords,xSize,ySize,zSize):
x,y,z = False,False,False
if abs(Coords.Get_X()-WaypointCoords.Get_X()) <= xSize:
x = True
if abs(Coords.Get_Y()-WaypointCoords.Get_Y()) <= ySize:
y = True
if abs(Coords.Get_Z()-WaypointCoords.Get_Z()) <= zSize:
z = True
return all([x,y,z])


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)
dx = abs(aCoord.Get_X() - bCoord.Get_X())
dy = abs(aCoord.Get_Y() - bCoord.Get_Y()) # in degrees
dz = abs(aCoord.Get_Z() - bCoord.Get_Z())


yCircumference = 40008000
xCircumference = 40075160

mdy = dy * yCircumference /360
mdx = dx * xCircumference * math.cos(toRadian(aCoord.y)) /360
mdx = dx * xCircumference * math.cos(toRadian(aCoord.Get_Y())) /360

Distance = math.sqrt(mdx**2 + mdy**2 + dz**2)
return Distance
2 changes: 1 addition & 1 deletion AATC_DB.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ def GetFlight(self,DroneID,FlightID):

def MarkFlightComplete(self,DroneID,FlightID,Code):
self.cur.execute("SELECT 1 FROM Flight WHERE DroneID = %s AND FlightID = %s",(DroneID,FlightID))
if self.cur.fetchall != ():
if len(self.cur.fetchall) == 0:
self.cur.execute("UPDATE Flight SET Completed = %s,EndTime = %s WHERE FlightID = %s",(Code,GetTime(),FlightID))
self.cur.execute("UPDATE Drone SET FlightsFlown = FlightsFlown +1 WHERE DroneID = %s",(DroneID,))
self.db_con.commit()
Expand Down
109 changes: 76 additions & 33 deletions AATC_Drone.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import socket,codecs,ast,recvall,sys,heapq,AATC_Crypto
from AATC_Coordinate import *
import socket,codecs,ast,recvall,sys,AATC_Crypto, AATC_Coordinate


class DroneInterface:
Expand Down Expand Up @@ -39,8 +38,7 @@ def Recv(self): #Returns tuple of Sucess,Message,Data of which data may just
# Sucess, Message , Data
return data[0],data[1],data[2]
except Exception as e:
print("Socket data recive error")
print(str(e))
print("Socket data recive error",e)
return (False,"Conversion/Transfer Error"+str(e),[])


Expand All @@ -62,8 +60,8 @@ def UpdateDroneStatus(self,LastCoords,LastBattery):

############################

def DroneGetDroneInfo(self,DroneID):
self.Send("DroneGetDroneInfo",(DroneID,))
def DroneGetDroneInfo(self):
self.Send("DroneGetDroneInfo",())
Sucess,Message,DroneInfo = self.Recv()
return Sucess,Message,DroneInfo

Expand Down Expand Up @@ -93,27 +91,57 @@ def MarkFlightComplete(self,FlightID,Code):

class Flight:
def __init__(self,FlightID,DroneID,StartCoord,EndCoord,StartTime,ETA,Distance):
self.FlightID = FlightID
self.DroneID = DroneID
self.StartCoord = StartCoord
self.EndCoord = EndCoord
self.StartTime = StartTime
self.ETA = ETA
self.Distance = Distance
self._FlightID = FlightID
self._DroneID = DroneID
self._StartCoord = StartCoord
self._EndCoord = EndCoord
self._StartTime = StartTime
self._ETA = ETA
self._Distance = Distance

def Get_FlightID(self):
return self._FlightID

def Get_DroneID(self):
return self._DroneID

def Get_StartCoord(self):
return self._StartCoord

def Get_EndCoord(self):
return self._EndCoord

def Get_StartTime(self):
return self._EndCoord

def Get_Distance(self):
return self._Distance

class Waypoint:
def __init__(self,FlightID,WaypointNumber,Coord,ETA):
self.FlightID = FlightID
self.WaypointNumber = WaypointNumber
self.Coord = Coord
self.ETA = ETA
self._FlightID = FlightID
self._WaypointNumber = WaypointNumber
self._Coord = Coord
self._ETA = ETA

def Get_FlightID(self):
return self._FlightID

def Get_WaypointNumber(self):
return self._WaypointNumber

def Get_Coord(self):
return self._Coord

def Get_ETA(self):
return self._ETA




def ConvertCoordString(CoordString):
CoordTuple = ast.literal_eval(CoordString)
Coord = Coordinate(CoordTuple[0],CoordTuple[1],CoordTuple[2],0,0,0)
Coord = AATC_Coordinate.Coordinate(CoordTuple[0],CoordTuple[1],CoordTuple[2],0,0,0)
return Coord

def GetFlightObject(Message,Data):
Expand Down Expand Up @@ -160,14 +188,14 @@ def GetWaypointObjects(Message,Data):
waypoint = Waypoint(FlightID,WaypointNumber,Coord,ETA)
WaypointList.append(waypoint)


z = []
for item in WaypointList: #Sorts into waypoint order
z.append((item.WaypointNumber,item))
heapq.heapify(z)
WaypointList = []
for item in z:
WaypointList.append(item[1])
WaypointList = sorted(WaypointList, key = lambda x:x.Get_WaypointNumber())
## z = []
## for item in WaypointList: #Sorts into waypoint order
## z.append((item.WaypointNumber,item))
## heapq.heapify(z)
## WaypointList = []
## for item in z:
## WaypointList.append(item[1])
return WaypointList

def MakeDroneInfo(DroneMessage,DroneData):
Expand All @@ -182,13 +210,28 @@ def __init__(self,Message,DroneInfo):
for title in Titles:
ColumnValue[title] = DroneInfo[Message.index(title)]

self.DroneID = ColumnValue["DroneID"]
self.UserID = ColumnValue["UserID"]
self.DroneName = ColumnValue["DroneName"]
self.DroneType = ColumnValue["DroneType"]
self.DroneSpeed = ColumnValue["DroneSpeed"]
self.DroneRange = ColumnValue["DroneRange"]
self.DroneWeight = ColumnValue["DroneWeight"]
self._DroneID = ColumnValue["DroneID"]
self._UserID = ColumnValue["UserID"]
self._DroneName = ColumnValue["DroneName"]
self._DroneType = ColumnValue["DroneType"]
self._DroneSpeed = ColumnValue["DroneSpeed"]
self._DroneRange = ColumnValue["DroneRange"]
self._DroneWeight = ColumnValue["DroneWeight"]

def Get_DroneID(self):
return self._DroneID
def Get_UserID(self):
return self._UserID
def Get_DroneName(self):
return self._DroneName
def Get_DroneType(self):
return self._DroneType
def Get_DroneSpeed(self):
return self._DroneSpeed
def Get_DroneRange(self):
return self._DroneRange
def Get_DroneWeight(self):
return self._DroneWeight



Expand Down
Loading

0 comments on commit b671c35

Please sign in to comment.