PID algorithm for line follower

Hello guys!
I have some problems with tuning the PID algoritm for my line follower.
This is my PID implementation:

void loop() {

  int proportional = ((int)position) - 3500;   //setpoint=3500,position between 0 and 7000
  int derivative = proportional - last_proportional;
  integral += proportional;
  last_proportional = proportional;
  int power_difference = proportional*Kp + integral*Ki + derivative*Kd; 
  const int max = 255;
  if(power_difference > max)
    power_difference = max;
  if(power_difference < -max)
    power_difference = -max; 
}

Right now, Kp is 1, Ki is 0.011 and Kd is 5.3 and the robot is a little bit shaky on straights. Lowering Kp below 1 results in a line non-following robot :stuck_out_tongue_closed_eyes:

I mention that I use QTR-8A sensors, all eight wired up to an Atmega328P in TQFP package and Pololu 30:1 motors driven by a couple of TLE5206 from Infineon.

Hope someone can share some light if my implementation is correct and what to change. Thanks!

the robot is a little bit shaky on straights.

I'm having a little trouble mapping this technical terminology to the parts of the code you posted. Can you explain this in layman's terms? 8)

PaulS:

the robot is a little bit shaky on straights.

I'm having a little trouble mapping this technical terminology to the parts of the code you posted. Can you explain this in layman's terms? 8)

:slight_smile: I mean that the robot is following the line very accurate in curves, but it is oscillating on straight lines.

I mean that the robot is following the line very accurate in curves, but it is oscillating on straight lines.

I can't see that loop() actually accomplishes anything. There must be more to your code than that. For instance, position is not assigned a value prior to being used. power_difference is computed but never used.

PaulS:

I mean that the robot is following the line very accurate in curves, but it is oscillating on straight lines.

I can't see that loop() actually accomplishes anything. There must be more to your code than that. For instance, position is not assigned a value prior to being used. power_difference is computed but never used.

Sorry, now I understand. The code posted above is ONLY the PID implementation, not the whole loop. I have attached the sketch. Maybe I need to remap that 0-7000 interval that I have for "position" to something more close to 0-255 PWM range ?

sketch_sep15a.ino (4.88 KB)

have you looked at how the values of integral, proportional and derivative change over time

contrast and compare when its following a streight line, against when it on curve,

drjiohnsmith:
have you looked at how the values of integral, proportional and derivative change over time

contrast and compare when its following a streight line, against when it on curve,

How can I do that? Thanks for reply!

u need to put print statments in the code,
and print out to a terminal,

u end up following the robot with the laptop,
and grabbing a lump of data , but you should see whats happening

You are using int for ki , kp , etc , 0.003 = 0 in int. try making them float. (ints do not store decimals)
I am not sure why unsigned proportional is not , say long , does it ever go negative? (i only looked quickly through the code).
If it oscillates it usually means kp is too high. Hope this helps.

I have made a tutorial on Arduino PID Line Follower with complete code and have also explained how PID works in a simple manner. Followe this link:
http://samvrit.tk/tutorials/pid-control-arduino-line-follower-robot/

Awesome code 8)