Complimentary filter

Hey guys, I am making a drone.

I need the values of the gyro, but after that i read i need to use a complimentary filter to filter the values such that, the small unnecessary vibrations don’t cause unwanted spikes in the output.

I could not understand the program they gave int the video i saw at 27 min
(Embedded Programming for Quadcopters - YouTube)

I am using a mpu6050 module and a arduino uno.

this is my program, cam someone implement the filter and help me pls.
i don’t need to filter the z axis value

#include <Wire.h>
const int MPU_addr=0x68; 
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
int minVal=265; int maxVal=402;
double x;         //A4 (SDA), A5 (SCL)
double y; 
double z;

void setup(){

  Wire.begin();
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x6B);
  Wire.write(0);
  Wire.endTransmission(true);
  Serial.begin(9600); } 

void loop(){ 
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x3B);
  Wire.endTransmission(false); 
  Wire.requestFrom(MPU_addr,14,true);

  AcX=Wire.read()<<8|Wire.read(); 
  AcY=Wire.read()<<8|Wire.read();
  AcZ=Wire.read()<<8|Wire.read();
 
  int xAng = map(AcX,minVal,maxVal,-90,90);
  int yAng = map(AcY,minVal,maxVal,-90,90);
  int zAng = map(AcZ,minVal,maxVal,-90,90);

  x= RAD_TO_DEG * (atan2(-yAng, -zAng)+PI);
  y= RAD_TO_DEG * (atan2(-xAng, -zAng)+PI);
  z= RAD_TO_DEG * (atan2(-yAng, -xAng)+PI);

//------------------------------------------
if(y>180){
  y=y-360;
}
else if(y>0){  //          this part is 
  y=y;         //          to conver 0-360
}              //          to -180 to 180
if(x>180){     //          it works perfectly
  x=x-360;
}
else if(x>0){
  x=x;
}                                               
//-----------------------------------------------
Serial.print("XYZ");
Serial.print("    ");
Serial.print(x);                //need to filter theese calues
Serial.print("    ");
Serial.println(y);
//Serial.print("    ");
//Serial.println(z);
delay(50);

}

Hey, I am trying to make my own flight controller program for my drone.

I have done a self balancing robot in the past.

My progress:
My transmitter and receiving program is done.
I have got raw values from the gyroscope and got the ‘angle’ values and ‘rate of rotation’ values

Problem:
1.I need to use a complimentary filter to avoid noise in the angle value. The rate of rotation value works good.
2. I don’t know how to use both the rate of rotation and the angle to correctly apply to the pid algorithm.

heres my program

#include <SoftwareSerial.h>
#include<Wire.h>
 
int a;
int b;
int c;
int d;
int e;
int f;
 
String input;
int boundLow;
int boundHigh;
const char delimiter = ',';
SoftwareSerial mySerial(11, 10);
 
const int MPU_addr=0x68; 
int16_t AcX,AcY,AcZ,GyX,GyY,GyZ;
double Tmp;
int minVal=265; 
int maxVal=402;
double x;         //A4 (SDA), A5 (SCL)
double y; 
double z;








void setup() {
 Serial.begin(9600);
  mySerial.begin(9600);
  
  Wire.begin();
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x6B);  // PWR_MGMT_1 register
  Wire.write(0);     // set to zero (wakes up the MPU-6050)
  Wire.endTransmission(true);
}

void loop() {
  /*variables of HC-12    
   *    a,b,c,d,e,f
   *    
   *    required vals
   *    GyX,GyY,GyZ   - perfeect in degreees per sec
   *    x,y need to be avoided noise
   */
   
   decoding();  //ready
   mpu6050();   //ready
   pid_calculation_and_output();
   delay(100);
}




























void mpu6050(){
  Wire.beginTransmission(MPU_addr);
  Wire.write(0x3B);  // starting with register 0x3B (ACCEL_XOUT_H)
  Wire.endTransmission(false);
  Wire.requestFrom(MPU_addr,14,true);  // request a total of 14 registers
  
  AcX=Wire.read()<<8|Wire.read();  // 0x3B (ACCEL_XOUT_H) & 0x3C (ACCEL_XOUT_L)    
  AcY=Wire.read()<<8|Wire.read();  // 0x3D (ACCEL_YOUT_H) & 0x3E (ACCEL_YOUT_L)
  AcZ=Wire.read()<<8|Wire.read();  // 0x3F (ACCEL_ZOUT_H) & 0x40 (ACCEL_ZOUT_L)
  Tmp=Wire.read()<<8|Wire.read();  // 0x41 (TEMP_OUT_H) & 0x42 (TEMP_OUT_L)
  GyX=Wire.read()<<8|Wire.read();  // 0x43 (GYRO_XOUT_H) & 0x44 (GYRO_XOUT_L)
  GyY=Wire.read()<<8|Wire.read();  // 0x45 (GYRO_YOUT_H) & 0x46 (GYRO_YOUT_L)
  GyZ=Wire.read()<<8|Wire.read();  // 0x47 (GYRO_ZOUT_H) & 0x48 (GYRO_ZOUT_L)
  //until here raw values are obtained
  
  int xAng = map(AcX,minVal,maxVal,-90,90);
  int yAng = map(AcY,minVal,maxVal,-90,90);
  int zAng = map(AcZ,minVal,maxVal,-90,90);
  
  x= RAD_TO_DEG * (atan2(-yAng, -zAng)+PI);
  y= RAD_TO_DEG * (atan2(-xAng, -zAng)+PI);
  z= RAD_TO_DEG * (atan2(-yAng, -xAng)+PI);
//here gyro is alnoe converter
//--------------------------------------- 
  GyX=GyX/100;
  GyX=GyX+2.73;
  GyY=GyY/100;
  GyY=GyY-2.78;
  GyZ=GyZ/100;           //   calibrated
  GyZ=GyZ+0.58;
//--------------------------------------
  if(y>180){
  y=y-360;
  }
  else if(y>0){  //        this part is 
  y=y;         //          to convert 0-360
  }              //        to -180 to 180
  if(x>180){     //        it works perfectly
  x=x-360;
  }
  else if(x>0){
  x=x;
  }
//-----------------------------------------
  Tmp=Tmp/340.00+36.53;
}






void decoding(){
  if(mySerial.available()){
    input = mySerial.readStringUntil('n');
    if (input.length() > 0){
       boundLow = input.indexOf(delimiter);
       a = input.substring(0, boundLow).toInt();
       boundLow = input.indexOf(delimiter);
       b = input.substring(boundLow+1, boundHigh).toInt(); 
       boundLow = input.indexOf(delimiter, boundLow+1);
       c = input.substring(boundLow+1, boundHigh).toInt();       
       boundLow = input.indexOf(delimiter, boundLow+1);
       d = input.substring(boundLow+1, boundHigh).toInt();      
       boundLow = input.indexOf(delimiter, boundLow+1);
       e = input.substring(boundLow+1, boundHigh).toInt();      
       boundLow = input.indexOf(delimiter, boundLow+1);
       f = input.substring(boundLow+1, boundHigh).toInt();      
       boundHigh = input.indexOf(delimiter, boundLow+1);
      }
  }
}






void pid_calculation_and_output(){
  //   i dont know what to do here
}

someone pls help me int applying the complimentary filter and the pid algorithm.

Hi PROBOT135. Are you offering to pay someone to provide this help? The reason I ask is because that is the purpose of this "Gigs and Collaborations" forum board you have posted to. If you are willing to pay, then it's good to make that clear so the people who follow this board and offer their services will be aware that is your intent. If not, then I should move your topic to the appropriate board.

@PROBOT135's parallel post in the "Project Guidance" board:

@PROBOT135 has also posted about this in Gigs and Collaborations:

Topics merged

If you are prepared to pay someone to write the program for you please report the topic to a moderator and it can be moved to Gigs and Collaborations rather than have two topics in different places on the same subject

ok thanks, i was not intending to pay someone, anyway thanks for shifting this post