Sunday, June 29, 2014

BeagleBone Black and the LV-MaxSonar-EZ2 Range Finder



Now that I have a working robot, I decided that I wanted to make it autonomous.  After thinking about this a bit I decided that the next step would be to give the robot the ability to “sense” obstacles.  After a little research I found the LV-MaxSonar-EZ2 Range finder by Maxbotix.  This is a low cost sonar range finder that will detect obstacles and return the distance up to 254 inches away (this according to the specs).

You can find the datasheet for the LV-MaxSonar-EZ2 RangeFinder here.  The MaxSonar accepts an input voltage range of 2.5V to 5.5V and outputs the range in pulse width, analog voltage or serial digital output.  For the example here we will be providing 3.3V to the MaxSonar from pin 3 of the P9 expansion header and we will be taking the output from the analog voltage out.

The first thing we have to do is to solder some connecting wire to the MaxSonar so we can attach it to a breadboard.  This will allow us to prototype and test the range finder.




Now we need to connect the MaxSonar to the BeagleBone Black.  Here is how we wire everything:

This diagram was made with fritzing.  Now you do not have to look at my bad wiring to see how I connected everything. 

We connect a 1k ohm resistor to the analog voltage output pin on the MaxSonar.  Then connect a 3.3k ohm resistor in series with the 1k ohm resistor.  We then tie the 3.3k ohm resistor to ground.  Finally connect pin 40 of the P9 expansion header in series with the two resistors as shown in the diagram.

Now before we power everything up we need to determine mV per inch so we can calculate how far the obstacles are.   According to the datasheet, with 3.3V in should yield 6.4mV/in and according to this page we should get 4.57mV/in but neither were correct when I tested my MaxSonar.  I actually have two of the MaxSonars sensors and they both yielded the same results.  After determining that I was consistently getting results that were different from both sites, I went about determining what was correct in my test environment.  To determine mV per inch I measured the output from the MaxSonar for certain distances.  The results showed that I was getting 2.148mV/in.

Now lets write some code to test the MaxSonar range finder.  I will be using JavaScript/Bonescript here instead of Python because I really want to try to use Bonescript more to get a good feel for it.  Create a file called maxsonar.js and insert the following code:

var b = require('bonescript');

var sensorPin = "P9_40";

setInterval(read,3000);

function read() {
      b.analogRead(sensorPin,sensorStatus);
}

function sensorStatus(v) {
      var distanceIn;
      analogV = v.value*1.8;
      console.log('v.value = ' + analogV);
      distanceIn = analogV/0.002148;
      console.log("Object at " + parseFloat(distanceIn).toFixed(2) + " inches away");
}

We begin by setting our sensorPin to pin 40 of the P9 expansion header.  We then use the JavaScript function setInterval to call our read function every three seconds.  The read function calls the Bonescript’s analogRead function to read the output from the MaxSonar range finder and sets the callback function to the sensorStatus function.

The sensorStatus function is where we do all of our calculations.  We begin by converting the ADC to voltage by multiplying the value by 1.8.  We then convert that to inches by dividing the calculated voltage by the Volts per inch (notice we convert the 2.148 mV/in to .002148V/in).

We can run this code by running the following command: 

node maxsonar.js

Now every three seconds we should see the how close an object is to the MaxSonar range finder.  I would be interested in hear how what you find the mV per inch to be.  Is it closer to the other sites I mentioned or closer to what I found it to be?  I am much better at the programming and OS then I am with the electronics therefore if you spot anything that I may of done wrong on the electronic side to get the different mV per inch results, please leave a comment below.

The next step will be connecting the MaxSonar Range Finder to our robot and writing a quick test application that will let the robot move around avoid obstacles.  



2 comments: