 ### Author Topic: PID problem  (Read 1519 times)

0 Members and 1 Guest are viewing this topic.

#### TheloniusMonk • Beginner
• • Posts: 3 ##### PID problem
« on: October 19, 2012, 11:15:10 AM »
I am trying to make a car that stops a certain distance from a wall.  I have the servo of the car connected to my microcontroller output and a ultrasonic rangefinder connected to the microcontroller input with the microcontroller running a PID algorithm.

I understand the idea of the control feedback loop and I have a general sense of how PID works.  However, from a systems perspective, I am not able to conceptually match up the inputs and outputs.  The set point (a distance from the wall) and the output of the rangefinder are both distances, so that matches up well.  I understand they get subtracted to find a error value.  This error value is fed into a PID algorithm which spits out an output that (to me at least) doesn't have a physical meaning and this value goes into the car servo which takes an input of velocity (i.e. a pulse of a certain width).

The part in bold I am struggling with conceptually.  I would appreciate if someone more experienced could point me in the right direction.  Logically, it would seem that I have to map the range of possible output values of the PID algorithm (is there such a range?) to the range of possible velocities that the car can move in.  Is that right? How can I do this?

#### Billy

• Robot Overlord
•    • Posts: 132 ##### Re: PID problem
« Reply #1 on: October 19, 2012, 02:05:09 PM »
PID loop output can have various meanings depending on the input source and the output mechanism.
In your case, the input is a distance, and the output is a voltage.
Voltage is hard to translate directly into physical quantity in this case as it affects motor torque, but the torque is velocity dependent, and vis-versa.

So you have three errors to deal with:
P - Proportional - the larger the difference between set point and actual point ("error"), the larger the P term is, there is no time impact on this effect
I - Integral -  the larger the difference between set point and actual point, the larger the I term is, but the I term accumulates everytime you run the equation, so a small error will make the I term large over time if the error doesn't go away
D - Derivative - this one varies on implementation. In some cases it is based on the change in error from one loop iteration to the next. In other cases it is based on the change in the actual point (not a function of error).

Purpose:
P - if far away. try very hard to get to the set point. If close, don't worry about it
I - if not at the set point, try harder and harder to get there. The further away, the more quickly you increase the effort
D - this acts as a brake to smooth motion and act to negate excessive signals, or depending on implementation, will actively resist motion all motion

The output from the PID loop = P + I + D, but the D term has a negative value in it so I think of it as PID = P + I - D

So back to me saying that in this case its hard to translate a voltage output into a meaningful quantity like velocity or torque, the beauty of the PID equation is that it doesn't matter what it is, or rather it changes based on the need.

When a motor is stopped, any voltage generates a current and that current defines the torque. Torque turns the motor.
When a motor is spinning, current still defines torque, but PID output voltage no longer defines the current alone.
Current is now a function of motor speed and PID output voltage.
The PID voltage needs to equal the back EMF of the motor, plus add extra voltage to get the current to generate the torque needed.

High performance motion controllers don't like this question of "what does the output mean?", so they break it down into steps that make each output very clear.
They will put a P(I)(D) filter on the position/error to get a velocity command, then run a filter on velocity to get the torque command, and run a filter on that to get the current command that goes to the motor.  In that last loop, the controller monitors the actual current and does what ever it needs to do with the voltage to force the current commanded.

So a high performance PID output = torque command.
What we do at home with PID is not so clear...but at a high level, we can call it torque as that is 70% correct.

Now to your real question:

Logically, it would seem that I have to map the range of possible output values of the PID algorithm (is there such a range?) to the range of possible velocities that the car can move in.  Is that right? How can I do this?

For PID to work well, errors should be kept small. If you need to move 10 meters, instead of giving a single command to move 10 meters (huge error), give it a command it can actually accomplish prior to the next loop through the PID filter.  So if the car is physically limited to 1 meter/second, and you're running your PID loop at 10 Hz, you should give it 100 commands to move 10cm over 10 seconds. That way you never ask it to move faster than it can.

This control of command inputs to PID filters is called motion profiling.  If you want to get fancy with acceleration and deceleration you could issue a command sequence in cm per move like 2, 4, 6, 8, 10, 10 ....... 10, 10, 8, 6, 4, 2, 0.

If you're interested in profiling but can't find the equations/code on google, let me know and I'll post them here.

#### jim5192 ##### Re: PID problem
« Reply #2 on: October 19, 2012, 02:38:26 PM »
On a side note, P-I-D is a standard form of control, but it's usually implemented as Phase-lead-lag (i.e., approximate P-I-D through proportional and derivative bounds)... Because Integrating a constant (car isn't moving) over time can reach infinity if not bounded and derivative and similar for Differentiating a signal changing very quickly (car accelerates quickly) can reach infinity if not bounded. Thus, look up, "phase-lead-lag".

Wikipedia actually gives a pretty good introduction to the topic.