Joseph Rollinson

Distance Sensor Interface

11 Dec 2011

Sensor board diagonal Sensor board horizontal Connected Arduino

The distance sensor interface is a set of distance sensors connected to an arduino, which is then connected via a serial connection to a computer. The computer has python modules that allow it to calibrate and collect data from the sensors. This interface uses GP2D02 sensors. These sensors have four connections to the arduino: 5v, ground, input to sensor, and output to sensor. The input and output lines both need digital pins. There are only 12 i/o digital pins on the arduino, so only six GP2D02 sensors can be connected to the arduino at any one time.

The arduino module is used to control the arduino and the sensors. To use it you must have pySerial installed on your computer.

This example prints the distance data that comes straight from the sensors.

import arduino

if arduino.checkSerial("COM1"):
    a = arduino.arduino('COM1',9600,'calibrations.dat')
    print a.getRawRanges()
else: print 'Connect the arduino to COM1'

checkSerial() makes sure that the serial port is open and available. It is very smart to check this whenever you start the arduino.

a is an arduino object. To start it, you must give it the port and speed on which the arduino is communicating. The final string is optional. It is the location or future location of the calibration data for these sensors. This is necessary to use the calibration features of the arduino. To use the arduino's calibration ability, replace the function getRawRanges() with the function getCalibratedRanges(). You must use the files module to create the calibration file.

This example creates the calibration file, stores values in it, and the recalls them.

import files

path = 'calibrations.dat'
nSensors = 6

files.updateCalibrationFile(0, (25,100), path)
print files.getCalibrationData(path)

This code will create a calibration file for 6 sensors in the file path. It then updates the information for the first sensor to be (25,100). It then prints the data for all the sensors. The calibration data stored in this file is two values, kG and kO for each sensor ina list of tuples. This data is then used by the arduino to linearize the data coming from the sensors through this equation: distance = kG/(x-kO) with x being the raw data. The Sharp GP2D02 sensors do not return linear distance data. Instead, it is rather more complicated, with high values when close to the sensor, are low values when far away. The formula used by this class return quite accurate results.

The calibration module contains a simple gui for collecting the calibration data. In order to use it, you must have the files and arduino modules installed.

import calibration, files

path = 'calibrations.dat'
nSensors = 6
sensorToCalibrate = 0

data = calibration.calibrate(i = sensorToCalibrate,
                             firstHeight = 10,
                             secondHeight = 30,
                             com = 'COM1',
                             speed = 9600,)

files.updateCalibration(path = path,
                        newData = data,
                        i =  sensorToCalibrate)

This really simple code will calibrate the first sensor at a height of 10cm and then 30cm, and store the data in the calibration path.

Calibration also contains another gui for collecting the values that will zero the sensors. This allows you to use the raw range, with infinity being zero. This is how to use it.

import calibration

print zeroValues('COM1',9600)

These three modules give you complete control over your arduino and the values it is returning.

sensorDisplay is also included in this package. This module provides a simple gui for view the raw and calibrated data that comes from the arduino. It is very simple to use, just remember to click quit instead of the x button.

sensorDisplay.rawRange(com,speed) runs the raw range window sensorDisplay.calibratedRange(com,speed,calibrationPath) runs the calibrated range window

These modules are available on GitHub.