1#ifndef JENCODER_AS5600I2C_H
2#define JENCODER_AS5600I2C_H
20 float distPerCountFactor;
23 uint16_t lastVelAngle;
25 unsigned long lastVelTimeMicros;
26 unsigned long velEnoughTime;
27 unsigned long velEnoughTicks;
28 bool recognizeOutOfRange;
46 wire->beginTransmission(address);
49 wire->endTransmission();
55 wire->beginTransmission(0x36);
57 wire->write((uint8_t)((value >> 8) & 0b1111));
58 wire->write((uint8_t)(value & 0b11111111));
59 wire->endTransmission();
67 wire->beginTransmission(0x36);
69 wire->endTransmission(
false);
71 wire->requestFrom(0x36, 2);
72 readArray[0] = wire->read();
73 readArray[1] = wire->read();
75 uint16_t readValue = ((readArray[0] & 0b1111) << 8) | readArray[1];
83 wire->beginTransmission(address);
85 wire->endTransmission(
false);
87 wire->requestFrom(address, (uint8_t)1);
88 readValue = (uint8_t)wire->read();
106 JEncoderAS5600I2C(
bool _reverse =
false,
float _distPerCountFactor = 1.0, uint16_t _zeroAngle = 0,
unsigned long _velEnoughTime = 0,
unsigned long _velEnoughTicks = 0,
bool _recognizeOutOfRange =
true)
123 lastVelTimeMicros = 0;
124 velEnoughTime = _velEnoughTime;
125 velEnoughTicks = _velEnoughTicks;
126 recognizeOutOfRange = _recognizeOutOfRange;
163 if (abs((int16_t)angle - (int16_t)lastAngle) >
STEPS_PER_TURN / 2) {
164 if (angle > lastAngle) {
171 long velDist = ((int16_t)angle - (int16_t)lastVelAngle) + (turns - lastVelTurns) *
STEPS_PER_TURN;
172 if (micros() - lastVelTimeMicros > velEnoughTime || abs(velDist) > velEnoughTicks) {
173 velocity = (double)1000000.0 * velDist / (micros() - lastVelTimeMicros) * distPerCountFactor * reverse;
174 lastVelTimeMicros = micros();
176 lastVelAngle = angle;
177 lastVelTurns = turns;
234 return turns * reverse;
273 return (int32_t)((turns *
STEPS_PER_TURN + angle) * reverse) * distPerCountFactor;
278 return distPerCountFactor;
301 velEnoughTime = _velEnoughTime;
305 velEnoughTicks = _velEnoughTicks;
312 recognizeOutOfRange = _recognizeOutOfRange;
reads a type of absolute encoder https://ams.com/en/as5600 (uses I2C)
Definition JEncoderAS5600I2C.h:11
float getPos()
returns how far the encoder has turned from the zero position converted to distance
Definition JEncoderAS5600I2C.h:271
uint16_t readRegister12(uint8_t reg)
Definition JEncoderAS5600I2C.h:62
long intTurns()
how many full turns the encoder has made.
Definition JEncoderAS5600I2C.h:232
void setVelEnoughTime(unsigned long _velEnoughTime)
Definition JEncoderAS5600I2C.h:299
void setRecognizeOutOfRange(bool _recognizeOutOfRange)
Definition JEncoderAS5600I2C.h:310
uint8_t readRegister8(uint8_t reg)
Definition JEncoderAS5600I2C.h:79
bool isMagnetInRange()
is the magnet in the optimal position Unlike other functions, this function does not rely on run()
Definition JEncoderAS5600I2C.h:222
uint16_t readAngle()
angle in raw units, but adjusted for custom zero
Definition JEncoderAS5600I2C.h:140
void writeRegister8(uint8_t reg, uint8_t value)
Definition JEncoderAS5600I2C.h:44
void run()
communication is done over I2C and requires constant polling instead of being able to use interrupts
Definition JEncoderAS5600I2C.h:158
const byte RAWANGLEMSB_REG
Definition JEncoderAS5600I2C.h:37
const byte BURN_REG
Definition JEncoderAS5600I2C.h:42
long zeroCounter(bool _resetAngle)
reset the counter of how far the encoder has turned
Definition JEncoderAS5600I2C.h:251
const byte MAGNITUDE_REG
Definition JEncoderAS5600I2C.h:41
float getDistPerCountFactor()
returns a conversion factor between encoder ticks and distance that can be set for the encoder
Definition JEncoderAS5600I2C.h:276
long getCounter()
returns how far the encoder has turned from the zero position
Definition JEncoderAS5600I2C.h:266
const byte MPOSMSB_REG
Definition JEncoderAS5600I2C.h:34
void writeRegister12(uint8_t reg, uint16_t value)
Definition JEncoderAS5600I2C.h:52
const byte ANGLEMSB_REG
Definition JEncoderAS5600I2C.h:38
const byte CONF_REG
Definition JEncoderAS5600I2C.h:36
const byte AGC_REG
Definition JEncoderAS5600I2C.h:40
static const byte AS5048B_DEFAULT_ADDRESS
Definition JEncoderAS5600I2C.h:94
const byte ZPOSMSB_REG
Definition JEncoderAS5600I2C.h:33
void setVelEnoughTicks(unsigned long _velEnoughTicks)
Definition JEncoderAS5600I2C.h:303
JEncoderAS5600I2C(bool _reverse=false, float _distPerCountFactor=1.0, uint16_t _zeroAngle=0, 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 JEncoderAS5600I2C.h:106
const byte STATUS_REG
Definition JEncoderAS5600I2C.h:39
void setEncoderZero()
set current angle as zero
Definition JEncoderAS5600I2C.h:201
bool hasDirection()
can this encoder measure direction or just speed
Definition JEncoderAS5600I2C.h:290
void useCustomWire(TwoWire &_wire)
Set what Wire (I2C) bus to use (for microcontrollers with more than one)
Definition JEncoderAS5600I2C.h:149
long zeroCounter()
reset the counter of how far the encoder has turned
Definition JEncoderAS5600I2C.h:241
const unsigned int STEPS_PER_TURN
Definition JEncoderAS5600I2C.h:93
float getVel()
calculates velocity in distance per second where distance was set by setdistPerCountFactor()
Definition JEncoderAS5600I2C.h:261
uint8_t getAutoGain()
Definition JEncoderAS5600I2C.h:132
void setEncoderZero(int _zeroAngle)
A custom (repeatable) angle can be set for what the sensor calls zero relative to default zero.
Definition JEncoderAS5600I2C.h:192
void setDistPerCountFactor(float _factor)
Definition JEncoderAS5600I2C.h:285
const byte MANG_REG
Definition JEncoderAS5600I2C.h:35
bool isVelNew()
could be useful for only recalculating a control loop if there's new velocity data
Definition JEncoderAS5600I2C.h:295
const byte ZMCO_REG
Definition JEncoderAS5600I2C.h:32
int rawReading()
Definition JEncoderAS5600I2C.h:211
defines common interface for JEncoder
Definition JEncoder.h:33