implemented communication interface with real robots
This commit is contained in:
parent
3082eebc8d
commit
019c4590aa
15
gauss-turing/webserver/event_server_comm.py
Normal file
15
gauss-turing/webserver/event_server_comm.py
Normal file
|
@ -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)
|
|
@ -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'
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user