find out speed of android device using GPS - android

In my application I am retriving speed of a device using getSpeed() method of Location class.
It gives speed in meters/sec. To convert this value to km/hr I am multiplying this with 3.6. Then storing it in sqlite database. But I am getting wrong speed. ex. 580 when it is moving with speed of 40km/hr. This is my code:
float mps = location.getSpeed();
float kmh = (float) (mps*3.6);
String speed = String.valueOf(kmh);
Toast.makeText(getBaseContext(),"speed: "+speed,Toast.LENGTH_LONG ).show();
Where is It going wrong. I have googled for this but could find the same method.

Related

How to detect device is moving or not using sensor android

I am using below code to identify the movement of the device, means I would like to know that device is moving or not. I also use Google Activity APIs which provides different activity modes like WALKING, ON_FOOT, STILL, etc without using GPS. I would like to achieve the same with Sensors but I am not able to get it accurately.
The issue with the following code is that as soon as I move the device quickly like take it from the table then I am getting the result as moving whereas it's not actually moving.
// calling method from onSensorChanged method and using TYPE_ACCELEROMETER sensor.
double speed = getAccelerometer(event.values);
// then checking the speed.
if(speed > 0.9 && speed < 1.1) {
// device is not moving
} else {
// device is moving.
}
/**
* #return
*/
private double getAccelerometer(float[] values) {
// Movement
float x = values[0];
float y = values[1];
float z = values[2];
float accelerationSquareRoot =
(float) ((x * x + y * y + z * z) / (9.80665 * 9.80665));
return Math.sqrt(accelerationSquareRoot);
}
Can anyone guide me how to make this logic accurate so that I can identify the device is moving or not?
The accelerometer is made to return acceleration data and according to Netwon's 2nd law if the acceleration is constant then the body is not moving or moving with constant speed(this is quite impossibile in your case).
Therefore if you keep reading the same data on all three axis(or better in a quite strict range) from accelerometer over time it means the phone is not moving otherwise it is.
For the purpose, you need to use Activity Recognition API which will provide you some events like moving, stop, driving, e.t.c, And activity recognize use some sensor data and also help of location service when is running. For the more how we can use and what actually it. You can read from below link
https://developers.google.com/location-context/activity-recognition/

Is it possible to track acceleration and speed using gps data?

Is it possible to track the speed and the acceleration through an android app using the GPS data it provides? I'm planning to create a react native app with these features. But I want to know whether it's possible to do it with the data we can get. Even in native android?
You can do it in pure JS.
For the speed part you have the core React Native Geolocation API where you can find the method watchPosition. On each position update, an object is returned with the current speed amongst other attributes (location, altitude...).
For the acceleration part you have the react-native-motion-manager library. You can listen to the accelerometer updates and get the acceleration.
Is it possible to track the speed and the acceleration through an
android app using the GPS data it provides?
Answer: yes
location.getSpeed() only returns what was set with
location.setSpeed(). This is a value that you can set for a location
object.
To calculate the speed using GPS, you'll have to do a little math:
Speed = distance / time
so how to do that
(currentGPSPoint - lastGPSPoint) / (time between GPS points)
Each location provided by all LocationProviders has the instant speed at the point where the location was taken, which is unstable and it can differ between devices. The average speed is more precise, you have to calculate distance and time. This is how I calculate distance between 2 locations:
static double distance (Location in1, Location in2) {
double R = 6371000;
double la1 = in1.getLatitude()* Math.PI/180;
double la2 = in2.getLatitude()* Math.PI/180;
double lo1 = in1.getLongitude()* Math.PI/180;
double lo2 = in2.getLongitude()* Math.PI/180;
double tmp1 = Math.sin((la1-la2)/2)*Math.sin((la1-la2)/2) + Math.cos(la1)*Math.cos(la2) * Math.sin((lo1-lo2)/2) * Math.sin((lo1-lo2)/2);
double tmp2 = Math.sqrt(tmp1);
double d = Math.abs(2 * R * Math.asin(tmp2) * 100000) / 100000;
return d;
}
You can use this function as well, but I'd rather use the other one, which stores the result in "results" :)
Location.distanceBetween(in1.getLatitude(),in1.getLongitude(),in2.getLatitude(),in2.getLongitude(),results);
So getting the speed in m/s (that's the 1000* for) is quite straightforward:
double avgSpeed = 1000 * distance(loc1,loc2) / (loc2.getTime()-loc1.getTime)

Can't get Speed Value properly - Casting does not work

I am currently attempting to get a person's speed through the use of GooglePlayServices.
I've managed to connect to the Services and get updates on the location, but the problem is on working out the speed for the person in question - that is, I am having trouble dividing the time and distance.
I have posted this previously, and someone suggested that casting long would fix the problem. It was quickly marked duplicate, so I didn't get to continue the discussion, but I wanted to say it did not work.
The below is my current code.
float Distance = OldLocation.distanceTo(location);
//Getting Difference in seconds
long TimeDiff = location.getTime()-OldLocation.getTime();
float SecondDiff = TimeUnit.MILLISECONDS.toSeconds(TimeDiff);
//Finally working out speed (m/s)
if (SecondDiff == 0) {
SecondDiff = 1;
}
float Speed = ((long)Distance/SecondDiff);
Speed keeps on resulting on 0, rather than the proper value. I've cast the long on it, but it refused.
I've also tried:
float Speed = ((long)Distance/(long)SecondDiff);
and
float Speed = (long)(Distance/SecondDiff);
What could be the source of my problem here?
Why don't you just get the speed from every location object by the getSpeed() method, like:
float speed = location.getSpeed();

Check if device is really moving

I am working on an application where I need to get the speed of a car. To get the speed, I know I can use something like double speed =locationB.getSpeed();` however when I am testing, the speed varies between 0.0 and 40 km/h when I am just sitting right behind my laptop not moving at all. In the car, the speed actually comes close to the cars speed, so that shouldn't be a problem.
What would be the best way to check if the device is really moving? I've already tried to get the distance between locationA and locationB and use that with the time it took to get the 2 locations, to get the speed.
double distance = locationA.distanceTo(locationB);
double speed = (distance / time) * 3600 / 1000;
However this seems to be not stable at all, like the getSpeed() method.
Is there a way to only display the speed if the device is moving? And would it be reliable?
Any help is appreciated,
Thanks.
Check the horicontal accuracy attribute of Location.
If it is under 30m you can ignore the location.
If you are sitting on your laptop and get speed = 40km/h (which I never saw in good GPS devices), then look what the hor. accuracy is.
It probably is much over 30m.
In GPS based systems, never ever calculate the speed by positional change in time,
just use the location.getSpeed().
The reason is that the GPS chip internally calculates the speed via physical doppler effect, not via positional change.
While standing still, or at very low speeds this does not work well, so you have to filter out very low speeds, and bad gps signal. (via horicontal accuracy estimate)
I think you should limit the distance between A and B to be a minimum length. Small distances will introduce more error into your speed calculations.
Boolean moving - false;
double distance = locationA.distanceTo(locationB);
double speed = (distance / time) * 3600 / 1000;
if (distance > SOME_THRESHOLD) {
moving = true
}

Approximate indoor positioning using the integration of the linear acceleration

I am trying to calculate the approximate position of an Android phone in a room. I tried with different methods such as location (wich is terrible in indoors) and gyroscope+compass. I only need to know the approximate position after walking during 5-10seconds so I think the integration of linear acceleration could be enough. I know the error is terrible because of the propagation of the error but maybe it will work in my setup. I only need the approximate position to point a camera to the Android phone.
I coded the double integration but I am doing sth wrong. IF the phone is static on a table the position (x,y,z) always keep increasing. What is the problem?
static final float NS2S = 1.0f / 1000000000.0f;
float[] last_values = null;
float[] velocity = null;
float[] position = null;
float[] acceleration = null;
long last_timestamp = 0;
SensorManager mSensorManager;
Sensor mAccelerometer;
public void onSensorChanged(SensorEvent event) {
if (event.sensor.getType() != Sensor.TYPE_LINEAR_ACCELERATION)
return;
if(last_values != null){
float dt = (event.timestamp - last_timestamp) * NS2S;
acceleration[0]=(float) event.values[0] - (float) 0.0188;
acceleration[1]=(float) event.values[1] - (float) 0.00217;
acceleration[2]=(float) event.values[2] + (float) 0.01857;
for(int index = 0; index < 3;++index){
velocity[index] += (acceleration[index] + last_values[index])/2 * dt;
position[index] += velocity[index] * dt;
}
}
else{
last_values = new float[3];
acceleration = new float[3];
velocity = new float[3];
position = new float[3];
velocity[0] = velocity[1] = velocity[2] = 0f;
position[0] = position[1] = position[2] = 0f;
}
System.arraycopy(acceleration, 0, last_values, 0, 3);
last_timestamp = event.timestamp;
}
These are the positions I get when the phone is on the table (no motion). The (x,y,z) values are increasing but the phone is still.
And these are the positions after calculate the moving average for each axis and substract from each measurement. The phone is also still.
How to improve the code or another method to get the approximate position inside a room?
There are unavoidable measurement errors in the accelerometer. These are caused by tiny vibrations in the table, imperfections in the manufacturing, etc. etc. Accumulating these errors over time results in a Random Walk. This is why positioning systems can only use accelerometers as a positioning aid through some filter. They still require some form of dead reckoning such as GPS (which doesn't work well in doors).
There is a great deal of current research for indoor positioning systems. Some areas of research into systems that can take advantage of existing infrastructure are WiFi and LED lighting positioning. There is no obvious solution yet, but I'm sure we'll need a dedicated solution for accurate, reliable indoor positioning.
You said the position always keeps increasing. Do you mean the x, y, and z components only ever become positive, even after resetting several times? Or do you mean the position keeps drifting from zero?
If you output the raw acceleration measurements when the phone is still you should see the measurement errors. Put a bunch of these measurements in an Excel spreadsheet. Calculate the mean and the standard deviation. The mean should be zero for all axes. If not there is a bias that you can remove in your code with a simple averaging filter (calculate a running average and subtract that from each result). The standard deviation will show you how far you can expect to drift in each axis after N time steps as standard_deviation * sqrt(N). This should help you mathematically determine the expected accuracy as a function of time (or N time steps).
Brian is right, there are already deployed indoor positioning systems that work with infrastructure that you can easily find in (almost) any room.
One of the solutions that has proven to be most reliable is WiFi fingerprinting. I recommend you take a look at indoo.rs - www.indoo.rs - they are pioneers in the industry and have a pretty developed system already.
This may not be the most elegant or reliable solution, but in my case it serves the purpose.
Note In my case, I am grabbing a location before the user can even enter the activity that needs indoor positioning.. and I am only concerned with a rough estimate of how much they have moved around.
I have a sensor manager that is creating a rotation matrix based on the device orientation. (using Sensor.TYPE_ROTATION_VECTOR) That obviously doesn't give me movement forward, backward, or side to side, but instead only the device orientation. With that device orientation i have a good idea of the user's bearing in degrees (which way they are facing) and using the Sensor_Step_Detector available in KitKat 4.4, I make the assumption that a step is 1 meter in the direction the user is facing..
Again, I know this is not full proof or very accurate, but depending on your purpose this too might be a simple solution..
everytime a step is detected i basically call this function:
public void computeNewLocationByStep() {
Location newLocal = new Location("");
double vAngle = getBearingInDegrees(); // returns my users bearing
double vDistance = 1 / g.kEarthRadiusInMeters; //kEarthRadiusInMeters = 6353000;
vAngle = Math.toRadians(vAngle);
double vLat1 = Math.toRadians(_location.getLatitude());
double vLng1 = Math.toRadians(_location.getLongitude());
double vNewLat = Math.asin(Math.sin(vLat1) * Math.cos(vDistance) +
Math.cos(vLat1) * Math.sin(vDistance) * Math.cos(vAngle));
double vNewLng = vLng1 + Math.atan2(Math.sin(vAngle) * Math.sin(vDistance) * Math.cos(vLat1),
Math.cos(vDistance) - Math.sin(vLat1) * Math.sin(vNewLat));
newLocal.setLatitude(Math.toDegrees(vNewLat));
newLocal.setLongitude(Math.toDegrees(vNewLng));
stepCount =0;
_location = newLocal;
}

Categories

Resources