From 019c4590aa834a92bd2237f7076f5461d54e45f3 Mon Sep 17 00:00:00 2001 From: Simon Pirkelmann Date: Tue, 7 Sep 2021 22:35:02 +0200 Subject: [PATCH] implemented communication interface with real robots --- gauss-turing/webserver/event_server_comm.py | 15 ++++++++++++++ gauss-turing/webserver/gauss_turing.py | 23 +++++++++++++++++---- 2 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 gauss-turing/webserver/event_server_comm.py diff --git a/gauss-turing/webserver/event_server_comm.py b/gauss-turing/webserver/event_server_comm.py new file mode 100644 index 0000000..d3cb082 --- /dev/null +++ b/gauss-turing/webserver/event_server_comm.py @@ -0,0 +1,15 @@ +import socket +import json + +HOST, PORT = "localhost", 42424 + +def move_grid(x, y, orientation, dimx, dimy): + sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + sock.connect((HOST, PORT)) + payload = json.dumps({"x": x, "y": y, "dimx": dimx, "dimy": dimy, "orientation": orientation}) + sock.sendall(f"move_grid_blocking;{payload}\n".encode()) + reply = sock.recv(1024) + print(reply) + +if __name__ == "__main__": + move_grid(1,1,'^', 5, 5) \ No newline at end of file diff --git a/gauss-turing/webserver/gauss_turing.py b/gauss-turing/webserver/gauss_turing.py index d0141cf..0e2befb 100644 --- a/gauss-turing/webserver/gauss_turing.py +++ b/gauss-turing/webserver/gauss_turing.py @@ -3,6 +3,8 @@ import random import pygame import os +from event_server_comm import move_grid + BLACK = np.array([0, 0, 0], dtype=np.uint8) WHITE = np.array([255, 255, 255], dtype=np.uint8) RED = np.array([255, 0, 0], dtype=np.uint8) @@ -22,6 +24,7 @@ P0_text = myfont.render('P0', False, tuple(BLACK)) tiledt = np.dtype([('x', np.uint8), ('y', np.uint8), ('color', np.uint8, 3), ('star', np.bool)]) + class Board: valid_colors = [WHITE, RED, BLUE] @@ -29,8 +32,6 @@ class Board: self.tiles = np.zeros((dim_y, dim_x), dtype=tiledt) for x in range(dim_x): for y in range(dim_y): - self.tiles[y, x]['x'] = x - self.tiles[y, x]['y'] = y self.tiles[y, x]['color'] = random.choice(Board.valid_colors) def render(self, scale_fac): @@ -51,6 +52,12 @@ class Board: return board_surf + def get_xdims(self): + return self.tiles.shape[1] + + def get_ydims(self): + return self.tiles.shape[0] + # def __repr__(self): # s = '' # for y in range(self.tiles.shape[0]): @@ -103,6 +110,9 @@ class Robot: robot_surf = pygame.transform.rotate(robot_surf, self.get_angle()) return robot_surf + def update_pos(self, dimx, dimy): + move_grid(self.x, self.y, self.orientation, dimx, dimy) + def __repr__(self): return f"({self.y}, {self.x}) - {self.orientation}" @@ -201,8 +211,10 @@ class Program: ynew, xnew = self.robot.get_forward_coordinates() self.robot.x = xnew self.robot.y = ynew + self.robot.update_pos(self.board.get_xdims(), self.board.get_ydims()) elif cmd.action in {'left', 'right'}: self.robot.orientation = Robot.resulting_orientation[self.robot.orientation][cmd.action] + self.robot.update_pos(self.board.get_xdims(), self.board.get_ydims()) elif cmd.action == 'P0': self.prg_counter = 0 else: @@ -266,12 +278,14 @@ class Game: def __init__(self, dimx, dimy, robotx, roboty): self.robot = Robot(x=robotx, y=roboty, orientation='v') self.board = Board(dimx, dimy) - self.board.tiles[3,3]['star'] = True + coin1x = np.random.randint(0, dimx) + coin1y = np.random.randint(0, dimy) + self.board.tiles[coin1y,coin1x]['star'] = True self.board.tiles[3,2]['star'] = True # TODO fix number of commands at 5 self.cmds = [Command('forward'), Command('left', color=RED), Command('left', color=BLUE), Command('P0'), Command('-')] - self.state = 'input' + self.state = 'reset' self.prg = Program(self.robot, self.board, self.cmds) @@ -427,6 +441,7 @@ class Game: self.robot.x = self.initial_pos[0] self.robot.y = self.initial_pos[1] self.robot.orientation = self.initial_pos[2] + self.robot.update_pos(self.board.get_xdims(), self.board.get_ydims()) self.board.tiles = self.inital_board_tiles.copy() return 'input'