Robotics/Feedback Sensors/Encoders
It's not at all uncommon to require information concerning rotation on a robot, whether from arm or manipulator axis motion to drive wheel speed. Rotary encoders can be found in several varieties, most commonly mechanical or using photodetectors. In mechanical encoders, electrical contacts periodically connect, usually to drive a terminal high or low. These electrical pulses are then used for information. Photodetector encoders can be made smaller and operate much quicker. Typically, a reflective sensor will shine on a marked reflective surface, or a photointerruptor will shine through a disc with transparent and opaque sections, such that the amplitude of received light causes high and low electrical pulses in the receiver. These pulses are then decoded by external circuitry.
Contents
Encoding[edit]
Depending on the type and reliability of information needed, the patterning on the rotary encoder will be increasingly complex. If only rotational speed is required, a simple alternating pattern will do, but if both speed and direction are needed, more encoded information is required. Encoding position will inherently yield information regarding velocity and direction, as comparing the direction of change in position will give direction of rotation, where the change in position with respect to time will (by definition) indicate speed. The specific type of position encoding will depend on the precision required, as well as the reliability: how much of an error in measured rotational position due to an error in reading the encoded information, is acceptable in the application.
Measuring Rotational Speed[edit]
For some applications, only rotational speed needs to be considered. If a toothed wheel which is mechanically limited to spinning a single direction is being measured, the direction is already known, and only the velocity remains. Perhaps a feedback system is being developed for drive speed on a robot, and an encoder on the axle will enable the controller to calibrate speed by varying PWM values; if the controller enables the motor to spin in a specific direction, all it requires is the rotational speed. All that is necessary to measure speed is a single alternating pattern causing the detector to emit a pulsetrain. The pulse rate will be directly proportional to the rotational speed; measuring the time between consecutive rising (or falling) edges and dividing that into the angle of rotation represented by a pulse will yield the rate of rotation.
If the rate is changing during the time of the measurement, the calculated rate will be an average approximating the instantaneous rate. This is where precision comes into play – the more pulses per rotation, the shorter the time between measurements, and the closer the approximation is to correct. Anyone familiar with calculus will recognize that the rate of change of position with respect to time is a derivative, and the definition of the derivative states that as the timestep across which a measurement for approximation is taken approaches zero, the result is equal to the derivative at that time.
Measuring the difference in the calculated velocities of successive pulses can give an approximation of rotational acceleration as well.
Measuring Direction[edit]
Assume a machine is tuning its PWM routines for powering the drive wheels. The controller obviously needs to know the rotational speed of the wheels so it can determine the speed outputs for given duty cycles. But what if the machine is placed on a hill? It starts to roll backward and the controller sees a substantial rotational speed for no input, which messes up the entire calibration process. If the controller were able to determine that the wheel was spinning backward, it would know to ignore the speed information. In order to determine direction with a rotary encoder, there must be at least two patterns on the disk, so there is a reference for the direction of change. The simplest way to achieve this is with two alternating patters that are half a pulse out of phase with each other.
If the disc is rotating in a particular direction (in this case, such that the pattern slides to the left), the rising (or falling) edge of channel A will be detected before that of channel B; if rotation is in the opposite direction, the edge of channel B will be detected first. By testing for which pattern's edges are detected first, the direction of rotation can be determined. The time between pulses for a single channel can be measured to approximate rotational speed. However, combining detections from the two channels gives an effective pulse rate double that of a single channel, which halves the measuring time and doubles precision. A means of measuring speed and direction with a single channel exists as well. Two receivers are placed on a single channel spaced half a pulse width apart, so that as the disc rotates, one sensor will be in state as the other transitions. The order in which the two sensors transition into a state is dependent on the direction of rotation of the disc.
Measuring Position[edit]
The simplest way to determine an angular position on a rotary encoder is to divide the disc into segments and imprint each segment with a unique pattern. The more segments the disc is divided into, the more precision is gained in knowing the location at a given time (each segment will cover a span of angles, so there will be uncertainty, but more segments will give fewer degrees per segment). More segments, however, require more unique identifiers, which means a more complex scheme. This discussion is limited to digitalsignal encoders, where the possible outputs for any given channel are limited to 0 or 1. This is conducive to a binary encoding scheme, where each segment is imprinted with a unique binary number. The number of unique IDs required depends on the number of segments; the number of channels is equal to log2(n) where n is the number of segments. A disc with 8 segments has a resolution of 360° / 8 = 45° per segment, and requires log2(8) = 3 separate channels. For a doubling of the number of segments, precision is doubled with an increase of only one pattern channel.
Reliability[edit]
One of the problems with using a binary encoding scheme is detector error. There's no guarantee that every channel is going to transition at the same time, which can cause a misread of a segment's ID. If the section of the disc below the detector is transitioning from one segment to the next, briefly the decoder might read only part of the bits correctly. Assume a standard eightsegment binary encoding scheme, where segments are numbered from 0 to 8 sequentially: if the transition from segment 000 to segment 001 doesn't read the rising edge correctly, your read is off by one space, which for the moment is negligible. If, however, it's transitioning from segment 011 to segment 100, and the least significant bit is read in first, briefly your controller will think it's transitioned to 010, in the complete opposite direction. If the most significant bit is read in first, it'll think it jumped from 011 to 111, on the complete opposite side of the wheel. Transition errors when a single bit changes can often be considered negligible, as the error in calculated position is 1. Errors from multibit transitions can be catastrophic.
Gray Code[edit]

Gray code is a specific type of binary pattern in which there is only a single bit changing during a transition from one element to the next. Being a binary pattern, Gray codes use the same number of channels for the same number of specific patterns; they are just ordered differently. Binary uses patterns in a numerically sequential order; Gray code does not. Encoding positions using this method will require additional software or hardware to decode the ID (as a simple binary conversion will not yield a sequentiallyordered set of segments), but the benefits for the reliability of hardware are substantial – as only a single bit changes during each transition, then the maximum error in detected position for an error in pattern reception is 1 segment. Gray code can be written out bitwise in much the same way binary can. In binary, the least significant bit alternates from 0 to 1 every step; the next bit alternates every two steps, the next every four and so on. In Gray code, each bit holds a pattern of 0110; the least bit holds each element for one step; the next bit holds each two steps, the next four and so on. It can be noticed that twochannel Gray code is identical to the twochannel pattern used to find rotational direction.