Skip to content

Remote application for NAO in Python: Detect and track QR tags (normal)

Manos Tsardoulias edited this page May 11, 2016 · 13 revisions

The current tutorial describes a more complicated application, as it will employ both the robot-agnostic RAPP robot API and the RAPP Platform API, aiming to implement a simple closed-loop tracking of a QR code. This application will be executed remotely in a PC.

For this tutorial we will use the following tools:

  • A real NAO robot
  • the NaoQi Python libraries
  • the rapp-robots-api Github repository
  • the rapp-api Github repository
  • the rapp-platform up and running

Of course the standard prerequisites are a functional installation of Ubuntu 14.04, an editor and a terminal.

##Preparation steps

####NaoQI Python libraries setup Please check here.

####RAPP Robots Python API setup Please check here.

####RAPP Platform API setup

It is assumed that till now you have created the ~/rapp_nao folder where the NaoQi Python libraries and the RAPP Robots API Python libraries exist. The next step is to clone the RAPP Platform API:

cd ~/rapp_nao
git clone https://github.com/rapp-project/rapp-api.git

In order to utilize the RAPP Platform API the PYTHONPATH environmental variable has to be updated:

echo 'export PYTHONPATH=$PYTHONPATH:~/rapp_nao/rapp-api/python' >> ~/.bashrc
source ~/.bashrc

It should be stated that if you have already set-up the RAPP Platform in your PC, the RAPP Platform API has been already installed and the previous step is not necessary.

####RAPP Platform setup

In order to set-up the RAPP Platform you can follow this tutorial. Have in mind that the RAPP Platform installation is quite time consuming.

##Writing the application

Let's create a Python file for our application and give it execution rights:

cd ~/rapp_nao
mkdir rapps && cd rapps
touch qr_app.py
chmod +x qr_app.py

The robotic application follows:

#!/usr/bin/env python

import time, threading
# Import the RAPP Robot API
from rapp_robot_api import RappRobot
# Import the QR detection module
from RappCloud import QrDetection

# Create an object in order to call the desired functions
rh = RappRobot()
# Instantiate a new FaceDetection service.
qrDetector = QrDetection(image='')

# Enable the NAO motors for the head to move
rh.motion.enableMotors()

def callback():
    # Capture an image from the NAO cameras
    rh.vision.capturePhoto("/home/nao/qr.jpg", "front", "640x480")
    # Get the photo to the PC
    rh.utilities.moveFileToPC("/home/nao/qr.jpg", "~/rapp_nao/qr.jpg")
    # Check if QRs exist
    qrDetector.file = "~/rapp_nao/qr.jpg"
    res = qrDetector.call()

    if len(res.qr_centers) == 0: # No QR codes were detected
        return
    else: # One or more QR codes detected
        qr_center = res.qr_centers[0]
 
    # Directions are computed bounded in [-1,1]
    dir_x = (qr_center.x - (640/2)) / (640.0 / 2.0)
    dir_y = (qr_center.y - (480/2)) / (480.0 / 2.0)
    # Set NAO angles according to the QR center
    rh.humanoid_motion.setJointAngles(["HeadYaw", "HeadPitch"], [dir_x * 0.1, dir_y * 0.1], 0.5)

# Call the callback every 1 sec
threading.Timer(1, callback).start()

Finally, you just have to execute python ~/rapp_nao/rapps/qr_app.py to deploy the application.

Clone this wiki locally