Quadrature Encoder Interface (QEI) PDF

Title Quadrature Encoder Interface (QEI)
Author P. Ng
Course Micro-Controller Based Systems
Institution University of the West of England
Pages 7
File Size 332.2 KB
File Type PDF
Total Downloads 61
Total Views 139

Summary

Quadrature Encoder Interface (QEI)...


Description

7 7.1

Quadrature Encoder Interface (QEI) Introduction

In order to control the position or the velocity of a motor it is essential that we are able to measure its angular position or velocity. For example, this allows the actual velocity of the shaft (speed and direction of rotation) to be compared to the desired velocity. Similarly, in a position control application you would measure the position, compare it with the desired position. The error between the desired and actual velocities can then be used to set an appropriate drive signal to reduce that error, which is one of the underlying principles of a feedback control system. To measure the speed and direction of a rotating shaft we often use a quadrature encoder, also referred to as an incremental encoder, an optical encoder or, more generally, a shaft encoder. A quadrature optical encoder consists of a wheel with a particular pattern of holes cut into its periphery. This wheel it mounted to the rotating shaft to be measured, therefore, spinning at the same velocity as the shaft. A Light Emitting Diode (LED) and photo-transistors are aligned either side of the freely rotating wheel such that it runs between them as shown in figure 57. As a hole passes in front of a photo-transistor the light from the LED beneath will switch on that transistor. Consequently, between holes the light will again be obscured and the transistor will switch off.

Figure 57: Abstract diagram of the component parts of a simple quadrature optical encoder. The wheel spins between an LED and a photo-transistor with light from the LED switching on the photo-transistor when a hole is in alignment The time between a photo-transistor switching on, switching off and then switching on again is referred to as a cycle (as shown in figure 58) which, ultimately, is used to determine the rotational speed of the shaft that is spinning the encoder wheel. The resolution of the encoder, therefore, is set by the spacing between subsequent holes in the encoder wheel. For example, if there was only one hole cut into the wheel then the cycle time of the encoder would represent the time for the shaft to rotate 360◦ . By summing the number of cycles that occur in a fixed period of time, for example in one second, we can directly infer the speed 55

of the shaft in revolutions per second. By reducing the spacing between holes in the wheel we can measure the time that the shaft rotates through smaller angles, i.e., we increase the resolution of the encoder. However, in order to determine the velocity of the shaft we also need to measure the direction of shaft rotation. Herein lies the namesake of the “quadrature” encoder; by using 2 photo-transistors that are offset by 1 quarter of the cycle period, set by the spacing between holes in the wheel, we can observe the phase difference between the two measured cycle periods and thus determine the direction of rotation.

Figure 58: Signals generated by a quadrature encoder as the wheel is rotated in a forward (clockwise) direction and a reverse (anti-clockwise ) direction. The mechanism for this is demonstrated by inspection of the signals diagrams of figure 58. The two photo-transistors that are offset from each other are labelled as QEA and QEB, with the time trace for each channel plotted in the case of forward (top) and reverse (bottom) rotation of the wheel. During forward rotation, photo-transistor QEA switches on (goes low) before QEB, which we more formally describe as phase A leading phase B. The opposite is true when the wheel is rotating in the reverse direction, i.e., phase A lags phase B. In addition to the two photo-transistors required to measure speed and direction of rotation many quadrature encoders include a third photo-transistor (labelled as IND or index in figure 58) which is aligned to a different hole pattern cut into the wheel. Typically this pattern will consist of a single hole which can be used to count the number of rotations or for absolute positioning of the shaft during start-up.

7.2

QEI module on the dsPIC30f4011

The quadrature encoder interface module on the dsPIC30f4011 allows connection to each of the three signal sources provided by most quadrature encoders. The physical pins to which these connections are made can be found in the datasheet for each package, however the special function registers that must be

56

set in code are the same for all devices in the 30F family. A brief inspection of the block diagram of the QEI, shown in figure 59, reveals that many of its components are similar to both the timer and the PWM modules described in previous sections.

Figure 59: Block diagram of the components that make up the quadrature encoder interface on the dsPIC30f4011 The clock divider is equivalent to the clock pre-scalers that were used in other modules to set the temporal resolution of each system, however, in the QEI module this is used to set the pass band of the optional digital filters that can be applied to the inputs. The influence of noise in signals presented to the inputs of digital systems (as observed during the switch de-bounce exercises) can be reduced by filtering. Here, instead of having to write a software based filter for each of the input pins, a hardware solution has been provided for your convenience. The principle applied is that if a state transition measured on a

Figure 60: Schematic of the 3-stage digital filter provided as part of the QEI module

57

pin is not stable for 3 clock cycles then it is rejected. Therefore, the pass band for this simple filter is set by the frequency of the clock that drives the 3-stage flip-flop chain shown in figure 60. The maximum speed at which pulses coming in from the encoder can be measured by the QEI module of the dsPIC will be bounded by the clock frequency of the device. In the case of passing the non-filtered pulses through to the quadrature decode logic the minimum period between pulses that can be registered is 3*TCY .

7.3

Velocity measurement

As mentioned in the introduction to this section, in order to determine angular velocity of a shaft using a quadrature encoder the number of pulses, or cycles, must be counted during a fixed period of time. In the QEI module of the dsPIC30F4011 the counting is done for you with the accumulative figure stored in a special function register called POSCNT (see figure 59 for reference). The direction of shaft rotation is determined by the phase difference between the 2 channels A and B which again is done for you by the Quadrature Decoder Logic block of the QEI module. POSCNT will be incremented when a cycle is measured and phase A leads B, or decremented when a cycle is measured and phase A lags B. Therefore, in order to determine the instantaneous velocity in cycles per second Vcps (n), one first needs to read POSCNT, wait for a period of time (Tupdate )9 before reading POSCNT again and subtracting the second from the first reading. Vcps (n) =

P OSC NT (n) − P OSC NT (n − 1) Tupdate

(5)

However, since divisions take longer than multiplications it is advisable to replace the devision by Tupdate with a multiplication by fupdate , with fupdate = 1 . This results in the following equation, usabel in real time control apTupdate plications: Vcps (n) = (P OSC NT (n) − P OSC NT (n − 1)) · fupdate .

(6)

To transform this measure of velocity (cycles per second) to a measure of angular velocity we need to know what physical angle is represented by a cycle, or pulse, from the encoder. For example a 16 count encoder means that it has 16 holes in the wheel, therefore will generates 16 cycles per revolution, or each cycle represents a 2π radians rotation which we will call the base angular resolution 16 of the encoder ωbase . However, in the QEI module of the dsPIC there is the option of counting between the cycles, i.e., to count the negative as well as positive edges of the switching photo-transistor. This can also be extended to counting the edges of each channel (A and B) therefore giving the option of 2x or 4x the number of pulses counted for each cycle. This is set during the initialisation of the module using the special function registers shown in figure 61, and described in more detail in the family reference manual, so we will refer to this setting as the edge gain, kE . Finally, it is typical in most motor control applications to use a gearbox between the motor and the driven load to increase 9 Which

you can implement using a timer interrupt!!

58

torque. The gearing ratio of the gearbox (Gr) describes how the speed of the motor is reduced by the gearbox driving the load shaft. Therefore, by measuring the rotation of the motor shaft rather than the load shaft the encoder resolution of the load shaft is, effectively, further increased by that ratio. In summary the angular velocity Vω can be calculated as follows: Vω (n) = Vcps (n) ∗

ωbase kE ∗ Gr

(7)

Figure 61: Special function registers that are applicable to the QEI module [taken from dsPIC30f4011 data sheet]

7.4

QEI interrupts

In previous sections the use of a comparator to reset an accumulating counter has been described. In the QEI module the same circuit has been incorporated to set the maximum value to which the counter (POSCNT) can reach before resetting to zero and generating an interrupt. As the value of POSCNT can decrease

Figure 62: Interrupts can be generated when 2 conditions are met; a) when the rotation is in the forward direction the POSCNT will increase until it reaches MAXCNT; and b) when rotation is in reverse, POSCNT will decrease until it reaches zero, it will then be reset to MAXCNT as well as increase, depending on the direction of rotation, an interrupt is also generated if the value reaches zero in the negative direction as shown in figure 59

62. However, it is not indicated to the programmer whether the interrupt was generated by an over-run condition, i.e., POSCNT = MAXCNT, or an underrun condition whereby POSCNT = zero. This is important if you would like to maintain a measure of displacement of your robot that may extend beyond the range of the 16-bit value that can be represented in the POSCNT register. An example of a solution to solve this problem is by immediately checking the value of POSCNT in the interrupt service routine. If the value is high (i.e., greater than half of MAXCNT) then it is more than likely that the interrupt was generated by an under-run condition and vice versa. Once established, the range of the POSCNT register can be extended by incrementing or decrementing the 16th bit of (for example) a 32-bit container that also stores a copy of the POSCNT at each update interval. Alternatively, you could prevent an over- or under-run event occurring in the QEI by resetting the value of POSCNT to a known value at each update period. For example, by setting POSCNT to the value 32678, i.e., exactly half of the full 65535 range of the 16-bit register then an interrupt would only occur if 32678 cycles are measured during an update period. Care must then be taken in the design of the system such that the angular velocities and, therefore, the number of cycles that will be counted during an update period do not exceed this value.

7.5

Configuring the QEI

As with all the other hardware peripherals available on the dsPIC30f4011 the QEI is configured by setting bits in special function registers. All of these registers are described in the data sheet for the device and are defined in the "p30f4011.h" file using the same syntax for your convenience. Here follows example code for configuring a QEI module and accompanying interrupt service routine that should be written into its own function and stored as a separate file with accompanying header file. void init_QEI(void) { // configure analogue pins as digital channels ADPCFGbits.PCFG5 = 1; // QEB on pin 7 shared with AN5 ADPCFGbits.PCFG4 = 1; // QEA on pin 6 shared with AN4 // note: IND pin is not connected on firecracker board // Configure QEI module QEICONbits.QEISIDL = 1; QEICONbits.QEIM = 0b111; QEICONbits.SWPAB = 0; QEICONbits.PCDOUT = 0; QEICONbits.POSRES = 0; QEICONbits.TQCS = 0; DFLTCONbits.QEOUT = 0;

// // // // // // //

Discontinue operation when in idle mode x4 edge gain and reset POSCNT when == MAXCNT Do not swap the polarity of phase comparison Disable counter direction pin (normal I/O operation) index pulse does not reset POSCNT internal clock source (Tcy) disable digital filters

// set initial counter value and maximum range MAXCNT = 0xffff; // set the highest possible time out POSCNT = 0x7fff; // set POSCNT into middle of range

60

// Configure Interrupt controller IFS2bits.QEIIF = 0; // clear interrupt flag IEC2bits.QEIIE = 1; // enable QEI interrupt IPC10bits.QEIIP = 5; // set QEI interrupt priority } // interrupt service routine void __attribute__((interrupt, auto_psv)) _QEIInterrupt(void) { IFS2bits.QEIIF = 0; // clear interrupt flag if(POSCNT...


Similar Free PDFs