PID-filter routine (2nd try).


Closed Thread
Results 1 to 40 of 132

Hybrid View

  1. #1


    Did you find this post helpful? Yes | No

    Default Re: Hi Henrik again after little time

    Hello Henrik, All,

    I have read through this thread regarding servo PID motor control that Henrik wrote. I need to modify the code for the direction part of the code. My h-bridge has 3 i/o. PWM drive, Portd.0 and Portd.1.

    The allowable states are:
    portd.0 = 1 and portd.1 = 0
    portd.1 = 0 and portd.1 = 1

    I see that Henrik uses 1 bit to control direction. can I simply replace his code
    "Direction = pid_Out.15 'Set direction pin accordning to sign"

    with.....

    if pid_out.15 = 1 then
    portd.0 = 1
    portd.1 = 0
    else
    portd.0 = 0
    portd.1 = 1
    endif

    I am concerned about messing up timing by introducing these lines of code. Will this affect the integrity of the original code?

    Best Regards,
    Nick

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


    Did you find this post helpful? Yes | No

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

    Hi Nick,
    Sure, you can do that - it won't have any effect on the PID code itself and it won't have any effect on the timing of the loop if you're not running the loop at extremely high frequencies.

    However, your pins will be in an invalid state for a short period of time since you set each individual bit separately. Ie when going from 10 to 01 you first clear the high bit, now the output is 00, then you set the low bit to make the output 01. This is likely not a problem but depending on your particular hardware it's possible that 00 and/or 11 has some either undefined function or perhaps they brake the motor by shorting the two lower or two higher switches of the bridge.

    If this prooves to be a concern then you can either fix it with hardware, a single inverter is all you need and you save a PIC pin. If adding hardware is not an option then look into writing all PortD-bits in one go, perhaps something like:
    Code:
    IF PID_Out.15 = 1 THEN
       PortD = (PortD & %11111100) + 1
    ELSE
       PortD = (PortD & %11111100) + 2
    ENDIF
    I'm sure there are other ways of doing it too.

    /Henrik.

    EDIT: Aaarh, now the bloody editor keeps messing with my code again, removing the %-sign AND two digits in the binary value following it...
    Last edited by HenrikOlsson; - 7th November 2011 at 18:19.

  3. #3


    Did you find this post helpful? Yes | No

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

    Good point. I will make the suggested change. In your main code you have HPWM 1, pid_out, 10000 .... I know that pid_out is declared a word (2 bytes) and frequency is 10Khz? The HPWM in the manual says it works with 1 byte for duty cycle....can you explain this part of it? How does feeding a word work with HPWM?

    Best Regards,
    Nick

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


    Did you find this post helpful? Yes | No

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

    Uhh, I guess it doesn't really...you're the first to notice!
    That's an oversight on my behalf as far as the example goes. However, if you clamp the output to +/-255 using pid_Out_Clamp=255 I think it'll work as shown. I think PBP is smart enough to only get the low byte of the word in this case.

    All internal calculations in the PID filter are based on word-sized variables so pid_Out should and must be a WORD.

    /Henrik.

  5. #5


    Did you find this post helpful? Yes | No

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

    Hello Henrik,

    I just thought you should know that the code works perfectly in my servo application with the mods. I need to tweak constants / gains. Making small incremental changes in position is smooth. Making large changes causes some jerkiness and overshoot but no oscillations!


    Best Regards,
    Nick

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


    Did you find this post helpful? Yes | No

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

    Hi Nick,
    It's always nice to hear that things work out.
    Large step inputs are always "hard" on the filter. It's definitely possible to tune the overshoot out, (lowering I and increasing D should help) but that has other drawbacks. In a servomotor application it's common to have a trajectory planner (think ramp up speed, run, ramp down) before the filter. Basically this "divides" a large step into several small ones making the overshoot you see with a large step input considerably smaller.

    /Henrik.

  7. #7


    Did you find this post helpful? Yes | No

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

    I see...basically no different than a CNC profile step shape going into the motor driver. I will work on the ramp up/down function and see if I can improve things a bit.

    I need to control 2 servo motors each with feedback pot. Is it as simple (yet inefficient) as two separate routines one for each servo on the same MCU? or is there a more cleavor way of doing it? Maybe using Flags to switch between the two servo's running through 1 PID routine (not 2 routines)?

    Nick

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 : 1

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