# Accelerometer acquisition, filtering and treatment on Pic

I've been asked to experiment with dynamic testing. Using accelerometer and strain gage bolted onto a metallic pipe,and hammering it ( the pipe ) I need to show out force of the impact, and displacement of the pipe, in fact sinking in the ground.

There are already systems doing that but it's out of budget and for big pipes.

Strain gage is used to get force of impact and can deal with that.

But for accelerometer, I'm a bit lost. One full impact recording seems to be close to 50ms.

So I'd need help to point me in the right direction in order to get displacement from accelerometer recordings.

I know how to condition sensors and record data... but next I'm lost :) I should integrate 2 times the accelerometer data in order to get displacement. That's the hard part for me, as I have never done that using pics, only analog OP AMPs long time ago.

So what kind of integration works best for that type of job ?

Thanks a lot

• It looks like you have a nice resolution on your 'velocity,' but I think that's raw acceleration, right? So have a velocity array long enough to accomdate the same number of samples as acceleration, and step through the array adding subsequent values of acceleration. Then step through velocity and add it into a single variable displacement measurement. But, if the sensor isn't linear, there's more math to do. – Sean Boddy May 12 '14 at 1:49
• why can't you just post-process the acceleration readings with MATLAB or R or some similar mathematics package? Just use backwards difference/Euler integration on the signal twice to get displacement over time, given the acceleration readings. – KyranF May 12 '14 at 2:17
• for the PIC to sample properly, and conduct double backwards difference equations, I think you would suffer aliasing and other issues. Record acceleration as fast as possible, and post-process if possible. If not possible, then read up on the Nyquist rate and digital aliasing - then read how to implement digital backwards difference equations for faster integration on embedded systems - then try to run two loops simultaneously (the second backwards difference loop only runs when the one before it is done, seeing as you need to wait for new data) – KyranF May 12 '14 at 2:19
• @Sean: it's just an example of what I'm trying to achieve. Accelerometer is linear. – Cedric May 12 '14 at 13:34
• @Kyran: I can not post process, goal is to have results on site. Yes, I was aware of sampling troubles. I'll implement a filter for that, and I take note of your suggestions. Thanks bot of you – Cedric May 12 '14 at 13:35

Assuming you are sampling at a fixed time step, called $\Delta$, and $t_n$ denote your sample times (i.e. $t_n = n \Delta$) then a traditional estimate of the velocity is $$v(t_{n}) \sim \sum_{k=0}^{n-1} a(t_k) \Delta$$ where $a(t_k)$ is the acceleration value at time $t_k$ as sampled by your accelerometer. It sounds like you are also assuming the initial conditions that $v(t_0) =0$ and $p(t_0) = 0$.

Applying this idea again to get the position via integrating the computed velocity we obtain $$p(t_{n}) \sim \sum_{k=0}^{n-1} v(t_k) \Delta \sim \sum_{k=0}^{n-1}\sum_{i=0}^{k-1} a(t_i) \Delta^2.$$

This latter summation simplifies a bit to $$\sum_{k=0}^{n-1}\sum_{i=0}^{k-1} a(t_i) \Delta^2 = \sum_{i=0}^{n-2}(n-1-i)a(t_i)\Delta^2 = \left(\sum_{i=0}^{n-3}(n-2-i)a(t_i)\Delta^2\right) + \sum_{i=0}^{n-2}a(t_i)\Delta^2.$$

Now if you notice the bit in parenthesis is our estimate of $p(t_{n-1})$ then you see we get the recursive formula $p(t_{n}) = p(t_{n-1}) + \sum_{i=0}^{n-2} a(t_i)\Delta^2$. This allows us to easily compute $p(t_{n})$ by keeping track of two (and only two) values: $p(t_{n-1})$ and an auxiliary variable $s_n = \sum_{i=0}^{n-2} a(t_i) \Delta^2$. Note we have the recursive formulas $$s_n = s_{n-1} + a(t_{n-2})\Delta^2$$ and $$p(t_n) = p(t_{n-1}) + s_n.$$

Your computation algorithm will go something like this:

1) Initial: $p(t_0) = p(t_1) = 0$. $s_2 = a(t_0)\Delta^2$.

2) At stage $n$: $s_n = a(t_{n-2})\Delta^2 + s_{n-1}$; free the memory containing $s_{n-1}$; $p(t_{n}) = p(t_{n-1}) + s_{n}$.

3) Repeat.

A few small remarks: You will probably want to delay multiplying everything by $\Delta^2$ until the end. You will have to experiment with the size of floating point data type you need so as not to overflow it. You need not use separate variables for $s_n$; in XC you would have a statement similar to $s \hspace{5pt} +\hspace{-5pt}= a(t_{n-2})\Delta^2$.

• Thanks a lot. Yes v(t0)=0 and p(t0)=0 I think I was scratching my head a bit too much, looks so simple :) I'll implement that and test it as soon as I got the components and the board ready. – Cedric May 12 '14 at 15:13