среда, 8. фебруар 2012.

Arduino and Android parking assistance


Back to work gangsters,

don't fasten your seatbelts so quick, we have a product that will garantie safety without them :)

After a long time when we published last news about Arduino + Android programming, we decided to refresh content on this blog. Now, Assignment was to make hardware configuration which measures distance from barrier, and show distance on Android device.

Hardware components which are included into solution are:

1) Arduino Pro chip
2) Long Range Infrared Sensor: Sharp serial no. GP2Y0A02YK0F
3) Bluetooth module - Bluetooth Mate Silver (described in post no.2).
4) Devices that use Android OS (Asus Eee Pad Transformer TF101 to work on it and test software solution during project).

Physical computing, in the broadest sense, means building interactive physical systems by the use of software and hardware that can sense and respond to the analog world. While this definition is broad enough to encompass things such as smart automotive traffic control systems or factory automation processes, it is not commonly used to describe them. In the broad sense, physical computing is a creative framework for understanding human beings' relationship to the digital world. In practical use, the term most often describes handmade art, design or DIY hobby projects that use sensors and microcontrollers to translate analog input to a software system, and/or control electro-mechanical devices such as motors, servos, lighting or other hardware.

How look like this implemented parking assistant sistem see the video at the following references:





Hardware component that we want to talk about in this post is Long Range Infrared Sensor Sharp (serial no. GP2Y0A02YK0F).

Long Range Infrared Sensor Sharp - GP2Y0A02YK0F

               Long Range Infrared Sensor Sharp

HW connection infrared sensor Sharp with Arduino microcontroller




This long range infrared sensor can calculate the distance from objects up to 150cm away(roughly 5ft). It outputs an analog voltage corresponding to the distance that varies from roughly 2.8V at 15cm to 0.4V at 150cm. This value would be read by the Arduino using the analogRead()function.

Now, while this sensor is quite accurate between the range of 15-150cm (0.5-5ft), it does have two drawbacks:
  • The minimum cutoff for an accurate reading is 15cm, so any reading closer than that will be garbage
  • The voltage output over distance is not linear (more of a power function)


If your project requires checking for a distance closer than 15cm, there are short and mid-range versions available. If you don't want to sacrifice distance you can always just use more sensors in combination with the long range IR to cover it's "blind spot". Another way to compensate for the minimum distance would be to just place the sensor 6 inches behind the front of your robot.



As for the non-linearity, this just means that we'll have to throw together a nifty little algorithm to convert the sensor's output voltage into distance. Thankfully, Sharp has already taken care of the hard part and provided us with a chart of output values (as seen in the graph above). All we have to do is convert the analog voltages into a 10-bit digital value and determine a best-fit line equation.

Fancy graph drawn up in Excel


By placing the data values into Excel, I got the following graph. The graph most closely resembled a Power function, so the best-fitting line equation was y = 1735 * x ^ (-1.07), with an R^2 value of 0.997. Not bad. You may have noticed that the graph starts at 20 cm instead of 15 cm. I decided to sacrifice an extra 5 cm on the minimum distance in order to have a more accurate algorithm. Since I'll be using the sensor to make sure nothing comes within about 15 cm (6 in) of my robot, this won't be a problem. Also note that the algorithm doesn't take into account the x axis values, so in the equation x = 20 cm would be equivalent to x = 3... just make sure that you take that into consideration if your making an equation this way.
So, what do we do with this equation? Right now it doesn't help us at all since we are reading in y (voltage)   and want to solve for x (distance). We could bust out some scratch paper and a calculator and solve for x, or we could use the All Mighty WolframAlpha.



So, taking into account the oddities of my graph, our equation to calculate the distance in cm is:

                                                                y = 10650.08 * x ^ (-0.935) - 10

To use the algorithm in your code, you need to use the pow() function. So, in your Arduino sketch it would look something like this:

cm = 10650.08 * pow(sensorValue,-0.935) - 10;

In order to test our algorithm, I taped my IR sensor to a tape measure and used a white sheet of printer paper as the test object.

Arduino code with bluetooth configure code in setup() function
#include <NewSoftSerial.h> 

int bluetoothTx = 2;  // TX-O pin of bluetooth mate, Arduino D2
int bluetoothRx = 3;  // RX-I pin of bluetooth mate, Arduino D3

#define sensorIR 15              
float sensorValue, inches, cm;    //Must be of type float for pow()
boolean connection = false;

NewSoftSerial bluetooth(bluetoothTx, bluetoothRx);

void setup()
{
  Serial.begin(9600);  // Begin the serial monitor at 9600bps
  bluetooth.begin(115200);  // The Bluetooth Mate defaults to 115200bps
  bluetooth.print("$$$");  // Enter command mode
  delay(100);  // Short delay, wait for the Mate to send back CMD
  bluetooth.println("U,9600,N");  // Tempora rily Change the baudrate to 9600, no parity
  // 115200 can be too fast at times for NewSoftSerial to relay the data reliably

  bluetooth.begin(9600);  // Start bluetooth serial at 9600
  bluetooth.print("$$$");
  delay(100);
  bluetooth.println("w"); // awake bt module
  delay(1000);
  bluetooth.println("---");
}

void loop()
{

  if(bluetooth.available())  // If the bluetooth sent any characters
  {
    if ((char)bluetooth.read() == 'c')
    {
      connection = true;
      Serial.println("c");
    }
  }
  
  if (connection)
  {
    sensorValue = analogRead(0);
    inches = 4192.936 * pow(sensorValue,-0.935) - 3.937;
 
    cm = 2.54 *inches;
   
     Serial.println(cm);
     bluetooth.println(cm);
     delay(100);
     connection=false;
  }
  
}



 The algorithm works surprisingly well and, without interference, is easily accurate within the inch. The sensor starts at approximately 19cm so anything closer than that should be considered garbage data.

The only problem with the sensor is that it is susceptible to noise on the power supply line and needs to be perpendicular to the object for an accurate reading. Being an analog sensor, the output value is going to be dependent on the supply voltage. If you aren't supplying a constant voltage, the numbers are going to fluctuate and if your voltage starts to dip under 5V, you wont get the full 5 ft range. You may need to add an extra 0.1uF cap at the power supply or maybe average every few readings to smooth out the data.




Right now the code doesn't have a cutoff for minimum and maximum distances, but this would be an easy exercise to see if you've been paying any attention. The sensor should be able to read any value inside of roughly 7.5 - 60 inches if you're using this code.

If we want to configure alone this Bluetooth Mate Silver module, download RN-42Bluetooth .pdf file on next web address: http://www.sparkfun.com/datasheets/Wireless/Bluetooth/rn-bluetooth-um.pdf 

In next post, we’ll upload more information about Android application and how we implemented  software for Parking assistance application. For now we can see how look like Android app interface.

CU soon M&M




7 коментара:

  1. i hope this will be useful on the parrot drone :-)

    ОдговориИзбриши
    Одговори
    1. I hope so.
      If you want to share information about parrot drone, please publish some link about it?

      Избриши
  2. AR.Drone Parrot

    http://www.youtube.com/watch?v=ht_seyL-quo
    http://ardrone.parrot.com/parrot-ar-drone/usa/ar.free-flight-for-android

    ОдговориИзбриши
  3. So you beat me to it , but i'll be glad that i will have someone to assist me in parrot and kinect communication programming :-)

    ОдговориИзбриши
  4. Hello, i'm working with this sensor Sharp, and I don't understand some steps. Can you help me?

    when you have y = 1735 * x ^ (-1.07) and you use wolframe site to solve in order to x, the result y = 1065.08 * x ^ (-0.935) is different of the equation you use it y = 10650.08 * x ^ (-0.935) - 10. Can you explain to me please that step? where came 10? and why 1065.08 pass to 10650.08?

    best regards
    Daniel Pereira

    ОдговориИзбриши
  5. Hi,

    i don't remeber what i did to solve this, it's been a long time since then. I used MathLab to see dependence between my and their functional graph in datasheet.

    But you can solve this problem much better and faster. The power function is very expensive and need a lot of time to execute. The easier way to do this:
    make an array or matrix and measure analog signal (voltage) and change with the second element in array.

    This means that it will map input voltages between 0 and 5 volts into integer values between 0 and 1023. This yields a resolution between readings of: 5 volts / 1024 units or, .0049 volts (4.9 mV) per unit. Therefore 5v/1024 = 0.0048828125 volts for 1 digital value. Then you do next
    0.0048828125*digital value (from 0 till 1023)=> analog value, then this value change with second part in array.

    Voltage Centimeters
    2.75 15
    2.55 20
    2 30
    1.55 40
    1.25 50
    1.15 60
    .9 70
    .8 80
    .75 90
    .65 100
    .6 110
    .55 120
    .5 130
    .455 140
    .45 150

    ОдговориИзбриши
  6. Анониман7. мај 2022. 06:16

    Sample Assignment provides Arduino Assignment Help services in Australia. Students from every field of academics can contact Sample Assignment to make their assignments in an error-free format.

    ОдговориИзбриши