Absolute Encoders and NI Products
Overview
This document discusses how to connect an absolute encoder to a PCI/PXI-7344 FlexMotion board and how to read it from LabVIEW.
Table of Contents
Introduction
Rotary absolute encoders provide the absolute position of a rotating shaft upon power up. Controllers that use only an incremental encoder as feedback must first home the axis in order to restore position information. National Instruments FlexMotion controllers do not support the use of absolute encoders as feedback devices in a closed control loop. However, you can gain the benefit of an absolute encoder with the FlexMotion controllers.
See Also:
Linear and Rotary Encoders
Resolvers
Connecting a Digital Absolute Encoder to a FlexMotion PCI/PXI-7344 Controller
To connect a digital absolute encoder to a PCI/PXI-7344 FlexMotion controller, you must use the digital I/O lines on the 68-pin digital I/O connector. Since the I/O pins on this connector are grouped into 8-bit ports, encoders with more than 8 bits will span across more than one port. For example, an encoder with 13 bits could be connected as follows:
|
Encoder Bit
|
Connector Bit
|
Connector Pin #
|
|
0
|
Port 1 bit 0
|
10
|
|
1
|
Port 1 bit 1
|
44
|
|
2
|
Port 1 bit 2
|
45
|
|
3
|
Port 1 bit 3
|
12
|
|
4
|
Port 1 bit 4
|
13
|
|
5
|
Port 1 bit 5
|
47
|
|
6
|
Port 1 bit 6
|
48
|
|
7
|
Port 1 bit 7
|
15
|
|
8
|
Port 2 bit 0
|
16
|
|
9
|
Port 2 bit 1
|
17
|
|
10
|
Port 2 bit 2
|
51
|
|
11
|
Port 2 bit 3
|
52
|
|
12
|
Port 2 bit 4
|
53
|
The remaining bits of port two (pins 54, 21, 22) could either be connected to digital ground, or the values of these bits could be manually zeroed in software. You must also connect an incremental encoder to the motion I/O connector to provide position feedback if you are implementing closed loop control. Then, mechanically couple both encoders to the shaft being controlled. Apply power to the system and initialize the FlexMotion controller according to the motor type (servo or closed loop stepper). Set up the digital I/O lines as inputs using the flex_set_port_direction function, and use the flex_set_port_pol function to set all of the bits of both ports to non-inverting. Some encoders provide data that is negative in its logic. In this case, a logic 1 would be represented by 0 volts and a logic 0 would be represented by 5 volts. You could remedy this by using the flex_set_port_pol function to set all of the bits to inverting.
Considerations When Using an Absolute Encoder
The basic purpose of the absolute encoder is to provide absolute position data at periodic intervals. The data from the encoder would be read and used to update the position data in the encoder position register. The FlexMotion products group the digital I/O lines into ports of 8 bits each. Therefore, you can only read/write data on these lines in 8-bit chunks. If the encoder data is greater than 8 bits, you will need to execute multiple reads to get all of the data. If the data on these lines is changing when the reads are made, it may not be coherent. In other words, the data from the encoder may have changed between the time the bytes were read, resulting in garbled data. One way to work around this is to make sure the axis is stopped when data from the encoder is read. This would correct the problem of coherency assuming the data from the encoder is stable when the axis is stopped. If the data from the encoder is not stable when the axis is stopped, the problem would still potentially exist.
There could be several reasons for unstable data from a stopped axis. One reason could be that the axis is vibrating due to other machinery. If this vibration were of great enough magnitude, the shaft could be rotated 1 bit. This might be a small displacement but it could have significant consequences. For example, assume that you have a 12-bit encoder on a shaft stopped at the position where the encoder reads x0FF. Also assume that for some reason (noise, vibration, etc.) there is a periodic displacement of 1 bit value. This means that the data presented to the I/O lines alternates from x00FF to x0100. The lower 8 bits alternate from xFF to x00 and the upper 8 bits alternate from x00 for x01. The actual data obtained from the two port reads would have the following four possibilities: x00FF, x0000, x0100, and x01FF. Obviously, only two of these positions can be considered correct. The other two would represent a significant error from the actual position. One way to reduce this effect would be to use an encoder that presents its data in gray code. Gray code is similar to binary except that only 1 bit is allowed to change from one number and the two adjacent numbers. Many absolute encoders offer this as an option. Using gray code, vibration that displaces the shaft 1 bit position would cause a change in only 1 bit of the data presented to the I/O lines. For the example above, this would mean the data would be bouncing back and forth from x180 to x080. In this case, there would only be two possibilities for the data read and they would both be considered correct. This approach is less effective if the vibration amplitude is larger than 1 bit.
For cases where noise in the data is larger than 1 bit, you must use alternate means to ensure the coherency of the data reads. Many absolute encoders provide a means of querying the encoder for the data. Typically, a signal is strobed and the data is presented at the outputs. If this signal were of a type that latched the data from the encoder, this would ensure the coherency of the data provided the signal is not strobed between reads. If this is not available, you could design an external data register that could latch the data from the encoder when a signal line is toggled. This signal could also be controlled by a digital I/O line.
Using the Encoder in LabView
Using the data from the encoder is a simple process of getting the data from the digital I/O ports, converting to the proper binary format, then using the data to update the position register. Figure 1 shows a simple diagram that accomplishes this task.

Figure 1. Reading data from an absolute encoder.
In this VI, the data from a 16-bit encoder is read from ports 1 and 2. The data from the encoder is natural binary. The data connected to port 1 represents the lower byte of the word and the data connected to port 2 represents the upper byte. The VIs that read the digital ports return the data in the format of a cluster of booleans. The data is first converted to an array of Boolean values and then to a number. These numbers are formatted to unsigned 8-bit and then concatenated to form a single 16-bit number. In this example, the data is interpreted as a signed 16-bit number. This implies that we are using the encoder's 0 position as a midpoint between two limits of travel. We could also use the 0 position as one limit and the 65535 position as the other limit. Since the position registers are 32-bit signed numbers, 16-bit number must be reformatted to 32 bits. In this example, this reformatting is accomplished by first checking to see if the 16-bit number is negative. If not, then it is simply passed directly into the position registers with 0s in all of the bits above 16 bits. If it is negative, then all of the bits above 16 are set to 1s before being put into the position registers.
For an encoder that produces gray code, the procedure is very similar. Figure 2 shows a diagram for this case.

Figure 2. Reading data from a gray code absolute encoder.
This diagram is almost identical to the previous one. However, there is the addition of an extra VI. This VI simply takes the 16-bit gray code and converts it to 32-bit binary. In this case, the straight binary value is placed into the position registers directly.
Additional Considerations
The diagrams above both assume that the absolute encoder and the incremental encoder have the same resolution -- they both have the same number of counts per revolution. This is rarely the case. To properly coordinate between the absolute encoder and the incremental encoder, you must account for the difference in resolution. Initially, you could simply multiply the reading from the absolute encoder by a constant K, where K equals the ratio of the incremental encoder resolution to the absolute encoder resolution. However, this would result in a quantization error equal to that of the absolute encoder. This error could be reduced down to that of the incremental encoder in a number of ways. One way to minimize the quantization error of the absolute encoder would be to first take a reading from both the absolute encoder and the incremental position registers on the FlexMotion controller. Next, make a succession of single count moves while reading the absolute encoder between each move. When the absolute encoder shows a single bit change in position, the two encoders are aligned. The position read from the absolute encoder can now be scaled up by K to get the correct incremental position. This method is simple and effective. However, it could have a significant disadvantage in that it could take as many as K single count moves to minimize the error. This might be acceptable for low values of K, but if K is reasonably large, it could be too time consuming to be a viable option.
Another method would be to make a series of moves in a binary search pattern. For example, you first take a reading from both the incremental register and the absolute encoder. Next, move the axis a number of incremental counts equal to K/2. Now, read the absolute encoder position. If the absolute encoder value has not changed, then make another move in the same direction a distance of K/4 incremental counts. If the reading has changed after this move, then the next move will still be K/4 incremental counts, but it will be in the direction opposite to the first. This is repeated until the number of moves made is such that it satisfies the following relation:
Where n is the number of moves. After the moves are complete, take another reading of the absolute encoder. If the current reading is the same as the very first reading, a single count move is needed. This move should be in the forward direction. If the numbers are not the same, then you are finished. The encoders’ quantization error bins are aligned.
With this method, the number of moves is guaranteed to be the power of two that equals K. However, it is a much more complicated algorithm than the simple step method. As a comparison, if K is 8, the first method would require no more than 8 single count moves to minimize the error. Assuming a uniformly distributed random placing of the error, you could estimate the average number of moves to be 4. With the binary search algorithm, you will know exactly where you are with 3 moves. This savings of one move may not justify the complicated algorithm. But, if the K ratio were more like 16, you could estimate an average of 8 moves to minimize the error with the simple method whereas the binary search method will take only 4.
Reader Comments | Submit a comment »
Legal
This tutorial (this "tutorial") was developed by National Instruments ("NI"). Although technical support of this tutorial may be made available by National Instruments, the content in this tutorial may not be completely tested and verified, and NI does not guarantee its quality in any way or that NI will continue to support this content with each new revision of related products and drivers. THIS TUTORIAL IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND AND SUBJECT TO CERTAIN RESTRICTIONS AS MORE SPECIFICALLY SET FORTH IN NI.COM'S TERMS OF USE (http://ni.com/legal/termsofuse/unitedstates/us/).
