RoboticsEngineer

Voice-Controlled Robot That Works Offline

Say 'forward', 'left' or 'stop' and an offline speech engine drives the motors — in English or Spanish.

LevelEngineer
Time2 h
Est. cost~$175
Steps5

Vosk is an offline speech recognition toolkit with small models for many languages, including Spanish. Limiting it to a short word list makes it fast and accurate on a Pi.

Mission steps

  1. Build the robot

    Use the obstacle robot wiring for motors and power.

  2. Install Vosk

    pip install vosk sounddevice in a venv and download a small English or Spanish model from the Vosk website.

  3. Limit the vocabulary

    Pass a JSON grammar of your commands so the recognizer only listens for them.

  4. Map words to moves

    Each recognized word calls a gpiozero Robot method.

  5. Add a wake word

    Require 'robot' before a command so conversation in the room does not move it.

Code

import json, queue, sounddevice as sd
from vosk import Model, KaldiRecognizer
from gpiozero import Robot, Motor, OutputDevice

OutputDevice(25).on()
bot = Robot(left=Motor(5, 6, enable=12), right=Motor(13, 19, enable=26))
WORDS = {'adelante': bot.forward, 'atrás': bot.backward, 'izquierda': bot.left, 'derecha': bot.right, 'para': bot.stop}
rec = KaldiRecognizer(Model('vosk-model-small-es-0.42'), 16000, json.dumps(list(WORDS) + ['[unk]']))
q = queue.Queue()

with sd.RawInputStream(samplerate=16000, blocksize=8000, dtype='int16', channels=1, callback=lambda d, f, t, s: q.put(bytes(d))):
    while True:
        if rec.AcceptWaveform(q.get()):
            word = json.loads(rec.Result()).get('text', '')
            if word in WORDS:
                print('->', word); WORDS[word]()

Tip: test every script on the bench before installing it in a moving machine.

Questions

Which model name do I use?

Use the folder name of the small model you downloaded from the Vosk models page — names include the language and version.