PID-filter routine (2nd try).


Closed Thread
Results 1 to 40 of 132

Hybrid View

  1. #1
    Join Date
    Aug 2009
    Posts
    7


    Did you find this post helpful? Yes | No

    Default Speed control

    Dear sir,
    I check INCPID routine it works fine as position control.
    I want to control speed via PWM what should i change in the INCPID routine
    and also how can i give pid_Kp pid_Ki pid_Kd using external potantiometer
    which is reading through ADC CH which gives 0-1023 value and how i convert
    this value to $0xxx figure.

  2. #2
    Join Date
    May 2007
    Posts
    65


    Did you find this post helpful? Yes | No

    Wink

    Quote Originally Posted by hadiengg View Post
    Dear sir,
    I check INCPID routine it works fine as position control.
    I want to control speed via PWM what should i change in the INCPID routine
    and also how can i give pid_Kp pid_Ki pid_Kd using external potantiometer
    which is reading through ADC CH which gives 0-1023 value and how i convert
    this value to $0xxx figure.
    As I understand, this PID checks only position.
    To control speed, you should use another routine to control position AMONG TIME (speed), using this PID routine as position control. I did that and works fine. Not just simple speed but parametrical trapezoidal (lineal, curve, log, exp) starts and ends, via USB to my laptop. It works fine almost like an industrial servomotor (24V 500W DC motors). My next experiment is to implement it with 4 BLDC motors I have. I'm in the way to control with a iPhone. Henrik doesn't know how gratefull I am to him for his seed piece.
    Last edited by RodSTAR; - 5th August 2009 at 17:47.
    "Beethoven had his critics too, see if you can name 3 of them"

  3. #3
    Join Date
    Aug 2009
    Posts
    7


    Did you find this post helpful? Yes | No

    Smile PID SPEED CONTROL QUESTION FOR Mr Henrikolsson

    Dear sir,
    What should i change in INCPID file that it controls the speed of motor not position.
    And also i want to put Kp Ki Kd values through potantiometers.How it is possible.?

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


    Did you find this post helpful? Yes | No

    Default

    Hi,
    You don't need to change anything. If you're aiming for speed control only then simply measure the speed of your motor with whatever you like, encoder, tachometer, BEMF or whatever. Compare that to your target speed and send the difference between the two (ie. the error) to the PID-filter. The filter itself does not know or care what you are regulating, it might as well have been air-pressure as far as the PID-routine is concerned.

    If you need to move to a certain position WITH a certain speed then you need to take the moving target aproach like RODStart outlined. If you control position and add 1 to the target position every second the motor will move 1 encoder count every second or 10.8° per minute or 0.03rpm with a 500line encoder.

    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

    /Henrik.
    Last edited by HenrikOlsson; - 6th August 2009 at 10:54.

  5. #5
    Join Date
    Aug 2009
    Posts
    7


    Did you find this post helpful? Yes | No

    Default QUESTION FOR Mr HENRIKOLLSON

    Dear sir thank u for reply.
    My question is that i can get 10 bit resolution of PWM output to control motor speed.
    I use following code.
    CCP1CON = %00001100 ' Set CCP1 to PWM
    T2CON = %00000111 ' Turn on Timer2, Prescale=4
    PR2 = 249 ' Set PR2 to get 1KHz out
    CCP1CON.4 = pid_out.0 ' Store duty to registers as
    CCP1CON.5 = pid_out.1 ' a 10-bit word
    CCPR1L = pid_out >> 2
    It will give 10bit PWM output at 16MHz crystal.
    Now spose pid_Error=+5 then spose after calculation
    pid_Out=100
    pid_out.15=0 'DIRECTION BIT
    same result will be if pid_Error=-5 but
    pid_out.15=1
    With both errors final PWM resolution is same but how can I use direction bit to change PWM output to increase or decrease motor speed according to requirement.?
    If there is any OFFSET which must be used ?
    Last edited by hadiengg; - 7th August 2009 at 14:46.

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


    Did you find this post helpful? Yes | No

    Default

    Hi,
    The approach with the direction bit is for sign and magnitude PWM while, by the sound of it, you're looking for locked anti phase. In locked antiphase the PWM dutycycle is 50% when no torque should be delivered by the motor (the average current thru the motor is 0A). The dutycycle is then increased or decreased from 50% depending on which direction and how much torque the motor should deliver.

    So, if you ARE doing locked antiphase PWM simply set the PWM duty cycle to 50% on startup and then, if pid_out.15 is set (output is negative) you subtract the ABS value of pid_out from the value coresponding to 50% and if pid-out.15 is "0" you add the ABS value of pid_out to value coresponding to 50%.

    If you are doing sign and magnitude then your hardware will have an input for direction and you need to set that according to pid_out.15 and then set the dutycycle to the ABS value of pid_out.

    I sugest you set the pid_Out_Clamp variable to a suitable value to prevent over or underflow in the duty-cycle register.

    /Henrik.

  7. #7
    Join Date
    Aug 2009
    Posts
    7


    Did you find this post helpful? Yes | No

    Default THANKS Mr OLLSON

    Thanks for coperation.
    I am trying to complete my job.

  8. #8
    Join Date
    Oct 2005
    Location
    Stuttgart, Germany
    Posts
    24


    Did you find this post helpful? Yes | No

    Default

    First of all, i would like to thank Mr. Olsson for his great work.
    Currently i'm working with the AC heater, driven by the SSR. This heater is very inertial and slow, in particular dropping the temperature.

    In order to drive it, i implemented some sort of rude PWM, which looks as following:

    ---------------------------------------------------------
    pid_Out_Clamp=200
    ' i also set the pid_Out value to 0 in case of direction flag is raised (pid_Sign = pid_Out.15 thing)

    loop:
    period=2000 'period of PWM in mS
    time_on=pid_Out*10 ' i feed to pid filter the temperature, not the ADC value, therefore multiplication in order to scale to the period. Or this is wrong and i should use pid_Out value directly?'
    HIGH portc.3
    PAUSE time_on
    time_off=period-time_on
    LOW portc.3
    PAUSE time_off
    LCDOUT ...
    GOTO loop
    -----------------------------------------------------------------

    Now, i have run a test and the filter seems to work, except it gives an overshoot on the beginning, but then stabilises 1-2 degrees below target.
    Now i would have to tune the gains and i tried to understand the explanation of the HEX format, but unfortunately it is not completely clear for me. Are you using this representation in order to have floating point values?

    I just can't get the inverse conversion algoritm from, let's say, 2.55 to HEX.
    As i undersand, in this case it would be $02..<-- here should come 0.55 part.
    So, using your example: "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."

    1/256*x=0.55 --> x=0.55/(1/256) -->140 --> convert to HEX -->8C
    Final result: $028C, or decimal 652 <--- is it correct??

  9. #9
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,615


    Did you find this post helpful? Yes | No

    Default

    Hi,
    The gains are just numerical values like any other variable. Assigning values to the gain variables using hex doesn't, in itself, allow "floating point" but it makes it easier to visualise what the "real" gain is. Or perhaps not... ;-)

    Lets look at the proportional term:

    Lets say you set pid_Kp to $0280 (or 640 in decimal if you like). Now you calculate pid_Error to, lets say, 125 and send it to the filter. The filter will then calculate the proportional term to 125 */ 640 which is the same as 125*640/256=312. Or, put another way 640/256=2.5 and 125*2.5=312.

    If you set pid_kP to 102 (or $66) you'll get a gain 102/256=0.398 and an output, in the example above of 125*102/256=49 which is basically the same as 125*0.398.

    If you know you want a gain of 3.7 then simply multiply that value by 256 to get the value. 3.7*256=947. pid_Kp=947 or pid_Kp=$03B3. Where, in this case $03 means 3 and $B3 means $B3/$FF or 179/256=0.7 ie a gain of 3.7

    With the same error as above: 125*947/256=462 which is the same as 125*3.7=462.

    So, using HEX doesn't make it floating point per se, it's just another way of assigning values to the variables. Say you wanted a gain of 4, then pid_kP=$0400 is easier to understand then pid_Kp=1024 but they both result in the exact same thing.

    /Henrik.
    Last edited by HenrikOlsson; - 25th January 2011 at 16:11.

  10. #10
    Join Date
    Oct 2005
    Location
    Stuttgart, Germany
    Posts
    24


    Did you find this post helpful? Yes | No

    Default

    Thanks for the prompt reply! Well, now it is clear for me, that it is clear for you
    And i also got now how it works. But for myself, i would use DEC values, they are much obvious, IMHO of course.

    But i do have one more question: what would be a good point to start with pid_I_Clamp?

    In your example code you had for pid_Out_Clamp 511 and pid_I_Clamp 100.
    If i have pid_Out_Clamp 200, would, say, 40 be a good starting point for pid_I_Clamp?
    Also for the sampling frequency you have ser every eight's, don't you think is to high in my case, with the period of 2 seconds?
    I ask it, because i just want to realy reduce the possible variables to Kp,d,i, before i start tuning them.

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