Quadrature encoder interface by Aaron Berk
Fork of QEI by
QEI.h@1:5f28ceec8280, 2016-05-04 (annotated)
- Committer:
- aminomar
- Date:
- Wed May 04 18:47:39 2016 +0000
- Revision:
- 1:5f28ceec8280
- Parent:
- 0:5c2ad81551aa
test encoder;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
aberk | 0:5c2ad81551aa | 1 | |
aberk | 0:5c2ad81551aa | 2 | #ifndef QEI_H |
aberk | 0:5c2ad81551aa | 3 | #define QEI_H |
aberk | 0:5c2ad81551aa | 4 | |
aberk | 0:5c2ad81551aa | 5 | #include "mbed.h" |
aberk | 0:5c2ad81551aa | 6 | |
aminomar | 1:5f28ceec8280 | 7 | #define PREV_MASK 0x1 //Mask for the previous state in determining direction of rotation. |
aminomar | 1:5f28ceec8280 | 8 | #define CURR_MASK 0x2 //Mask for the current state in determining direction of rotation. |
aberk | 0:5c2ad81551aa | 9 | #define INVALID 0x3 //XORing two states where both bits have changed. |
aberk | 0:5c2ad81551aa | 10 | |
aminomar | 1:5f28ceec8280 | 11 | |
aberk | 0:5c2ad81551aa | 12 | class QEI { |
aberk | 0:5c2ad81551aa | 13 | |
aberk | 0:5c2ad81551aa | 14 | public: |
aberk | 0:5c2ad81551aa | 15 | |
aminomar | 1:5f28ceec8280 | 16 | typedef enum Encoding |
aminomar | 1:5f28ceec8280 | 17 | { |
aminomar | 1:5f28ceec8280 | 18 | X2_ENCODING, |
aberk | 0:5c2ad81551aa | 19 | X4_ENCODING |
aminomar | 1:5f28ceec8280 | 20 | } |
aminomar | 1:5f28ceec8280 | 21 | Encoding; |
aberk | 0:5c2ad81551aa | 22 | |
aberk | 0:5c2ad81551aa | 23 | /** |
aberk | 0:5c2ad81551aa | 24 | * Constructor. |
aberk | 0:5c2ad81551aa | 25 | * |
aberk | 0:5c2ad81551aa | 26 | * Reads the current values on channel A and channel B to determine the |
aberk | 0:5c2ad81551aa | 27 | * initial state. |
aberk | 0:5c2ad81551aa | 28 | * |
aberk | 0:5c2ad81551aa | 29 | * Attaches the encode function to the rise/fall interrupt edges of |
aberk | 0:5c2ad81551aa | 30 | * channels A and B to perform X4 encoding. |
aberk | 0:5c2ad81551aa | 31 | * |
aberk | 0:5c2ad81551aa | 32 | * Attaches the index function to the rise interrupt edge of channel index |
aberk | 0:5c2ad81551aa | 33 | * (if it is used) to count revolutions. |
aberk | 0:5c2ad81551aa | 34 | * |
aberk | 0:5c2ad81551aa | 35 | * @param channelA mbed pin for channel A input. |
aberk | 0:5c2ad81551aa | 36 | * @param channelB mbed pin for channel B input. |
aberk | 0:5c2ad81551aa | 37 | * @param index mbed pin for optional index channel input, |
aberk | 0:5c2ad81551aa | 38 | * (pass NC if not needed). |
aberk | 0:5c2ad81551aa | 39 | * @param pulsesPerRev Number of pulses in one revolution. |
aberk | 0:5c2ad81551aa | 40 | * @param encoding The encoding to use. Uses X2 encoding by default. X2 |
aberk | 0:5c2ad81551aa | 41 | * encoding uses interrupts on the rising and falling edges |
aberk | 0:5c2ad81551aa | 42 | * of only channel A where as X4 uses them on both |
aberk | 0:5c2ad81551aa | 43 | * channels. |
aberk | 0:5c2ad81551aa | 44 | */ |
aberk | 0:5c2ad81551aa | 45 | QEI(PinName channelA, PinName channelB, PinName index, int pulsesPerRev, Encoding encoding = X2_ENCODING); |
aberk | 0:5c2ad81551aa | 46 | |
aberk | 0:5c2ad81551aa | 47 | /** |
aberk | 0:5c2ad81551aa | 48 | * Reset the encoder. |
aberk | 0:5c2ad81551aa | 49 | * |
aberk | 0:5c2ad81551aa | 50 | * Sets the pulses and revolutions count to zero. |
aberk | 0:5c2ad81551aa | 51 | */ |
aberk | 0:5c2ad81551aa | 52 | void reset(void); |
aberk | 0:5c2ad81551aa | 53 | |
aberk | 0:5c2ad81551aa | 54 | /** |
aberk | 0:5c2ad81551aa | 55 | * Read the state of the encoder. |
aberk | 0:5c2ad81551aa | 56 | * |
aberk | 0:5c2ad81551aa | 57 | * @return The current state of the encoder as a 2-bit number, where: |
aberk | 0:5c2ad81551aa | 58 | * bit 1 = The reading from channel B |
aberk | 0:5c2ad81551aa | 59 | * bit 2 = The reading from channel A |
aberk | 0:5c2ad81551aa | 60 | */ |
aberk | 0:5c2ad81551aa | 61 | int getCurrentState(void); |
aberk | 0:5c2ad81551aa | 62 | |
aberk | 0:5c2ad81551aa | 63 | /** |
aberk | 0:5c2ad81551aa | 64 | * Read the number of pulses recorded by the encoder. |
aberk | 0:5c2ad81551aa | 65 | * |
aberk | 0:5c2ad81551aa | 66 | * @return Number of pulses which have occured. |
aberk | 0:5c2ad81551aa | 67 | */ |
aberk | 0:5c2ad81551aa | 68 | int getPulses(void); |
aberk | 0:5c2ad81551aa | 69 | |
aberk | 0:5c2ad81551aa | 70 | /** |
aberk | 0:5c2ad81551aa | 71 | * Read the number of revolutions recorded by the encoder on the index channel. |
aberk | 0:5c2ad81551aa | 72 | * |
aberk | 0:5c2ad81551aa | 73 | * @return Number of revolutions which have occured on the index channel. |
aberk | 0:5c2ad81551aa | 74 | */ |
aberk | 0:5c2ad81551aa | 75 | int getRevolutions(void); |
aberk | 0:5c2ad81551aa | 76 | |
aberk | 0:5c2ad81551aa | 77 | private: |
aberk | 0:5c2ad81551aa | 78 | |
aberk | 0:5c2ad81551aa | 79 | /** |
aberk | 0:5c2ad81551aa | 80 | * Update the pulse count. |
aberk | 0:5c2ad81551aa | 81 | * |
aberk | 0:5c2ad81551aa | 82 | * Called on every rising/falling edge of channels A/B. |
aberk | 0:5c2ad81551aa | 83 | * |
aberk | 0:5c2ad81551aa | 84 | * Reads the state of the channels and determines whether a pulse forward |
aberk | 0:5c2ad81551aa | 85 | * or backward has occured, updating the count appropriately. |
aberk | 0:5c2ad81551aa | 86 | */ |
aberk | 0:5c2ad81551aa | 87 | void encode(void); |
aberk | 0:5c2ad81551aa | 88 | |
aberk | 0:5c2ad81551aa | 89 | /** |
aberk | 0:5c2ad81551aa | 90 | * Called on every rising edge of channel index to update revolution |
aberk | 0:5c2ad81551aa | 91 | * count by one. |
aberk | 0:5c2ad81551aa | 92 | */ |
aberk | 0:5c2ad81551aa | 93 | void index(void); |
aberk | 0:5c2ad81551aa | 94 | |
aberk | 0:5c2ad81551aa | 95 | Encoding encoding_; |
aberk | 0:5c2ad81551aa | 96 | |
aberk | 0:5c2ad81551aa | 97 | InterruptIn channelA_; |
aberk | 0:5c2ad81551aa | 98 | InterruptIn channelB_; |
aberk | 0:5c2ad81551aa | 99 | InterruptIn index_; |
aberk | 0:5c2ad81551aa | 100 | |
aberk | 0:5c2ad81551aa | 101 | int pulsesPerRev_; |
aberk | 0:5c2ad81551aa | 102 | int prevState_; |
aberk | 0:5c2ad81551aa | 103 | int currState_; |
aberk | 0:5c2ad81551aa | 104 | |
aberk | 0:5c2ad81551aa | 105 | volatile int pulses_; |
aberk | 0:5c2ad81551aa | 106 | volatile int revolutions_; |
aberk | 0:5c2ad81551aa | 107 | |
aberk | 0:5c2ad81551aa | 108 | }; |
aberk | 0:5c2ad81551aa | 109 | |
aberk | 0:5c2ad81551aa | 110 | #endif /* QEI_H */ |