Computer Science 101: Lab #7

This lab has some pretty cool projects in it, especially the behavior-based robotics project that starts off Chapter 7. Behavior-based robotics was a radical movement in artificial intelligence and robotics back in the 1990's, which led to some pretty amazing results and even a popular documentary film. So to get started, copy-and-paste the code on pages 134-135 of the textbook, making sure to indent and space it out nicely and eliminate the bogus text like page numbers that you may have pasted in.

A behavior-based robot

Running the author's behavior-based program in one of the corrals that we'll set up, you'll soon see that Scribby mainly runs into walls and grinds against them uselessly. This is because the author's code is pretty far from anything that actually works. We're going to spend some time improving it to the point where we get some real results.

First, as always, style! The author has some constants at the top of the code whose names should be in upper-case. So fix that first. This won't make the code run any better, but it'll make it obvious what the constants are that we need to fiddle with.

Now, in any complicated system, it's impossible to combine components to get something useful if the components themselves don't work. So we're going to test and refine each of the behaviors in turn. Let's start with seekLight(). Comment-out the two lines in the while loop in main(), and replace them with a line that prints out the value returned by seekLight. Like me, you may find the author's light threshold absurdly low – so seekLight() always returns False, even when I put Scribby right in front of one of the desk lamps we're using. So what's a good threshold value? As usual when searching for a value between two extremes, we can use binary search (keep an upper, lower and mean/middle value, and stop when the middle value is good enough). In our case, a good value is one that makes a distinction between light and dark. It might also help to print out the values returned from the sensors.

Once you've got Scribby to distinguish between light and dark, remove your print() statement from the loop in main(), and un-comment the original two lines. To test the light-seeking behavior, we'll want to let Scribby cruise around looking for light, but not worry about avoding obstacles. So in the list of behaviors above main(), remove the avoid. (Safe programming practice dictates that you make commented backup copy of this line first!) Shine the desklamp over a patch of the floor, have Scribby face the patch from a few feet away, and run the program, and run the program. When I did this I found that my robot moved away from the light, like that kid in Poltergeist or something. By changing the signs on TURN_SPEED in the seekLight() function, I was able to get Scribby to go into the light.

So the one remaining behavior to deal with is obstacle avoidance. If you consider that Scribby's has “eyes on the back of his head“ (or, at least IR sensors there), you can see why the author's code would fail to detect obstacles in front. Fortunately, the Fluke faces forward and has its own obstacle detectors, accessible through the getObstacle() function. I'll leave it to you to figure out how to convert these into 0,1 values to in the avoid function. You also might want to implement a stall behavior to deal with the times when the robot gets jammed up against a wall or corner.

A project this complicated can be frustrating. Do the best you can in the first 90 minutes of lab, and don't worry if your robot can perform two of the three behaviors. Make sure to take a photo of your corral before you move on to the next part.

Car payments

Once again the author tells you to run your program in Calico (make it neat!), and once again there's an error. Spot the error and fix it to figure out your car payments.

Rock, paper, scissors

This one's pretty straightforward (no typos): copy/paste/cleanup the code, run it, and do the looping version that the author suggests. For extra credit, create a program that offers the human the choice of a random strategy. Keep score over a sequence of games, and show that the outcome is no different between a random strategy and a constant strategy.

Turnin

Turn in a copy of each program, as well as your individual writeups summarizing what you found in each program.