Showing posts with label fritzing. Show all posts
Showing posts with label fritzing. Show all posts

Saturday, July 19, 2014

Sensors, Sensors Everywhere

We got back from vacation and what was waiting for us?  Our four new LV-MaxSonar-EZ2 Range Finders to give us a total of five sensors for our robot.  This will give us one looking forward, two at 45 degrees and two at 90 degrees.  The pictures below show how we mounted them.  You may also notice the nice table that our robot is sitting on.  Kailey, my eight-year-old daughter, made it for me this afternoon. 





Below is the diagram of how I wired all of the sensors.  For the actual wiring I have one of the sensors wired on a second breadboard because I did not have enough room to put all five on one small breadboard and I also had an extra breadboard so why try to cram it all on one.




Now you may be asking, what can we do with all those sensors?  One project is going to be to expand on our autonomous robot that we showed in this post and give the robot a much broader view of the world.  The other project we want to do is to have the robot send the sensor readings back to a controller so we can control the robot when we are not in the same room as the robot.  This second project would be useful if the robot was exploring someplace where we could not go like a different planet or small caverns.  I also want to eventually connect a camera to the robot and stream video back.


My concern with the second project is how will we communicate back with the controller.  In some earlier projects I used Bluetooth but I am thinking in the long term that Bluetooth is not going to be the answer for my needs.   I was thinking about adding a WIFI USB adapter however I think the power drain will be too much when I connect the robot to the EasyAcc power bank.  So after thinking about the problem quite a bit and doing a lot of reading, I decided to order a Spark Core.  I ordered it from MakerShed and should be here this week.  The Spark Core has WIFI built in and I can have it act as my communication module that will relay commands from the controller to the BeagleBone Black.  This should also allow us to use our iPhone/iPad to control our robot. 

I am still debating on the language to use for our robot.  If I can offload the communication piece from the BeagleBone to the Spark Core, I will probably use Javascript/Bonescript but I am still considering Python.

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.