CS 110 Lab Project 10
Learning the Scribbler Robot
In this lab, we'll learn to combine Python programming with the control of the Scribbler robot. Once we learn how to command the robot and to retrieve sensor information from the robot, we'll set a few problem-solving goals.
Background: Setting up the Environment
We first need to familiarize ourselves with the tools we will be using, along with the basic operation of establishing a connection (over Bluetooth) to the robot. The instructions differ slightly between the desktop machines in the back rows of Olin 217 and the laptops on the laptop cart. We will work in teams of two on this assignment, so the first step is to find a partner to work with on this lab.
The next step is to get (1) one of the laptops, or to sit at one of the eight desktops on the aisles of the classroom, (2) one of the scribbler robots, and (3) a "dongle", the small circuit board that allows the robot to communicate over bluetooth with the robot. For the initial session, we will use only the laptops. It does not matter which robot you get, but the dongle has to match the laptop/desktop you are using, because we have pre "paired" the dongles with particular computers. The dongles are labeled with both a number and a letter, where the number indicates the laptop it has been paired with, and the letter indicates the desktop it has been paired with.
If you have a computer, a robot, and the computer-matched dongle, we are ready to begin.
- Boot the computer and login. Note that if you are on a laptop, you share the 'Student' account with anyone else who uses the same laptop. If you are on a desktop, you login to your own account. Since we are going to use the Desktop for many of the steps below, we need to keep this in mind. On the laptops, clean up your own Desktop files after you are done with a session, and leave the 'Robot' folder shared with all students using the laptop as you found it.
- If you are on a desktop, and logging in for the first time to do robot work, you should copy the 'Robot' folder to your Desktop. Use the Finder and select 'Go' from the menu bar, and 'Computer' from the resultant menu. In the Finder window, navigate to /Userspace/Temp. In that folder is a folder named 'Robot'. Using a Drag operation, and while holding down the option key on your keyboard, drag the Robot folder to the Desktop and then close the Finder window.
- Whether on a laptop or on a desktop, you should now see the Robot folder on your Desktop. Double click the folder to see its contents. Now double click on the Terminal icon to bring up the command line interface to the operating system.
- You should now, in the Terminal window, be able to start an instance of the Python shell, within which we will start issuing Python statements for working with the robot. At the Terminal's command line prompt, ending with a $, start by issuing the following command to set the Terminal's "current location" to your Desktop folder:
$ cd ~/Desktop
- Invoke the Python interactive shell by issuing the following command:
$ py2
You should see the same Python 2.7.3 shell as we often see in the Python Shell window of the Wing IDE. - At the Python >>> prompt, type:
>>> from myro import *
If you get the message that myro version 2.9.1 has loaded successfully, then you have accessed the correct version of Python that has the robot library module installed with it. -
To establish communication and initialize the robot, first, with the robot off, plug in the dongle to the top of the robot. It should only fit in the 9-pin port one way. Then turn the robot on (the black toggle switch on top from the 0 position to the 1 position). You should hear a short series of beeps from the robot. Then enter the following:
>>> init('/dev/tty.scribbler')
If successful, this should also result in another series of beeps. Note that this function call step is the one most likely to encounter problems. If we get a "serial error", the reasons could range from mismatch of computer and dongle, mistype of the string argument in the function call, a bluetooth pairing problem, or a bluetooth passcode problem. If you get an error, type control-C and either try again or enlist your instructor's help.
Interactive Session with the Scribbler
In this section, we suggest a number of the possible ways of interacting with the robot. All such interactions occur through function calls in Python, and some such calls are value-returning and others are "commands" to the robot. The primary goal is to understand how, from a program, you will be able to command the robot to go through a set of motions. Then next week, you will use the sensors on the robot to return information to your Python program and enable much more sophisticated robot behaviors (useful to your next program). So I encourage you to experiment and try as many commands and their variations as you can. In the 'Robot' folder on the desktop, there is a web link to a complete set of the commands available on the scribbler and dongle.
- Type name = getName() at the command prompt. Then print the variable name. What do you get? Now type setName('Tom') (or your own name!). Then repeat the getName() call. Turn the robot off, and then back on. When the power was cycled, you will have lost the bluetooth connection, so repeat the init() command from above. Did the robot 'remember' the new name you gave it?
- Next try beep(1, 440). Continue experimenting by varying the value of the first argument. When you understand the purpose of that argument, vary the value of the second argument. Form a hypothesis as to the significance of the second argument. Verify with the documentation.
- Type forward(1,1) at the command prompt. Next try backward(1, 0.5), and then forward(0.2,3). Between your observations and experimenting with the arguments, can you figure out what the two arguments mean? Verify your hypothesis by looking at the web page documentation.
- Look up the documentation for turnRight() and turnLeft() and the meaning of their arguments. Based on what you learn from the documentation, experiment with the robot.
- With what you have learned in the last couple of steps, try and issue a sequence of commands to make the robot traverse a square on the desk. Think about how this is the same as well as how this is different from accomplishing the same goal with our Turtle graphics.
- Practice other types of motion by using the functions translate(), rotate(), motors(), and stop(). If you issue a command that does not also terminate the motion of the robot, make sure you have plenty of clearance and be ready to issue the stop() function.
- Postion the robot on the table with the dongle facing toward you, and then issue the following two function all invocations:
>>> pic = takePicture() >>> show(pic)
What do you see? Repeat the above commands until you get (relatively) clear images of the face of both you and your partner. - If time remains, start to learn about the sensors that are available on the Scribbler robot and on its dongle. Do this by looking at the reference page and issuing various sensor-retrieving commands.
- Exit the python shell by typing exit() at the command prompt. Alternatively, you should be able to type control-D. This should return you to the Terminal's command prompt ... the one ending with a $.
Writing Scribbler Programs
When we want to save a set of commands as a Python program, we need to do two things. First, we need to use an editor to create a text file and to put the program text into that file. The second step is to tell the Python interpreter to execute the save python script file.
In the past we have used IDE's for text file creation, namely Wing and JES. Now we will use a simple text editor, called TextWrangler. We have placed an alias link to TextWrangler in the Robot folder that should now reside on your Desktop. Double click on the icon to launch the application. The application should not be too dissimilar from Word or other document creating applications. We will begin by putting together a simple program. Start by doing a 'Save As ...' and saving the new blank document to your Desktop with a filename that includes the .py extension. That way, TextWrangler will do colored syntax highlighting as you work.
# Thomas C. Bressoud and Partner # Mini Lab 10: Working with the Scribbler Robots # November 2, 2012 from myro import * def main(): init('/dev/tty.scribbler') beep(1,587) timestr = ask('How long to run on a side?') robotSquare(int(timestr)) # robotSquare() is a function to use the currently initialized Scribber robot # to draw a square. The parameter t specifies how long (in seconds of time) # each side of the square should be. def robotSquare(t): forwardSpeed = .8 turnSpeed = xx turnTime = yy forward(forwardSpeed, t) turnLeft(turnSpeed, turnTime) # etc.; perhaps a good place for a loop main()The second step, executing the written program by the Python interpreter, is accomplished back in the Terminal window. Assuming your current location in the Terminal window is your Desktop, which it should be if you have the same Terminal window from the start of this lab, then you should be able to execute a program named drawSquare.py by issuing the following command at the Terminal command prompt:
$ py2 drawSquare.pySo py2 is the command, and the filename, drawSquare.py is called the command line argument, specifying to the Python interpreter exactly what file to execute. If the interpreter claims the file is not found, then check your spelling and capitalization. If you have re-opened the Terminal window, you may need to issue the cd ~/Desktop command to set your location in the Terminal window back to the Desktop.
Programs to turn in
- Star.py: Write a full program with a function named star() that commands the robot to draw a five pointed star. Begin with a sequence of commands with hard coded values for the distances and the turn values. For full credit, paramaterize your function so that star takes a single parameter, length, whose value is the linear length of each of the line segments in the star.
- Maze.py: Write a program to traverse the maze taped out on the back of the twister mat. Write a function called maze() that, given the robot is placed at the correct starting position, will use "dead reckoning" to move the robot from the starting position to the ending position. Full credit will be given for solutions that use additional defined functions for moving forward a designated linear distance and turning a designated degree amount.
- Dance.py: Choreograph a simple dance routine for your robot and define functions to carry it out. Make sure you divide the tasks into re-usable moves and sounds and as much as possible parameterize the moves so they can be used in customized ways in different steps. Use the building block idea to build more and more complex series of dance moves. Make sure the routine lasts for at least several seconds and it includes at least two repetitions of the entire sequence. The more entertaining and involved the dance, the better the subjective part of the grade.