Skip to content

Commit

Permalink
Fix #65, getCumulativePosition() + some more (#68)
Browse files Browse the repository at this point in the history
- fix #65, make **getCumulativePosition()** direction aware.
- refactor**readAngle()** and **rawAngle()**.
- fix negative values in **getRevolutions()**.
- fix #66, make I2C functions virtual to improve derived class.
- fix **setOffset()** to not set offset to 360.
- add unit test for offset -0.01 and 360.0 (both should become 0.0).
- add **AS5600_output_speedtest.ino**, thanks to Pollyscracker.
- update readme.md.
  • Loading branch information
RobTillaart authored Oct 5, 2024
1 parent 20b40c1 commit d5f90ad
Show file tree
Hide file tree
Showing 11 changed files with 649 additions and 69 deletions.
21 changes: 11 additions & 10 deletions AS5600.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//
// FILE: AS56000.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.6.1
// VERSION: 0.6.2
// PURPOSE: Arduino library for AS5600 magnetic rotation meter
// DATE: 2022-05-28
// URL: https://github.com/RobTillaart/AS5600
Expand Down Expand Up @@ -303,8 +303,9 @@ uint8_t AS5600::getWatchDog()
//
uint16_t AS5600::rawAngle()
{
int16_t value = readReg2(AS5600_RAW_ANGLE) & 0x0FFF;
if (_offset > 0) value = (value + _offset) & 0x0FFF;
int16_t value = readReg2(AS5600_RAW_ANGLE);
if (_offset > 0) value += _offset;
value &= 0x0FFF;

if ((_directionPin == AS5600_SW_DIRECTION_PIN) &&
(_direction == AS5600_COUNTERCLOCK_WISE))
Expand All @@ -317,8 +318,9 @@ uint16_t AS5600::rawAngle()

uint16_t AS5600::readAngle()
{
uint16_t value = readReg2(AS5600_ANGLE) & 0x0FFF;
if (_offset > 0) value = (value + _offset) & 0x0FFF;
uint16_t value = readReg2(AS5600_ANGLE);
if (_offset > 0) value += _offset;
value &= 0x0FFF;

if ((_directionPin == AS5600_SW_DIRECTION_PIN) &&
(_direction == AS5600_COUNTERCLOCK_WISE))
Expand All @@ -337,8 +339,8 @@ bool AS5600::setOffset(float degrees)
if (neg) degrees = -degrees;

uint16_t offset = round(degrees * AS5600_DEGREES_TO_RAW);
offset &= 4095;
if (neg) offset = 4096 - offset;
offset &= 0x0FFF;
if (neg) offset = (4096 - offset) & 0x0FFF;
_offset = offset;
return true;
}
Expand Down Expand Up @@ -462,7 +464,7 @@ float AS5600::getAngularSpeed(uint8_t mode)
//
int32_t AS5600::getCumulativePosition()
{
int16_t value = readReg2(AS5600_ANGLE) & 0x0FFF;
int16_t value = readAngle();
if (_error != AS5600_OK) return _position;

// whole rotation CW?
Expand All @@ -487,9 +489,8 @@ int32_t AS5600::getCumulativePosition()
int32_t AS5600::getRevolutions()
{
int32_t p = _position >> 12; // divide by 4096
if (p < 0) p++; // correct negative values, See #65
return p;
// if (p < 0) p++;
// return p;
}


Expand Down
16 changes: 9 additions & 7 deletions AS5600.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// FILE: AS5600.h
// AUTHOR: Rob Tillaart
// VERSION: 0.6.1
// VERSION: 0.6.2
// PURPOSE: Arduino library for AS5600 magnetic rotation meter
// DATE: 2022-05-28
// URL: https://github.com/RobTillaart/AS5600
Expand All @@ -12,7 +12,7 @@
#include "Wire.h"


#define AS5600_LIB_VERSION (F("0.6.1"))
#define AS5600_LIB_VERSION (F("0.6.2"))


// default addresses
Expand Down Expand Up @@ -102,7 +102,8 @@ class AS5600
AS5600(TwoWire *wire = &Wire);

bool begin(uint8_t directionPin = AS5600_SW_DIRECTION_PIN);
bool isConnected();
// made virtual, see #66
virtual bool isConnected();

// address = fixed 0x36 for AS5600,
// = default 0x40 for AS5600L
Expand Down Expand Up @@ -237,10 +238,11 @@ class AS5600


protected:
uint8_t readReg(uint8_t reg);
uint16_t readReg2(uint8_t reg);
uint8_t writeReg(uint8_t reg, uint8_t value);
uint8_t writeReg2(uint8_t reg, uint16_t value);
// made virtual, see #66
virtual uint8_t readReg(uint8_t reg);
virtual uint16_t readReg2(uint8_t reg);
virtual uint8_t writeReg(uint8_t reg, uint8_t value);
virtual uint8_t writeReg2(uint8_t reg, uint16_t value);

uint8_t _address = AS5600_DEFAULT_ADDRESS;
uint8_t _directionPin = 255;
Expand Down
12 changes: 11 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,23 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.6.2] - 2024-10-04
- fix #65, make **getCumulativePosition()** direction aware.
- optimize **readAngle()** and **rawAngle()**.
- fix negative values in **getRevolutions()**.
- fix #66, make I2C functions virtual to improve derived class.
- fix **setOffset()** to not set offset to 360.
- add unit test for offset -0.01 and 360.0 (both should become 0.0).
- add **AS5600_output_speedtest.ino**, thanks to Pollyscracker.
- update readme.md.


## [0.6.1] - 2024-03-31
- improve **getCumulativePosition()**, catch I2C error, see #62
- update readme.md (incl reorder future work).
- update GitHub actions
- minor edits


## [0.6.0] - 2024-01-25
- add experimental error handling
- add **int lastError()** so user can check the status of last I2C actions.
Expand Down
Loading

0 comments on commit d5f90ad

Please sign in to comment.