### Author Topic: PID algorithm  (Read 3143 times)

0 Members and 1 Guest are viewing this topic.

#### [email protected]

• Jr. Member
• Posts: 40
• Helpful? 0
##### PID algorithm
« 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.....

#### newInRobotics

• Supreme Robot
• Posts: 1,015
• Helpful? 48
• N.I.R.
##### Re: PID algorithm
« Reply #1 on: January 23, 2013, 01:02:15 AM »
What do You expect to get and what do You actually get?
"Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian W

#### [email protected]

• Jr. Member
• Posts: 40
• Helpful? 0
##### Re: PID algorithm
« Reply #2 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...

#### jkerns

• Robot Overlord
• Posts: 270
• Helpful? 12
##### Re: PID algorithm
« Reply #3 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?
I get paid to play with robots - can't beat that with a stick.

http://www.ltu.edu/engineering/mechanical/bachelor-science-robotics-engineering.asp

#### briselec

• Jr. Member
• Posts: 10
• Helpful? 1
##### Re: PID algorithm
« Reply #4 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.
In assembly language no one can hear you scream

#### [email protected]

• Jr. Member
• Posts: 40
• Helpful? 0
##### Re: PID algorithm
« Reply #5 on: January 24, 2013, 09:33:56 AM »
@ briselec so sud i put that function directly in d loop

#### [email protected]

• Jr. Member
• Posts: 40
• Helpful? 0
##### Re: PID algorithm
« Reply #6 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 ...?

#### jkerns

• Robot Overlord
• Posts: 270
• Helpful? 12
##### Re: PID algorithm
« Reply #7 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.)
I get paid to play with robots - can't beat that with a stick.

http://www.ltu.edu/engineering/mechanical/bachelor-science-robotics-engineering.asp

#### [email protected]

• Jr. Member
• Posts: 40
• Helpful? 0
##### Re: PID algorithm
« Reply #8 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.....

#### [email protected]

• Jr. Member
• Posts: 40
• Helpful? 0
##### Re: PID algorithm
« Reply #9 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.....

#### [email protected]

• Jr. Member
• Posts: 40
• Helpful? 0
##### Re: PID algorithm
« Reply #10 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?
« Last Edit: January 26, 2013, 03:43:39 AM by [email protected] »

#### [email protected]

• Jr. Member
• Posts: 40
• Helpful? 0
##### Re: PID algorithm
« Reply #11 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....

#### jkerns

• Robot Overlord
• Posts: 270
• Helpful? 12
##### Re: PID algorithm
« Reply #12 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.
I get paid to play with robots - can't beat that with a stick.

http://www.ltu.edu/engineering/mechanical/bachelor-science-robotics-engineering.asp

#### jkerns

• Robot Overlord
• Posts: 270
• Helpful? 12
##### Re: PID algorithm
« Reply #13 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.
« Last Edit: January 28, 2013, 01:38:05 PM by jkerns »
I get paid to play with robots - can't beat that with a stick.

http://www.ltu.edu/engineering/mechanical/bachelor-science-robotics-engineering.asp

#### [email protected]

• Jr. Member
• Posts: 40
• Helpful? 0
##### Re: PID algorithm
« Reply #14 on: January 29, 2013, 10:32:29 AM »
thanks! for your reply...!!!
i have passed the current position term through the main loop....

#### [email protected]

• Jr. Member
• Posts: 40
• Helpful? 0
##### Re: PID algorithm
« Reply #15 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???

#### jkerns

• Robot Overlord
• Posts: 270
• Helpful? 12
##### Re: PID algorithm
« Reply #16 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.
I get paid to play with robots - can't beat that with a stick.

http://www.ltu.edu/engineering/mechanical/bachelor-science-robotics-engineering.asp

#### [email protected]

• Jr. Member
• Posts: 40
• Helpful? 0
##### Re: PID algorithm
« Reply #17 on: January 30, 2013, 07:02:23 AM »
is the term correction  right?
will it work properly...?

« Last Edit: January 30, 2013, 07:04:01 AM by [email protected] »

#### jkerns

• Robot Overlord
• Posts: 270
• Helpful? 12
##### Re: PID algorithm
« Reply #18 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)
I get paid to play with robots - can't beat that with a stick.

http://www.ltu.edu/engineering/mechanical/bachelor-science-robotics-engineering.asp

#### [email protected]

• Jr. Member
• Posts: 40
• Helpful? 0
##### Re: PID algorithm
« Reply #19 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.....

Get Your Ad Here