...
Table of Contents | ||||
---|---|---|---|---|
|
Basic SIC Connector
Introduction
The Python API also provides its own concrete implementation of the AbstractSICConnector
class, called the BasicSICConnector
. It allows you to register callback functions for each action you send.
callback functions
called when the action is finished or a result becomes available
e.g.:
for device actions (e.g.:
wake_up
,say
orset_eye_color
), the callback function is called only oncefor touch events (e.g.
MiddleTactilTouched
), the callback function is called every time the event becomes availablethe result of vision operations (e.g.
on_face_recognized(identifier)
), the callback function is called every time the result becomes available
Example
Code Block | ||||
---|---|---|---|---|
| ||||
import threading
from social_interaction_cloud.basic_connector import BasicSICConnector
from time import sleep
class Example:
def __init__(self, server_ip):
self.sic = BasicSICConnector(server_ip)
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('127.0.0.1')
example.run() |
In the example above:
A connected Nao robot will stand up, saying “You can tickle me by touching my head”
To wait until the Nao has finished standing up, the program is locked by the
self.awake_lock.wait()
statement.awake_lock
is an threading.Event() object, that blocks the main thread until the threading.Event() is set by callingself.awake_lock.set()
. This is done in theawake
callback function. This callback function is added to thewake_up
action.
Once the robot is finished standing up,
awake
is called, and the “lock is lifted”, allowing the program to continue.
For 10 seconds will say “that tickles” every time you touch the sensor on the middle of its head.
After 10 seconds, the Nao will sit down again.
A different callback function is that_tickles
. It is subscribed to the MiddleTactilTouched
event. Whenever the program is running, that_tickles
is called each time the middle head sensor is touched.
State Machines Interaction Flows
...
In order to facilitate the implementation of state machines, the library pytransitions ca be used. It is “a lightweight, object-oriented finite state machine implementation in Python with many extensions”. Read their guide to learn more.
Example
Let’s look at an example of how to use it together with the SIC Python API. The example is comprised of a basic interaction flow. In this interaction flow, the robot starts by being asleep, wakes up, introduces itself and gets acquainted with the person and then says goodbye.
...