1

I am trying to read a quadrature motor encoder with the Raspberry Pi. After attempting to write the code in Python, I came to the conclusion that Python was too slow (which was expected). I tried to write the code in C++, but the output surprised me.

C++ Code:

#include <iostream>
#include <pigpio.h>
#include <unistd.h>

using std::cout;
using std::endl;

class Encoder {
    public:
        int pinA;
        int pinB;
        volatile int steps = 0;
        Encoder(int aPinA, int aPinB) {
            pinA = aPinA;
            pinB = aPinB;
            gpioSetMode(pinA, PI_INPUT);
            gpioSetMode(pinB, PI_INPUT);

            gpioSetAlertFuncEx(pinA, static_callback, this);
        }
        void static static_callback(int gpio, int level, unsigned int tick, void * this_pointer) { // Static function so it can be passed as a callback
            Encoder * self = static_cast<Encoder*>(this_pointer);
            if (level == 1) {
                cout << self->steps << endl;
                if (gpioRead(self->pinB) == 1) self->steps++;
                else self->steps--;
            }
        }
        void stopCallbacks() {
            gpioSetAlertFuncEx(pinA, NULL, this);
        }
};

int main() {
    gpioInitialise();
    Encoder encoder1(23, 24);
    usleep(3000000);
    encoder1.stopCallbacks();

    return 0;
}

The output of which looked like this:

-116
-117
-118
-119
-118
-117
-118
-119
-120
-119
-118
-119
-120
-121
-120
-119
-120
-121
-122
-121
-120
-119
-120
-121
-120
-119
-120
-121
-122
-121
-120
-119
-118
-119
-120
-119
-118
-117
-118
-119
-118
-117
-118
-119
-120
-119
-118
-119
-120
-121
-120
-119
-120
-121
-122
-121
-120
-121
-122
-123
-122
-121
-120
-119
-120
-121
-120
-119
-120
-121
-122
-121
-120
-121
-122
-123
-122
-121
-120
-119
-120
-121
-120
-119
-120
-121
-122
-121
-120
-121
-122
-123
-124
-123
-122
-121
-122
-123
-122
-121
-122
-123
-124
-123
-122
-123
-124
-125
-124
-123
-122
-123
-124
-125
-124
-123
-122
-123
-124
-123
-122
-123
-124
-125
-124
-123
-124
-125
-126
-127
-126
-125
-124
-125
-126
-125
-124
-125
-126
-127
-126
-125
-126
-127
-128
-129
-128
-127
-126
-127
-128
-127
-126
-127
-128
-129
-128
-127
-128
-129
-130
-129
-128
-129
-130
-131
-130
-129
-128
-127
-128
-129
-128
-127
-126
-127
-128
-127
-126
-127
-128
-129
-128
-127
-126
-127
-128
-127
-126
-127
-128
-127
-126
-127
-128
-129
-128
-127
-128
-129
-130
-129
-128
-129
-130
-131
-130
-129
-128
-129
-130
-129
-128
-127
-128
-129
-128
-127
-128
-129
-130
-129
-128
-129
-130
-129
-128
-129
-130
-131
-130
-129
-130
-131
-132
-131
-130
-131
-132
-133
-132
-131
-130
-131
-132
-131
-130
-129
-130
-131
-130
-129
-130
-131
-130
-129
-128
-129
-130
-129
-128
-129
-130
-131
-130
-129
-130
-131
-132
-131
-130
-129
-130
-131
-130
-131
-132
-133
-132
-131
-132
-133
-134
-133
-132
-131
-132
-131
-130
-131
-132
-133
-132
-131
-132
-133
-134
-133
-132
-131
-132
-133
-132
-133
-134
-135
-134
-133
-134
-135
-136
-135
-134
-135
-136
-137
-136
-135
-134
-135
-136
-135
-134
-133
-134
-133
-132
-133
-134
-135
-134
-133
-134
-135
-136
-135
-134
-135
-136
-137
-136
-135
-134
-135
-134
-133
-134
-135
-136
-135
-134
-135
-136
-137
-136
-135
-136
-137
-138
-137
-136
-135
-136
-137
-136
-135
-134
-135
-136
-135
-134
-133
-134
-133
-132
-133
-134
-135
-134
-133
-134
-135
-136
-135
-134
-133
-134
-135
-134
-133
-134
-135
-136
-135
-134
-135
-136
-137
-136
-135
-134
-135
-136
-135
-134
-133
-134
-135
-134
-133
-132
-133
-132
-131
-132
-133
-134
-133
-132
-133
-134
-135
-134
-133
-134
-135
-136
-135
-134
-133
-134
-135
-136
-135
-134
-133
-134
-135
-134
-133
-132
-133
-134
-133
-132
-131
-132
-133
-132
-131
-132
-133
-134
-133
-132
-131
-132
-133
-134
-133
-132
-131
-132
-133
-132
-131
-132
-133
-134
-133
-132
-131
-130
-131
-132
-131
-130
-131
-132
-133
-132
-131
-130
-129
-130
-131
-130
-129
-130
-131
-132
-131
-130
-131
-132
-133
-134
-133
-132
-133
-134
-135
-134
-133
-134
-135
-136
-135
-134
-133
-132
-133
-134
-133
-132
-131
-132
-133
-132
-131
-132
-133
-134
-133
-132
-131
-130
-131
-132
-131
-130
-129
-130
-131
-130
-129
-128
-129
-130
-129
-128
-127
-128
-129
-130
-129
-128
-127
-128
-129
-128
-127
-126
-127
-128
-127
-126
-125
-126
-127
-126
-125
-124
-125
-126
-125
-124
-125
-126
-127
-126
-125
-126
-127
-128
-127
-126
-127
-128
-129
-128
-127
-128
-129
-130
-129
-128
-129
-130
-131
-130
-129
-128
-129
-130
-129
-128
-127
-128
-129
-128
-127
-126
-127
-128
-127
-128
-129
-130
-129
-128
-129
-130
-131
-130
-129
-128
-129
-130
-129
-128
-129
-130
-131
-130
-129
-130
-131
-132
-131
-130
-129
-130
-131
-130
-129
-130
-131
-132
-131
-130
-131
-132
-133
-132
-131
-132
-133
-134
-133
-132
-133
-134
-135
-134
-133
-132
-133
-134
-133
-132
-133
-134
-133
-132
-133
-134
-135
-134
-133
-134
-135
-136
-135
-134
-135
-136
-135
-134
-135
-136
-137
-136
-135
-134
-135
-136
-135
-134
-133
-134
-133
-132
-133
-134
-135
-134
-133
-134
-135
-136
-135
-134
-133
-134
-133
-132
-133
-134
-135
-134
-133
-134
-135
-134
-133
-134
-135
-136
-135
-134
-135
-136
-137
-136
-135
-134
-135
-136
-135
-134
-133
-134
-133
-132
-133
-134
-135
-134
-133
-134
-135
-136
-135
-134
-133
-134
-135
-134
-133
-134
-135
-136
-135
-136
-137
-138
-137
-136
-137
-138
-139
-138
-137
-138
-139
-140
-139
-138
-137
-138
-139
-138
-137
-136
-137
-136
-135
-134
-135
-136
-135
-134
-135
-136
-137
-136
-135
-134
-135
-136
-137
-136
-135
-134
-135
-134
-133
-134
-135
-136
-137
-136
-135
-136
-137
-138
-137
-136
-135
-136
-135
-134
-135
-136
-137
-136
-135
-134
-135
-136
-137
-136
-135
-134
-135
-136
-135
-134
-135
-136
-137
-136
-135
-136
-137
-138
-139
-138
-137
-136
-137
-138
-137
-136
-137
-138
-139
-138
-137
-138
-139
-140
-141
-140
-139
-138
-139
-140
-139
-138
-139
-140
-141
-140
-139
-138
-137
-138
-139
-138
-137
-138
-139
-140
-141
-140
-139
-140
-141
-142
-141
-140
-141
-142
-143
-142
-141
-140
-139
-140
-141
-140
-139
-140
-141
-142
-143
-142
-141
-142
-143
-144
-143
-142
-143
-144
-145
-144
-143
-142
-141
-142
-143
-142
-141
-140
-141
-142
-141
-140
-139
-140
-141
-142
-141
-140
-139
-140
-141
-140
-139
-138
-139
-140
-139
-138
-139
-140
-141
-140
-139
-138
-137
-138
-139
-138
-137
-136
-137
-138
-137
-136
-137
-138
-139
-140
-139
-138
-137
-138
-139
-138
-137
-136
-137
-138
-137
-136
-135
-136
-137
-136
-135
-136
-137
-138
-137
-136
-135
-136
-137
-136
-135
-134
-135
-136
-135
-134
-135
-136
-137
-136
-135
-136
-137
-138
-137
-136
-137
-138
-137
-136
-135
-136
-137
-138
-137
-136
-135

I expected the output to increase or decrease, instead of going all over the place, as the motor was going in one direction.

Sorry if this is just a dumb mistake, I am learning C++ :). If there is more info that needs to be included, please ask. Here is the motor encoder I am using: https://www.pololu.com/product/4866.

1

One problem is the way you are using pigpio.

You are using alerts (GOOD) which supply an ordered sequence of timed GPIO events. You are reading the current level of a GPIO within the callback (VERY BAD). The event which triggered the callback may have happened several milliseconds before and will not necessarily be consistent with the current state of the GPIO. For an encoder read both the A and B channel GPIO and supply them to the callback.

For examples of what I mean see the following.

Python (via daemon)

C++

C (via daemon)

1
  • Thank you so much for the help!
    – KetZoomer
    May 23 at 18:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.