I know it's been quite awhile since I updated the blog here. Believe it or not, I've actually been pretty busy. That's why I haven't been able to work on many projects or get much blog content up lately. However, tonight I am going to rectify that with a quick description about some of the testing I am doing for my next upgrade for Ardie.
Basically I want to give Ardie a set of 'sight' sensors to help him avoid obstacles. While that sensor suite could, one day, include a full set of cameras, today is not that day. Today, instead, I am going to be testing some ultrasonic rangefinders for Ardie to use in place of actual eyes. Rangefinders work in the same manner that a bat or a dolphin's echolocation does. They bounce a super-high-pitched sound wave off of nearby objects and then process the reflected wave to determine how far away the object is. The result is returned as a digital step wave
Notes on the Sensor
Basically these rangefinders, or ping sensors, work by sending out a sound wave. The sound wave then bounces off of a nearby object and returns back to the rangefinder. Based on that soundwave response, or "ping," the rangefinder calculates the distance to the nearby object. If the object is further away, the rangefinder sends a long digital step wave out its "SIG" pin. If the object is close, a digital step wave with the same amplitude is returned, but it has a shorter wavelength. The exact length of the returned step wave (measured in time) depends on the length of the ping pulse shot out by the sensor in the first place. This relationship is depicted below in Figure 1 (image is the property of Parallax).
In order to properly use the data returned by the sensor, we need to have some means of scaling t_in-min and t_in-max. Luckily, Parallax has already provided us with that information for a pulse output burst of 5 us (us = microsecondes for the purpose of this blog entry. 1x10^-6 us = 1 regular second), These values are depicted in the Parallax provided Table 1 below.
Now we know that the sensor should return a pulse length somewhere between 115 us and 18.5 ms (note, this is equivalent to 18,500 us). We must note that this numerical value is not returned directly via the signal pin, as would be the case for an analog sensor. Rather we must configure the our signal pin to switch from an output mode (to generate the 5 us t_out burst) to an input mode. We can then use a while loop to add 1 us to a time counter variable for every tick through the loop (Arduino keeps time in microseconds) until the input on the signal pin switches from high to low. By doing so, we will effectively measure the pulse width of the high amplitude return signal. The relevant bits of the source code for performing this reading are shown in the preformatted text box below.
Reconfiguring the Testbed
output pins for the LED display. Each wire depicted
corresponds to a display LED on the breadboad.
Digital pin 13 was used for the I/O interface to the
SIG pin on the rangefinder. The white and blue wire
corresponds to this connection.
I also reconfigured the LED pins I am using for output. Rather than streaming my output along pins 0 through 9, I used digital pins 2 through 11. I had to do this because pins 0 and 1 are used when transmitting and receiving data across the serial bus attached to the computer. Since I wanted the serial output enabled for this test I needed to use pins other than 0 and 1 for my first two LED outputs. Thus, I moved every LED wire over by two slots. This final configuration is depicted, albeit sloppily, in Figure 2.
The full source code for this testbed, including a well-commented discussion of the pins used on the board, can be found at the end of this blog.
Mapping the Readings
The possible input values I could receive would vary between 115 us and 18,500 us according to Table 1 above. This left me with a 10 slot interval of (18500 - 115) / 10 ~= 1839 us. This was, therefore, the first interval mapping I tried. Essentially I lit 1 LED for every 1839 us measured on the input pin. Unfortunately, I didn't bother to scale my interval mapping against common sense. According the the Parallax specification document (PDF linked to above) this particular sensor has a maximum range of about 3 meters. Well I don't think my room is 3 meters across. Thus, calibrating my sensor for such a large distance was impractical. I discovered this when, originally, my sensor testbed would only light up two LED's for any given position of the testbed.
To fix this, I read some of the sensor input readings for various positions of my testbed and developed my own map scale. This time, I turned on one LED for every 200 us of input read. This allowed for a higher fidelity reading on the low end of the spectrum which was more important since the high end (of 18,500 us) was never really approached in practice. The final results of this 200 us mapping are depicted in the full source code. Video 1 a below shows the final test of one of my two ping sensors on the test bed.