V5: Using PD for a gyro in VCS

Hello. I am trying to use VCS to program a PD control loop for turning using the average of the rotations of two gyro, but something has gone wrong as the robot will do nothing but spin counterclockwise endlessly. The code is below. Thanks for any help.


float error=0.0;
int integral=0;
int derivative=0;
int speed;
int previous_error=0;

//these three variables below need to be adjusted for each robot to make the motors move effiecently
float kp=1.795;//the proportional term

float kd=0.0;//how fast is error changing. 

double Angle;
bool Continue = true;
double theta;

void TurnRobot()
{
  while(Continue)
  {
    theta = (Gyro1.value(vex::rotationUnits::deg)+Gyro2.value(vex::rotationUnits::deg))/2;
    //the gyro reads upto 1/10th of a degree so its max value is 3600 so any angle measure given must be multiplied by 10 or you will turn a very small amount
    error=(Angle) - theta;//the difference between the actual angle and the desired angle is the error
    integral = integral+error;//the integral is an accumulator that keps the sum of all the errors
    
    derivative=error-previous_error;//the derivative is the change in error since the last recorded error
    
    previous_error=error;//sets the previous error as the one calculated in line 60 
      
    speed=(kp*error)+(kd*derivative);//this is the final product. It uses the sum of the calcukated terms times the coefficents to generate a speed
      
    if(speed>200)//200 is the maximum velocity for the motors being used, so if the value calculated is greater than 200, it will be set to the max
    {
      speed=200;
    }
    if(speed<-200)//-200 is the maximum velocity in the reversed direction, so if it less than -200, it is set to -200
    {
      speed=-200;
    }
    LeftDrive.spin(vex::directionType(vex::rev), speed, vex::rpm);//one motor moves forward and the other moves backwards to make the robot spinv
    RightDrive.spin(vex::directionType(vex::fwd), speed, vex::rpm);
    vex::task::sleep(30);
  }
}

I’m not very familiar with gyros in Vex but it seems that the math may have some errors. When you calculate the speed you’re multiplying kd by the derivative. However, you set kd = 0.0 and there is no function that changes that value. Because of this your speed is being calculated with kd*derivative = 0.
So your speed is Speed = (1.795 * error) + (0). This is causing your motors to take the error, increase it by a factor of 1.795, which only increases the error. This is what is causing the infinite counterclockwise spin. I suggest either reversing the right and left motors so that they spin in the opposite direction, or set speed = (1.795 * error) - derivative. I’m not sure if either of these would work, but it’s my best guess. I hope this helps!

The variable Continue is never set to false, so the robot will never stop moving. Also, as ChanceJewell said, your derivative gain is currently 0, causing the proportional gain to speed up rather than slow down. I would recommend using a PID control rather than PD. In fact, using a PD controller can cause these types of problems to happen quite frequently. All you would need to do is add in your sum of all the errors times your ki constant. I would also recommend tuning your constants using the Ziegler-Nichols method. It is very accurate and I have found success with it. Another problem might be that you are measuring your gyroscope using 1/10 of a degree And setting your result in rpm. Try dividing the variable speed by 10 and setting the motors to dps (degrees per second). Hope this helped.