MPU6050 Data averaging

i have an MPU6050 module hooked up to an arduino uno and a number of servos connected to a PCA9685, i have the x and y axis of the gyroscope hooked up to control 2 of the servos, the problem i am having is that the motion comes out jittery.

currently i take a moving average of the raw gyro data then using some maths i mix that for the servo control then i take a second average and it is still comes out erratic and jittery.

i have tried a few different theory's to "smooth" the data but i am still suffering the jitter.

  //----------------------------------------------//
                                //Position Mixing//
                                
  int GX = map(MnXAv, 0, 400, -100, 100);
  int GXa = map(MnXAv, 0, 400, 100, -100);

  float SnL = GX + MnYAv;
  float SnR = GXa + MnYAv;
  //------------------------------------------------------------------Averging mixed values--//
  int SnLRead = 10; // number of samples
  int SnLSum = 0; //sum of samples
  for(int k = 0; k <SnLRead; k++){
    SnLSum += SnL;
    delay(1);
  }
  int SnLAv = SnLSum / SnLRead;

any ideas on how i can get a smoother output to the servo positions

a way to smooth a signal is to merge part of the previous value with the new value. The relative percentage defines how fast your curve will vary

// the value you are really using for driving the motor
double currentValue;

... // later in the code when you acquire the data
double newValue = getNewReading(); // get the true instant reading
currentValue  = 0.9 * currentValue  + 0.1 * newValue; // 90% of the old value, 10% of the new value

this way you don't need any array or multiple readings, you smooth your output as you go. if it's too slow to react to changes, then increase the 10%, if it's still too jerky then reduce the 10%

of course the fundamental CICO rule of signal processing applies : Crap In, Crap out :slight_smile: (aka GIGO = Garbage in, garbage out)

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