Skip to content

Commit

Permalink
add resetCumulativeCounter() (#31)
Browse files Browse the repository at this point in the history
- add **resetCumulativePosition(int32_t position)**
- add parameter position to **resetPosition(int32_t position)**
- add example
- update readme.md
  • Loading branch information
RobTillaart committed Feb 21, 2023
1 parent 2fdb294 commit 8b62620
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 20 deletions.
24 changes: 17 additions & 7 deletions AS5600.cpp
@@ -1,7 +1,7 @@
//
// FILE: AS56000.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.3.5
// VERSION: 0.3.6
// PURPOSE: Arduino library for AS5600 magnetic rotation meter
// DATE: 2022-05-28
// URL: https://github.com/RobTillaart/AS5600
Expand Down Expand Up @@ -451,18 +451,19 @@ 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
// less than half a circle
if ((_lastPosition > 2048) && ( value < (_lastPosition - 2048)))
{
_position = _position + 4096 - _lastPosition + value;
}
// whole rotation CCW?
else if ((value > 2048) && ( _lastPosition < (value - 2048))) // less than half a circle
// less than half a circle
else if ((value > 2048) && ( _lastPosition < (value - 2048)))
{
_position = _position - 4096 - _lastPosition + value;
}
Expand All @@ -475,17 +476,26 @@ int32_t AS5600::getCumulativePosition()

int32_t AS5600::getRevolutions()
{
int32_t p = _position >> 12;
int32_t p = _position >> 12; // divide by 4096
return p;
// if (p < 0) p++;
// return p;
}


int32_t AS5600::resetPosition()
int32_t AS5600::resetPosition(int32_t position)
{
int32_t old = _position;
_position = position;
return old;
}


int32_t AS5600::resetCumulativePosition(int32_t position)
{
_lastPosition = readReg2(AS5600_RAW_ANGLE) & 0x0FFF;
int32_t old = _position;
_position = 0;
_position = position;
return old;
}

Expand Down
12 changes: 8 additions & 4 deletions AS5600.h
Expand Up @@ -2,7 +2,7 @@
//
// FILE: AS5600.h
// AUTHOR: Rob Tillaart
// VERSION: 0.3.5
// VERSION: 0.3.6
// 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.3.5"))
#define AS5600_LIB_VERSION (F("0.3.6"))

// default addresses
const uint8_t AS5600_DEFAULT_ADDRESS = 0x36;
Expand Down Expand Up @@ -214,8 +214,12 @@ class AS5600
int32_t getCumulativePosition();
// converts last position to whole revolutions.
int32_t getRevolutions();
// resets position, returns last position.
int32_t resetPosition();
// resets position only (not the i)
// returns last position but not internal lastPosition.
int32_t resetPosition(int32_t position = 0);
// resets position and internal lastPosition
// returns last position.
int32_t resetCumulativePosition(int32_t position = 0);


protected:
Expand Down
9 changes: 8 additions & 1 deletion CHANGELOG.md
Expand Up @@ -5,12 +5,19 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.3.6] - 2023-02-20
- add **resetCumulativePosition(int32_t position)** to completely reset the cumulative counter.
This includes the delta since last call to **getCumulativePosition()**.
- add parameter position to **resetPosition(int32_t position)** so a new position can be set.
This does not reset the delta since last call to **getCumulativePosition()**.
- update readme.md


## [0.3.5] - 2023-02-01
- update GitHub actions
- update license 2023
- update readme.md


## [0.3.4] - 2022-12-22
- fix #26 edges problem of the experimental cumulative position (CP).
- decoupled CP from **rawAngle()**
Expand Down
17 changes: 11 additions & 6 deletions README.md
Expand Up @@ -16,7 +16,7 @@ Arduino library for AS5600 and AS5600L magnetic rotation meter.
#### AS5600

**AS5600** is a library for an AS5600 / AS5600L based magnetic **rotation** meter.
More exact, it measures the angle (rotation wrt reference) and not RPM.
More exact, it measures the angle (rotation w.r.t. reference) and not RPM.
Multiple angle measurements allows one to calculate / estimate the RPM.

The AS5600 and AS5600L sensors are pin compatible (always check datasheet).
Expand Down Expand Up @@ -345,11 +345,14 @@ 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 (0.4.0).
- **int32_t resetPosition(int32_t position = 0)** resets the "revolutions" to position (default 0).
It does not reset the delta (rotation) since last call to **getCumulativePosition()**.
Returns last position (before reset).
- **int32_t resetCumulativePosition(int32_t position = 0)** completely resets the cumulative counter.
This includes the delta (rotation) since last call to **getCumulativePosition()**.
Returns last position (before reset).

As this code is experimental, names might change in the future (0.4.0)?
As the function are mostly about counting revolutions the current thoughts for new names are:

```cpp
Expand Down Expand Up @@ -649,6 +652,8 @@ priority is relative.

- add error handling
- investigate PGO programming pin.
- check for compatible devices
- AS5200 ?


#### Wont
Expand Down
28 changes: 28 additions & 0 deletions examples/AS5600_resetCumulativeCounter/.arduino-ci.yml
@@ -0,0 +1,28 @@
platforms:
rpipico:
board: rp2040:rp2040:rpipico
package: rp2040:rp2040
gcc:
features:
defines:
- ARDUINO_ARCH_RP2040
warnings:
flags:

packages:
rp2040:rp2040:
url: https://github.com/earlephilhower/arduino-pico/releases/download/global/package_rp2040_index.json

compile:
# Choosing to run compilation tests on 2 different Arduino platforms
platforms:
# - uno
# - due
# - zero
# - leonardo
# - m4
- esp32
# - esp8266
# - mega2560
# - rpipico

@@ -0,0 +1,42 @@
//
// FILE: AS5600_resetCumulativeCounter.ino
// AUTHOR: Daniel-Frenkel, (slightly by Rob Tillaart)
// PURPOSE: demo - see issue #30


#include "AS5600.h"
#include "Wire.h"

AS5600L as5600; // use default Wire


void setup()
{
Serial.begin(115200);
as5600.begin(14, 15); // ESP32
as5600.setAddress(0x40); // AS5600L has address

as5600.setDirection(AS5600_CLOCK_WISE); // default, just be explicit.
delay(1000);
as5600.resetCumulativePosition(777);
}


void loop()
{
static uint32_t lastTime = 0;

// set initial position
as5600.getCumulativePosition();

// update every 100 ms
if (millis() - lastTime >= 100)
{
lastTime = millis();
Serial.println(as5600.getCumulativePosition());
}
}


// -- END OF FILE --

1 change: 1 addition & 0 deletions keywords.txt
Expand Up @@ -45,6 +45,7 @@ getAngularSpeed KEYWORD2
getCumulativePosition KEYWORD2
getRevolutions KEYWORD2
resetPosition KEYWORD2
resetCumulativePosition KEYWORD2


# CONFIGURATION FIELDS
Expand Down
2 changes: 1 addition & 1 deletion library.json
Expand Up @@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/AS5600.git"
},
"version": "0.3.5",
"version": "0.3.6",
"license": "MIT",
"frameworks": "arduino",
"platforms": "*",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
@@ -1,5 +1,5 @@
name=AS5600
version=0.3.5
version=0.3.6
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for AS5600 and AS5600L magnetic rotation meter.
Expand Down

0 comments on commit 8b62620

Please sign in to comment.