Пример #1
0
class Example:
    """Example that shows how to use the BasicSICConnector. For more information go to
    https://socialrobotics.atlassian.net/wiki/spaces/CBSR/pages/616398873/Python+Examples#Basic-SIC-Connector"""
    def __init__(self, server_ip, robot):

        self.sic = BasicSICConnector(server_ip, robot)

        self.awake_lock = threading.Event()

    def run(self):
        # active Social Interaction Cloud connection
        self.sic.start()

        # set language to English
        self.sic.set_language('en-US')

        # stand up and wait until this action is done (whenever the callback function self.awake is called)
        self.sic.wake_up(self.awake)
        self.awake_lock.wait(
        )  # see https://docs.python.org/3/library/threading.html#event-objects

        self.sic.say_animated('You can tickle me by touching my head.')
        # Execute that_tickles call each time the middle tactile is touched
        self.sic.subscribe_touch_listener('MiddleTactilTouched',
                                          self.that_tickles)

        # You have 10 seconds to tickle the robot
        sleep(10)

        # Unsubscribe the listener if you don't need it anymore.
        self.sic.unsubscribe_touch_listener('MiddleTactilTouched')

        # Go to rest mode
        self.sic.rest()

        # close the Social Interaction Cloud connection
        self.sic.stop()

    def awake(self):
        """Callback function for wake_up action. Called only once.
        It lifts the lock, making the program continue from self.awake_lock.wait()"""

        self.awake_lock.set()

    def that_tickles(self):
        """Callback function for touch listener. Every time the MiddleTactilTouched event is generated, this
         callback function is called, making the robot say 'That tickles!'"""

        self.sic.say_animated('That tickles!')
class Example:
    """Example that shows how to use the Action, ActionFactory, and ActionRunner classes. For more information go to
    https://socialrobotics.atlassian.net/wiki/spaces/CBSR/pages/616398873/Python+Examples#Action,-ActionFactory,-and-ActionRunner"""

    def __init__(self, server_ip, robot):
        self.sic = BasicSICConnector(server_ip, robot)

        self.hello_action_lock = threading.Event()

    def run(self):
        self.sic.start()
        self.sic.set_language('en-US')

        #################
        # Action        #
        #################
        # Create an action that can be executed when needed and reused
        hello_action = Action(self.sic.say, 'Hello, Action!', callback=self.hello_action_callback,
                              lock=self.hello_action_lock)
        hello_action.perform().wait()  # perform() returns the lock, so you can immediately call wait() on it.
        hello_action.perform().wait()  # you can reuse an action.

        #################
        # Action Factory#
        #################
        # Use the ActionFactory to build actions. You can build actions with a lock build inside of them by
        # using build_waiting_action(). It saves you from keeping track of your locks.
        # build_action() and build_waiting_action() use the name of SIC method instead of an explicit reference to it
        # as input. For example build_action('say', ...) instead of build_action(self.sic.say, ...).
        action_factory = ActionFactory(self.sic)
        hello_action_factory = action_factory.build_waiting_action('say', 'Hello, Action Factory!',
                                                                   additional_callback=self.hello_action_factory_callback)
        hello_action_factory.perform().wait()

        #################
        # Action Runner #
        #################
        # With an ActionRunner you can run regular and waiting actions right away or load them to run them in parallel.
        # When running the loaded actions, it will wait automatically for all loaded waiting actions to finish.
        action_runner = ActionRunner(self.sic)
        action_runner.run_waiting_action('say', 'Hello, Action Runner!',
                                         additional_callback=self.hello_action_runner_callback)
        # run_waiting_action('say', ...) waits to finish talking before continuing

        # The following actions will run in parallel.
        #action_runner.load_waiting_action('wake_up')
        action_runner.load_waiting_action('set_eye_color', 'green')
        action_runner.load_waiting_action('say', 'I am standing up now')
        action_runner.run_loaded_actions()  # If you want to keep an action sequence for reuse, add False as input.
        # run_loaded_actions() will wait automatically before all loaded waiting actions (not regular actions) to finish
        # before continuing

        action_runner.run_action('say', 'I will start sitting down as I say this. Because I am not a waiting action')
        action_runner.run_action('set_eye_color', 'white')
        action_runner.run_waiting_action('rest')

        self.sic.stop()

    def hello_action_callback(self):
        print('Hello Action Done')
        self.hello_action_lock.set()
        self.hello_action_lock.clear()  # necessary for reuse

    def hello_action_factory_callback(self):
        print('Hello Action Factory Done')

    def hello_action_runner_callback(self):
        print('Hello Action Runner Done')