Measuring velocity

Hi!

I would like to know what is the best way to measure velocity.

The Arduino and components are in movement, in a car. And without using the car's wheels encoder (or external sources).

I wrote the following code using accelerometer (7361). It is being used the X axis as forward direction, and I was thinking in using the Z axis (gravity) to calculate the vehicle's inclination and then
remove the gravity component on X axis when not in horizontal position.

#define pinx 14

void setup() {
pinMode(pinx, INPUT);
Serial.begin(9600);
}

unsigned long StartTime = millis();
unsigned long CurrentTime;
unsigned long ElapsedTime;
float velocityx = 0;

void loop() {
Serial.print("VELOCIDADE :");
Serial.println(velocityx);

int x = analogRead(pinx)- 516; //accel in x: raw reading minus offset
x = ((float)x/64)*980; //converting to cm/s^2
CurrentTime = millis();
ElapsedTime = CurrentTime - StartTime;
Serial.println(ElapsedTime);

if (abs(x) > 15) //Velocity will increase if the acceleration is more than 15cm/s^2 to avoid noises
velocityx += x*((float)ElapsedTime/1000);

StartTime = CurrentTime;

}

If I leave it in a table, it drifts 20m/s in an hour. (should be 0)
If I leave it with the X axis facing up, I get +-100m/s in 10 seconds (pretty good).
What could be improved?
I also have the MPU6050 available.
I was trying to avoid using GPS due the price, but if there is no other solution I will use it.

Thanks in advance

You don't say if 20m/s/hr is adequate for your application. I think that is a very good result. You are unlikely to get much better than this for under $1000.

Use the GPS. It is so much easier. The only reason not to would be if you did not have a clear vier of the sky.

If a GPS is too slow or consumes too much power, you could use the accelerometer for short time intervals and use a GPS to make corrections over time. If your using the analog accelerometer, I'd try out the other one before going to the GPS.

without using the car's wheels encoder (or external sources).

In practice, you can't.

Although you have the right idea, inexpensive accelerometers are too noisy to estimate velocity, as explained here.

The GPS method has the added advantage of giving you the ability to determine direction, which is something you have to have if you want to determine velocity, which is a vector. Or did you mean speed, a mere scalar?

I guess another option would be an ultrasonic sensor aimed down at a 45 degree angle looking for backscatter off the road - the doppler shift would give you speed you could calculate. You have to have some frame of reference and accelerometers, while possible have drift in them and it takes $$$$ to come up with some sort of inertial nav system (INS) that gives reasonable accuracy. Using the much cheaper consumer ones but being able to update/correct them periodically via GPS signal or something would work, but you need that periodic update to prevent your calculations from deciding after a period of time you are going faster than light ...

The old way was a "Correvit" which was a laser/dopper type system. Hang it on the side window looking at the ground and you got very accurate data.

These days, an optical mouse with bigger optics would do the same thing. It's still not a long-term solution as a speck of mud on the lens will disable it.

MorganS:
The old way was a "Correvit" which was a laser/dopper type system. Hang it on the side window looking at the ground and you got very accurate data.

These days, an optical mouse with bigger optics would do the same thing. It's still not a long-term solution as a speck of mud on the lens will disable it.

The first thing that comes to mind there would be to modify a VW "Beetle" - it even looks like a giant mouse - a little paint and a tail on it ... :slight_smile:

Your method wont work for more than a few seconds whilst the car is accelerating.

When the car has reached its maximum velocity, and the acceleration is now zero how are you going to calculate the velocity?

Thanks for all the replies!
After reading all the comments, I am very prone to use GPS, but the laser Doppler idea also sounds interesting (although I didn't find specific sensors for arduino).

MorganS:
You don't say if 20m/s/hr is adequate for your application. I think that is a very good result. You are unlikely to get much better than this for under $1000.

Use the GPS. It is so much easier. The only reason not to would be if you did not have a clear vier of the sky.

20m/s in one hour is not that good (72km/h after 1 hour), and I was not considering acceleration less than 2m/s^2 due noise. I think 2m/s^2 is too much for cars to be disregarded.
I think GPS will be easier indeed

MorganS:
The old way was a "Correvit" which was a laser/dopper type system. Hang it on the side window looking at the ground and you got very accurate data.

These days, an optical mouse with bigger optics would do the same thing. It's still not a long-term solution as a speck of mud on the lens will disable it.

Very interesting! Seems like it is called LSV (Laser Surface Velocity).

Do you know any sensor like this used with Arduino?
I found this Breakout for ADNS2620 Optical Mouse Sensor - BOB-10105 - SparkFun Electronics but these sensors are not good for speeds more than 12"/s

JohnLincoln:
Your method wont work for more than a few seconds whilst the car is accelerating.

When the car has reached its maximum velocity, and the acceleration is now zero how are you going to calculate the velocity?

The velocity is an accumulator, every iteration it is being added a small velocity component into the velocity accumulator.

velocity in X will be itself plus (acceleration * very small time interval)
velocityx += x*((float)ElapsedTime/1000);

When the acceleration is zero the velocity will be itself plus zero (will not change).

I don't know if you are acquainted with the "+=" operator
This:
"velocityx += x*((float)ElapsedTime/1000);"
is the same of this:
"velocityx = velocityx + x*((float)ElapsedTime/1000);"

It is very used in C, I don't know about other languages.

Thank you all again!

Just to clarify though, when you say "velocity" do you really mean velocity, a vector (speed and direction), or do you actually just mean "speed", a scalar.

hoshiro:
The velocity is an accumulator, every iteration it is being added a small velocity component into the velocity accumulator

What JohnLincoln was referring to, the problem is the accelerometer isn't perfect. Every little bit of inaccuracy in the accelerometer's readings adds up and, in a real world situation, your velocity calculations will be junk after a few seconds.

A top-of-the-line accelerometer like the MPU-6050 has about 97% accuracy. I think you can imagine how quickly that 3% error would add up.