liveBook · Manning

QR code

As explained in Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi

A QR code is a two-dimensional machine-readable barcode that encodes data in a patterned square image.

A QR code is a square, machine-readable marker that carries data such as text. In a robot workspace, printed codes can tag objects or locations: a camera reads the code, and the decoded value becomes a simple target for movement or interaction. section 10.0 section 1.1

Generate labels with the Python qrcode package, either from the command line or in Python. A code can be saved as an image, printed, and attached to an object. Data volume affects the chosen symbol version: larger versions produce denser codes, while lower versions are easier to read with a low-resolution camera. section 10.3.1 section 10.6

A practical QR-guided robot loop is:

  • Capture the latest camera image and pass it to OpenCV's QRCodeDetector. section 10.5.1
  • Compare decoded data with the requested target value. section 10.5.1
  • Move in short increments until the target matches or a maximum move count is reached. section 10.5.1
  • Use the match to stop at a tagged object, then trigger another action such as moving a servo arm. section 11.3

Search for a tagged location

A robot drives forward at low speed, decodes the newest video frame after each move, and stops when its data equals start or end. A move limit makes an unsuccessful search return False rather than continuing into the end of the track. section 10.5.1

Listing 10.7 goto_qr.py: Searching and going to the target QR code
#!/usr/bin/env python3
import os
import sys
import cv2
import motor
 
MAX_MOVES = 20
IMG_PATH = os.environ['XDG_RUNTIME_DIR'] + '/robo_stream.jpg'
decoder = cv2.QRCodeDetector()
 
def decode_qr():
    img = cv2.imread(IMG_PATH)
    data, points, _ = decoder.detectAndDecode(img)
    return data
 
def goto(target):
    for i in range(MAX_MOVES):
        motor.forward(speed=1, duration=0.1)
        data = decode_qr()
        print(f'searching {i + 1}/{MAX_MOVES}, data: {data}')
        if data == target:
            return True
    return False
 
def main():
    target = sys.argv[1]
    print('target:', repr(target))
    found = goto(target)
    print('found status:', found)
 
main()

This runnable script combines camera-frame decoding, target comparison, motor movement, and a maximum-search limit. section 10.5.1

How Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi covers it

How deep it goes
Explains generation settings and implements image decoding, target comparison, and bounded motor search without detailing QR encoding internals.
Examples
Demonstrates hello and hi again image labels, a Python.org code, track searches for start and end, and QR-tagged snacks.
What it assumes
Uses a Python and Linux robot environment while introducing QR generation through terminal commands and a Python REPL.
What you can do after
Build QR-guided robot searches that decode camera frames, stop on matching values, and limit unsuccessful movement.
How it connects
Connects QR codes with OpenCV, computer vision, camera video streams, DC motors, servo motors, CSV files, and web applications.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi. The section links point to the parts of the book it draws on.