Using the Scribbler 2 with MyroC
Today we introduce how to program the Scribbler 2 robot in using the MyroC library.
Formatted Output
Before diving into the details of the robot sensors, the following
reading from your textbook reminds you how to nicely format printed
program output with the printf function.
- King: Section 3.1, pp. 37-42
Introducing the Scribbler 2
First, you should read the S2 start-up guide, pp. 4–7 to familiarize yourself with the general logistics of using the robot.
Programming the Scribbler 2 with MyroC
When a C program is used to control a Scribbler 2 robot, processing follows four main steps:
-
The program references the
MyroClibrary, which contains numerous operations for the Scribbler 2 robot. -
The workstation establishes a wireless connection with the robot, using
the command:
rConnect ("/dev/rfcomm0"); -
Processing with the robot continues, using commands from
the
MyroClibrary.
For consistency in naming, all robot commands start with
, such asrrConnect,rBeep, andrDisconnect -
The workstation stops its wireless connection with the robot using
the command:
rDisconnect();.
These steps are illustrated in the following program; steps for compiling and running the code follow the program listing.
/* This program illustrates how to
* connect to the Scribbler robot,
* beep, and
* disconnect.
*/
#include /* include the signatures for Scribbler commands */
int
main (void)
{
rConnect ("/dev/rfcomm0"); /* connect to Scribbler */
rBeep (1,550); /* beep for 1 second at a frequency of 550 Hz. */
rDisconnect (); /* disconnect from Scribbler */
return 0; /* return, indicating no errors have occurred */
} /* main */
Compiling and Running Scribbler2/MyroC Programs
From the lab on Linux
basics, the following lines should already be placed in
your .bashrc file:
# make the libraries known to the execution environment LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/home/walker/MyroC/lib" export LD_LIBRARY_PATH
Assuming the program is called scribblerlab.c,
you could compile the program using the following line in
your terminal window:
clang -I/home/walker/MyroC/include -L/home/walker/MyroC/lib -lMyroC -lbluetooth -ljpeg -o scribblerlab scribblerlab.c
The MyroC interacts directly with a bluetooth
package for Bluetooth communication in Linux. This requires the
additional parameter -lbluetooth, which instructs the
linker to include these libraries in the final program. In addition, some
Scribbler operations include image processing, requiring
the linking parameter -ljpeg.
This line is clearly too long and awkward to type for compiling each
MyroC program. Thankfully, if you have placed a copy of
the Makefile, (as
described in the lab on
linux basics) in the same directory, you can simply
use make
make scribblerlabCommentary: Look at the
Makefile you copied to your
directory during the lab
on Linux basics.
# Generic Makefile for compiling a single C program and linking with relevant
# math, MyroC, bluetooth, and jpeg libraries for CSC 161
# To build a program "foo" from "foo.c", simply type "make foo".
# To build an object file "foo.o" from "foo.c", simply type "make foo.o"
# We rely on make's implicit rules foo.c -> foo.o for compiling and
# foo.o --> foo for linking
# Set compiler to clang, rather than the system default
CC = clang
# Set preprocessor flags
CPPFLAGS+=-I/home/walker/MyroC/include
# Set appropriate compiler flags
CFLAGS+=-Wall -Werror -std=c11
# Set linker flags to include the relevant libraries
LDFLAGS+=-L/home/walker/MyroC/lib -lm -lMyroC -lbluetooth -ljpeg
#----------------------------------------------------------------------------
# cleanup rules: To invoke this command, type "make clean".
# Use this target to clean up your directory, deleting (without warning)
# object files, old emacs source versions, and core dumps.
clean:
rm -f *.o *~ core*
-
The
makecommand utilizesMakefilefor information about how to compile. -
In using
make, the program name is given without the.csuffix (e.g.,make scribbler-lab). -
makeassumes the program name has a.cextension, andmakeproduces an executable program using the name (without the.c). -
The variable
LDFLAGSidentifies the libraries that are needed for MyroC programs and where they are located. These flags are exactly what appears in theclangline above. -
The variable
CFLAGSprovides guidance in compiling programs.-
The
-Wallflag asks the compiler to notify you of all warnings that something might not be right (read-Wallas "Warnings: all"). -
The
-Werrorflag asks the compiler to treat all warnings as errors that must be fixed in order to build an executable program. -
The
-std=c11flag specifies that the compiler should utilize the most recent implementation of the C 2011 standard .
-
The
-
The variable
CPPFLAGStells the C preprocessor where to find certain function or data type declarations that may be necessary to compile the given program code. -
Altogether, the
makeapproach uses the fullclangline, together with a directive about printing warnings and a specification of which version of C to utilize.
Once the program has been compiled (with compiled
name scribblerlab):
-
Be sure the Scribbler 2 robot is turned on, with its lights on.
Note: If the program does not respond to subsequent commands, either turn it off and back on again, or press the "reset" button near the IPRE fluke card. -
Run the program in your terminal window just as you would any C
program:
./scribblerlab
Examples
If you like, you may read over the longer examples,
earsHangLow.c
and spirit-song.c
You may also want to briefly review the MyroC documentation to see what functions you will eventually be able to use with the robot.
