Ultrasonic Distance Sensing using the Polaroid 6500 Sonar Ranging Sensor
Last Update 5/5/2003

If you are not interested in the theory of operation then jump to How to Use the Polaroid Distance Sensor section and download the file listing links and include the files in your code.

Theory of Operation

"To perform ultrasonic ranging, a transmitter circuit causes an ultrasonic burst, or "chirp," to be emitted from a speaker element.  This sound travels out to an object and is reflected back into a receiver circuit, which is tuned to detect the specific frequency of sound emitted by the transmitter.

By measuring the elapsed time from when the chirp is emitted to when the echo is received, the distance may be calculated.  In normal room temperature, sound travels about 1.12 feet per millisecond or 0.89 millisecond per foot.  Because the sound has to go out to the object and then back to the receiver, 1.78 msec of elapsed time corresponds to an object at one foot's distance from each of the emitter and receiver.

So the distance to the target object (in feet) is the time it takes for a chirp to make a round trip (in msec) divided by 1.78." (Martin, p.271)

This process is performed by the Handy Board which triggers the transmitter to emit the ultrasonic sound (INIT signal sent to Polaroid controller) and then measures the elapsed time in Handy Board count units (2,000,000 counts per second) until an echo is received (ECHO signal on Polaroid controller goes high) from the nearest object.  The number of counts measure the time of flight to the object and back and can be converted to distance measurements.

The Polaroid ultrasonic sonar uses a transducer that is both the transmitter and receiver and because of this it can experience "ringing".  Ringing caused by the transmitted pulse can cause the sensor circuit to detect a false echo, which is why the Polaroid controller board has a blanking signal to block any return signal for 2.38 milliseconds after the transmitted pulse.  Unfortunately, this limits the default range to 1.33 feet which is the elapsed distance for sound in 2.38 milliseconds.  If you need to detect an object closer than 1.33 feet the Polaroid controller provides a  blanking inhibit (BINH) signal to disable the blanking feature.  Disabling blanking before the ringing stops could cause false echo's and caution is recommended when using this feature.  The Handy Board sonar as used with the LTU laptop robots uses the blanking feature by default and is thus not as accurate at less than 1.33 feet.  A close-up capability is available in the Handy Board code but it is only available by substituting the close-up function into the code, recompiling, and downloading to the Handy Board.  The Polaroid sonar operates at a frequency of 49.4 kHz with 16 cycles per chirp. (Martin)

How to Use the Polaroid Distance Sensor
 
The Handy Board can be commanded to perform a momentary sonar distance measurement or to continuously measure sonar distance and transmit the sensor values in a data packet through the serial port.  The current Polaroid sensor driver program on the Handy Board can only support on sonar sensor but the effect of multiple sensors can be achieved by mounting the sonar transducer on a servomotor and rotating it in the direction of interest.  Servomotor control is performed by sending the appropriate motion control packet.  See the motion class description for servomotor control function commands.  All that is required to retrieve sensor data values is to capture the sonar sensor data packets and parse the packet for sensor distance values.The functions to capture and parse sensor packet data and return the sensor reading are contained in the SONAR_DISTANCE class member functions.

Data Packet Format
The format for the SONAR_DISTANCE sensor packet is:

packet[0] = 0x02 (ASCII 'STX' character) - Indicates start of packet
packet[1] = 'P' (ASCII 'I' character) - Indicates Polaroid data sensor packet identifier character
packet[x - xxxxx] = Sonar distance value in Handy Board count units
packet[x] = 'X' (ASCII 'X' character) - Delimiter that indicates end of sensor value
packet[x] = 0x03 (ASCII 'ETX' character) - Indicates end of Sonar distance sensor data packet

Communication Protocol (optional information)

In order to make the control system compatible with different computers and programming languages the serial port is used as the method to communicate and send sonar distance sensor control commands to the Handyboard and detailed knowledge of the Handyboard hardware or software is not necessary. The SONAR_DISTANCE interface handles the communications protocol and is invisible to the user of the class unless you want to write your own functions. If you do write your own functions then you must construct a sonar distance command packet, send the start transmission character “STX” (0x02), wait for acknowledgement character “ACK” (0x06) signal from the handyboard, and then send the remainder of the packet.The format for the packet is:

Packet[0] = 0x02 (“STX” start transmission character)

Packet[1] = ‘P’ (Polaroid Distance Sensor Command Packet Identifier tag character)

Packet[2] ='T' or 'F' (Activates sonar sensor to transmit data continuously (mode ='T')
                                    or single data packet (mode = 'F'))
Packet[3] = 0x03 (“ETX” end transmission character)

Packet[4] = 0x0D (Carriage return character)

Once you construct this packet you must send the first character (0x02) of the packet until you receive an “ACK” (0x06) character from the Handy Board. This indicates that the Handy Board is ready to receive the remainder of the packet.The Send_packet(char * speed_packet) function handles this process for you and all it needs is a pointer to the packet string passed as a parameter. IMPORTANT: If the handyboard is not turned on or has a bad connection to the serial port then the Send_packet function will lock up until it receives the “ACK” (0x06) character from the Handy Board.

Hardware Setup

Before running the program verify the hardware setup by:
 

  1.     Attach serial cable from the Handy Board serial interface to the laptop serial port.
  2.     Turn on the Handy Board.The LCD screen on the Handy Board should display Running... if the sensor program   is loaded and running.Press the stop button to stop the program (LCD screen will display Stopped...).
  3.     Ensure that no other applications are using the comport. The comport and SONAR_DISTANCE object will display an error message on the laptop screen if the comport is already in use by another application.
Follow these steps to use the methods of the SONAR_DISTANCE class:
  1. Include the sonar_distance.h header file in your program (you will also need the comport.h header)
  2. Instantiate an SONAR_DISTANCE and comport class objects.
  3. Call the comport1.getHandle() member method from the comport class to get a file handle to the serial port to pass as a parameter to the SONAR_DISTANCE class object. The SONAR_DISTANCE class needs this handle to communicate with the serial port (com port).
  4. Call the readSonarSensor(void) member function which takes one momentary sonar sensor reading and returns the distance value in Handy Board count units.
  5. Repeat step 4 for each new reading.
  6. Or for continuous sonar sensor readings call the activateSonar(true) method to activate continuous sensor readings and call the readSonarPacket(void) to return the sonar sensor readings.
  7. Call activateSonar(false) to deactivate continuos distance sensor data packets.
Sample Program
This sample program illustrates the use of the comport and SONAR_DISTANCE class:
 
#include "comport.h" //Com port RS-232 communications class interface header
#include "sonar_distance.h" //Sonar distance sensor class header file
#include <iostream.h>
int main(void)
{
COMPORT comport1(COM1, BAUD_RATE_9600, NO_PARITY, DATA_BITS_8, STOPBITS_1);

 //Instantiate robot sonar distance sensor class and pass comport handle
 SONAR_DISTANCE sonar(comport1.getHandle());
 

//Read Sonar Distance Sensor
cout<<"Read Sonar Distance Sensor"<<endl;
cout<<"Sonar Sensor value = "<<sonar.readSonarSensor()<<endl;
 

return 0;

}//End main

SONAR_DISTANCE class Interface

The methods of the SONAR_DISTANCE class are:

  int readSonarSensor(void);    Activate sonar range sensor and return distance value in sensor units

  int readSonarPacket(void);    Reads serial port and grabs sonar data packet and returns data value in packet
                                                 and also stores the return value in the distance private member variable
                                                you can also call getSonarDistance(void) after this function returns and
                                                get the sonar distance value.
                                                !!!CAUTION THIS FUNCTION WILL CONTINUOUSLY LOOP UNTIL A PACKET IS RECEIVED - THEREFORE MAKE SURE SONAR DATA PACKETS ARE BEING TRANSMITTED BEFORE USING THIS FUNCTION

  int getSonarDistance(void);   Returns sonar sensor value stored in distance private member variable

  void activateSonar(bool mode);     Activates sonar sensor to transmit data continuously
                                                         (mode = true) or single data packet (mode = false)

SONAR_DISTANCE Class Interface Header File Listing
sonar_distance.h
SONAR_DISTANCE Class Implementation .cpp File Listing
sonar_distance.cpp

Comport Class Interface Header File Listing
comport.h
Comport Class Implementation .cpp File Listing
comport.cpp

References

Martin, Fred G. (2001). Robotic Explorations. New Jersey: Prentice-Hall.