def setUp(self): # ToDo : Use mock instead of actual objects self.game_state = GameState() self.game = Game() self.game.set_referee(Referee()) game_world = ReferenceTransferObject(self.game) game_world.set_team_color_svc(TeamColorService(TeamColor.YELLOW)) self.game_state.set_reference(game_world) self.a_player = OurPlayer(TeamColor.YELLOW, A_PLAYER_ID)
def setUp(self): config_service = ConfigService().load_file("config/sim_standard.cfg") self.game_state = GameState() self.game = Game() self.game.set_referee(Referee()) self.game.ball = Ball() game_world = ReferenceTransferObject(self.game) game_world.set_team_color_svc(TeamColorService(TeamColor.YELLOW_TEAM)) self.game_state.set_reference(game_world) self.game_state.game.ball.set_position(Position(100, 0), 0)
def setUp(self): self.game = Game() self.referee = Referee self.game.set_referee(self.referee) self.tcsvc = TeamColorService(TeamColor.BLUE) self.game_world_OK = ReferenceTransferObject(self.game) self.game_world_OK.set_team_color_svc(self.tcsvc) self.GameStateManager1 = GameState() self.GameStateManager2 = GameState() self.GameStateManager1.set_reference(self.game_world_OK)
def setUp(self): # ToDo : Use mock instead of actual objects self.game_state = GameState() self.game = Game() self.game.set_referee(Referee()) self.game.ball = Ball() game_world = ReferenceTransferObject(self.game) game_world.set_team_color_svc(TeamColorService(TeamColor.YELLOW_TEAM)) self.game.set_our_team_color(TeamColor.YELLOW_TEAM) self.game_state.set_reference(game_world) self.player_id = 1 # random integer
def _create_game_world(self): """ Créé le GameWorld pour contenir les éléments d'une partie normale: l'arbitre, la Game (Field, teams, players). C'est un data transfer object pour les références du RULEngine vers l'IA """ self.referee = Referee() self.game = Game() self.game.set_referee(self.referee) self.reference_transfer_object = ReferenceTransferObject(self.game) self.reference_transfer_object.set_debug(self.incoming_debug)
def setUp(self): config_service = ConfigService().load_file("config/sim_standard.cfg") self.game = Game() self.referee = Referee self.game.set_referee(self.referee) self.tcsvc = TeamColorService(TeamColor.BLUE_TEAM) self.game.set_our_team_color(self.tcsvc.OUR_TEAM_COLOR) self.game_world_OK = ReferenceTransferObject(self.game) self.game_world_OK.set_team_color_svc(self.tcsvc) self.GameStateManager1 = GameState() self.GameStateManager2 = GameState() self.GameStateManager1.set_reference(self.game_world_OK)
class TestGameStateManager(unittest.TestCase): """ Teste les différentes fonctionnalités du GameStateManager """ def setUp(self): self.game = Game() self.referee = Referee self.game.set_referee(self.referee) self.tcsvc = TeamColorService(TeamColor.BLUE) self.game_world_OK = ReferenceTransferObject(self.game) self.game_world_OK.set_team_color_svc(self.tcsvc) self.GameStateManager1 = GameState() self.GameStateManager2 = GameState() self.GameStateManager1.set_reference(self.game_world_OK) def test_singleton(self): """ Teste si le Manager est un singleton, i.e. s'il ne peut y avoir qu'une seule instance du manager """ self.assertTrue(self.GameStateManager1 is self.GameStateManager2) self.assertIs(self.GameStateManager1, self.GameStateManager2) def test_set_reference(self): self.GameStateManager1.set_reference(self.game_world_OK) self.assertIs(self.GameStateManager1.game.referee, self.game_world_OK.game.referee) self.assertIs(self.GameStateManager1.field, self.game_world_OK.game.field) self.assertIs(self.GameStateManager1.game.our_team_color, self.game.our_team_color) game_state_manager = GameState() self.assertRaises(AssertionError, game_state_manager.set_reference, None) game = Game() game_world_nok = ReferenceTransferObject(game) self.assertRaises(AssertionError, game_state_manager.set_reference, game_world_nok) game_world_nok.game.set_referee(self.referee) self.assertRaises(AssertionError, game_state_manager.set_reference, game_world_nok) game = Game() game_world_nok = ReferenceTransferObject(game) game_world_nok.set_team_color_svc(self.tcsvc) self.assertRaises(AssertionError, game_state_manager.set_reference, game_world_nok)
def setUp(self): config_service = ConfigService().load_file("config/sim_standard.cfg") self.game_state = GameState() self.game = Game() self.game.set_referee(Referee()) self.game.ball = Ball() game_world = ReferenceTransferObject(self.game) game_world.set_team_color_svc(TeamColorService(TeamColor.YELLOW_TEAM)) self.game_state.set_reference(game_world) self.game_state.game.friends.players[0].update( Pose(Position(-4450, 0), 0)) self.tactic1 = GoalKeeper(self.game_state, 0) self.tactic2 = Stop(self.game_state, 1) self.node1 = Node(self.tactic1) self.node2 = Node(self.tactic2) self.vertex1 = Vertex(0, foo) self.vertex2 = Vertex(1, foo2)
def setUp(self): self.game_state = GameState() self.game = Game() self.game.set_referee(Referee()) self.game.ball = Ball() game_world = ReferenceTransferObject(self.game) game_world.set_team_color_svc(TeamColorService(TeamColor.YELLOW)) self.game_state.set_reference(game_world) self.game_state.game.friends.players[0].pose = Pose( Position(-4450, 0), 0) self.tactic1 = GoalKeeper( self.game_state, self.game_state.game.friends.players[A_GOAL_PLAYER_ID]) self.tactic2 = Stop(self.game_state, self.game_state.game.friends.players[A_PLAYER_ID]) self.node1 = Node(self.tactic1) self.node2 = Node(self.tactic2) self.vertex1 = Vertex(0, foo) self.vertex2 = Vertex(1, foo2) self.a_player = OurPlayer(TeamColor.BLUE, 0)
def setUp(self): self.game_state = GameState() self.game = Game() self.game.set_referee(Referee()) self.game.ball = Ball() game_world = ReferenceTransferObject(self.game) game_world.set_team_color_svc(TeamColorService(TeamColor.YELLOW)) self.game_state.set_reference(game_world) self.game_state = GameState() self.empty_graph = Graph() self.graph1 = Graph() self.a_player = OurPlayer(TeamColor.YELLOW, A_PLAYER_ID) self.tactic1 = Stop(self.game_state, self.a_player) self.tactic2 = GoToPositionNoPathfinder(self.game_state, self.a_player, Pose(Position(500, 0), 0)) self.node1 = Node(self.tactic1) self.node2 = Node(self.tactic2) self.vertex1 = Vertex(1, foo) self.graph1.add_node(self.node1) self.graph1.add_node(self.node2) self.graph1.add_vertex(0, 1, foo)
def setUp(self): config_service = ConfigService().load_file("config/sim_standard.cfg") self.game_state = GameState() self.game = Game() self.game.set_referee(Referee()) self.game.ball = Ball() game_world = ReferenceTransferObject(self.game) game_world.set_team_color_svc(TeamColorService(TeamColor.YELLOW_TEAM)) self.game_state.set_reference(game_world) self.game_state = GameState() self.empty_graph = Graph() self.graph1 = Graph() self.tactic1 = Stop(self.game_state, 1) self.tactic2 = GoToPositionNoPathfinder(self.game_state, 0, Pose(Position(500, 0), 0)) self.node1 = Node(self.tactic1) self.node2 = Node(self.tactic2) self.vertex1 = Vertex(1, foo) self.graph1.add_node(self.node1) self.graph1.add_node(self.node2) self.graph1.add_vertex(0, 1, foo)
def test_set_reference(self): self.GameStateManager1.set_reference(self.game_world_OK) self.assertIs(self.GameStateManager1.game.referee, self.game_world_OK.game.referee) self.assertIs(self.GameStateManager1.field, self.game_world_OK.game.field) self.assertIs(self.GameStateManager1.game.our_team_color, self.game.our_team_color) game_state_manager = GameState() self.assertRaises(AssertionError, game_state_manager.set_reference, None) game = Game() game_world_nok = ReferenceTransferObject(game) self.assertRaises(AssertionError, game_state_manager.set_reference, game_world_nok) game_world_nok.game.set_referee(self.referee) self.assertRaises(AssertionError, game_state_manager.set_reference, game_world_nok) game = Game() game_world_nok = ReferenceTransferObject(game) game_world_nok.set_team_color_svc(self.tcsvc) self.assertRaises(AssertionError, game_state_manager.set_reference, game_world_nok)
class Framework(object): """ La classe contient la logique nécessaire pour communiquer avec les différentes parties(simulation, vision, uidebug et/ou autres), maintenir l'état du monde (jeu, referree, debug, etc...) et appeller l'ia. """ def __init__(self): """ Constructeur de la classe, établis les propriétés de bases et construit les objets qui sont toujours necéssaire à son fonctionnement correct. """ # config self.cfg = ConfigService() # time self.last_frame_number = 0 self.time_stamp = None # time.time() self.last_camera_time = time.time() self.time_of_last_loop = time.time() self.ai_timestamp = float(self.cfg.config_dict["GAME"]["ai_timestamp"]) # thread self.ia_running_thread = None self.thread_terminate = threading.Event() # Communication self.robot_command_sender = None self.vision = None self.referee_command_receiver = None self.uidebug_command_sender = None self.uidebug_command_receiver = None self.uidebug_vision_sender = None self.uidebug_robot_monitor = None # because this thing below is a callable! can be used without being set self.vision_redirection_routine = lambda *args: None self.vision_routine = self._sim_vision # self._normal_vision # self._test_vision self._redirected_vision self._choose_vision_routines() # Debug self.incoming_debug = [] self.debug = DebugInterface() self.outgoing_debug = self.debug.debug_state self._init_communication() # Game elements self.reference_transfer_object = None self.game = None self.ai_coach = None self.referee = None self.team_color_service = None self._create_game_world() # VISION self.image_transformer = ImageTransformerFactory.get_image_transformer( ) # ia couplage self.ia_coach_mainloop = None self.ia_coach_initializer = None # for testing purposes self.frame_number = 0 # self.debug.add_log(1, "Framework started in {} s".format(time.time() - self.time_stamp)) def _choose_vision_routines(self): if self.cfg.config_dict["IMAGE"]["kalman"] == "true": self.vision_routine = self._kalman_vision def _init_communication(self): # first make sure we are not already running if self.ia_running_thread is None: # where do we send the robots command (serial for bluetooth and rf) self.robot_command_sender = RobotCommandSenderFactory.get_sender() # Referee self.referee_command_receiver = RefereeReceiver() # Vision self.vision = VisionReceiver() # do we use the UIDebug? if self.cfg.config_dict["DEBUG"]["using_debug"] == "true": self.uidebug_command_sender = UIDebugCommandSender() self.uidebug_command_receiver = UIDebugCommandReceiver() # Monitor robot if we are communicating with an actual robot self.uidebug_robot_monitor = UIDebugRobotMonitor( self.robot_command_sender, self.debug) else: self.stop_game() def game_thread_main_loop(self): """ Fonction exécuté et agissant comme boucle principale. """ self._wait_for_first_frame() self._wait_for_first_geometry_packet() while not self.thread_terminate.is_set(): self.time_stamp = time.time() self.vision_routine() time.sleep(0.01) def start_game(self, p_ia_coach_mainloop, p_ia_coach_initializer): """ Démarrage du moteur de l'IA initial, ajustement de l'équipe de l'ia et démarrage du/des thread/s""" # IA COUPLING self.ia_coach_mainloop = p_ia_coach_mainloop self.ia_coach_initializer = p_ia_coach_initializer # GAME_WORLD TEAM ADJUSTMENT self.team_color_service = TeamColorService() self.reference_transfer_object.team_color_svc = self.team_color_service print("Framework partie avec équipe", self.cfg.config_dict["GAME"]["our_color"]) self.ia_coach_initializer(self.reference_transfer_object) signal.signal(signal.SIGINT, self._sigint_handler) self.ia_running_thread = threading.Thread( target=self.game_thread_main_loop) self.ia_running_thread.start() self.ia_running_thread.join() def _create_game_world(self): """ Créé le GameWorld pour contenir les éléments d'une partie normale: l'arbitre, la Game (Field, teams, players). C'est un data transfer object pour les références du RULEngine vers l'IA """ self.referee = Referee() self.game = Game() self.game.set_referee(self.referee) self.reference_transfer_object = ReferenceTransferObject(self.game) self.reference_transfer_object.set_debug(self.incoming_debug) def _update_players_and_ball(self, vision_frame): """ Met à jour le GameState selon la frame de vision obtenue. """ time_delta = self._compute_vision_time_delta(vision_frame) # print(time_delta) self.game.update(vision_frame, time_delta) def _is_frame_number_different(self, vision_frame): # print(vision_frame.detection.frame_number) if vision_frame is not None: return vision_frame.detection.frame_number != self.last_frame_number else: return False def _compute_vision_time_delta(self, vision_frame): self.last_frame_number = vision_frame.detection.frame_number this_time = vision_frame.detection.t_capture # time.time() # vision_frame.detection.t_capture time_delta = this_time - self.last_camera_time self.last_camera_time = this_time # FIXME: hack return time_delta def _update_debug_info(self): self.incoming_debug += self.uidebug_command_receiver.receive_command() def _sim_vision(self): vision_frame = self._acquire_last_vision_frame() if vision_frame.detection.frame_number != self.last_frame_number: time_delta = self._compute_vision_time_delta(vision_frame) self.game.update(vision_frame, time_delta) self._update_debug_info() robot_commands = self.ia_coach_mainloop() # Communication self._send_robot_commands(robot_commands) self._send_debug_commands() def _kalman_vision(self): vision_frames = self.vision.pop_frames() new_image_packet = self.image_transformer.update(vision_frames) referee_frames = self.referee_command_receiver.pop_frames() self.game.referee.update(referee_frames) new_time_delta = time.time() - self.time_of_last_loop if new_time_delta > self.ai_timestamp: time_delta = new_time_delta self.time_of_last_loop = time.time() self.game.update(new_image_packet, time_delta) self.game.field.update_field_dimensions(vision_frames) self._update_debug_info() robot_commands = self.ia_coach_mainloop() # Communication self._send_robot_commands(robot_commands) self._send_debug_commands() #self._send_new_vision_packet() if time_delta > self.ai_timestamp * 1.3: warnings.warn( "Update loop took {:5.3f}s instead of {}s!".format( time_delta, self.ai_timestamp), RuntimeWarning, stacklevel=2) """ def _test_vision(self): vision_frames = self.vision.pop_frames() new_image_packet = self.image_transformer.update(vision_frames) if time.time() - self.last_loop > 0.05: time_delta = time.time() - self.last_time self.game.update_kalman(new_image_packet, time_delta) self._update_debug_info() robot_commands = self.ia_coach_mainloop() # Communication self._send_robot_commands(robot_commands) self.game.set_command(robot_commands) self._send_debug_commands() self.last_loop = time.time() time.sleep(0) """ def _acquire_last_vision_frame(self): return self.vision.get_latest_frame() def _acquire_all_vision_frames(self): return self.vision.pop_frames() def stop_game(self): """ Nettoie les ressources acquises pour pouvoir terminer l'exécution. """ self.thread_terminate.set() self.ia_running_thread.join() self.thread_terminate.clear() self.robot_command_sender.stop() if self.uidebug_robot_monitor: self.uidebug_robot_monitor.stop() try: team = self.game.friends # FIXME: hack for grsim for player in team.available_players.values(): if player.ai_command is not None: command = Stop(player) self.robot_command_sender.send_command(command) except Exception as e: print("Could not stop players") print("Au nettoyage il a été impossible d'arrêter les joueurs.") raise e # raise StopPlayerError("Au nettoyage il a été impossible d'arrêter les joueurs.") def _wait_for_first_frame(self): while not self.vision.get_latest_frame( ) and not self.thread_terminate.is_set(): time.sleep(0.1) print("En attente d'une image de la vision.") def _wait_for_first_geometry_packet(self): while not self.game.field.update_field_dimensions(self.vision.pop_frames()) and\ not self.thread_terminate.is_set(): time.sleep(0.01) print("En attente du premier packet de géométrie du terrain.") def _send_robot_commands(self, commands): """ Envoi les commades des robots au serveur. """ for command in commands: self.robot_command_sender.send_command(command) def _send_debug_commands(self): """ Envoie les commandes de debug au serveur. """ packet_represented_commands = [ c.get_packet_repr() for c in self.outgoing_debug ] if self.uidebug_command_sender is not None: self.uidebug_command_sender.send_command( packet_represented_commands) self.incoming_debug.clear() self.outgoing_debug.clear() # for testing purposes def _send_new_vision_packet(self): pb_sslwrapper = ssl_wrapper.SSL_WrapperPacket() pb_sslwrapper.detection.camera_id = 0 pb_sslwrapper.detection.t_sent = 0 pck_ball = pb_sslwrapper.detection.balls.add() pck_ball.x = self.game.field.ball.position.x pck_ball.y = self.game.field.ball.position.y pck_ball.z = self.game.field.ball.position.z # required for the packet no use for us at this stage pck_ball.confidence = 0.999 pck_ball.pixel_x = self.game.field.ball.position.x pck_ball.pixel_y = self.game.field.ball.position.y for p in self.game.blue_team.available_players.values(): packet_robot = pb_sslwrapper.detection.robots_blue.add() packet_robot.confidence = 0.999 packet_robot.robot_id = p.id packet_robot.x = p.pose.position.x packet_robot.y = p.pose.position.y packet_robot.orientation = p.pose.orientation packet_robot.pixel_x = 0. packet_robot.pixel_y = 0. for p in self.game.yellow_team.available_players.values(): packet_robot = pb_sslwrapper.detection.robots_yellow.add() packet_robot.confidence = 0.999 packet_robot.robot_id = p.id packet_robot.x = p.pose.position.x packet_robot.y = p.pose.position.y packet_robot.orientation = p.pose.orientation packet_robot.pixel_x = 0. packet_robot.pixel_y = 0. self.frame_number += 1 pb_sslwrapper.detection.t_capture = 0 pb_sslwrapper.detection.frame_number = self.frame_number try: self.vision_redirection_routine(pb_sslwrapper.SerializeToString()) except: print("Fail to send in vision redirection") def _sigint_handler(self, *args): self.stop_game()
class TestGameStateManager(unittest.TestCase): """ Teste les différentes fonctionnalités du GameStateManager """ def setUp(self): config_service = ConfigService().load_file("config/sim_standard.cfg") self.game = Game() self.referee = Referee self.game.set_referee(self.referee) self.tcsvc = TeamColorService(TeamColor.BLUE_TEAM) self.game.set_our_team_color(self.tcsvc.OUR_TEAM_COLOR) self.game_world_OK = ReferenceTransferObject(self.game) self.game_world_OK.set_team_color_svc(self.tcsvc) self.GameStateManager1 = GameState() self.GameStateManager2 = GameState() self.GameStateManager1.set_reference(self.game_world_OK) def test_singleton(self): """ Teste si le Manager est un singleton, i.e. s'il ne peut y avoir qu'une seule instance du manager """ self.assertTrue(self.GameStateManager1 is self.GameStateManager2) self.assertIs(self.GameStateManager1, self.GameStateManager2) def test_set_reference(self): self.GameStateManager1.set_reference(self.game_world_OK) self.assertIs(self.GameStateManager1.game.referee, self.game_world_OK.game.referee) self.assertIs(self.GameStateManager1.field, self.game_world_OK.game.field) self.assertIs(self.GameStateManager1.game.our_team_color, self.game.our_team_color) game_state_manager = GameState() self.assertRaises(AssertionError, game_state_manager.set_reference, None) game = Game() game_world_nok = ReferenceTransferObject(game) self.assertRaises(AssertionError, game_state_manager.set_reference, game_world_nok) game_world_nok.game.set_referee(self.referee) self.assertRaises(AssertionError, game_state_manager.set_reference, game_world_nok) game = Game() game_world_nok = ReferenceTransferObject(game) game_world_nok.set_team_color_svc(self.tcsvc) self.assertRaises(AssertionError, game_state_manager.set_reference, game_world_nok) def test_get_player_pose(self): self.assertIs(self.GameStateManager1.get_player_pose(0, True), self.game.friends.players[0].pose) self.assertIs(self.GameStateManager2.get_player_pose(0, False), self.game.enemies.players[0].pose) self.assertIsNot(self.GameStateManager1.get_player_pose(0, True), self.game.friends.players[1].pose) self.assertIsNot(self.GameStateManager2.get_player_pose(0, False), self.game.enemies.players[1].pose) self.assertIsNot(self.GameStateManager1.get_player_pose(0, True), self.game.enemies.players[0].pose) self.assertIsNot(self.GameStateManager2.get_player_pose(0, False), self.game.friends.players[0].pose) def test_get_player_position(self): self.assertIs(self.GameStateManager1.get_player_position(0, True), self.game.friends.players[0].pose.position) self.assertIs(self.GameStateManager2.get_player_position(0, False), self.game.enemies.players[0].pose.position)