Introduction to Scribbler Robots
Robots are one of those things we all think we know something about but that are surprisingly difficult to define. Broadly, a robot is a program that runs automatically without a human needing to interfere. Robots have logic of varying complexity that allow it to react to different situations on its own. Given this definition, a robot doesn’t technically need to be a physical machine like the Wall-Es and Transformers found in movies. One type of robot all of us depend on without necessarily realizing it is a webcrawler which indexes web content automatically to make search engines like Google possible.
For the next few days, we’ll be learning to program the Scribbler robots. These are simple little “tank” robots that have a number of sensors including a color camera, infrared sensors and light sensors. Our computers will connect to them using bluetooth which is short-range and reasonably fast.
I HIGHLY ENCOURAGE YOU TO CHECK OUT SAMPLE PROGRAMS!
In order to start using your robot, you have to make sure that your bluetooth dongle is connected to the computer.
You will need to click on the desktop icon that says ‘Python for ROBOTS.’
Once IDLE loads up, type in these commands:
from myro import * init("com3") # or whatever com port your bot has
Unfortunately, the com port seems to change all the darn time. In order to find it for your particular connection, you will need to right-click on the bluetooth icon in the system tray then select ‘Add bluetooth device.’ Find the serial number of your robot, click next, then you should use whatever outgoing com port is displayed in your call on the init function.
Your screen should look like the following:
You should get a popup asking you to accept the bluetooth communication. Click on it:
You should then be asked for a bluetooth passkey. Type in 1234 as follows:
To personalize your robot, you can give it a name. For example, to name a robot “Terminator”
You are now ready to send commands to your robot! For today, I want us to make sure there are no technical hurdles and then experiment with some of the basic commands. To start, try typing in joyStick() and use the mouse to direct your robot around the room. How far can it get before it is out of range? Make sure you’re doing this on the floor so your bot doesn’t fall off the table!!
Your robot can produce songs! This is going to get pretty annoying, but it’s in the name of learning, right? Each group is going to write a robot song. The basic idea is that you will be using a beep command. Try this:
>>> beep(1, 440)
You should have heard a tone play for 1 second at 440Hz. Hz? Hertz is a mesure of frequency. beep is a function that creates waves at specified frequencies. It turns out that 440 is the frequency of an A. Try creating a beep at a higher frequency:
>>> beep(1, 800)
It sounds higher-pitched, right? The human ear can hear roughly between 20 Hz – 20 kHz. Can you hear a beep(1, 20000)? What about beep(1, 20)?
In order to create your song, you will create a function. For example, I will call mine mySong. Here is what my code looks like:
I encourage you to download robotsong.py (just click on the link at left or on the calendar) and play it yourself. To play the song, go to ‘Run Module’ as follows:
When you are ready to write your own robot song, go to ‘File > New Window’ and get a new program file. Save it using a descriptive name of your choice and make sure it has .py at the end!
Once you’ve written your song, explore the following functions and complete the questions at the end. Keep in mind that all the Python you already know still applies!
Pops up a window that allows you to move the robot using a mouse-driven joystick.
Move backwards at SPEED (value in the range -1.0…1.0) for a time given in SECONDS, then stop.
Move forward at SPEED (value in the range -1.0…1.0) for a time given in seconds, then stop.
Stops the robot.
Turn left at SPEED (value in the range -1.0..1.0) for a time given in seconds, then stops.
Turn right at SPEED (value in the range -1.0..1.0) for a time given in seconds, then stops.
Pause for the given amount of TIME seconds. TIME can be a decimal number.
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.
Your robot has sensors that senses different aspects of its environment. The image below shows two types of sensors that your robot has – proximity and light sensors. Notice that they’re on the opposite side of the robot as the camera so you will have to drive backward to use them!
The following is a list of external stimuli that the robot can sense and a description of how the robot senses them.
- Light: There are three light sensors on your robot. These are located in the three holes on the front of your robot. These sensors can detect the levels of brightness (or darkness). Your robot can use these sensors to detect variations in ambient light in a room.
- Proximity: At the front of the robot you will see two tiny lamps on each side of the robot. These are IR emitters. If the light that is emitted by these sensors get reflected by the presence of an obstacle, then the light will bounce back towards the robot and is captured by the IR sensor that is present in the tiny notch in the middle of the two IR emmiters (see figure above).
- Line: If you look at the front portion of the Scribbler’s chassis (away from the third wheel), you will notice two pairs of tiny holes. These are also IR emitters and receivers and can be used to detect lines on the floor. THIS SENSOR IS REALLY FLAKY! USE AT YOUR OWN RISK.
Once you are familiar with your robot’s sensors, you can use them to create interesting creature-like behaviors for your Scribbler.
Enter the following command in the Python Shell:
Scribbler Sensor Values:
- Light: The values being reported by the three light sensors are: 135 (left), 3716 (center), and 75 (right). In general, the lower the values, the brighter the light being sensed. This snapshot was taken when the center light sensor was covered by a finger.
- Line: In the presence of a line under the Scribbler (the line has to be aligned with the length of the robot) the Scribbler can detect a bright edge against a dark edge (that forms a line). The display shows the values of left and right sensors (both are 1). If the right sensor is on a dark area of an edge or a line and the left sensor is on a lighter area or edge, the sensor values would be (left=0, right=1). If the edge is reversed, the values would be (left=1, right=0).
- IR: The IR values displayed are also left and right (in the same orientation as the light). Their values will be either 1 or 0. If there is an obstacle detected, the value will be 0. It is 1 otherwise.
Unfortunately, some of the sensors are pretty flaky. Try to get the line sensor to get different values for left and right, for example. It doesn’t really happen, so it’s not very useful!
Obtaining Sensory Information
In addition to using the senses operation, you can use various functions in the Myro library to obtain these sensor values. You can use these functions as tests for loops or conditionals to make your robot react to its environment.
- getLight(<POSITION>): This returns the current value in the <POSITION> light sensor. Your Scribbler has three light sensors (see picture above). <POSITION> can either be ‘left’, ‘center’, ‘right’ or one of the numbers 0, 1, 2. The positions 0, 1, and 2 correspond to the left, center, and right sensors. For example, keep your robot in the same position and try the following:
>>> getLight('left') >>> getLight(0) >>> getLight('center') >>> getLight(1) >>> getLight('right') >>> getLight(2)
- getIR(<POSITION>): This returns the values of the IR sensors. <POSITION> can be 0 or ‘left’ and 1 or ‘right’. Try the following:
>>> getIR('left') >>> getIR('right') >>> getIR(0) >>> getIR(1)
The Fluke dongle has an additional set of obstacle sensors on it. These are also IR sensors but behave very differently in terms of the kinds of values they report. The following functions are available to obtain values of the obstacle IR sensors:
- getObstacle() Returns a list containing the two values of all IR sensors
- getObstacle(<POSITION>) Returns the current value in the <POSITION> IR sensor. <POSITION> can either be one of ‘left’, ‘center’, or ‘right’ or one of the
numbers 0, 1, or 2. The positions 0, 1, and 2 correspond to the left, center, and
right sensors. Try the following:
>>> getObstacle() >>> getObstacle('center') >>> getObstacle(1) >>> getObstacle('right') >>> getObstacle(2) >>> getObstacle('left') >>> getObstacle(0)
You can use any of the loop techniques we’ve discussed so far. Using them with sensor values is particularly powerful. For example, you may want your robot to keep going forward while the light sensor gives values below a particular threshhold.
One very useful Scribbler-specific way of looping allows your robot to do things for a certain amount of time. To do so, you’ll want to use the Scribbler timeRemaining(SECONDS) function. For example:
# make the 'bot print a message and go forward for 10 seconds. while(timeRemaining(10)): print "Gogogo!" forward(1) # this makes your 'bot go forward at full speed forever! Until the stop() command is called. stop()
On to ACTIVITIES!