Quadcopter

Hi guys,
So, I am doing a school project which requires me to use a sonar sensor on a quadcopter so that it can avoid obstacle when it gets too close to it. The quadcopter should be able to sense obstacle from the left as the sonar sensor is mounted there. I am currently stuck at a problem which requires me to use PID to stabilise the quadcopter at a fixed setpoint. I am only using the P and D constant and it seems that no matter what value I put for the D value, the quadcopter will overshoot and this overshoot from its setpoint will be progression greater till it hit the obstacle. Anyone know why and how to solve it? Between I never use the I constant because my supervisor say there is not a need to use it.

Right now you're the only one who knows anything about how this quadcopter is built or the code that is running it. I don't see how anyone could possibly help you under those conditions.

Sit down and think for a minute. Can you imagine any details we might need? I'm guessing something is wrong with your code. That's about all that can be said at this point.

I’m sry I can’t post the whole code as my supervisor won’t not allow me to do so. But here is part of my code. The value of Kp is currently 4 and the value of D is 0.01 . By the way i am using the sonar sensor MB1010 LV-MaxSonar-EZ1.

void loop()
{

update_sonar();
.
.
.
if (SONAR == 1)
{
moveLeft();
}
.
.
.

}

void update_sonar(void)
{
switch (sonarstate)
{
case 0:
{
digitalWrite(triggerPin1, HIGH);
delayMicroseconds(20);
digitalWrite(triggerPin1, LOW);
}
sonartimer = millis();

sonarstate = 1;
break;

case 1:
if (millis() - sonartimer > readDelay)
{

Left = (analogRead(anPin1) * 2.54) / 2;

sonarstate = 0;

}

break;
}
}

void moveRight()
{
prevErrLeft = currErrorLeft;
currErrorLeft = setpoint - Left;
velocityLeft = (currErrorLeft - prevErrLeft) / readDelay;
op2 = kp * currErrorLeft;
dFactorLeft = D * velocityLeft;
error = op2 + dFactorLeft;

if ( error < -250) {
error = -250 ;
}
else if (error > 250) {
error = 250;
}

roll = roll - error;
}

chuankianGB:
I'm sry I can't post the whole code as my supervisor won't not allow me to do so.

That's too bad. You won't be able to get much help here. Is this a school project? Maybe your supervisor wants you to do the work yourself.

Is the code proprietary? If so you won't get much sympathy here as this is an open source community.

How does the quadcopter behave with just P?

Are you sure D is the best extra component to add?

Have you tried using I instead of D?

Does the quadcopter hover on its own? Is it stable?

chuankianGB:

if (SONAR == 1)

{
   moveLeft();
 }





if (millis() - sonartimer > readDelay)
     {
       Left = (analogRead(anPin1) * 2.54) / 2;
       sonarstate = 0;
     }

It is unfortunate you can't post working code. Then we could see if the variable 'SONAR' had anything to do with variable 'Left'. If not, that could prevent your collision avoidance from working.

I sure hope that moveLeft() moves the quadcopter AWAY from the object which you said can only be detected on the left. So, moveLeft() moves right?!?

Hi,
If your copter is stable, it must have a stabilising control system helping it.
If you introduce another to get it to tip and turn, you will be fighting agianst the existing stabilising system.

How are you controlling the motors and is there a stabilising controller?

Tom... :slight_smile:

Hi guys. Thank you all for the help. I found the problem. The problem is because the value of my D is too small.

Now I have another problem. I am using two sonar sensors, one on the left, one on the right and I am supposed
to make the quadcopter such that the quadcopter is able to hover between two obstacles, such as walls, and I will only nid to control the pitch and the throttle, the program will be able to auto correct the roll such that the
quadcopter is able to hover between two walls.

I only know how to write a PID program such that it will auto correct only from one wall, using one sonar sensor. The program is shown above. Anyone know how to do a PID program such that both sonar sensors is able to hover the quadcopter in the middle position of two walls? Thanks.

Do you really need to use two PID processes?

Can't you measure the distance to either side, average them for the desired setpoint, then use PID control on one side only to reach/maintain that setpoint?

(Also, are you allowed to use a PID library, or must you do it the hard way?)

Nope I dont have to use two PID processes.

How do I average them for the desired setpoint, then use PID control on one side only to reach/maintain that setpoint using the Left calculation?

chuankianGB:
Nope I dont have to use two PID processes.

How do I average them for the desired setpoint, then use PID control on one side only to reach/maintain that setpoint using the Left calculation?

I thought I was pretty clear. I know very little about PID control, but surely if you measure the distance to the wall on the left and the distance to the wall on the right, then average those distances, you automatically have the distance that you need to be from one wall. That becomes your setpoint for the PID calculation, the error is the difference between that setpoint and your current position, so you plug those values into your PID calculation and just work from one side. Since you say you're just required to hover in that position, the distances won't vary so you only need to average the distances to either side once.

I don't see that you have any need to do auto-correction from both sides as you suggested.