RBE2002FinalProject/PID_RBE.cpp
//Write this function to calculate a control signal from the set velocity 
//and the current velocity 
Write this function to calculate a control signal from the set velocity
and the current velocity
Write this function to calculate a control signal from the set velocity and the current velocity
float PID_RBE::calc(double setVel, double curVel){
  double error = curVel - setVel;
  double dError = error - last_error;
  sum_error = sum_error+error-all_errors[errorCount];   
  all_errors[errorCount] = error;                       
  errorCount = (errorCount+1)%10;
  float pid = (error * kp + dError * kd + sum_error * ki)*-1;
  if(pid > 254){
    pid = 254;
  }
  else if (pid < -254){
    pid = -254;
  }
  last_error = error;
  return pid;
}
float PID_RBE :: calc ( double setVel , double curVel ) { double error = curVel - setVel ; double dError = error - last_error ; sum_error = sum_error + error - all_errors [ errorCount ] ; all_errors [ errorCount ] = error ; errorCount = ( errorCount + 1 ) % 10 ; float pid = ( error * kp + dError * kd + sum_error * ki ) * -1 ; if ( pid > 254 ) { pid = 254 ; } else if ( pid < -254 ) { pid = -254 ; } last_error = error ; return pid ; }
Write this function to calculate a control signal from the set velocity
and the current velocity
Write this function to calculate a control signal from the set velocity and the current velocity
// calculate error // calculate derivative of error // calculate integral error. Running average is best but hard to implement // replace old error with new error // overwrites old errors, only ten of the errors are used // sum up the error value to send to the motor based off gain values.  // limit control value to -254 to 254 //return the control signal