class ExamplePlayer(): def __init__(self, colour): self.pos = Position(initial, 0, True, colour == colour) def action(self): print("\n" + "The following actions are available:" + "\n") available_actions = [] for i, move in enumerate(self.pos.gen_moves()): formatted_move = format_move(move) if i > 0 and i % 3 == 0: print() print("{:3d}: {:32s}".format(i + 1, format(formatted_move)), end='') available_actions.append(formatted_move) print("\n") move_index = int( input("Please choose an action to take (enter the index): ")) return available_actions[move_index - 1] # update method to keep internal representation of the state-of-play current def update(self, colour, action): # interpret and decode formatted 'action' move = parse_formatted_move(action) self.pos = self.pos.move(move) # update!
def test_position_hash(self): self.assertEqual( hash(Position("MYSec", "Münster", "Other Person", "2019-12-12")), hash(Position("MYSec", "Münster", "Other Person", "2019-12-12"))) self.assertNotEqual( hash(Position("MYSec", "Münster", "Other Person", "2019-12-12")), hash(Position("MYSec", "Thüringen", "Other Person", "2019-12-12")))
def test_position_factory_methods(self): self.assertEqual(len(Position.create_all(title="MYSec")), 2) self.assertEqual( len( Position.create_all_fitting_data( [["start_date", ">", "2020-12-12"]], title="MYSec")), 1) self.assertEqual( len(Position.create_all_held_positions("Test Person")), 1)
def test_update_db(self): pos = Position.create_all_held_positions("Test Person")[0] original_end_date = pos.end_date pos.end_date = "2021-01-18" pos.update_in_db() pos = Position.create_all_held_positions("Test Person")[0] new_end_date = pos.end_date self.assertNotEqual(original_end_date, new_end_date)
def get_robot_position_from_vision(self): if self.__connected: if self.last_robot_info_from_vision: return Position(self.last_robot_info_from_vision['x'], self.last_robot_info_from_vision['y']) else: return Position(0, 0) else: raise NoConnectionException('Cannot connect to base station')
def relative_position(self, position): robot_current_position = self.get_robot_position() matrix = rotate_vector( self.get_robot_angle(), np.array(position.to_tuple()) - np.array(robot_current_position.to_tuple())) return Position(matrix[0], matrix[1])
def __init__(self, width, height, worldmap_service, table_calibration_service): self._width = width self._height = height self._robot_position = Position(width / 2, height / 2) self._robot_angle = 0 self.worldmap_service = worldmap_service self.table_calibration_service = table_calibration_service
def robot_move_to(): destination = Position(request.json['x'], request.json['y']) try: robot.move_to(destination, None) except Exception as any_error: print(any_error) raise any_error return "OK"
def test_position_accessibility(self): pos = Position(title="MYSec", region="Münster", held_by="Other Person", start_date="2019-12-12") self.assertEqual(pos.title, "MYSec") self.assertEqual(pos.region, "Münster") self.assertEqual(pos.held_by, "Other Person") self.assertEqual(pos.start_date, "2019-12-12") self.assertEqual(pos.end_date, "")
def test_post_position(self): printed_statements = [] self.monkeypatch.setattr("builtins.print", lambda x: printed_statements.append(x)) UserInteraction.post_position_details( Position("MYSec", "Sachsen", "M Q", "2019-03-30")) self.assertEqual(printed_statements, [ "Title: MYSec", "Held by: M Q", "Region: Sachsen", "Start date: 2019-03-30", "End date: " ])
def __calculate_islands_risk(self, treasure_position): islands = self.__worldmap.get_islands() islands_positions = list( map(lambda island: Position(island["x"], island["y"]), islands)) islands_remoteness = list( map( lambda island_position: island_position.distance( treasure_position), islands_positions)) closest_island_distance = min(islands_remoteness) path_ponderation = PONDERATION_FACTOR / closest_island_distance return path_ponderation
def __init__(self, actual_map, initial_position, initial_angle, distance_threshold=50): time_init = time() self.__robot_position = (time_init, initial_position) self.__robot_angle = (time_init, initial_angle) self.__velocity = Position(0, 0) self.__map = actual_map self.__distance_threshold = distance_threshold
def test_contains_point_should_return_false_if_point_is_not_inside_cell_boundaries( self): # Given height = 10 width = 10 cell = Cell(width, height, 5, 5) point = Position(15, 15) # When is_contained = cell.contains_point(point) # Then assert_false(is_contained)
def __init__(self, wheels, world_map, pathfinder, manchester_antenna, movement, battery, magnet, camera_rotation): self.__wheels = wheels self.__world_map = world_map self.__pathfinder = pathfinder self.__movement = movement self.__manchester_antenna = manchester_antenna self.__battery = battery self.__magnet = magnet self.__manchester_code = '' self.__island_clue = '' self.__camera_rotation_control = camera_rotation self.__position = Position(0, 0)
def find_easiest_treasure_from(self, position, target_island_position): treasures = self.__worldmap.get_treasures() treasures_positions = list( map(lambda treasure: Position(treasure['x'], treasure['y']), treasures)) paths = list( map( lambda treasure_position: { 'length': self.__find_path_length(position, treasure_position, target_island_position), 'treasure': treasure_position }, treasures_positions)) return min(paths, key=lambda x: x['length'])['treasure']
def test_position_mutability(self): pos = Position(title="MYSec", region="Münster", held_by="Other Person", start_date="2019-12-12") pos.end_date = "2019-12-13" self.assertEqual(pos.end_date, "2019-12-13") with self.assertRaises(AttributeError): pos.title = "New" with self.assertRaises(AttributeError): pos.region = "New" with self.assertRaises(AttributeError): pos.held_by = "New" with self.assertRaises(AttributeError): pos.start_date = "0001-01-01"
def start(self): self.running = True print('Moving to Treasure') self.treasure_position = self._context.robot.get_target_treasure_position( ) self.facing_angle = self.__find_facing_angle_when_upfront( self.treasure_position, self._context.worldmap.table_calibration_service.get_table_corners( )) wall_distance = 180 position_ajustment = Position( -cos(2 * pi * self.facing_angle / 360.0), sin(2 * pi * self.facing_angle / 360.0)) * wall_distance print(self.facing_angle, position_ajustment) try: self._context.robot.move_to( self.treasure_position + position_ajustment, self.path_done) except Exception as error: print(error)
def find_path(self, from_point, to_point): from_cell = self.find_closest_node_to(from_point) to_cell = self.find_closest_node_to_neighbors( networkx.node_connected_component(self.__graph, from_cell), to_point) try: cell_path = networkx.astar_path(self.__graph, from_cell, to_cell) except: raise Exception('No Path found!') path = list() for cell in cell_path: path.append(Position(cell.x, cell.y)) if len(path) == 1: path.pop() path.append(to_point) else: path.pop(0) path.pop() path.insert(0, from_point) path.append(to_point) return path
def move_robot(self, delta_x, delta_y): delta = rotate_vector(-self._robot_angle, Position(delta_x, delta_y)) self.set_robot_position(self._robot_position.x + delta[0], self._robot_position.y + delta[1])
from nose import with_setup from nose.tools import * from utils.position import Position from unittest.mock import * from robot.simulation.simulation_map import SimulationMap WIDTH = 300.0 HEIGHT = 100.0 A_POSITION = Position(20.0, 50.0) A_POSITION_OUTSIDE_BOUNDARIES = Position(500.0, 200.0) AN_ANGLE = 142 A_MOVE_OUTSIDE = Position(A_POSITION.x + WIDTH, 0.0) PRECISION = 1e-6 world_map = SimulationMap(WIDTH, HEIGHT, Mock(), Mock()) def setup(): world_map.__init__(WIDTH, HEIGHT, Mock(), Mock()) @with_setup(setup) def test_when_init_map_then_width_is_set(): assert_equal(WIDTH, world_map._width) @with_setup(setup) def test_when_init_map_then_height_is_set(): assert_equal(HEIGHT, world_map._height)
def find_island_with_clue(self, clue): islands = self.worldmap_service.get_islands() target_islands = list( filter(lambda island: self.filter_by_clue(island, clue), islands)) target_island = target_islands.pop() return Position(target_island['x'], target_island['y'])
def get_treasure_closest_to(self, position): #TODO return Position(500, 600)
def get_recharge_station_position(self): position = self.worldmap_service.get_charging_station_position() charging_station_position = Position(position["x"], position["y"]) return charging_station_position
def set_robot_position(self, x, y): self._robot_position = Position(x, y)
def test_when_set_robot_position_then_position_is_set(): a_position_x = 3 a_position_y = 4 a_position = Position(a_position_x, a_position_y) world_map.set_robot_position(a_position_x, a_position_y) assert_equal(a_position, world_map.get_robot_position())
def test_get_details_fitting_data(self): self.assertEqual( Position.get_details_fitting_data([["end_date", "=", ""]], title="MYSec"), [(1, "Test Person", "Münster", "2020-03-02", "")])
manchester_antenna = ManchesterAntennaSimulation() battery = BatterySimulation() magnet = MagnetSimulation() robot_service = RobotServiceSimulation() camera_rotation = Mock() elif wheels_config == "usb-arduino": assembler = RobotInfoAssembler() vision_daemon = VisionDaemon(base_station_address, assembler) camera_position_x = config.getint('robot', 'camera-position-x') camera_position_y = config.getint('robot', 'camera-position-y') camera_height = config.getfloat('robot', 'camera-height') robot_height = config.getfloat('robot', 'robot-height') vision_perspective_corrected = VisionPerspectiveCorrection( vision_daemon, Position(camera_position_x, camera_position_y), camera_height, robot_height) world_map = Map(vision_perspective_corrected, world_map_service, table_calibration_service) arduino_pid = config.getint('robot', 'arduino-pid') arduino_vid = config.getint('robot', 'arduino-vid') polulu_pid = config.getint('robot', 'polulu-pid') polulu_vid = config.getint('robot', 'polulu-vid') arduino_baudrate = config.getint('robot', 'arduino-baudrate') ports = lp.comports() arduino_port = list( filter( lambda port: port.pid == arduino_pid and port.vid == arduino_vid, ports)) polulu_port = list(
def __init__(self, colour): self.pos = Position(initial, 0, True, colour == colour)
def _relative_position(self, position): matrix = rotate_vector(self.__actual_robot_angle, position - self.__actual_position) return Position(matrix[0], matrix[1])
def test_Vector(): origin = Position(0, 1) vector = Vector(-1, 2) points = [(-1, 3), (-2, 5)] for i, point in enumerate(vector.iter(origin, 2)): assert point == points[i]