...
Code Block | ||
---|---|---|
| ||
from social_interaction_cloud.abstract_connector import AbstractSICConnector from time import sleep class MyConnector(AbstractSICConnector): def __init__(self, server_ip, robot): super(MyConnector, self).__init__(server_ip, robot) def run(self): self.start() self.set_language('en-US') sleep(1) # wait for the language to change self.say('Hello, world!') sleep(3) # wait for the robot to be done speaking (to see the relevant prints) self.stop() def on_robot_event(self, event): print(event) # Run the application my_connector = MyConnector(server_ip='<SIC IP Address>', robot='nao127.0.0.1') my_connector.run() |
self.start()
activates the connection. Under the hood a thread is started allowing the connector to receive actions and events. self.stop()
gracefully closes the connection. self.set_language('en-US')
and self.say('Hello, world!')
are the two actions send to the robot to make it say ‘Hello, world!’ in English. These methods, as are all actions, are asynchronous. This means that they do not wait for a result before continuing. It also allows, if supported by the robot, to execute actions in parallel (e.g. simultaneously speaking and gesturing).
...
Code Block | ||
---|---|---|
| ||
import threading from social_interaction_cloud.basic_connector import BasicSICConnector from time import sleep class Example: 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. Everytime the MiddleTactilTouched event is generated, this callback function is called, making the robot say 'That tickles!'""" self.sic.say_animated('That tickles!') example = Example('<SIC IP Address>', 'nao'127.0.0.1') example.run() |
In the example above the Nao robot will stand up, say “You can tickle me by touching my head”, and for 10 seconds will say “that tickles” every time you touch the sensor on the middle of its head. After 10 second, the robot will sit down again.
...
Code Block | ||
---|---|---|
| ||
sic = BasicSICConnector(server_ip, robot)
hello_action_lock = threading.Event()
hello_action = Action(self.sic.say, 'Hello, Action!', callback=hello_action_callback,
lock=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.
def hello_action_callback():
print('Hello Action Done')
hello_action_lock.set()
hello_action_lock.clear() # necessary for reuse |
...
Code Block |
---|
sic = BasicSICConnector(server_ip, robot)
action_factory = ActionFactory(sic)
hello_action_factory = action_factory.build_action('say', 'Hello, Action Factory!')
hello_action_factory.peform() |
...
The build_touch_listener()
method lets you build a touch listener that is regsitered registered to the BasicSICConnector
. You can register to all the robot’s sensor events (e.g. MiddleTactilTouched
). You can use it to wait until a button is pressed or to do something when a button is pressed. The listener can be notified only once or every time (until you unregister the listener) the button is pressed.
...
Code Block | ||
---|---|---|
| ||
sic = BasicSICConnector(server_ip, robot)
action_runner = ActionRunner(sic)
action_runner.run_waiting_action('say', 'Hello, Action Runner!',
additional_callback=hello_action_runner_callback)
# run_waiting_action('say', ...) waits to finish talking before continuing
def hello_action_runner_callback():
print('Hello Action Runner Done')
# 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 clear=False as input.
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')
|
...