Elementary C Programming and the Scribbler 2


This laboratory exercise provides practice with basic elements of writing, editing, compiling, and running programs written in the C programming language. You will also experiment with the Scribbler 2 robot's sound capabilities by writing and compiling programs that use the robot.

Basic C Program Development

Starting Emacs

  1. Working within a terminal window, open a program file called quarts.c in emacs.
    Starting at your home directory, you could use these commands:
    cd csc161
    cd labs
    emacs quarts.c &
    Troubleshooting: If your emacs editor opens with a split screen, and if you find the split screen annoying, you might want to switch to one window. Within emacs, hold down the control and x keys at the same time, release them and then type a 1 (one) to change to one window.

  2. Paste the contents of quarts.c from the reading into the emacs window you opened from the terminal.

Compiling and Running

  1. Compile and run the program in your terminal window by typing:
    clang -o quarts  quarts.c

    In this example, clang is the name of the compiler program that you want to use to translate your file into a machine-executable one.

    The next token in the line is -o which tells the compiler that you want to create a machine-executable output file with a specific name, which is the next token in the command line: quarts. This becomes the name of the program, which you will type in order to run it. The final token in the line is the name of the file you created, which is used as the source of the compilation/translation process. In this example, typing the command to compile a single file into an executable program is pretty straight-forward. But when we are compiling multiple files (such as when we create programs to run the robot), we will use scripts to automate what can be a rather tedious and error-prone process (see step 8 below).

  2. Run the program several more times by typing just ./quarts. (You need not compile the program each time unless you have changed quarts.c.)

Experimenting with Compiling

  1. Make the following typographical errors in quarts.c, recompile, and observe what, if anything, happens. (Note: in many cases, you will get an error of some kind.)

    In each case, check whether the program compiles, and whether the program runs. If the program does not compile, what happens if you try to run ./quarts?

    In working through the following, pay close attention to the error messages that result. What do they say? These will be helpful cues to you later as you try to find the underlying cause of errors the compiler reports to you.

    • Type a few characters into your program BEFORE any of the code.
    • Type a few characters into your program AFTER the code.
    • Type some extra words inside of your main method.
    • Misspell your variables.
    • Misspell your printed output.
    • Misspell the name of your main() method.
    • Misspell the name of the included header file.

Writing Your Own Program

  1. In this exercise, you will write a C program that uses values for pints, quarts, and gallons and determines the corresponding number of liters. For example, your program might compute the number of liters corresponding to 3 gallons, 2 quarts, and 1 pint (i.e., 14.5 quarts total).

    1. In your existing emacs window, open a new buffer for another program you will write. To do this, use the Control-x Control-f keyboard sequence, then add the name of the program file to the path at bottom of your emacs window.

      Note: Although you can name this new program whatever you like, you should end the file name with .c for several reasons:

      • You can identify the C programs quickly when you list files in your directory with the ls command.
      • emacs recognizes the .c extension as indicating a C program, and emacs adjusts its setting to aid your editing for that type of file.
      • The build tool make also recognizes the .c extension and uses this information to properly compile your program.
    2. To organize this program, begin by declaring pints and gallons as variables in addition to quarts and liters in the existing program. Next, assign values to these variables, such as:

      gallons = 3;
      quarts = 2;
      pints = 1;

      In computing the total value of liters, one approach would be to compute the total number of quarts from pints, quarts, and gallons (possibly using another variable, such as total_quarts). From this total_quarts, you could compute the total number of liters.

      In computing the total number of quarts, you should use 4 quarts per gallon and 2 pints per quart. (Be sure that 3 gallons (given in the example above) translates to 12 quarts, not 3/4 or 0.75 quarts.)

      You should refer to the annotations on quarts.c to properly print out your output.

    Remember that you will need to save your buffer (e.g., using the C-x C-s command sequence in emacs) and compile your program (using clang) before you can run it.

Getting Started with the Scribbler 2

For every program using the Scribbler 2 robot, you first must connect to the robot. At the end of the program (before return 0;), you will disconnect from the robot. The command to connect to the robot is rConnect ("/dev/rfcomm0"). This opens a connection to the robot using the port /dev/rfcomm0. Later in the lab, you will experiment with what happens when the port is not included. The command to disconnect from the robot is rDisconnect(). Here, you do not need to state any port.

Hint: Don't forget to include the library MyroC.h at the beginning of every program.

Connecting to and disconnecting from the robot

In this exercise, you will download the code for a program that connects to the Scribbler, beeps once, and then disconnects from the Scribbler. Read the program and its annotations to further understand what is happening. You will then copy the program to your emacs file and compile the program on your terminal. Finally you will run the program.

Here are the steps to do these:

  1. First you will copy the existing scribblerlab.c program into your own file in one of two ways:
    • If you already have an emacs window open, create a new buffer called scribblerlab.c in your labs directory (e.g., using the C-x C-f command sequence).
    • If you are starting this exercise on a new lab day and do not already have an emacs window open, open a terminal window and move to the lab directory you are using for this course, then start emacs with the command:
      emacs scribblerlab.c &

    Paste the program scribblerlab.c into your emacs editor's window. Don't forget to save it every time before you compile.

  2. Compile the program by typing in the terminal. Two approaches are possible.
    • For the MyroC environment, use the line:
      clang -I/home/walker/MyroC/include -L/home/walker/MyroC/lib -lMyroC -lbluetooth -ljpeg -o scribblerlab scribblerlab.c

      In this line, you should recognize some patterns from the discussion under step 3, but there are some new options given of the form -I, -L, and -l. These tell the compiler to bring in additional files so that your program can use code written and compiled by Professor Walker to run the Scribbler2 robots. Typing all of this correctly multiple times is difficult; so we use the Makefile to automate the process. The command make tells the computer to open the file Makefile and run the commands it finds there. All it needs to know from you is what filename you want it use when it stores the final, executable code.

      This is a fairly complex process, and so the Makefile will probably seem intimidating. We will have a lab in a few weeks in which we start to understand how to write our own Makefiles to automate compilation.

    • In the meanwhile, you can use the command:
      make scribblerlab


      • If the clang command prints errors that there are undefined references to `rConnect', `rBeep', and/or `rDisconnect',
        • Be sure you have included the library flags -lMyroC -lbluetooth -ljpeg in your clang command.
        • Be sure you included the MyroC material into your .bashrc file, as described in the early parts of the lab on Linux Basics
      • If the clang command works, but the make command indicates errors with undefined references to `rConnect', `rBeep', and/or `rDisconnect', be sure you have copied the Makefile file to your current directory, as described in the Makefile section of the lab on Linux Basics
  3. With either approach for compilation, run the program in your terminal by typing:

Experimenting with Connections

  1. In the program scribblerlab.c, delete the /dev/rfcomm0 port that is in rConnect(). What happens when you compile and run? Now type hello into the port. What happens when you compile and run? Do the same for the empty string "". When you are done, restore the port to /dev/rfcomm0 and save the program.
  2. Delete the line containing the #include statement. What happens when you try to compile? Restore the include statement and save (you could do this with Emacs' undo command, C-_; note that the Shift key is necessary to get the underscore).

Sound from the Scribbler 2 Robot

Here is the documentation for rBeep() from the development version of the MyroC.h header file:

 * @brief Beeps with the given duration and frequency
 * @param duration   length of note in seconds
 * @param frequency  frequency of pitch in cycles per second (hertz)
 * @pre              duration > 0.0
void rBeep (double duration, int freq);
  1. In your scribblerlab.c program, copy the beep statement and change the frequency to 600, 700, 900, 400, 15000 and 200. Experiment with frequencies. Which frequencies are audible?
  2. Now vary the length of the beeps. Copy the beep statement again and this time change the duration to 0.75, 2, 2.5, 3, 3.1, and 4. Listen to what happens.

Writing More C

  1. Write a C program that uses a value for the radius of a circle and computes the circle's area and circumference.
  2. Write a program that connects to the robot, makes it beep a short tune that sounds good to you, then disconnects from the robot.
    Note: The pitches for various notes can be found on numerous online sources or in the example programs for today.

Optional Activities

  1. Look at the MyroC documentation. Write a program to explore and test other procedures that interest you.

  2. Experiment more with the emacs editor, following the reading on the Emacs text editor. Find and try out at least one command from the GNU Emacs Quick Refence card that was not discussed in the reading.