Hello,
I am building a drone using an Arduino board as an FC and I got stuck. My PID controller stabilizes the drone when It takes of in the pitch and roll axis quite well, but the drone spins counter-clockwise when I try and take off. When I try to hold the drone in my hand without the propellers, and I try to rotate it in the counter-clockwise direction, it speeds up the motors more than when I try to spin it in the clockwise direction. Bellow is my code. Any help is appreciated.
#include <ESP32Servo.h>
#include <Wire.h>
float RatePitch, RateRoll, RateYaw;
float RateCalibrationPitch, RateCalibrationRoll, RateCalibrationYaw;
int RateCalibrationNumber;
uint32_t LoopTimer;
float DesiredRateRoll, DesiredRatePitch, DesiredRateYaw;
float ErrorRateRoll, ErrorRatePitch, ErrorRateYaw;
float InputRoll, InputThrottle, InputPitch, InputYaw;
float PrevErrorRateRoll, PrevErrorRatePitch, PrevErrorRateYaw;
float PrevItermRateRoll, PrevItermRatePitch, PrevItermRateYaw;
float PIDReturn[]={0, 0, 0};
//PID constants
float PRateRoll=1; float PRatePitch=1; float PRateYaw=1;
float IRateRoll=3.5; float IRatePitch=3.5; float IRateYaw=3.5;
float DRateRoll=0.03; float DRatePitch=0.03; float DRateYaw=0.03;
float MotorInput1, MotorInput2, MotorInput3, MotorInput4;
float pr,ir,dr;
float pp,ip,dp;
float py,iy,dy;
#include "CRSFforArduino.hpp"
#define USE_SERIAL_PLOTTER 0
Servo ESC1;
Servo ESC2;
Servo ESC3;
Servo ESC4;
bool mainState;
int gyroLEDPin = D6;
int mainLEDPin = D7;
int switchPin = D8;
int servoPin1 = D9;
int servoPin2 = D10;
int servoPin3 = D11;
int servoPin4 = D12;
int channel1;
int channel2;
int channel3;
int channel4;
int throttleSafety;
CRSFforArduino *crsf = nullptr;
int rcChannelCount = crsfProtocol::RC_CHANNEL_COUNT;
const char *rcChannelNames[] = {
"A",
"E",
"T",
"R",
"Aux1",
"Aux2",
"Aux3",
"Aux4",
"Aux5",
"Aux6",
"Aux7",
"Aux8",
"Aux9",
"Aux10",
"Aux11",
"Aux12"};
void onReceiveRcChannels(serialReceiverLayer::rcChannels_t *rcChannels);
void gyro_signals(void) {
Wire.beginTransmission(0x68);
Wire.write(0x1A);
Wire.write(0x05);
Wire.endTransmission();
Wire.beginTransmission(0x68);
Wire.write(0x1B);
Wire.write(0x08);
Wire.endTransmission();
Wire.beginTransmission(0x68);
Wire.write(0x43);
Wire.endTransmission();
Wire.requestFrom(0x68,6);
int16_t GyroX=Wire.read()<<8 | Wire.read();
int16_t GyroY=Wire.read()<<8 | Wire.read();
int16_t GyroZ=Wire.read()<<8 | Wire.read();
RateRoll=(float)GyroX/65.5;
RatePitch=(float)GyroY/65.5;
RateYaw=(float)GyroZ/65.5;
}
void pid_equation(float Error, float P , float I, float D, float PrevError, float PrevIterm)
{
float Pterm=P*Error;
float Iterm=PrevIterm+I*(Error+PrevError)*0.004/2;
if (Iterm > 400) Iterm=400;
else if (Iterm <-400) Iterm=-400;
float Dterm=D*(Error-PrevError)/0.004;
float PIDOutput= Pterm + Iterm + Dterm;
if (PIDOutput>400) PIDOutput=400;
else if (PIDOutput <-400) PIDOutput=-400;
PIDReturn[0]=PIDOutput;
PIDReturn[1]=Error;
PIDReturn[2]=Iterm;
PIDReturn[3]=Pterm;
PIDReturn[4]=Dterm;
}
void reset_pid(void)
{
PrevErrorRateRoll=0; PrevErrorRatePitch=0; PrevErrorRateYaw=0;
PrevItermRateRoll=0; PrevItermRatePitch=0; PrevItermRateYaw=0;
}
void main_reset(void)
{
RateCalibrationRoll=0;
RateCalibrationPitch=0;
RateCalibrationYaw=0;
delay(500);
digitalWrite(gyroLEDPin, HIGH);
delay(1000);
for (RateCalibrationNumber=0; RateCalibrationNumber<2000; RateCalibrationNumber ++) {
gyro_signals();
RateCalibrationRoll+=RateRoll;
RateCalibrationPitch+=RatePitch;
RateCalibrationYaw+=RateYaw;
delay(1);
}
RateCalibrationRoll/=2000;
RateCalibrationPitch/=2000;
RateCalibrationYaw/=2000;
delay(500);
digitalWrite(gyroLEDPin, LOW);
digitalWrite(mainLEDPin, HIGH);
Serial.println("MPU6050 Gyro calibrated!");
crsf = new CRSFforArduino();
if (!crsf->begin())
{
crsf->end();
delete crsf;
crsf = nullptr;
Serial.println("CRSF for Arduino initialisation failed!");
while (1)
{
delay(10);
}
}
rcChannelCount = rcChannelCount > crsfProtocol::RC_CHANNEL_COUNT ? crsfProtocol::RC_CHANNEL_COUNT : rcChannelCount;
crsf->setRcChannelsCallback(onReceiveRcChannels);
delay(1000);
Serial.println("Ready");
delay(1000);
crsf->update();
//Safety loop - Throttle into lowest position
while (channel3 < 980 || channel3 > 1050)
{
crsf->update();
delay(10);
}
}
void loop_pause(void)
{
while(mainState == 0)
{
delay(50);
mainState = digitalRead(switchPin);
}
main_reset();
}
void setup()
{
Serial.begin(115200);
ESC3.attach(servoPin3,989,2000);
ESC4.attach(servoPin4,989,2000);
ESC1.attach(servoPin1,989,2000);
ESC2.attach(servoPin2,989,2000);
pinMode(gyroLEDPin, OUTPUT);
pinMode(mainLEDPin, OUTPUT);
pinMode(switchPin, INPUT);
Wire.setClock(400000);
Wire.begin(D4, D5);
delay(250);
Wire.beginTransmission(0x68);
Wire.write(0x6B);
Wire.write(0x00);
Wire.endTransmission();
mainState = digitalRead(switchPin);
while(mainState == 0)
{
delay(50);
digitalWrite(gyroLEDPin, LOW);
digitalWrite(mainLEDPin, LOW);
mainState = digitalRead(switchPin);
}
main_reset();
LoopTimer=micros();
}
//Main cycle of program
void loop()
{
mainState = digitalRead(switchPin);
if(mainState == 0)
{
digitalWrite(gyroLEDPin, LOW);
digitalWrite(mainLEDPin, LOW);
ESC1.write(989);
ESC2.write(989);
ESC3.write(989);
ESC4.write(989);
loop_pause();
}
if(mainState == 1)
{
digitalWrite(mainLEDPin, HIGH);
gyro_signals();
RateRoll-=RateCalibrationRoll;
RatePitch-=RateCalibrationPitch;
RateYaw-=RateCalibrationYaw;
//Update from ELRS receiver
crsf->update();
channel3 = (crsf->rcToUs(crsf->getChannel(3)));
//Desired values between -75 and 75 (channels have value between 1000 and 2000)
DesiredRateRoll=0.15*(channel1-1500);
DesiredRatePitch=0.15*(channel2-1500);
DesiredRateYaw=0.15*(channel4-1500);
//Between 1000 and 2000
InputThrottle=channel3;
ErrorRateRoll=DesiredRateRoll-RateRoll;
ErrorRatePitch=DesiredRatePitch-RatePitch;
ErrorRateYaw=DesiredRateYaw-RateYaw;
//Calls the PID equation procedure for left-right
pid_equation(ErrorRateRoll, PRateRoll, IRateRoll, DRateRoll, PrevErrorRateRoll, PrevItermRateRoll);
InputRoll=PIDReturn[0];
PrevErrorRateRoll=PIDReturn[1];
PrevItermRateRoll=PIDReturn[2];
pr = PIDReturn[3];
ir = PIDReturn[2];
dr = PIDReturn[4];
//Calls the PID equation procedure for forward-back
pid_equation(ErrorRatePitch, PRatePitch, IRatePitch, DRatePitch, PrevErrorRatePitch, PrevItermRatePitch);
InputPitch=PIDReturn[0];
PrevErrorRatePitch=PIDReturn[1];
PrevItermRatePitch=PIDReturn[2];
pp = PIDReturn[3];
ip = PIDReturn[2];
dp = PIDReturn[4];
//Calls the PID equation procedure for rotation
pid_equation(ErrorRateYaw, PRateYaw, IRateYaw, DRateYaw, PrevErrorRateYaw, PrevItermRateYaw);
InputYaw=PIDReturn[0];
PrevErrorRateYaw=PIDReturn[1];
PrevItermRateYaw=PIDReturn[2];
py = PIDReturn[3];
iy = PIDReturn[2];
dy = PIDReturn[4];
if (InputThrottle > 1800) InputThrottle = 1800;
MotorInput1= 1.024 *(InputThrottle+InputRoll-InputPitch-InputYaw);
MotorInput2= 1.024 *(InputThrottle+InputRoll+InputPitch+InputYaw);
MotorInput3= 1.024 *(InputThrottle-InputRoll-InputPitch+InputYaw);
MotorInput4= 1.024 *(InputThrottle-InputRoll+InputPitch-InputYaw);
if (MotorInput1 >= 2000) MotorInput1 = 1999;
if (MotorInput2 >= 2000) MotorInput2 = 1999;
if (MotorInput3 >= 2000) MotorInput3 = 1999;
if (MotorInput4 >= 2000) MotorInput4 = 1999;
//If motor should go under minimal throttle keep it at minimal 1180 (18%)
int ThrottleIdle=1180;
if (MotorInput1 < ThrottleIdle) MotorInput1 = ThrottleIdle;
if (MotorInput2 < ThrottleIdle) MotorInput2 = ThrottleIdle;
if (MotorInput3 < ThrottleIdle) MotorInput3 = ThrottleIdle;
if (MotorInput4 < ThrottleIdle) MotorInput4 = ThrottleIdle;
//Motor cutoff
int ThrottleCutOff=1000;
if (channel3<1050)
{
MotorInput1=ThrottleCutOff;
MotorInput2=ThrottleCutOff;
MotorInput3=ThrottleCutOff;
MotorInput4=ThrottleCutOff;
reset_pid();
}
//Hold switch to fly
if(throttleSafety == 2000)
{
ESC1.write(MotorInput1);
ESC2.write(MotorInput2);
ESC3.write(MotorInput3);
ESC4.write(MotorInput4);
}
else
{
ESC1.write(989);
ESC2.write(989);
ESC3.write(989);
ESC4.write(989);
}
}
while (micros() - LoopTimer < 4000);
LoopTimer=micros();
}
void onReceiveRcChannels(serialReceiverLayer::rcChannels_t *rcChannels)
{
if (rcChannels->failsafe == false)
{
/* Print RC channels every 100 ms. */
unsigned long thisTime = millis();
static unsigned long lastTime = millis();
/* Compensate for millis() overflow. */
if (thisTime < lastTime)
{
lastTime = thisTime;
}
if (thisTime - lastTime >= 100)
{
lastTime = thisTime;
channel2 = (crsf->rcToUs(crsf->getChannel(2)));
channel1 = (crsf->rcToUs(crsf->getChannel(1)));
channel4 = (crsf->rcToUs(crsf->getChannel(4)));
channel3 = (crsf->rcToUs(crsf->getChannel(3)));
channel3 = map(channel3, 989, 2017, 1000, 2000);
throttleSafety = (crsf->rcToUs(crsf->getChannel(5)));
channel2 = map(channel2, 989, 2010, 1000, 2000);
channel1 = map(channel1, 989, 2010, 1000, 2000);
channel4 = map(channel4, 994, 2012, 1000, 2000);
if(channel1 > 2000)
{
channel1 = 2000;
}
else if(channel1 < 1000)
{
channel1 = 1000;
}
if(channel2 > 2000)
{
channel2 = 2000;
}
else if(channel2 < 1000)
{
channel2 = 1000;
}
if(channel4 > 2000)
{
channel4 = 2000;
}
else if(channel4 < 1000)
{
channel4 = 1000;
}
Serial.println("-------------------------------------------------");
Serial.print("Motor 1: ");
Serial.print(MotorInput1);
Serial.println();
Serial.print("Motor 2: ");
Serial.print(MotorInput2);
Serial.println();
Serial.print("Motor 3: ");
Serial.print(MotorInput3);
Serial.println();
Serial.print("Motor 4: ");
Serial.print(MotorInput4);
Serial.println();
Serial.print("Roll: ");
Serial.print(InputRoll);
Serial.print(" Error: ");
Serial.print(ErrorRateRoll);
Serial.print(" PID: ");
Serial.print(pr);
Serial.print(" ");
Serial.print(ir);
Serial.print(" ");
Serial.print(dr);
Serial.println();
Serial.print("Pitch: ");
Serial.print(InputPitch);
Serial.print(" Error: ");
Serial.print(ErrorRatePitch);
Serial.print(" PID: ");
Serial.print(pp);
Serial.print(" ");
Serial.print(ip);
Serial.print(" ");
Serial.print(dp);
Serial.println();
Serial.print("Yaw: ");
Serial.print(InputYaw);
Serial.print(" Error: ");
Serial.print(ErrorRateYaw);
Serial.print(" PID: ");
Serial.print(py);
Serial.print(" ");
Serial.print(iy);
Serial.print(" ");
Serial.print(dy);
Serial.println();
}
}
}