Skip to content

Commit

Permalink
Fix #67, Fix #69
Browse files Browse the repository at this point in the history
  • Loading branch information
RobTillaart committed Oct 9, 2024
1 parent d5f90ad commit f55e473
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 20 deletions.
35 changes: 26 additions & 9 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.2
// VERSION: 0.6.3
// PURPOSE: Arduino library for AS5600 magnetic rotation meter
// DATE: 2022-05-28
// URL: https://github.com/RobTillaart/AS5600
Expand Down Expand Up @@ -426,8 +426,15 @@ bool AS5600::magnetTooWeak()
// }


float AS5600::getAngularSpeed(uint8_t mode)
float AS5600::getAngularSpeed(uint8_t mode, bool update)
{
static float speed = 0;

if (update == false)
{
return speed;
}
// default behaviour
uint32_t now = micros();
int angle = readAngle();
uint32_t deltaT = now - _lastMeasurement;
Expand All @@ -436,9 +443,9 @@ float AS5600::getAngularSpeed(uint8_t mode)
// assumption is that there is no more than 180° rotation
// between two consecutive measurements.
// => at least two measurements per rotation (preferred 4).
if (deltaA > 2048) deltaA -= 4096;
if (deltaA < -2048) deltaA += 4096;
float speed = (deltaA * 1e6) / deltaT;
if (deltaA > 2048) deltaA -= 4096;
else if (deltaA < -2048) deltaA += 4096;
speed = (deltaA * 1e6) / deltaT;

// remember last time & angle
_lastMeasurement = now;
Expand All @@ -462,10 +469,17 @@ float AS5600::getAngularSpeed(uint8_t mode)
//
// POSITION cumulative
//
int32_t AS5600::getCumulativePosition()
int32_t AS5600::getCumulativePosition(bool update)
{
if (update == false)
{
return _position;
}
int16_t value = readAngle();
if (_error != AS5600_OK) return _position;
if (_error != AS5600_OK)
{
return _position;
}

// whole rotation CW?
// less than half a circle
Expand All @@ -479,7 +493,10 @@ int32_t AS5600::getCumulativePosition()
{
_position = _position - 4096 - _lastPosition + value;
}
else _position = _position - _lastPosition + value;
else
{
_position = _position - _lastPosition + value;
}
_lastPosition = value;

return _position;
Expand All @@ -504,7 +521,7 @@ int32_t AS5600::resetPosition(int32_t position)

int32_t AS5600::resetCumulativePosition(int32_t position)
{
_lastPosition = readReg2(AS5600_RAW_ANGLE) & 0x0FFF;
_lastPosition = readAngle();
int32_t old = _position;
_position = position;
return old;
Expand Down
8 changes: 4 additions & 4 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.2
// VERSION: 0.6.3
// 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.2"))
#define AS5600_LIB_VERSION (F("0.6.3"))


// default addresses
Expand Down Expand Up @@ -219,11 +219,11 @@ class AS5600
// approximation of the angular speed in rotations per second.
// mode == 1: radians /second
// mode == 0: degrees /second (default)
float getAngularSpeed(uint8_t mode = AS5600_MODE_DEGREES);
float getAngularSpeed(uint8_t mode = AS5600_MODE_DEGREES, bool update = true);

// EXPERIMENTAL CUMULATIVE POSITION
// reads sensor and updates cumulative position
int32_t getCumulativePosition();
int32_t getCumulativePosition(bool update = true);
// converts last position to whole revolutions.
int32_t getRevolutions();
// resets position only (not the i)
Expand Down
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).


## [0.6.3] - 2024-10-04
- fix #67, add update flag for speed and cumulative position
- fix #69, **resetCumulativePosition()**
- minor edits

## [0.6.2] - 2024-10-04
- fix #65, make **getCumulativePosition()** direction aware.
- optimize **readAngle()** and **rawAngle()**.
Expand All @@ -15,7 +20,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- 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).
Expand Down
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,11 @@ as.increaseOffset(-30);

### Angular Speed

- **float getAngularSpeed(uint8_t mode = AS5600_MODE_DEGREES)** is an experimental function that returns
- **float getAngularSpeed(uint8_t mode = AS5600_MODE_DEGREES, bool update = true)**
is an experimental function that returns
an approximation of the angular speed in rotations per second.
If the update flag is set to false, it returns the last calculated speed.

The function needs to be called at least **four** times per rotation
or once per second to get a reasonably precision.

Expand Down Expand Up @@ -418,7 +421,7 @@ should be within 180° = half a rotation.
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 **three** times
per revolution with **getCumulativePosition()**
per revolution with **getCumulativePosition(bool update = true)**

The cumulative position (32 bits) consists of 3 parts

Expand All @@ -431,8 +434,9 @@ The cumulative position (32 bits) consists of 3 parts

Functions are:

- **int32_t getCumulativePosition()** reads sensor and updates cumulative position.
- **int32_t getCumulativePosition(bool update = true)** reads sensor and updates cumulative position.
Updated in 0.6.2 to follow the setting of the **directionPin**.
If update == false, it returns the last calculated position.
- **int32_t getRevolutions()** converts last position to whole revolutions.
Convenience function.
Updated in 0.6.2 to return **zero** for the first negative revolution as this
Expand All @@ -445,7 +449,7 @@ Returns last position (before reset).
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 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
Expand Down
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"type": "git",
"url": "https://github.com/RobTillaart/AS5600.git"
},
"version": "0.6.2",
"version": "0.6.3",
"license": "MIT",
"frameworks": "*",
"platforms": "*",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=AS5600
version=0.6.2
version=0.6.3
author=Rob Tillaart <[email protected]>
maintainer=Rob Tillaart <[email protected]>
sentence=Arduino library for AS5600 and AS5600L magnetic rotation meter.
Expand Down

0 comments on commit f55e473

Please sign in to comment.