Go Down

### Topic: Confusion about PID control (Read 10921 times)previous topic - next topic

##### Jul 22, 2016, 12:24 pm
Hi!

I want to control a motor with PWM, reading a rotary encoder to get speed, and I thought PID would be a good fit. However, I'm confused about a couple things, mostly regarding the proportional term.

First off, when the speed gets higher than the setpoint, the error gets negative, right? When then multiplied by the proportional constant, the output variable also is negative, but the PWM duty cycle is only 0-100% (or 0-255).

Second, if my target speed is, say, 120, and the measured speed is 50, the error is 70 - which, when multiplied by the proportional constant, comes out to (for instance) 40% duty cycle, and the speed never reaches the set point.
Surely it can't be the integral term that helps bring the output variable to appropriate levels, simple proportional control should be more or less functional as well.

What am I missing? Thanks in advance!

#### Southpark

#1
##### Jul 22, 2016, 01:01 pmLast Edit: Jul 23, 2016, 12:12 am by Southpark
Just look at the general block diagram of a closed loop automatic control system. It has a subtraction module..... a differencer.

If the error signal becomes negative.... then the negative value at the output circles around....and comes back around to the negative input terminal. So .... what happens when you subtract a negative number?..... The result is the same as adding the absolute value of that negative number.....aka adding a positive value.

The typical negative feedback system ... when designed properly...works toward equalising the setpoint and the signal on the -ve input terminal.

Integral is for cases where the error (setpoint minus -ve terminal value) is stuck at a small value...such as when friction stops a motor from reaching the desired position. The integral of a constant is a linearly increasing quantity (with respect to time). So the integral helps a system to get a 'constant' error unstuck (so that the control system can keep moving ahead to drive the error toward zero, or get at least some time averaged error of zero).

Sure.... at first glance, the PWM appears to be limited to POSITIVE PWM voltages. But that's where the H-bridge comes in. The digital controller handles the 'error' value. If the error is zero or positive, then the digital controller can make the h-bridge work in 1 state. If the error becomes negative, then the digital controller can make the h-bridge flick over to the other state.

#### MartinL

#2
##### Jul 22, 2016, 01:39 pmLast Edit: Jul 22, 2016, 11:17 pm by MartinL

Your desired speed is the PID controller's "setpoint" and your encoder is the "process variable" that's measuring the actual speed. Both your desired and actual speed should be the same unit of measurement, be it degrees/s, radians/s, RPM or whatever.

The proportional part of the PID controller simply subtracts the actual speed from the desired speed (to give your error) and multiplies it by a gain factor:

PID proportional output = (desired speed - actual speed) * gain

What took me some time to understand was the fact that the PID output is just a value, it doesn't have to be the same units as the inputs. It's just used to drive your "process" or in your case a motor. You just need to map and constrain the PID output to drive whatever it is you're trying to control, again in your case mapping and constraining it to a value between 0 and 255.

The thing to think about here is feedback. The PID system is trying to drive the actual motor speed as measured by your encoder so that it matches your desired speed, in effect it's trying to minimise the error. This error is always about and with respect to your setpoint (desired speed). Naturally the error will always go sightly postive and negative about your setpoint, as the PID system never quite reaches equilibrium. In an extreme case, if you set your gain too high you can get overshoot or even worse oscillations, as your error swings uncontrollably between negative and positive.

#### MarkT

#3
##### Jul 30, 2016, 08:45 pm
Hi!

I want to control a motor with PWM, reading a rotary encoder to get speed, and I thought PID would be a good fit. However, I'm confused about a couple things, mostly regarding the proportional term.

First off, when the speed gets higher than the setpoint, the error gets negative, right? When then multiplied by the proportional constant, the output variable also is negative, but the PWM duty cycle is only 0-100% (or 0-255).
No, often when you control a motor the PID output is a signed value, positive for forward, negative for
backwards - then you can do full control by position or speed, forwards or backwards or stationary.

Even for one-direction speed control you forget the integrating term which "remembers" the normal
steady state output from the loop, so the the proportional error term is merely correcting a difference from
this normal output.  The integrating term slowly adapts to changes in operating point and nulls out the
error to zero in steady state.

However the complexity arises when the system as a whole is not linear (a varying load, or using
slow-decay mode in the motor controller), then you often have to turn down the PID gains for
stability and lose control performance.  Great though the I term is it reduces stability, so is always
rather a compromise.

There's no substitute for actually playing with a PID loop and tuning it and seeing what these things
mean in practice - a lot of the theory assumes linear system with constant load, but real systems are
messier.  More advanced control strategies can help, but they are more complex.

And lastly don't forget that feed-forward is often very useful, not just feed-back - this could be as
simple as adding a fixed offset onto the output of the PID loop to compensate for the normal load.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

#### mamunedt

#4
##### Feb 06, 2017, 11:27 pm
im looking Pid motor control code tutorial ... i already pass many time on youtube & Google ..Im Still Confused ..Thare is No Alternative for pid ...only ball balancing servo is working

#### MartinL

#5
##### Feb 07, 2017, 12:39 pmLast Edit: Feb 07, 2017, 12:39 pm by MartinL
Hi mamunedt,

I found Brett Beauregard's step-by-step PID tutorial for his Arduino PID library, both informative and easy to follow: http://brettbeauregard.com/blog/2011/04/improving-the-beginners-pid-introduction/

#### whitebank

#6
##### Mar 29, 2017, 03:08 pm
You got right thinking, but in good match PID parameter, PWM will change gradually, so actual speed will match with setting value. In bad PID parameters (not match), PWM will change a lot -> actual speed will go up and down quickly, and it will never get match with setting value.
Here is our project for this, from code to video, please visit to see:
http://engineer2you.blogspot.com/2016/12/arduino-motor-pid-speed-control.html

Thanks.

#7
##### Apr 07, 2017, 10:39 pm
Hello,

i'm a Newbie so forgive me for the Q? but i am new to this whole PID thing but i'm trying to get my mind around it.. i'm using a pneumatic system(2 solenoids) so i will need to control 2 outputs with 1 input, is this even possible to do? i mean, i know people are doing it all the time but i cant find any real help.
Don't i need 2 outputs in the "define Variables" here?  i'm so lost. if anyone can bring insight that would be much appreciated thanks..

//Define Variables we'll be connecting to
double Setpoint, Input, Output;

PID myPID(&Input, &Output, &Setpoint,2,5,1, DIRECT);

#### Southpark

#8
##### Apr 08, 2017, 05:48 am
Hello,

i'm a Newbie so forgive me for the Q? but i am new to this whole PID thing but i'm trying to get my mind around it.. i'm using a pneumatic system(2 solenoids) so i will need to control 2 outputs with 1 input, is this even possible to do? i mean, i know people are doing it all the time but i cant find any real help.
Don't i need 2 outputs in the "define Variables" here?  i'm so lost. if anyone can bring insight that would be much appreciated thanks..
PID refers to "proportional plus integral plus differential" control. It is part of automatic control systems theory. The system is typically 1 input and 1 output. And 'PID' theory typically is based on 'second order systems' and 'second order system response'.

The combination of the parameters associated 'PID' (such as proportional gain Kp, integral gain Ki, and derivative gain Kd) sets a particular system behaviour (in response to a particular kind of input - such as an input that might immediately change from 1 value to another value; or an input that might increase gradually from value to another etc. The 3 parameters will determine how response or unresponsive the system is, and how quickly the system will react in order to make the output reach a desired level; and other response features.

To begin to understand PID, it is usually helpful to begin with basic automatic control theory. And like many theories involving movement and activity, some maths knowledge can come in handy - like understanding integrals and integration, derivatives and differentiation.

Go Up