Skip to content

Servo Drive Platform FPGA Architecture QEP

jdannynewman edited this page Dec 17, 2020 · 4 revisions

Encoder IF for the FPGA

Features of the QEP (Quadrature Encoder Pulse)

The QEP IP in the Intel Cyclone V FPGA has the following features:

  • Quadrature encoder for 2-bit grey code​
  • Position capture by index pulse​
  • Minimum pulse filter on all input signals​
  • Clock-domain synchronization on all input signals​
  • Synchronization to PWM possible​
  • Position measurement​
    • With and without synchronization to external trigger​
  • N-by-1 speed measurement​
    • With and without synchronization to external trigger​
  • N-by-N speed measurement​
    • With and without synchronization to external trigger​
  • Configurable clockwise/counter-clockwise rotation​
  • Configurable position roll-over​
  • Position direction detection​

QEP Implementation

Incremental encoders continue to be a very popular position sensor​. The purpose of this block is to decode a 2-bit grey code from an incremental encoder into a rotor position and speed measurement​. The block also acts as an interface for 3 hall elements (absolute position).




QEP IP Block Diagram

Here is a functional block diagram of the QEP IP implementation in the Intel FPGA:


QEP IP I/O

Signal Direction I/O Standard Description
A input 3.3V CMOS A line from encoder. Route to external pin
B input 3.3V CMOS B line from encoder. Route to external pin
Z input 3.3V CMOS Index line from encoder. Route to external pin
HALL[2:0] input 3.3V CMOS Hall signal input. 3-bit absolute position
sync_strobe[2:0] input n/a Synchronization-pulse input. Typically driven by PWM sync

Registers

Register R/W Bits Description
QEP_RESET W 1 QEP module reset. Assert->deassert LSB of register before using module
QEP_ENABLE W 1 QEP module enable. LSB of register must be asserted before using module
QEP_CONFIG W 7 Bit [0] A line active level. 0 = active-high A pulse. 1 = active-low A pulse
Bit [1] B line active level. 0 = active-high B pulse. 1 = active-low B pulse
Bit [2] Z line active level. 0 = active-high index pulse. 1 = active low index pulse.
Bit [3] Count direction. 0 = Positive count direction. 1 = Negative count direction
Bit [5:4] index mode. 01 = Capture on rising edge. 10 = Capture on falling edge
Bit [6] Sync enable. 0 = Disable sync to external pulse. 1= Enable sync to external pulse
QEP_MPW_CNT W 32 Sets the minimum pulse width to be ignored
t_mpw=QEP_MPW_CNT/PL_CLK PL_CLK is 100Mhz in current configuration
QEP_CNT_WRAP W 32 Counter wrap-around value
QEP_CNT R 32 Current counter value.​ Updated at every A/B count event
QEP_CNT_IDX_LATCH R 32 Counter value captured at index pulse.​ Updated at active edge of index pulse
QEP_CNT_STROBE_LATCH R 32 Counter value captured at external sync pulse
Updated each time external sync pulse is asserted
QEP_HALL R 3 Filtered hall states: [Hall 2, Hall 1, Hall 0]
QEP_M W 32 Number of A/B count events between capture of N/M speed measurement
For best results, set M to 2^i with i=1,2,3…
QEP_N_BY_M R 32 Number (N) of PL_CLKs between M A/B count events​
N is number of PL_CLKs since last capture
Low-noise speed measurement, especially effective at higher speed
QEP_N_BY_1  R 32 Number (N) of PL_CLKs between two A/B count events​
N is number of PL_CLKs since last capture (note 1)
QEP_N_BY_M_STROBE_LATCH R 32 The value of QEP_N_BY_M captured at external sync pulse
QEP_N_BY_1_STROBE_LATCH R 32 The value of QEP_N_BY_1 captured at external sync pulse
QEP_PER_CNT_N_BY_M R 32 Number (N) of PL_CLKs since last capture of M A/B count events​ (note 2)
QEP_PER_CNT_N_BY_1 R 32 Number (N) of PL_CLKs since last capture of A/B count events​ (note 2)
QEP_DIRECTION R 1 Actual count direction
0 = negative count direction
1 = positive count direction
QEP_DIRECTION_STROBE_LATCH R 1 The value of QEP_DIRECTION captured at external sync pulse
note 1

With poor encoders, this speed measurement tends to be noisy but signal can be helpful at low speed as it is updated more often than QEP_N_BY_M (M times more often).

note 2

This time measurement is useful at very low speed when there is none or only a few A/B count per control period.


N/M Speed Measurement

Quite often the pulse train from an encoder is far from ideal. The signals do not have 50% duty cycle and often the lines are skewed. This leads to a noisy speed measurement.​

While the A/B signals are distorted, there is periodicity in the signal. If the speed measurement is done over multiple count events the signal is actually very reliable. ​The best approach is to do speed measurement over 4,8,16… count events. ​ Over 4 count events, most encoder inaccuracies even out.​ To measure speed over multiple count events, a frequency divided version of QEP_N_BY_1 can be read in register QEP_N_BY_M


Synchronization of QEP to PWM

In real time control it is essential to capture all feedback signals at the same instant. ​ Feedback is

  • current
  • position
  • speed
  • and voltages​

To make it possible to capture QEP registers at the same time as other feedback, the module accepts an external trigger, sync_strobe.​ At the trigger, position, all speed measurements and direction of rotation is captured. Typically the registers are read in an ISR (Interrupt Service Routine) at some point after the trigger.

Direction Detection

The register QEP_DIRECTION indicates direction of rotation of the motor​. Essential information is at speeds close to zero​. The instantaneous direction can be read in _QEP_DIRECTION_​. The direction at the last sync_strobe can be read in QEP_DIRECTION_STROBE_LATCH



Noisy Encoder Signals

The pictures below show noisy encoder signals in a motor-control application. The turquoise signals are input to the QEP​. The spikes occurs when the PWM inverter switches​.

On the left-hand example one false spike is induced when the inverter switches.​
On the right-hand example multiple false spikes happen shortly after each other.​



Debounce vs. Minimum Pulse Filter

The common approach to filtering noisy encoder signals is a debounce filter. However, this encoder module uses a minimum-pulse filter which is more effective for the kind of noise found in a motor-control application​.

Operation of debounce filter​

When there is a transition on the signal, a counter is started. When the counter expires (tfilter) the level of the signal is checked and if there has been a transition, the filter output is changed. Any noise spikes within the window tfilter are ignored​

Operation of minimum pulse filter​

When there is a transition on the signal a counter is started. If the input signal changes back to the original level before the counter expires (tfilter) the counter is reset. This goes on until the counter is allowed to expire (tfilter) and at this point the filter output is changed. Any noise spikes shorter than tfilter are ignored.​

Three types of noise scenarios are shown in the figure below. The 1st two are handled slightly different by the two filters but both results are acceptable. The only difference lies in how the filters phase shift the signals. Note that for speed measurement phase shift at this scale (us) does not matter.​

The 3rd noise scenario is handled fundamentally differently by the two filters. ​

  • Debounce filter: The counter is started at the first falling edge of the noise. When the timer expirers, the input signal is low and hence the filter output is also changed to low. However, shortly after, the input signal changes back to high and the counter starts again. When the counter expires the input signal is high so the output is also set high. The desired behavior is to ignore the narrow pulses but since the filter is not reset at every noise edge the output becomes unreliable​

  • Minimum pulse filter: The counter is started at the first falling edge, but since there are new noise transitions before the counter expires, the counter is reset and the process starts over. This means any pulse narrower than tfilter is ignored and the filter output remains at the correct level.​



The figure below shows how the filters handle the noise scenario from the above discussion. Count event is the signals used to measure speed​.

Note how Count event has multiple incorrect transitions with the Debounce Filter and consequently the speed measurement is highly distorted. The Minimum Pulse Filter handles the situation correctly​.





Back to FPGA Architecture home

Clone this wiki locally