From a6e2af6b80fb8138ebae59d0ef18f92d0301d64d Mon Sep 17 00:00:00 2001 From: rob tillaart Date: Thu, 22 Dec 2022 14:03:01 +0100 Subject: [PATCH] 0.3.4 AS5600 --- libraries/AS5600/AS5600.cpp | 33 +++++++-------- libraries/AS5600/AS5600.h | 9 +++-- libraries/AS5600/CHANGELOG.md | 8 +++- libraries/AS5600/README.md | 40 ++++++++++++++----- .../AS5600_position/AS5600_position.ino | 16 ++++++-- libraries/AS5600/library.json | 2 +- libraries/AS5600/library.properties | 2 +- 7 files changed, 73 insertions(+), 37 deletions(-) diff --git a/libraries/AS5600/AS5600.cpp b/libraries/AS5600/AS5600.cpp index 27b4323c..59fc9230 100644 --- a/libraries/AS5600/AS5600.cpp +++ b/libraries/AS5600/AS5600.cpp @@ -1,7 +1,7 @@ // // FILE: AS56000.cpp // AUTHOR: Rob Tillaart -// VERSION: 0.3.3 +// VERSION: 0.3.4 // PURPOSE: Arduino library for AS5600 magnetic rotation meter // DATE: 2022-05-28 // URL: https://github.com/RobTillaart/AS5600 @@ -311,27 +311,12 @@ uint8_t AS5600::getWatchDog() uint16_t AS5600::rawAngle() { int16_t value = readReg2(AS5600_RAW_ANGLE) & 0x0FFF; - - // whole rotation CW? - if ((_lastPosition > 2500) && ( value < 1500)) - { - _position = _position + 4096 - _lastPosition + value; - } - // whole rotation CCW? - else if ((value > 2500) && ( _lastPosition < 1500)) - { - _position = _position - 4096 - _lastPosition + value; - } - else _position = _position - _lastPosition + value; - _lastPosition = value; - if (_offset > 0) value = (value + _offset) & 0x0FFF; if ((_directionPin == 255) && (_direction == AS5600_COUNTERCLOCK_WISE)) { value = (4096 - value) & 0x0FFF; } - return value; } @@ -466,8 +451,24 @@ float AS5600::getAngularSpeed(uint8_t mode) // // POSITION cumulative // + int32_t AS5600::getCumulativePosition() { + int16_t value = readReg2(AS5600_RAW_ANGLE) & 0x0FFF; + + // whole rotation CW? + if ((_lastPosition > 2048) && ( value < (_lastPosition - 2048))) // less than half a circle + { + _position = _position + 4096 - _lastPosition + value; + } + // whole rotation CCW? + else if ((value > 2048) && ( _lastPosition < (value - 2048))) // less than half a circle + { + _position = _position - 4096 - _lastPosition + value; + } + else _position = _position - _lastPosition + value; + _lastPosition = value; + return _position; } diff --git a/libraries/AS5600/AS5600.h b/libraries/AS5600/AS5600.h index 3eab6a37..a1c91b00 100644 --- a/libraries/AS5600/AS5600.h +++ b/libraries/AS5600/AS5600.h @@ -2,7 +2,7 @@ // // FILE: AS5600.h // AUTHOR: Rob Tillaart -// VERSION: 0.3.3 +// VERSION: 0.3.4 // PURPOSE: Arduino library for AS5600 magnetic rotation meter // DATE: 2022-05-28 // URL: https://github.com/RobTillaart/AS5600 @@ -12,7 +12,7 @@ #include "Wire.h" -#define AS5600_LIB_VERSION (F("0.3.3")) +#define AS5600_LIB_VERSION (F("0.3.4")) // default addresses const uint8_t AS5600_DEFAULT_ADDRESS = 0x36; @@ -210,9 +210,12 @@ public: float getAngularSpeed(uint8_t mode = AS5600_MODE_DEGREES); // EXPERIMENTAL CUMULATIVE POSITION + // reads sensor and updates cumulative position int32_t getCumulativePosition(); + // converts last position to whole revolutions. int32_t getRevolutions(); - int32_t resetPosition(); // resets counter returns last value. + // resets position, returns last position. + int32_t resetPosition(); protected: diff --git a/libraries/AS5600/CHANGELOG.md b/libraries/AS5600/CHANGELOG.md index 9cd80cb2..27d98416 100644 --- a/libraries/AS5600/CHANGELOG.md +++ b/libraries/AS5600/CHANGELOG.md @@ -5,6 +5,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [0.3.4] - 2022-12-22 +- fix #26 edges problem of the experimental cumulative position (CP). +- decoupled CP from **rawAngle()** + - now one needs to call **getCumulativePosition()** to update the CP. +- updated the readme.md section about CP. + + ## [0.3.3] - 2022-12-19 - add experimental continuous position. - add **getCumulativePosition()** @@ -18,7 +25,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - add AS5600L_DEFAULT_ADDRESS - update readme.md - ## [0.3.2] - 2022-10-16 - add CHANGELOG.md - update readme.md diff --git a/libraries/AS5600/README.md b/libraries/AS5600/README.md index efa69718..3fb5a6bf 100644 --- a/libraries/AS5600/README.md +++ b/libraries/AS5600/README.md @@ -301,21 +301,39 @@ with a short interval. The only limitation then is that both measurements should be within 180° = half a rotation. -### Cumulative position +### Cumulative position (experimental) + +Since 0.3.3 an experimental cumulative position can be requested from the library. +The sensor does not provide interrupts to indicate a movement or revolution +Therefore one has to poll the sensor at a frequency at least 3 times per revolution with **getCumulativePosition()** + +The cumulative position consists of 3 parts + +| bit | meaning | notes | +|:-------:|:--------------|:--------| +| 31 | sign | typical + == CW, - == CCW +| 30-12 | revolutions | +| 11-00 | raw angle | call getCumulativePosition() + + +Functions are: + +- **int32_t getCumulativePosition()** reads sensor and updates cumulative position. +- **int32_t getRevolutions()** converts last position to whole revolutions. +Convenience function. +- **int32_t resetPosition()** resets **revolutions**, returns last position. +The cumulative position does not reset to 0 but to the last known raw angle. +This way the cumulative position always indicate the (absolute) angle too. + +As this code is experimental, names might change in the future. +As the function are mostly about counting revolutions the current thoughts for new names are: ```cpp - // EXPERIMENTAL CUMULATIVE POSITION - int32_t getCumulativePosition(); - int32_t getRevolutions(); - int32_t resetPosition(); // resets position returns last value. +int32_t updateRevolutions() replaces getCumulativePosition() +int32_t getRevolutions() +int32_t resetRevolutions() replaces resetPosition() ``` -to elaborate - -- call rawAngle() often enough == at least 4x per rotation -- example -- only the revolutions are reset - ### Status registers diff --git a/libraries/AS5600/examples/AS5600_position/AS5600_position.ino b/libraries/AS5600/examples/AS5600_position/AS5600_position.ino index a69b9eed..7f19143c 100644 --- a/libraries/AS5600/examples/AS5600_position/AS5600_position.ino +++ b/libraries/AS5600/examples/AS5600_position/AS5600_position.ino @@ -39,9 +39,12 @@ void setup() void loop() { static uint32_t lastTime = 0; - // Serial.print("\ta = "); - // Serial.print(as5600.readAngle()); - as5600.rawAngle(); + + // set initial position + as5600.getCumulativePosition(); + + // update every 100 ms + // should be enough up to ~200 RPM if (millis() - lastTime >= 100) { lastTime = millis(); @@ -49,7 +52,12 @@ void loop() Serial.print("\t"); Serial.println(as5600.getRevolutions()); } - if (as5600.getRevolutions() >= 10) as5600.resetPosition(); + + // just to show how reset can be used + if (as5600.getRevolutions() >= 10) + { + as5600.resetPosition(); + } } diff --git a/libraries/AS5600/library.json b/libraries/AS5600/library.json index 039d8a66..4e7a7186 100644 --- a/libraries/AS5600/library.json +++ b/libraries/AS5600/library.json @@ -15,7 +15,7 @@ "type": "git", "url": "https://github.com/RobTillaart/AS5600.git" }, - "version": "0.3.3", + "version": "0.3.4", "license": "MIT", "frameworks": "arduino", "platforms": "*", diff --git a/libraries/AS5600/library.properties b/libraries/AS5600/library.properties index d8faba2e..3088c9b7 100644 --- a/libraries/AS5600/library.properties +++ b/libraries/AS5600/library.properties @@ -1,5 +1,5 @@ name=AS5600 -version=0.3.3 +version=0.3.4 author=Rob Tillaart maintainer=Rob Tillaart sentence=Arduino library for AS5600 and AS5600L magnetic rotation meter.