PDA

View Full Version : Henrik's incPID, can't get it to work (Darrel?)



Muller
- 9th April 2008, 23:34
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

Darrel Taylor
- 10th April 2008, 02:39
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,

Muller
- 10th April 2008, 04:50
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

mister_e
- 10th April 2008, 05:02
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 ;)

Darrel Taylor
- 10th April 2008, 05:24
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>

mister_e
- 10th April 2008, 05:30
or if you have some spare time and patience, MPLAB SIM

Ioannis
- 10th April 2008, 12:25
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