PDA

View Full Version : Quadrature encoder and loop count



phoenix_1
- 7th October 2009, 18:57
After much our spend it work in interrupt's like Henrik suggest me and I am nearly finish.
Need only one sugget more from peoples here.
Next problem or maybe not :?
QEI counter count in circles up/down for example 0-1999 and oposite 1999-0...in 4X mode.
Now when I am at position 100 for example and command new position - 100 pulses motor will go to 0 and stop - OK but if maybe inertia of shaft move QEI only one step down then here is problem Setpoint is on position 0 value but QEI is 1999!
I am little confused on how to set relation between Setpoint and QEI register of position ?
I want to move motor to any direction with PID without limit of numbers of turn to+ or minus.
Maybe I ned dead zero like minus limit /?
Any suggest.
Regards Robert:)

phoenix_1
- 8th October 2009, 23:02
Here is QEI module count set to count from 0 - 999 .
x is number of cyrcle's of QEI module up and down.
QEI variable is final counter..etc


loop:
z = 0
PosHigh = POSCNTH 'Get high byte of counter
PosLow = POSCNTL 'Get low byte of counter
PosTemp = POSCNTL 'Get low byte again
If PosLow - PosTemp = 0 then Goto Done 'Compare, if equal we're done.
PosHigh = POSCNTH 'If not, get values again.
PosLow = POSCNTL
z = 1
Done:
Position = POSHIGH * 256 + PosLow 'Put high and lowbyte together.
if position > 1000 then position = 1000
if position < 1 then position = 0
h = QEICON.5 'QEICON.5 for right = 1 for left = 0
if PIR3.2 = 1 and h =1 then x = x+ 1
if x <> 0 and PIR3.2 = 1 and h =0 then x = x- 1
PIR3.2 = 0 'PIR3.2 maxcount transition down or up was detected
h = 0
QEI = (1000 * x) + position ' increment total counter
if z = 1 and h = 0 then QEI = QEI -(1000-position) ' dectement total counter
if h = 1 and QEI = 0 then
QEI = 65535
endif
if QEI > 65535 then
x = 0
QEI = 0
endif
if porta.0 = 0 then setpoint = setpoint + 1
if setpoint > 65535 then
setpoint = 0 and QEI = 0 and x = 0
endif

Butwhen I count to minus after QEI variable com to 0 if I move shaft of motor more to CCW real QEI conter will continue 999,998 and if my setpoint stay at 0 I am in big problem...
Any suggest please
Regards Robert