Elementary C Programming and the Scribbler 2
Introduction
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
-
Working within a terminal window,
open a program file called
quarts.cin emacs.
Starting at your home directory, you could use these commands:cd csc161/labs emacs quarts.c &
Troubleshooting: If youremacseditor opens with a split screen, and if you find the split screen annoying, you might want to review the section on Modifying emacs in the lab on Linux basics. -
Paste the contents of
quarts.cfrom the reading into theemacswindow you opened from the terminal and save the file.
Compiling and Running
-
Compile and run the program in your terminal window by typing:
clang -o quarts quarts.c ./quarts
-
Run the program several more times by typing
just
./quarts. (You need not compile the program each time unless you have changedquarts.c.)
Experimenting with Compiling
-
Make the following typographical errors in
quarts.c, recompile, and observe what, if anything, happens.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
-
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
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
.cfor several reasons:-
You can identify the C programs quickly when you list files in
your directory with the
lscommand. -
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
the
.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
pintsandgallonsas variables in addition toquartsandlitersin 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,andgallons(possibly using another variable, such astotal_quarts). From thistotal_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
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:
-
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
buffer called
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 &
scribblerlab.cto your emacs 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
possible.
-
For the
MyroCenvironment, use the line:clang -I/home/walker/MyroC/include -L/home/walker/MyroC/lib -lMyroC -lbluetooth -ljpeg -o scribblerlab scribblerlab.c
-
Alternatively, you can use the command
make scribblerlab
Troubleshooting:
-
If the
clangcommand prints errors that there are undefined references to`rConnect', `rBeep', and/or`rDisconnect',-
Be sure you have included the library flags
-lMyroC -lbluetooth -ljpegin yourclangcommand. -
Be sure you included the MyroC material into
your
.bashrcfile, as described in the early parts of the lab on Linux Basics
-
Be sure you have included the library flags
-
If the
clangcommand works, but themakecommand indicates errors with undefined references to`rConnect', `rBeep', and/or`rDisconnect', be sure you have copied theMakefilefile to your current directory, as described in theMakefilesection of the lab on Linux Basics
-
If the
-
For the
-
With either approach for compilation, run the program in your terminal
by typing:
./scribblerlab
Experimenting with Connections
-
In the program
scribblerlab.c, delete the/dev/rfcomm0port that is inrConnect(). What happens when you compile and run? Now typehellointo 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);
-
In your
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.
Optional Activities
-
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.
