Well I'm not sure if I'm making progress but here a few things I've noticed.
First, I cannot get this to maintain a stable output current at all. It will vary along with the input voltage.
Second, The flickering is, depending on input voltage, the pid_out going from the clamp limit (240) to what I assume to be a pid derived number. Depending on the input voltage, that number is anywhere from 0 - 240, however the range of input voltage is tiny and nowhere near the setpoint. See the code below:
Code:
0000- 240 000 240 035 240 036 240 035
0008- 240 036 240 036 240 037 240 035
0010- 240 038 240 036 240 037 240 036
0018- 240 037 240 038 240 036 240 038
0020- 240 036 240 036 240 037 240 036
0028- 240 035 240 037 240 037 240 036
0030- 240 036 240 035 240 037 240 036
0038- 240 036 240 036 240 037 240 035
0040- 240 037 240 035 240 036 240 035
0048- 240 037 240 036 240 037 240 035
0050- 240 037 240 036 240 037 240 036
0058- 240 037 240 035 240 036 240 035
0060- 240 038 240 035 240 038 240 035
0068- 240 036 240 037 240 038 240 039
0070- 240 037 240 037 240 035 240 037
0078- 240 036 240 037 240 036 240 036
0080- 240 035 240 036 240 036 240 036
0088- 240 038 240 038 240 035 240 036
0090- 240 038 240 035 240 036 240 036
0098- 240 037 240 037 240 037 240 035
00a0- 240 036 240 037 240 035 240 038
00a8- 240 036 240 036 240 036 240 036
00b0- 240 036 240 036 240 037 240 038
00b8- 240 036 240 037 240 036 240 037
00c0- 240 035 240 000 240 001 240 000
00c8- 240 001 240 004 240 007 240 009
00d0- 240 010 240 014 240 019 240 022
00d8- 240 025 240 027 240 029 240 034
00e0- 240 034 240 037 240 036 240 036
00e8- 240 036 240 036 240 036 240 037
00f0- 240 036 240 036 240 037 240 036
00f8- 240 037 240 035 240 036 240 036
Here is the code:
Code:
CLEAR
Define OSC 8
OSCCON=110000
DEFINE ADC_BITS 10
DEFINE ADC_CLOCK 3
DEFINE ADC_SAMPLEUS 50
DEFINE CCP1_REG PORTC 'Hpwm 1 pin port
DEFINE CCP1_BIT 2 'Hpwm 1 pin bit
CHANNEL1 var word
SETPOINT VAR WORD
SETPOINT = 480
INCLUDE "incPIDv1.5.pbp" 'Include the PID routine.
ADCON1 = 001011
ADCON2 = 001010
TRISA = 111111
TRISB = 000000
TRISC = 110001
CMCON = 7
UCON.3 = 0
UCFG.3 = 1
portc.1 = 0
alpha var word
CHANNEL14 var byte
PCBTEMP var word
CURRENT var word
LEDS var byte
POWER var byte
LEDS = 0
POWER = 0
pcbtemp = 0
CURRENT = 0
alpha = 0
channel1 = 0
channel14 = 0
'ADValue VAR WORD '<---This is your variable.
'Setpoint VAR WORD '<---This is your variable.
'Direction var PortB.7 '<---Direction bit to motor driver
'These variables are declared by the incPID routine but
'the user needs to assign values to them.
pid_Kp = $0100 'Set Kp to 7.0
pid_Ki = $0020 'Set Ki to 0.5
pid_Kd = $0010 'Set Kd to 2.14
pid_Ti = 8 'Update I-term every 8th call to PID
pid_I_Clamp = 25 'Clamp I-term to max ±100
pid_Out_Clamp = 240 'Clamp the final output to ±511
LOOP1:
portb.1 = 0
portb.2 = 0
portb.3 = 0
portb.4 = 0
portb.5 = 1
Gosub GetAD 'Get speed from tacho - NOT SHOWN HERE.
pid_Error = Setpoint - CHANNEL1 'Calculate the error
Gosub PID 'Result returned in pid_Drive
IF pid_Out.15 THEN pid_Out = 0 ' limit to positive values
pid_Out = ABS pid_Out 'Convert from two's comp. to absolute
HPWM 1, pid_Out, 30000 'Set PWM output
PORTB.0 = 1
PORTC.7 = 1
PORTC.6 = 1
PORTC.1 = 1
PORTA.6 = 1
PAUSE 10
GOTO LOOP1
GetAD:
ADCIN 0, CHANNEL1
alpha = alpha+1
'channel14 = channel1/4
WRITE alpha, pid_Out 'channel14
RETURN
Bookmarks