ALBarcodeReader Tutorial

NAOqi Vision - Overview | API | Tutorial


Introduction

This tutorial explains how to run the ALBarcodeReader module using Python. Two methods can be used to retrieve the results pushed by the module in ALMemory, query the memory regularly or subscribe to an event.

Periodic query of ALMemory

This is done by calling the ALMemoryProxy::getData() regularly.

vision_barcodeReader_readMemory.py


from naoqi import ALProxy
import time

ROBOT_IP="your.robot.ip.here"

barcode=ALProxy("ALBarcodeReader", ROBOT_IP, 9559)
barcode.subscribe("test_barcode")

memory=ALProxy("ALMemory", ROBOT_IP, 9559)
# Query last data from ALMemory twenty times

for i in range(20):
  data = memory.getData("BarcodeReader/BarcodeDetected")
  print data
  time.sleep(1)

Subscribe to an event

This method requires to write a small module which inherits from ALModule and contains a callback method which will be called each time the event BarcodeReader/BarcodeDetected() is raised.

Unlike the previous method, this one will only print new data.

vision_barcodeReader_subscribeToEvent.py


from naoqi import *
import time

# Get a proxy on ALBarcodeReader

ROBOT_IP="your.robot.ip.here"
barcode=ALProxy("ALBarcodeReader", ROBOT_IP, 9559)
memory=ALProxy("ALMemory", ROBOT_IP, 9559)
broker = ALBroker("pythonBroker","0.0.0.0", 0, ROBOT_IP, 9559)

# Handler class
class myEventHandler(ALModule):
  def myCallback(self, key, value, msg):
    print "Received \"" + str(key) + "\" event with data: " + str(value)

# Subscribe to the event (this will start the module)
handlerModule = myEventHandler("handlerModule")
memory.subscribeToEvent("BarcodeReader/BarcodeDetected", "handlerModule", "myCallback")

time.sleep(20) # Keep the broker alive for 20 seconds

# Unsubscribe to event
memory.unsubscribeToEvent("BarcodeReader/BarcodeDetected", "handlerModule")

Example of output


Received "BarcodeReader/BarcodeDetected" event with data: [['test', [[84.0, 14.0], [77.0, 72.0], [132.0, 77.0], [139.0, 22.0]]]]
Received "BarcodeReader/BarcodeDetected" event with data: [['test', [[52.0, 11.0], [49.0, 91.0], [130.0, 100.0], [137.0, 15.0]]]]
Received "BarcodeReader/BarcodeDetected" event with data: [['test', [[62.0, 12.0], [54.0, 92.0], [135.0, 103.0], [148.0, 19.0]]]]
Received "BarcodeReader/BarcodeDetected" event with data: [['test', [[63.0, 13.0], [56.0, 93.0], [137.0, 105.0], [148.0, 20.0]]]]
...