PID-filter routine (2nd try).


Closed Thread
Results 1 to 40 of 132

Hybrid View

  1. #1
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,146


    Did you find this post helpful? Yes | No

    Default

    Hi Darrel.
    Yes I did clamp the output to 255 although it was not mentioned in my previous post.

    Quote Originally Posted by Darrel Taylor View Post
    You may want to use 10-bit PWM for finer control.
    Is this possible with PBP or do I have to mess directly with the PIC registers?

    My next step will be to use MiBAM, as I do not have hardware PWM on the final PIC.

    Ioannis

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Default

    Hi,
    Start with pid_Ki set to 0. Increase pid_Kp and "bump" the regulator (change the setpoint) do this a couple of times until you see it begin to overshoot or a tendency to oscillate.

    Increase pid_Kd in order to damp out the overshoot.

    Now you can try to increase pid_Kp even further, once again it will overshoot or start to osciallate, follow with pid_Kd.

    This should give you a stable loop but it will most likely never hit the actual setpoint. Slowly increase i pid_Ki to bring the value right up to the setpoint. Increasing pid_Ki will introduce an overshoot which you might should be able to reduce by further increasing pid_Kd.

    With that said it's crucial that you run the PID (read input, run PID, update output) at a constant interval. If the time between updates varies you won't get a stable output.

    /Henrik.

  3. #3
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,146


    Did you find this post helpful? Yes | No

    Default

    Thanks Henrik.

    What is the range for the p,i,d variables?

    Or is it absolutely project dependant?

    Ioannis
    Last edited by Ioannis; - 29th December 2010 at 11:52.

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Default

    Hi,
    They are all WORD variables so 0-65535 in theory (each representing a gain of 0-255). However, there is no protection from, or detection of, overflow in the calculations that may be the result of very high gains and very large errors.

    /Henrik.

  5. #5
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Ioannis View Post
    (10-bit PWM)
    Is this possible with PBP or do I have to mess directly with the PIC registers?

    My next step will be to use MiBAM, as I do not have hardware PWM on the final PIC.
    You might try HPWM10 for now. It's just like HPWM ... almost.
    http://www.picbasic.co.uk/forum/show...7805#post37805

    MIBAM is only 8-bit, but if you only have one output it'll be better to create a higher resolution interrupt driven PWM that could also be a time base for the PID loop.
    DT

  6. #6
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,146


    Did you find this post helpful? Yes | No

    Default

    Thanks for the tips.

    By he way, comapring the MiBAM and Multi_SPWM which is lighter? I suppose Multi_SPWM.

    On my final board, PIC will be probably the old F819, so I do not have the luxary of HPWM.

    Maybe the Interrupt driven software and PID is the ultimte solution.

    Currently I am dancing, err, oscillating...

    Ioannis

  7. #7
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Default

    Hi Ioannis,
    At what frequency are you currently running the PID-loop?

    You have a fairly large timeconstant, 0.22s if I'm not entirely mistaken. Depending on the frequency you're running the loop at this might be a source for your instability. I'd try setting up a timer interrupt to get constant and predicatble timing and try running the PID-loop at something like 5Hz.

    The variables pid_P, pid_I and pid_D contains the "effort" each term contributes to the total output with. Those might be interesting to "watch" as you tune the loop.

    Also, you might gain quite a lot by using the velocity feed forward. For example, you can't possible get 12V output without feeding the circuit atleast a 50% dutycycle (provided the load doesn't regenerate etc). The velocity feedforward can help you provide this "baseline" output.

    If I'm not mistaken you would, for an output of 12V, get a return value from the ADC of ~430 and therefor the setpoint value for a 12V output is 430. To set the velocity feedforward so that it outputs a 50% dutycycle for a setpoint of 430 you set pid_Vel_kFF to 74(dec) because 128/430=0.29 (128 being the value for 50% dutycycle) and 74/256 = 0.29 (because the gain is expressed in 1/256 units).

    Finally you must also set the pid_Velocity_cmd variable to equal your setpoint value whenever you change it.

    Now the PID regulator only has to worry about "external disturbances" such as power supply voltage variation, load variation and so on.

    /Henrik.

  8. #8
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,146


    Did you find this post helpful? Yes | No

    Default

    Hi Henrik.

    Hmm, I do not have a time base for the PID. It is in the close loop of the main. Sure is not predictable rate.

    Also I noticed that using a 470uF capacitor in the place of the 10uF help a lot, so your point of using a slow like 5Hz rate is a good one.

    I did not used the Velocity so far, it is a good poin also. Will try it an report.

    Thanks both for the help.

    Ioannis

  9. #9
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Default

    Hi,
    The very first thing to tend to, IMHO, is to make the update rate as stable as possible - it is very important for stability. If you haven't already seen it, here's a pretty good article on digital PID filter implementation. It's a good read even if you're not actually writing the PID filter code - just using it.

    Keep at it and keep us posted!

  10. #10
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,146


    Did you find this post helpful? Yes | No

    Default

    I recall downloading that article, but did not read it. Will do.

    My pre-PID implementation was based on a loose control loop like this one:

    Code:
    if out_value < (setpoint - 1) then
        pwm_signal=pwm_signal+1
    endif
    
    if out_value > (setpoint + 1) then
        pwm_signal=pwm_signal-1
    endif
    It is a very simple idea that gives also a hysteresis of +/-2 points. So if the out_value is within the "dead band" nothing happens.

    The down side is that, depending on the overall loop it may take too long to reach the setpoint and also may do some oscillations also if there is delay getting the correct analog feedback.

    My first impressions using the PID is that has the potential to be very fast and accurate, despite the fact I did not managed to make it work reliable yet.

    Will come back after correcting the setup.

    Ioannis
    Last edited by Ioannis; - 30th December 2010 at 20:48.

  11. #11
    Join Date
    Jun 2013
    Posts
    18


    Did you find this post helpful? Yes | No

    Default Re: PID-filter routine (2nd try).

    Dear Mr. Henrik,

    Would you please modify your PID routine for using on the other kinds of closed loop control, such as pressure control?

    I want to implement a pressure transducer as feedback sensor and drive a water-pump by using of a frequency inverter to achieve a desired pressure in pipes. And of course, to make a stable level of pressure in pipes when people use water.

    Deeply appreciating in advance.

    Would you please help?

    Regards,

    P.S. I am beginner.

  12. #12
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Default Re: PID-filter routine (2nd try).

    Hi,
    I don't know how many times I've written this but here it goes again:
    The PID filter doesn't know what you are trying to control/regulate and it doesn't matter, all it does is crunch numbers. These numbers can represent pressure, lightlevel, voltage, current, position, velocity, torque, temperature, humidity, pH, whatever. It's just numbers and math as far as the PID filter is concerned and it'll close a loop around "anything" you want.

    With that said, PID isn't the only type of regulator and may not always be the best method but that's not the point.

    /Henrik.

  13. #13
    Join Date
    Jun 2013
    Posts
    18


    Did you find this post helpful? Yes | No

    Default Re: PID-filter routine (2nd try).

    Hello Henrik,

    Thank you very much in advance for your kind response.

    After I sent my post here, I searched more at topic and I found another work of you here. The version is 1.5. If it be your last version or last has been published version?

    Best regards & good luck

  14. #14
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,146


    Did you find this post helpful? Yes | No

    Default Re: PID-filter routine (2nd try).

    Quote Originally Posted by andymuller View Post
    ...And of course, to make a stable level of pressure in pipes when people use water.
    ...
    P.S. I am beginner.
    1. If you are a beginner, this may be a difficult project.

    2. On a water pipe network you have to place your sensor on the far most distant point. But given that the pipes are not 100% rigid, you understand that near your pump, pressure will be much higher than the last water outlet.

    Add to that, you have to find optimal values for the P, I and D, you see that it is not as easy as a-b-c.

    Ioannis

  15. #15
    Join Date
    Jun 2013
    Posts
    18


    Did you find this post helpful? Yes | No

    Default Re: PID-filter routine (2nd try).

    Quote Originally Posted by Ioannis View Post
    1. If you are a beginner, this may be a difficult project.

    2. O....

    .... that it is not as easy as a-b-c.

    Ioannis
    Hello Ioannis,

    Thank you very much in advance for your quick response and of course you have put your time to reply here. I appreciate.

    I am beginner in PIC's world but I know what the PID is what the ziegler-nichols method says Thank you buddy for your kidding ...haaa haaaa haaaa I agree with you: it is not easy such as counting a-b-c.... wonderful. lol
    But your second mentioned point is a good point and thinkable because as long as sensor be closer to pump, the fluctuations be more and more, logically.

    Regards,

    P.S. I saw your website, I wish you all the best and good luck, dude.

  16. #16
    Join Date
    Mar 2011
    Posts
    2


    Did you find this post helpful? Yes | No

    Smile Re: PID-filter routine (2nd try).

    I am doing a mini project for a PID control using PIC18F452 or PIC18F877 but I am not good in C code as I am electricial trained.

    Below is my project requirement:
    Hardware interfaces :


    (1) 4 potentiometers as inputs to set the values of Kp, Ki , Kd and desired speed setting via analogue channels
    (2) A dc permanent magnet motor to act as the targeted process to be controlled.


    (3) A driver circuit to power the motor to its desired speed , using PWM output control from the PIC controller ( unidirectional control will be sufficient )
    (4) A speed sensor to provide speed feed-back signal to the PIC controller ( a tacho-generator mechanically coupled to the motor shaft will be the easiest interface)
    Software codes in C :


    (1) Speed of the dc motor to be adjustable by the setting of the speed potentiometer.

    (2) A simple PID algorithm to enable the motor speed response characteristics to be adjusted via the settings of Kp, Ki and Kd.
    I had already done a simple driver circuit to control a motor and it coupled with another motor which provide the output voltage. Can you help me how to kick off to start my c code using pic19F452 for the PID requirement and also help to interface the circuit with my controller with the 4 potential meter intergated?

  17. #17
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,146


    Did you find this post helpful? Yes | No

    Default Re: PID-filter routine (2nd try).

    Maybe the best suggestion is tolook for a C lanquage forum.

    We do Basic here (Pic Basic by Melabs).

    Ioannis

Similar Threads

  1. Darrel's latest 16 bit averaging routine?
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 17th October 2009, 01:57
  2. 2nd order Low-pass passive RC filter on PWM
    By munromh in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 29th January 2009, 19:03
  3. Atod Digital Filter
    By GeoJoe in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 2nd April 2008, 17:04
  4. PID controller in 16F737
    By joeri in forum mel PIC BASIC
    Replies: 8
    Last Post: - 24th June 2006, 11:39
  5. 2nd Order Digital Filter for 24-bit
    By sefayil in forum mel PIC BASIC
    Replies: 0
    Last Post: - 2nd December 2005, 21:55

Members who have read this thread : 2

You do not have permission to view the list of names.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts