1

I'd like to use my accelerometer in my car and with the accelerometer values, draw a trajectory in excel or any other platform with the origin in the first position value, that is the beginning of the path.

How can I achieve this? Please give me details I don't have any physics notion.

Please help, thanks in advance.

PS: I already programmed the SensorListener...

I have this for instance:

@Override
public void onSensorChanged(SensorEvent event){
    if(last_values != null){
        float dt = (event.timestamp - last_timestamp) * NS2S;
        for(int index = 0 ; index < 3 ; ++index){
            acceleration[index] = event.values[index];
            velocity[index] += (acceleration[index] + last_values[index])/2 * dt;
            position[index] += velocity[index] * dt;
        }
        vxarr.add(velocity[0]);
        vyarr.add(velocity[1]);
        vzarr.add(velocity[2]);
        axarr.add(acceleration[0]);
        ayarr.add(acceleration[1]);
        azarr.add(acceleration[2]);
    }
    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;
    }
    xarr.add(position[0]);
    yarr.add(position[1]);
    zarr.add(position[2]);
    tvX.setText(String.valueOf(acceleration[0]));
    tvY.setText(String.valueOf(acceleration[1]));
    tvZ.setText(String.valueOf(acceleration[2]));
    last_timestamp = event.timestamp;
}

but when I draw a circle with my phone I got this:

enter image description here

Sometimes I have just only negative values and sometimes I have just positive values, I never have negative AND positive values in order to have circle.

Roman Panaget
  • 1,578
  • 12
  • 21
  • I'm not sure I understand how your code and the image are related, but anyway: maybe this [link](http://developer.android.com/reference/android/hardware/SensorEvent.html#values) will help. – Bö macht Blau Feb 15 '16 at 14:51
  • I just draw xarr in X axis and yarr in Y axis because xarr and yarr are supposed to gve me the path followed by the phone, and apparently the don't – Roman Panaget Feb 15 '16 at 15:02
  • The link doesn't help, i read this a thousamd times to make my SensorListener – Roman Panaget Feb 15 '16 at 15:03
  • Sorry about that. One new thing for me certainly was the fact that a device which is lying still has a positive accelaration. Needs getting used to, but makes sense from a more universal perspective ;) OK, I'll look into the problem and be back as soon as I've found out something. – Bö macht Blau Feb 15 '16 at 15:06
  • Looks like you never update *last_values*. Should probably be the last acceleration value on that axis? – Bö macht Blau Feb 15 '16 at 15:12
  • Can you color the dots depending on the time when the position was measured? (depending on the index of xarr/ yarr)? By the way, I think it's entirely possible for a circular movement to produce a one-line-kind of graph, if you look at the circle precisely from its side. – Bö macht Blau Feb 15 '16 at 15:37
  • So, you're absolutely right about last_values, I have corrected it but seems like the problem persists. Well, I don't how to color the dots but I can tell you that the first dot is the (0,0) one and the lastest is the farest from.the origin (values don't come back). – Roman Panaget Feb 15 '16 at 15:40
  • And thus, how can i get a circle? – Roman Panaget Feb 15 '16 at 15:41
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/103500/discussion-between-0x0nosugar-and-roman-panaget). – Bö macht Blau Feb 15 '16 at 15:44
  • @RomanPanaget I', wondering if you ever found a solution to this? –  Feb 25 '17 at 16:09

2 Answers2

2

Acceleration is the derivative of speed by time (in other words, rate of change of speed); speed is the derivative of position by time. Therefore, acceleration is the second derivative of position. Conversely, position is the second antiderivative of acceleration. You could take the accelerometer measurements and do the double intergration over time to obtain the positions for your trajectory, except for two problems:

1) It's an indefinite integral, i.e. there are infinitely many solutions (see e.g. https://en.wikipedia.org/wiki/Antiderivative). In this context, it means that your measurements tell you nothing about the initial speed. But you can get it from GPS (with limited accuracy) or from user input in some form (e.g. assume the speed is zero when the user hits some button to start calculating the trajectory).

2) Error accumulation. Suppose the accelerometer error in any given direction a = 0.01 m/s^2 (a rough guess based on my phone). Over t = 5 minutes, this gives you an error of a*t^2/2 = 450 meters.

So you can't get a very accurate trajectory, especially over a long period of time. If that doesn't matter to you, you may be able to use code from the other answer, or write your own etc. but first you need to realize the very serious limitations of this approach.

biggvsdiccvs
  • 299
  • 3
  • 12
  • If initial conditions (i.e. values for position and velocity) are given, then the solution for the differential equation exists and is unique. Having said that, I absolutely agree with you that the result will not be very exact under "real life" conditions, and so one would better use GPS instead. – Bö macht Blau Feb 18 '16 at 18:10
  • I specifically mentioned the initial conditions. – biggvsdiccvs Feb 18 '16 at 22:19
  • Yes, you did. And you suggested a method how to get initial values. I did not miss that, but somehow I think many people may. Because I know what to look for. Now others who don't might stop paying attention soon after "indefinitely many solutions (see..." plus some link to a mathematical topic. So I just wanted to clarify, sorry that it can be read as contradicting what you wrote. And I repeat, your contribution to this problem is very useful. I'm sure there are some issues with the code which can be fixed. But as you say: the approach as such has serious limitations. – Bö macht Blau Feb 19 '16 at 07:00
  • For instance, OP evaluates all event.values without checking the event type. But as OP seems to have lost interest I'm wondering whether to include this in my post or to delete my post altogether. Half of it is not about programming but about mathematics. An interesting puzzle, but maybe off topic. – Bö macht Blau Feb 19 '16 at 07:06
  • Don't delete it, someone could find it useful as a programming example. The use of the rotation matrix method is also interesting -- I was not aware of it, since I actually haven't had a chance to deal with this side of Android programming. BTW it would be possible to compute the basis of the trajectory plane just from the ideal accelerometer measurements, except there would be one more unknown initial value (direction). – biggvsdiccvs Feb 19 '16 at 08:04
  • @biggvsdiccvs I realise this is quite an old post, but I think it's okay to ask questions even on old posts? Sorry if it's not. Anyhow, you mentioned the limitations of this approach. What other approaches are there that may give reasonable trajectories? Currently in the same boat and am thinking of using GPS, but my problem is that the changes are relatively small (say a 10m x 10m room), over a long time. –  Feb 25 '17 at 02:00
  • I think a good external GPS receiver could give 1 to 1.5-m resolution, if there's good reception. Otherwise, maybe you could do something with sound. – biggvsdiccvs Feb 26 '17 at 03:50
0

How to calculate the position of the device using the accelerometer values?

Physicists like to think of the position in space of an object at a given time as a mathematical function p(t) with values ( x(t), y(t), z(t) ). The velocity v(t) of that object turns out to be the first derivative of p(t), and the acceleration a(t) nicely fits in as the first derivative of v(t).

From now on, we will just look at one dimension, the other two can be treated in the same way.

In order to get the velocity from the acceleration, we have to "reverse" the operation using our known initial values (without them we would not obtain a unique solution).

Another problem we are facing is that we don't have the acceleration as a function. We just have sample values handed to us more or less frequently by the accelerometer sensor.

So, with a prayer to Einstein, Newton and Riemann, we take these values and think of the acceleration function as a lot of small lines glued together. If the sensor fires often, this will be a very good approximation.

Our problem now has become much simpler: the (indefinite) integral (= antiderivative) to a linear function f(t) = m*t + b is F(t) = m/2 * t^2 + b*t + c, where c can be chosen to satisfy the initial condition (zero velocity in our case) .

Let's use the point-slope form to model our approximation (with time values t0 and t1 and corresponding acceleration values a0 and a1):

a(t) = a0 + (a1 – a0)/(t1 – t0) * (t – t0)

Then we get (first calculate v(t0) + "integral-between-t0-and-t-of-a", then use t1 in place of t)

v(t1) = v(t0) + (a1 + a0) * (t1 – t0) / 2

Using the same logic, we also get a formula for the position:

p(t1) = p(t0) + (v(t1) + v(t0)) * (t1 – t0) / 2

Translated into code, where last_values is used to store the old acceleration values:

float dt = (event.timestamp - last_timestamp) * NS2S;
for(int index = 0 ; index < 3 ; ++index){
    acceleration[index] = event.values[index];
    float last_velocity = velocity[index];
    velocity[index] += (acceleration[index] + last_values[index])/2 * dt;
    position[index] += (velocity[index] + last_velocity )/2 * dt;

    last_values[index] = acceleration[index];
}

**EDIT: **

All of this is only useful for us as long as our device is aligned with the world's coordinate system. Which will almost never be the case. So before calculating our values like above, we first have to transform them to world coordinates by using something like the rotation matrix from SensorManager.getRotationMatrix().

There is a code snippet in this answer by Csaba Szugyiczki which shows how to get the rotation matrix.

But as the documentation on getRotationMatrix() states

If the device is accelerating, or placed into a strong magnetic field, the returned matrices may be inaccurate.

...so I'm a bit pessimistic about using it while driving a car.

Community
  • 1
  • 1
Bö macht Blau
  • 12,820
  • 5
  • 40
  • 61
  • @Roman Panaget - I'm sorry but I found an error in my calculation result (from chat) while writing this answer. Please note that the formula you were using for the velocity is correct (I overlooked the v(to) + ... at some point when the calculation was getting a bit messy), but the formula for the position should indeed be similar to the other one. – Bö macht Blau Feb 16 '16 at 08:42
  • So maybe this is no help for your problem after all, but if you understand the mathematical part then if your problem persists you can at least feel confident that the error source has to be elsewhere. – Bö macht Blau Feb 16 '16 at 08:46