2019-04-29 06:52:57 +02:00
|
|
|
import asyncio
|
|
|
|
import sys
|
|
|
|
import time
|
2019-04-29 10:49:03 +02:00
|
|
|
from queue import PriorityQueue
|
2019-04-29 06:52:57 +02:00
|
|
|
|
|
|
|
def heuristic(a, b):
|
|
|
|
(x1, y1) = a
|
|
|
|
(x2, y2) = b
|
|
|
|
return abs(x1 - x2) + abs(y1 - y2)
|
|
|
|
|
2019-04-30 00:08:38 +02:00
|
|
|
def addnode(local_graph, direction):
|
|
|
|
if direction == 'N':
|
|
|
|
local_graph.append((current[0] - 1, current[1]))
|
|
|
|
elif direction == 'S':
|
|
|
|
local_graph.append((current[0] + 1, current[1]))
|
|
|
|
elif direction == 'W':
|
|
|
|
local_graph.append((current[0], current[1] - 1 ))
|
|
|
|
else:
|
|
|
|
local_graph.append((current[0], current[1] + 1 ))
|
2019-04-29 06:52:57 +02:00
|
|
|
|
2019-04-30 00:08:38 +02:00
|
|
|
return local_graph
|
|
|
|
|
|
|
|
def getrotation(a, b):
|
|
|
|
y = b[0] - a[0]
|
|
|
|
x = b[1] - a[1]
|
|
|
|
if y > 0:
|
|
|
|
return 'S'
|
|
|
|
elif y < 0:
|
|
|
|
return 'N'
|
|
|
|
elif x > 0:
|
|
|
|
return 'E'
|
|
|
|
else:
|
|
|
|
return 'W'
|
|
|
|
|
|
|
|
|
|
|
|
async def move_tractor(start_position, goal):
|
2019-04-29 10:49:03 +02:00
|
|
|
directions = ['N', 'W', 'S', 'E']
|
|
|
|
current_rotation = 'N'
|
2019-04-30 00:08:38 +02:00
|
|
|
last_node = None
|
|
|
|
current_position = start_position
|
2019-04-29 06:52:57 +02:00
|
|
|
|
|
|
|
frontier = PriorityQueue()
|
|
|
|
frontier.put(start, 0)
|
|
|
|
cost_so_far = {}
|
|
|
|
cost_so_far[start] = 0
|
2019-04-29 10:49:03 +02:00
|
|
|
start_flag = True
|
2019-04-29 06:52:57 +02:00
|
|
|
|
|
|
|
while not frontier.empty():
|
2019-04-29 10:49:03 +02:00
|
|
|
local_graph = []
|
2019-04-30 00:08:38 +02:00
|
|
|
|
|
|
|
for direction in directions:
|
|
|
|
if direction == last_node:
|
2019-04-29 10:49:03 +02:00
|
|
|
continue
|
2019-04-30 00:08:38 +02:00
|
|
|
|
2019-04-29 10:49:03 +02:00
|
|
|
reader, writer = await asyncio.open_connection('127.0.0.1', 8888)
|
|
|
|
writer.write(("rotate " + direction + "\n").encode())
|
2019-04-30 00:08:38 +02:00
|
|
|
await reader.readline()
|
2019-04-29 10:49:03 +02:00
|
|
|
time.sleep(0.7)
|
2019-04-30 00:08:38 +02:00
|
|
|
writer.close()
|
|
|
|
|
2019-04-29 10:49:03 +02:00
|
|
|
reader, writer = await asyncio.open_connection('127.0.0.1', 8888)
|
2019-04-30 00:08:38 +02:00
|
|
|
writer.write("try\n".encode())
|
2019-04-29 10:49:03 +02:00
|
|
|
data = await reader.readline()
|
2019-04-30 00:08:38 +02:00
|
|
|
result = data.decode()
|
2019-04-29 10:49:03 +02:00
|
|
|
time.sleep(0.7)
|
2019-04-30 00:08:38 +02:00
|
|
|
writer.close()
|
|
|
|
|
|
|
|
if result == "OK":
|
|
|
|
local_graph = addnode(local_graph, direction)
|
|
|
|
|
|
|
|
current_rotation = directions[(directions.index(current_rotation) - 1) % 4]
|
|
|
|
|
|
|
|
|
|
|
|
current = frontier.get()
|
|
|
|
if not start_flag:
|
|
|
|
rotation = getrotation(current_position, current)
|
|
|
|
if rotation != current_rotation:
|
2019-04-29 10:49:03 +02:00
|
|
|
reader, writer = await asyncio.open_connection('127.0.0.1', 8888)
|
2019-04-30 00:08:38 +02:00
|
|
|
writer.write(("rotate " + rotation + "\n").encode())
|
|
|
|
await reader.readline()
|
|
|
|
current_rotation = rotation
|
2019-04-29 10:49:03 +02:00
|
|
|
time.sleep(0.7)
|
|
|
|
writer.close()
|
2019-04-30 00:08:38 +02:00
|
|
|
last_node = directions[(directions.index(rotation) - 2) % 4]
|
2019-04-29 10:49:03 +02:00
|
|
|
|
|
|
|
reader, writer = await asyncio.open_connection('127.0.0.1', 8888)
|
|
|
|
writer.write("move\n".encode())
|
2019-04-30 00:08:38 +02:00
|
|
|
await reader.readline()
|
|
|
|
current_position = current
|
2019-04-29 10:49:03 +02:00
|
|
|
time.sleep(0.7)
|
2019-04-30 00:08:38 +02:00
|
|
|
writer.close()
|
2019-04-29 06:52:57 +02:00
|
|
|
|
|
|
|
if current == goal:
|
|
|
|
break
|
|
|
|
|
2019-04-29 10:49:03 +02:00
|
|
|
for next in local_graph:
|
2019-04-29 06:52:57 +02:00
|
|
|
new_cost = cost_so_far[current] + 1
|
|
|
|
if next not in cost_so_far or new_cost < cost_so_far[next]:
|
|
|
|
cost_so_far[next] = new_cost
|
|
|
|
priority = new_cost + heuristic(goal, next)
|
|
|
|
frontier.put(next, priority)
|
2019-04-30 00:08:38 +02:00
|
|
|
print(next)
|
2019-04-29 10:49:03 +02:00
|
|
|
|
|
|
|
start_flag = False
|
2019-04-30 00:08:38 +02:00
|
|
|
writer.close()
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
start = (0,0)
|
|
|
|
goal = (10,6)
|
|
|
|
asyncio.run(move_tractor(start, goal))
|