PID-filter routine (2nd try).


Closed Thread
Results 1 to 40 of 132

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default

    Ioannis,

    I think you need to limit the pid_Out values to only positive numbers, and discard the negative values instead of converting them to positive ones.

    The way you have it ... the more it tries to reduce the voltage, the higher the output goes.

    After it gosubs to the PID routine ... Try this ...
    Code:
        IF pid_Out.15 THEN pid_Out = 0   ' limit to positive values
        HPWM 1, pid_Out, 500             'Set PWM output
    DT

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


    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.

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


    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

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

  5. #5
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,170


    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

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,623


    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.

  7. #7
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,170


    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 12:52.

  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,170


    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
    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?

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, 02: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, 20:03
  3. Atod Digital Filter
    By GeoJoe in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 2nd April 2008, 18:04
  4. PID controller in 16F737
    By joeri in forum mel PIC BASIC
    Replies: 8
    Last Post: - 24th June 2006, 12:39
  5. 2nd Order Digital Filter for 24-bit
    By sefayil in forum mel PIC BASIC
    Replies: 0
    Last Post: - 2nd December 2005, 22: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