## Wednesday, August 3, 2011

### Fun with an iPhone Accelerometer

The iPhone 3GS has a built-in accelerometer, the LIS302DL, which is primarily used for detecting device orientation. I wanted to come up with something interesting to do with it, but first I had to see how it did on some basic tests. It turns out that the tests gave really interesting results themselves! A drop test gave clean results and a spring test gave fantastic data; however a pendulum test gave some problems.

You might guess the accelerometer would give a reading of 0 in all axes when the device is sitting on a desk. However, this accelerometer measures "proper acceleration," which essentially is a measure of acceleration relative to free-fall. So the device will read -1 in the z direction (in units where 1 corresponds to 9.8 m/s^2, the acceleration due to gravity at the surface of Earth).

Armed with this knowledge, let's take a look at the drop test:

To perform this test, I stood on the couch which was in my office (before it was taken away from us!), and dropped my phone hopefully into the hands of my officemate. I suspected that the device would read magnitude 1 before dropping, 0 during the drop, and a large spike for the large deceleration when the phone was caught.

As you can see, the results were basically as expected. The purple line shows the magnitude of the acceleration relative to free-fall. Before the drop, the magnitude bounces around 1, which is due to my inability to hold something steadily. The drop occurred near time 12.6, but I wasn't able to move my hand arbitrarily quickly so there's not a sharp drop to 0 magnitude. The phone fell for around 0.4 seconds corresponding to

$y = \frac{1}{2} g t^2 = \frac{1}{2} (9.8 \frac{m}{s^2})*(0.4 s)^2 = 0.784 m = 2.57 feet$
As for the spike at 13 seconds, the raw data shows that the catch occurs in
$t = 0.02 \pm 0.01 s$. In order for the device to come to rest in such a short amount of time, there needs to be a large deceleration provided by my officemate's hands.

Now the pendulum test consisted of taping my phone to the bottom of a 20 foot pendulum.

I didn't think enough about this, but the period of a pendulum, assuming we have a small amplitude, is given by:
$T = 2 \pi \sqrt{\frac{L}{g}}$ which is about 5 seconds. With a relatively small amplitude, the acceleration in the x direction will be small. Basically I'm reaching the limit of the resolution of the acceleration device. It appears that the smallest increment the device can measure is 0.0178 g. This happens to match the specifications from the spec sheet I linked at the top of the page, where they specify a minimum of 0.0162 g, and a typical sensitivity of 0.018 g!

Now we come to the most exciting test, the spring test! Setup: I taped my phone to the end of a spring and let it go. Ok.

Here is the actual acceleration data:

The first thing I see is that the oscillation frequency looks constant, as it should be for a simple harmonic oscillator. There is also a decay which looks exponential! Let's see how well the data fits if we have a frictional term proportional to the velocity of the phone. This gives is a differential equation which looks like this:

$m \ddot{x} + F\dot{x} + k x = 0$
Now we can plug in an ansatz (educated guess) to solve this equation:

$x(t) = A*e^{i b t}$
$-b^2 mx(t) + i b Fx(t) + kx(t) = 0$
$-m b^2+iFb+k = 0$
We can solve this equation for b with the quadratic equation:

$b = \frac{\sqrt{4km - F^2}}{2m} + i\frac{F}{2m} \equiv \omega + i \gamma$

where I defined two new constants here. So we see that our ansatz does solve the differential equation. Now we want acceleration, which is the second time derivative of position with respect to time.

$a(t) \equiv \ddot{x} = -b^2 A e^{ibt}$

Now are only interested in the real part of this solution, which gives us (adding in a couple of constants to make the solution more general):

$a(t) = -(\omega^2 - \gamma^2) A e^{-\gamma t} cos(\omega t + \phi) + C$
Let's redefine the coefficient of this acceleration to make things a little cleaner!

$a(t) = B e^{-\gamma t} cos(\omega t + \phi) + C$
Ok, with that math out of the way (for now), we can try to fit this data. I actually used Excel to fit this data using a not-so-well-known tool called Solver. This allows you to maximize or minimize one cell while Excel varies other cells. In this case, I defined a cell which is the Residual Sum of Squares of my fit versus the actual data, and I tell Excel to vary the 5 constants which make the fit! The values jump around for a little while then it gives up when it thinks it converged to a solution. Using this you can fit arbitrary functions, neato!

With this, I come up with the following plot:

$B = 0.633740943$
$\gamma = 0.012097581$
$\omega = 8.599670376$
$\phi = 0.693075811$
$C =-1.004454967$
with an R^2 value of 0.968!

At this point it should be noted that if I discretize my smooth fit to have the same resolution (0.0178 g) as the accelerometer, then see what the error is comparing the smooth fit to its own discretization, I get an R^2 of 0.967! This means that there is a decent amount of built-in error to these fits due to discretization on the order of the error we're seeing for our actual fits.

Immediately we can recognize that C should be -1, since this is just a factor relating "free-fall" acceleration to actual iPhone acceleration. If we wanted, we could solve for the ratio of the spring constant to the mass, but I'll leave that as an exercise for the reader.

If you look closely, you can see that the frequency appears to match very well. The two lines don't go out of phase. One problem with the fit is the decay. The beginning and the end of the data are too high compared to the fit, which is a problem. This implies that there is some other kind of friction at work.

Some larger objects or faster moving objects tend to experience a frictional force proportional to the square of the velocity. I don't think my iPhone is large or fast (compared to a plane for example), but I'll try it anyway.

The differential equation is:

$m \ddot{x} + F\dot{x}^2 + k x = 0$
yikes. This is a tough one because of the velocity squared term. One trick I found here attempts a general solution for a similar equation. They make an approximation in order to solve it, but the approximation is pretty good in our case. Take a look at the paper if you're interested.

The basic idea is to note that the friction term is the only one that affects the energy. So, assuming that the energy losses are small in a cycle, we can look at a small change in energy with respect to a small change in time due to this force term. This gives us an equation which can let us solve for the amplitude as a function of time approximately! Really interesting idea.

So I plugged the following equation into the Excel Solver:

$a(t) = \frac{A cos(\omega t + \phi)}{\gamma t + 1} + B$

Here's the fit:

Which uses these values:
$A = 0.772773705$
$\gamma = 0.029745368$
$\omega = 8.600177692$
$\phi = 0.688610161$
$B = -1.004530009$
with an R^2 value of 0.964!

This fit seems to have the opposite effect. The middle of the data is too high compared to the fit, while the beginning and end of the data seems too low. This makes me think that the actual friction terms involved in this problem are possibly a sum of a linear term and a squared term. I don't know how to make progress on that differential equation, so I wasn't able to fit anything. If you try the same trick I mentioned earlier, you run into a problem where you can't separate some variables which you need to separate in the derivation unfortunately.

So there you have it, I wanted to find something neat to do, and I got really cool data from just testing the accelerometer.

Stay tuned for an interesting challenge involving some physical data from my accelerometer!

1. How do you get the data from the phone? Is there an app that logs accelerometer data?

2. Hi Flavin, I used an app called "Dynolicious Log Box." It seemed to work pretty well. The data is sent to their server. They give you a link to the dataset, which can be read into Excel or the program of your choice. I didn't try any other accelerometer programs, however.

3. Bohn, why don't you numerically integrate the various nonlinear differential equations in Excel? Then you can use any kind of nonlinear ODE you please! Since it's an initial value problem, you can just use:

x_next = x_last + v_last*dt + 0.5*a_last*dt^2

dt should be at least ten times smaller than the period of the system.
--

Another thing is that your spring might be slightly nonlinear also, so you could try using a force like:
Fspring = -k*x - K*x^3
(the second term has to be cubic, not square, in x because we want the force to oppose the stretching)

4. I've also heard that you can do the same thing on Macbooks (accelerometer for when you drop it), and even use them as a seismograph. Although I don't think you can see anything but noise from the resolution.

5. Andy,
One issue with numerically integrating the non-linear differential equations is that I don't actually know the coefficients of my equations before fitting. I would have to take guesses at them and refine my guesses, continuing to compare the fits I suppose. This does seem possible, but for some reason it's not as fulfilling for me as finding a "general" solution and finding the best fit parameters. I may eventually get around to checking to see if my spring really has both frictional terms. I can post the data if you or anyone else wants to give it a go.

As for the nonlinearity of the spring, it certainly could be the case! That's a point I didn't consider. I am using a spring from an intro physics course here, and it's definitely possible that the spring is worn. I only know that the spring was linear when it was purchased.

---

Brian, I haven't played around with my accelerometer on my Macbook, but I would assume that your statement about resolution is correct. I think it's primary purpose is to turn off the hard drives in the event of a free-fall or high acceleration. It doesn't seem like the resolution needs to be very high. Additionally, while doing my spring test, my hard drive most likely would have turned off, which might adversely effect my results!

6. You might be qualified to get a Apple iPhone 7.