Processing Command

Module QDECM :: QDECODE

Command to define a task that counts up-down pulses from a quadrature encoder.

Syntax

QDECODE( PPORT, BITS, VOUT )



Parameters

PPORT
Pipe containing captured samples of digital port state
WORD PIPE
BITS
Address of signal pair in digital port
WORD CONSTANT
VOUT
Variable where current decoded count is posted
LONG VARIABLE

Description

The QDECODE command is useful for monitoring the angular position of a rotating mechanism having an attached quadrature encoder. Samples are captured from the digital port and passed through pipe PPORT, selecting the bits indicated by the BITS parameter. Based on an analysis of the sequence of digital transitions, positive and negative counts of rotation are accumulated, and the current sum is posted to shared variable VOUT.

QDECODE is intended for applications in which rotating speeds and ranges of motion are relatively small. An example would be monitoring the position of a manually-operated control wheel. Encoder applications with high speed rotating equipment or multiple channels should use a hardware-based implementation such as the Microstar Laboratories Q-D Decoder Board, MSXB050.

The two quadrature signals are TTL digital signals connected to adjacent pins on the Data Acquisition Processor digital port. At a constant rotation speed, the encoder device produces square wave outputs on the two digital lines, with the transitions of one signal exactly in the center of the squared pulses from the other signal. Or, in other words, the two signals are "in quadrature" at 90 degrees phase shift. Transitions never occur simultaneously on both lines. The two digital signals must be adjacent bits on the digital port: bit positions 0-1, or bit positions 2-3, etc. Specify the position of the lower bit of the pair as the BITS parameter.

One digital signal alone would be enough to indicate that the device is rotating, but not the direction. Using the two signals in combination, it is possible to identify both the step and its direction. A running sum of the position steps, up or down, can then be accumulated, so the count corresponds to the current angular position.

To be sure that every pulse is accurately detected, it must be guaranteed that at the maximum possible rotation speed the sampling rate on the digital signals is more than twice as fast as the rate that transitions occur. This rate is often faster than necessary for other signals monitored by the DAP, so appropriate configuration of the input channel list or decimation processing can be used to reduce data rates on other channels. For example, the following input configuration samples the quadrature decoder digital signals at four times the rate of data capture in two analog channels.

  Idefine  An2QD
    channels 8
    set IP0  b0      // digital
    set IP1  g
    set IP2  b0      // digital
    set IP3  d0          // ANALOG 0
    set IP4  b0      // digital
    set IP5  g
    set IP6  b0      // digital
    set IP7  d1          // ANALOG 1
    time  2.4
  end

Counting up or down requires detection of transitions in the digital signals. Because the state of the digital port is sampled, a transition is detected by observing when a sample is different from the previous observation. This leads to the following simple state machine scheme for identifying steps of rotation.

State New input pattern Counting action
State 1
Previously L - L
L - L
L - H
H - L
0, stay state 1
+ 1, new state 2
- 1, new state 4
State 2
Previously L - H
L - H
L - L
H - H
0, stay state 2
- 1, new state 1
+ 1, new state 3
State 3
Previously H - H
H - H
H - L
L - H
0, stay state 3
+ 1, new state 4
- 1, new state 2
State 4
Previously L - H
H - L
L - L
H - H
0, stay state 4
+ 1, new state 1
- 1, new state 3

The first sample determines the starting state and initializes the counter to zero. After that, the next observation of the digital port determines the new state and the action to adjust the sum: plus 1, minus 1, or no change. The current accumulated count is posted to shared variable VOUT where other tasks can access it.

Example

  QDECODE(IP(0,2,4,6), 10, VQDACCUM)

Using the input sampling configuration shown in the description text above, combine the samples from the digital port into a single stream of observations. Observe bit positions 10 and 11 for determining state. Depending on the observed state of the bits, appropriately update the count in shared variable VQDACCUM and maintain the decoding state internally.