1#ifndef J_ENCODER_AS5048B_I2C_H
2#define J_ENCODER_AS5048B_I2C_H
21 float distPerCountFactor;
24 uint16_t lastVelAngle;
26 unsigned long lastVelTimeMicros;
27 unsigned long velEnoughTime;
28 unsigned long velEnoughTicks;
29 bool recognizeOutOfRange;
31 const byte AS5048B_ZEROMSB_REG = 0x16;
32 const byte AS5048B_ZEROLSB_REG = 0x17;
33 const byte AS5048B_ANGLMSB_REG = 0xFE;
34 const byte AS5048B_GAIN_REG = 0xFA;
36 void writeRegister8(uint8_t reg, uint8_t value)
38 wire->beginTransmission(address);
41 wire->endTransmission();
44 uint16_t readRegister14(uint8_t reg)
47 uint16_t readValue = 0;
50 wire->beginTransmission(address);
52 wire->endTransmission(
false);
54 wire->requestFrom(address, (uint8_t)2);
55 readArray[0] = wire->read();
56 readArray[1] = wire->read();
58 readValue = (((uint16_t)readArray[0]) << 6);
59 readValue += (readArray[1] & 0b111111);
63 uint8_t readRegister8(uint8_t reg)
67 wire->beginTransmission(address);
69 wire->endTransmission(
false);
71 wire->requestFrom(address, (uint8_t)1);
72 readValue = (uint8_t)wire->read();
77 void writeToZeroRegister(uint16_t val)
79 writeRegister8(AS5048B_ZEROMSB_REG, (uint8_t)(val >> 6));
80 writeRegister8(AS5048B_ZEROLSB_REG, (uint8_t)(val & 0b00111111));
85 return readRegister14(AS5048B_ANGLMSB_REG);
102 JEncoderAS5048bI2C(
bool _reverse =
false,
float _distPerCountFactor = 1.0, uint8_t _address = 0x40,
unsigned long _velEnoughTime = 0,
unsigned long _velEnoughTicks = 0,
bool _recognizeOutOfRange =
true)
119 lastVelTimeMicros = 0;
120 velEnoughTime = _velEnoughTime;
121 velEnoughTicks = _velEnoughTicks;
122 recognizeOutOfRange = _recognizeOutOfRange;
131 return readRegister8(AS5048B_GAIN_REG);
152 if (abs((int16_t)angle - (int16_t)lastAngle) >
STEPS_PER_TURN / 2) {
153 if (angle > lastAngle) {
160 long velDist = ((int16_t)angle - (int16_t)lastVelAngle) + (turns - lastVelTurns) *
STEPS_PER_TURN;
161 if (micros() - lastVelTimeMicros > velEnoughTime || abs(velDist) > velEnoughTicks) {
162 velocity = (double)1000000.0 * velDist / (micros() - lastVelTimeMicros) * distPerCountFactor * reverse;
163 lastVelTimeMicros = micros();
165 lastVelAngle = angle;
166 lastVelTurns = turns;
182 writeToZeroRegister(0);
183 uint16_t newZero = readRegister14(AS5048B_ANGLMSB_REG);
184 writeToZeroRegister(newZero);
193 writeToZeroRegister(zeroAngle);
203 return angle * reverse;
215 return (gain != 0) && (gain != 255);
225 return turns * reverse;
260 return (int32_t)((turns *
STEPS_PER_TURN + angle) * reverse) * distPerCountFactor;
265 return distPerCountFactor;
288 velEnoughTime = _velEnoughTime;
292 velEnoughTicks = _velEnoughTicks;
299 recognizeOutOfRange = _recognizeOutOfRange;
reads a type of absolute encoder https://ams.com/as0548b (uses I2C)
Definition JEncoderAS5048bI2C.h:12
const unsigned int STEPS_PER_TURN
Definition JEncoderAS5048bI2C.h:89
long getCounter()
returns how far the encoder has turned from the zero position
Definition JEncoderAS5048bI2C.h:253
float getPos()
returns how far the encoder has turned from the zero position converted to distance
Definition JEncoderAS5048bI2C.h:258
static const byte AS5048B_DEFAULT_ADDRESS
Definition JEncoderAS5048bI2C.h:90
long zeroCounter()
reset the counter of how far the encoder has turned
Definition JEncoderAS5048bI2C.h:228
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()
Definition JEncoderAS5048bI2C.h:102
void setEncoderZero()
The current angle of the sensor is read and set to zero.
Definition JEncoderAS5048bI2C.h:180
int rawReading()
Definition JEncoderAS5048bI2C.h:201
void setEncoderZero(int zeroAngle)
A custom (repeatable) angle can be set for what the sensor calls zero relative to default zero.
Definition JEncoderAS5048bI2C.h:190
void run()
communication is done over I2C and requires constant polling instead of being able to use interrupts
Definition JEncoderAS5048bI2C.h:147
float getDistPerCountFactor()
returns a conversion factor between encoder ticks and distance that can be set for the encoder
Definition JEncoderAS5048bI2C.h:263
long intTurns()
how many full turns the encoder has made.
Definition JEncoderAS5048bI2C.h:223
bool isVelNew()
could be useful for only recalculating a control loop if there's new velocity data
Definition JEncoderAS5048bI2C.h:282
void setVelEnoughTicks(unsigned long _velEnoughTicks)
Definition JEncoderAS5048bI2C.h:290
uint8_t getAutoGain()
read the gain setting of the sensor
Definition JEncoderAS5048bI2C.h:129
void setVelEnoughTime(unsigned long _velEnoughTime)
Definition JEncoderAS5048bI2C.h:286
void setDistPerCountFactor(float _factor)
Definition JEncoderAS5048bI2C.h:272
long zeroCounter(bool _resetAngle)
reset the counter of how far the encoder has turned
Definition JEncoderAS5048bI2C.h:238
bool isMagnetInRange()
is the magnet in the optimal position Unlike other functions, this function does not rely on run()
Definition JEncoderAS5048bI2C.h:212
void setRecognizeOutOfRange(bool _recognizeOutOfRange)
Definition JEncoderAS5048bI2C.h:297
void useCustomWire(TwoWire &_wire)
Set what Wire (I2C) bus to use (for microcontrollers with more than one)
Definition JEncoderAS5048bI2C.h:138
bool hasDirection()
can this encoder measure direction or just speed
Definition JEncoderAS5048bI2C.h:277
float getVel()
calculates velocity in distance per second where distance was set by setdistPerCountFactor()
Definition JEncoderAS5048bI2C.h:248
defines common interface for JEncoder
Definition JEncoder.h:33