73 Online
5 Member And 68 Guest
Today Visits : 6440
Yesterday Visits : 29386
All Visits : 1979458

Welcome to Electronoobs Q&A, where you can ask questions and receive answers from other members of the community.



Categories


0 like 0 dislike

Hello Sir,

   I have made a drone using this  Drone frame and made a remote using HC12 transceiver and joystick module for controlling,I am able to send the flight instructions, as i am using arduino as a flight controller I have followed the instruction provided in your tutorials but drone is not balancing itself. I have mechanically balanced the drone properly but when i test the drone it it is not properly lifting and balancing itself on controlling, i ahve also tried to change the pid values but nothing happened.Kindly please help me to get rid of this problem.

float Roll_PID_P=0; float Roll_PID_I=0; float Roll_PID_D=0; double Roll_Kp=4.55;//3.55 double Roll_Ki=0.003;//0.003 double Roll_Kd=3.05;//2.05 float Pitch_PID_P=0; float Pitch_PID_I=0; float Pitch_PID_D=0; double Pitch_Kp=4.55;//3.55 double Pitch_Ki=0.003;//0.003 double Pitch_Kd=3.05;//2.05 #include <Wire.h> #include <SoftwareSerial.h> #include <Servo.h> SoftwareSerial HC12(10, 11); int mot_activated=0; float elapsedTime, time, timePrev; int gyro_error=0; float Gyr_rawX, Gyr_rawY, Gyr_rawZ; float Gyro_angle_x, Gyro_angle_y; float Gyro_raw_error_x, Gyro_raw_error_y; int acc_error=0; float rad_to_deg = 180/3.141592654; float Acc_rawX, Acc_rawY, Acc_rawZ; float Acc_angle_x, Acc_angle_y; float Acc_angle_error_x, Acc_angle_error_y; float Total_angle_x, Total_angle_y,Total_Yaw; Servo Front_CW,Front_CCW, Back_CW,Back_CCW; int Pitch_val = 0; int Roll_val = 0; int Yaw_val = 0; int Throttle_val = 0; int i = 0; int escT=1000; int escY=1000; int escR=1000; int escP= 1000; int escFront_CW,escFront_CCW,escBack_CCW,escBack_CW; float Roll_PID,Roll_Error,Roll_Previous_Error; float Pitch_PID,Yaw_PID, Pitch_Error, Pitch_Previous_Error,Yaw_Error,Yaw_Previous_Error; float Roll_Desired_Angle = 0,Yaw_Desired_Angle=0; float Pitch_Desired_Angle = 0; float minMax(float value, float min_value, float max_value) { if (value > max_value) { value = max_value; } else if (value < min_value) { value = min_value; } return value; } void setupMpu6050Registers() { Wire.begin(); Wire.beginTransmission(0x68); Wire.write(0x6B); Wire.write(0x00); Wire.endTransmission(true); Wire.beginTransmission(0x68); Wire.write(0x1B); Wire.write(0x10); Wire.endTransmission(true); Wire.beginTransmission(0x68); Wire.write(0x1C); Wire.write(0x10); Wire.endTransmission(true); Wire.beginTransmission(0x68); Wire.write(0x1A); Wire.write(0x03); Wire.endTransmission(); } void calibrateMpu6050() { if(acc_error==0) { for(int a=0; a<200; a++) { Wire.beginTransmission(0x68); Wire.write(0x3B); Wire.endTransmission(false); Wire.requestFrom(0x68,6,true); Acc_rawX=(Wire.read()<<8|Wire.read())/4096.0 ; Acc_rawY=(Wire.read()<<8|Wire.read())/4096.0 ; Acc_rawZ=(Wire.read()<<8|Wire.read())/4096.0 ; Acc_angle_error_x = Acc_angle_error_x + ((atan((Acc_rawY)/sqrt(pow((Acc_rawX),2) + pow((Acc_rawZ),2)))*rad_to_deg)); Acc_angle_error_y = Acc_angle_error_y + ((atan(-1*(Acc_rawX)/sqrt(pow((Acc_rawY),2) + pow((Acc_rawZ),2)))*rad_to_deg)); if(a==199) { Acc_angle_error_x = Acc_angle_error_x/200; Acc_angle_error_y = Acc_angle_error_y/200; acc_error=1; } } } if(gyro_error==0) { for(int i=0; i<200; i++) { Wire.beginTransmission(0x68); Wire.write(0x43); Wire.endTransmission(false); Wire.requestFrom(0x68,4,true); Gyr_rawX=Wire.read()<<8|Wire.read(); Gyr_rawY=Wire.read()<<8|Wire.read(); Gyro_raw_error_x = Gyro_raw_error_x + (Gyr_rawX/32.8); Gyro_raw_error_y = Gyro_raw_error_y + (Gyr_rawY/32.8); if(i==199) { Gyro_raw_error_x = Gyro_raw_error_x/200; Gyro_raw_error_y = Gyro_raw_error_y/200; gyro_error=1; } } } } void ReadSensor() { Wire.beginTransmission(0x68); Wire.write(0x43); Wire.endTransmission(false); Wire.requestFrom(0x68,4,true); Gyr_rawX=Wire.read()<<8|Wire.read(); Gyr_rawY=Wire.read()<<8|Wire.read(); Gyr_rawZ=Wire.read()<<8|Wire.read(); Wire.beginTransmission(0x68); Wire.write(0x3B); Wire.endTransmission(false); Wire.requestFrom(0x68,6,true); Acc_rawX=(Wire.read()<<8|Wire.read())/4096.0 ; Acc_rawY=(Wire.read()<<8|Wire.read())/4096.0 ; Acc_rawZ=(Wire.read()<<8|Wire.read())/4096.0 ; Gyr_rawX = (Gyr_rawX/32.8) - Gyro_raw_error_x; Gyr_rawY = (Gyr_rawY/32.8) - Gyro_raw_error_y; Gyr_rawZ = (Gyr_rawZ/32.8); Gyro_angle_x = Gyr_rawX*elapsedTime; Gyro_angle_y = Gyr_rawY*elapsedTime; Acc_angle_x = (atan((Acc_rawY)/sqrt(pow((Acc_rawX),2) + pow((Acc_rawZ),2)))*rad_to_deg) - Acc_angle_error_x; Acc_angle_y = (atan(-1*(Acc_rawX)/sqrt(pow((Acc_rawY),2) + pow((Acc_rawZ),2)))*rad_to_deg) - Acc_angle_error_y; Total_angle_x = 0.98 *(Total_angle_x + Gyro_angle_x) + 0.02*Acc_angle_x; Total_angle_y = 0.98 *(Total_angle_y + Gyro_angle_y) + 0.02*Acc_angle_y; Total_Yaw=-Gyr_rawZ; /*Serial.print(Total_angle_x); Serial.print(" | "); Serial.print("Yº: "); Serial.print(Total_angle_y); Serial.println(" "); */ } void getFlightInstruction() { while(HC12.available()) { Throttle_val=HC12.read(); delay(10); Yaw_val=HC12.read(); delay(10); Roll_val=HC12.read(); delay(10); Pitch_val=HC12.read(); delay(10); escT = map (Throttle_val,0,127,1000,2000); escY = map (Yaw_val,0,127,1000,2000); escR = map (Roll_val,0,127,1000,2000); escP = map (Pitch_val,0,127,1000,2000); /*Serial.print("RF: "); Serial.print(escT); Serial.print(" | "); Serial.print("RB: "); Serial.print(escY); Serial.print(" | "); Serial.print("LB: "); Serial.print(escR); Serial.print(" | "); Serial.print("LF: "); Serial.print(escP); Serial.print("\n"); */} } void stopAll() { escFront_CW = 1000; escFront_CCW = 1000; escBack_CW = 1000; escBack_CCW = 1000; } void resetPidController() { Roll_Error = 0; Pitch_Error = 0; Yaw_Error = 0; Yaw_Previous_Error = 0; Roll_Previous_Error = 0; Pitch_Previous_Error = 0; } void pidController() { float Yaw_PID_P=0; float Yaw_PID_I=0; float Yaw_PID_D=0; /* double Yaw_Kp=4.0;//3.55 double Yaw_Ki=0.02;//0.003 double Yaw_Kd=0;//2.05 */ Roll_Desired_Angle = map(escR+20,1000,2000,-20,20); Pitch_Desired_Angle = map(escP+20,1000,2000,-20,20); // Yaw_Desired_Angle= map(escY,1000,2000,-180,180); Roll_Error = Total_angle_y - Roll_Desired_Angle; Pitch_Error = Total_angle_x - Pitch_Desired_Angle; // Yaw_Error = Total_Yaw-Yaw_Desired_Angle; Roll_PID_P = Roll_Kp*Roll_Error; // Yaw_PID_P = Yaw_Kp*Yaw_Error; Pitch_PID_P = Pitch_Kp*Pitch_Error; if(-3 < Roll_Error <3) { Roll_PID_I = Roll_PID_I+(Roll_Ki*Roll_Error); } if(-3 < Pitch_Error <3) { Pitch_PID_I = Pitch_PID_I+(Pitch_Ki*Pitch_Error); } /*if(-3 < Yaw_Error <3) { Yaw_PID_I = Yaw_PID_I+(Yaw_Ki*Yaw_Error); }*/ Roll_PID_D = Roll_Kd*((Roll_Error - Roll_Previous_Error)/elapsedTime); Pitch_PID_D = Pitch_Kd*((Pitch_Error - Pitch_Previous_Error)/elapsedTime); //Yaw_PID_D = Yaw_Kd*((Yaw_Error - Yaw_Previous_Error)/elapsedTime); Roll_PID = Roll_PID_P + Roll_PID_I + Roll_PID_D; Pitch_PID = Pitch_PID_P + Pitch_PID_I + Pitch_PID_D; //Yaw_PID = Yaw_PID_P + Yaw_PID_I + Yaw_PID_D; Roll_PID = minMax(Roll_PID,-400,400); Pitch_PID = minMax(Pitch_PID,-400,400); //Yaw_PID = minMax(Yaw_PID,-400,400); escBack_CW = escT-11 - Roll_PID + Pitch_PID; escFront_CCW = escT -11- Roll_PID - Pitch_PID ; escFront_CW = escT -11+ Roll_PID - Pitch_PID; escBack_CCW = escT -11+ Roll_PID + Pitch_PID; escFront_CW= minMax(escFront_CW,1000,2000); escFront_CCW = minMax(escFront_CCW,1000,2000); escBack_CW = minMax(escBack_CW,1000,2000); escBack_CCW = minMax(escBack_CCW,1000,2000); Roll_Previous_Error = Roll_Error; Yaw_Previous_Error = Yaw_Error; Pitch_Previous_Error = Pitch_Error; Serial.print("Pid Roll: "); Serial.print(Roll_PID); Serial.print(" | "); Serial.print("PID PITCH "); Serial.print(Pitch_PID); Serial.print(" | "); Serial.print("RF: "); Serial.print(escFront_CW); Serial.print(" | "); Serial.print("RB: "); Serial.print(escBack_CCW); Serial.print(" | "); Serial.print("LB: "); Serial.print(escBack_CW); Serial.print(" | "); Serial.print("LF: "); Serial.print(escFront_CCW); Serial.print(" | "); Serial.print("Xº: "); Serial.print(Total_angle_x); Serial.print(" | "); Serial.print("Yº: "); Serial.print(Total_angle_y); Serial.println(" "); escFront_CW = minMax(escFront_CW, 1100, 2000); escBack_CW = minMax(escBack_CW, 1100, 2000); escBack_CCW = minMax(escBack_CCW, 1100, 2000); escFront_CCW = minMax(escFront_CCW, 1100, 2000); } void applyMotorSpeed() { Front_CW.writeMicroseconds(escFront_CW); Front_CCW.writeMicroseconds(escFront_CCW); Back_CW.writeMicroseconds(escBack_CW); Back_CCW.writeMicroseconds(escBack_CCW); } void setup() { Front_CW.attach(3); Front_CCW.attach(4); Back_CW.attach(9); Back_CCW.attach(13); stopAll(); Front_CW.writeMicroseconds(1000); Front_CCW.writeMicroseconds(1000); Back_CW.writeMicroseconds(1000); Back_CCW.writeMicroseconds(1000); pinMode(6,OUTPUT); digitalWrite(6,LOW); pinMode(8,OUTPUT); digitalWrite(8,LOW); pinMode(5,OUTPUT); digitalWrite(5,LOW); HC12.begin(9600); HC12.print(F("AT+C001")); delay(100); digitalWrite(6,HIGH); Serial.begin(115200); delay(1000); setupMpu6050Registers(); time = millis(); calibrateMpu6050(); } void loop() { timePrev = time; time = millis(); elapsedTime = (time - timePrev) / 1000; ReadSensor(); getFlightInstruction(); if(escT < 1100 && escY > 1800 && !mot_activated) { mot_activated=1; } if(mot_activated && escT < 1100 && escY <1100) { mot_activated=0; } pidController(); if(mot_activated) applyMotorSpeed(); else if ((!mot_activated)) { resetPidController(); stopAll(); applyMotorSpeed(); } }

Above is the code for the drone, and below is the code for the remote

#include<SoftwareSerial.h> #include<math.h> SoftwareSerial HC12(5,6); int SW_pin1= 2; int X_pin1 =A0; int Y_pin1= A1; int SW_pin2 = 5; int X_pin2 = A3; int Y_pin2= A4; int sigX1; int sigX2; int sigY1; int sigY2; float mapX1; float mapX2; float mapY1; float mapY2; char sig; void setup() { pinMode(7,OUTPUT); digitalWrite(7,LOW); Serial.begin(115200); HC12.begin(9600); HC12.print(F("AT+C001")); delay(100); digitalWrite(7,HIGH); pinMode(SW_pin1, INPUT); digitalWrite(SW_pin1, HIGH); pinMode(SW_pin2, INPUT); digitalWrite(SW_pin2, HIGH); } void loop() { sigX1=analogRead(X_pin1); mapX1=sigX1/8.0; sigY1=analogRead(Y_pin1); mapY1=sigY1/8.0; sigX2=analogRead(X_pin2); mapX2=sigX2/8.0; sigY2=analogRead(Y_pin2); mapY2=sigY2/8.0; HC12.write(mapX1); HC12.write(mapY1); HC12.write(mapX2); HC12.write(mapY2); delay(40); Serial.print("RF: "); Serial.print(mapX1); Serial.print(" | "); Serial.print("RB: "); Serial.print(mapY1); Serial.print(" | "); Serial.print("LB: "); Serial.print(mapX2); Serial.print(" | "); Serial.print("LF: "); Serial.print(mapY2); Serial.print("\n"); }

Thanks for your time and consideration

Regards,

Harsh

in Arduino by (120 points)

1 Answer

0 like 0 dislike
On which pins are your motors?
by (160 points)
On pin 3,4,9,13
Is this on arduino nano. If it is on nano you must to connect on pwm pins(3,5,6,9,10,11). If you connect this on nano you only have 2 pwm pons(3,9).
i am using Arduino Mega, Motors are rotating and i am able to control the speed from my joystick module problem is in balancing, its not balancing itself
...