1#ifndef J_ENCODER_ATTACH_INTERRUPT_SINGLE_H
2#define J_ENCODER_ATTACH_INTERRUPT_SINGLE_H
24 float distPerCountFactor;
27 volatile long tickCounter;
28 volatile unsigned long lastEncoderTickMicros;
29 volatile unsigned long encoderIntervalMicros;
31 unsigned long slowestIntervalMicros;
33 unsigned long switchBounceIntervalMicros;
45 JEncoderSingle(
byte _encoderPin,
float _distPerCountFactor,
bool _reverse,
unsigned long _slowestIntervalMicros,
unsigned long _switchBounceIntervalMicros,
byte _interruptType)
49 distPerCountFactor = _distPerCountFactor;
50 slowestIntervalMicros = _slowestIntervalMicros;
51 switchBounceIntervalMicros = _switchBounceIntervalMicros;
61 encoderIntervalMicros = 0;
62 lastEncoderTickMicros = 0;
97 long tempCounter = tickCounter * reverse;
104 unsigned long tempEncoderTickMicros = lastEncoderTickMicros;
105 unsigned long tempInterval = encoderIntervalMicros;
109 if ((micros() - tempEncoderTickMicros + tempInterval) > slowestIntervalMicros) {
112 if (tempInterval == 0) {
115 return (rev ? -1 : 1) * reverse * 1000000.0 / tempInterval * distPerCountFactor;
119 return tickCounter * reverse;
123 return tickCounter * distPerCountFactor * reverse;
127 return distPerCountFactor;
131 distPerCountFactor = _factor;
155 unsigned long tempEncoderTickMicros = lastEncoderTickMicros;
156 unsigned long tempInterval = encoderIntervalMicros;
157 if ((micros() - tempEncoderTickMicros + tempInterval) > slowestIntervalMicros) {
176 if (micros() - lastEncoderTickMicros >= switchBounceIntervalMicros) {
177 unsigned long tempMicros = micros();
178 encoderIntervalMicros = tempMicros - lastEncoderTickMicros;
179 lastEncoderTickMicros = tempMicros;
181 tickCounter += (rev ? -1 : 1);
defines common interface for JEncoder
Definition JEncoder.h:33
reads a single channel (incremental) encoder
Definition JEncoderSingle.h:15
virtual void turnOffInterrupts()
disable interrupts and stop monitoring encoder
void setReverse(bool _reverse)
reverse readings of encoder
Definition JEncoderSingle.h:138
byte interruptType
Definition JEncoderSingle.h:19
bool hasDirection()
can this encoder measure direction or just speed
Definition JEncoderSingle.h:146
void setDistPerCountFactor(float _factor)
set the conversion factor between encoder ticks and distance
Definition JEncoderSingle.h:129
void setRev(bool _rev)
set which direction the encoder is moving (if you have external information about direction (like mot...
Definition JEncoderSingle.h:90
long zeroCounter()
reset the counter of how far the encoder has turned
Definition JEncoderSingle.h:95
void run()
if an encoder needs to have some code called each loop (like absolute encoder polling encoder and cal...
Definition JEncoderSingle.h:172
float getDistPerCountFactor()
returns a conversion factor between encoder ticks and distance that can be set for the encoder
Definition JEncoderSingle.h:125
unsigned char encoderPin
Definition JEncoderSingle.h:18
virtual void setUpInterrupts(void(*_isrPointer)(void))
set up pins and interrupts
float getVel()
calculates velocity in distance per second where distance was set by setdistPerCountFactor()
Definition JEncoderSingle.h:101
void encoderISR(void)
Definition JEncoderSingle.h:174
bool getRev()
get whether the encoder is told it's going backwards currently
Definition JEncoderSingle.h:82
JEncoderSingle(byte _encoderPin, float _distPerCountFactor, bool _reverse, unsigned long _slowestIntervalMicros, unsigned long _switchBounceIntervalMicros, byte _interruptType)
constructor, sets pins and settings
Definition JEncoderSingle.h:45
float getPos()
returns how far the encoder has turned from the zero position converted to distance
Definition JEncoderSingle.h:121
long getCounter()
returns how far the encoder has turned from the zero position
Definition JEncoderSingle.h:117
bool isVelNew()
could be useful for only recalculating a control loop if there's new velocity data
Definition JEncoderSingle.h:150