Cubie-3 Code
An open-source 3d printable cube robot you can build yourself
Video
For every project I create, I often make a corresponding YouTube video. Sometimes, there might be more than one video for a single project. You can find these videos in this section.
Explore more through this this dedicated video.
The full version controlled code for this project is available at Cubie-3 GitHub Respository.
Note
This code requires the Sunfounder Fusion HAT+ Python library. You can find installation instructions in the Sunfounder Fusion HAT+ - What Can’t This Thing Do? video.
Example code to drive the motors:
from cubie_3 import Cubie3
import asyncio
async def main():
robot = Cubie3()
# Move forward at 30% speed
await robot.forward(0.3)
await asyncio.sleep(1)
# Strafe right
await robot.strafe_right(0.5)
await asyncio.sleep(0.5)
# Stop all motors
await robot.stop()
asyncio.run(main())
Voice Recognition with Vosk
Cubie-3 uses Vosk for offline speech-to-text recognition. This means your robot can respond to voice commands without needing an internet connection - all processing happens locally on the Raspberry Pi.
What is Vosk?
Vosk is a lightweight, offline-capable speech recognition engine that:
- Works without an internet connection
- Supports 20+ languages
- Runs on resource-constrained devices like Raspberry Pi
- Has small language models (as small as 50MB)
How It Works in Cubie-3
The voice recognition system uses the Sunfounder Fusion HAT+ library which wraps Vosk functionality. When you call the listen() method:
- Audio capture - The USB microphone captures audio continuously
- Real-time processing - Vosk processes audio in a stream, providing partial results as you speak
- Command matching - When a final result is recognized, Cubie-3 checks if it matches a known command
- Action execution - Matching commands trigger the corresponding robot movement
Setting Up Voice Recognition
1. Hardware Requirements
- USB microphone (essential for clear audio input)
- Sunfounder Fusion HAT+ installed on your Raspberry Pi
2. Configure Your Microphone
First, identify your USB microphone:
arecord -l
You’ll see output like:
card 1: Device [USB Audio Device], device 0: USB Audio [USB Audio]
Test recording (replace 1,0 with your card and device numbers):
arecord -D plughw:1,0 -f S16_LE -r 16000 -d 3 test.wav
aplay test.wav
If the recording is too quiet or muted, adjust levels:
alsamixer
- Press
F6to select your USB microphone - Find the Mic or Capture channel
- Ensure it shows
[OO](unmuted) - pressMto toggle - Use arrow keys to adjust volume
3. Install Dependencies
The Fusion HAT+ library handles Vosk installation, but if you need to install manually:
sudo apt install portaudio19-dev python3-pyaudio
pip3 install vosk
The language model downloads automatically on first use.
Voice-Controlled Demo
Here’s the complete voice control example from the Cubie-3 repository:
from cubie_3 import Cubie3
import asyncio
async def main():
cubie3 = Cubie3()
# Announce startup
cubie3.tts.say("Starting movement demo.")
# Start listening for voice commands
await cubie3.listen()
if __name__ == "__main__":
asyncio.run(main())
Recognized Voice Commands
Cubie-3 responds to these voice commands:
| Command | Action |
|---|---|
| “forward” | Move forward |
| “backward” | Move backward |
| “strafe left” | Move sideways to the left |
| “strafe right” | Move sideways to the right |
| “rotate left” | Spin counter-clockwise |
| “rotate right” | Spin clockwise |
| “stop” | Stop all motors |
| “goodbye” | End the listening session |
How the Listen Method Works
The listen() method in the Cubie3 class implements continuous speech recognition:
# Simplified example of how listen() works internally
from fusion_hat.stt import Vosk as STT
stt = STT(language="en-us")
while True:
print("Listening...")
for result in stt.listen(stream=True):
if result["done"]:
# Final recognized text
command = result['final'].lower()
if "forward" in command:
await robot.forward(0.5)
elif "stop" in command:
await robot.stop()
# ... more commands
else:
# Partial result (updates as you speak)
print(f"Heard: {result['partial']}", end="\r")
Text-to-Speech (TTS)
Cubie-3 can also speak back to you using the say() method:
from cubie_3 import Cubie3
robot = Cubie3()
# Robot speaks using Espeak TTS
robot.tts.say("Hello, I am Cubie-3!")
robot.tts.say("Ready for your commands.")
The TTS system uses Espeak, which is lightweight and built into Raspberry Pi OS. While the voice sounds robotic, it’s highly configurable for volume, pitch, and speed.
Tips for Better Voice Recognition
- Microphone position - Keep the microphone 15-30 cm from your mouth
- Quiet environment - Background noise reduces accuracy
- Clear speech - Speak clearly and at a normal pace
- Pause between commands - Give the system time to process
Troubleshooting
“No audio detected”
- Check microphone connection with
arecord -l - Verify volume levels in
alsamixer - Test recording with
arecordcommand
“Commands not recognized”
- Speak the exact command phrases
- Check you’re using the correct language model
- Try speaking more slowly and clearly
“Model download fails”
- Check internet connection (only needed for initial download)
- Manually download from Vosk Models
- Use the small English model:
vosk-model-small-en-us-0.15
Supported Languages
Vosk supports many languages. Change the language when initializing:
# For British English
stt = STT(language="en-gb")
# For German
stt = STT(language="de")
# For French
stt = STT(language="fr")
Available language codes include: en-us, en-gb, de, fr, es, it, pt, ru, cn, ja, ko, and many more.
Further Resources
You can use the arrows ← → on your keyboard to navigate between lessons.