Exemplo n.º 1
0
    def __init__(self, color_settings, pitch_number, team_color, color_group09, color_group10, port_group09=None,
                 port_group10=None):
        assert port_group09 is not None or port_group10 is not None
        self.vision = VisionInterface(team_color, color_group09, color_group10,
                                      color_settings=color_settings, pitch=pitch_number, gui=True)

        world_state = WorldState(self.vision, team_color, color_group09, color_group10)
        if port_group09 is not None:
            self.com_group09 = CommunicationGroup09(port_group09)
            self.planner_group09 = PlannerGroup09(world_state, self.com_group09)
        if port_group10 is not None:
            self.com_group10 = CommunicationGroup10(port_group10)
            self.planner_group10 = PlannerGroup10(world_state, self.com_group10)

        try:
            self.strategy = Strategy(world_state, self.planner_group09, self.planner_group10)
        except AttributeError:
            try:
                self.strategy = TestStrategyGroup09(world_state, self.planner_group09)
            except AttributeError:
                self.strategy = TestStrategyGroup10(world_state, self.planner_group10)
        self.display = TerminalDisplay(self.strategy)

        self.main_thread = Thread(target=self.main_task, name="Main Thread")
        self.strategy_thread = Thread(target=self.strategy_task, name="Strategy Thread")
Exemplo n.º 2
0
class Launcher:
    def __init__(self, color_settings, pitch_number, team_color, color_group09, color_group10, port_group09=None,
                 port_group10=None):
        assert port_group09 is not None or port_group10 is not None
        self.vision = VisionInterface(team_color, color_group09, color_group10,
                                      color_settings=color_settings, pitch=pitch_number, gui=True)

        world_state = WorldState(self.vision, team_color, color_group09, color_group10)
        if port_group09 is not None:
            self.com_group09 = CommunicationGroup09(port_group09)
            self.planner_group09 = PlannerGroup09(world_state, self.com_group09)
        if port_group10 is not None:
            self.com_group10 = CommunicationGroup10(port_group10)
            self.planner_group10 = PlannerGroup10(world_state, self.com_group10)

        try:
            self.strategy = Strategy(world_state, self.planner_group09, self.planner_group10)
        except AttributeError:
            try:
                self.strategy = TestStrategyGroup09(world_state, self.planner_group09)
            except AttributeError:
                self.strategy = TestStrategyGroup10(world_state, self.planner_group10)
        self.display = TerminalDisplay(self.strategy)

        self.main_thread = Thread(target=self.main_task, name="Main Thread")
        self.strategy_thread = Thread(target=self.strategy_task, name="Strategy Thread")

    def launch(self):
        try:
            self.main_thread.start()
            if enable_curses:
                self.display.run()
        except KeyboardInterrupt:
            if enable_curses:
                self.display.close()
        self.close()

    def main_task(self):
        if enable_curses:
            print("[LAUNCH] waiting for terminal display to start...")
        if not enable_curses or self.display.wait_for_start():
            self.strategy_thread.start()
            self.vision.launch_vision()
        else:
            self.close()

    def strategy_task(self):
        try:
            try:
                self.com_group09.open(self.strategy.handle_message_group09)
            except AttributeError:
                pass
        except (serial.SerialException, SenderException):
            traceback.print_exc()
        try:
            try:
                self.com_group10.open(self.strategy.handle_message_group10)
            except AttributeError:
                pass
        except (serial.SerialException, SenderException):
            traceback.print_exc()

        vision_launched = False
        while not self.display.closed and not vision_launched:
            print "[LAUNCH] waiting for vision to start..."
            vision_launched = self.vision.wait_for_start(1)
        if vision_launched:
            self.strategy.launch()

    def close(self):
        print("[LAUNCH] closing...")
        self.strategy.close()
        self.vision.close()
        try:
            self.com_group09.close()
        except AttributeError:
            pass
        try:
            self.com_group10.close()
        except AttributeError:
            pass
        self.display.close()