Error: no such theme PatternSkinTheme
You are here: Foswiki>Main Web>CSAndRobotsExco>ExcoLab5 (2013-04-24, KateIngersoll)EditAttach

Set up

  1. Open the terminal
  2. Use cd and pwd to move into your exco directory
  3. In your terminal, open the python shell by typing python
  4. Turn on your robot
  5. Import the myro library in the python shell with from myro import *
  6. Connect to the robot with the command init("/dev/rfcomm0")

Taking Pictures

Your Scribbler robot can take photos with the command takePicture(). Python stores this data as a 'picture instance'. You can convert a 'picute instance' into a graphical display with the command show(), or save the instance as a .jpg with the command savePicture(). To get practice using these commands, experiment with the following code in your Python terminal.
pic = takePicture()
pic
show(pic)

grey_pic = takePicture("gray")
show(gray_pic)

show(takePicture())

savePicture(pic, "test_picture.jpg") 

Pictures will be saved to your current folder (for most people, this will be your Desktop, your homefolder, or a subfolder for this exco.) Experiment taking your own photos & saving them.

Video

We can also use savePicture() to do something more interesting-- store a list of pictures as an animated gif. We will experiment with code that does this in this next section.
Test- Put the following code into your terminal. See if you can figure out what it does.

Forward = []
for i in range(10):
    forward(1,1)
    p = takePicture()
    Forward.append(p)
    print "In loop %d" % i
    print Forward

savePicture(Forward, "forward.gif")

Find forward.gif in your current folder. Right-click on it, and select "open with Iceweasel" to see the gif in action.
Explanation

# We start with an empty list, and we give it the name 'Forward'
Forward = []

# Then, we loop through this list of instructions 10 times
for i in range(10):

   # move the robot forward at speed 1 for 1 sec.
    forward(1,1)
   
   # take a picture, and give it the name 'p'
    p = takePicture()
   
   # add 'p' to the list called 'Forward'
    Forward.append(p)

   #print the loop number to the screen
    print "In loop %d" % i
   
   #print Forward to the screen
    print Forward
   
#The two print lines are not needed, but may be helpful to help 'see' what is happening to 'Forward' in the loop.

# Finally, we save the list of photos as a gif called 'forward.gif'
savePicture(Forward, "forward.gif")

Another Example

R = []
for i in range(100):
      turnLeft(.01,.01)
      R.append(takePicture())
savePicture(R, "test4.gif")

Observe that this video is much more fluid & continuous, because we used a smaller 'step' between each image.
Lab

Create your own video & share with the class. Be creative-- the video can capture the movement of the robot as it travels the classroom, or capture movement of a person or object in front of the stationary robot. You can experiment with stop-motion images or drawings (i.e., make a flip book, & show each page to the robot), or capture the process of drawing an image on the whiteboard.

askQuestion() and if-statements

if-statements- Python if-statements are a way to execute different commands under different circumstances. The basic syntax of an if-statement looks like this:

if <condition-1>:
  <this>
elif <condition-2>:
  <that>
elif <condition-3>:
  <something else>
...
else:
  <other>
 

The Python interpreter will check the first condition. If it is true, the commands "this" will be run, and then the if-statement will be exited. If the first condition fails, the next one will be checked. Once the first passing condition is found, we exit the if statement. If all conditions fail, we execute the commands in the else clause.
# A simple example of if. Predict the output of this code, and then c/p into your terminal to test your theory.

if 2 < 1 :
   print "Math is broken forever"
elif 1 < 2 :
   print "duh."
   print "that's obvious"
elif 1 <3 :
   print "do I print?"
else :
   print "I'm just horribly confused."

(Note that case one is false, so we skip it, case two is true, so we do all the commands in that section, case three is also true, but we don't do the commands in that section because we've already left the if-statement.)
askQuestion()

askQuestion() is a myro command that lets the user interact with the robot. askQuestion can take one or two parameters. Experiment with the following code to learn how askQuestion() works.

askQuestion("Is it raining right now?")

askQuestion("Am I human?")

askQuestion("What's your favorite color?" ["Red", "Green", "Blue", "Yellow", "Orange", "Purple"])

askQuestion("What is the meaning of life?" ["I don't know", "42", "Not something to be explained in a CS exco"])

A more advanced example. Read the code & comments carefully, run the code to see how it works, and ask any questions you have.

#aboutLeft & aboutRight turn robot at ~90 degree angles

def aboutLeft():
    turnLeft(.5, 1.55)

def aboutRight():
    turnRight(.5, 1.55)

#loop through these commands forever
while 'true':
     # ask for user input, and store result in 'q'
     q = askQuestion ("Where to, master?", ["Forward", "Left", "Right", "End"])

     # if 'q' is left, turn left (== is a test to see if two things are equal)
     if q == "Left":
        aboutLeft()

     #if 'q' is right, turn right
     elif q == "Right":
        aboutRight()

     # if 'q' is end, use 'break' command to exit the while loop
     elif q == "End":
        break

     # if 'q' is anything else, go forward
     else:
        forward(1,1)

     # ask for user input, and store result in 's'
     s = askQuestion("Spy?")

     # if 's' is "True", then take & show a picture.
     if s: 
       show(takePicture())

Free Exploration

Using the new python & myro commands you've learned, write your own interactive commands. You may want to use this list of myro commands as a reference when writing your programs:

beep(<TIME>, <FREQUENCY>)
Makes the robot beep for <TIME> seconds at frequency specified by
<FREQUENCY>.

backward(SPEED,SECONDS)
Move backwards at SPEED (value in the range -1.0...1.0) for a time given in
SECONDS, then stop.

forward(SPEED,TIME)
Move forward at SPEED (value in the range -1.0...1.0) for a time given in
seconds, then stop.

turnLeft(SPEED,SECONDS)
Turn left at SPEED (value in the range -1.0..1.0) for a time given in seconds,
then stops.

turnRight(SPEED,SECONDS)
Turn right at SPEED (value in the range -1.0..1.0) for a time given in seconds,
then stops.

randomNumber()
Returns a random number in the range 0.0 and 1.0. This is an alternative Myro 
function that works just like the random function from the Python random library 
(see below). 

askQuestion(MESSAGE-STRING)
A dialog window with MESSAGE-STRING is displayed with choices: 'Yes' and 
'No'. Returns 'Yes' or 'No' depending on what the user selects. 
 
askQuestion(MESSAGE-STRING, LIST-OF-OPTIONS)
A dialog window with MESSAGE-STRING is displayed with choices indicated in 
LIST-OF-OPTIONS. Returns option string depending on what the user selects. 

currentTime()
The current time, in seconds from an arbitrary starting point in time, many years 
ago. 

takePicture()

TakePicture(“gray”)
Takes a picture and returns a picture object. When no parameters are
specified, the picture is in color.

show(<picture>)
Displays the picture in a window. You can click the left mouse anywhere in
the window to display the (x, y) and (r, g, b) values of the point in the
window’s status bar.

savePicture(<picture>, <file>)
savePicture([<picture1>, <picture2>, ...], <file>)
Saves the picture in the file specified. The extension of the file should be
“.gif” or “.jpg”. I


range(10)
Generates a sequence, a list, of numbers from 0..9. There are other, more
general, versions of this function. These are shown below.

range(n1, n2)
Generates a list of numbers starting from n1...(n2-1). For example,
range(5, 10) will generate the list of numbers [5, 6, 7, 8, 9].

range(n1, n2, step)
Generates a list of numbers starting from n1...(n2-1) in steps of step. For
example, range(5, 10, 2) will generate the list of numbers [5, 7, 9].

<, <=, >, >=, == (equal), != (not equal)
These are relational operations in Python. They can be used to compare values. 

Topic revision: r2 - 2013-04-24, KateIngersoll
 

This site is powered by FoswikiCopyright © by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding Foswiki? Send feedback