Hardware Reference
In-Depth Information
void UpdateRotation() {
// update sensor's reading upon the falling edge of pin 2
if (digitalRead(B)) {
Rotor++; // increment direction if second pin is HI
} // at time of the interrupt
else {
Rotor--; // decrement direction if second pin is LOW
} // at time of the interrupt
Serial.println(Rotor, DEC);
} // end UpdateRotation()
Outputting Gray Code
For the Arduino to mimic the gray code, it must produce multiple square waves that are evenly out of phase from
one to another. Using a series of digitalWrite() function calls to control the output pins' states and a delay() to
control the phase is a perfect way to control a series of digital signals that need a specific order. One digitalWrite()
is used per output of the rotary encoder to be mimicked, with a delay() after each write to make an overlap of the
digital cycle. An encoder that has two outputs needs two digitalWrite() calls in a loop, with a delay() after each
write, flipping the state that is written to the pin each time the loop is run. A square wave will be produced, having a
total cycle time equal to twice the total delay time. Each time the loop is run, one half of the gray code cycle is output.
The order in which the pins are manipulated determines the direction of the encoder; the orders are opposite if the
forward order goes from pin 1 to 3 and the reverse order goes from 3 to 1. The percentage of time that the cycle is
out of phase is controlled by the delay() after the digitalWrite() . To calculate the phase difference, the individual
delay() is divided by total delay() . For two outputs having a total delay of 6ms and individual delays of 3ms, the
second output is out of phase by 50 percent.
Some rotary encoders have outputs that are out of phase by 100 percent, being in completely opposite states.
To achieve a four-output encoder with the third output being the opposite of the first output and still having an even
distribution of phase, the first output has to flip state at the same time as the third, and the fourth output needs no
delay, creating a cycle of 6ms with a 1ms phase shift. The cycle time created is representative of how fast the sensor
can be manipulated.
To calculate the maximum rate an encoder can simulate, divide 60 by the total cycle time multiplied the total
steps over a specific distance. The distance for rotary style is one revolution and the linear distance can be inches,
centimeters, or another unit. The encoder being emulated for the example has a cycle of 12 steps per revolution.
The shortest cycle time implemented by the reader code is 8ms, making the calculation 60s / (0.008s / step * 12 steps /
revolution) = 625rpm. The digitalWrite() is negligible in calculating maximum manipulation speed. The added
time is about 6.75ms for this code, giving a 0.3% tolerance. If the delay is removed, the sensor can run at about
1.8 million rpm.
Calculating the maximum speed capable is not for determining the delay to use, but for information about the
application of the simulated hardware in control feedback loops. The delay to use between the pin writes should be
at least 1ms, and a separate delay should be used to control and vary the manipulation speed. If the sensor code is
having problems accurately reading the sensor's output, increase the delay between the digitalWrite() function
calls.
The setup for the hardware is as shown in Figure 6-5 , with reader pins 2 and 3 connected to sensor pins 10 and 11,
respectively. Two momentary switches used to control the direction of the output pulses are connected to ground
and independently connected to sensor pin 2 for down and 3 for up. The code from Listing 6-5 needs to be loaded
on the Arduino to be used as the sensor. The reader Arduino is loaded with the code from Listing 6-4. Simulating this
rotary encoder requires one more pin than the actual sensor; the ground and 5V pins need to be connected between
the two Arduinos.
 
Search WWH ::




Custom Search