1. Quadrature Encoder Overview
Quadrature Encoders are a common type of incremental encoders that use two output channels (A and B) to sense position. Using two code tracks with sectors positioned 90° out of phase (Figure 1), the two output channels of the quadrature encoder indicate both position and direction of rotation. (For more information about the working of quadrature encoders refer to the related link, Quadrature Encoder Fundamentals)
Figure 1: Quadrature Encoder Output Channels A and B
The major advantage offered by incremental encoders (quadrature encoders are a type of incremental encoders) is their high resolution. Precision quadrature encoders are available that produce over 10,000 pulses per revolution at speeds exceeding 5000 RPM. Their high resolutions, extreme durability and ease of installation make Quadrature Encoders ideal for a wide variety of applications:
- Motor drive control systems use quadrature encoders to provide closed loop feedback for position and velocity controlled applications.
- Printers, fax machines and copy machines use quadrature encoders to synchronize various moving parts for trouble free operation.
- Elevators use quadrature encoders to maintain velocity and acceleration and for correct door alignment.
- Automated blood analyzers use quadrature encoders to ensure the exact position of vials containing blood samples while performing automated tests.
High speed and precision applications that utilize these quadrature encoders for velocity and acceleration control require hardware that can deterministically process quadrature encoder signals to estimate velocity and acceleration and instantaneously output controls signals at hardware-timed precision. (For more information about encoders refer to the related link, Using Quadrature Encoders with E Series DAQ Boards)
2. Velocity and Acceleration Calculations
There can be multiple ways to determine the angular velocity and acceleration of a Quadrature Encoder. In this tutorial we will develop an example that will count the number of Quadrature Encoder pulses in a fixed time interval to estimate the velocity and acceleration of the encoder, Figure 2 below demonstrates this procedure. This method is appropriate for high speed applications.
Figure 2: Velocity Estimation
Once the number of pulses in a fixed time interval is measured the angular velocity of the Quadrature Encoder can be calculated using the following formula:
Where, “Encoder Pulses” is the number of quadrature encoder pulses received in the Fixed Time Interval.
Acceleration is the rate of change of velocity. The following formula can be used to estimate acceleration of the Quadrature Encoder:
Where, the numerator divided by one Fixed Time Interval represents the change in velocity. Dividing the change in velocity by one Fixed Time Interval gives the acceleration.
3. CompactRIO Hardware Setup
The first step in the development of this application is to select an appropriate CompactRIO digital input module. The cRIO-9411 with six differential/TTL digital inputs and a maximum delay time of 500 ns, is ideal for high-speed quadrature encoder applications. Follow the wiring diagram below to connect the quadrature encoder to CompactRIO cRIO-9411.
Figure 3: Connecting a Quadrature Encoder to cRIO-9411
An external power supply of 5-24V can be connected to the Vsup line. This supply is required only if the external device, in this case the Quadrature Encoder, needs to be powered. The CompactRIO cRIO-9411 provides a clean, regulated 5 V power supply. For more information about cRIO-9411 refer to the CompactRIO cRIO-9411 Operating Instructions linked below.
For this example we will use a PXI-7811R for the FPGA platform and a PXI controller as the Real Time Host for the CompactRIO R Series expansion chassis. For a step by step guide on setting up the R Series Expansion hardware and a detailed tutorial on the basics of developing an FPGA vi refer to Getting Results with CompactRIO and LabVIEW linked below.
4. Application Development
Once all the appropriate hardware selections have been made we can start developing the LabVIEW application to acquire the quadrature encoder pulses and estimate velocity and acceleration of rotation. The acquisition and some initial calculations will be done on the FPGA then the collected data will be passed to the real-time Host to convert to engineering units.
LabVIEW FPGA STEPS
- Hardware configuration
- Calculate quadrature encoder position
- Calculate quadrature encoder velocity/acceleration
LabVIEW HOST STEPS
- Receive data from FPGA
- Convert to engineering units
LabVIEW FPGA Steps
1: Hardware configuration
Change the LabVIEW execution target to your FPGA device. This will launch the Embedded Project Manager. Start a new project and configure the CompactRIO hardware by going to Hardware>>Configure Target for CompactRIO. This will allow you to discover what CompactRIO I/O modules you have installed in the chassis. Now on the block diagram of the FPGA VI place the Digital Input function from the FPGA device I/O pallet. Double click the Digital Input vi to configure the channels to acquire from. Figure 4 below shows digital input channels DI0 and DI1 selected for Quadrature Encoder inputs A and B respectively. Notice that cRIO-9411 has been connected to Connector 1 of the R Series expansion chassis.
Figure 4: Digital Input Channel Setup
2: Calculate quadrature encoder position
In this example we will use X4 encoding for the quadrature encoder. This is a common type of encoding where each set of Quadrature Encoder pulses increments or decrements the position count by 4. Figure 5 shows an example of X4 encoding:
Figure 5: X4 Encoding Example
Build the block diagram as shown in Figure 6 to calculate the position of the Quadrature Encoder based on X4 encoding.
Figure 6: Position Estimation
In this code we use XOR between the present and the previous values of each channel to detect the change in state of the channel. If the state of either one of the channels has changed we would increment or decrement the position counter. To determine whether to increment or decrement the position we use an XOR between the previous state of Channel A and the present state of Channel B. If the previous state of Channel A is the same as the present state of Channel B then we increment the position count, otherwise we decrement it. By following these steps if the pulse on channel A leads Channel B the position count is incremented by 4, otherwise it is decremented by 4. Refer to Figure 7 below for an example of this procedure:
Figure 7: Example of Position Determination Code
3: Calculate quadrature encoder velocity/acceleration
Once we have the code to keep track of the position of the Quadrature Encoder we can estimate the velocity of the encoder by determining the change in position over a fixed interval of time. Acceleration can be estimated by keeping track of the change in velocity over the fixed interval. Figure 8 below shows the complete FPGA code with velocity and acceleration estimation:
Figure 8: Velocity and Acceleration Estimation
It is very important to select an appropriate time interval depending upon the expected velocity and counts per revolution of the quadrature encoder. Very small time periods can result in erratic changes in velocity and acceleration readings. For example, if the expected velocity for a quadrature encoder that produces 2000 counts/revolution is about 300rpm then a Fixed Time Interval of 100ms (100,000μs) is appropriate. If you observe large variations in velocity and acceleration consider increasing the Fixed Time Interval 1.
Once the FPGA VI is complete we complile the VI by pressing the run arrow. Now we can start developing the Real Time Host application.
LabVIEW Real-Time Steps
1: Receive data from FPGA
Switch execution target form the FPGA device to your Real Time Host and start a New VI. The first step in developing the Real Time Host application is to open a reference to the FPGA VI and pass the Fixed Time Interval information and receive the velocity and acceleration information from the FPGA VI.
On the block diagram place an Open FPGA VI Reference from the FPGA Interface pallet.
- Right click on Open FPGA VI Reference and click on “Select Target VI…” and select your FPGA VI in the window that pops up. You should observe the icon of your FPGA VI appear in the Open FPGA VI Reference.
- Again right click on Open FPGA VI Reference and select your FPGA target (in this case PXI-7811R) and enable the External VISA Input and the Open and Run options.
- Now right click on the VISA Resource Name input of Open FPGA Reference and create a control.
- Select the VISA resource name of your CompactRIO device in the VISA Resource Name control. Following these steps will open and run the FPGA VI, now we need to read and write data to the FPGA VI in a loop.
- Place the Read/Write Control from the FPGA Reference pallet in a while loop and connect the HW Exec Ref and error in to the HW Exec Ref Out and error out of the Open FPGA Reference.
- Right click on the Read/Write Control and create six controls, one for each input and output.
- Now close the FPGA reference.
Once you have followed all the steps above you should have a VI similar to the one shown in Figure 9 below:
Figure 9: Receive Velocity and Acceleration from the FPGA
2: Convert to engineering units
The last step in the development of this application is to convert the velocity from Counts/Interval to RPM and convert acceleration from Counts/Interval to rps/s. Figure 10 shows the final Host application.
Figure 10: Final Host Application
CompactRIO utilizes the power of FPGAs and the ease of use of NI LabVIEW to offer high performance along with productivity. CompactRIO is also extremely flexible; it gives you the ability to easily modify existing applications and keep up with future needs without hardware upgrades. The quadrate encoder example that was described in this document can be downloaded from the link below.
1If the Fixed Time Interval is small then there will be few pulses that are received from the quadrature encoder in that time period. For example, if we select a Fixed Time Interval of 1ms for a quadrature encoder that is rotating at 300rpm and producing 2000counts/revolution, we will receive 10 counts per time interval. A change of 1 count between interval results in a change in velocity of 10%, in this case we would observe velocity vary between 270 to 330rpm and the acceleration varying between -500 to 500rps/s.