Rotational position is often measured with potentiometers. The devices typically are low cost and provide reasonable accuracy (typ. 1%) but they have limited ranges of rotation. Encoders are more expensive devices that allow unlimited ranges of motion. An optical encoder is shown in Figure 24.1 An optical encoder. In this device two pairs of optical emitters and detectors are directed through a disk with openings. By counting the number of times the light beam is broken the distance of rotation is measured. Two light beams are used to detect the direction of rotation.
A very low resolution encoder disk is shown in Figure 24.2 Encoder disk. The dashed line indicates the position where the light beams cut across the disk. As the disk rotates the beams are broken at staggered intervals.
Optical encoders can operate at high speeds with a longer life because there is no mechanical contact. Mechanical encoders use a rotating disk with metal rings in patterns similar to those in optical encoders. As the disk rotates mechanical contacts are opened and closed. These mechanical contacts are prone to ’bouncing’ as they make and break contact, and hence they do not work well for higher rotational speeds. Typically the contacts bounce time is less that 10ms. Although limited in speed, mechanical encoders are popular because they generally cost less than similar optical encoders.
An quadrature input pattern from an encoder is shown in greater detail in Figure 24.3 Quadrature Encoders. If we were to simply count the rising edges on one of the inputs we would be able to measure a total distance covered. However we must also use the second phase to determine the direction of motion. This method still uses one of the phases for the count. When a positive edge is encountered the counter is incremented if the other phase is true. However, if the other phase is false then the counter is decremented.
The previous method is simple to implement, but less accurate. A more accurate method looks at the current and previous state of both quadrature inputs. Consider the example in Figure 24.4 Quadrature Encoders as binary inputs where the two phases are binary inputs. If the last input value was 00 and an updated value is 10 then the motion is clockwise, if the updated value is 01 then the motion is counterclockwise.
A program to read position and velocity input from an encoder is given in Figure 24.5 Subroutines for reading position using an encoder. The program uses pins 0 and 1 on port B as encoder inputs. In this case the program counts upon both the positive and negative edges of both phases. This is done by comparing the current encoder position to the previous encoder position. When the inputs change the encoder value is either incremented or decremented, depending upon the preious values.
A position control system is shown in Figure 24.6 Position control system. This system uses the H-bridge from the previous laboratory to drive a motor, which in turn drives an encoder used for position feedback.
The block diagram for the control system is shown in Figure 24.7 Position control feedback loop. This control system will use a desired position for the system, in terms of the encoder position. An interrupt driven subroutine will be used to implement the feedback controller. It will calculate the system error by subtracting the encoder counter from the desired position. This will then be passed through a proportional and integral function. The result will be corrected for the deadband and output to control the motor as a PWM signal.
All of the functions in the control system have been implemented in previous laboratories, except for the integration term. To integrate in a program we will need to add new values to previous values, using a known time step, as shown in Figure 24.8 Numerical integration. A subroutine to implement the proportional-integral controller is shown in Figure 24.9 Proportional integral controller. The integration subroutine also includes a crude limiting function to prevent the integral value from becoming excessively large.
Please note that the interrupt subroutines given in a previous lab use the same timer (1) as the PWM output. As a result new version using counter 0 is given in Figure 24.10 Interrupt subroutine using counter 0.
1. Connect the control system and the motor using an L293D to drive the motor and an encoder as shown in Figure 24.11 Connecting the Encoder to the ATMega or Figure 24.12 Connecting a mechanical encoder to the ATMega32. Note the motor used in this lab may be a 3V motor, so the power supply voltage may differ from previous labs.
2. Test the program developed for the prelab. (Note: Remember to build and test in pieces whenever possible.) Determine all necessary parameters, such as the deadband limits. Note: if the motor positive and encoder positive directions are opposite, then the motor will rotate forever. This problem can be solved by reversing the motor leads.
5. Supply different set points of motor positions and measure the steady state position. Also observe the response speed. After the motor settles, attempt disturbing the motor position (gently) and record your observations.