🤖Lesson 2 : Build a character

Aim

The aim of this lesson is to introduce you to the concept of building robot characters using Misty's API calls that allow you to enable Misty's speech, as well as display and vocalize expressions. If you have any questions about the APIs in this lesson you can check out the Display and LED and Speech and NLP sections.

Expressions and Speech

Challenge 1: Eyes to the skies

Imagine that Misty is a robot astronaut visiting Mars and she raised her head to look at the stars, suddenly a UFO flies by. Is it a plane, a helicopter, an alien bird? The first step is to decide how you want Misty to express her reaction, by using the misty.display_image API you can choose which default expressions Misty should show. In this example we chose 'Terror'. You can find a full list of them here: Image files. Now try out combining the display and movement APIs to build the described sequence.

For more information about the display and its parameters, you can check the Display and LEDsection in Misty Python API. The parameter "alpha" represents the opacity of the image.

from mistyPy.Robot import Robot
import time
misty = Robot()

#the alpha parameter determines the opacity of the image
misty.display_image(fileName= "e_Contempt.jpg", alpha= 1) 
time.sleep(1)
misty.move_head(-40, 0, 0)
misty.move_arm("both", -90)
time.sleep(2)
misty.display_image("e_Terror2.jpg")

Misty' has many other display capabilities which you can uncover by typing the keyword "display". If you want her to display a video or text instead of an image, you can use the same arguments within the parameters.

Challenge 2: I spy with my little eye

Great job! You have built your first robot character sequence, but it's still missing an important human element to really bring your character to life. The misty.speak API works much in the same way as the Speak block in Blockly and shares many of the same parameters. You can choose what Misty will say, in what language and with which pitch. Misty uses US English by default if nothing else is specified. Let's try inserting the API into the sequence and have Misty tell you what she sees.

from mistyPy.Robot import Robot
import time
misty = Robot()

misty.display_image("e_Contempt.jpg")
time.sleep(1)
misty.move_head(-40, 0, 0)
misty.move_arm("both", -90)
misty.speak(text= "I spy with my little eye", pitch= 0.8)
time.sleep(0.5)
misty.display_image("e_Terror2.jpg")
misty.speak("a UFO!!")

Challenge 3: Uh oh!

You can also have Misty emphasize her reaction with one of her vocal expressions by using the misty.play_audio API. You can find all Misty's default audio in Audio files and if you've uploaded audio files on Misty in Blockly, you can use their names in the parameters. How would you customize it?

As shown in the example below, the first parameter is a string, and it contains the name of the file you want to use, the second is a float and it represents the volume.

from mistyPy.Robot import Robot
import time
misty = Robot()

misty.display_image("e_Contempt.jpg")
time.sleep(1)
misty.move_head(-40, 0, 0)
misty.move_arm("both", -90)
misty.display_image("e_Terror2.jpg")
misty.speak("I spy with my little eye")
time.sleep(0.5)
misty.speak("a UFO!!")
time.sleep(0.5)
misty.play_audio(fileName= "s_PhraseUhOh.wav",volume= 80)

Language

Challenge 4: Can you speak my language?

What if your robot astronaut ends lands by accident in a different country and suddenly you need Misty to speak a different language? In the Speak Block the language could be selected from a drop-down list, however in Python you will need to set the language as voice parameter with a string of code. You can find the strings for each language in Languages. Let's try having Misty repeat the previous sequence in Italian or in one of your favorite languages.

from mistyPy.Robot import Robot
import time
misty = Robot()

misty.display_image("e_Contempt.jpg")
time.sleep(1)
misty.move_head(-40, 0, 0)
misty.move_arm("both", -90)
misty.display_image("e_Terror2.jpg")
misty.speak("Spio con i miei occhietti", voice= "it-it-x-itb-local")
time.sleep(0.5)
misty.speak("un UFO!!", voice= "it-it-x-itb-local")
time.sleep(0.5)
misty.play_audio(fileName= "s_PhraseUhOh.wav",volume= 80)

Chest LED

Challenge 5: Houston we have a problem

To emphasize your robot character's expressions even more you can use the misty.change_led API to control the Chest LED. For example you can add a yellow alert light to the previous example to emphasize Misty's terror of seeing the UFO. You can also use the misty.transition_led API to cycle between different colors.

from mistyPy.Robot import Robot
import time
misty = Robot()

misty.display_image("e_Contempt.jpg")
time.sleep(1)
misty.move_head(-40, 0, 0)
misty.move_arm("both", -90)
misty.speak(text= "I spy with my little eye", pitch= 0.8)
time.sleep(2)
misty.display_image("e_Terror2.jpg")
misty.change_led((red= 255, green= 255, blue=0)
misty.speak("a UFO!!")
misty.play_audio(fileName= "s_PhraseUhOh.wav",volume= 80)
misty.transition_led(255,0,0,255,255,0,"blink")
misty.speak(text= "Houston, we have a problem", pitch= 0.8)

Challenge 6: E.T go home

Remember how Misty created a disco ball effect in Blockly Lessons? We can adapt the same sequence to create a scenario where your robot astronaut gets sucked up by a colorful beam of light from the UFO.

from mistyPy.Robot import Robot
import time
misty = Robot()

misty.move_head(40,0,0,100)
time.sleep(0.5)
misty.move_head(-40,0,0,100)
time.sleep(0.5)
misty.move_head(0,0,0,100)
misty.move_arm("both",-60,100)
misty.display_image("e_EcstacyHilarious.jpg")
misty.play_audio("s_PhraseNoNoNo.wav",60)
misty.change_led(red= 0, green= 255, blue=40)
time.sleep(0.2)
misty.change_led(red= 150, green= 30, blue=0)
time.sleep(0.2)
misty.change_led(red= 48, green= 42, blue=79)
time.sleep(0.2)
misty.change_led(red= 231, green= 25, blue=41)
time.sleep(0.2)
misty.change_led(red= 94, green= 200, blue=194)
time.sleep(0.2)
misty.change_led(red= 3, green= 147, blue=240)
time.sleep(0.2)
misty.change_led(red= 149, green= 36, blue=236)
time.sleep(0.2)
misty.stop()

Challenge 7: Compact your code

Inserting values for each of the color parameters and adding new lines of code can be tiresome, this is where importing libraries can be incredibly helpful. By importing the "random" library you can extract a random number in a range from 0 to 255. in this way yo can randomize the color values, save lines of code and space for the sequence.

With a cleaner code and the ability to randomize your values, you can now generate how many colors you want without creating an enormous code. To do this, you will need to create a loop and insert the random.randrange function inside your misty.change_led API as shown in the example below. We can select the range and Misty will do the rest. Amazing isn't it?

from mistyPy.Robot import Robot
import random
import time
misty = Robot()

misty.move_head(40,0,0,100)
time.sleep(0.5)
misty.move_head(-40,0,0,100)
time.sleep(0.5)
misty.move_head(0,0,0,100)
misty.move_arm("both",-60,100)
misty.display_image("e_EcstacyHilarious.jpg")
misty.play_audio("s_PhraseNoNoNo.wav",60)
for i in range (100):
    misty.change_led(random.randrange(0, 255), random.randrange(0, 255), random.randrange(0, 255))
    time.sleep(0.2)
misty.stop()

Up Next 👇

🧠pageLesson 3: Create memories

Last updated