Python Robots

CSC 105 - The Digital Age - Weinman



Summary:
In this laboratory, you begin the journey and joy of learning to write computer programs in the Python programming language.

A. Getting Started

This part is not hard, but we will probably do it all together so everyone keeps up to speed. We will launch our environment, connect to the robot, and give it some simple commands.
  1. Open a terminal window and launch the Calico program by typing (or pasting) the following command
    /opt/Calico/calico &
  2. In the Calico window that opens, click the Shell tab. You should see a blank white window with a python> prompt and a blinking cursor. 1
  3. Next, turn your robot on. It should reply: beep-ity-boop.
  4. In your Shell pane, type the command to import all the subroutines for talking to the robot into the current Calico environment.
    from Myro import *
    and press enter. 2
  5. You should see the interpreted command below in the Output pane. Your command is echoed followed by any resulting value. In this case, the command only has invisible side effects. You will generally need to do this import step each time you launch Calico.
  6. Next, initiate a connection to the robot from your workstation by typing the following command into the Shell pane and pressing enter:
    initialize("/dev/rfcomm0")
    This time you should not only see that command echoed in the Output pane, you should shortly see a red light turn on the Scribbler's exposed Fluke circuit board (indicating communication) and more text like the following
    You are using:
      Fluke, version 3.0.9
      Scribbler2, version 1.1.6
    Hello, my name is 'noyce'!
    where, instead of noyce, it should respond with the name of the workstation at which you are sitting.
  7. Now that we have a connection, we should be able to give the robot commands to follow. Try entering the following sequence of commands, one by one, pressing enter after each line and listening for the result.
    beep(1, 523)
    beep(0.5, 659)
    beep(2, 784)

B. Simple Programs

Now that you have gotten Calico running and connected to the robot, let's try doing some modestly more interesting things. You will recall from today's reading that you can set variables names to represent values. We can then use the variable names in general expressions so the exact effects depend on the values held.
  1. Set the variables partner1 and partner2 to hold the names of the two partners. For instance, if the instructor and mentor were working together on this lab we might type the following in the Shell pane.
    partner1 = "Larry"
    partner2 = "Jerod"
  2. Create the following sentence using these variables.
    sentence = partner1 + " and " + partner2 + " control this computer."
    Note that everyone will execute the same command, but will get different results because their variables hold different values (hence the name)-that's a key element of the power in computing.
  3. Now (politely, right?) ask the computer to vocalize this sentence.
    speak(sentence)
    Once again, all are issuing the same command, but with different results.
  4. In addition to variables, subroutines help us by abstracting away details we don't much care about with a simple named helper algorithm. We can use subroutines, often called functions, to get values or behavior we want. Let's specify the name of the robot/workstation pair you are controlling by using the getName() function in your sentence.
    sentence = partner1 + " and " + partner2 + " control " + getName()
    speak(sentence)
    It's ok to let this amazing power go to your head. Just a little. Remember, with great power comes great responsibility.

C. Functions (Subroutines)

We learned that functions make computational problems tractable through abstraction. Your readings gave you both the syntax for writing functions and some examples. Let's begin by trying these out.
  1. Type the following wiggle function into your Shell. Note that Python is very particular about how things are indented; these spaces tell the computer which instructions go with other instructions. Most likely you won't be able to copy and paste. Instead, when you type one line and the next needs to be indented, type tab to increase the indentation after pressing enter.
    def wiggle(numTimes):
        for step in range(0,numTimes):
            turnLeft(1,1)
            turnRight(1,1)
    If all goes well, the code should simply be echoed (but not executed) in your Output pane.
  2. Now let's call your wiggle routine in a short dance. You can probably paste these instructions to your Shell.
    forward(0.5, 2)
    wiggle(2)
    backward(1, 2)
    wiggle(3)
Congratulations! You've written your first short algorithms!!

D. Modules

Our reading reminds us:
As you can imagine, while working with different behaviors for the robot, you are likely to end up with a large collection of new functions. It would make sense then that you do not have to type in the definitions over and over again. Python enables you to define new functions and store them in files in a folder on your computer. Each such file is called a module and can then be easily used over and over again.
Let's try packaging all your previous commands as functions in a module.
  1. Notice the Script pane to the right of the Shell and Output panes. It probably has a tab called "New Python Script". You can enter module commands for later execution in this tab and save them. Add the following two short functions and accompanying commentary (computers don't care, but humans need to know what this is about!) to a Python script in this tab. Note you can press tab to indent. To "un"indent when the computer tries to indent for you, press shift+tab at the beginning of a new line. Again, indentation is critical!
    # File: moves.py
    # Purpose: Useful robot dance moves to demonstrate a module
    # Author: Jerod Weinman
     
    from Myro import *
    initialize("/dev/rfcomm0")
     
    def wiggle(numTimes):
        for step in range(0,numTimes):
            turnLeft(1,1)
            turnRight(1,1)
     
    def dance():
        forward(0.5,2)
        wiggle(2)
        backward(1,2)
        wiggle(3)
  2. Now we want to save all that hard work in typing so we can come back the next day and ask the robot to dance. Save your file to the 105 directory in your home folder by clicking on the disk icon or selecting File>Save or typing Control+s.
  3. We should now be able to run these commands from your Shell. First, we need to run your script to define them. Click green round button above your script window.
  4. Now, you can hopefully execute the dance! In your shell, type
    dance()
    Or if you like, just wiggle a bit
    wiggle(1)
  5. What do you expect to happen if you call wiggle with the following unexpected or non-existent parameters? After thinking about it, verify your prediction by trying it out.
    wiggle('A')
    wiggle()
    wiggle(3.14159)
    Why do you suppose Python responds the way it does for each of these?

F. Hoe down!

Now it's time to have some fun. Create your own module called dance.py. Give the script a header with file, purpose, and author comments. Use the import and initialize commands next. Finally, write your own function called bustMoves that gives the robot commands you learned from the readings. Your result should
Copyright © 2015 Jerod Weinman.
cc-by-nc-sa.png This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 4.0 International License.

Footnotes:

1Note the term "shell" is the same we used with the Terminal window; it's common parlance for an interactive command. In this case, we are interacting with the interpreter for the Python programming language, rather than the general computing command environment you use in Terminal
2This is the real-world equivalent of Trinity asking for programs to fly a helicopter in The Matrix.