Robot Python so far

posted by: Ms. Martin 13 December 2009 No Comment

Movement

joyStick()
Pops up a window that allows you to move the robot using a mouse-driven joystick.

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.

stop()
Stops the robot.

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.

wait(TIME)
Pause for the given amount of TIME seconds. TIME can be a decimal number.

General Python

# Comment text
Any text preceded by a # character is ignored by Python.  This is really helpful for giving yourself and others who read your programs some background information.

def <FUNCTION NAME>(<PARAMETERS>):
    <SOMETHING>
    ...
    <SOMETHING>

Defines a new function named <FUNCTION NAME>. A function name should always begin with a letter and can be followed by any sequence of letters, numbers, or underscores (_), and not contain any spaces. Try to choose names that appropriately describe the function being defined.

print(TEXT)
Display TEXT on the prompt

raw_input(TEXT)
Reads text from the user.  Results must be saved in a variable to be used later as in:

name = raw_input("What is your name? ")
print "Hello", name

Repetition

for <variable> in <sequence>:
    <do something>
    <do something>
    ...

while timeRemaining(<seconds>):
    <do something>
    <do something>
    ...

These are different ways of doing repetition in Python. The first version will assign <variable> successive values in <sequence> and carry out the body once for each such value. The second version will carry out the body for <seconds> amount of time. timeRemaining is a Myro function.

Random

Whenever you want to use random numbers in your programs, you have to write the following at the top:

from random import *

random()
Pick a random value between 0.0 and 1.0

randint(MIN, MAX)
Pick a random whole number between MIN and MAX

Pictures

pic = takePicture()
show(pic)

The first line commands the robot to take a picture and save it in a variable called pic. The second line displays this picture on the screen.

grayPic = takePicture("gray")
show(grayPic)

This example is very similar to the previous but the picture is black and white.  Taking a black and white picture is less time-demanding.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Leave a comment

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>