Go Down

Topic: PID algorithm for line follower (Read 21158 times) previous topic - next topic

YO9ICT

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

Code: [Select]
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  :smiley-yell:

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!

PaulS

Quote
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)
The art of getting good answers lies in asking good questions.

YO9ICT


Quote
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)


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

PaulS

Quote
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.
The art of getting good answers lies in asking good questions.

YO9ICT


Quote
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 ?

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,


YO9ICT


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!

drjiohnsmith

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


silverxxx

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.

samvrit

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/

Gaur

http://crackeconcept.blogspot.com/2014/03/3-sensor-line-follower-ardiuno.html
Awesome code 8)

Go Up