PID Control

Discussion specific to NXT-G, NXC, NBC, RobotC, Lejos, and more.
Post Reply
cyragia
Posts: 8
Joined: 11 Jan 2012, 12:57

PID Control

Post 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;
}
mattallen37
Posts: 1818
Joined: 02 Oct 2010, 02:19
Location: Michigan USA
Contact:

Re: PID Control

Post by mattallen37 »

I personally found that the derivative is far more important than the integral with line following.
Matt
http://mattallen37.wordpress.com/

I'm all for gun control... that's why I use both hands when shooting ;)
cyragia
Posts: 8
Joined: 11 Jan 2012, 12:57

Re: PID Control

Post 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 ?
hassenplug
Posts: 346
Joined: 27 Sep 2010, 03:05
Contact:

Re: PID Control

Post by hassenplug »

How well is the robot following the line? Is it smooth, or does it jerk back & forth?
---> Link to lots of MINDSTORMS stuff under my picture --->
cyragia
Posts: 8
Joined: 11 Jan 2012, 12:57

Re: PID Control

Post 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!)

afanofosc
Site Admin
Posts: 1256
Joined: 26 Sep 2010, 19:36
Location: Nashville, TN
Contact:

Re: PID Control

Post by afanofosc »

Very nice!

John Hansen
Multi-platform LEGO MINDSTORMS programming
http://bricxcc.sourceforge.net/
cyragia
Posts: 8
Joined: 11 Jan 2012, 12:57

Re: PID Control

Post by cyragia »

Thanks, any idea's for improving ?
hassenplug
Posts: 346
Joined: 27 Sep 2010, 03:05
Contact:

Re: PID Control

Post 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
---> Link to lots of MINDSTORMS stuff under my picture --->
Post Reply

Who is online

Users browsing this forum: Semrush [Bot] and 2 guests