JMotor
Loading...
Searching...
No Matches
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
JEncoderSingle Class Reference

reads a single channel (incremental) encoder More...

#include <JEncoderSingle.h>

Inheritance diagram for JEncoderSingle:
Inheritance graph
[legend]
Collaboration diagram for JEncoderSingle:
Collaboration graph
[legend]

Public Member Functions

bool getRev ()
 get whether the encoder is told it's going backwards currently
 
void setRev (bool _rev)
 set which direction the encoder is moving (if you have external information about direction (like motor power) and want getVel and getDist to go the right direction even with this directionless encoder)
 
long zeroCounter ()
 reset the counter of how far the encoder has turned
 
float getVel ()
 calculates velocity in distance per second where distance was set by setdistPerCountFactor()
 
long getCounter ()
 returns how far the encoder has turned from the zero position
 
float getPos ()
 returns how far the encoder has turned from the zero position converted to distance
 
float getDistPerCountFactor ()
 returns a conversion factor between encoder ticks and distance that can be set for the encoder
 
void setDistPerCountFactor (float _factor)
 set the conversion factor between encoder ticks and distance
 
void setReverse (bool _reverse)
 reverse readings of encoder
 
bool hasDirection ()
 can this encoder measure direction or just speed
 
bool isVelNew ()
 could be useful for only recalculating a control loop if there's new velocity data
 
void run ()
 if an encoder needs to have some code called each loop (like absolute encoder polling encoder and calculating amount turned)
 
void encoderISR (void)
 

Protected Member Functions

 JEncoderSingle (byte _encoderPin, float _distPerCountFactor, bool _reverse, unsigned long _slowestIntervalMicros, unsigned long _switchBounceIntervalMicros, byte _interruptType)
 constructor, sets pins and settings
 
virtual void setUpInterrupts (void(*_isrPointer)(void))
 set up pins and interrupts
 
virtual void turnOffInterrupts ()
 disable interrupts and stop monitoring encoder
 

Protected Attributes

unsigned char encoderPin
 
byte interruptType
 

Detailed Description

reads a single channel (incremental) encoder

direction can't be calculated with a single channel encoder, only speed can be.
speed calculation is done by measuring time between 2 encoder ticks (not between every tick since encoders may not have evenly spaced ticks)
velocity is set to zero if the encoder has not turned in slowestIntervalMicros

Note
don't use this class directly, use a subclass

Constructor & Destructor Documentation

◆ JEncoderSingle()

JEncoderSingle::JEncoderSingle ( byte  _encoderPin,
float  _distPerCountFactor,
bool  _reverse,
unsigned long  _slowestIntervalMicros,
unsigned long  _switchBounceIntervalMicros,
byte  _interruptType 
)
inlineprotected

constructor, sets pins and settings

Parameters
_encoderPinencoder input pin
_distPerCountFactorconversion factor for getting distance in an actual unit
_reversefalse(default)
_slowestIntervalMicrosafter this many microseconds without an encoder tick velocity is set to zero.
_switchBounceIntervalMicrosignore additional pulses for this many microseconds after each pulse
_interruptTypeCHANGE, RISING, FALLING

Member Function Documentation

◆ encoderISR()

void JEncoderSingle::encoderISR ( void  )
inline

◆ getCounter()

long JEncoderSingle::getCounter ( )
inlinevirtual

returns how far the encoder has turned from the zero position

Note
remember that variables overflow
Return values
(long)encoder ticks

Reimplemented from JEncoder.

◆ getDistPerCountFactor()

float JEncoderSingle::getDistPerCountFactor ( )
inlinevirtual

returns a conversion factor between encoder ticks and distance that can be set for the encoder

Note
default is 1.0
Return values
(float)distPerCountFactor

Reimplemented from JEncoder.

◆ getPos()

float JEncoderSingle::getPos ( )
inlinevirtual

returns how far the encoder has turned from the zero position converted to distance

Note
remember that variables overflow (counter) and that floats have limited precision
Return values
(float)encoder ticks converted to distance

Reimplemented from JEncoder.

◆ getRev()

bool JEncoderSingle::getRev ( )
inline

get whether the encoder is told it's going backwards currently

Return values
(bool)

◆ getVel()

float JEncoderSingle::getVel ( )
inlinevirtual

calculates velocity in distance per second where distance was set by setdistPerCountFactor()

Return values
(float)velocity

Reimplemented from JEncoder.

◆ hasDirection()

bool JEncoderSingle::hasDirection ( )
inlinevirtual

can this encoder measure direction or just speed

Return values
(bool)true = can measure direction

Reimplemented from JEncoder.

◆ isVelNew()

bool JEncoderSingle::isVelNew ( )
inlinevirtual

could be useful for only recalculating a control loop if there's new velocity data

Return values
(bool)true if velocity has changed since this function was last called

Reimplemented from JEncoder.

◆ run()

void JEncoderSingle::run ( )
inlinevirtual

if an encoder needs to have some code called each loop (like absolute encoder polling encoder and calculating amount turned)

Note
interrupt based encoders don't need this to do anything and can define an empty function

Reimplemented from JEncoder.

◆ setDistPerCountFactor()

void JEncoderSingle::setDistPerCountFactor ( float  _factor)
inlinevirtual

set the conversion factor between encoder ticks and distance

Parameters
_factor(float)

Reimplemented from JEncoder.

◆ setRev()

void JEncoderSingle::setRev ( bool  _rev)
inlinevirtual

set which direction the encoder is moving (if you have external information about direction (like motor power) and want getVel and getDist to go the right direction even with this directionless encoder)

Parameters
_rev(bool) false=forwards true=backwards

Reimplemented from JEncoder.

◆ setReverse()

void JEncoderSingle::setReverse ( bool  _reverse)
inline

reverse readings of encoder

Parameters
_reversewhether to invert sign of readings
Return values
None

◆ setUpInterrupts()

virtual void JEncoderSingle::setUpInterrupts ( void(*)(void)  _isrPointer)
protectedvirtual

set up pins and interrupts

Parameters
_isrPointerglobal function that calls internal ISRA to use with enableInterrupt

Reimplemented in JEncoderSingleAttachInterrupt, and JEncoderSinglePinChange.

◆ turnOffInterrupts()

virtual void JEncoderSingle::turnOffInterrupts ( )
protectedvirtual

disable interrupts and stop monitoring encoder

Note
use setUpInterrupts to start encoder again

Reimplemented in JEncoderSingleAttachInterrupt, and JEncoderSinglePinChange.

◆ zeroCounter()

long JEncoderSingle::zeroCounter ( )
inlinevirtual

reset the counter of how far the encoder has turned

Return values
(long)returns value of counter before it is reset

Reimplemented from JEncoder.

Member Data Documentation

◆ encoderPin

unsigned char JEncoderSingle::encoderPin
protected

◆ interruptType

byte JEncoderSingle::interruptType
protected

The documentation for this class was generated from the following file: