class Comms(Provider): """Comms class spins a thread to repeated send the commands stored in gamestate to the robots via radio""" def __init__(self, team, is_second_comms=False): super().__init__() assert (team in ['blue', 'yellow']) self._team = team self._is_second_comms = is_second_comms self._radio = None self._owned_fields = [ '_blue_robot_status', '_yellow_robot_status', ] # self._receive_loop_sleep = Radio.MESSAGE_DELAY # self._messages_received = [] # self._last_receive_loop_time = None def pre_run(self): if self._radio is None: self._radio = Radio(self._is_second_comms) def run(self): team_commands = self.gs.get_team_commands(self._team) for robot_id, commands in team_commands.items(): # self.logger.info(commands) if self.gs.is_robot_lost(self._team, robot_id): self.logger.debug(f"Robot {robot_id} is lost") commands.set_speeds(0, 0, 0) else: # recalculate the speed the robot should be commanded at pos = self.gs.get_robot_position(self._team, robot_id) commands.derive_speeds(pos) # send serialized message for whole team message = RobotCommands.get_serialized_team_command(team_commands) self._radio.send(message) for robot_id, commands in team_commands.items(): robot_status = self.gs.get_robot_status(self._team, robot_id) # simulate charge of capacitors according to commands if commands.is_charging: robot_status.simulate_charge(self.delta_time) # TODO: UNTESTED if commands.is_kicking: robot_status.charge_level = 0 # sleep to avoid spamming the xbee radio time.sleep(Radio.MESSAGE_DELAY) def post_run(self): if self._radio is not None: self._radio.close()
FL.update_params(val[0], val[1]) elif key == FR.identifier: FR.update_params(val[0], val[1]) elif key == RR.identifier: RR.update_params(val[0], val[1]) elif key == RL.identifier: RL.update_params(val[0], val[1]) for each in Tires: pygame.draw.rect(screen, each.get_color(), each.background_area) screen.blit(pfont.render(each.pressure(), 1, BLACK), each.pressure_pos) screen.blit(tfont.render(each.temperature(), 1, GREY), each.temperature_pos) screen.blit(sfont.render(each.tire_status(), 1, BLUE), each.status_pos) screen.blit(mazda, (screen_pixels.w / 2 - car_pixels.w / 2, 40)) # print count # --- Drawing code should go here # --- Go ahead and update the screen with what we've drawn. pygame.display.flip() # --- Limit to 10 frames per second clock.tick(30) # Close the window and quit. radio_dev.close() pygame.quit() __version = '0.1'