Shop OBEX P1 Docs P2 Docs Learn Events
PID control — Parallax Forums

PID control

SiriSiri Posts: 220
edited 2008-12-15 19:49 in Propeller 1
I am working on a project that require good control of two proportional valves.The
Valves are controlled via PWM using a prop.
I am not a good programmer just self taught one.

I would like if some one could help/direct me in the right direction to attain better
Control of the proportional using PID (Proportional-Intergral-Derivertive) control.

I read a lot about PID control – looks very complicated and most of the literature
Is geared towards programming in C etc. – which I have no knowledge.

If someone can point me in the right direction – PID control using the Prop.

Regards,

Siri

Comments

  • T ChapT Chap Posts: 4,223
    edited 2008-12-12 22:35
    Here is a PID loop I am using that works great for my application, you would need to adapt it to your own needs. It requires the pwmasm.spin object I have uploaded, as this one uses a duty range of 0 - 1000, not 0-100.

    You can set the parameters for PID, or turn them off as needed.

    The way it works is, some main method sends out a position that is constantly updating a range of numbers from 0 - x. The PID loop chases the position wherever it goes, the values of the parameters determine how the PId loop reacts. In my case, the range increments one number at a time from 0 - example 10000, the PID loop attempts to correct the error by moving the motor at a speed via PWM 0 - 1000.

    There is a dead band section that you can adjust if the error is within a certain range to help eliminate overshoot and oscillation.

    Post Edited (TChapman) : 12/13/2008 6:13:57 AM GMT
  • HarpritHarprit Posts: 539
    edited 2008-12-12 22:56
    Siri:

    Here is an explanationof the PID loop in plain Englis from my book on how to run motors with micros


    The PID loop explained in simple English
    The PID control equation and its components.

    The usual scheme used to control an encoded DC motor is called a PID loop. In the equation that represents the gain/motion of the motor the P, I and D represent the three basic components of the feedback loop. A constant K is needed to take care of friction components. In layman’s terms these variables are defined as follows:

    K (when used) represents a constant needed to represent the overall system friction.
    P represents the proportional part of the loop.
    I represents the integrating function in the loop
    D represents the derivative part of the feedback equation.

    Before we go any further let us get an understanding of what we are talking about when we say that the motor is controlled by a “PID” loop or equation. The PID loop defines how much energy is to be fed to the motor at any one time during a move based on where the motor is and where it is expected to be. As stated above there are four parts to the equation that determines this load. The three main components are referred to as the P, I and D and the minor friction component is referred to as K.

    If these four components are described properly, within the control algorithm, and if a proper encoder has been selected, much improved control of the motor will be achieved. Let us look at the components, one at a time, to see what their functions are and what they accomplish. The control scheme that we develop does not have to the mathematically perfect to give us good performance. In fact with PBP and its limited 8/16 bit math, a mathematically perfect system cannot be achieved but we can get close enough to have acceptable operation.

    Because the motor does not start moving until it has overcome the friction in the system a certain amount of power has to be added to the system before the motor will start to move. This is the constant K. Keep in mind that if we do not use this constant, the integrating part of the equation will take care of it but it will take time.

    The friction component “K” is often ignored because it is a minor component and the integrating function will take care of it the first few times through the control loop. In any system with moving parts there will be some friction. In the case of a motor, even one with nothing attached and no load, there will be friction at the two shaft bearings and at the commutators brushes and a small voltage applied to the motor will not move it. As the voltage is increased, the motor will start to move. The voltage at which the motor starts to move is the voltage needed to overcome the friction and for our purposes it can be considered constant although it increases as the motor speed increases. In most cases we can ignore this increase and use a constant to represent the frictional load. Mathematically this is:

    K = small, fixed value


    The proportion component “P” assumes that the power we supply to the motor will be proportional to the load that the motor is under. This too is not exactly accurate but it can be defined in that way for most practical purposes. This is the largest part of the equation and so has to be picked with some care to prevent over control. The faster we want the motor to run the larger the load and the larger the P component. In mathematical terms the energy provided can be expressed as

    P = Load * factor

    If you are running a motor under a variable load the speed that the motor attains will be approximately proportional to the load that is on the motor. Keep in mind that the gain can vary from 0 to 255 in our system, we have to select a gain that will stay well within these limits under all conditions. The multiplier selected and a conditional test ensures this.

    If there are no load changes and the system response is linear (meaning that twice the speed requires twice the power), the proportional component is all we need to run the motor. If however if the system in not linear or if the load is changing, we have to add to or subtract from the gain to keep the motor at a constant speed. We have to do this a little bit at each time through the control loop till the motor gets to the desired speed. This is the “I” or integrating component in the equation. Because it is needed only when there is an error in the motor position, it has to be based on this error. The higher, the positional error, the more we have to add to or subtract from the power setting to make the motor speed up or slow down to where we need it to be. As mentioned above, this is done every time we go through the control loop. In mathematical terms the energy provided can be expressed as

    I = (Commanded position - actual position ) * (constant or a variable for some kind)


    The derivative component “D” is a measure of the difference between where the motor is and where we had expected the motor to be at any one time. This value is calculated each time through the control loop. If there is a large difference between the two numbers, we cannot wait to integrate the power in in little increments but need to make a larger adjustment right away to get the motor within acceptable parameters immediately. Exactly how much power has to be added is a function of the system inertia, the load and how tightly the trajectory specified for the motor has to be followed. In a metal cutting CNC machine the tool has to follow the specified path very closely so these corrections are made very frequently. The equation is best designed as a time based function where we have an equation which tells us where the motor is supposed to be at any one time in each move. We can then read the actual position, compare it to what the equations tells us and make the correction. In our case the 8/16 bit math and 20 MHz processor do not lend themselves to the task at hand with ease. Even so adequate approximations can be implemented and a working system achieved.

    Summary: To determine “D” we need to know where the motor should be and were it actually is. The difference is the error. We want this error to be as small as possible and our response is based on how small this error should be. If we are running a very accurate positional system we may need to look at this many hundred times a second and respond immediately. A high count encoder would be highly desirable.

    D = (Expected position - real position) * (constant or variable for some kind)

    One thing this means in simple terms is that there is no need for a change in the power input if the motor needs to be where it is and is moving at the desired speed.

    Good Luck

    Harprit
  • SiriSiri Posts: 220
    edited 2008-12-13 00:53
    Chapman/Harprit

    Thank you very much for the quick replies.

    As you might have expected i will give Chapman,s PID engine a try as this may be the easier solution.

    Thanks again,

    Siri
  • evanhevanh Posts: 15,872
    edited 2008-12-13 13:17
    Harprit, I'm afraid your write up is nonsense.

    TChapman's code is sound. The method of generating the three components of error, ierror and Daverage is the good stuff. Except ((encodercount * 10) / 100) is a bit buggy, but is fine for short range motion.


    Evan
  • HarpritHarprit Posts: 539
    edited 2008-12-13 18:26
    evanh

    Sorry you think that explaining things in English is nonsense. Maybe so.
    I think un-commented code is pretty hard for beginners to understand.
    If you understand the problem there is a chance you might find a solution.
    My explanation is in that vein
    Thanks for posting.
    Harprit
  • T ChapT Chap Posts: 4,223
    edited 2008-12-13 21:51
    evanh said...
    Harprit, I'm afraid your write up is nonsense.

    TChapman's code is sound. The method of generating the three components of error, ierror and Daverage is the good stuff. Except ((encodercount * 10) / 100) is a bit buggy, but is fine for short range motion.


    Evan


    Dividing the encoder is just a workaround for my application as I am using a 1:10 gear box, the encoder generates 1600 counts per revolution without the gear box, so with the gearbox it is 16000 counts to move the same distance, and using spin, the pulse/profile generation method can't run fast enough to run the motor at speeds I want.

    BTW, my experience in learning PID completely from scratch is that all the reading I did still did not benefit until I had some working code so that the logic behind each parameter could make sense. I found not one single code example on the web that would really work in the real world. I suggest that someone with no experience start out by changing the values dramatically, even setting some values to zero to get an awareness of what each parameter does. An LCD connected to the pwm output, plus looking at the error, and even looking at each value separately is an invaluable practice starting out.

    Post Edited (TChapman) : 12/13/2008 10:03:05 PM GMT
  • SiriSiri Posts: 220
    edited 2008-12-14 14:55
    TChapman et al

    Thank you.I reallly appriciate your comments - I too learn fast by tinkering with examples to figure out the logic behind the code and try them out to suit my needs.
    I will get back after I do some tinkering.

    Regards,

    Siri
  • Paul RowntreePaul Rowntree Posts: 49
    edited 2008-12-14 22:59
    I am afraid that the detailed explanations of PID operation that have been given by Harprit do not conform to any workable PID model. Each of the three terms (P,I,D) should be functions of the error between the current set-point and the actual system state. In the Harprit model the error is only used in the (I,D) terms. Each term should have a gain to identify the manner in which the error drives its component of the feedback used to change the current system state. For example,

    P = PGain*( Set-point-Current State)

    The Integral term is not a proportional to the error, as previously posted, but is an integral of this error over time. The Derivative term is related to the rate of change of the error; because it tends to amplify noise in the system, it is often ignored in working systems unless extremely rapid corrections are required.

    There are excellent descriptions available, for example on wikipedia (http://en.wikipedia.org/wiki/PID_controller) and the National Instruments (http://zone.ni.com/devzone/cda/tut/p/id/3782) websites. I strongly recommend that this material be read and understood prior to attempting to implement the approach outlined above.

    Cheers!
  • Graham StablerGraham Stabler Posts: 2,510
    edited 2008-12-14 23:43
    I'm afraid I have to agree with both Evan and Paul. Harprit, your explanation was easy enough to read but incorrect, either that or it does not say what you intended.

    Graham
  • Steph LindsaySteph Lindsay Posts: 767
    edited 2008-12-15 17:57
    Hi All,

    In the Propeller Education Kit Labs sticky-thread there is an application called "PEKbot" that uses PID control·for distance detection.· There are also sample code and and a video clip to download for it·that may be of use to anyone needing·some additional·practical examples in an application.

    Andy also has an article in the Stamps in Class Mini Projects sticky-thread called "PID Control Intro with the BASIC Stamp" that may be useful to read, even though it is for the BS2.

    -Stephanie Lindsay
    Editor, Parallax Inc.
    ·
  • edited 2008-12-15 19:49
    Hi folks,

    I haven't chimed in on this subject because the objects used by the PEKbot need some maintenance. The PID object the PEKbot uses has a bug that makes it just a PI controlled bot.

    Here is a link to a corrected PID Object along with a simple test program.

    More updates to come.

    Andy

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Andy Lindsay

    Education Department
    Parallax, Inc.
Sign In or Register to comment.