1#ifndef J_ENCODER_ATTACH_INTERRUPT_PWM_ABSOLUTE_H
2#define J_ENCODER_ATTACH_INTERRUPT_PWM_ABSOLUTE_H
10#define JEncoderPWMAbsolute_AS5048settings \
12 4095, 4119, 16, HIGH, 800, 1100 \
63 bool resetAngleOnStart;
66 float distPerCountFactor;
68 uint16_t lastVelAngle;
70 unsigned long lastVelTimeMicros;
71 unsigned long velEnoughTime;
72 unsigned long velEnoughTicks;
75 volatile bool newAngle;
76 volatile unsigned long dataStartMicros;
77 volatile unsigned long dataEndMicros;
78 volatile unsigned long earlyDataStartMicros;
79 volatile unsigned long oldDataEndMicros;
96 JEncoderPWMAbsolute(
byte _encoderPin,
pwmSettings _ps,
bool _reverse =
false,
float _distPerCountFactor = 1.0,
unsigned long _velEnoughTime = 0,
unsigned long _velEnoughTicks = 0,
bool _resetAngleOnStart =
false, uint16_t _zeroPos = 0)
100 resetAngleOnStart = _resetAngleOnStart;
109 velEnoughTime = _velEnoughTime;
110 velEnoughTicks = _velEnoughTicks;
119 lastVelTimeMicros = 0;
124 earlyDataStartMicros = 0;
125 oldDataEndMicros = 0;
126 justStartedVel =
true;
148 return angle * reverse;
158 return turns * reverse;
178 justStartedVel =
true;
189 return (turns * ps.
RESOLUTION + angle) * reverse;
194 return (turns * ps.
RESOLUTION + angle) * reverse * distPerCountFactor;
199 return distPerCountFactor;
243 long interval = dataEndMicros - dataStartMicros;
244 long cycle = dataEndMicros - oldDataEndMicros;
248 if ((resetAngleOnStart && firstAngle) || setPosZero) {
252 if ((resetAngleOnStart && firstAngle) || setPosZero) {
259 if (abs((int16_t)angle - (int16_t)lastAngle) >= ps.
RESOLUTION / 2) {
260 if (angle > lastAngle) {
269 int64_t velDist = ((int16_t)angle - (int16_t)lastVelAngle) + (turns - lastVelTurns) * ps.
RESOLUTION;
270 if ((micros() - lastVelTimeMicros > velEnoughTime || abs(velDist) > velEnoughTicks)) {
271 if (justStartedVel) {
272 justStartedVel =
false;
273 lastVelAngle = angle;
276 velocity = (double)1000000.0 * velDist / (micros() - lastVelTimeMicros) * distPerCountFactor * reverse;
278 lastVelTimeMicros = micros();
279 lastVelAngle = angle;
280 lastVelTurns = turns;
288 earlyDataStartMicros = micros();
290 oldDataEndMicros = dataEndMicros;
291 dataEndMicros = micros();
292 dataStartMicros = earlyDataStartMicros;
defines common interface for JEncoder
Definition JEncoder.h:33
reads a PWM signal from an absolute encoder
Definition JEncoderPWMAbsolute.h:23
long zeroCounter()
reset the counter of how far the encoder has turned
Definition JEncoderPWMAbsolute.h:161
virtual void setUpInterrupts(void(*_isrPointer)(void))
set up pins and interrupts
void run()
Definition JEncoderPWMAbsolute.h:239
long zeroCounter(bool resetAngle)
reset the zero point of the encoder
Definition JEncoderPWMAbsolute.h:171
bool hasDirection()
can this encoder measure direction or just speed
Definition JEncoderPWMAbsolute.h:211
long getCounter()
returns how far the encoder has turned from the zero position
Definition JEncoderPWMAbsolute.h:187
float getPos()
returns how far the encoder has turned from the zero position converted to distance
Definition JEncoderPWMAbsolute.h:192
void encoderISR()
Definition JEncoderPWMAbsolute.h:285
byte encoderPin
Definition JEncoderPWMAbsolute.h:56
JEncoderPWMAbsolute(byte _encoderPin, pwmSettings _ps, bool _reverse=false, float _distPerCountFactor=1.0, unsigned long _velEnoughTime=0, unsigned long _velEnoughTicks=0, bool _resetAngleOnStart=false, uint16_t _zeroPos=0)
sets pins and settings for reading the encoder
Definition JEncoderPWMAbsolute.h:96
virtual void turnOffInterrupts()
disable interrupts and stop monitoring encoder
float getDistPerCountFactor()
returns a conversion factor between encoder ticks and distance that can be set for the encoder
Definition JEncoderPWMAbsolute.h:197
bool setZeroPos(uint16_t zeroAngle)
change what angle of the absolute encoder is zero
Definition JEncoderPWMAbsolute.h:227
bool isVelNew()
could be useful for only recalculating a control loop if there's new velocity data
Definition JEncoderPWMAbsolute.h:216
float getVel()
calculates velocity in distance per second where distance was set by setdistPerCountFactor()
Definition JEncoderPWMAbsolute.h:182
long intTurns()
how many full turns the encoder has made.
Definition JEncoderPWMAbsolute.h:156
int rawReading()
Definition JEncoderPWMAbsolute.h:146
void setDistPerCountFactor(float _factor)
Definition JEncoderPWMAbsolute.h:206
struct for holding parameters for reading PWM Absolute Encoders
Definition JEncoderPWMAbsolute.h:28
uint16_t MIN_STEPS
number of steps corresponding to min angle. MAX_STEPS=MIN_STEPS+RESOLUTION
Definition JEncoderPWMAbsolute.h:40
uint16_t MAX_CYCLE
longest time (microseconds) between falling edges that will be considered a valid encoder signal (hig...
Definition JEncoderPWMAbsolute.h:52
uint16_t RESOLUTION
resolution of encoder data (pwm steps)
Definition JEncoderPWMAbsolute.h:32
bool dataState
HIGH or LOW, which state when pulse length increasing=increasing angle.
Definition JEncoderPWMAbsolute.h:44
uint16_t MIN_CYCLE
shortest time (microseconds) between falling edges that will be considered a valid encoder signal (lo...
Definition JEncoderPWMAbsolute.h:48
uint16_t PWM_STEPS
number of total clock steps in pwm output
Definition JEncoderPWMAbsolute.h:36