Search code examples
arduinoaccelerometergyroscope

Arduino Accelerometer: detect braking/ deceleration on a bike


I have the following issue: I'm having an Arduino + accelerometer(MMA8452Q) mounted on a bike. I'm trying to read decelerations when braking. The issue is that, due to design constraints, I can only mount the above hardware setup on the bike rider. Since the rider will change position very often the accelerometer will also change the base values I'm using for measuring decelerations (e.g. when the Z axis is perpendicular to the ground it will measure 1g, but at a different angle it will show a different value), thus rendering my code unusable.

Here are some snippets from my code:

/* Set up thresholds - 0.42g is maximum braking force the bike can do */
maximum_value = 0.7;
soft_braking = 0.4 * maximum_value;
strong_braking = 0.6 * maximum_value;

if(val > strong_braking)
                    { 
                      analogWrite(ledPin, 255);
                    }
                    else if(acc > soft_braking)
                    {
                      analogWrite(ledPin, 127);
                    }

*val is the acceleration on the axis I'm using for measuring deceleration (after some moving average filtering).

How I can reliably compute deceleration considering that the hardware is not having a fixed position relative to the bike frame and also readings are influenced by the angle of the terrain? I'm considering using a 6DOF board (e.g. MPU6050) with a gyro and measure the angle of the board continually and adjust accelerations values based on this, but I do not know on what math I should use for this? Does anybody had the same issue or can direct me to some similar project?


Solution

  • You're going to have to give up on using the acceleration in a particular direction, but you can use the norm of the acceleration measurement and infer the deceleration in the forward direction by assuming that it is roughly perpendicular to the acceleration downwards.

    As for mounting on the rider, I suggest a belt around the waist. This is near the center of gravity of the rider, and you can ignore shortlived accelerations from the rider moving by standing up, etc.

    Don't bother trying to track the angle of the board with a gyro. The movements are so small and the uncertainties accumulate so quickly that you won't get a useful result.

    For the math, let g be the acceleration due to gravity, gb be the acceleration/deceleration of the bicycle, and gx, gy, gz, be the measurements of acceleration from the device. Then by the Pytagorean theorem, the square of the acceleration

    gx^2 + gy^2 + gz^2 = g^2 + gb^2

    gb = sqrt(gx^2 + gy^2 + gz^2 - g^2)