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
