Society of Robots - Robot Forum

Software => Software => Topic started by: [email protected] on January 23, 2013, 12:27:48 AM

Title: PID algorithm
Post by: [email protected] on January 23, 2013, 12:27:48 AM
hey guys m tryin to build a linefollower based on d PID algorithm...
bt for some reason m nt gettin the expected result.....
plzz tell me if d algorithm ive made is wrng or thers some error.....
ive attached d code.....
Title: Re: PID algorithm
Post by: newInRobotics on January 23, 2013, 01:02:15 AM
What do You expect to get and what do You actually get?
Title: Re: PID algorithm
Post by: [email protected] on January 23, 2013, 09:20:05 AM
i expect it to follow d line ... but it jus goes straight...
n m using a 5 IR sensor array...
Title: Re: PID algorithm
Post by: jkerns on January 23, 2013, 09:47:01 AM
Your integrator doesn't appear to integrate, you don't have a gain for the derivitive. But, to start with I would turn I and D terms off anyhow - they will just make it hard to tune the controller.

With a gain of 1, your correction is going to change the pwm by just a few counts. How much does this actually change the speed of your motors?

For a starting point make the proportional gain big enough that when you sense the line at one extreme of your array the two motor speeds are different by a factor of two at least.

Are you also sure that high/low really corresponds to over the line / off the line?
Title: Re: PID algorithm
Post by: briselec on January 23, 2013, 12:36:43 PM
Your error calculation in your loop function won't work correctly. You calculate the current position in the other function but then use a different uninitialized variable with the same name for working out the error.
Title: Re: PID algorithm
Post by: [email protected] on January 24, 2013, 09:33:56 AM
@ briselec so sud i put that function directly in d loop
Title: Re: PID algorithm
Post by: [email protected] on January 24, 2013, 09:39:10 AM
Quote
[Are you also sure that high/low really corresponds to over the line / off the line?]]


what do u mean ???
i dint get it ...?
Title: Re: PID algorithm
Post by: jkerns on January 25, 2013, 09:19:40 AM
Quote
Are you also sure that high/low really corresponds to over the line / off the line?]]


what do u mean
i dint get it ...?

Does the sensor read high when it is over the line and low when it is not? Or does it read low (or high) all the time? Does it really detect the line?

Connect an LED and write the code to turn it on when a sensor is in the high state and off when it is low. Move your robot across the line. Does the LED go on and off when it should?

(I missed the problem with the position variable not being passed to the main when I looked at your code.)
Title: Re: PID algorithm
Post by: [email protected] on January 25, 2013, 09:38:22 AM
Quote
[/Does the sensor read high when it is over the line and low when it is not? Or does it read low (or high) all the time? Does it really detect the line?]
yes i m sure it detects the line when its over d line.....
Title: Re: PID algorithm
Post by: [email protected] on January 25, 2013, 09:40:29 AM

Quote
[Connect an LED and write the code to turn it on when a sensor is in the high state and off when it is low. Move your robot across the line. Does the LED go on and off when it should?]

there is an led already.....
Title: Re: PID algorithm
Post by: [email protected] on January 26, 2013, 12:53:50 AM
and how do i hook up the integral/derivative term with the motors???

EDIT -
Your integrator doesn't appear to integrate, you don't have a gain for the derivitive.


does that mean that my algorithm is wrng????
Title: Re: PID algorithm
Post by: [email protected] on January 28, 2013, 08:37:28 AM
hey guys please help me i have to finish this project as soon as possible...
jus tell me if something's worng with my algorithm....
Title: Re: PID algorithm
Post by: jkerns on January 28, 2013, 09:07:23 AM
So far, we have determined that you did not scope the variables with the current position properly.

So far, we have determined that your PID doesn't really work as a PID because of errors in your software.

Have you fixed those problems yet?


First you need to make sure the current position term is passed to the main program.

Then you need to calculate the P I and D terms.

P_correction = KP * error

I_correction = I_correction + ( KI * error)

D_correction = KD * (last_error - error) Note: This is a simple implementation, but often has problems due to noise in your sensor input. But in your case, since the error will change only in integer steps and the change will persist for only one program loop, this term won't do much of anything.

Then you can add them up to get the PID output.

Then you need to actually tune your controller to work.
Title: Re: PID algorithm
Post by: jkerns on January 28, 2013, 11:03:23 AM
In theory, the I and D terms should have time associated with them as well, but if your software loop runs at a nearly constant rate, you can fake it with the KI and KD terms.
Title: Re: PID algorithm
Post by: [email protected] on January 29, 2013, 10:32:29 AM
thanks! for your reply...!!!
i have passed the current position term through the main loop....
Title: Re: PID algorithm
Post by: [email protected] on January 29, 2013, 10:44:51 AM
Error = target_pos - current_pos;             //calculate error
 previos_error=error;
 P = Error * Kp;                       //error times proportional constant gives P
 I = I + (Ki*Error);                             //calculates the integral value
 D = kd * (Error - previos_error);       //stores change in error to derivate
 Correction = P + I + D;
 pwm = 175;
  RPM_of_Lm = pwm - (Correction);        // calculate pwm for motors
  RPM_of_Rm = pwm + (Correction);

now is this controller right???
Title: Re: PID algorithm
Post by: jkerns on January 29, 2013, 11:18:47 AM
You have two error terms one Error and one error. That is a problem.

You set the value for the previous error right after you calculate the Error - too late  - you need to do that BEFORE you change the value of Error.

But you are getting closer.
Title: Re: PID algorithm
Post by: [email protected] on January 30, 2013, 07:02:23 AM
is the term correction  right????
will it work properly...?

Title: Re: PID algorithm
Post by: jkerns on January 30, 2013, 09:27:14 AM
If you fix the errors noted above it should work (you will have to tune the gains of course - I would set Ki and kd = 0 and just work with Kp to start. Set the intital value to 10 - if that doesn't work try 1 (if it's turning too much) or 100 (not turning enough) once you get close you can change it by double or half instead of by 10 or 1/10)
Title: Re: PID algorithm
Post by: [email protected] on January 31, 2013, 11:48:48 AM
thanxx .. alot for your help....!
i am fixing the issues with the driver circuit ....
i would probably finish the driver problem in some days and than start tuning part.....