PID-filter routine (2nd try).


Closed Thread
Results 1 to 40 of 132

Hybrid View

  1. #1
    Join Date
    Feb 2009
    Posts
    10


    Did you find this post helpful? Yes | No

    Smile PID for servo

    Hi Alain and others,
    Thank you all for info given, it is much appreciated.

    Alain, regarding the linear problem - I have tested the actual distance travel of the servo arm by connecting a ruler to the arm.
    To test I then adjusted the speed voltage, taking voltage readings and distance readings as I go along.
    The distance values on Excel graph showed a non linear line in comparison to the linear nature of the voltage.
    /Daan Joubert

  2. #2
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,653


    Did you find this post helpful? Yes | No

    Wink

    Quote Originally Posted by daan.joubert View Post
    Hi Alain

    The distance values on Excel graph showed a non linear line in comparison to the linear nature of the voltage.
    /Daan Joubert
    Hi, Daan

    I hope you've considered the rotation angle of the arm is linear to the signal ...

    But, whatever your transmission system ... only rack and pinion or cable and pulley can give a linear travel !!!

    You could use a LOOKUP Table to correct your output ... but, once more, if it is a real PID controller, it will compensate for non-linearities ...

    Or try to use ... FUZZY LOGIC !!!

    Or find an old LINEAR R/C servo ( Kraft, Robbe, Varioprop, Lextronic ... produced some in the 70-80's )


    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  3. #3
    Join Date
    Nov 2008
    Posts
    19


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by daan.joubert View Post
    Hi Alain and others,
    Thank you all for info given, it is much appreciated.

    Alain, regarding the linear problem - I have tested the actual distance travel of the servo arm by connecting a ruler to the arm.
    To test I then adjusted the speed voltage, taking voltage readings and distance readings as I go along.
    The distance values on Excel graph showed a non linear line in comparison to the linear nature of the voltage.
    /Daan Joubert
    Your gains might need to be fractional all the way up to 20 times and more via your vresistors via adc. Example engine running no load 1500 rpm, adc 1.5/5 * 256 = 77. setpoint = 77,unknown but say pwm 25, all gains apart from p set to zero. 25% load applied engine droops to 500rpm,adc 0.5/5 * 256 = 26. 77-26= 51, pwm output 51. If output to much p gain * 0.8 etc,to little * 1.5 etc. You should get to the stage of engine hunting turn the gain slightly below this. This is then where the data from i and d comes into play and slightly tweaking each one results in a reactive genset regardless of load.

  4. #4
    Join Date
    Aug 2009
    Posts
    7


    Did you find this post helpful? Yes | No

    Default Motor Speed Control

    Thanks Mr Henrik,Mr RodSTAR and Mr Anonymouse for help.
    I succesfully run the motor at constant speed.Motor speed also change through given speed refrence.
    Now one problem that i face is motor overshoot in slow speed refrence whenever i turn on the power.Its overshoot cycle comes down when i increase speed refrence.At full speed refrence it get exact speed never overshoot.I am using same PID gains used in original file.I think i must change the GAIN value to overcome this problem.
    And also i am using variable for 0-5V to 0-1023 value through ADC.How i use this value to put the gain as used under.
    pid_Kp = $0700 'Set Kp to 7.0
    Please give some code to convert the value.

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


    Did you find this post helpful? Yes | No

    Default

    Hello,
    I thought I explained that in post #16 above. I'll simply quote myself:
    Regarding the pots, you can set the PID gains in any number system you like. The reason I show it in hex is that it is easier IMO. You can simply do pid_Kp = myADResult if you like.

    1023 in decimal is the same as $03FF which, in the PID filter, is treated as 3.996 basically. See, 03 is the number on the left side of the decimalpoint and FF is on the right side. FF represents 1/256*255 or 0.996. If you'd feed it 102 from the ADConverter it would be the same as $0066 or a gain of 0.258.

    If you need more gain than 1023 then simply multiply your ADResult, pid_KP=myADResult*2, now you get a gain of 0-2046 or $0000-$07FE or 0.0-7.992
    I don't really know how to explain it in any other way but let me know if doesn't make sense and I'll try, or perhaps someone else can attack it from another angle.

    /Henrik.

  6. #6
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,653


    Did you find this post helpful? Yes | No

    Wink

    Hi, Hadienggggggg

    At full speed refrence it get exact speed never overshoot.I am using same PID gains used in original file.I think i must change the GAIN value to overcome this problem
    EACH system has its own set of parameters ( Kp, Ki, Kd ) and you are the one and only one who can find them ...

    NOW, just Google around for the " Ziegler and Nichols method " ... sure it will help you !

    FURTHER ... you can disable The Ki and Kd action at startup and engage then for a maximum error of ???, you can use retrieve tables for Kp,Ki,Kd as a function of the running point, you can program a rampup for engine start ...

    But that is VERY typical to YOUR project ... and far out from Henrik's project shown here ...

    BTW ... MERCI Henrik ...

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  7. #7
    Join Date
    May 2007
    Location
    Republic Serbia
    Posts
    105


    Did you find this post helpful? Yes | No

    Default My probe with problem of use incPID.pbp

    The QEI reading work good with count for position from 0-1000.
    The PPWM give good voltage and signal too from 0 - 1024 at 20KHz.
    The Henrik PID rutine work good but only when position and setpoint is max +/-50 !
    and realy how make to motor clip +/- when position = setposition in some range about +/- 25% as break in position.
    My output driver have next logic :
    with PWM 512 motor STOP
    0<--------512----->1024
    left <---- stop-----> right
    fast slow stop slow fast

    Here is my debug program:
    Code:
    '****************************************************************
    '                 Definicije PIC-a  18F4431                     *
    '****************************************************************
        DEFINE OSC 20
        include "incPID.pbp"
    '****************************************************************
    '                        LCD DEFINE LCD 4X20 chr             *
    '****************************************************************
        DEFINE LCD_DREG PORTD
        DEFINE LCD_DBIT 4
        DEFINE LCD_RSREG PORTC
        DEFINE LCD_RSBIT 0
        DEFINE LCD_EREG PORTC
        DEFINE LCD_EBIT 1
        DEFINE LCD_BITS 4
        DEFINE LCD_LINES 4
        DEFINE LCD_COMMANDUS 2000
        DEFINE LCD_DATAUS 50
                     
    '****************************************************************
    '                          Definicije Portova                   *
    '****************************************************************    
        ADCON0=0                                
        ANSEL0=0
        TRISB = %00000000
        PortA = 0                               
        PortB = 0                                  
        PortC = 0
        PortD = 0                                
        PortC.3 = 1                                 
                             
    '****************************************************************
    '                          Definicije  PPWM                     *
    '****************************************************************   
        DTCON = 0 
        PTCON0 = %00000000 
        PTCON1 = %10000000 
        PTPERL=$FF      
        PTPERH=$00
        PWMCON0 = %00100000 
        PWMCON1 = 1 
        OVDCOND = %11111111                     
    '****************************************************************
    '                          Definicije QEI encodera              *
    '**************************************************************** 
        'QEICON = %10011000
        'DFLTCON = %00111000                
        'MAXCNTL = %11001111              
        'MAXCNTH = %00000111
        QEICON=%10001000
        DFLTCON = %00111000                
        MAXCNTL = %11101000               
        MAXCNTH = %00000011               
        PosLow VAR BYTE                           
        PosHigh VAR BYTE                           
        PosTemp VAR BYTE                          
        Position VAR WORD                          
        POSCNTL = 0                                
        POSCNTH = 0
    '****************************************************************
    '                          Definicije  TMR0                     *
    '****************************************************************     
        T0CON = %11001000
        INTCON.5 = 1
        INTCON.1 = 0
        INTCON.7 = 1
        INTCON.4 = 0
        INTCON.3 = 0
        INTCON.2 = 0
        INTCON2.2 = 1
        RCON.7 = 1
        TMR0L =254 
    '****************************************************************
    '                          Definicije PID filtera               *
    '****************************************************************
        x var word
        y var word
        ADValue VAR word                           '<---This is your variable.
        Setpoint VAR WORD                          '<---This is your variable.
        Direction var bit                          '<---Direction bit to motor driver 
        pid_Kp = $0700                             'Set Kp to 7.0
        pid_Ki = $0080                             'Set Ki to 0.5
        pid_Kd = $0225                             'Set Kd to 2.14
        pid_Ti = 8                                 'Update I-term every 8th call to PID
        pid_I_Clamp = 100                          'Clamp I-term to max ±100
        pid_Out_Clamp = 511                        'Clamp the final output to ±511
        x = 0
        y = 0
        setpoint = 100                               
        pause 250                                  
        lcdout $fe,1                               
    '****************************************************************
    '                          Osnovna petlja                       *
    '****************************************************************
    on interrupt goto PIDcalc
         
         loop:
         if portc.3 = 1 then setpoint = setpoint + 1
         if setpoint > 1000 then setpoint = 0
         LCDOUT $fe,128,"POSITION = ",dec position  
         LCDOUT $fe,192,"SETPOINT = ",dec setpoint  
         LCDOUT $fe,148,"PID = ",dec pid_out        
         LCDOUT $fe,212,"PWM = ",dec y              
         pause 10                                   
         lcdout $fe,1                               
         goto loop                                  
             
         disable
    PIDcalc:
         INTCON.1 = 0                                
         if INTCON.2 = 1 then                        
         PosHigh = POSCNTH                           
         PosLow = POSCNTL                            
         PosTemp = POSCNTL                           
         If PosLow - PosTemp = 0 then Goto Done      
         PosHigh = POSCNTH                           
         PosLow = POSCNTL
    Done: 
         Position = POSHIGH * 256 + PosLow           
         advalue = position 
         pid_Error = Setpoint - ADValue               
         Gosub PID                                    
         Direction = pid_Out.15                       
         pid_out = ABS pid_Out
         x = (512 - pid_Out) + 512
         select case direction
         case 1
         y = pid_Out
         case 0
         y = x
         end select
         PDC0L = y.LowByte
         PDC0H = y.HighByte
         endif
         INTCON.2 = 0                                 
         resume                                       
         enable
         end
    If any help pls.
    Regards Robert
    Last edited by phoenix_1; - 26th September 2009 at 14:17.

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


    Did you find this post helpful? Yes | No

    Default

    The Henrik PID rutine work good but only when position and setpoint is max +/-50 !
    OK, so what does it do if the setpoint is 51 then?

    I see you're using On Interrupt and it seems you're using TMR0 as the source for the interrupt. It would be good if you could comment that section a bit so we don't have to read thru the datasheet in order to figure out what you're trying to do.

    If I haven't missed anything you're running TMR0 in 8bit mode, clocked from the internal clock @20/4=5Mhz, prescaler selection is set to 1:2 which means the timer is clocked at 2.5Mhz, overflowing and generating interrupts at 9765.625Hz. There's no need to run the PID-loop at that speed, generally 1000Hz is good enough for motor control.

    The Pause 10 statement needs to go away, and the LCDOut statements will likely mess with the timing as well. Read up on how On Interrupt works in the PBP manual. I highly recomend you look up Darrels Instant Interrupt routines as they don't suffer from the things On Interrupt does.

    What do you mean by
    and realy how make to motor clip +/- when position = setposition in some range about +/- 25% as break in position
    It's really hard to understnad what you mean..... A deadband where anything within 25% of the setpoint is good enough or a following error trip and when the error is bigger than a set amount?

    I've just received my copy of v2.60 so I can start using 32bit variables. I'm having a bt of a problem getting it going though. On top of that I fried my servo development board by connecting 24V to the 5V input so I need to get that sorted....

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