Archive

Archive for the ‘C++’ Category

BeagleBone with a Rotary Encoder

April 11, 2012 8 comments
Rotary Encoder

PEC11 Incremental Encoder

I’ve been looking at a variety of potential input devices for my BeagleBone. adaruit packages a PEC11 Series – 12 mm Incremental Encoder and I decide to give it a try.

This encoder requires ground on the center lead and provides “output” on the two outside leads. As you turn (click) the encoder knob from one detent to another the output leads will signal either high or low. Combining the output from those leads gives you the relative position of the knob. Monitoring the changes in those output tells you if the encoder has been turned clockwise or counter clockwise.

BeagleBone with Rotary Encoder

BeagleBone with Rotary Encoder

I grounded the encoder using a ground pin from the BeagleBones expansion header and wired the two output leads to a GPIO pin of their own. I could now read the value (signal) from each output lead. All this is pretty straight forward stuff when using a BeagleBone. Now for the fun stuff.

I’m using the sysfs GPIO interface to read output from the encoder. This means that I have to set the direction of the GPIO to “in”. Polling the GPIO’s value returns either a “1” or “0”. As you turn the encoder the value of each GPIO will change from 1 to 0 and then back to zero. But they transition from one value to another at different positions as the encoder turns. This is called a quadrature outputs.

Quadrature Output Table

Quadrature Output Table

This quadrature output table is taken from the PEC11’s data sheet. For the untrained (like me) this diagram is a bit tough to understand at first. When the encoder is positioned on a detent both the A and B leads will signal low. As the encoder is turned clockwise (CW) the A lead will start to signal high and then B lead will follow with a high signal. As the encoder continues to turn the A signal goes low and the B signal follows as the encoder arrives at the next detent. The opposite occurs if the encoder is turned counter clockwise (CCW). These signal pairs are called Gray code.

Another twist is that low signal from an encoder lead results in the GPIO’s value returning 1 (ON). A high signal returns a GPIO value of 0 (OFF). These tables describes the Gray code sent from the encoder and the associated values from the GPIO’s.

Clockwise
Encoder Gray Code GPIO Values
Phase A B A B
1 0 0 1 1
2 1 0 0 1
3 1 1 0 0
4 0 1 1 0
Counter Clockwise
Encoder Gray Code GPIO Values
Phase A B A B
1 0 0 1 1
2 0 1 1 0
3 1 1 0 0
4 1 0 1 1

An incremental rotary encoder such as this cannot represent it’s absolute position.  It just signals it’s position relative to the last detent it was on. Tracking the signal changes as it transitions from one detent to another will identify when the encoder travels clockwise or counter clockwise. This encoder has 24 detents (segments).

I wrote a program that constantly polls the two GPIO’s connected to the encoder’s A/B leads. With a very simple finite state machine (a switch statement) I was able to track the rotary of the encoder’s knob. Here’s some of the C++ code.

Rotary::Rotary(unsigned a, unsigned b) : _position(0) {
        A = new GPIO(a);
        B = new GPIO(b);

        A->direction("in");
        B->direction("in");
}

unsigned Rotary::phase() {
    unsigned a = A->value();
    unsigned b = B->value();

    a = (a == 0) ? 1 : 0;
    b = (b == 0) ? 1 : 0;

    return (a << 1) | b;
}

#define DETENT   0
#define CW_1     1
#define CW_2     2
#define CW_3     3
#define CCW_1   -1
#define CCW_2   -2
#define CCW_3   -3

void *Rotary::monitor(void *arg) {
        Rotary *rotary = static_cast<Rotary *>(arg);
        int state = DETENT;
        int last = -1;

        while (rotary->run_state) {
                int value = rotary->phase();

                if (value != last) {
                        switch (state) {
                        case DETENT:
                                if (value == 2) {
                                        state = CW_1;
                                }
                                else if (value == 1) {
                                        state = CCW_1;
                                }
                                break;
                        case CW_1:
                                if (value == 0) {
                                        state = DETENT;
                                }
                                else if (value == 3) {
                                        state = CW_2;
                                }
                                break;
                        case CW_2:
                                if (value == 2) {
                                        state = CW_1;
                                }
                                else if (value == 1) {
                                        state = CW_3;
                                }
                                break;
                        case CW_3:
                                if (value == 1) {
                                        state = CW_2;
                                }
                                else if (value == 0) {
                                        state = DETENT;
                                        rotary->_position++;
                                }
                                break;
                        case CCW_1:
                                if (value == 0) {
                                        state = DETENT;
                                }
                                else if (value == 3) {
                                        state = CCW_2;
                                }
                                break;
                        case CCW_2:
                                if (value == 1) {
                                        state = CCW_1;
                                }
                                else if (value == 2) {
                                        state = CCW_3;
                                }
                                break;
                         case CCW_3:
                                if (value == 3) {
                                         state = CCW_2;
                                }
                                else if (value == 0) {
                                         state = DETENT;
                                         rotary->_position--;
                                }
                          }

                          last = value;
                  }
        }
}

This source code is available at github.

When run in a hard loop the monitor code will consistently track the knobs rotation. I spun the knob as quickly as I could and was unable to make the program miscount detents (clicks). Later when I ran the code in a thread I did see some missed detents.

Finally, you can plug this encoder into a breadboard but it doesn’t go in easily and is prone to pop out.

Advertisements
Categories: BeagleBone, C++ Tags: , ,