You are on page 1of 5

Document Type: Tutorial NI Supported: Yes Publish Date: Feb 03, 2012

Quadrature Encoder Velocity and Acceleration Estimation with CompactRIO and LabVIEW FPGA
Overview Reconfigurable I/O (RIO) technology gives you the ability to define FPGA logic for high levels of performance and reliability. This tutorial demonstrates the steps required to develop a LabVIEW application, using LabVIEW FPGA and LabVIEW Real-Time, that will calculate the velocity and acceleration of rotation of a Quadrature Encoder.

Table of Contents 1. 2. 3. 4. 5. 6. Quadrature Encoder Overview Velocity and Acceleration Calculations CompactRIO Hardware Setup Application Development Conclusion Quadrature Encoder Velocity and Acceleration with LabVIEW FPGA Example

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

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

1/5

www.ni.com

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. 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. 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 1. Hardware configuration 2. Calculate quadrature encoder position 3. Calculate quadrature encoder velocity/acceleration

LabVIEW HOST STEPS 1. Receive data from FPGA 2. Convert to engineering units

LabVIEW FPGA Steps 1: Hardware configuration

2/5

www.ni.com

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:

3/5

www.ni.com

Figure 8: Velocity and Acceleration Estimation

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.

4/5

www.ni.com

Figure 10: Final Host Application Conclusion 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. Note:
1

If 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. Quadrature Encoder Velocity and Acceleration with LabVIEW FPGA Example

Quadrature Encoder Velocity and Acceleration with LabVIEW FPGA

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/).

5/5

www.ni.com