Skip to content

Google Assistant

Reem Alsaffar edited this page Jan 6, 2019 · 4 revisions

Guide Lines

1- Prepare the RaspberryPi (RPi). We will use RPi 3:

  • Instruction for installing Raspbian on the SD Card can be found here.
  • Raspbain image can be download from here.
  • Raspbian already comes bundled with Python 2.7 and Python 3. No need to install Python 🎉 .
  • ⚠️We will use Python 3. Python 2 has the same syntax except with minor differences. For now, it's safe to assume the only difference that matters is the print function :

python 2.7:

print "hello world!"

python 3:

print ("hello world!")

2- Instruction for installing Google Assistant on RPi

  • Follow this tutorial on how to install Google Assistant on RPi.

  • Steps are shown in this YouTube playlist

If you have time sync problems, you can use the htpdate tool which retrieves time from a given website.
  • First, install htpdate using apt-get:
sudo apt-get install htpdate 
  • Next, type this commnad everytime you open a terminal:
sudo  htpdate -q -d -s www.raspberrypi.org
  • If you don't want to write it everytime. You can put this command in the '.bashrc' file. The '.bashrc' file is a bash script that runs everytime you open a terminal. To add the command to the basrc file, open it with the "nano" text editor as follows (with sudo for root privilege-equivelent to admin rights in windows PC):
 sudo nano ~/.bashrc

Once opened, add the command to the last line of the file. Save it with ctrl+X, press y (yes), then Enter.

3- Learn Python:

  • Python tutorial here. Topics needed are:
    • Python - Home
    • Python - Overview
    • Python - Environment Setup
    • Python - Basic Syntax
    • Python - Variable Types
    • Python - Basic Operators
    • Python - Decision Making
    • Python - Loops
    • Python - Numbers
    • Python - Strings
    • Python - Lists
    • Python - Tuples
    • Python - Dictionary
    • Python - Functions
    • Python - Modules
    • Python - Exceptions
    • Python - Classes/Objects

4- Extend Google Assistant capabilities by defining custom actions.

5- Integrate with your device.

Edit the sample python script that comes with google assistant SDK. The only thing that needs to be changed is the process_event function. Example is listed below:

def process_event(event):
    """Pretty prints events.

    Prints all events that occur with two spaces between each new
    conversation and a single space between turns of a conversation.

    Args:
        event(event.Event): The current event to process.
    """
    if event.type == EventType.ON_CONVERSATION_TURN_STARTED:
        print()

    print(event)

    if (event.type == EventType.ON_CONVERSATION_TURN_FINISHED and
            event.args and not event.args['with_follow_on_turn']):
        print()
    if event.type == EventType.ON_DEVICE_ACTION:
        for command, params in event.actions:
            if command=='pick'and params['cube_color']=="BLACK":
                msg.data="74"
                pub.publish(msg)
            if command=='pick'and params['cube_color']=="RED":
                msg.data="113"
                pub.publish(msg)
            if command=='pick'and params['cube_color']=="BLUE":
                msg.data="107"
                pub.publish(msg)       

The actions.json file associated with it is:

{
    "manifest": {
        "displayName": "IRI Voice Assistant",
        "invocationName": "Voice Assistant",
        "category": "PRODUCTIVITY"
    },
    "actions": [
        {
            "name": "pickCube",
            "availability": {
                "deviceClasses": [
                    {
                        "assistantSdkDevice": {}
                    }
                ]
            },
            "intent": {
                "name": "pick Cube",
                "parameters": [
                    {
                        "name": "cube_color",
                        "type": "Color"
                    }
                ],
                "trigger": {
                    "queryPatterns": [
                        "pick ($Color:cube_color)? cube",
                        "pic ($Color:cube_color)? cube",
                        "choose ($Color:cube_color)? cube",
                        "pick ($Color:cube_color)? tube",
                        "pic ($Color:cube_color)? tube",
                        "choose ($Color:cube_color)? tube"
                    ]
                }
            },
            "fulfillment": {
                "staticFulfillment": {
                    "templatedResponse": {
                        "items": [
                            {
                                "simpleResponse": {
                                    "textToSpeech": "picking $cube_color cube"
                                }
                            },
                            {
                                "deviceExecution": {
                                    "command": "pick",
                                    "params": {
                                        "cube_color": "$cube_color"
                                    }
                                }
                            }
                        ]
                    }
                }
            }
        }
    ],
    "types": [
        {
            "name": "$Color",
            "entities": [
                {
                    "key": "RED",
                    "synonyms": [
                        "red"
                    ]
                },
                {
                    "key": "BLUE",
                    "synonyms": [
                        "blue"
                    ]
                },
                {
                    "key": "Yellow",
                    "synonyms": [
                        "yellow"
                    ]
                },
                {
                    "key": "BLACK",
                    "synonyms": [
                        "black"
                    ]
                }                
            ]
        }
    ]
}

launch command:

python hotword.py --project-id social-arm-69980 --device-model-id social-arm-69980-my-social-arm-9xnl96