Henrik's incPID, can't get it to work (Darrel?)


Closed Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    Apr 2008
    Posts
    12

    Default Henrik's incPID, can't get it to work (Darrel?)

    I've tried using Henrik's include file incPID but I can't get it to work. I plugged in a LCD to see what was going on and noticed that my ADC, Set_point and Error variables are working fine, but my PID_out variable is always 0, no matter what.. By the way, this code does work without the PID part.

    I'm using a pic 18f2520 at 16Mhz int osc.

    here's my code



    asm
    __CONFIG _CONFIG1H, _OSC_INTIO67_1H & _FCMEN_OFF_1H & _IESO_OFF_1H
    __CONFIG _CONFIG2L, _BOREN_OFF_2L & _PWRT_ON_2L
    __CONFIG _CONFIG2H, _WDT_ON_2H
    __CONFIG _CONFIG3H, _MCLRE_OFF_3H & _CCP2MX_PORTBE_3H
    __CONFIG _CONFIG4L, _LVP_OFF_4L & _DEBUG_OFF_4L & _XINST_OFF_4L
    __CONFIG _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L & _CP2_OFF_5L & _CP3_OFF_5L
    __CONFIG _CONFIG5H, _CPB_OFF_5H & _CPD_OFF_5H
    __CONFIG _CONFIG6L, _WRT0_OFF_6L & _WRT1_OFF_6L & _WRT2_OFF_6L & _WRT3_OFF_6L
    __CONFIG _CONFIG6H, _WRTC_OFF_6H & _WRTB_OFF_6H & _WRTD_OFF_6H
    __CONFIG _CONFIG7L, _EBTR0_OFF_7L & _EBTR1_OFF_7L & _EBTR2_OFF_7L & _EBTR3_OFF_7L
    __CONFIG _CONFIG7H, _EBTRB_OFF_7H
    endasm
    '************************************************* **********************
    '
    ' PID

    Setpoint VAR WORD '<---This is your variable.


    INCLUDE "incPID.pbp" 'Include the PID routine.
    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 = 1000 'Clamp the final output to ±1000
    '************************************************* ***********************

    '
    ' A/D
    '

    ADCON0 = %00000001 ' A/D converter on, CH0 selected
    ADCON1 = %00001110 ' AN<0> Analog, other digital
    ADCON2 = %10010110 ' FOSC/64, 4 TAD, right justified

    @ADRead = ADRESL
    ADRead VAR WORD EXT
    GoDone var ADCON0.1
    '************************************************* **********************

    '
    ' Osc
    '
    OSCCON = %1100000 '4Mhz
    OSCTUNE.6 = 1 'PLL on
    Define OSC 16
    '************************************************* **********************

    '
    ' Com
    '
    cmcon = 7
    '************************************************* **********************

    '
    'PWM
    '
    ccp1con = 0
    pr2 = 249 '1Khz
    t2con = %110 ' T2 On Prescaler 1:16

    '************************************************* **********************

    '
    'I/O
    '
    Trisb = 0
    Trisa = %11
    trisc = 0
    portb = 0
    portc = 0
    '-----------------------------------------------------------------------------------

    '
    'Vars
    '
    duty var word
    duty = 0
    advar var word
    b0 var byte
    CounterA var Byte
    RawData var Byte [8]
    <div id="ClearText"><b>clear</b></div><script language="JavaScript" src="http://www.pbpgroup.com/js/ColorText.js"></script><script>ColorText("ClearText",['red','green','blue','magenta','cyan','black']);</script>'-----------------------------------------------------------------------------------
    duty = 200
    CCP1CON.5=DUTY.1
    CCP1CON.4=DUTY.0
    CCPR1L=DUTY>>2
    ccp1con = %1100

    setpoint = 120

    goto main

    '
    'Reads Analog value, Sorts it, And gets the mean value of the middle 4 bytes, that's advar
    '
    makeADC:
    for b0 = 0 to 7
    PAUSEUS 10
    GODONE=1
    WHILE GODONE : WEND
    rawdata[b0]=(ADREAD>>2)
    next b0

    CounterA = 0
    SortLoop:
    If RawData(CounterA+1) < RawData(CounterA) then
    RawData(CounterA) = RawData(CounterA) ^ RawData(CounterA+1)
    RawData(CounterA+1+0) = RawData(CounterA) ^ RawData(CounterA+1)
    RawData(CounterA) = RawData(CounterA) ^ RawData(CounterA+1)
    If CounterA > 0 then CounterA=CounterA-2
    endif
    CounterA=CounterA+1
    If CounterA < 15 then goto SortLoop


    advar = 0
    for b0 = 2 to 5
    advar = advar +rawdata[b0]
    next b0
    advar = advar/4

    Return

    '************************************************* ************************

    main:



    gosub makeADC

    pid_Error = Setpoint - Advar 'Calculate the error
    Gosub PID 'Result returned in pid_Drive
    pid_Out = ABS pid_Out 'Convert from two's comp. to absolute



    duty = pid_out
    if duty>1000 then duty = 1000
    CCP1CON.5=DUTY.1
    CCP1CON.4=DUTY.0
    CCPR1L=DUTY>>2
    pause 1


    goto main
    Last edited by Darrel Taylor; - 10th April 2008 at 04:36. Reason: Hard to find. Pointed it out :)

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


    Did you find this post helpful? Yes | No

    Default

    Hi Muller,

    You have a CLEAR statement in the middle of the program.

    It 0's out the PID variables that were previously set.

    hth,
    DT

  3. #3
    Join Date
    Apr 2008
    Posts
    12


    Did you find this post helpful? Yes | No

    Default

    Darrel.. Thanks a lot, and sorry for the stupid mistake... I'll be more careful next time. By the way, nice eyes I would have never spotted that

  4. #4
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    I suggest you to group similar things together. Hardware, includes, Register aliases, I/o Aliases, Variable definition, Constant definition, then before your main, you set your initial variables values.

    and yeah a simple CLEAR may ruine your life
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

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


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Muller View Post
    By the way, nice eyes I would have never spotted that
    My eye's are old and useless, well, until I get that new pair of glasses I needed 3 years ago.

    But the problem was easily found with a quick run in MCS+ debugger.
    Might think about picking up MicroCode Studio Plus, if you haven't already.
    <br>
    DT

  6. #6
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    or if you have some spare time and patience, MPLAB SIM
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

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


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by mister_e View Post
    or if you have some spare time and patience, MPLAB SIM
    I am sure Darrel did not see the small fonts! At least untill he gets the new pair of glasses!

    Ioannis

Similar Threads

  1. PortA Doesn't Work
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 11
    Last Post: - 8th September 2015, 18:41
  2. Problem implement "Henrik" incPID
    By phoenix_1 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 26th September 2009, 14:36
  3. Can't get POT work on P12f675 - Newbie
    By berslan in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 26th March 2008, 21:22
  4. blink.bas help cant make any 18f's work
    By Bonxy in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 13th December 2004, 21:28
  5. Pin RA4 doesn't work
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 0
    Last Post: - 15th July 2004, 12:03

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts