Accelerometer and movement logic problem

I have an accelerometer value I get from an Mpu9250 between 0 and 250.
250 being a high accelerometer value reflecting movement / motion on the accelerometer:

I have 3 things I do with this value:
#1- there is no movement :
To get this no movement I am measuring movement value up to 20 from accelerometer- I do action1 if its as such.

#2- There is movement (or minimal movement!) - If i sense movement higher than 20 that cuts as movement. So I do action 2 based on movement.
HOWEVER I do an action based on sensor value so even with minimal movements, so values up to 20 also count and yet I cant get them since I set that as a criteria for no movement #1 above. Anything below 20 is cut for no movement.
I tried setting a timer for the no movement but still failed at the logic, I cant figure out of i should time when a movement is sensed (ie sensor larger than 20) or when a nonmovement is sensed (ie sensor smaller than 20)

#3- there is movement for 3 minutes (180000ms) I do action 3.

Unfortunately I am stuck in the beginning of this process, the most important part, the logic above with measure movement and no movement seems flawed. Is there any way to measure no movement and yet still be able to act on small movements?

You need to learn a bit more about how an accelerometer works. They measure accelerations, positive and negative, not movement.

Acceleration is zero for steady motion in a straight line, and unfortunately, the accelerometer measures the acceleration due to gravity in addition to that due to other forces.

When an object is sitting at rest on a table, it is subject to two forces that cancel to produce zero acceleration: gravity directed down, and an upwards force exerted by the table. But the accelerometer reports the difference in that particular case, registering acceleration in the upwards direction.

1 Like

Are you only using one axis (X, Y, or Z) and ignoring the other two? If not, do you calculate some kind of sum or average to get your one value?

Accelerometers measure acceleration: a change of speed - not the speed.
If you want to measure 3 mins. acceleration (seeing a value) - it works just on a rocket:
just a rocket would increase its speed over a period of 3 minutes.

Acceleration value 0 does not mean the object has stopped, is not in motion: it just means: the speed does not increase (or decrease) anymore. The velocity is constant.
You can just assume the object has stopped again when you saw the exact same amount of negative acceleration (braking) after a positive acceleration (speeding up). Then, object is back to same speed as before (but it does not mean stationary, when staring point was already a constant velocity).

BTW:
I would consider 3-axis calculation: use vectors in order to know how your object is moving in a 3D space. If you ignore other axis - your result is wrong: if it moves mainly in X, but also a bit in Y direction (or just in Z direction), the vector for X only taken is wrong: the speed of the object is for sure much larger (in 3D space) - consider a 3D vector.

In order to know the speed (and distance), you have to know the inertia (mass) of your object: if you measure the acceleration, combined with its mass - you can calculate the speed (change) and with it the distance it has traveled.

If you ignore any tiny acceleration - you get completely off: even a small speed change - ignored, can result in massive errors: acceleration of 1 over a period 3 minutes might end up in object is now moving with miles per hours after 3 minutes.
So, 20 from accelerometer can be drastic speed change. Any value not 0 tells you: the object is speeding up, getting faster and faster.

Acceleration measurement is "inertial navigation": any not zero value means: it gets faster, the speed changes, it moves even faster over a distance. It is "DELTA calculation": is the same amount of acceleration (integrated over time) the same as de-acceleration: then the object might have stopped (when it was stationary), or is back to original velocity. If the values for "speed up" and "break" are not the same: the system has entered a new different speed, but it keeps going to move.

The tiniest bit of acceleration tells you "there was a speed change". It is 3D space math. And with inertia (mass) considered - you can predict speed, distance and where it should be now (when starting condition is known).
Just acceleration value does not tell you anything, except: "it changes its speed".
And zero acceleration does not tell you it has stopped: just if up and down are equal values. Otherwise it keeps moving with the new (constant) speed.

The 9250 also has a magnometer. If you are just concerned with "movement" then you could use magnetic field variations instead of acceleration. The magnometer chip can detect the earth's magnetic field on 3 axis and any "movement" is bound to affect the measurements. However, it would be also be affected by metal or magnets near the 9250. If you want to use acceleration then you would need to keep track of the times between accelerometer readings to generate velocity in each dimension. When the velocities are zero (or close enough) you could decide it is at rest. Velocity would be the integral of the acceleration over the interval, or more simply vx += acceleration_x * time_since_last_reading; Same with y and z axis. Now, since you are only concerned with "movement" you probably don't need to worry about the gyroscope features to ascertain if your 9250 is tilted as the above should catch "movement".

And depending on your needs, there are other types of sensors that may do a better job. There are vibration sensors available, for instance.

Thank you I do need to learn more about accelerometers.

However: If I hold the accelerometer in place, without movement, and tilted the accelerometer, I would still measure an acceleration value. So isnt it correct to say that im measuring movement?

Im measuring the swing of a childrens tree swing.

The accelerometer always reports the acceleration due to gravity, projected along any axis that has a vertical component. Plus other accelerations that might be present.

the mag values I got from my mpu9250 library turned out to be "zero" for some reason and I didnt investigate further. I'm using Accel Z and Gyro X values and they seemed to be giving me a smooth wave-like data that I need from a childrens swing movement.

I'm using Z and X axis'.

I use accel Z to get the movement of a childrens swing and output that to lights.

I use Gyro X values to determine the direction of the swing, ie going forward or backward and I also use that to adjust lights output only on one direction.

From the logic part I meant basically how to structure the code and so far the below seems to have worked. I still need a lot to learn in programming, nested if's and sequential if's and also if's with multiple AND and OR criteria.... thats what i meant by logic. I'm a forever newbie, so I apologize:

anyways this is what i did:
set a timestamp1
and timestamp2

IF movement is larger than 25 set timestamp1.

If timestamp1 is less than 3 seconds do the movement code
if timestamp2 is more than 3 minutes do action 3.

if movement is less than 25 and timestamp1 is larger than 3 seconds
do the nonmovement code.

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