MCP robot

MCP Documentation: Sonar Subsystem


1. Introduction

Origins and History 

A long time ago in a galaxy far away, the MCP was controlled by several Handyboards, desgined by Professor Fred Martin. The Handy Boards could only use one sonar sensor, and the MCP needed 8, so a student by the name of Mike Bohan developed a multiplexor board. Andrew Chanler had also worked on his SerialSense board, which converts some of the "standard" sensors into a serial device, for use on a PC. Jay Kahrmann figured out how to use the multiplexor board with the SerialSense. Professor Martin and I thought it would be a good idea to combine the SerialSense board and the multiplexor board into one circuit, rather than having a rat's nest tangle of wires everywhere. Documentation already exists for the past projects, so this document will only cover my designs and modifications.

2. The Hardware Aspect

Section Title 

The hardware was designed using Eagle for Windows. The program is available, as a free demo version, at

Figure 2.1: Board Schematic

Fig. 1: schematic

Figure 2.2: Board Layout

Fig. 2: circuit board

These Eagle files, and all associated Gerber files for board manufacturing, are available in at

Note: Because this board is a modification of Andrew Chanler's SerialSense, the PIC chip used in this board must be flashed with the SerialSense assembly code.

3. The Software Aspect

Get the Code 

First and foremost, download and install Andrew's SerialSense code. Instructions are available on his website, .

My sonar multiplexor source code is available for download at After extracting the files from the ZIP, copy or move serialsense.cpp and serialsense.h into the same directory as the sonar code.


The sonar.h file must be included by whichever program will be reading from the shared memory. This contains the declarations for constants, as well as function prototypes. The serial port upon which the board will be connected is also defined here.

Important: Make sure to chance the #define SERIAL_PORT declaration to the serial port you are using. Otherwise, it will not work.

sonar_write.cpp is the stand-alone program that continuously polls the sonars in a ring, updating the shared array after every poll. Compile this program along with the SerialSense code. sonar_write takes care of itself, and should not need to be modified.

Code listing 3.1: Compile sonar_write.cpp

            # g++ -o sonar_write sonar_write.cpp serialsense.cpp

sonar_read.cpp contains the functions that allow you to acces the sonar readings. You will need to use these functions in your code, and compile sonar_read.cpp along with your program.

4. Function Documentation

These are the only two functions a programmer will need access to, and they live in sonar_read.cpp.

bool shm_init() This grants access to the shared memory segment set up in sonar_write. If the setup goes well, it returns a true, otherwise it will return a false.

int sonar_read(int p) Returns an integer representing time in milliseconds. The argument is a sonar port ranging from 1 to 8. An invalid argument will return an error.

5. Sample Code

The following is an example of how to use the sonar code.

Code listing 5.1: A sample program

            #include <stdio.h>
            #include "sonar.h"

            int main()
               int x;


               for(x = 1; x <= 8; x++)
                  printf("port %d: %5d", x, sonar_read(x));

               return 0;

Updated 2005-05-19
Brian Corbin

Summary:  This document contains pertinent information regarding the sonar subsystem of the MCP, developed in the Spring Semester of 2005 at the University of Massachusetts Lowell. The sonar code is in three pieces: sonar.h, sonar_write.cpp, and sonar_read.cpp.
Developed by students of the Engaging Computing Group in the Department of Computer Science at the University of Massachusetts Lowell