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

In these last two labs, we review the material thus far. You can either choose to do one or more of the exercises below that looko interesting to you, or create your own functions with the Python & myro functions you've learned. Remember that you now have experience commanding the robot to do a variety of activities: move around, draw, take photos, take video, and play beeps or 'songs'. At the end of the lab, there's a "cheat sheet" of the myro functions & Python syntax we've covered.
Setup
  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")
Exercises
A1) Write a function to print this pattern using loops:

      
+       
++    
+++ 
++++
+++ 
++    
+      

A2) Write a function triangle(n, symbol)

that prints out a triangle shape of symbol, where the longest part of the triangle has n symbols. e.g. triangle(4, '%') would print
%       
%%
%%%    
%%%%
%%%  
%%   
%    

and triangle(2, '@') would print
@
@@
@ 

B1) Create a function that, given an array of strings (e.g., ['Janet', 'Henry', 'Sam', 'John']) prints out only the names that come before 'L' alphabetically.

So, if

A = ['Janet', 'Henry', 'Sam', 'John']

weird-function(A) prints out 'Janet', 'Henry', and 'John', but not 'Sam'. (HINT: experiment with the <=, >=, <, and > signs in the terminal, e.g. "Kate" < "June")
B2) Adapt this function so that weird-function(array, 'key') prints out all names before "key" alphabetically

e.g., weird-function(A, 'I') prints out 'Henry', weird-function(A, 'Je') prints out 'Henry' and 'Janet', etc.
B3) Adapt this function so that weird-function(array, 'key-pre', 'key-post') prints out all names alphabetically between key-pre and key-post
C) Write a function draw-square(number, size)

So that the robot will draw 'number' squares in a row, with side-length 'size'
D) Create a function so that the robot draws based on user input using askQuestion()

For example, you might ask the user to choose between 'draw a square', 'go forward', 'turn 30 degrees left', 'turn 60 degrees left', or some other combination of choices. Once the user selects an option, the robot should do that action, and then prompt the user to choose again (as in Lab 5 with our 'spy' robot.)
E)There are more myro commands that we haven't covered in this course.

If you are interested in learning about them, you might want to work through some of the exercises in http://cs.brynmawr.edu/~dkumar/Myro/Text/Fall2011PS2/PDF/LCR2011.pdf

The text is written for beginners and is very informal. Chapter 6 discusses the light and object detection sensors for your robot, which you can use to program the robot to, for example, seek out light, or navigate a room without bumping into anything. Chapter 8 discusses creating simple graphical drawings.
Python
For Loops
Syntax:
for var in array:
   do something

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

While Loops
Syntax:
while (test):
  do something

Example:
i = 1
while (i < 100):
   print ("%d, ") % i

If statements

Syntax:

if (test):
  do something
elif (test):
  do something else
elif (test):
  do something else
...
else:
  do this

Example (Lab 5):

while 'true':
     q = askQuestion ("Where to, master?", ["Forward", "Left", "Right", "End"])
     if q == "Left":
        aboutLeft()
     elif q == "Right":
        aboutRight()
     elif q == "End":
        break
     else:
        forward(1,1)

Arrays
Arrays can be used to store a list of objects. You can create an array like this:

array1 = ['cat', 'dog', 'catdog']

Get a specific value in an array with it's index:  array1[1], array1[2], etc
Get the last thing in the array: array.pop()
Or add something to an array: array1.append('rabbit')

We use arrays in for-loops. For example, we can write

for animal in array1:
    print animal

to print every string in array1.

Another example:

for num in [1, 2, 3, 4, 5]:
    print "%d" % (num * num)

Relational Tests 
<, <=, >, >=, == (equal), != (not equal) 

These are relational operations in Python. They can be used to compare values.   
e.g., (5 < 3) returns 'false'; ("Kate" == "Kate") returns 'true'

Myro

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.

rotate(SPEED, TIME)

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].

KateIngersoll - 2013-04-23
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