### Author Topic: PID  (Read 929 times)

0 Members and 1 Guest are viewing this topic.

#### robojoker

• Beginner
• Posts: 1
##### PID
« on: July 29, 2012, 01:10:18 PM »
Hola.

So I am programming a robotic arm to draw a line, but having some issues.

So this robot is run off of dc motors with encoders. I used these encoders along with some switches that are at specific places relative to what I believe to be the 0 degree position of each motor.

But now I would like it to go to that 0 position from the location of the mechanical switch.

1. I know at how many counts the encoder tells me it is at 0.
2. So, I just have my input to my PID as the current position and output the compensation required to achieve the position (?)
3. And just run a constant velocity constantly in a certain direction depending on if overshoot occurred or it hadn't reached that point yet

But then I read online that you could have a position input to your control system that goes into the PID controller and somehow output a prescribed velocity (duty cycle) to attain that. And as the point got closer and closer the duty cycle would decrease hence the velocity decrease and eventually get to that point.

My problem is... if the input of a PID is a position then the output must be the compensation for that position correct or not ( a bit hazy on the subject) ?
And if so, then how do I use that information to alter the PWM or velocity of the motor?

#### waltr

• Supreme Robot
• Posts: 1,944
##### Re: PID
« Reply #1 on: July 29, 2012, 07:05:15 PM »
Quote
if the input of a PID is a position then the output must be the compensation for that position correct or not ( a bit hazy on the subject) ?

The position 'error' could be translated into 'velocity. So with a 'position error' greater than some preset constant the 'velocity' is at maximum. Once the 'position error' is below the preset then the 'position error' could be applied the reduce the 'velocity'.

This can become some what complicated to keep track of. Microchip has an App Note for a Servo Motor (command to a desired position) that could help explain the details of implementing a PID. It is AN696 and available on their web site. Very little of the App Note is specific to PICs but covers the theory and practice on implementing a PID position control loop.
http://www.microchip.com/search/searchapp/searchhome.aspx?id=2&q=AN696

#### jkerns

• Robot Overlord
• Posts: 270
##### Re: PID
« Reply #2 on: July 30, 2012, 09:32:15 AM »
PID stands for "Proportional" "Integral" "Derivative"

The input to the algorithm is the error - the difference between the current measured value from your physical plant (say, position) and the desired value for that output.

The output of the control algorithm is the sum of three values - a value proportional to the magnitude of the error, a value proportional to the error integrated over time, and a value proportional to the rate of change (derivative) of the error.

Output = Kp* error + Ki * Integral(error) + Kd*derror/dt

where Kp, Ki and Kd are the "gains" for each of the three terms, Integral(error) is essentially the running sum of the error, and derror/dt is the rate of change of the error

When your error is large, Kp * error will be large as the error gets smaller, that part of the control output will get smaller. Proportional control tends to be quick, but does not give zero error.

As long as the error is non-zero, the Ki * Integral(error) will continue to update. The correction from this term will be a result of both the magnitude of the error and how long it has been present.  This term will drive the steady state error to zero.

Any change in the error results in a output of Kd*derror/dt. For a practical use of this, the signal has to be properly filtered so it doesn't just react to signal noise. Personally, I would usually avoid implementing this part of the algorithm - but in this case I would make an exception.

Obviously, the output from a PI is not going to give you a constant velocity. The output from the controller will vary with the magnitude and time based behavior of the error.

Now, you have another problem. You want to measure position, but control via power /velocity. This means that the output of your motor (velocity) is integrated to get to the position that you are measuring. This extra integration in the system will tend to result in stability issues if you just blindly apply a P-I controller. This extra integration in the plant effectively makes the P term in your controller work like an integral term. The D term works more like P and the Integral term becomes essentially a double integral which is a problem for stability.

I would recommend implementing a simple P-D controller.

For tuning, I would use rules for a P-I controller applied to the D - P controller.  Set the P gain = 0, and play with the D gain until it is on the edge of oscillating  - then cut the D gain in half. Then play with the P gain until it is again just oscillating - then cut it in half. Done.
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