Calculation vibration with accelerometer (LSM303D sensor)with STM32 - accelerometer

I want to write an algorithm for ageing or condition monitoring of fluid valves.I read something about condition monitoring of valves and finally, I decided to calculate the vibration of the valve with accelerometer sensor.
Currently, I am working with LSM303D and ADXL345 sensors.
I am getting raw data from this sensor(LSM303D) like
X-axis = -2587 negative
Y -axis = -2230 negative
Z - axis = 16063
Is this mean acceleration is in Z axis direction?
I have fixed my sensor on the valve(There is no physical movement) and want to measure the vibration while switching on and off the valve.
How to proceed with my raw data for creating an algorithm for vibration calculation with STM32 controller?
Please explain about the raw data from sensor.
Sensor Datasheet
http://www.st.com/content/ccc/resource/technical/document/datasheet/1c/9e/71/05/4e/b7/4d/d1/DM00057547.pdf/files/DM00057547.pdf/jcr:content/translations/en.DM00057547.pdf
STM32 Datasheet:
http://www.st.com/content/ccc/resource/technical/document/datasheet/95/3c/2e/5b/21/09/45/a6/DM00115237.pdf/files/DM00115237.pdf/jcr:content/translations/en.DM00115237.pdf
OutPUT:
X axis
-2320,-2288,-2290,-2273,-2308,-2264,-2258,-2302,-2361,-2361,-2263,-2349,-2338,-2301,-2383,-2311,-2313,-2313,-2354,-2351,-2358,-2254,-2411,-2336,-2336,-2269,-2232,-2354,-2300,-2356,-2326,-2368,-2368,-2295,-2370,-2345,-2344,-2342,-2323,-2323,-2341,-2290,-2292,-2328,-2363,-2347,-2347,-2303,-2330,-2384,-2306,-2318,-2348,-2348,-2289,-2300,-2384,-2301,-2269,-2269,-2391,-2314,-2294,-2327,-2343,-2331,-2331,-2247,-2313,-2403,-2284,-2336,-2336,-2323,-2340,-2276,-2277,-2353,-2400,-2400,-2321,-2296,-2317,-2314,-2356,-2359,-2359,-2313,-2250,-2312,-2334,-2314,-2314,-2298,-2317,-2279,-2368,-2331,-2560,-2304,-2298,-2327,-2301,-2332,-2330,-2330,-2269,-2300,-2300,-2332,-2271,-2057,-2313,-2316,-2309,-2257,-2362,-2406,-2406,-2290,-2298,-2373,-2314,-2260,-2277,-2277,-2323,-2332,-2189,-2314,-2339,-2553,-2297,-2332,-2327,-2289,-2347,-2326,-2326,-2319,-2361,-2330,-2352,-2329,-2550,-2294,-2311,-2312,-2327,-2344,-2362,-2360,-2280,-2307,-2307,-2370,-2307,-2211,-2264,-2313,-2354,-2317,-2344,-2352,-2288,-2288,-2266,-2307,-2297,-2356,-2334,-2302,-2307,-2307,-2326,-2289,-2345,-2387,-2378,-2308,-2308,-2323,-2323,-2273,-2348,-2288,-2288,-2314,-2255,-2299,-2335,-2306,-2327,-2327,-2267,-2313,-2319,-2275,-2307,-2354,-2354,-2299,-2288,-2279,-2337,-2325,-2325,-2256,-2312,-2294,-2295,-2294,-2368,-2368,-2326,-2324,-2320,-2303,-2310,-2325,-2325,-2310,-2291,-2309,-2328,-2364,-2364,-2284,-2354,-2376,-2311,-2335,-2342,-2342,-2336,-2322,-2317,-2276,-2385,-2362,-2268,-2107,-2363,-2307,-2292,-2296,-2356,-2288,-2288,-2355,-2372,-2303,-2348,-2279,-2346,-2346,-2238,-2320,-2323,-2280,-2318,-2345,-2345,-2318,-2323,-2316,-2316,-2307,-2324,-2324,-2376,-2251,-2347,-2375,-2300,-2300,-2399,-2318,-2283,-2375,-2356,-2316,-2316,-2260,-2418,-2341,-2347,-2327,-2327,-2308,-2358,-2335,-2344,-2350,-2264,-2264,-2377,-2328,-2323,-2347,-2351,-2352,-2352,-2290,-2369,-2307,-2333,-2306,-2540,-2284,-2343,-2353,-2283,-2301,-2310,-2548,-2292,-2358,-2321,-2275,-2290,-2267,-2267,-2378,-2330,-2319,-2356,-2315,-2337,-2337,-2360,-2385,-2358,-2320,-2304,-2290,-2290,-2329,-2362,-2318,-2335,-2303,-2303,-2358,-2305,-2293,-2288,-2244,-2255,-2255,-2339,-2337,-2375,-2296,-2243,-2053,-2309,-2346,-2410,-2333,-2287,-2316,-2316,-2362,-2331,-2291,-2350,-2394,-2323,-2323,-2377,-2313,-2324,-2327,-2322,-2322,-2258,-2325,-2356,-2337,-2376,-2351,-2351,-2286,-2255,-2347,-2376,-2359,-2343,-2343,-2389,-2303,-2302,-2384,-2298,-2298,-2313,-2344,-2323,-2273,-2295,-2316,-2316,-2369,-2343,-2287,-2280,-2348,-2544,-2288,-2279,-2380,-2349,-2241,-2344,-2344,-2366,-2363,-2350,-2303,-2335,-2340,-2340,-2309,-2348,-2304,-2354,-2644,-2461,-2404,-2404,-2170,-2374,-2299,-2268,-2334,-2334,-2364,-2288,-2337,-2329,-2401,-2241,-2241,-2247,-2339,-2395,-2312,-2369,-2526,-2270,-2298,-2318,-2285,-2254,-2316,-2349,-2349,-2334,-2274,-2297,-2352,-2321,-2321,-2293,-2358,-2289,-2341,-2329,-2336,-2336,-2360,-2311,-2345,-2344,-2321,-2321,-2333,-2285,-2284,-2325,-2360,-2280,-2280,-2303,-2343,-2237,-2317,-2373,-2535,-2279,-2309,-2287,-2308,-2289,-2334,-2334,-2293,-2330,-2315,-2271,-2300,-2346,-2346,-2336,-2287,-2308,-2320,-2315,-2347,-2347,-2302,-2283,-2267,-2312,-2288,-2096,-2352,-2298,-2252,-2290,-2341,-2328,-2290,-2290,-2265,-2242,-2326,-2262,-2300,-2074,-2330,-2322,-2382,-2304,-2316,-2337,-2337,-2209,-2358,-2339,-2285,-2319,-2280,-2280,-2327,-2299,-2329,-2331,-2317,-2347,-2347,-2343,-2334,-2255,-2295,-2295,-2295,-2315,-2343,-2365,-2366,-2326,-2542,-2286,-2331,-2343,-2370,-2317,-2291,-2270,-2270,-2343,-2261,-2335,-2343,-2297,-2297,-2359,-2290,-2293,-2350,-2314,-2315,-2315,-2388,-2357,-2335,-2382,-2235,-2052,-2308,-2379,-2340,-2313,-2260,-2301,-2301,-2225,-2323,-2286,-2383,-2370,-2504,-2248,-2274,-2317,-2282,-2285,-2226,-2145,-2401,-2341,-2361,-2422,-2336,-2288,-2288,-2386,-2357,-2294,-2310,-2295,-2117,-2373,-2260,-2274,-2289,-2379,-2313,-2313,-2256,-2300,-2344,-2293,-2296,-2312,-2312,-2363,-2328,-2347,-2351,-2294,-2092,-2348,-2381,-2347,-2229,-2275,-2328,-2328,-2379,-2256,-2362,-2331,-2347,-2360,-2360,-2279,-2269,-2332,-2374
Y Axis
-2485,-2478,-2511,-2486,-2486,-2475,-2490,-2517,-2482,-2505,-2457,-2457,-2468,-2471,-2475,-2498,-2513,-2489,-2531,-2531,-2509,-2435,-2473,-2493,-2486,-2428,-2428,-2485,-2473,-2513,-2459,-2514,-2510,-2456,-2456,-2519,-2511,-2569,-2466,-2484,-2524,-2524,-2475,-2468,-2518,-2469,-2443,-2513,-2513,-2494,-2460,-2503,-2466,-2497,-2497,-2460,-2472,-2528,-2473,-2479,-2480,-2480,-2448,-2425,-2447,-2523,-2552,-2447,-2447,-2513,-2532,-2480,-2459,-2511,-2511,-2493,-2472,-2447,-2477,-2542,-2434,-2434,-2472,-2484,-2477,-2503,-2472,-2472,-2478,-2519,-2501,-2449,-2453,-2477,-2477,-2491,-2471,-2480,-2507,-2473,-2449,-2449,-2514,-2457,-2473,-2513,-2501,-2501,-2437,-2520,-2479,-2481,-2468,-2497,-2497,-2523,-2488,-2454,-2525,-2470,-2470,-2448,-2487,-2484,-2477,-2460,-2476,-2476,-2515,-2467,-2443,-2449,-2480,-2480,-2451,-2459,-2470,-2477,-2489,-2463,-2463,-2492,-2500,-2469,-2491,-2475,-2528,-2528,-2438,-2442,-2506,-2499,-2489,-2461,-2486,-2485,-2483,-2492,-2523,-2453,-2453,-2484,-2458,-2509,-2484,-2497,-2433,-2433,-2515,-2479,-2507,-2519,-2485,-2483,-2500,-2500,-2477,-2510,-2506,-2488,-2480,-2497,-2497,-2488,-2464,-2452,-2496,-2487,-2487,-2512,-2464,-2489,-2484,-2496,-2477,-2477,-2467,-2478,-2500,-2452,-2534,-2490,-2490,-2499,-2485,-2463,-2457,-2501,-2501,-2471,-2448,-2524,-2480,-2473,-2503,-2503,-2481,-2525,-2497,-2449,-2511,-2483,-2483,-2513,-2459,-2501,-2462,-2489,-2489,-2496,-2494,-2468,-2453,-2464,-2537,-2537,-2484,-2520,-2442,-2514,-2505,-2463,-2517,-2523,-2523,-2471,-2454,-2444,-2442,-2519,-2519,-2521,-2480,-2457,-2455,-2492,-2471,-2471,-2454,-2493,-2510,-2480,-2458,-2489,-2489,-2485,-2492,-2487,-2459,-2466,-2466,-2519,-2446,-2500,-2527,-2459,-2489,-2489,-2479,-2469,-2484,-2482,-2477,-2480,-2480,-2479,-2494,-2514,-2485,-2474,-2474,-2493,-2505,-2495,-2471,-2496,-2501,-2501,-2438,-2495,-2469,-2488,-2481,-2521,-2521,-2508,-2490,-2465,-2480,-2455,-2524,-2524,-2499,-2502,-2481,-2472,-2490,-2490,-2432,-2494,-2543,-2478,-2462,-2487,-2487,-2525,-2491,-2500,-2491,-2449,-2490,-2490,-2459,-2506,-2476,-2487,-2493,-2480,-2480,-2498,-2472,-2506,-2473,-2457,-2457,-2503,-2515,-2479,-2486,-2482,-2521,-2521,-2491,-2509,-2505,-2496,-2483,-2483,-2453,-2495,-2519,-2440,-2548,-2482,-2482,-2427,-2532,-2495,-2466,-2436,-2443,-2443,-2500,-2444,-2458,-2455,-2489,-2489,-2510,-2497,-2494,-2510,-2519,-2485,-2485,-2458,-2509,-2466,-2519,-2483,-2523,-2523,-2482,-2511,-2492,-2463,-2470,-2470,-2494,-2481,-2487,-2495,-2493,-2513,-2513,-2490,-2495,-2505,-2476,-2470,-2470,-2480,-2502,-2479,-2420,-2470,-2479,-2479,-2491,-2530,-2512,-2503,-2538,-2446,-2446,-2447,-2562,1029,-2413,-2366,-2607,-2607,-2426,-2440,-2552,-2446,-2503,-2517,-2517,-2428,-2496,-2462,-2489,-2525,-2513,-2513,-2456,-2476,-2500,-2535,-2524,-2453,-2453,-2534,-2493,-2480,-2487,-2437,-2437,-2470,-2514,-2465,-2532,-2511,-2448,-2448,-2479,-2532,-2519,-2507,-2463,-2510,-2510,-2459,-2461,-2483,-2488,-2520,-2520,-2485,-2455,-2491,-2492,-2445,-2458,-2458,-2477,-2489,-2492,-2478,-2506,-2501,-2501,-2489,-2485,-2481,-2480,-2486,-2486,-2471,-2453,-2505,-2473,-2478,-2472,-2472,-2493,-2487,-2491,-2479,-2492,-2518,-2518,-2520,-2508,-2503,-2465,-2501,-2488,-2488,-2485,-4231,-2508,-2483,-2376,-2474,-2474,-2453,-2503,-2498,-2455,-2495,-2495,-2495,-2517,-2455,-2470,-2441,-2478,-2478,-2470,-2500,-2484,-2473,-2418,-2477,-2477,-2512,-2459,-2503,-2473,-2464,-2476,-2476,-2480,-2497,-2499,-2501,-2492,-2492,-2443,-2478,-2500,-2476,-2486,-2529,-2529,-2455,-2521,-2463,-2482,-2460,-2460,-2510,-2506,-2470,-2484,-2473,-2441,-2441,-2500,-2489,-2502,-2496,-2415,-2504,-2504,-2518,-2417,-2500,-2498,-2460,-2501,-2501,-2498,-2460,-2449,-2465,-2499,-2499,-2496,-2479,-2521,-2513,-2513,-2479,-2479,-2480,-2546,-2514,-2502,-2479,-2479,-2446,-2484,-2531,-2524,-2533,-2489,-2489,-2486,-2444,-2477,-2462,-2474,-2513,-2513,-2485,-2475,-2463,-2438,-2477,-2477,-2470,-2481,-2467,-2497,-2454,-2475,-2475,-2507,-2477,-2499,-2464,-2501,-2501,-2523,-2502,-2452,-2497,-2488,-2456,-2456,-2487,-2504,-2490,-2472,-2547,-2458,-2458,-2469,-2429
Z Axis:
16068,16050,16097,16178,16142,16294,16038,16070,16184,16207,16125,16174,16246,16246,16161,16117,16132,16128,16104,16281,16326,16070,16012,16147,16174,15961,16097,16121,15919,16175,16173,16092,16019,16144,16074,16136,16136,16031,16016,16123,16077,16166,16019,16019,16169,16132,16150,16075,16100,15896,16152,16047,16215,16183,16040,16178,16178,16127,16099,16035,16169,16243,16323,16067,16150,16071,16144,16052,16118,15931,16187,16242,16096,16243,16090,16170,16170,16108,16137,16060,16081,16184,16323,16067,16078,16148,16104,16011,16174,16174,16123,16133,16196,16109,16117,16088,16088,16159,16153,16112,16127,16215,16215,16064,16046,16073,16081,16141,16351,16095,16143,16171,16145,16081,16226,16339,16083,16078,16209,15997,16071,16181,16181,16128,16070,16077,16018,16163,16293,16037,16146,16207,16147,16041,16124,16124,16093,16129,15987,16152,16175,16311,16055,16114,16116,16245,16052,16125,16148,16111,16111,16157,16040,15968,16174,16213,16106,16052,16161,16048,16142,16142,16147,16030,16102,16079,16153,16178,16129,16179,16179,16079,16060,16020,16103,16074,16078,16078,16164,16182,16163,16105,16046,16046,16160,16068,16060,16074,16125,16131,16131,16160,16026,16019,16153,16048,15924,16180,16087,16150,16048,16072,16040,16040,16139,16106,16077,16262,16117,16212,16212,16239,16043,16219,16119,16012,16105,16105,16111,16044,16161,16073,16020,16020,16135,16160,16096,16076,16110,16053,16053,16151,16066,16068,16129,16123,16114,15886,16142,16199,16181,16064,16090,16043,16099,16099,16128,16058,16016,16043,16167,16177,16177,16010,16158,16086,16112,16115,15888,16144,16135,16226,16110,16121,16053,15916,16172,16075,16061,16119,16137,16159,16159,16181,16075,16039,16242,16121,15977,15977,16141,16155,16207,16069,16107,16107,16114,16083,16102,16008,16071,16119,16119,16028,16124,16225,16022,16134,16130,16130,16137,16174,16086,16159,16056,16088,16088,16186,16154,16050,16092,16159,16146,16146,16070,16176,16225,16088,16118,16118,16093,16289,16118,16091,16141,16207,16207,16173,16151,16048,16195,16141,16202,16202,16058,16040,16145,16190,16140,16140,16159,16163,16153,16151,16106,16178,16178,16044,16064,16223,16131,16101,15955,16211,16151,16161,15998,16099,16053,16053,16085,16005,16134,16144,16088,15872,16128,16072,16029,16230,16109,16039,16081,16081,16267,16176,16049,16156,16162,16162,16130,16138,16018,16119,16180,16129,16129,16178,16091,16108,16169,16191,16191,16064,16070,16117,16160,16110,16082,16082,16096,15991,16130,16194,16068,15930,15930,16132,16121,16067,16105,16087,16087,16183,16269,16118,15998,16180,16129,16129,16128,16156,16166,16065,16136,16248,16248,11913,15882,16067,16164,16004,16159,16159,16201,16050,16129,16144,16119,16129,16129,16028,16126,16152,16117,16107,15903,16159,16138,16140,16131,16075,16110,15895,16151,16064,16168,16116,16131,16160,16160,16150,16130,16059,16141,16128,16195,16195,16216,16053,16097,16167,16122,16122,16077,16173,16031,16131,16092,16099,16099,16163,16126,16065,16137,16165,16342,16086,16050,16156,16087,16107,16169,16169,16178,16061,16091,16061,16260,16133,16133,16117,16174,16108,16164,16162,16311,16055,16219,16143,16127,16105,16121,15902,16158,16194,16044,16178,16057,16085,15329,15329,16090,16066,16131,16113,16163,16352,16096,16080,16057,16111,16146,16134,16134,16162,16092,16139,16120,16144,16028,16028,16142,16171,16066,16041,16154,16366,16110,16110,16128,16117,16125,16091,16091,16149,16050,16024,16154,16186,16072,16072,16090,16143,16206,16141,16073,16052,16052,16123,16158,16159,16197,16135,16135,16119,16128,16090,16063,16139,16153,16153,16019,16072,16128,16153,16129,16180,16180,16157,16207,16126,16158,16066,16066,16102,16177,16104,16202,16104,16159,16159,16090,16071,16070,16115,16127,15924,16180,16088,16122,16075,16085,16105,16105,16187,16056,16010,16150,16151,16166,16166,16124,16088,16166,16123,15991,15944,16200,16124,16152,16093,16106,16057,16057,16177,16163,16167,16021,16095,16106,16106,16088,16170,16162,16144,16266,16266,16115,16135,16159,16021,16240,16162,16162,16004,16154,16182,16073,16129,16299

The reference is relative to the chip position. See the datasheet for this.
Don't forget that gravity is an acceleration, so when the accelerometer is still, the output vector points downwards (in the Earth reference). You can store an initial value (an average on several points in the best case), and substract this value from your datapoints.
The best way to debug this is to plot the values in real time.

Related

Getting a trajectory from accelerometer and gyroscope (IMU)

I am well aware of the existence of this question but mine will differ. I also know that there could be significant errors with this approach but I want to understand the configuration also theoretically.
I have some basic questions which I find hard to answer for myself clearly. There is a lot of information about accelerometers and gyroscopes but I still haven't found an explanation "from first principles" of some basic properties.
So I have a plate sensor that contains an accelerometer and gyroscope. There is also a magnetometer which I skip for now.
The accelerometer gives information in each time t about the temporary acceleration vector a = (ax, ay, az) in m/s^2 according to the fixed coordinate system to the sensor.
The gyroscope gives a 3D vector in deg/s which says the temporary speed of rotation of the three axes (Ox, Oy and Oz). From this information, one can get a rotation matrix that corresponds to an infinitesimal rotation of the coordinate system (according to the previous moment). Here is some explanation how to obtain a quaternion, that represents R.
So we know that the infinitesimal movement can be calculated considering that the acceleration is the second derivative of the position.
Imagine that your sensor is attached to your hand or leg. In the first moment we can consider its point in 3D space as (0,0,0) and the initial coordinate system also attached in this physical point. So for the very first time step we will have
r(1) = 0.5a(0)dt^2
where r is the infinitesimal movement vector, a(0) is the acceleration vector.
In each of the following steps we will use the calculations
r(t+1) = 0.5a(t)dt^2 + v(t)dt + r(t)
where v(t) is the speed vector which will be estimated in some way, for example as (r(t)-r(t-1)) / dt.
Also, after each infinitesimal movement we will have to take into account the data from the gyroscope. We will use the rotation matrix to rotate the vector r(t+1).
In this way, probably with tremendous error I will get some trajectory according to the initial coordinate system.
My queries are:
Am I principally correct with this algorithm? If not, where am I wrong?
I would very much appreciate some resources with a working example where the first principles are not skipped.
How should I proceed with using the Kalman's filter to obtain a better trajectory? In what way exactly do I pass all the IMU data (accelerometer, gyroscope and magnetometer) to the Kalman filter?
Your conceptual framework is correct, but the equations need some work. The acceleration is measured in the platform frame, which can rotate very quickly, so it is not advisable to integrate acceleration in the platform frame and rotate the position change. Rather, the accelerations are transformed into a relatively slowly rotating frame and the integration to velocity change and position change is done there. Typically a locally-level frame (e.g. North-East-Down or Wander Aziumuth) or an Earth-centered frame (ECEF or ECI). Gravity and Coriolis force must be included in the acceleration.
Derivations from first principles can be found in many references, one of my favorites is Strapdown Inertial Navigation Technology by Titterton and Weston. Derivations of the inertial navigation equations in locally-level and Earth-fixed frames are given in Chapter 3.
As you've recognized in your question - the initial velocity is an unknown constant of integration. Without some estimate of initial velocity the trajectory resulting from integrating the inertial data can be wildly wrong.

How can I correctly apply the scalar calibration of vector magnetometers?

I need to calibrate a magnetometer using the method of Merayo with matlab.
I have found this code:
But I do not understand how I apply this technique. I fact I have the magnetic Data distorted, I apply the Magnetic Calibration.
[U,c] = MgnCalibration(X)
So I get U the Shape ellipsoid parameter and c the ellipsoid center.
And the calibrated measurement is:
w = U*(v-c)
The problem is that when I calculate the corrected Data I have another order of values.
Data=[1750 1460 -3940]
CalibratedData=[0.4042 0.3820 -0.6860]
What I did not very well understood?
How can I use my magnetic data after this calibration?
The calibrated data for each axis [mx=0.4042 my=0.3820 mz=-0.6860] should now be independent of the orientation in 3D space in which the fluxgate finds itself for that specific measurement at that point in space. The Total Field calculated with sqrt(mx.mx+my.my+mz.mz) will be the calibrated Earth's Total magnetic field at that point.
If scaled properly this total field value should be the same as that measured by a proton or cesium vapor magnetometer

How do I calculate acceleration/deceleration in the direction of travel from X,Y,Z accelerometer readings from iphone

I am writing an iPhone/iPad app. I need to compute the acceleration and deceleration in the direction of travel of a vehicle traveling in close to a straight horizontal line with erratic acceleration and deceleration. I have the sequence of 3 readings from the X,Y,Z orthogonal accelerometers. But the orientation of the iphone/ipad is arbitrary and the accelerometer readings include vehicle motion and the effect of gravity. The result should be a sequence of single acceleration values which are positive or negative depending on whether the vehicle is decelerating or accelerating. The positive and negative direction is arbitrary so long as acceleration has the opposite sign to deceleration. Gravity should be factored out of the result. Some amount of variable smoothing of the result would be useful.
The solution should be as simple as possible and must be computationally efficient. The answer should be some kind of pseudo-code algorithm, C code or a sequence of equations which could easily be converted to C code. An iPhone specific solution in Objective C would be fine too.
Thanks
You will need some trigonometry for this, for example to get the magnitude you need
magn = sqrt(x*x+y*y+z*z);
to get the angle you will need atan, then c function atan2 is better
xyangel = atan2(y,x);
xymagn = sqrt(x*x+y*y);
vertangle = atan2(z,xymagn)
no how you get negative and positive magnitude is arbitrary, you could for example interpret π/2 < xyangle < 3π/2 as negative. That would be taking the sign of x for the sign of magn, but it would be equally valid to take the sign from y
It is really tough to separate gravity and motion. It's easier if you can analyze the data together with a gyroscope and compass signal.
The gyroscope measures the rate of angular rotation. Its integral is theoretically the angular orientation (plus an unknown constant), but the integral is subject to drift, so is useless on its own. The accelerometer measures angular orientation plus gravity plus linear acceleration. With some moderately complex math, you can isolate all 3 of those quantities from the 2 sensors' values. Adding the compass fixes the XY plane (where Z is gravity) to an absolute coordinate frame.
See this great presentation.
Use userAcceleration.
You don't have to figure out how to remove gravity from the accelerometer readings and how to take into accont the orientation: It is already implemeted in the Core Motion Framework.
Track the mean value of acceleration. That will give you a reference for "down". Then subtract the mean from individual readings.
You'll need to play around with the sensitivity of the mean calculation, since, e.g., making a long slow turn on a freeway will cause the mean to slowly drift outwards.
If you wanted to compensate for this, you could use GPS tracking to compute a coarse-grained global acceleration to calibrate the accelerometer. In fact, you might find that differentiating the GPS velocity reading gives a good enough absolute acceleration all by itself (I haven't tried, so I can't say).

Remove gravity from raw accelerometer data of IMU--> please approve math and algo

I am using this device (http://www.sparkfun.com/products/10724) and have successfully implemented an quite well working orientation estimation based on a fusion of magnetometer, accelerometer and gyroscope data based on this http://www.x-io.co.uk/node/8#open_source_imu_and_ahrs_algorithms implementation. Now I want to calculate the dynamic acceleration (measures acceleration without static gravity acceleration). For doing this I came to the following idea.
Calculate a running average of the raw accelerometer data. If the raw acceleration is stable for some time (small difference between running average and current measured raw data) we assume the device does not move and we are measuring the raw gravity. Now save the gravity vector and also current orientation as quaternion. This approach assumes that our device could not be accelerated constantly without gravity.
For calculating the acceleration without gravity I am now doing following quaternion calculation:
RA = Quaternion with current x,y,z raw acceleration values
GA = Quaternion with x,y,z raw acceleration values of estimated gravity
CO = Quaternion of current orientation
GO = saved gravity orientation
DQ = GO^-1 * CO // difference of orientation between last gravity estimation and current orientation
DQ = DQ^-1 // get the inverse of the difference
SQ = DQ * GA * DQ^1 // rotate gravity vector
DA = RA - SQ // get the dynamic acceleration by subtracting the rotated gravity from the raw acceleration
Could someone check if this is correct? I am not sure because on testing it I get some high acceleration on rotating my sensor board, but I am able to get some acceleration data (but is is much smaller than the accelration during rotation) if the device is moved without rotating it.
Moreover I have the question if the accelerometer is also measuring acceleration if it is rotated on place or not!
Another way is to differentiate accel to give jerk (using finite difference, j = (a2 - a1) / dt). Run the jerk through a decay/leakage function (use a half life decay calc value rather than a simple multiplier). Then integrate the jerk (trapezoidal rule, a = dt * (j1 + j2) * 0.5) and it will remove the DC offset (gravity). Again run this signal through a decay function.
The decay functions avoid the value spiraling off but will reduce the magnitude of dynamic acceleration values that you see and will introduce some shaping to the signal. So you won't get values that are 'accurate' m/s/s readings any longer. But it is useful for short-time movements.
Of course you could just use a highpass filter instead but that generally requires a fixed sampling rate and is probably more computationally expensive if you are using convolution (finite impulse response filter).
It's easier than you think. You may wanna have a look at my post here about it:
http://www.varesano.net/blog/fabio/simple-gravity-compensation-9-dom-imus

Using the iPhone accelerometer in a car

I want to use the iPhones's accelerometer to detect motions while driving. I'm a bit confused what the accelerometer actually measures, especially when driving a curve.
As you can see in the picture, a car driving a curve causes two forces. One is the centripetal force and one is the velocity. Imagine the iPhone is placed on the dashboard with +y-axis is pointing to the front, +x-axis to the right and +z-axis to the top.
My Question is now what acceleration will be measured when the car drives this curve. Will it measure g-force on the -x-axis or will the g-force appear on the +y axis?
Thanks for helping!
UPDATE!
For thoses interested, as one of the answers suggested it measures both. The accelerometer is effected by centrifugal force and velocity resulting in an acceleration vector that is a combination of these two.
I think it will measure both. But don't forget that the sensor will measure gravity as well. So when your car is not moving, you will still get accelerometer readings. A nice talk on sensors in smartphones http://www.youtube.com/watch?v=C7JQ7Rpwn2k&feature=results_main&playnext=1&list=PL29AD66D8C4372129 (it's on android, but the same type of sensors are used in iphone).
Accelerometer measures acceleration of resultant force applied to it (velocity is not a force by the way). In this case force is F = g + w + c i.e. vector sum of gravity, centrifugal force (reaction to steering centripetal force, points from the center of the turn) and car acceleration force (a force changing absolute value of instantaneous velocity, points along the velocity vector). Providing Z axis of accelerometer always points along the gravity vector (which is rare case for actual car) values of g, w and c accelerations can be accessed in Z, X and Y coordinates respectively.
Unless you are in free fall the g-force (gravity) is always measured. If I understand your setup correctly, the g-force will appear on the z axis, the axis that is vertical in the Earth frame of reference. I cannot tell whether it will be +z or -z, it is partly convention so you will have to check it for yourself.
UPDATE: If the car is also going up/downhill then you have to take the rotation into account. In other words, there are two frames of reference: the iPhone's frame of reference and the Earth frame of reference. If you would like to deal with this situation, then please ask a new question.