🦿Lesson 1: Loco-motion

Aim

Welcome to your first lesson in Misty Python! The aim of this lesson is to introduce you to some of the basics of building interactions with Misty using Python, starting with movement and locomotion. If you have any questions about the individual APIs to trigger locomotion and movement, you can check out the Motion and Mobility section in Python Elements.

Motion Sequences

Challenge 1: Hello again human!

If you've completed the Blockly Lessons you might remember that Misty started her first lesson by teaching you how to make her greet humans. In this challenge you can rebuild the Movement blocks using Python and modify Misty's head and arm positions. Let's dive into Misty Python!

Below are three lines of API calls that you can use, the API misty.move_head on the first line controls Misty's head movement. Like the Head movement block in Blockly, the parameters are pitch, roll and yaw. If you don't recall the range of values for this API you can check them out in Motion and Mobility.

The second line is an arm movement API called misty.move_arm, in the parameters you need to specify which arm Misty has to move and in to which position, the first value is a string and can be "right", "left" or "both" and the second is the value to define the degree of movement.

The third line is another arm movement line with the API misty.move_arms, which allows you to control two arms simultaneously and set them to different degrees. Let's try using these APIs to have Misty wave a hello again!

from mistyPy.Robot import Robot

misty = Robot()

#first line
misty.move_head(pitch= 0, roll= 20, yaw= 0)

#second line 
misty.move_arm(arm= "both", position= 0)

#third line 
misty.move_arms(leftArmPosition= 0, rightArmPosition= 70)

Challenge 2 : Remember to sequence

Just like in Blockly, when you build a sequence of two or more movements Misty needs time to achieve each position and execute the code in a consecutive order. If you don't give her time between movements, she will try to execute the movements at the same time. While in Blockly we could simply insert a timer block, in Python you will need to import a library with the time functions you need.

A library is a group of functions created be programmers for other programmers to use. It's a really powerful tool because you can implement predefined functions instead of building your own from scratch. In this case, we'll use the library "time", where we can find many functions related to timing sequences.

To use a the time library you need to import it as shown in the example below and then call the specific function you want to use.

import time 

time.sleep(#timeinseconds)

In this case we will use the time.sleep function which works much like the timer block in Misty Blockly. The parameter's unit is in seconds.

Now try out timing your sequence to have Misty wave her right arm up and down.

from mistyPy.Robot import Robot
import time   # importing the library

misty = Robot()

misty.move_head(0, 30, 0)
misty.move_arms(70, -50)
time.sleep(0.4)  #using the function in the library
misty.move_arms(70, 0)
time.sleep(0.4)  #sleep for 0.4 seconds or 400 milliseconds
misty.move_arms(70, -50)
time.sleep(0.4)
misty.move_arms(70, 0)

If you want you can also define which arm you want to use.

from mistyPy.Robot import Robot
import time

misty = Robot()

misty.move_head(0, 30, 0)
misty.move_arm("left", 70) # choosing Misty's arm and its position
misty.move_arm("right", -50)
time.sleep(0.4)
misty.move_arm("right", 0)
time.sleep(0.4)
misty.move_arm("right", -50)
time.sleep(0.4)
misty.move_arm("right", 0)

Challenge 3: Loop your sequences

Just like in Blockly you can also create loops in Python to save space and compact your code. Let's give it a try by having Misty repeat your sequence 5 times!

There are man ways you can code loops in Python, for this example you can use the for i in range () method. Within the brackets you can choose the number of times you want the sequence to be repeated.

To create a loop with this method you need 4 elements:

  • initialization: where you define the variables that you'll use in the loop. In this case the "i"

  • check: where you control if the loop is verified and so go ahead. With this method, you don't need to insert the check because it's automatic.

  • body: where you have all the action that your loop does. In this code all that's after the ":" and one tab away from the "for"

  • update: where you update your variables, in this way your variable will be increased and there will be a moment where the check is not verified anymore, in this way, you can stop the loop. With this method, the update is automatic.

To exit from the loop and insert commands after the loop you can keep writing your code on the same line as the "for"

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

for i in range(5): #init, check, update
    misty.move_arms(70, -50) #body
    time.sleep(0.4) 
    misty.move_arms(70, 0)
    time.sleep(0.4)
#other actions outside the loop
misty.move_arms(0, 0)

Driving

Challenge 4: Need for speed

Now that you've mastered Misty's arm and head movement APIs, let's explore how to make her drive using the misty.drive_time and misty.drive_arc API calls.

In the code example below, Misty is driving forward for 3 seconds, then she does a turn and goes back to her initial position. In the first two lines of code you can see that the parameters clearly defined, while in the second set of lines they are shown as regular values. In the future you can choose to highlight the parameters or memorize their order and write values. Now let's test to see how she executes the pattern on the ground, if you want her to perform it like a street car, you can always satisfy your need for speed by increasing the velocity.

from mistyPy.Robot import Robot

misty = Robot()

misty.drive_time(linearVelocity= 80, angularVelocity= 0, timeMs= 3000)
misty.drive_arc(heading= 180, radius= 0.5,timeMs= 4000)
misty.drive_time(80, 0, 3000)
misty.drive_arc(180, 0.5, 4000)

Challenge 5: Plan a path

Misty's has many more driving options than just drive_time and drive_arc. This range of driving function allow to create path plans for Misty. In later lessons you will also learn how to use AR tags and QR code to help Misty navigate around your space.

Let's start by creating a simple path plan that uses misty.drive_track and misty.drive_heading.In this case Misty will go backwards for two seconds then will turn left, she'll move just her left track for one second, forward for one meter, and then drive like there was a curve for five seconds more.

When you want Misty to stop driving or moving between different stages of your path plan, you will need to use the misty.stop function which will stop all of her motors.

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

misty.drive(linearVelocity= -70, angularVelocity= 0)
time.sleep(2)
misty.stop()
misty.drive_arc(90, 0, 3000)
misty.drive_track(leftTrackSpeed= 100,rightTrackSpeed= 0)
time.sleep(1)
misty.stop()
misty.drive_heading(heading= 0,distance= 1,timeMs= 4000)
misty.drive_time(20, 60, 5000)

Challenge 6: Returning to the red carpet

Remember when Misty went out on a red carpet in the Misty Blockly Lessons? Let's try building that sequence again using all the Misty API's you've learned so far. You can use the following example for inspiration.

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

misty.drive(70, 0)
misty.move_head(0, 0, 20)
misty.move_arms(70, -50)
time.sleep(0.4) 
misty.move_arms(70, 0)
time.sleep(0.4)
misty.move_arms(70, -50)
time.sleep(0.4)
misty.move_arms(70, 0)
misty.move_head(0, 0, 0)
time.sleep(0.5)
misty.stop()

Up Next 👇

🤖Lesson 2 : Build a character

Last updated