Page 1 of 1

PID Control

Posted: 27 Feb 2012, 19:03
by cyragia
Hello,
I've build an nxt robot which uses pid to follow a line.
It is quite slow due to the motors (nxt motors without external gears), so i didn't use the Derivative.
I did use the Proportional part. (of course !)
My Question is: Do I need the Integral for a line follower ? What are the benefits of (not) using it ?
I tought it would be quite jerky with the integral so i added a "forgetrate", which is: integral = integral*forgetRate + error;

My code: (it's only the important part)

Code: Select all

  
#define Kp 1.5
#define Ki 0.2 
#define Kd 0.0
#define FORGETRATE 0.5
#define SPEED 1.5     

task main(){
    light_l = limit( scale(SENSOR_L/1.0, minl, maxl, 0.0, 100.0) , 0.0, 100.0); // read and scale (calibrate) sensors 
    light_r = limit( scale(SENSOR_R/1.0, minr, maxr, 0.0, 100.0) , 0.0, 100.0);
    
    error = (100-light_l) - (100-light_r);  //difference between left and right sensor is the error
    integral = integral*FORGETRATE + error;
    derivative = error - lastError;
    direction = Kp*error + Ki*integral + Kd*derivative;
    
    OnFwd(OUT_B, limit( (50 - direction)*SPEED , -100, 100));
    OnFwd(OUT_C, limit( (50 +direction)*SPEED , -100, 100));
    
    lastError = error;
}

float scale(float value, float valueMin, float valueMax, float desiredMin, float desiredMax){
  return (desiredMax - desiredMin) * (value - valueMin) / (valueMax - valueMin) + desiredMin;
}

float limit(float value, float valueMin, float valueMax){
  if(value <= valueMin)
    return valueMin;
  else if(value >= valueMax)
    return valueMax;
  else
    return value;
}

Re: PID Control

Posted: 27 Feb 2012, 19:26
by mattallen37
I personally found that the derivative is far more important than the integral with line following.

Re: PID Control

Posted: 27 Feb 2012, 19:33
by cyragia
Well me too, but only if you are going fast.
The derivative will make sure you don't overshoot any corners at high speeds, but as it's goind fairly slow it would'n really matter.
also, what do you think of the code ? any ideas to improve it ?

Re: PID Control

Posted: 28 Feb 2012, 14:05
by hassenplug
How well is the robot following the line? Is it smooth, or does it jerk back & forth?

Re: PID Control

Posted: 28 Feb 2012, 15:09
by cyragia
have a look at this,
It's supposed to follow the line, go around the obstacle, when it sees a green square it should turn that way, when it's in the big green "field" it should find the can an push it out. (It's for robocup junior belgium, we won last time!)


Re: PID Control

Posted: 28 Feb 2012, 16:57
by afanofosc
Very nice!

John Hansen

Re: PID Control

Posted: 28 Feb 2012, 17:01
by cyragia
Thanks, any idea's for improving ?

Re: PID Control

Posted: 28 Feb 2012, 20:22
by hassenplug
I would repeat what I said in this thread:

https://sourceforge.net/apps/phpbb/mind ... 691#p12691

Consider changing the robot design.

It looks like you have a good design, but it doesn't hurt to try some other options.

Steve