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

    Thanks a bunch Mike, it's always nice to hear when things works out!

    If you don't mind (and are allowed) to disclose some details about the system I'd love to hear about it. Like what it is you're controlling, sensor, sampling rate, how you tuned it etc. I think it would make a nice post for future (and current) users of the PID-routine.

    I've been considering an update of the routine for quite some time, you gave me the motivation to move forward with it. I just need to get my PBP260 working on my main PC here (looks like an OS re-install is needed and I hate doing that). I'll try adding feed-forward and a few other things, mostly tailored towards motor-control although probably useful in other applications as well.

    Thanks again Mike!
    /Henrik.

  2. #2
    Join Date
    Nov 2009
    Location
    Dallas, Texas, USA
    Posts
    10


    Did you find this post helpful? Yes | No

    Default

    The application was a rewind control on a rather elderly commercial printing press.

    The original setup was a commercial PID board made up of *many* op amps. At its best it was cranky acting when it was working well, and it had been going flaky for some time. When it finally failed, and I determined that it had chips on it that I had no idea how to get and that the OEM had discontinued the board long ago, I decided to replace it with a digital implementation.

    The system consists of a 50HP speed regulator DC drive and a control board that accepted an input from a pot set by the machine operator that determined the desired dancer position (check out Wiki or the like if you're not up on compressed air loaded printing press dancer rewind setups). A gear driven pot provided feedback from the dancer, indicating its position.

    The drive required a 0-15VDC reference signal. I built up a board consisting of a +18 and +5 supply. +5 supply was run out to the operator and dancer pots. Those two signals became setpoint and feedback, respectively, and were dumped directly into PIC A/D pins. Half of a LM358 op amp fed by a simple RC filter took the PIC PWM output, smoothed it, and raised it to 0-15VDC.

    My calling app sets the reference voltage to the drive to zero should the PID routine ever call for a negative, but that's probably superfluous.

    Took less than an hour to set up the P, I & D constants to achieve better dancer control than the original had *ever* provided.

    I have no doubt that the identical hardware, with no changes other than retuning in software, could do a perfectly acceptable job in any web material rewinding application. And speed regulator drives are simpler to set up, less expensive, and more readily available than the torque regulator drives that I've used in similar applications in the past.

    Again, thanks for your help and use of a very handy routine!

    Mike

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


    Did you find this post helpful? Yes | No

    Default

    Thanks Mike!
    A 50HP DC drive...that's cool!

    /Henrik.

  4. #4
    Join Date
    Dec 2009
    Location
    Kalamazoo
    Posts
    42


    Did you find this post helpful? Yes | No

    Default pid routine for h-bridge

    first of all, a big thanks to mr henrik for this wonderful code. it works, as i have it all set up right here.
    but instead of locked antiphase, im trying to implement a h-bridge powered by the two hpwm (forward/reverse motion)on a 18f4431.
    i have a lcd displaying variables like position, setpoint, dutycycle1(for hpwm0) and dutycycle2(hpwm1)

    Main: ; main loop

    LCDOUT $FE, $80, "POS=" ,DEC5 POSITION, " SP=" ,DEC5 SETPOINT
    LCDOUT $FE, $C0, "DUTY1=" ,DEC3 DUTY1, " DUTY2=" ,DEC3 DUTY2
    IF PORTB.3 = 0 then SETPOINT = SETPOINT + 1 ; Increment setpoint
    select case setpoint ; Circular count for Setpoint condition
    case is > 65535 ; Circular count for Setpoint condition
    setpoint = 0 ; reset setpoint
    POSCNTH=0 ; set counter for encoder, H bit ; reset QEI counter high byte
    POSCNTL=0 ; set counter for encoder, L bit ; reset QEI counter low byte
    end select
    ;SELECT CASE PID_ERROR
    IF POSITION<SETPOINT THEN ; End of Circular count for Setpoint condition
    pid_Error = setpoint - position
    ;GOSUB FORWARD
    ENDIF
    IF POSITION>SETPOINT THEN
    PID_ERROR = POSITION - SETPOINT
    endif

    gosub pid ; go to PID rutine
    DIRECTION = PID_OUT.15
    select case direction ; condition determine PWM
    case 0 ; condition determine PWM
    Duty1 = abs pid_out
    GOSUB FORWARD
    ;DUTY1 = 0 ; condition determine PWM
    case 1 ; condition determine PWM
    Duty2 = ABS pid_Out
    GOSUB BACKWARD
    ;DUTY2 = 0
    END SELECT

    goto Main ; do main loop

    sub forward turns on first hpwm and sub backward turns on second hpwm.

    here is the problem: if i increment/add to the setpoint, both duty cycles reset when position rolls over from 65535 to 0. i know its something to do with my integer math, and ive spent a whole week on this issue. im trying to implement a step/dir routine, for my cnc machine.

    any suggestion will be highly appreciated. thanks
    NAG CON WIFE!
    WIFE VAR MOOD

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


    Did you find this post helpful? Yes | No

    Default

    Hi,
    I'm not sure I understand....sorry....

    First of all, the SETPOINT variable you have - is it declared as a LONG or as a WORD? If declared as a WORD then I'm not sure how this...
    Code:
    select case setpoint ; Circular count for Setpoint condition
    case is > 65535 ; Circular count for Setpoint condition
    ...will work or what it'll evalute to. A word variable will "automatically" wrap around from 65535->0 and from 0 to 65535. If setpoint is declared as a LONG that seems not needed since you're trying to reset it 0 once it goes above 2^16.

    Then there's this line:
    Code:
    IF POSITION pid_Error = setpoint - position
    Does it really compile like that?

    If you have code that compiles correctly, please post it and I promise I'll do my best to help you. You may also be interested in this thread

    /Henrik.

  6. #6
    Join Date
    Dec 2009
    Location
    Kalamazoo
    Posts
    42


    Did you find this post helpful? Yes | No

    Default

    thanks for the quick reply

    the setpoint is a word variable, and resets itself from 65535 to 0 and vice versa.
    there must have been an error when i copied and pasted, because the sentence reads:

    pid_error = setpoint - position

    this is a very buggy piece of code, and when i get it working, im going to rearrange and clean it up good.
    NAG CON WIFE!
    WIFE VAR MOOD

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