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();
Related
I am trying to calculate the distance between two locations (Current location with the previous location).
So I tried the following:
Location previousLocation = new Location("");
previousLocation.setLatitude(sharedPreferences.getFloat("previousLatitude", 0f));
previousLocation.setLongitude(sharedPreferences.getFloat("previousLongitude", 0f));
float distance = location.distanceTo(previousLocation);
totalDistanceInMeters += distance;
editor.putFloat("totalDistanceInMeters", totalDistanceInMeters);
Log.e("Location Update","totalDistance"+totalDistanceInMeters);
if (totalDistanceInMeters > 1)
{
Toast.makeText(getApplicationContext(), "Total UpdateLocation"+totalDistanceInMeters/1609, Toast.LENGTH_SHORT).show();
Log.e("Alert","Update");
}
To test the above code. The first time result was perfect and when it triggered the second time. The phone was in the same location but I am getting distances like 141.0111 m. thrid time 304.0011 m. Am I doing something wrong here?
The results are not showing up correctly. According to doc online the results are in metres.
Is there an easy way to calculate the difference between the first location results with the second one and if it is more than 10m I would like to do some other calculation if not just keep quite.
Let me know.
why are you even using the following code
float distance = location.distanceTo(previousLocation);
totalDistanceInMeters += distance;
That adds up the previous distance to present distance and gives the added value everytime....
example
first time
distance between A and B is 100 m
second time
distance between A and B is 100 m+100 m=200 m
so try using distance directly in toast
float distance = location.distanceTo(previousLocation);
if (totalDistanceInMeters > 1)
{
Toast.makeText(getApplicationContext(), "Total UpdateLocation"+distance/1609,Toast.LENGTH_SHORT).show();
Log.e("Alert","Update");
}
I think accuracy needs to be set in order to get the exact distance. Also try to get hold of the previous and current locations manually so as to calculate the distance and verify.
I am working on an application which requires me to manually handle the fling process rather than giving it to the framework. What I want to achieve is basically calculate the amount of pixels a listview moves when it receives a fling action. As the scroll method already provides distance in form of delta, I have handled it easily. But is there a way to get fling distance as only velocity parameter is being passed in the super method.
Note- I have to move another view in accordance with the fling distance, so I need to get it simultaneously just like onScroll provides it.
Thanks.
It is passed 3 years but no answer yet. I found some workaround to achieve it.
Actually it is kind of advanced topic as there are a lot of nuances but basically you can refer to Android source code(OverScroller class in particular) and use this method. You will need to copy it into your class and use it.
private double getSplineFlingDistance(int velocity) {
final double l = getSplineDeceleration(velocity);
final double decelMinusOne = DECELERATION_RATE - 1.0;
return mFlingFriction * PHYSICAL_COEF * Math.exp(DECELERATION_RATE / decelMinusOne * l);
}
Other methods and values can be obtained from the same class.
The link to the source code: https://android.googlesource.com/platform/frameworks/base/+/jb-release/core/java/android/widget/OverScroller.java
Keep in mind that in some devices the value can be different (not too much). Some vendors change the formula depending on their requirements and hardware to make it more smooth.
It looks like the original question ended up with nothing, but it was formulated pretty good, so I landed here and started my research. Here are my results.
My question was: What is the final value at the end of Android standard FlingAnimation?
new FlingAnimation(new FloatValueHolder(0f))
.addEndListener((animation, canceled, value, velocity) -> {
? value
I needed that value before animation start based on the start velocity to make some preparations at the destination point of the FlingAnimation.
Actually I started with Overscroller.java mentioned by #Adil Aliyev. I collected all the portions of code, but the result was way less, that came from the animation.
Then I took a look into FlingAnimation.java in pair with DynamicAnimation.java.
The key function in FlingAnimation.java to start the research was:
MassState updateValueAndVelocity(float value, float velocity, long deltaT) {
After playing with some equations I composed this final code. It gives not totally exact estimation to the last digit, but very close. I will use it for my needs. You are welcome too:
final float DEFAULT_FRICTION = -4.2f;
final float VELOCITY_THRESHOLD_MULTIPLIER = 1000f / 16f;
float mFriction = 1.1f * DEFAULT_FRICTION; // set here friction that you set in .setFriction(1.1f) or 1 by default
final float THRESHOLD_MULTIPLIER = 0.75f;
float mVelocityThreshold = THRESHOLD_MULTIPLIER * VELOCITY_THRESHOLD_MULTIPLIER;
double time = Math.log(mVelocityThreshold / startVelocity) * 1000d / mFriction;
double flingDistance = startVelocity / mFriction * (Math.exp(mFriction * time / 1000d) - 1);
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
}
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;
}
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.