As aforementioned the software runs on an Arduino Diecimila board. All the sensors but the bumpers are interfaced on the I2C bus, directly or through MAX127 converter, the battery voltage too is monitored with an AD port to know when power shortage is coming. Thanks to the help of Wire libraries the software is very simple and it’s just a matter of cyclically polling each sensor with the right timing.
1 - Settings.
Because the SRF08 sensors come everyone with the same address, the first setting must be done to assign a different I2C address to each one. It is needed just once to change the default address E0. To do that you must have one sonar only on the bus at a time. The correct configuration sequence is well explained here. When powering the SRF08 up without sending any commands it will flash its address out on the LED, one long flash followed by a number of shorter flashes indicating its address, from 0 to 15. The flashing is terminated immediately on sending a command.
Other settings are not permanently written onto the sensors and must be applied at every startup, so before main cycle starts, the analog gain and measuring range must be configured on the SRF08. The analogue gain register sets the maximum gain of the analogue stages. To set it, just write the value to the gain register at location 1.
During a ranging, the analogue gain starts off at its minimum value of 94. This is increased at approximately 70uS intervals up to the maximum gain setting, set by register 1. Maximum possible gain is reached after about 390mm of range. The purpose of providing a limit to the maximum gain is to allow you to fire the sonar more rapidly than 65mS. Since the ranging can be very short, a new ranging can be initiated as soon as the previous range data has been read. A potential hazard with this is that the second ranging may pick up a distant echo returning from the previous "ping", give a false result of a close by object when there is none. To reduce this possibility, the maximum gain can be reduced to limit the modules sensitivity to the weaker distant echo, whilst still able to detect close by objects. The maximum gain setting is stored only in the module’s RAM and is initialized to maximum on power-up, so if you only want do a ranging every 65mS, or longer, you can ignore the range and gain registers. Note that the relationship between the gain register setting and the actual gain is not a linear one. Also there is no magic formula to apply. It depends on the size, shape and material of the object and what else is around in the room. Try playing with different settings until you get the result you want. If you appear to get false readings it may be echo's from previous "pings", try going back to firing the SRF08 every 65mS or longer.
An internal timer sets the maximum range of the SRF08. By default, this is 65mS or the equivalent of 11 meters of range. This is much further than the 6 meters the SRF08 is actually capable of. It is possible to reduce the time the SRF08 listens for an echo, and hence the range, by writing to the range register at location 2. The range can be set in steps of about 43mm (0.043m or 1.68 inches) up to 11 meters. The range is ((Range Register x 43mm) + 43mm) so setting the Range Register to 0 (0x00) gives a maximum range of 43mm; setting 255 (0xFF) gives the original 11 meters (255 x 43 + 43 is 11008mm). For this purpose I’ve chosen a value of 57 to obtain an about 2,5 meters range (57 x 43 + 43 = 2494 mm range). Considering a sound speed of 340 m/s we can apply a ping time of about 14ms: (2494mm x 2) / 340m/s = 14,67 ms ping time
Other sensors require no initialization.
As aforementioned also the digital compass must be calibrated for local inclination, but this procedure is needed just once and it is not part of the running software.
2 - Cycling.
Sensors cycle is performed in sequence every 14 ms, giving enough time to stabilize measurement for each kind of sensor. Sonars wait for echo 14ms and the different modules must not be fired at the same time to avoid lecture of false echoes. IR sensors require 39ms for each measure. The program acts as a state machine interlacing firing and lectures alternatively for different sensors every 14ms (see table). The entire cycle completes in about 100ms to read 3 US range finders, 3 IR distance sensors, 3 light sensors, battery voltage and gas sensors. At the end of this complete cycle a string with useful data is transmitted to the navigation board that takes care of the action needed to navigate safely toward the final destination
A robot running at 50cm/s travels for 5cm in 100ms, this could be too much. In order to have the fastest possible response on avoiding obstacles, a check is performed at each cycle for dangerous distances. If measured distance on any side is less than threshold or anyone of the three bumpers is active, an immediate alarm is sent to navigation board. In this way the maximum “blind” travel is much more acceptable (14ms @ 50cm/s = 0,7 cm).
3 - Reading.
The SRF08 sensors could return the distance already converted in centimeters or inches. While reading the distance value also the light value from SRF08 modules are read, this can be done within a single I2C reading cycle on register 1 and 2. Sharp IR sensors instead return an analog value inversely proportional to the distance of the object. Reading the datasheet for this device, you can learn that the response curve can be approximated with a 1/X trend. A simple conversion can be obtained with the following formula:
Dist = K / (ADCvalue - Offset)
K and Offset constants can be computed by just reading two values in the quasi-linear part of the response curve. I've chosen values of 4 cm for Dist1 and 40 cm for Dist2 still within the measurable range. In my configuration this returns: ADCval1 = 2492 and ADCval2 = 278 and:
K = (ADCval2 - ADCval1) * Dist1 * Dist2 / (Dist2 - Dist1) = 9840
Offset = (Dist2 * ADCval2 - Dist1 * ADCval1) / (Dist2 - Dist1) = 32
Of course using centimeters in calculating constants returns centimeters for Dist in the formula, using inches returns inches.
Comparing the converted reading of Sharp sensors with direct readings of SRF08 sonar the differences are within 1cm measuring distance from flat and mat objects.
In regular condition the actual distance from the object is considered as the smallest one between the measures of the three sensors in the same side (bumper = ON means Dist=0), in order to have the best safety on avoiding obstacles. MAX distance is considered 255 centimeters, allowing the use of a single byte in transmission packet.
When the voltage of the battery falls under a given threshold a sound and light alert occurs too on the sensors board to advise the operator.
The digital compass reading is very simple and plain. The bearing value is transmitted as is to the navigation board, splitting the double byte variable.
4 - Transmitting.
A simple protocol is used to transmit the information revealed and elaborated by the sensors board to the navigation board. This protocol is the same used for the telemetry on the navigation board already described.