Python

NAO robot works with Python 2.7. This guide will walk you through the installation process of Python.

Installation

You can download the python programming language from Python’s official website. For this project, Python version 2.7.17 32-bit was used.

For a quick download: Python 2.7.17

Warning

Note that a 32-bit version of Python is REQUIRED.

I suggest installing Python in the default path:

C:\Python27.

Setting the Environment Variable path

While installing python, an option to set the environment variable path will be available. This is optional but if you choose to set it, you can run Python from the cmd simply by opening your command prompt and typing:

C:\Users\YourUserName>python

Then you should see something similar appear:

C:\Users\YourUserName>python
Python 2.7.17 (v2.7.17:c2f86d86e6, Oct 19 2019, 20:49:36) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

Now you are ready to program in python.

If you choose to not set the path through the installation, you can do it manually by navigating to the following location:

Control Panel >> System (Under System and Security if not in icon mode) >> Advanced
System Settings >> Environment Variables

Once you are at the Environment Variables window, you should have a User variable called Path. Either select it and hit the Edit button or double click it. Once the edit window opens, hit the Edit button or double click an empty line and type:

C:\Python27

or

C:\path\to\your\python\here

For the official Python documentation visit: Python Documentation

NAOqi for Python

Once python is installed, you need to use Aldebaran’s NAOqi framework.

Visit SoftBank Robotics Community and download [FORMER NAO VERSIONS] - Python NAOqi SDK. I suggest doing the setup download as you will get the exe file.

Once you begin your installation, you will arrive at this point:

_images/pynaoqi.png

If you have multiple versions of python installed, make sure you have selected the correct version of python (python 2.7) otherwise you will get import issues.

After the installation is done, it will allow you to do

import naoqi
import qi

in your python files.

If these lines run without any issues, we are ready for our first tutorial.

RoboWalknTalk

This tutorial follows parts of examples from the documentation of NAO.

For more information, see: NAOqi’s Developer Guide

Open your favorite Python IDE. Run the following lines of code:

1
2
3
4
5
6
from naoqi import ALProxy
import qi
roboIP = "NAO_IP"   # Example: roboIP = "172.18.72.172"
roboPort = 9559     # This is by default
motion = ALProxy("ALMotion", roboIP, roboPort)
tts = ALProxy("ALTextToSpeech", roboIP, roboPort)

Lines 1-2 are importing the NAOqi for Python framework. For line 3 you need to set your NAO ip address. You can obtain the IP of NAO by simply pressing the button on its chest. For line 4, we are setting up the default port that NAO uses to communicate with your PC.

Line 5 and 6, the ALProxy object lets you create a proxy to a module. This lets you use ALMotion and ALTextToSpeech.

The next part of our script is:

 7
 8
 9
10
11
12
13
14
15
16
# Stiffness value has to be greater than 0
motion.moveInit()
motion.setStiffnesses("Body", 1.0)
motion.post.moveTo(0.5,0,0)
tts.say("I can walk!")
tts.say("I can do multiple thigns at the same time as well.")
tts.say("It is only a matter of time before I take over the world!")
motion.moveTo(0,0, 1.5709*2)
motion.post.moveTo(0.5,0,0)
tts.say("I can even turn around on my own!")

First off, we are required to have NAO in the correct position to walk. We use the function moveInit() in line 8 to do this.

Second, the robot will not walk unless the stiffness levels are set. That is, any value greater than 0 will work. We do this in line 9 by using the function setStiffness(“Body”, 1.0) where the value 1.0 is the stiffness level.

In line 10, we make use of the post method. This method will allow NAO to speak and walk at the same time through synchronous programming. For more information, visit Blocking and Non-Blocking Calls. We also use the moveTo() method to make NAO walk.

In short, moveTo(x, y, z) means x and y are coordinate planes where the front of NAO is the positive x axis and the right arm direction is the positive y axis. The z variable tells NAO how much to rotate.

As NAO continues to walk, it will continue to run lines 11-13. The say(string) function tells NAO what to say.

Once NAO finishes walking, it will execute line 14. The value 1.5709*2 is simply pi, telling NAO to rotate pi, or 180, degrees.

Line 15 tells NAO to move again 0.5 meters and as NAO walks, line 16 will execute.

For other variations such as having NAO talk first then walk or vice versa, visit this site.

You have now completed your first script! I suggest visiting the NAOqi framework website to learn more and create other interesting scripts.