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.

 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;
}

You need to be a member of diydrones to add comments!

Join diydrones

Email me when people reply –

Activity