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
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!
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.
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 ?
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.