Go Down

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

#### YO9ICT

##### Sep 22, 2013, 07:07 pm
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

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

#1
##### Sep 22, 2013, 07:13 pm
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?

#### YO9ICT

#2
##### Sep 22, 2013, 07:16 pm

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?

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

#### PaulS

#3
##### Sep 22, 2013, 07:56 pm
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.

#### YO9ICT

#4
##### Sep 22, 2013, 08:18 pm

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

#5
##### Sep 22, 2013, 08:25 pm
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

#6
##### Sep 22, 2013, 08:33 pm

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

#7
##### Sep 22, 2013, 08:38 pm
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

#8
##### Sep 23, 2013, 09:56 am
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

#9
##### Dec 06, 2013, 08:41 pm
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

#10
##### Mar 11, 2014, 07:56 pm
http://crackeconcept.blogspot.com/2014/03/3-sensor-line-follower-ardiuno.html
Awesome code

Go Up