2020-09-04 14:53:39 +00:00
|
|
|
import sys
|
|
|
|
import socket
|
|
|
|
import threading
|
|
|
|
import time
|
|
|
|
|
2020-10-15 20:41:30 +00:00
|
|
|
from mpc_controller import MPCController
|
2020-10-18 16:03:33 +00:00
|
|
|
from robot import Robot
|
2020-10-24 18:11:32 +00:00
|
|
|
from aruco_estimator import ArucoEstimator
|
2020-10-14 19:15:43 +00:00
|
|
|
|
2020-09-15 13:23:01 +00:00
|
|
|
MSGLEN = 64
|
2020-10-18 16:03:33 +00:00
|
|
|
|
2020-09-09 15:33:13 +00:00
|
|
|
def myreceive(sock):
|
|
|
|
chunks = []
|
|
|
|
bytes_recd = 0
|
|
|
|
while bytes_recd < MSGLEN:
|
2020-10-14 19:15:43 +00:00
|
|
|
chunk = sock.recv(1)
|
2020-09-09 15:33:13 +00:00
|
|
|
if chunk == b'':
|
|
|
|
raise RuntimeError("socket connection broken")
|
|
|
|
chunks.append(chunk)
|
|
|
|
bytes_recd = bytes_recd + len(chunk)
|
|
|
|
|
2020-10-14 19:15:43 +00:00
|
|
|
if chunks[-1] == b'\n':
|
2020-09-09 15:33:13 +00:00
|
|
|
break
|
|
|
|
|
|
|
|
return b''.join(chunks)
|
2020-09-04 14:53:39 +00:00
|
|
|
|
|
|
|
|
2020-10-18 16:03:33 +00:00
|
|
|
class RoboRallyRobot(Robot):
|
2020-10-15 18:35:32 +00:00
|
|
|
# dictionary mapping the current orientation and a turn command to the resulting orientation
|
|
|
|
resulting_orientation = {
|
|
|
|
'^': {'turn left': '<', 'turn right': '>', 'turn around': 'v'},
|
|
|
|
'>': {'turn left': '^', 'turn right': 'v', 'turn around': '<'},
|
|
|
|
'v': {'turn left': '>', 'turn right': '<', 'turn around': '^'},
|
|
|
|
'<': {'turn left': 'v', 'turn right': '^', 'turn around': '>'},
|
|
|
|
}
|
|
|
|
|
|
|
|
# dictionary mapping an orientation to its opposite
|
|
|
|
opposites = {'^': 'v', '>': '<', 'v': '^', '<': '>'}
|
|
|
|
|
2020-10-18 16:03:33 +00:00
|
|
|
def __init__(self, id, ip, x, y, orientation):
|
|
|
|
super().__init__(id, ip)
|
2020-09-04 14:53:39 +00:00
|
|
|
|
2020-10-18 16:03:33 +00:00
|
|
|
self.grid_x = x
|
|
|
|
self.grid_y = y
|
|
|
|
self.grid_orientation = orientation
|
2020-09-09 16:12:12 +00:00
|
|
|
|
2020-10-15 18:35:32 +00:00
|
|
|
def get_neighbor_coordinates(self, direction):
|
|
|
|
# get the coordinates of the neighboring tile in the given direction
|
|
|
|
if direction == '^':
|
2020-10-18 16:03:33 +00:00
|
|
|
return self.grid_x, self.grid_y - 1
|
2020-10-15 18:35:32 +00:00
|
|
|
elif direction == '>':
|
2020-10-18 16:03:33 +00:00
|
|
|
return self.grid_x + 1, self.grid_y
|
2020-10-15 18:35:32 +00:00
|
|
|
elif direction == 'v':
|
2020-10-18 16:03:33 +00:00
|
|
|
return self.grid_x, self.grid_y + 1
|
2020-10-15 18:35:32 +00:00
|
|
|
elif direction == '<':
|
2020-10-18 16:03:33 +00:00
|
|
|
return self.grid_x - 1, self.grid_y
|
2020-10-15 18:35:32 +00:00
|
|
|
else:
|
|
|
|
print("error: unknown direction")
|
|
|
|
sys.exit(1)
|
|
|
|
|
2020-10-18 16:03:33 +00:00
|
|
|
def move(self, move_type):
|
|
|
|
if move_type == 'forward':
|
|
|
|
target_tile = self.get_neighbor_coordinates(self.grid_orientation)
|
|
|
|
self.grid_x = target_tile[0]
|
|
|
|
self.grid_y = target_tile[1]
|
|
|
|
elif move_type == 'backward':
|
|
|
|
opposite_orientation = RoboRallyRobot.opposites[self.grid_orientation]
|
2020-10-15 18:35:32 +00:00
|
|
|
target_tile = self.get_neighbor_coordinates(opposite_orientation)
|
2020-10-18 16:03:33 +00:00
|
|
|
self.grid_x = target_tile[0]
|
|
|
|
self.grid_y = target_tile[1]
|
|
|
|
elif 'turn' in move_type:
|
|
|
|
self.grid_orientation = RoboRallyRobot.resulting_orientation[self.grid_orientation][move_type]
|
|
|
|
elif 'nop' in move_type:
|
|
|
|
pass # nop command -> robot grid position does not change (used e.g. for driving the robot to initial
|
|
|
|
# position)
|
2020-10-15 18:35:32 +00:00
|
|
|
else:
|
2020-10-24 18:11:32 +00:00
|
|
|
print(f"error: invalid move: {move_type}")
|
2020-10-15 18:35:32 +00:00
|
|
|
sys.exit(1)
|
2020-09-09 16:12:12 +00:00
|
|
|
|
2020-10-15 18:35:32 +00:00
|
|
|
def __str__(self):
|
|
|
|
return self.__repr__()
|
|
|
|
|
|
|
|
def __repr__(self):
|
2020-10-18 16:03:33 +00:00
|
|
|
return f"grid x: {self.grid_x}, grid y: {self.grid_y}, grid orientation: {self.grid_orientation}"
|
2020-10-15 18:35:32 +00:00
|
|
|
|
2020-09-04 14:53:39 +00:00
|
|
|
|
|
|
|
class RemoteController:
|
2020-10-18 16:03:33 +00:00
|
|
|
valid_cmds = ['forward', 'backward', 'turn left', 'turn right', 'turn around', 'nop', 'get position',
|
|
|
|
'set position', 'initialize_robot', 'initialize_grid']
|
|
|
|
|
2020-10-15 20:41:30 +00:00
|
|
|
def __init__(self):
|
2020-10-18 16:03:33 +00:00
|
|
|
self.robots = []
|
2020-10-24 18:11:32 +00:00
|
|
|
#self.robots = [RoboRallyRobot(12, '192.168.1.12', x=1, y=1, orientation='>')]
|
2020-09-04 14:53:39 +00:00
|
|
|
|
|
|
|
self.robot_ids = {}
|
|
|
|
for r in self.robots:
|
|
|
|
self.robot_ids[r.id] = r
|
|
|
|
|
2020-09-09 15:33:13 +00:00
|
|
|
# socket for movement commands
|
|
|
|
self.comm_socket = socket.socket()
|
|
|
|
self.comm_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
|
|
|
2020-09-09 16:12:12 +00:00
|
|
|
for robot in self.robots:
|
|
|
|
robot.connect()
|
|
|
|
|
2020-09-09 15:33:13 +00:00
|
|
|
self.comm_socket.bind(('', 1337))
|
|
|
|
self.comm_socket.listen(5)
|
2020-09-04 14:53:39 +00:00
|
|
|
|
|
|
|
self.t = time.time()
|
|
|
|
|
2020-10-15 20:41:30 +00:00
|
|
|
# start thread for marker position detection
|
2020-10-24 18:11:32 +00:00
|
|
|
self.estimator = ArucoEstimator(self.robot_ids.keys())
|
2020-10-14 19:15:43 +00:00
|
|
|
self.estimator_thread = threading.Thread(target=self.estimator.run_tracking)
|
|
|
|
self.estimator_thread.start()
|
2020-09-04 14:53:39 +00:00
|
|
|
|
2020-10-15 20:41:30 +00:00
|
|
|
self.controller = MPCController(self.estimator)
|
2020-09-04 14:53:39 +00:00
|
|
|
|
2020-10-18 16:03:33 +00:00
|
|
|
print("waiting for corner and robot detection..")
|
|
|
|
while not self.estimator.all_robots_detected() or not self.estimator.all_corners_detected():
|
|
|
|
pass
|
|
|
|
print("everything detected!")
|
|
|
|
# drive robots to initial position
|
|
|
|
for robot_id in self.robot_ids:
|
|
|
|
self.grid_control(robot_id, 'nop')
|
|
|
|
|
2020-10-15 20:41:30 +00:00
|
|
|
def run(self):
|
2020-10-15 18:35:32 +00:00
|
|
|
print("waiting until all markers are detected...")
|
|
|
|
while not self.estimator.all_corners_detected():
|
|
|
|
pass
|
2020-09-04 14:53:39 +00:00
|
|
|
print("starting control")
|
|
|
|
|
2020-09-09 15:33:13 +00:00
|
|
|
running = True
|
|
|
|
while running:
|
|
|
|
(clientsocket, address) = self.comm_socket.accept()
|
2020-09-09 18:21:34 +00:00
|
|
|
clientsocket.settimeout(None)
|
2020-09-04 14:53:39 +00:00
|
|
|
|
2020-09-09 15:33:13 +00:00
|
|
|
connected = True
|
|
|
|
while connected:
|
|
|
|
try:
|
|
|
|
data = myreceive(clientsocket)
|
2020-09-15 13:23:01 +00:00
|
|
|
print("data received: ", data)
|
2020-09-09 15:33:13 +00:00
|
|
|
|
2020-09-09 18:21:34 +00:00
|
|
|
inputs = data.split(b',')
|
|
|
|
cmd = inputs[0]
|
2020-10-14 19:15:43 +00:00
|
|
|
cmd = cmd.strip().decode()
|
2020-09-09 18:21:34 +00:00
|
|
|
|
|
|
|
if len(inputs) > 1:
|
2020-10-18 16:03:33 +00:00
|
|
|
if cmd in RemoteController.valid_cmds:
|
|
|
|
if cmd == 'initialize_grid':
|
|
|
|
try:
|
|
|
|
grid_columns = int(inputs[1])
|
|
|
|
grid_rows = int(inputs[2])
|
|
|
|
|
|
|
|
self.estimator.grid_columns = grid_columns
|
|
|
|
self.estimator.grid_rows = grid_rows
|
|
|
|
clientsocket.sendall(b'OK\n')
|
|
|
|
except ValueError:
|
|
|
|
print("could not initialize grid!")
|
|
|
|
clientsocket.sendall(b'"could not initialize grid!\n'
|
|
|
|
b'expected: initialize_robot, <grid columns>, <grid rows>')
|
|
|
|
except IndexError:
|
|
|
|
print("could not initialize grid!")
|
|
|
|
clientsocket.sendall(b'"could not initialize grid!\n'
|
|
|
|
b'expected: initialize_robot, <grid columns>, <grid rows>')
|
|
|
|
else: # robot command
|
2020-09-09 18:21:34 +00:00
|
|
|
try:
|
|
|
|
robot_id = int(inputs[1])
|
|
|
|
except ValueError:
|
2020-10-18 16:03:33 +00:00
|
|
|
robot_id = None
|
2020-09-09 18:21:34 +00:00
|
|
|
print("could not read robot id!")
|
|
|
|
clientsocket.sendall(b'Could not read robot id!\n')
|
|
|
|
|
2020-10-24 18:11:32 +00:00
|
|
|
if cmd == 'initialize_robot':
|
2020-10-18 16:03:33 +00:00
|
|
|
# add a new robot to the game
|
|
|
|
try:
|
|
|
|
id = int(inputs[1])
|
|
|
|
ip = inputs[2].decode().strip()
|
|
|
|
x = int(inputs[3])
|
|
|
|
y = int(inputs[4])
|
|
|
|
orientation = inputs[5].decode().strip()
|
|
|
|
|
|
|
|
print(f"initializing new robot with id {id} and ip {ip} at pos ({x},{y}) with "
|
|
|
|
f"orientation '{orientation}'")
|
2020-10-24 18:11:32 +00:00
|
|
|
if id not in self.robot_ids:
|
|
|
|
new_robot = RoboRallyRobot(id=id, ip=ip, x=x, y=y, orientation=orientation)
|
|
|
|
new_robot.connect()
|
|
|
|
|
2020-10-18 16:03:33 +00:00
|
|
|
# store the new robot in the list of robots
|
|
|
|
self.robots.append(new_robot)
|
2020-10-24 18:11:32 +00:00
|
|
|
self.robot_ids[new_robot.id] = new_robot # this also means the estimator
|
|
|
|
# will track the new robot because
|
|
|
|
# it got a reference to the list of
|
|
|
|
# robot ids to keep an eye out for
|
|
|
|
|
|
|
|
else: # robot is already created -> just re-initialize its position
|
|
|
|
new_robot = self.robot_ids[id]
|
|
|
|
new_robot.grid_x = x
|
|
|
|
new_robot.grid_y = y
|
|
|
|
new_robot.orientation = orientation
|
|
|
|
|
|
|
|
if new_robot.connected:
|
|
|
|
print("created new robot and successfully connected to it!")
|
2020-10-18 16:03:33 +00:00
|
|
|
|
|
|
|
while not self.estimator.all_robots_detected(): # wait until the robot gets detected
|
|
|
|
pass
|
|
|
|
|
|
|
|
# drive the new robot to its starting position
|
|
|
|
self.grid_control(new_robot.id, 'nop')
|
|
|
|
|
|
|
|
clientsocket.sendall(b'OK\n')
|
|
|
|
else:
|
|
|
|
clientsocket.sendall(f"error: could not connect to new robot {new_robot}".encode())
|
|
|
|
|
|
|
|
except IndexError:
|
|
|
|
print("could not initialize a new robot")
|
|
|
|
clientsocket.sendall('could not initialize a new robot: invalid command format\n'
|
|
|
|
'expected: initialize_robot, <id>, <ip>, <x>, <y>, <orientation>\n'.encode())
|
|
|
|
except ValueError:
|
|
|
|
print("could not initialize a new robot")
|
|
|
|
clientsocket.sendall('could not initialize a new robot: invalid command format\n'
|
|
|
|
'expected: initialize_robot, <id>, <ip>, <x>, <y>, <orientation>\n'.encode())
|
2020-10-24 18:11:32 +00:00
|
|
|
|
|
|
|
elif robot_id in self.robot_ids:
|
|
|
|
if cmd == b'get position':
|
|
|
|
clientsocket.sendall(bytes('{}\n'.format(self.robot_ids[robot_id].grid_pos)))
|
|
|
|
elif cmd == b'set position':
|
|
|
|
try:
|
|
|
|
pos_data = ",".join(inputs[2:])
|
|
|
|
new_grid_pos = tuple(map(lambda x: int(x[1]) if x[0] < 2 else float(x[1]),
|
|
|
|
enumerate(pos_data.strip().strip('()').split(','))))
|
|
|
|
self.robot_ids[robot_id].grid_pos = new_grid_pos
|
|
|
|
clientsocket.sendall(b'OK\n')
|
|
|
|
except IndexError as e:
|
|
|
|
print("could not set grid position!")
|
|
|
|
clientsocket.sendall(bytes(
|
|
|
|
'could not set grid position! (invalid format)\n'.format(
|
|
|
|
self.robot_ids[robot_id].grid_pos)))
|
|
|
|
except ValueError as e:
|
|
|
|
print("could not set grid position!")
|
|
|
|
clientsocket.sendall(bytes(
|
|
|
|
'could not set grid position! (invalid format)\n'.format(
|
|
|
|
self.robot_ids[robot_id].grid_pos)))
|
|
|
|
else:
|
|
|
|
self.grid_control(robot_id, cmd)
|
|
|
|
clientsocket.sendall(b'OK\n')
|
|
|
|
|
2020-09-09 18:21:34 +00:00
|
|
|
else:
|
|
|
|
print("invalid robot id!")
|
|
|
|
clientsocket.sendall(b'Invalid robot id!\n')
|
|
|
|
else:
|
|
|
|
clientsocket.sendall(b'Invalid command!\n')
|
2020-10-18 16:03:33 +00:00
|
|
|
else: # len(inputs) <= 1
|
2020-10-14 19:15:43 +00:00
|
|
|
if b'quit' in inputs[0]:
|
2020-09-09 15:33:13 +00:00
|
|
|
clientsocket.close()
|
|
|
|
self.comm_socket.close()
|
|
|
|
connected = False
|
|
|
|
running = False
|
|
|
|
else:
|
2020-09-09 18:21:34 +00:00
|
|
|
print("could not process command!")
|
|
|
|
clientsocket.sendall(b'Could not process request!\n')
|
|
|
|
|
2020-09-09 15:33:13 +00:00
|
|
|
except RuntimeError:
|
|
|
|
print("disconnected")
|
|
|
|
connected = False
|
|
|
|
clientsocket.close()
|
|
|
|
|
2020-10-15 20:41:30 +00:00
|
|
|
def grid_control(self, robot_id, cmd):
|
2020-10-18 16:03:33 +00:00
|
|
|
robot = self.robot_ids[robot_id] # get robot to be controlled
|
2020-09-04 14:53:39 +00:00
|
|
|
|
2020-10-15 18:35:32 +00:00
|
|
|
print("robot grid pos before move: ", robot)
|
|
|
|
robot.move(cmd)
|
|
|
|
print("robot grid pos after move: ", robot)
|
2020-09-04 14:53:39 +00:00
|
|
|
|
2020-10-18 16:03:33 +00:00
|
|
|
target = self.estimator.get_pos_from_grid_point(robot.grid_x, robot.grid_y, robot.grid_orientation)
|
2020-09-04 14:53:39 +00:00
|
|
|
|
2020-10-15 20:41:30 +00:00
|
|
|
self.controller.move_to_pos(target, robot)
|
2020-09-04 14:53:39 +00:00
|
|
|
|
2020-10-18 16:03:33 +00:00
|
|
|
|
2020-10-15 20:41:30 +00:00
|
|
|
def main(args):
|
|
|
|
rc = RemoteController()
|
|
|
|
rc.run()
|
2020-09-04 14:53:39 +00:00
|
|
|
|
2020-10-18 16:03:33 +00:00
|
|
|
|
2020-09-04 14:53:39 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main(sys.argv)
|