pid routine for h-bridge
first of all, a big thanks to mr henrik for this wonderful code. it works, as i have it all set up right here.
but instead of locked antiphase, im trying to implement a h-bridge powered by the two hpwm (forward/reverse motion)on a 18f4431.
i have a lcd displaying variables like position, setpoint, dutycycle1(for hpwm0) and dutycycle2(hpwm1)
Main: ; main loop
LCDOUT $FE, $80, "POS=" ,DEC5 POSITION, " SP=" ,DEC5 SETPOINT
LCDOUT $FE, $C0, "DUTY1=" ,DEC3 DUTY1, " DUTY2=" ,DEC3 DUTY2
IF PORTB.3 = 0 then SETPOINT = SETPOINT + 1 ; Increment setpoint
select case setpoint ; Circular count for Setpoint condition
case is > 65535 ; Circular count for Setpoint condition
setpoint = 0 ; reset setpoint
POSCNTH=0 ; set counter for encoder, H bit ; reset QEI counter high byte
POSCNTL=0 ; set counter for encoder, L bit ; reset QEI counter low byte
end select
;SELECT CASE PID_ERROR
IF POSITION<SETPOINT THEN ; End of Circular count for Setpoint condition
pid_Error = setpoint - position
;GOSUB FORWARD
ENDIF
IF POSITION>SETPOINT THEN
PID_ERROR = POSITION - SETPOINT
endif
gosub pid ; go to PID rutine
DIRECTION = PID_OUT.15
select case direction ; condition determine PWM
case 0 ; condition determine PWM
Duty1 = abs pid_out
GOSUB FORWARD
;DUTY1 = 0 ; condition determine PWM
case 1 ; condition determine PWM
Duty2 = ABS pid_Out
GOSUB BACKWARD
;DUTY2 = 0
END SELECT
goto Main ; do main loop
sub forward turns on first hpwm and sub backward turns on second hpwm.
here is the problem: if i increment/add to the setpoint, both duty cycles reset when position rolls over from 65535 to 0. i know its something to do with my integer math, and ive spent a whole week on this issue. im trying to implement a step/dir routine, for my cnc machine.
any suggestion will be highly appreciated. thanks
NAG CON WIFE!
WIFE VAR MOOD
Bookmarks