The control PID is a filter for make fast response in a control system, with optimal adjust, this permit
no make oscilation and fast correction error.
The sample write beupper is a control position adjust the gains PID and position and make compile
sample file and see respnse in proteus.
can modify motor torque, encoder resolution, mass efect.
this algoritm is very used in temperature control speed motor control, position control, lasers figure maker with servo mirrors, cable guide robotics, cnc servo controls, and very much more
see this link
http://en.wikipedia.org/wiki/PID_controllerPosted on: February 07, 2008, 02:09:57 02:09 - Automerged
HERE ONE NEW VERSION MORE SHORT CODE THIS SAMPLE RUN BEST AND MORE FAST
' MADE IN MEXICO BY FERNANDO DZ.
' ESTA ELABORADO PARA EL CONTROL DE POSICION DE UN SERVO DC BRUSH
' SI PUEDEN CONTRIBUIR CON UNA MEJORA AL CODIGO SERIA MUY BUENO.
' ES PARA LA COMUNIODAD DE ESTE FORO
' ESTA PROBADO CON UN MOTOR MAXON (SUIZA) DE 24 VOLTS Y UN ENCODER DE 500 SLOTS POR REV.
' ESTE CODIGO NO EXCEDE 1 K DE MEMORIA POR LO QUE ES MUY RAPIDO DE CORRER
' SE PUEDE USAR HPLL EN 48 MHZ PARA CONDICIONES ADVERSAS
' USAR EJEMPLO DE MICROCHIP AN696 DSN PROTEUS, VER DATOS DE POSICION ENVIADOS
' EN VIRTUAL TERMINAL PONER PAUSA EN PROTEUS CUANDO SE DETENGA MOTOR
' COPIAR LOS DATOS DE LA VIRTUAL TERMINAL
' PEGARLOS EN EXCEL, PARA HACER GRAFICA DE LINEAS Y VER RESPUESTA DEL MOTOR.
' BEST REGARDS GOOD LOOK
' 15.5 KHZ SATURADO 20 US
' 6.3 KHZ COMPENSAND0 80 US
Device = 18F452 ' espacio de CWRITE & CREAD 0 32768 0 FFFE 18f4620
XTAL = 48 ' USA 993 BYTES DE MEMORIA EE
CONFIG_START ' COMIENZA CONFIGURACION FUSE
OSC = xt ' HSPLL CAMBIAR XTAL SPEED
PWRT = On ' Power Up Timer Enabled
WDT = OFF ' Watchdog Timer Disabled
WDTPS = 128 ' Watchdog Postscaler: 128 MAX(18F452) 1:256 '8192 16384 32768
LVP = OFF ' Low Voltage ICSP Disabled
Debug = OFF ' Background Debugger Disabled
CP0 = OFF ' Code Protection Block 0 Disabled
CP1 = OFF ' Code Protection Block 1 Disabled
CPB = OFF ' Boot Block Code Protection Disabled
CPD = OFF ' Data EEPROM Code Protection Disabled
WRT0 = OFF ' Write Protection Block 0 Disabled
WRT1 = OFF ' Write Protection Block 1Disabled
WRTB = OFF ' Boot Block Write Protection Disabled
WRTC = OFF ' Configuration Register Write Protection Disabled
WRTD = OFF ' Data EEPROM Write Protection Disabled
EBTR0 = OFF ' Table Read Protection Block 0 Disabled
EBTR1 = OFF ' Table Read Protection Block 1 Disabled
EBTRB = OFF ' Boot Block Table Read Protection Disabled
CONFIG_END
'Declare PLL_REQ = On ' XTAL X 4
'Declare CCP2_PIN PORTC.2
'CCP1_PIN = PORTC.2
Declare OPTIMISER_LEVEL = 6
Declare SERIAL_BAUD = 19200
Declare RSOUT_MODE = TRUE
Declare RSOUT_PACE = 1
Declare RSOUT_PIN = PORTC.6
Dim CONTROL As DWord
Dim POSICIONOLD As Word
Dim PROPORCIONAL As Word
Dim EROR As Word
Dim INTEGRAL As Word
Dim INTEGRAL_TEMP As Word
Dim DERIVATIVO As Word
Dim SETPOINT As Word
Dim MODULACION As Word
Dim P As Word
Dim I As Word
Dim D As Word
Dim UP As Word
Dim DOWN As Word
Dim VELOCIDAD As Word
Dim VELOCIDAD_A As Word
Dim POSICION As Word
Dim POSTEMP As Word
Dim SATURADO As Bit
Dim SIGNO As Bit
Dim SIGNO_ANTERIOR As Bit
Dim SE_PASO As Bit
' INIT PWM
CCP1CON = %00001100 ' Set CCP1 to PWM
T2CON = %00000100 ' Turn on Timer2, PRESCALE HZ PWM @20 MHZ XX ,11 1250 ,01 5KHZ ,00 20 KHZ
PR2 = 255 ' Set PR2 to get 1KHz out 249 EN 500 Y 255 EN 511
MODULACION = 512
CCP1CON.4 = MODULACION.0 ' COLOCA duty PWM @ 10 BITS
CCP1CON.5 = MODULACION.1 '
CCPR1L = MODULACION >> 2
Output PORTC.2 ' ESTABILIZAR MOTOR EVITA ARRANQUES ARBITRARIOS
High PORTE.2 : DelayMS 100 ' ACTIVAR CHIP L6203 PIN USAR RESISTENCIAS 1K ENTRE PINS DE PIC Y L6203 INPUTS
' END PWM
Dim IMAX As 5000 ' 80
Dim IMIN As 100
Dim COMPL As 511 ' ERA 512
P = 150 ' AJUSTAR PROPORCIONAL
I = 100 ' AJUSTAR INTEGRAL
D = 100 ' AJUSTAR DERIVATIVO
'________________________
POSICION = 32000 ' IN THE MIDDLE WORD VALUE
SETPOINT = 35000 ' AJUSTAR SET POINT
CONTROL = 512
SIGNO = 0
TMR0L = $0
TMR0H = $0
TMR1H = $0
TMR1L = $0
VELOCIDAD = 0
EROR = 0
INTEGRAL_TEMP = 0
PROPORCIONAL = 0
INTEGRAL = 0
DERIVATIVO = 0
POSICIONOLD = 0
SE_PASO = 0
UP = 0
DOWN = 0
ADCON1 = %110 ' 18F452 ALL DIGITAL
INTCON2.7 = 0 ' PULLUPS
High PORTC.6 ' PARA SERIAL
Input PORTA.4 ' CONTADOR DE EVENTOS
Input PORTC.0 ' CONTADOR DE EVENTOS
T0CON =%10101000 ' contador eventos TMR 0 BIT 4 0 LOW TO HI
T1CON =%00000011 ' contador eventos TMR 1
Y:
' INTERRUPCION DE 5 US LOW PRIORITY 6000 RPM ' RB0 AND I/O INTERRUP MOTOR 2000 RPM JALA OK
'UP.LowByte = TMR1L
'UP.HighByte = TMR1H
'DOWN.LowByte = TMR0L
'DOWN.HighByte = TMR0H
'POSICION = UP - DOWN
'VELOCIDAD = POSICION - POSICIONOLD ' CALCULA VELOCIDAD
'POSICIONOLD = POSICION ' ccw positivo hasta 32768
'________________________________________________________________
VELOCIDAD = DOWN
VELOCIDAD = VELOCIDAD - UP
UP.LowByte = TMR0L
UP.HighByte = TMR0H
DOWN.LowByte = TMR1L
DOWN.HighByte = TMR1H
VELOCIDAD = VELOCIDAD + UP ' en los dos casos el num es negativo
VELOCIDAD = VELOCIDAD - DOWN 'OBTENER VAL ABSOLUTO DE VELOCIDAD PARA MOSTRAR CORRECTAMENTE
POSICION = POSICION + VELOCIDAD 'POSICION ABS - 32768 PARA MOSTRAR CORRECTAMENTE
POSTEMP = POSICION ' 0 - 65535
'RSOut Dec UP,13
'RSOUT DEC DOWN,13
' RSOut Dec POSTEMP ,13
EROR = SETPOINT - POSTEMP
EROR = Abs EROR
If POSTEMP < SETPOINT Then SIGNO = 0 ': Low PORTE.0 ' PARA DERECHA O IZQUIERDA
If POSTEMP > SETPOINT Then SIGNO = 1 ': High PORTE.0
If EROR > 255 Then
SATURADO = 1
MODULACION = 512
High PORTE.0
GoTo HASTA_LA_MADRE
Else
Low PORTE.0
PROPORCIONAL = (EROR * P)' >> 1 RSOut Dec PROPORCIONAL,13
SATURADO = 0
EndIf
If SIGNO_ANTERIOR = 1 And POSTEMP < SETPOINT Then INTEGRAL_TEMP = 0 : SE_PASO = 1 ' SI SE PASA DE LA POSICION INTEGRAL 1
If SIGNO_ANTERIOR = 0 And POSTEMP > SETPOINT Then INTEGRAL_TEMP = 0 : SE_PASO = 1 ' ACUMULADA = 0 PARA EVITAR ANTIPHASE OSCILATION 0
If SATURADO = 0 Then INTEGRAL_TEMP = INTEGRAL_TEMP + (EROR)
If INTEGRAL_TEMP > IMAX Then
INTEGRAL_TEMP = IMAX
Else
If INTEGRAL_TEMP < IMIN Then INTEGRAL_TEMP = IMIN
EndIf
' RSOUT DEC INTEGRAL_TEMP,10
INTEGRAL = (INTEGRAL_TEMP >> 6) * I ' >> 6 ' /64
DERIVATIVO = Abs ((VELOCIDAD * D) << 3) ' << 3 ' VELOCIDAD * 8 << 8
' RSOUT DEC INTEGRAL , 13
' RSOUT DEC DERIVATIVO,13
If SE_PASO = 1 Then CONTROL = (PROPORCIONAL + INTEGRAL + DERIVATIVO ) ' SI LE FALTA OK + 1 baja mejor
If SE_PASO = 0 Then CONTROL = Abs ((PROPORCIONAL + INTEGRAL) - DERIVATIVO) ' SI SE PASO OK - 0 baja mejor
SE_PASO = 0
If CONTROL > 65535 Then CONTROL = 65535 : SATURADO = 1 ' PARA 511
CONTROL = (CONTROL >> 7) ' /128 = >> 7 ' 2 4 8 16 32 64 128 256 ' OK
MODULACION = CONTROL
HASTA_LA_MADRE:
If SIGNO = 0 Then MODULACION = (MODULACION + COMPL) ' OK 0
If SIGNO = 1 Then MODULACION = Abs (MODULACION - COMPL) ' OK 1
' RSOut Dec MODULACION,13
'MODULACION = 512 ' 412 - 612 PARA TEST 1023 MAX
CCP1CON.4 = MODULACION.0 ' Store duty PWM
CCP1CON.5 = MODULACION.1 ' a 10-bit
CCPR1L = MODULACION >> 2
SIGNO_ANTERIOR = SIGNO
' APAGA BANDERA INTERRUPCION
DelayMS 3
GoTo Y
End