Using a Beaglebone with an HC-SR04 sonar

Over the last couple of days, now that Robogames 2013 has come and gone, I’ve been working on projects that are more experiments than anything else.  A few weeks before Robogames, I noticed that there were sonars for real cheap on http://www.wrighthobbies.com/.  At the time I didn’t realize that these were all over the internet and are essentially a cheap Chinese knockoff of devices like the Ping))) and SRF04/5.  These sonars are dirt cheap, buying in bulk from Ebay can lead gets you prices approaching $1.50 a unit.  Seems insane, considering I’ve bought a 5 pack of Ping)))s for $100, but one can by a pack of 10 of these little guys for $15.  Needless to say I didn’t have high expectations for them.  So I ordered a couple so that I could test against my collection of Ping)))s and SRF04s.  Much to my surprise, they perform just as well as the Ping))) and the SRF04.  I don’t know how long they will be available at this price, so I ordered a whole lot more of them.  Their interface is the same as an SRF05, and you can connect the trigger pin and the echo pin and use only one I/O line per sonar, giving it the same interface as a Ping))).

One of my personal side projects is the Beaglebot, a small, tabletop or hardwood roving bot that I want to entirely operate using the Beaglebone, as in, without external AVRs, arduinos, etc.  So I set out to understanding Linux kernel development so that I could write kernel drivers for all of my hardware.  I was very happy to discover that kernel module development is actually really easy, nothing like driver development is with Windows.  Within a week I had an LED driven by PWM and you could control its max brightness and blink rate from sysfs.  At this point I was feeling confident enough to tackle driving a sonar from my Beaglebone.  My testing of the sonars previously had used an Arduino Uno and interrupts to keep the CPU free from having to wait on the sonars to respond and to be able to do stuff during measurements, etc.  This is exactly how you should write drivers for operating systems.  So I wrote my sonar driver.  And when I loaded it up and debugged it as best I could, I was disheartened that I could not get the sonar working properly, for I was getting extremely variant or random measurements.  With all of my experience with microcontrollers, my thought had been that interrupt handlers would execute when interrupts occurred, but it’s a bit more complicated than that.  Interrupts aren’t handled immediately, but rather put onto a queue which is sorted by priority and processed sequentially.  This is not good for a device where its input is time dependent.  So I began to explore other options and discovered that the Beaglebone has two high speed microcontroller cores, known as PRUs (Programmable Realtime Units), built into the CPU that can access the entirety of the memory map, meaning that they can access all of the memory, all of the IO, and the communications buses.  They both have an interrupt to the CPU so that they can signal when they’ve completed some action, in this case, that the sonar has taken a reading.  Before toying with the code, we have to hook up the sonar to the Beaglebone.  The HC-SR04 is a 5V device, and will not operate at any less than that.  As the beaglebone, along with every other ARM Cortex based system, has a maximum voltage of 3.3V, we have to protect the beaglebone from the sonar.  Essentially all I did for this was connect an NPN transistor between one of the beaglebone’s IO pins and ground.  This is fine considering that each of the IO lines on the beaglebone has a configurable pullup or pulldown resistor, we just have to note that the signal is now inverted.  The base of the transistor is connected through a 220 ohm resistor to the echo line of the sonar.  From experimenting, turns out 3.3V is enough to cause the sonar to trigger, so we can hook one of the beaglebone’s IO lines right up to the trigger pin.  The schematic is essentially this

bb-hcsr04-circuit-diagram

And this is what the  setup looks like

bb-hcsr04-circuit

bb-hcsr04-occillosope

The software I have provided on github assumes that the sonar’s trigger is using GPIO1_6 and the inverted echo signal is on GPIO1_7.  The operation of the HC-SR04 sonar is fairly straight forward.  One sends a trigger pulse of at least 10 microseconds.  The device then waits for the echo line to activate (in our inverted case, high to low transition) and measures the time from there until it deactivates (low to high in our case).  This time is the round trip flight time of the sonar pulse.  Divide this number by 58, and you have the distance to target in centimeters.

The code on the Beaglebone side is a bit less straightforward.  Unfortunately there is no higher level language available for the PRU than PRU Assembler.  Oh well I suppose.  The guide on how to use the sample code is here on my github: https://github.com/Teknoman117/beaglebot/tree/master/hcsr04-demo  In the mean time, here is a YouTube video of my sonar code in action

Kybernetes at Robogames 2013

Kybernetes is a test platform for autonomous navigation I developed with the Robotics Society of UC Merced.  Up until recently, I’ve been the only person on the project, however our recent increase in membership means Kybernetes will serve as an important resource for educating new members in electronics, sensor systems, localization, and mapping.

Please ignore the outburst of “ran out of power” in the video, I was jumping to conclusions since I was running on a two hours of sleep and countless cups of coffee.

The robotics society placed second in the Robomagellan competition, getting within seven feet of the goal cone.  We didn’t manage to complete the course for two major reasons – power problems and the vision system.  The power problem reinforced an important lesson, that electric motors cause serious amount of electrical noise in single power supply systems.  Because of the filtering in the switching regulator leading to the electronics, we avoided the classic issue where “gunning it” would reset the computers.  The major issue at the competition was that anytime the motors generated a spike (speeding up, slowing down rapidly), the servos would jerk to one of their extremes before accepting any control input again. This problem is evident at the beginning of the video where the robot makes a sudden right turn as it starts. We had only been testing on paved areas on campus and the dry, short grass in the UCM quad. The wet and tall grass at Robogames was the first time we observed the issue, and we spent the next hour or so patching in the servo power straight to the battery instead of through the main motor ESC’s battery eliminator circuit. We didn’t do this earlier because the battery supplied a voltage above the rated value for the servos (8.4V battery, 7.2V servos), but for the course of the competition at least, we didn’t blow up the servos.

Solving the power issue consumed the time I had allotted to calibrating our vision system for the conditions of the Robomagellan course. We solved the issue right before we were called up for our first run of the day. I hadn’t even put in the course coordinates yet, so I ran the heading lock test, hoping that Kybernetes would at least follow a straight course and put some distance between the starting point and the goal cone. Since the robot would not stop automatically (it was just following the heading it was pointing when the program started), I stopped the run with our failsafe remote. We were around 40 meters from the goal cone, about half the distance to it from the starting cone.

After walking and plotting a course for Kybernetes to take during the lull between rounds, I loaded the coordinates for our next run. When our time came, I put Kybernetes in the starting position, started the navigation program, and prayed. Camp called for the start of the run and off Kybernetes went, following the course I plotted perfectly.  It drove to the point at which it previously stopped, made a hard right towards the goal cone, and opened the throttle (well, the electronic equivalent of it anyways).  Its hard to see in the video, but it hit the transition between the grass and pavement pretty harshly, tilting the front up about 45 degrees, sending it about four inches into the air.  It landed on its wheels and continued all the way up to the cone, stopping seven feet of it.  In all honesty I was a little sad it didn’t continue, but since the vision was disabled, it was instructed to stop after predicting its distance to target within the error radius of the GPS.

In the end, there were things that could have been done better, but I went home that day knowing that Kybernetes had done everything it was instructed to do flawlessly.  If you want to check out the software written to control Kybernetes, check out the “kybernetes” repository on my GitHub.

Kybernetes Software:

https://github.com/Teknoman117/kybernetes