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 things are dirt cheap, buying in bulk from Ebay can lead gets you prices approaching $1.50 a unit.  Seems insane because I once 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

Advertisements

Kybernetes at Robogames 2013

Kybernetes is a robot I created at UC Merced for the Robotics Society. Up until now I have been the principle developer of both hardware and software, but we are working on teaching all of our members about hardware and/or software so the burden isn’t entirely on my shoulders. Either way, we placed second at Robogames by getting within 7 feet from the goal cone. Didn’t manage to complete the course for two reasons – We had a power problem and I spent three hours working on a solution with the spare parts I had, and the vision was not so great because I hadn’t had a chance to calibrate it, because I completed the software between our second and third run, in which our IMU malfunctioned and botched the run. A lot of work and love went into Kybernetes, and its practically been my pet this semester. It accompanies me to class many days because I worked on it during my sporadic free time. I’ve open sourced all of the code, because I like to share my work. One of the interesting things about Kybernetes is that I am optimizing the software for Linux and the ARM architecture, and am building a lightweight, optimized alternative to ROS on ARM platforms.

Kybernetes Software:

https://github.com/Teknoman117/kybernetes

Kybernetes Linux Kernel:

https://github.com/Teknoman117/kybernetes-kernel

Work to bring the Beagleboard xM to full frequency under Linux 3.2.28 and beyond.

Well the owners and fans of the Beagleboard xM have a bit of a dilema when it comes to picking a version of Linux to run on their boards.  Many of you, including myself are quite annoyed that the big dog is clocked down when running a current version of Ubuntu or Angstrom.  For instance, the other day I installed the latest build of Ubuntu 12.04-r5 for armhf on my board and was quite unhappy to learn that the maximum frequency was capped at 800 MHz with the board being advertised as delivering a 1 GHz arm computer.  The SD card that ships with the board allows for this, but it uses an older version of the Linux kernel, 3.0.x.  The current stable is kernel 3.5.3.  Well, I don’t know if its just me, but I want to squeeze every last instruction per second that it will allow me.  So upon searching the internet for days, I came across this one blog:

http://blog.galemin.com/2012/03/buildroot-2012-02-for-beagleboard-xm-with-li-5m03-mt9p031-camera-support/

It provides a custom buildroot setup that for some camera hardware directly attached to the Beagleboard-xM’s camera capture lines.  But the thing I noticed mainly about it when I tried it was that the board was operating at 1 GHz, and on kernel 3.2.7.  So upon further inspection I found the patches that they were using to increase the top frequency.  So I forked Robert Nelson’s stable kernel repository, which can build one of the latest stable kernels, and I’ve personally tested it for Ubuntu and Angstrom.  It also includes the patches for the xM which keep the USB ports from shutting down randomly.  I integrated the patches into my fork and modified them a bit to fit into the later kernel, and after a little wait for building, I had Ubuntu 12.04 for armhf operating with the 3.2.28-x14 kernel.  You can find my repository for this at: (edit: Kybernetes uses the 3.5.7 kernel now, the repo reflects this update)

https://github.com/Teknoman117/kybernetes-kernel

However, before you get all excited, its not quite done yet.

Note #1 – Don’t use this on a Beaglebone or original beagleboard.  There is this patch enabled (0002-Fix-sprz319-erratum-2.1.patch) which fixes the xM’s usb problems.  However is will cause the other beagleboards not to boot at all.

Note #2 – I talked with Robert Nelson about my patches and without further work they could potentially damage your Beagleboard xM.  The automatic voltage scaling (Smart Reflex) does not work properly for the OMAP 3 on recent linux kernels, at least for the beagleboard.  The people over at TI and the people working on Angstrom have patches for this that make it completely safe to run at 1 GHz, however the patches are for Linux 3.0.x and there are over a hundred of them and many are hundred of lines long.  Some of them will probably patch into 3.2 properly, and some will definitely not.   So before this works, I am going to have to figure out what works and what doesn’t and integrate this into the kernel.  I will do this for kernel 3.4, because they have completely changed the AVS system in 3.6 entirely.  So if I am going to do this work, then I will do it for the current kernel version because otherwise it would be a waste.  So keep following because I am taking this on!

– Teknoman117