hi. I am trying to measure speed with sharp IR distance sensor. But, I am unable to get accurate results. I am facing problems with taking time.I am using MRAA library and I am using putty with CPP coding.
# define VOLTS_PER_UNIT 0.0049
using namespace std;
a3= new mraa::Aio(3);
adc_value = a3 -> read();
volts = adc_value * VOLTS_PER_UNIT;
d = 60.495 * pow(volts, - 1.1904) ;
double timedifference_msec(struct timeval t0, struct timeval t1)
return (((t1.tv_sec - t0.tv_sec)*1000000) +(t1.tv_usec - t0.tv_usec))/1000;
double d1, d2,diff_dis;
struct timeval t0;
struct timeval t1;
d2 = distance();
t= (timedifference_msec(t0, t1))/1000;
diff_dis= abs(d1-d2);speed= diff_dis/t;
cout<< "Speed"<< speed<<"CM/Sec" <<"\n";
We would appreciate if you could answer the following questions.
I would also suggest you to check https://software.intel.com/en-us/articles/burglar-alarm-using-the-intel-galileo-board this guide which uses a similar proximity sensor, it can be helpful as a reference document (The Arduino board in the image should be interpreted as Galileo board).
I am using Sharp 2Y0A02 F distance senosr and mraa 0.8.1 devkit 2015.0.010 version. I connected sensor to analog pin 3( a3). Since the the Galileo runs slower than the arduino. I want to measure the exact time at each instant to get the accurate speed. right now I am not getting the exact time.
take care of some of your assumptions. The Galileo does not run slower than the Arduino. It runs much, much faster, but in a complete different way.
Arduino Uno or Mega : 8 bits processor running at 16MHz without operating system
Galileo : 32 bits processor running at 400MHz with Linux
You have to understand that the Galileo does not read the ADC in the same manner as the Arduino (it looks the same from programmer point of view, because of the library, but under the hood, it's completely different)
On the Arduino, the ADC output is a simple register that is read directly by your program without any external access by the CPU core using the internal databus. It takes one or two CPU cycles and it's done.
On the Galileo, the ADC is read over the SPI. So it takes time to retrieve the data, since it's a serial communication scheme. Moreover, the SPI communication interface can not be accessed directly from your code, because Linux is in the game, and it takes care of possible concurrent access (Linux is multitasking, do not forget it). The Linux kernel makes the SPI interface appear as a file for the library. That means that any attempt to read an analog input is in fact : opening a "file" (every device is a file with Linux), send a command, wait for the answer, close the file. This is extremely fast even if it appears as heavy to do. The problem is that you do not control when the Linux kernel will really read the analog value.
That's probably where you have a problem (I do not know the sensor you use, so I can not guarantee that it is the explanation). If you need to measure precisely the time difference between two analog inputs, the Linux kernel will add some uncontrolled jitter and you will get false readouts. If it's the case with this sensor, then you will need to use a AVR based Arduino, on which there is no operating system and on which you can control very tightly the timing
Just as BenKissBox said, the conditions working with an Arduino board and an embedded platform running Linux or any other OS are quite different. However, among the things that you can try to improve your results would be to update mraa, update 0.9.0 is already available.