CAD CAM EDM DRO - Yahoo Group Archive

Using an Encoder to Generate a Tach (Velocity) Value

Posted by Matt Shaver
on 1999-09-18 23:02:59 UTC
OK, how about some pseudo code:

/*
This program assumes that the quadrature encoder has been
decoded in hardware to a step and direction interface.
(US Digital has a little chip for this)
Inputs: boolean step_bit, boolean dir_bit
Outputs: integer velocity, boolean dir_polarity
*/

init
;assume we are not moving
velocity = 0
;store in RAM, write a DAC, or send to another CPU
output velocity

enter
;store the initial polarity of dir_bit in dir_polarity
input dir_polarity, dir_bit
;send the direction value to where it will be used
output dir_polarity
;store the initial polarity of step_bit in step_polarity
input step_polarity, step_bit

main
;***
;we could call a deterministic, but non
;real-time procedure here, but don't
;waste too much time or you'll lose counts!
;***
;wait for the encoder to move
if step_bit = step_polarity then goto main
;update the step polarity
complement step_polarity

motion
;OK, we're moving!
;Are we going the same direction?
;If not start over.
if dir_bit != dir_polarity then goto enter

loop
;***
;we could call a deterministic, but non
;real-time procedure here, but don't
;waste too much time or you'll lose counts!
;***
;if velocity = 0 this will underflow to the max value
decrement velocity
;if we underflow again, bail out
if velocity = 0 goto init
;watch for the next transition
if step_bit = step_polarity then goto loop
;update the step polarity
complement step_polarity
;store in RAM, write a DAC, or send to another CPU
output velocity
;start over
velocity = 0
goto motion

I'm thinking Microchip PIC for the CPU as I have experience writing programs
for it, plus it's a fast RISC chip.

Thoughts?

Matt

Discussion Thread

Matt Shaver 1999-09-18 23:02:59 UTC Using an Encoder to Generate a Tach (Velocity) Value