Skip to main content

Lesson 8 · Voice Recognition

What is this lesson about?

This lesson focuses on one core idea: voice commands.

Unlike distance or light sensors, the voice module does not keep returning a changing number.
It behaves more like this: once it hears a command, it returns the matching command ID.

ASR voice recognition module

Learning goals

  • Know the default wake phrases and common built-in command phrases
  • Read the command ID returned by the voice module
  • Map a few common command IDs to robot actions
  • Understand the difference between voice input and continuous sensor input
  • Complete a simple "listen and act" program

Hardware and APIs used

This lesson requires the ASR voice recognition module.

The main APIs used here are:

  • cm.power_on(): power on the expansion modules
  • cm.asr_get_command(): read the latest command ID. It usually returns 0 when there is no new command.
  • cm.asr_play_command(cmd_id): play back the phrase for a given command ID
  • cocube.move_ms(direction, speed, ms): perform one short movement action
  • cocube.rotate_ms(direction, speed, ms): perform one short turn
  • cocube.brake(): stop with braking

First, understand what this capability means

Voice recognition is different from distance or light sensing in one obvious way:

  • distance and light are continuous inputs
  • voice commands are discrete events

So the programs in this lesson look very much like "wait for a trigger":

  • if a command is heard, act
  • if not, keep waiting

Before you start writing code, also remember the normal use order:

  1. speak a wake phrase first
  2. wait for the module to enter listening mode
  3. then speak the command phrase
  4. read the returned command ID in code

The default wake phrases are:

  • 你好小智
  • 小智你好

If the module has been trained before, a learned wake phrase may also exist as ID 1.

These built-in command IDs are enough to remember first:

IDSpoken phraseGood first use
19前进first movement test
20后退first movement test
21停车first movement test
22左转九十度extended action
25右转九十度extended action
28加速一档extended action
29减速一档extended action
82打开夹爪expansion-module linkage
83关闭夹爪expansion-module linkage

Start coding

1. First inspect which command ID the module returns

import time
import cocube_module as cm

cm.power_on()
time.sleep_ms(500)

while True:
command = cm.asr_get_command()

if command:
print("command =", command)

time.sleep_ms(50)

Run this first and make sure the module can return command IDs stably.

1.1 What should you say first?

Use this rhythm for testing:

  1. say 你好小智 or 小智你好 first
  2. wait for the module to respond
  3. then say 前进, 后退, or 停车

If the module has been retrained before, you can also try your custom wake phrase.

2. Bind three common commands to robot actions

Start with the smallest useful set:

  • 19: forward
  • 20: backward
  • 21: stop
import time
import cocube
import cocube_module as cm

cm.power_on()
time.sleep_ms(500)

while True:
command = cm.asr_get_command()

if command == 19:
print("received forward command")
cm.asr_play_command(19)
cocube.move_ms(cocube.FORWARD, 25, 500)
elif command == 20:
print("received backward command")
cm.asr_play_command(20)
cocube.move_ms(cocube.BACKWARD, 25, 500)
elif command == 21:
print("received stop command")
cm.asr_play_command(21)
cocube.brake()

time.sleep_ms(50)

The key point in this program is simple:
the same numeric ID can mean any robot action you choose to assign to it.

What should you observe while it runs?

When you run this lesson, focus on these effects:

  • when there is no new command, asr_get_command() usually returns 0
  • when a command arrives, the action is a one-time trigger, not a continuous state
  • forward, backward, and stop are the best first command group to test
  • make recognition stable first, then add more complex action combinations

Common issues / reminders

  • after power-on, the module needs a little time to get ready
  • the quieter the environment, the more stable recognition is
  • speak a wake phrase first, then the command phrase; saying only the command phrase usually does not trigger anything
  • get 2 or 3 commands working first, then add more
  • if nothing happens, print the command ID first instead of debugging the action logic immediately

If you want to use your own command phrases

cocube-learn has a more complete operational guide for training. Here we only keep the programming details you need most.

1. Learn a new wake phrase

The wake phrase moves the module from standby into listening mode.

Use this order:

  1. wake the module first with the default wake phrase 你好小智 or 小智你好
  2. say 学习唤醒词
  3. follow the voice prompt and say the wake phrase you want to set
  4. after the module confirms it, test the new wake phrase once

Notes:

  • before relearning, it is best to delete the old learned wake phrase first
  • keep the new wake phrase short and distinct from common command phrases
  • a quiet environment helps a lot

2. Learn new command phrases

If you do not want to use only the default commands such as 前进, 后退, and 停车, you can record your own command phrases.

Use this order:

  1. wake the module with a wake phrase
  2. say 学习命令词
  3. follow the prompt to record your command phrases one by one
  4. each successful new phrase occupies a new command ID

The most important point is:

  • learned command IDs start from 4
  • the first learned phrase is usually 4
  • the second is usually 5
  • the third is usually 6

So after training, it is best to run the print-only program first, confirm which ID belongs to which phrase, and only then write the action mapping.

3. Delete learned entries

If you trained something incorrectly or want to start over:

  1. wake the module first
  2. say 我要删除
  3. then choose one of the prompted options:
  • 删除命令词
  • 删除唤醒词
  • 全部删除
  • 退出删除

Challenge

Build your own "custom command remote-control car":

  1. train 3 custom command phrases
  2. run the print-only program to confirm their IDs
  3. map the 3 IDs to forward, backward, and stop
  4. add one extra command such as turn left or open the gripper

If you want to extend it further, try:

  • supporting both the default command phrases and your custom phrases
  • giving different commands different screen prompts or audio feedback
  • adding one more turning or gripping action
Reference code: control CoCube with learned custom commands
import time
import cocube
import cocube_module as cm

cm.power_on()
time.sleep_ms(500)

while True:
command = cm.asr_get_command()

if command == 4:
cocube.move_ms(cocube.FORWARD, 25, 600)
elif command == 5:
cocube.move_ms(cocube.BACKWARD, 25, 600)
elif command == 6:
cocube.brake()
elif command == 7:
cocube.rotate_ms(cocube.LEFT, 25, 500)

time.sleep_ms(50)

This example maps the first four learned commands to forward, backward, stop, and turn left.
If your training order is different, replace 4/5/6/7 with the IDs you actually observed.

Quick reference

The most common pattern in this lesson is:

import cocube_module as cm

cm.power_on()
print(cm.asr_get_command())
cm.asr_play_command(19)
  • cm.power_on(): power on the expansion modules
  • cm.asr_get_command(): read the latest command ID
  • cm.asr_play_command(id): play the matching spoken response
  • cocube.move_ms(...): perform one short action
  • cocube.brake(): stop

If you want to keep looking up peripheral APIs, go to the cocube_module reference.