# 8. Mission 3 Make the Car Understand You

##### Share for us

Now, the car can speak. Next, we need to make it understand our commands.

There is a MIC in the PiSmart as its “ear”. You only need to write the corresponding code to make it get your commands (but need to be preset).

Use nano to modify the previous code.

pi@raspberrypi:~/SunFounder_PiSmart_Car \$ nano my_pismart.py

First, add my_pismart. listen at the very beginning of the loop() to let the car starts listening.

Then add a judgment statement if my_pismart.heard to see whether the car gets the command or not. If it heard something, my_pismart.heard will return true and run the if statement; if it does not hear anything, my_pismart.heard will return false and not run if.

If the car heard something, then run my_pismart.result to figure out the commands. If it’s the go forward command, my_pismart.resultwill return forward which satisfies the condition of if (rules written). Then set the motor speed as 60, and say its actions like “I go forward”.

Then delete stop and put it in the backmost, which indicates the car takes each action and keep it for 3 seconds before stop, and then continue to take commands. Please pay attention to the number of spaces indented and align this line with if my_pismart.result ==“right”:

``````GNU nano 2.2.6                                                                  File: my_pismart.py
… …
… …
def loop():
my_pismart.listen    # Begin to listen
if my_pismart.heard:    # if heard something
if my_pismart.result == "forward":  # if heard forward
# PiSmart Car move forward
my_pismart.MotorA = 60
my_pismart.MotorB = 60
my_pismart.Say = "I go forward!"
sleep(3)

if my_pismart.result == "backward":  # if heard backward
# PiSmart Car backward
my_pismart.MotorA = -60
my_pismart.MotorB = -60
my_pismart.Say = "I go backward!"
sleep(3)

if my_pismart.result == "left":  # if heard turn left
# PiSmart Car turn left
my_pismart.MotorA = 60
my_pismart.MotorB = 20
my_pismart.Say = "I turn left!"
sleep(3)

if my_pismart.result == "right":  # if heard turn right
# PiSmart Car turn right
my_pismart.MotorA = 20
my_pismart.MotorB = 60

my_pismart.Say = "I turn right!"
sleep(3)

my_pismart.MotorA = 0              # stop PiSmart car
my_pismart.MotorB = 0
sleep(0.5)
… …
… …

[ Read 25 lines ]
^G Get Help   ^O WriteOut    ^R Read File   ^Y Prev Page    ^K Cut Text       ^C Cur Pos
^X Exit       ^J Justify     ^W Where Is    ^V Next Page    ^U UnCut Text     ^T To``````

Since some if statements are added in the code, the corresponding main code underneath should indent 4 spaces twice (8 spaces in total).

After running, there will be lots of information printed on the screen about the initialization and working of the speech recognition engine. Speak to the mic of the PiSmart, and it will do the corresponding action after receiving the command.

``````......
......
00000000 (0x72716e48): 68 65 79 20 70 69 20 73 6d 61 72 74 0a 00        forward..
forward
00000000 (0x727216f0): 68 6f 77 20 61 72 65 20 79 6f 75 0a 00           right..
right
......``````

Press Ctrl +C to stop listening and exit the program.

The commands for PiSmart to recognized pre-establishing in the file dictionary.sps. Use nano to open this file:

pi@raspberrypi:~/SunFounder_PiSmart_Car \$ nano dictionary.sps

`````` GNU nano 2.2.6                                                    File: my_pismart.py
[ forward ];
@results
0 {'forward'}
@

[ left ];
@results
0 {'left'}
@

[ right ];
@results
0 {'right'}
@

[ backward ];
@results
0 {'backward'}
@

[ Read 10 lines ]
^G Get Help   ^O WriteOut    ^R Read File   ^Y Prev Page    ^K Cut Text       ^C Cur Pos
^X Exit       ^J Justify     ^W Where Is    ^V Next Page    ^U UnCut Text     ^T To Spell``````

You need to write the .sps file according to certain principles. Here we offer a simple example. A completed sps structure should be like below:

``````[ words ];
@results
0 {'result'}
@``````

[ words ];          Command recognized which should be in a square bracket.

@results           Use @results and another @ subsequent to quote contents as the result.

0 {‘result’}         0 stands for result No.0. The content inside {‘result’} is the response to [words] (the recognized command)

@                 Ending symbol of the result lines.

For beginners, you’re recommended to copy the whole structure and then modify the words and result.

If you want to know the detailed writing syntax of an .sps file, please refer to The SPS File Format at:

https://github.com/sunfounder/SunFounder_PiSmart/blob/master/documents/The-SPS-File-Format.md

Or go to the GitHub page of SunFounder and find SunFounder_PiSmart/document/The-SPS-File-Forma.md.

Since the car may take single words as the command wrongly, it would be better to enrich the command words. Now, add action words before the direction ones.

``````GNU nano 2.2.6                                                                  File: my_pismart.py
[ go forward ];
@results
0 {'forward'}
@

[ turn left ];
@results
0 {'left'}
@

[ turn right ];
@results
0 {'right'}
@

[ go backward ];
@results
0 {'backward'}
@

[ Read 10 lines ]
^G Get Help   ^O WriteOut    ^R Read File   ^Y Prev Page    ^K Cut Text       ^C Cur Pos
^X Exit       ^J Justify     ^W Where Is    ^V Next Page    ^U UnCut Text     ^T To Spell ``````

Keep result unchanged; single-word result makes code writing more quickly.

Press Ctrl + O to save and Ctrl + X to exit. Run my_pismart.py again:

pi@raspberrypi:~/SunFounder_PiSmart_Car \$ nano my_pismart.py

Try saying “go forward” to it.