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
Working within a terminal window,
open a program file called
Starting at your home directory, you could use these commands:
cd csc161 cd labs emacs quarts.c &Troubleshooting: If your
emacseditor 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
xkeys at the same time, release them and then type a 1 (one) to change to one window.
Paste the contents of
quarts.cfrom the reading into the
emacswindow you opened from the terminal.
Compiling and Running
Compile and run the program in your terminal window by typing:
clang -o quarts quarts.c ./quarts
In this example,
clangis 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
-owhich 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).
Run the program several more times by typing
./quarts. (You need not compile the program each time unless you have changed
Experimenting with Compiling
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
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
- Misspell the name of the included header file.
Writing Your Own Program
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).
In your existing emacs window, open a new buffer for
another program you will write. To do this, use
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
.cfor several reasons:
You can identify the C programs quickly when you list files in
your directory with the
emacs recognizes the
.cextension 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
.cextension and uses this information to properly compile your program.
- You can identify the C programs quickly when you list files in your directory with the
To organize this program, begin by declaring
gallonsas variables in addition to
litersin 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
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.cto 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.
- 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.
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
rConnect ("/dev/rfcomm0"). This
opens a connection to the robot using the
/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
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:
First you will copy the existing
scribblerlab.cprogram into your own file in one of two ways:
If you already have an emacs window open, create a new
scribblerlab.cin 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.cinto your emacs editor's window. Don't forget to save it every time before you compile.
- If you already have an emacs window open, create a new buffer called
Compile the program by typing in the terminal. Two approaches are
MyroCenvironment, 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
-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
Makefileto automate the process. The command
maketells the computer to open the file
Makefileand 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:
clangcommand prints errors that there are undefined references to
`rConnect', `rBeep', and/or
Be sure you have included the library flags
-lMyroC -lbluetooth -ljpegin your
Be sure you included the MyroC material into
.bashrcfile, as described in the early parts of the lab on Linux Basics
- Be sure you have included the library flags
clangcommand works, but the
makecommand indicates errors with undefined references to
`rConnect', `rBeep', and/or
`rDisconnect', be sure you have copied the
Makefilefile to your current directory, as described in the
Makefilesection of the lab on Linux Basics
- If the
- For the
With either approach for compilation, run the program in your terminal
Experimenting with Connections
In the program
scribblerlab.c, delete the
/dev/rfcomm0port that is in
rConnect(). What happens when you compile and run? Now type
hellointo 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/rfcomm0and save the program.
Delete the line containing the
#includestatement. 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);
scribblerlab.cprogram, copy the beep statement and change the frequency to 600, 700, 900, 400, 15000 and 200. Experiment with frequencies. Which frequencies are audible?
- 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
- Write a C program that uses a value for the radius of a circle and computes the circle's area and circumference.
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.
Look at the MyroC documentation. Write a program to explore and test other procedures that interest you.
- 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.