Skip to content

Commit d5f90ad

Browse files
authored
Fix #65, getCumulativePosition() + some more (#68)
- 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.
1 parent 20b40c1 commit d5f90ad

File tree

11 files changed

+649
-69
lines changed

11 files changed

+649
-69
lines changed

AS5600.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: AS56000.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.6.1
4+
// VERSION: 0.6.2
55
// PURPOSE: Arduino library for AS5600 magnetic rotation meter
66
// DATE: 2022-05-28
77
// URL: https://github.com/RobTillaart/AS5600
@@ -303,8 +303,9 @@ uint8_t AS5600::getWatchDog()
303303
//
304304
uint16_t AS5600::rawAngle()
305305
{
306-
int16_t value = readReg2(AS5600_RAW_ANGLE) & 0x0FFF;
307-
if (_offset > 0) value = (value + _offset) & 0x0FFF;
306+
int16_t value = readReg2(AS5600_RAW_ANGLE);
307+
if (_offset > 0) value += _offset;
308+
value &= 0x0FFF;
308309

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

318319
uint16_t AS5600::readAngle()
319320
{
320-
uint16_t value = readReg2(AS5600_ANGLE) & 0x0FFF;
321-
if (_offset > 0) value = (value + _offset) & 0x0FFF;
321+
uint16_t value = readReg2(AS5600_ANGLE);
322+
if (_offset > 0) value += _offset;
323+
value &= 0x0FFF;
322324

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

339341
uint16_t offset = round(degrees * AS5600_DEGREES_TO_RAW);
340-
offset &= 4095;
341-
if (neg) offset = 4096 - offset;
342+
offset &= 0x0FFF;
343+
if (neg) offset = (4096 - offset) & 0x0FFF;
342344
_offset = offset;
343345
return true;
344346
}
@@ -462,7 +464,7 @@ float AS5600::getAngularSpeed(uint8_t mode)
462464
//
463465
int32_t AS5600::getCumulativePosition()
464466
{
465-
int16_t value = readReg2(AS5600_ANGLE) & 0x0FFF;
467+
int16_t value = readAngle();
466468
if (_error != AS5600_OK) return _position;
467469

468470
// whole rotation CW?
@@ -487,9 +489,8 @@ int32_t AS5600::getCumulativePosition()
487489
int32_t AS5600::getRevolutions()
488490
{
489491
int32_t p = _position >> 12; // divide by 4096
492+
if (p < 0) p++; // correct negative values, See #65
490493
return p;
491-
// if (p < 0) p++;
492-
// return p;
493494
}
494495

495496

AS5600.h

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// FILE: AS5600.h
44
// AUTHOR: Rob Tillaart
5-
// VERSION: 0.6.1
5+
// VERSION: 0.6.2
66
// PURPOSE: Arduino library for AS5600 magnetic rotation meter
77
// DATE: 2022-05-28
88
// URL: https://github.com/RobTillaart/AS5600
@@ -12,7 +12,7 @@
1212
#include "Wire.h"
1313

1414

15-
#define AS5600_LIB_VERSION (F("0.6.1"))
15+
#define AS5600_LIB_VERSION (F("0.6.2"))
1616

1717

1818
// default addresses
@@ -102,7 +102,8 @@ class AS5600
102102
AS5600(TwoWire *wire = &Wire);
103103

104104
bool begin(uint8_t directionPin = AS5600_SW_DIRECTION_PIN);
105-
bool isConnected();
105+
// made virtual, see #66
106+
virtual bool isConnected();
106107

107108
// address = fixed 0x36 for AS5600,
108109
// = default 0x40 for AS5600L
@@ -237,10 +238,11 @@ class AS5600
237238

238239

239240
protected:
240-
uint8_t readReg(uint8_t reg);
241-
uint16_t readReg2(uint8_t reg);
242-
uint8_t writeReg(uint8_t reg, uint8_t value);
243-
uint8_t writeReg2(uint8_t reg, uint16_t value);
241+
// made virtual, see #66
242+
virtual uint8_t readReg(uint8_t reg);
243+
virtual uint16_t readReg2(uint8_t reg);
244+
virtual uint8_t writeReg(uint8_t reg, uint8_t value);
245+
virtual uint8_t writeReg2(uint8_t reg, uint16_t value);
244246

245247
uint8_t _address = AS5600_DEFAULT_ADDRESS;
246248
uint8_t _directionPin = 255;

CHANGELOG.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,23 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
55
and this project adheres to [Semantic Versioning](http://semver.org/).
66

77

8+
## [0.6.2] - 2024-10-04
9+
- fix #65, make **getCumulativePosition()** direction aware.
10+
- optimize **readAngle()** and **rawAngle()**.
11+
- fix negative values in **getRevolutions()**.
12+
- fix #66, make I2C functions virtual to improve derived class.
13+
- fix **setOffset()** to not set offset to 360.
14+
- add unit test for offset -0.01 and 360.0 (both should become 0.0).
15+
- add **AS5600_output_speedtest.ino**, thanks to Pollyscracker.
16+
- update readme.md.
17+
18+
819
## [0.6.1] - 2024-03-31
920
- improve **getCumulativePosition()**, catch I2C error, see #62
1021
- update readme.md (incl reorder future work).
1122
- update GitHub actions
1223
- minor edits
1324

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

0 commit comments

Comments
 (0)