def __init__(self, framework): super().__init__(name=self.__class__.__name__) self.framework = framework self.logger = logging.getLogger(self.__class__.__name__) # Managers for shared memory between process manager = Manager() self.vision_state = manager.list([ manager.dict() for _ in range(config['ENGINE']['number_of_camera']) ]) self.game_state = self.framework.game_state self.field = self.framework.field # Queues for process communication self.ui_send_queue = self.framework.ui_send_queue self.ui_recv_queue = self.framework.ui_recv_queue self.ai_queue = self.framework.ai_queue self.referee_queue = self.framework.referee_queue # External communication self.vision_receiver = VisionReceiver( config['COMMUNICATION']['vision_info'], self.vision_state, self.field) self.ui_sender = UIDebugCommandSender( config['COMMUNICATION']['ui_sender_info'], self.ui_send_queue) self.ui_recver = UIDebugCommandReceiver( config['COMMUNICATION']['ui_recver_info'], self.ui_recv_queue) self.referee_recver = RefereeReceiver( config['COMMUNICATION']['referee_info'], self.referee_queue) self.robot_cmd_sender = RobotCommandSender() # main engine module self.tracker = Tracker(self.vision_state, self.ui_send_queue) self.controller = Controller(self.ui_send_queue) # fps and limitation self.fps = config['ENGINE']['fps'] self.is_fps_locked = config['ENGINE']['is_fps_locked'] self.frame_count = 0 self.last_frame_count = 0 self.dt = 0.0 self.last_time = 0.0 def callback(excess_time): if excess_time > Engine.MAX_EXCESS_TIME: self.logger.debug('Overloaded (%.1f ms behind schedule)', 1000 * excess_time) self.fps_sleep = create_fps_timer(self.fps, on_miss_callback=callback) # profiling self.profiler = None
def test_callback_delay_fps_timer(): fps = 10 callback_time = None def mock_callback(time_var): nonlocal callback_time callback_time = time_var sleep = timing.create_fps_timer(fps, on_miss_callback=mock_callback) sleep() time.sleep(0.2) sleep() assert callback_time >= 0
def test_callback_called_fps_timer(): fps = 10 callback_called = False def mock_callback(time_var): # pylint: disable=unused-argument nonlocal callback_called callback_called = True sleep = timing.create_fps_timer(fps, on_miss_callback=mock_callback) sleep() time.sleep(0.2) sleep() assert callback_called is True
def test_no_load_fps_timer(): # example preliminary test start = time.time() time_total = 2 fps = 10 num_iterations = time_total*fps sleep = timing.create_fps_timer(fps) sleep() # to initialize it for _ in range(num_iterations-1): # -1 because of the first sleep sleep() end_time = time.time()-start assert end_time == pytest.approx(time_total, 0.1)
def test_near_full_load_fps_timer(): # example preliminary test start = time.time() time_total = 2 fps = 10 num_iterations = time_total*fps sleep = timing.create_fps_timer(fps) sleep() # to initialize it for _ in range(num_iterations-1): # -1 because of the first sleep time.sleep(1/(fps+0.05)) # simulate load very close to full load sleep() end_time = time.time()-start assert end_time == pytest.approx(time_total, 0.1)
def __init__(self, framework): super().__init__(name=self.__class__.__name__) self.framework = framework self.logger = logging.getLogger(self.__class__.__name__) # Managers for shared memory between process manager = Manager() self.vision_state = manager.list([manager.dict() for _ in range(config['ENGINE']['number_of_camera'])]) self.game_state = self.framework.game_state self.field = self.framework.field # Queues for process communication self.ui_send_queue = self.framework.ui_send_queue self.ui_recv_queue = self.framework.ui_recv_queue self.ai_queue = self.framework.ai_queue self.referee_queue = self.framework.referee_queue # External communication self.vision_receiver = VisionReceiver(config['COMMUNICATION']['vision_info'], self.vision_state, self.field) self.ui_sender = UIDebugCommandSender(config['COMMUNICATION']['ui_sender_info'], self.ui_send_queue) self.ui_recver = UIDebugCommandReceiver(config['COMMUNICATION']['ui_recver_info'], self.ui_recv_queue) self.referee_recver = RefereeReceiver(config['COMMUNICATION']['referee_info'], self.referee_queue) self.robot_cmd_sender = RobotCommandSender() # main engine module self.tracker = Tracker(self.vision_state, self.ui_send_queue) self.controller = Controller(self.ui_send_queue) # fps and limitation self.fps = config['ENGINE']['fps'] self.is_fps_locked = config['ENGINE']['is_fps_locked'] self.frame_count = 0 self.last_frame_count = 0 self.dt = 0.0 self.last_time = 0.0 def callback(excess_time): if excess_time > Engine.MAX_EXCESS_TIME: self.logger.debug('Overloaded (%.1f ms behind schedule)', 1000*excess_time) self.fps_sleep = create_fps_timer(self.fps, on_miss_callback=callback) # profiling self.profiler = None
def __init__(self, framework): super().__init__(name=__name__) self.framework = framework self.logger = logging.getLogger(self.__class__.__name__) # Managers for shared memory between process self.engine_game_state = self.framework.game_state self.field = self.framework.field # Queues for process communication self.ai_queue = self.framework.ai_queue self.referee_queue = self.framework.referee_queue self.ui_send_queue = self.framework.ui_send_queue self.ui_recv_queue = self.framework.ui_recv_queue # States self.game_state = GameState() self.play_state = PlayState() # Executors self.play_executor = PlayExecutor(self.play_state, self.ui_send_queue, self.referee_queue) self.debug_executor = DebugExecutor(self.play_state, self.play_executor, self.ui_send_queue, self.ui_recv_queue) # fps and limitation self.fps = config['GAME']['fps'] self.frame_count = 0 self.last_frame_count = 0 self.dt = 0.0 self.last_time = 0.0 def callback(excess_time): if excess_time > Coach.MAX_EXCESS_TIME: self.logger.debug('Overloaded (%.1f ms behind schedule)', 1000*excess_time) self.fps_sleep = create_fps_timer(self.fps, on_miss_callback=callback) # profiling self.profiler = None
def start(self): self.engine.start() self.coach.start() sleep = create_fps_timer(Framework.CHECK_SUBPROCESS_STATE_IN_SECONDS) try: while self.coach.is_alive() and self.engine.is_alive(): sleep() except SystemExit: pass except KeyboardInterrupt: self.logger.debug('Interrupted.') except BrokenPipeError: self.logger.exception('A connection was broken.') except: self.logger.exception('An error occurred.') finally: self.stop_game()
def wait_for_vision(self): self.logger.debug('Waiting for vision frame from the VisionReceiver...') sleep_vision = create_fps_timer(1) while not any(self.vision_state): sleep_vision()
def wait_for_vision(self): self.logger.debug( 'Waiting for vision frame from the VisionReceiver...') sleep_vision = create_fps_timer(1) while not any(self.vision_state): sleep_vision()