1#ifndef J_ENCODER_QUADRATURE_H
2#define J_ENCODER_QUADRATURE_H
21 float distPerCountFactor;
24 volatile long tickCounter;
25 volatile bool encForwards;
26 volatile unsigned long lastEncoderTickMicros;
27 volatile unsigned long encoderIntervalMicros;
29 unsigned long slowestIntervalMicros;
42 JEncoderQuadrature(
byte _encoderAPin,
byte _encoderBPin,
float _distPerCountFactor = 1.0,
bool _reverse =
false,
unsigned long _slowestIntervalMicros = 100000UL)
51 distPerCountFactor = _distPerCountFactor;
52 slowestIntervalMicros = _slowestIntervalMicros;
57 encoderIntervalMicros = 0;
58 lastEncoderTickMicros = 0;
67 virtual void setUpInterrupts(
void (*_isrAPointer)(
void),
void (*_isrBPointer)(
void));
77 long tempCounter = tickCounter * reverse;
84 unsigned long tempEncoderTickMicros = lastEncoderTickMicros;
85 unsigned long tempInterval = encoderIntervalMicros;
89 if ((micros() - tempEncoderTickMicros + tempInterval) > slowestIntervalMicros) {
92 if (tempInterval == 0) {
97 return reverse * 1000000.0 / (tempInterval)*distPerCountFactor * 4;
99 return reverse * -1000000.0 / (tempInterval)*distPerCountFactor * 4;
104 return tickCounter * reverse;
108 return tickCounter * distPerCountFactor * reverse;
112 return distPerCountFactor;
116 distPerCountFactor = _factor;
140 unsigned long tempEncoderTickMicros = lastEncoderTickMicros;
141 unsigned long tempInterval = encoderIntervalMicros;
142 if ((micros() - tempEncoderTickMicros + tempInterval) > slowestIntervalMicros) {
162 unsigned long tempMicros = micros();
163 encoderIntervalMicros = tempMicros - lastEncoderTickMicros;
164 lastEncoderTickMicros = tempMicros;
defines common interface for JEncoder
Definition JEncoder.h:33
reads a quadrature (incremental) encoder
Definition JEncoderQuadrature.h:13
void setReverse(bool _reverse)
reverse readings of encoder
Definition JEncoderQuadrature.h:123
long zeroCounter()
reset the counter of how far the encoder has turned
Definition JEncoderQuadrature.h:75
float getDistPerCountFactor()
returns a conversion factor between encoder ticks and distance that can be set for the encoder
Definition JEncoderQuadrature.h:110
float getVel()
calculates velocity in distance per second where distance was set by setdistPerCountFactor()
Definition JEncoderQuadrature.h:81
byte encoderBPin
Definition JEncoderQuadrature.h:17
void ISRB(void)
Definition JEncoderQuadrature.h:174
virtual void setUpInterrupts(void(*_isrAPointer)(void), void(*_isrBPointer)(void))
set up pins and interrupts
JEncoderQuadrature(byte _encoderAPin, byte _encoderBPin, float _distPerCountFactor=1.0, bool _reverse=false, unsigned long _slowestIntervalMicros=100000UL)
constructor, sets pins and settings
Definition JEncoderQuadrature.h:42
bool hasDirection()
can this encoder measure direction or just speed
Definition JEncoderQuadrature.h:131
float getPos()
returns how far the encoder has turned from the zero position converted to distance
Definition JEncoderQuadrature.h:106
void run()
if an encoder needs to have some code called each loop (like absolute encoder polling encoder and cal...
Definition JEncoderQuadrature.h:157
bool isVelNew()
could be useful for only recalculating a control loop if there's new velocity data
Definition JEncoderQuadrature.h:135
virtual void turnOffInterrupts()
disable interrupts and stop monitoring encoder
long getCounter()
returns how far the encoder has turned from the zero position
Definition JEncoderQuadrature.h:102
byte encoderAPin
Definition JEncoderQuadrature.h:16
void ISRA(void)
Definition JEncoderQuadrature.h:159
void setDistPerCountFactor(float _factor)
set the conversion factor between encoder ticks and distance
Definition JEncoderQuadrature.h:114