Robot motion

posted by: Ms. Martin 8 December 2009 No Comment

Think back to our first days with Scratch.  We drew some geometric shapes to get familiar with the environment and we’re going to do the same with our robots!

In Python, commands given are called functions.  Yesterday, you explored some basic functions including beep.  Functions can take in values, called parameters.  They’re a lot like variables in Scratch.  beep takes two parameters: first the length of the beep, then its frequency.  For example:

>>> beep(1, 440)

told the robot to spend one second making a middle A at 440Hz.

Motion functions are:

>>> forward(speed, time)
>>> forward(speed) # goes forever until you call stop()!
>>> backward(speed, time)
>>> backward(speed) # goes forever until you call stop()!
>>> turnLeft(speed, time)
>>> turnRight(speed, time)
>>> stop()

Try, for example, going forward(1, 1).  It should go forward 1sec at full speed.  Change the first number to .5 to make it shorter.  When you change those numbers, you’re changing parameters to the forward function.  When you wrote your song yesterday, you probably put it in a function called mySong() because that’s what my example showed.  You could have called it anything, though!  awesomeSong(), playThat()… anything, as long as it doesn’t start with a number and doesn’t have spaces in it.  Also note that it must have parentheses.

Functions and Parameters

Why have parentheses?  In case we need parameters.  Let’s look at a function that takes parameters.  Copy this into a file and give it a descriptive name that ends with .py:

def scale(start):
    beep(1, start)
    beep(1, start + 10)
    beep(1, start + 20)
    beep(1, start + 30)
    beep(1, start + 40)
scale(440)

Notice that if you take out the line scale(440), your program doesn’t do anything anymore!  That line is the function call, where you actually tell Python to run a command.  Try changing the parameter value or adding more calls.

The line starting with def tells Python you’re going to write a function definition.  Then you give it its name and any parameters it may need in parentheses.  If you don’t need parameters, you still must have parentheses!  Notice that everything that’s part of the function definition is indented in.  That’s a Python rule.

Drawing shapes

You should be ready to write a square function that takes one parameter: size.  You’ll have to go forward size seconds, turn 90 degrees and repeat four times.  In Python, repeating is done using a for loop.  You may have recognized that our scale function in the previous section was pretty repetitive.  It quickly becomes unwieldy if we want to add more notes.  Here is an improved version:

def loop_scale(start):
    for i in range(10):
        beep(1, start + i * 10)
        print start + i * 10
loop_scale(440)

In order to change how many times the contents of the loop repeat, all you have to do is change the 10.  Any statement that is indented inside the ‘for i in range(x)’ part gets repeated that many times.

Write a program that will create a scalable square using a loop.

Calibration

You’ve probably noticed that your robot is not going straight at all.  That’s because one motor is a little stronger than the other.  We can compensate for that using calibration.  Run the calibrate() function at the >>>. 

The basic idea in using this calibrate program is that you will just try to drive your robot in a straight line; the computer will automatically set up “tweaked” motor values for the robot, which you can view on the interface. You’ll use the calibrate interface, which is similar to the joyStick interface, to drive the robot. If you can’t look at both your computer and your robot at the same time (which is often the case if you don’t have a laptop), the process will require a little more finesse.

The function is set up to calibrate your robot at four speeds: 1.0, 0.5, -0.5, and -1.0. On the interface, you should see four horizontal bars corresponding to these four speeds, and a “Turn Left” on the left side of the screen and a “Turn Right” analogously placed. When you click inside one of the bars, the robot will move at the given speed, but with a higher left or right component depending on where in the bar you click. If you click in the center of the bar, the robot will move in what it thinks is a straight direction, but which may tend towards either the left or the right (if it goes perfectly straight, there’s no need to calibrate for that speed!). If you click on the left part of the bar, the robot will still move at the given speed, but with a higher left component. The further left from the center you click, the higher the left component will be. The same idea follows for the right side of the bar.

Once your robot is calibrated, try your square function again.  Better?

Flashing lights

There are a few functions that allow us to control the various LEDs on the robot.  Experiment with these:

setLED(position, value)

>>> setLED(“left”, “on”)
>>> setLED(“right”, “off”)

setLEDFront(value): turn on the led on the front of the Fluke (0 for off and or 1 for on)

setLEDBack(value): turn on the LED on the back of the Fluke. The brightness of this LED is configurable between 0-1.

Dancing robot

Write a dance program for your robot in a file called dance.py.  You will be turning this in.  Make sure you use multiple functions as well as parameters to make your program extensible and reusable.  Your whole dance routine should be at least 20sec long and include beeps and light flashes.

Light sensor

Your robot is equipped with multiple sensors including 3 light sensors on the side opposite from the green add-on board.  Your next task will be to write a program that does three different things of your choice when each of the 3 sensors are covered.  Try to be creative!  Feel free to reuse some older code — for example, covering the center sensor could make your robot dance.

When learning to program, there’s nothing quite like looking at examples.  I’ve heavily commented the program lightsense.py and I’d like you to download it and read through it.  You’ll see that it’s doing what I’m asking of you — it does three things based on which sensor is covered.  We’ll talk about if statements and while loops in more depth, but for now see if you can make sense of them by changing the program and seeing what happens.

Once you’re ready, open a new file and write a program that does three different things depending on which light sensor is covered.  Base it off my example, but make the cases more interesting!  Try to use if statements and/or while loops in the cases to see if you understand them.  You will turn this in.

Once you’re ready to turn in your robot dance and your light sensor decision maker, play around with the commands you’ve learned so far and see what interesting things you can come up with!

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>