When building a robot one needs to use sensors. I used sonar sensors before for this post:
The main idea is to get rid of the Arduino on our rover “Scarabeus” and use the sonar sensor directly on the BeagleBone. The problem is that this sensor needs a 5V pulse to trigger ultrasonic output and it also answers with a pulse according to the received incoming signal but all the GPIOs on the BeagleBone operate at 3.3V. There are several possible ways to solve this and I’m presenting two of them here.
- Do some electronic hack and apply the sensor directly onto the BeagleBone.
- Use a dedicated 5V chip to handle the sensor and communicate via serial port (uart) to the BeagleBone.
Sonar sensor directly on the BeagleBone
So we have the main issue of voltage levels. It’s a simple task for an electronics guy so it’s clear that I’m not talking about myself 🙂 This problem was solved by Tamás Csibrák. Schematics: And fancy implementation in SMD size:
So far so good. I had to realize that even though the BeagleBone has lot of libraries (and 2 especially nice ones) implementing an Arduino-style hardware interface they still all lack the pulseIn() function.
So in order to have something like this I checked which existing library is more rich and I chose PyBBIO as my target of expanding. Here’s how it looks like:
def pulseIn(gpio_pin, value, timeout=23200): """ pulseIn by Bence Magyar. """ """ timeout default value represents 400 cm sonar distance signal length. """ """ Returns length of chosen signal in microseconds. """ assert (gpio_pin in GPIO), "*Invalid GPIO pin: '%s'" % gpio_pin assert (value in [HIGH, LOW]), "*Invalid value parameter: '%d'" % value endSig = value startSig = LOW if value==HIGH else HIGH start = micros() while digitalRead(gpio_pin) == startSig and (micros()-start) < timeout: delayMicroseconds(30) if micros()-start > timeout: return timeout start = micros() while digitalRead(gpio_pin) == endSig and (micros()-start) < timeout: delayMicroseconds(30) return micros() - start
Notes: The Python code above is not performing really well with the timing so because of this the measured distances are not really precise and the usable range of the sensor is limited. To solve this I’m planning to try a C++ implementation.