CSC 161 Schedule Readings Labs & Projects Homework Deadlines Resources

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.

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:

  1. The program references the MyroC library, which contains numerous operations for the Scribbler 2 robot.
  2. The workstation establishes a wireless connection with the robot, using the command: rConnect ("/dev/rfcomm0");
  3. Processing with the robot continues, using commands from the MyroC library.
    For consistency in naming, all robot commands start with r, such as rConnect, rBeep, and rDisconnect
  4. 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 <MyroC.h>          /* 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 scribblerlab
Commentary: 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*

Once the program has been compiled (with compiled name 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.