PID-filter routine (2nd try).


Closed Thread
Results 1 to 40 of 132

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    Did you find this post helpful? Yes | No

    Default

    Hi Ioannis,
    I'll just second what Darrel said. For unidirectional control, ie when you can only apply more or less power, not remove power from your "plant" you can't have the output swing negative (which the ABS convert to positive again).

    The example was for controlling a motor where the direction signal basically controls the polarity and the applied torque is controlled by a PWM signal corresponding to the absolute value of the PID signal. That way torque can be applied in both directions.

    In your case you can only apply more or less power, you can't 'reverse' or remove 'power'. Because of that you can't allow negative 'drive'.

    As fir the polarity of the direction bit, that's just a matter of matching it to the hardware I used at the time. If I reversed the polarity of the motor I'd have to reverse the "polarity" of the direction signal. Again, you don't need it in this application.

    /Henrik.

  2. #2
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,133


    Did you find this post helpful? Yes | No

    Default

    Ok, thanks both. I was stupid.

    Now, I 've got a wonderful oscillator!

    No matter the level of P,I,D (except for 0) it does oscillate.

    I suppose it is a matter of tuning, but even with the trimmer as analog inputs to the P,I,D variables, it seems hard to fine tune it.

    When I think I done it, after a power cycle it start again at a rate of 2-5Hz.

    Ioannis

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


    Did you find this post helpful? Yes | No

    Default

    Whatever the values the pid_out stays at 511
    Oh, and clamp the output to 255 for the HPWM command.

    You may want to use 10-bit PWM for finer control.
    DT

  4. #4
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,133


    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

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    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.

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


    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.

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


    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.

  8. #8
    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

  9. #9
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,133


    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

  10. #10
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    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.

  11. #11
    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?

  12. #12
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,133


    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