# Need help with getting acceleration without gravity using mpu9250 on a swing

**URL:** <https://forum.arduino.cc/t/need-help-with-getting-acceleration-without-gravity-using-mpu9250-on-a-swing/1051418>\
**Category:** General Guidance\
**Created:** [November 9, 2022, 9:44am UTC](https://forum.arduino.cc/t/need-help-with-getting-acceleration-without-gravity-using-mpu9250-on-a-swing/1051418 "2022-11-09T09:44:00Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![nervusvagus](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/nervusvagus/32/223258_2.png) [@nervusvagus](https://forum.arduino.cc/u/nervusvagus)\
**Post date:** [November 9, 2022, 9:44am UTC](https://forum.arduino.cc/t/need-help-with-getting-acceleration-without-gravity-using-mpu9250-on-a-swing/1051418/1 "2022-11-09T09:44:00Z")

</div>

I'm having big troubles finding the speed of a childrens swing seat independent from its tilt. I believe this is called **aceleration without gravity** / **acceleration in world frame** / **linear acceleration**.

I installed an arduino, nodemcu with mpu9250 imu on a swing seat and light it up with movement using fastled library and ws2812b leds.

The swing seat is tilted towards the end of its swing which means some acceleration is on the mpu, and this is exactly when I want the swing to be dark. So when its swinging it lights up and at the end of its swing, it should be dark.

The final current version of it is here:

> **[r/FastLED - childrens swing with fastled](https://www.reddit.com/r/FastLED/comments/u8q5oe/childrens_swing_with_fastled/)**
>
> 2 votes and 10 comments so far on Reddit

There is some info on getting this acceleration value on

> <https://github.com/kriswiner/MPU9250/issues/181>
>
> Hi and first of all thanks for all your nice work.
> 
> I am using MPU9250\_MS5637\_…AHRS\_t3.ino and i am trying to get the acceleration in the world frame of reference regardless of how the sensor is tilted in space.
> 
> In order to get acceleration without gravity I had to modify your code a little bit:
> 
> using:
> \`\`\`
> lin\_ax = ax + a32;
> lin\_ay = ay - a31;
> \`\`\`
> instead of:
> \`\`\`
> //lin\_ax = ax + a31;
> //lin\_ay = ay + a32;
> \`\`\`
> With this modification, the lin\_ax/ay/az are always around 0 when they are at rest, regardles of how i 
> position my sensor.
> 
> I am not sure how to interpret this and i thought that since i can see the presumably intended effect with the above modification it is probably due to set up differences (i am using the sparkfun breakout board).
> 
> ...
> Following that, as i said i am trying to graph the acceleration in the world frame of reference. For that, the way i am finding the acceleration in the x axis is a bit naive, and i tried to combine the accelerations in the sensor frame with the angles in the world frame (Y/P/R); this is what i do for the x axis (perpendicular to Yaw=0): 
> 
> \`\`\`
> float x = lin\_ax \* cos(pitch) \* cos(roll) \* sin(yaw) 
> + lin\_ax \* cos(pitch) \* sin(roll) \* sin(yaw)
> + lin\_ay \* cos(pitch) \* cos(roll) \* cos(yaw)
> + lin\_ay \* sin(pitch) \* cos(roll) \* cos(yaw)
> + lin\_az \* cos(pitch) \* sin(roll) \* cos(yaw)
> + lin\_az \* sin(pitch) \* cos(roll) \* sin(yaw);
> \`\`\`
> By first sight this seems to be working alright but i was wondering if there is a cleaner way to do it. What i would like to ask is if there is a more straightforward way to do it by adding up some quaternion products similar to a31, a32 etc.. - or if you have any advice in calculating the acceleration the above way.

> <https://stackoverflow.com/questions/12617200/remove-gravity-from-imu-accelerometer>

however these information definetely exceeds my capacity. Is anyone familiar with any library or any methods which I can use to get the speed of this simple swing?

---

<div class="post-metadata">

**Author:** ![danzelfin](https://avatars.discourse-cdn.com/v4/letter/d/47e85d/32.png) [@danzelfin](https://forum.arduino.cc/u/danzelfin)\
**Post date:** [November 9, 2022, 9:50am UTC](https://forum.arduino.cc/t/need-help-with-getting-acceleration-without-gravity-using-mpu9250-on-a-swing/1051418/2 "2022-11-09T09:50:15Z")

</div>

Sounds like you need to open up the old kinematics textbook and write your own, I haven't heard of a library for this personally, nice project

---

<div class="post-metadata">

**Author:** ![nervusvagus](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/nervusvagus/32/223258_2.png) [@nervusvagus](https://forum.arduino.cc/u/nervusvagus)\
**Post date:** [November 9, 2022, 10:33am UTC](https://forum.arduino.cc/t/need-help-with-getting-acceleration-without-gravity-using-mpu9250-on-a-swing/1051418/3 "2022-11-09T10:33:07Z")

</div>

thank you, thats exactly what im trying to avoid. 🙂 Its rather odd that there are no easy ways to get this measurement yet.

---

<div class="post-metadata">

**Author:** ![anon94801460](https://avatars.discourse-cdn.com/v4/letter/a/278dde/32.png) [@anon94801460](https://forum.arduino.cc/u/anon94801460)\
**Post date:** [November 9, 2022, 3:26pm UTC](https://forum.arduino.cc/t/need-help-with-getting-acceleration-without-gravity-using-mpu9250-on-a-swing/1051418/4 "2022-11-09T15:26:09Z")

</div>

The reason that you are having trouble finding "easy ways to get this measurement" (remove the acceleration due to gravity) is that in actual practice, this is not possible with consumer grade IMUs.

To do so, you need to know the 3D orientation of the IMU very accurately. This page discusses and illustrates the problem: [https://web.archive.org/web/20180112063505/http://www.chrobotics.com/library/accel-position-velocity](https://web.archive.org/web/20180112063505/http://www.chrobotics.com/library/accel-position-velocity)

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [November 9, 2022, 5:17pm UTC](https://forum.arduino.cc/t/need-help-with-getting-acceleration-without-gravity-using-mpu9250-on-a-swing/1051418/5 "2022-11-09T17:17:05Z")

</div>

If it's just for visual effects, why do you need that specific measurement? Can't you just graph the three raw acceleration values and find some useful patterns to trigger your effects? If you have a laptop you can live-graph the data with Serial Plotter.

---

<div class="post-metadata">

**Author:** ![nervusvagus](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/nervusvagus/32/223258_2.png) [@nervusvagus](https://forum.arduino.cc/u/nervusvagus)\
**Post date:** [November 9, 2022, 7:48pm UTC](https://forum.arduino.cc/t/need-help-with-getting-acceleration-without-gravity-using-mpu9250-on-a-swing/1051418/6 "2022-11-09T19:48:45Z")

</div>

yes I use serial plotter. The raw acceleration values dont seem to help because of the 3 different states of the swing:  
#1: not swinging initially, the seat doesnt move. acceleration value is zero so the leds are dark.  
#2: the swing is moving and swinging so the leds get brighter with the swing of the seat  
#3: the swing moves to one end and stops for a brief moment. This is the trouble point. This is when the swing should slowly go all the way dark. however because the seat is tilted the swing registers acceleration on the axis and does not go completely dark.

This might have some work around through measuring x axis for the swing movement and then the y axis for the tilt . This seems like a simple problem regarding measuring values on certain axises but i couldn't figure out.

---

<div class="post-metadata">

**Author:** ![nervusvagus](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/nervusvagus/32/223258_2.png) [@nervusvagus](https://forum.arduino.cc/u/nervusvagus)\
**Post date:** [November 9, 2022, 7:51pm UTC](https://forum.arduino.cc/t/need-help-with-getting-acceleration-without-gravity-using-mpu9250-on-a-swing/1051418/7 "2022-11-09T19:51:53Z")

</div>

from what i see the errors for each angle degree tilt cause in acceleration readings seems to be a linear value. I'm still hoping to offset these errors with simple arithmetic functions for just the use on a swing.

> [@anon94801460](#):
>
> The reason that you are having trouble finding "easy ways to get this measurement" (remove the acceleration due to gravity) is that in actual practice, this is not possible with consumer grade IMUs.
> 
> To do so, you need to know the 3D orientation of the IMU very accurately. This page discusses and illustrates the problem: [Using Accelerometers to Estimate Position and Velocity | CH Robotics](https://web.archive.org/web/20180112063505/http://www.chrobotics.com/library/accel-position-velocity)

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [November 9, 2022, 9:32pm UTC](https://forum.arduino.cc/t/need-help-with-getting-acceleration-without-gravity-using-mpu9250-on-a-swing/1051418/8 "2022-11-09T21:32:20Z")

</div>

> [@nervusvagus](#):
>
> #1: not swinging initially, the seat doesnt move. acceleration value is zero so the leds are dark.

Except that the acceleration value is not 0. It is 1G downward because of gravity. Are you using only one axis? What happens if you graph all three?

---

<div class="post-metadata">

**Author:** ![claude\_j\_greengrass](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/claude_j_greengrass/32/636443_2.png) [@claude\_j\_greengrass](https://forum.arduino.cc/u/claude_j_greengrass)\
**Post date:** [November 9, 2022, 9:54pm UTC](https://forum.arduino.cc/t/need-help-with-getting-acceleration-without-gravity-using-mpu9250-on-a-swing/1051418/9 "2022-11-09T21:54:22Z")

</div>

> [@nervusvagus](#):
>
> The raw acceleration values dont seem to help because of the 3 different states of the swing:  
> #1: not swinging initially, the seat doesnt move. acceleration value is zero so the leds are dark.  
> #2: the swing is moving and swinging so the leds get brighter with the swing of the seat  
> #3: the swing moves to one end and stops for a brief moment. This is the trouble point.

Consider using an IMU with a gyroscope. At state #1, all three axis with read 0 (zero). At state #2, at least 1 gyro's will read non-zero. At state #3, all three gyro axis should be zero.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 8, 2023, 10:12pm UTC](https://forum.arduino.cc/t/need-help-with-getting-acceleration-without-gravity-using-mpu9250-on-a-swing/1051418/11 "2023-05-08T22:12:17Z")

</div>

Closed on May 8, 2023, 10:12pm UTC

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
