Esempio n. 1
0
    def animate(self,
                animation: Animation,
                block: bool = True,
                done_cb: Callable[[], None] = None) -> ActionHandle:
        """ An action to make the robot perform an animation.

        :param animation: the type of animation to perform.
        :param block: whether to block until the action has finished.
        :param done_cb: a callback to be triggered when the action has completed.
        :return: an action handle.
        """
        self._set_action_start()
        builder = self.__animation_map[animation]()
        handle = ActionHandle()
        handle.add_callback(done_cb)
        handle.add_callback(self._set_action_done)
        self._add_action_handle(handle)
        # For the TE callback fire after motors, you *need a space* and then the <PA> command before <TE=...>
        # this forces the TE command to wait until after the robot has spoken the space.
        cmd = activity_cmd(builder.build() + " " +
                           wait_for_motors_and_speaking().build() +
                           callback_end(handle.id).build())
        self._transport.send(cmd)

        if block:
            self.wait(handle)

        return handle
Esempio n. 2
0
    def walk_right(self,
                   steps: int = 4,
                   block: bool = True,
                   done_cb: Callable[[], None] = None) -> ActionHandle:
        """ An action to make the robot walk right a given number of steps.

        :param steps: the number of steps to take. Must be between 1 and 10 steps. A step means the left foot takes
        a single step, making the robot walk right.
        :param block: whether to block until the action has finished.
        :param done_cb: a callback to be triggered when the action has completed.
        :return: an action handle.
        """
        self._set_action_start()
        builder = walk_right(steps=steps)
        handle = ActionHandle(timeout=builder.duration())
        handle.add_callback(done_cb)
        handle.add_callback(self._set_action_done)
        cmd = activity_cmd(builder.build())
        self._transport.send(cmd)
        handle.start_timer()

        if block:
            self.wait(handle)

        return handle
Esempio n. 3
0
    def __init__(self, read_rate_hz: float = 0.05, log_level=logbook.INFO):
        """ The Robot constructor.

        :param read_rate_hz: the rate in Hz to read data from the robot.
        :param log_level: the level for displaying and logging information, e.g. debugging information.
        """

        StreamHandler(sys.stdout).push_application()
        self.version: str = ""
        self.voltage: float = float("NaN")
        self.is_connected: bool = False
        self._log = Logger('Robot')
        self._log.level = log_level
        self._read_rate_hz = read_rate_hz
        self._keep_alive_secs = 9.0
        self._read_commands = [voltage_cmd()]
        self._read_thread = threading.Thread(target=self._send_read_cmds)
        self._read_event = threading.Event()
        self._keep_alive_thread = threading.Thread(target=self._keep_alive)
        self._keep_alive_event = threading.Event()
        self._transport = TcpTransport(data_received_cb=self._data_received_cb,
                                       initial_cmd=activity_cmd(" "),
                                       log_level=log_level)
        self._action_handle_lock = threading.Lock()
        self._action_handles = {}
        self._is_action_active_lock = threading.Lock()
        self._is_action_active = False
        self._is_running = False
        self._last_cmd_time = 0
Esempio n. 4
0
 def _keep_alive(self):
     """ This function makes sure that the robot doesn't say or do it's automatic functions whilst we
     are using it, because it interferes with the programs we want to write. This function is called
     every 9 seconds and only sends a command to the robot if an action isn't currently running
     and the last command was sent 9 seconds ago. """
     while self._is_running:
         secs_since_last_cmd = time.time() - self._last_cmd_time
         if not self._is_action_active and secs_since_last_cmd > self._keep_alive_secs:
             self._transport.send(activity_cmd(" "))
             self._last_cmd_time = time.time()
             self._log.debug("Keeping alive")
         self._keep_alive_event.wait(timeout=self._keep_alive_secs)
Esempio n. 5
0
    def do(self,
           *commands,
           block: bool = True,
           done_cb: Callable[[], None] = None) -> ActionHandle:
        self._set_action_start()
        cmd_list = [cmd for cmd in commands]
        builder = command_list(*cmd_list)
        handle = ActionHandle()
        handle.add_callback(done_cb)
        handle.add_callback(self._set_action_done)
        self._add_action_handle(handle)
        # For the TE callback fire after motors, you *need a space* and then the <PA> command before <TE=...>
        # this forces the TE command to wait until after the robot has spoken the space.
        cmd = activity_cmd(builder.build() + " " +
                           wait_for_motors_and_speaking().build() +
                           callback_end(handle.id).build())
        self._transport.send(cmd)

        if block:
            self.wait(handle)

        return handle
Esempio n. 6
0
    def say(self,
            text: str,
            block: bool = True,
            done_cb: Callable[[], None] = None) -> ActionHandle:
        """ An action to make the robot speak.

        :param text: the text for the robot to speak.
        :param block: whether to block until the action has finished.
        :param done_cb: a callback to be triggered when the action has completed.
        :return: an action handle.
        """
        self._set_action_start()
        builder = say(text)
        handle = ActionHandle()
        handle.add_callback(done_cb)
        handle.add_callback(self._set_action_done)
        self._add_action_handle(handle)
        cmd = activity_cmd(builder.build() + callback_end(handle.id).build())
        self._transport.send(cmd)

        if block:
            self.wait(handle)

        return handle