I'm looking for some input on my code and some helpful hints. I've been attempting to incorporate a quadrature encoder with my StartKit and ran into some problems.
After sorting out my hardware issues (needs some pull-up resistors) I found out that it looks like my code is missing transitions. I should be getting ~700 counts / revolution but I end up with ~20-30 (it varies) and more if I rotate slowly. This is leading me to believe my code is inefficient or my approach is bad.
Code is below, but to summarize:
1. Use a 4 bit port (with inputs A and B on bits 0 and 1 respectively)
2. Dwell on this port and wait for changes (and change to A or B should trigger a recalculation)
3. Process the new data and share it to a process on another core to give it to the user (my attempt at speeding up the collection)
Any thoughts on this approach?
Thanks!
Code: Select all
#include <xs1.h>
#include <timer.h>
#include <stdio.h>
/*
 * This code will measure the counts of a quadrature encoder.
 */
// Define the bits that correspond to the signals. Since we are combining them on the 4 bit port this will make it easier to address them
#define quad1_a_mask 1
#define quad1_b_mask 2
// Quadrature Encoder Matrix; props to http://www.robotshop.com/media/files/PDF/tutorial-how-to-use-a-quadrature-encoder-rs011a.pdf for the idea
int quadEncoderMatrix [16] = {0,-1,1,0,1,0,0,-1,-1,0,0,1,0,1,-1,0};
/* Inter task-communication layer */
interface my_interface {
    void encoderCounts ( int x);
};
// Monitor Encoder Task
void monitorEncoder (client interface my_interface i);
//Print Encoder Task
void printEncoder ( server interface my_interface i);
/* This the port where the leds reside */
port quad = XS1_PORT_4E;
int main(void) {
    interface my_interface i1 ;
    par {
        monitorEncoder(i1);
        printEncoder(i1);
    }
    return 0;
}
void monitorEncoder (client interface my_interface i) {
    int quadTicks = 0;
    int oldQuadPulse, newQuadPulse;
    oldQuadPulse = 0;
    newQuadPulse = 0;
    while(1) {
        quad :> newQuadPulse;
        quadTicks += quadEncoderMatrix[oldQuadPulse * 4 + newQuadPulse];
        i.encoderCounts (quadTicks);
        oldQuadPulse = newQuadPulse;
    }
}
void printEncoder ( server interface my_interface i) {
    while (1) {
        // wait for either encoderCounts over connection 'i '.
        select {
            case i.encoderCounts ( int x ):
                printf("Encoder: %i \n", x);
            break ;
        }
    }
}
