Laboratory Exercise: Using the Scribbler 2 and Basic Input
Goals
This lab introduces the Scribbler 2 robot to students, combines the Scribbler 2 with eSpeak, and provides practice with basic input.
Work Started in Class
Getting Started with the Scribbler 2
In starting work with a Scribbler 2 robot, this lab focuses on sounds (beeps). Later labs explore other capabilities of the Scribbler 2.
Some reminders from the reading
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 into your emacs
file and compile the program on your terminal. Finally you will run the
program.
Here are the steps to do these:
-
Open a terminal window and move to the directory you are using for this course.
-
Start aquamacs and open a new file.
-
Copy the program scribblerlab.c to your aquamacs window. Don't forget to save it every time before you compile.
-
Compile the program by typing in the terminal. Two approaches are possible.
-
For the MyroC environment, use the line:
gcc -Wall -Wno-deprecated-declarations -std=gnu99 -pthread -lMyroC -leSpeakPackage -lMyroC -leSpeakPackage -framework OpenGL -framework GLUT scribblerlab.c -o scribblerlab
-
The MyroC interacts directly with a bluetooth package for Bluetooth communication in Linux. This requires the additional parameter -lbluetooth. In addition, some Scribbler operations include image processing, and this requires the parameter -ljpeg
-
Alternatively, you can use the command
make scribblerlab
-
With either approach for compilation, run the program in your terminal by typing:
./scribblerlab
Reminders: Look at the Makefile you copied to your directory during the lab on Linux basics.
- The make command utilizes Makefile for information about how to compile.
- In using make, the program name is given without the .c suffix (e.g., make scribbler-lab).
- make assumes the program name has a .c extension, and make produces an executable program using the name (without the .c).
- The variable LDFLAGS identifies the libraries that are needed for MyroC programs when running eSpeak and MyroC. These flags are exactly what appears in the gcc line above.
-
The variable CFLAGS provides guidance in compiling programs.
- The -Wall flag asks the compiler to notify you of all warnings that something might not be right (read -Wall as "Warnings all").
- The -std=gnu99 flag specifies that the compiler should utilize the most recent implementation of the C 1999 standard, as implemented by the gnu project of the Free Software Foundation.
- Altogether, the make approach uses the full gcc line, together with a directive about printing warnings and a specification of which version of C to utilize.
Troubleshooting:
-
If the gcc 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 gcc 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 gcc 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
Experimenting with Connections
-
In the program
scribblerlab.c
, delete the/dev/rfcomm0
port that is inrConnect()
. What happens when you compile and run? Now typehello
into the port. What happens when you compile and run? Do the same for the null string "" . Replace the port when you are done and save the program. -
Delete the include statement. What happens when you try to compile? Replace the include statement and save.
Sound from the Scribbler 2 Robot
Here is the documentation for rBeep() from 3.1 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 frequency);
-
In the original program
scribblerlab.c
, 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.
Basic user input
-
Copy the program quarts-rev.c to your account and compile it (e.g., with gcc).
-
Run the program several times, entering several integer values.
-
Describe what happens if you enter a real number (such as 10.5 or 7.89)?
-
Describe what happens if your input begins with a non-letter (e.g., "the number 23.5")?
-
Initialize the quarts variable at the start of the program, by changing the declaration to:
int quarts = 7;
Now repeat steps 10b and 10c. Can you explain what happens?
- What happens if you change the format string for "%d" to "%lf" (without changing any other part of the program?
-
Assigning values to variables
Variables can store different values as a program progresses.
-
scanf provides one mechanism for assigning a value.
-
Computed values may be stored by placing the computation on the right hand side of a equal sign (=) and the variable on the left.
variable = arithmetic formula
This type of code is called an assignment statement. Unlike mathematics, the left and right sides of an equal sign have different meanings in C. Within a C program, the value on the right is stored in the variable on the left. As an example, consider the code;
int x = 7; int y = 5; x = y;
Here the value 5 (from y) is stored in x, so both x and y finish with the same value 5. In contrast, for the code
int x = 7; int y = 5; y = x;
Here the value 7 (from x) is stored in y, so both x and y finish with the same value 7.
-
Copy the program quarts-gallons.c to your account and compile it (e.g., with gcc).
-
Run the program several times, entering integer values for quarts and gallons.
-
Describe what happens if you enter two real numbers with decimal points (such as "10.5 2.7")?
-
As with Step 10, initialize variables quarts and gallons to non-zero integer values, and repeat Step 10b. Describe what happens.
-
Homework
Write your own programs: Beeps and Music
-
Write a program that reads a frequency (an int) and a duration in seconds (a double) and then commands the robot to beep at the given frequency for the prescribed duration.
-
Write a program that connects to the robot, makes it beep a short tune that sounds good to you, then disconnects from the robot.
Hint: The pitches for various notes can be found in file scale-notes.
Scribbler 2 Robots and eSpeak
A challenge arises when trying to test a robot-based program, in which the robot performs several actions — including movement.
- Testing requires the programmer to look at the code to know what is supposed to happen.
- Testing requires the programmer to watch the robot to see what it does.
Even with two eyes, watching both the program and the robot can be difficult. The eSpeak package can help resolve this difficulty.
-
Examine the scribbler-espeak.c program.
-
Open the scribbler-espeak.c program and copy it to your account.
-
To compile the program with gcc, you will need to specify both MyroC and eSpeak. Since typing the locations of each package is error prone, you should use the make command:
make scribbler-espeak
-
Run the program and describe what happens.
-
Edit the program, make some changes, and compile and run the revised program to check what happens.
-
Optional Activity: Playing a Duet
-
Look at the MyroC documentation.
- MyroC data structures (Picture, Pixel)
- MyroC header file (Web-based format)
- MyroC header file (C-style format)
Find the function rBeep2(). Write a simple program and test how rBeep2() works. If you finish this, go through the rest of the header file and see if there is anything else you want to try testing out.
created 13 July 2011 by Dilan Ustek & April O'Neill last full revision 18 July 2011 by Dilan Ustek & April O'Neill minor editing 22 August 2011 by Henry M. Walker revised 18 September 2011 by Erik Opavsky minor editing (added emacs note, changed footer) 20 July 2012 by Henry M. Walker Expanded with eSpeak 15 January 2014 by Henry M. Walker readings added 1 September 2014 by Henry M. Walker updated for MyroC.2.2 8 January 2015 by Henry M. Walker updated for make/Makefiles 15 January 2015 by Henry M. Walker updated for user input and reformatting 14 May 2016 by Henry M. Walker updated for Sonoma State 29 January 2022 by Henry M. Walker |
![]() ![]() |
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu. |