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

reads a type of absolute encoder https://ams.com/as0548b (uses I2C) More...

#include <JEncoderAS5048bI2C.h>

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

Public Member Functions

 JEncoderAS5048bI2C (bool _reverse=false, float _distPerCountFactor=1.0, uint8_t _address=0x40, unsigned long _velEnoughTime=0, unsigned long _velEnoughTicks=0, bool _recognizeOutOfRange=true)
 sets pins and settings for reading the encoder, remember to use Wire.begin()
 
uint8_t getAutoGain ()
 read the gain setting of the sensor
 
void useCustomWire (TwoWire &_wire)
 Set what Wire (I2C) bus to use (for microcontrollers with more than one)
 
void run ()
 communication is done over I2C and requires constant polling instead of being able to use interrupts
 
void setEncoderZero ()
 The current angle of the sensor is read and set to zero.
 
void setEncoderZero (int zeroAngle)
 A custom (repeatable) angle can be set for what the sensor calls zero relative to default zero.
 
int rawReading ()
 
bool isMagnetInRange ()
 is the magnet in the optimal position Unlike other functions, this function does not rely on run()
 
long intTurns ()
 how many full turns the encoder has made.
 
long zeroCounter ()
 reset the counter of how far the encoder has turned
 
long zeroCounter (bool _resetAngle)
 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)
 
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 setVelEnoughTime (unsigned long _velEnoughTime)
 
void setVelEnoughTicks (unsigned long _velEnoughTicks)
 
void setRecognizeOutOfRange (bool _recognizeOutOfRange)
 
- Public Member Functions inherited from JEncoder
virtual void setRev (bool _rev)
 empty function for directionless encoders to override
 

Public Attributes

const unsigned int STEPS_PER_TURN = 16384
 

Static Public Attributes

static const byte AS5048B_DEFAULT_ADDRESS = 0x40
 

Detailed Description

reads a type of absolute encoder https://ams.com/as0548b (uses I2C)

based on work by sosandroid https://github.com/sosandroid/AMS_AS5048B

Note
remember to add pull up resistors on I2C lines (~4.7k for 5v, ~2.4k for 3.3v microcontrollers).

Constructor & Destructor Documentation

◆ JEncoderAS5048bI2C()

JEncoderAS5048bI2C::JEncoderAS5048bI2C ( bool  _reverse = false,
float  _distPerCountFactor = 1.0,
uint8_t  _address = 0x40,
unsigned long  _velEnoughTime = 0,
unsigned long  _velEnoughTicks = 0,
bool  _recognizeOutOfRange = true 
)
inline

sets pins and settings for reading the encoder, remember to use Wire.begin()

Note
remember to use Wire.begin()
Parameters
_reverse(bool) reverse positive direction, default=false
_distPerCountFactor(float) for the purposes of setting this factor a "count" is considered a full revolution of the absolute encoder
_address(byte) I2C address of encoder
_velEnoughTime(default=0, no limit) shortest interval in microseconds between velocity calculations, if run() is called faster the calculation will wait to run
_velEnoughTicks(default=0, no limit) if the encoder turns more than this number of steps velocity calculations will be done even if velEnoughTime hasn't been reached
_recognizeOutOfRange(boolean, default=true) set velocity to zero and stop updating position if encoder reports that it's out of range

Member Function Documentation

◆ getAutoGain()

uint8_t JEncoderAS5048bI2C::getAutoGain ( )
inline

read the gain setting of the sensor

Note
bad if 255 (magnet too far) or 0 (magnet too close)

◆ getCounter()

long JEncoderAS5048bI2C::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 JEncoderAS5048bI2C::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 JEncoderAS5048bI2C::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.

◆ getVel()

float JEncoderAS5048bI2C::getVel ( )
inlinevirtual

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

Return values
(float)velocity

Reimplemented from JEncoder.

◆ hasDirection()

bool JEncoderAS5048bI2C::hasDirection ( )
inlinevirtual

can this encoder measure direction or just speed

Return values
(bool)true = can measure direction

Reimplemented from JEncoder.

◆ intTurns()

long JEncoderAS5048bI2C::intTurns ( )
inline

how many full turns the encoder has made.

Note
this and rawReading can be used as an alternative to getPos if the limited precision of a float is a concern
Return values
(long)how far the encoder has turned

◆ isMagnetInRange()

bool JEncoderAS5048bI2C::isMagnetInRange ( )
inline

is the magnet in the optimal position Unlike other functions, this function does not rely on run()

Note
if this is false, the encoder may still be providing angle information
Return values
(bool)

◆ isVelNew()

bool JEncoderAS5048bI2C::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.

◆ rawReading()

int JEncoderAS5048bI2C::rawReading ( )
inline
Note
divide by STEPS_PER_TURN to get fraction of full turn
Return values
theraw angle reading from the sensor (0 to 16,383) (but negative if reverse is true)

◆ run()

void JEncoderAS5048bI2C::run ( )
inlinevirtual

communication is done over I2C and requires constant polling instead of being able to use interrupts

Note
call this function as frequently as possible or this code can't keep track of how many times the encoder has turned

Reimplemented from JEncoder.

◆ setDistPerCountFactor()

void JEncoderAS5048bI2C::setDistPerCountFactor ( float  _factor)
inlinevirtual
Note
for the purposes of setting this factor a "count" is considered a full revolution of the absolute encoder it is converted to actual counts within this function

Reimplemented from JEncoder.

◆ setEncoderZero() [1/2]

void JEncoderAS5048bI2C::setEncoderZero ( )
inline

The current angle of the sensor is read and set to zero.

◆ setEncoderZero() [2/2]

void JEncoderAS5048bI2C::setEncoderZero ( int  zeroAngle)
inline

A custom (repeatable) angle can be set for what the sensor calls zero relative to default zero.

Parameters
zeroAngle(int) [0, STEPS_PER_TURN)

◆ setRecognizeOutOfRange()

void JEncoderAS5048bI2C::setRecognizeOutOfRange ( bool  _recognizeOutOfRange)
inline
Parameters
_recognizeOutOfRange(boolean, default=true) set velocity to zero and stop updating position if encoder reports that it's out of range

◆ setVelEnoughTicks()

void JEncoderAS5048bI2C::setVelEnoughTicks ( unsigned long  _velEnoughTicks)
inline

◆ setVelEnoughTime()

void JEncoderAS5048bI2C::setVelEnoughTime ( unsigned long  _velEnoughTime)
inline

◆ useCustomWire()

void JEncoderAS5048bI2C::useCustomWire ( TwoWire &  _wire)
inline

Set what Wire (I2C) bus to use (for microcontrollers with more than one)

Parameters
_wire(TwoWire)

◆ zeroCounter() [1/2]

long JEncoderAS5048bI2C::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.

◆ zeroCounter() [2/2]

long JEncoderAS5048bI2C::zeroCounter ( bool  _resetAngle)
inline

reset the counter of how far the encoder has turned

Parameters
_resetAngle(bool) true=zero absolute angle measurement and set current position to zero. false= zero out number of turns, but angle stays relative to absolute encoder's zero point
Return values
(long)returns value of counter before it is reset

Member Data Documentation

◆ AS5048B_DEFAULT_ADDRESS

const byte JEncoderAS5048bI2C::AS5048B_DEFAULT_ADDRESS = 0x40
static

◆ STEPS_PER_TURN

const unsigned int JEncoderAS5048bI2C::STEPS_PER_TURN = 16384

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