Skip to content

Commit

Permalink
fix #8 Bug in setConfigure (#9)
Browse files Browse the repository at this point in the history
* fix #8 Bug in setConfigure
* Add AS5600_RAW_TO_RADIANS
* Add getAngularSpeed() mode parameter.
  • Loading branch information
RobTillaart authored Jun 26, 2022
1 parent 21746e2 commit b9f98c4
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 17 deletions.
16 changes: 11 additions & 5 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.1.2
// VERSION: 0.1.3
// PURPOSE: Arduino library for AS5600 magnetic rotation meter
// DATE: 2022-05-28
// URL: https://github.com/RobTillaart/AS5600
Expand All @@ -12,7 +12,9 @@
// Fix shift-direction @ getZPosition, getMPosition,
// getMaxAngle and getConfigure
// 0.1.2 2022-06-02 Add getAngularSpeed()
//
// 0.1.3 2022-06-26 Add AS5600_RAW_TO_RADIANS
// Add getAngularSpeed() mode parameter.
// Fix #8 bug in configure.


// TODO
Expand Down Expand Up @@ -174,7 +176,7 @@ uint16_t AS5600::getMaxAngle()

void AS5600::setConfigure(uint16_t value)
{
writeReg(AS5600_CONF, (value >> 8) & 0x0F);
writeReg(AS5600_CONF, (value >> 8) & 0x2F);
writeReg(AS5600_CONF + 1, value & 0xFF);
}

Expand Down Expand Up @@ -354,15 +356,19 @@ bool AS5600::detectMagnet()
// }


float AS5600::getAngularSpeed()
float AS5600::getAngularSpeed(uint8_t mode)
{
uint32_t now = micros();
int angle = readAngle();
uint32_t deltaT = now - _lastMeasurement;
int deltaA = angle - _lastAngle;
float speed = (deltaA * 1e6) / deltaT;
// remember last time & angle
_lastMeasurement = now;
_lastAngle = angle;
return (deltaA * 1e6 * AS5600_RAW_TO_DEGREES) / deltaT;
// return degrees or radians
if (mode == 1) return speed * AS5600_RAW_TO_RADIANS;
return speed * AS5600_RAW_TO_DEGREES;
}


Expand Down
11 changes: 8 additions & 3 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.1.2
// VERSION: 0.1.3
// PURPOSE: Arduino library for AS5600 magnetic rotation meter
// DATE: 2022-05-28
// URL: https://github.com/RobTillaart/AS5600
Expand All @@ -12,12 +12,15 @@
#include "Wire.h"


#define AS5600_LIB_VERSION (F("0.1.2"))
#define AS5600_LIB_VERSION (F("0.1.3"))

#define AS5600_CLOCK_WISE 0 // LOW
#define AS5600_COUNTERCLOCK_WISE 1 // HIGH


#define AS5600_RAW_TO_DEGREES (0.0879120879120879121)
#define AS5600_RAW_TO_RADIANS (0.00153435538636864138630654133494)


class AS5600
{
Expand Down Expand Up @@ -106,7 +109,9 @@ class AS5600

// experimental 0.1.2 - to be tested.
// approximation of the angular speed in rotations per second.
float getAngularSpeed();
// mode == 1: radians /second
// mode == 0: degrees /second (default)
float getAngularSpeed(uint8_t mode = 0);


private:
Expand Down
31 changes: 24 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ To be adjusted via command line (or in AS5600.h file)
- **AS5600_CLOCK_WISE 1**
- **AS5600_COUNTERCLOCK_WISE 0**
- **AS5600_RAW_TO_DEGREES 0.0879120879120879121**
- **AS5600_RAW_TO_RADIANS 0.00153435538636864138630654133494**


### Constructor + I2C
Expand Down Expand Up @@ -107,12 +108,18 @@ This is the one most used.

### Angular Speed

- **getAngularSpeed()** is an experimental function that returns an approximation of the angular speed in rotations per second.
- **getAngularSpeed(uint8_t mode = 0)** is an experimental function that returns
an approximation of the angular speed in rotations per second.
The function needs to be called at least **four** times per rotation
to get a reasonably accuracy.

Negative values indicate reverse rotation. What that means depends on
the setup of your project.
(0.1.3 added mode parameter).
- mode == 1: radians /second
- mode == 0: degrees /second (default)
- mode == ?: degrees /second

Negative values indicate reverse rotation.
What that means depends on the setup of your project.

Note: the first call will return an erroneous value as it has no
reference angle or time. Also if one stops calling this function
Expand Down Expand Up @@ -148,15 +155,21 @@ Please read datasheet for details.

Please read datasheet twice.

The burn functions are used to make settings persistent. As these functions can only be called one or three times, they are highly permanent, therefore they are commented in the library.
The burn functions are used to make settings persistent.
As these functions can only be called one or three times,
they are highly permanent, therefore they are commented in the library.

The risk is that you make your as5600 **USELESS**.

**USE AT OWN RISK**

- **uint8_t getZMCO()** reads back how many times the ZPOS and MPOS registers are written to permanent memory. You can only burn a new Angle 3 times to the AS5600.
- **void burnAngle()** writes the ZPOS and MPOS registers to permanent memory. You can only burn a new Angle maximum **THREE** times to the AS5600.
- **void burnSetting()** writes the MANG register to permanent memory. You can write this only **ONE** time to the AS5600.
- **uint8_t getZMCO()** reads back how many times the ZPOS and MPOS
registers are written to permanent memory.
You can only burn a new Angle 3 times to the AS5600.
- **void burnAngle()** writes the ZPOS and MPOS registers to permanent memory.
You can only burn a new Angle maximum **THREE** times to the AS5600.
- **void burnSetting()** writes the MANG register to permanent memory.
You can write this only **ONE** time to the AS5600.



Expand Down Expand Up @@ -192,18 +205,22 @@ See examples.

Some ideas are kept here so they won't get lost.


### high prio

- improve documentation
- improve performance (I2C issue)
- get hardware to test.
- write examples
- as5600_calibration.ino ?
- add functions
- **setOutputMode()** + constants.
- **setPowerMode()** + constants
- **magnetStrength()**
- combination of AGC and MD, ML and MH flags?
- do we need **ANGLE_FACTOR** = 0.0879121


### low prio

- unit test
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.1.2",
"version": "0.1.3",
"license": "MIT",
"frameworks": "arduino",
"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.1.2
version=0.1.3
author=Rob Tillaart <rob.tillaart@gmail.com>
maintainer=Rob Tillaart <rob.tillaart@gmail.com>
sentence=Arduino library for AS5600 magnetic rotation meter
Expand Down
1 change: 1 addition & 0 deletions test/unit_test_001.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ unittest(test_constants)
assertEqual(0, AS5600_CLOCK_WISE);
assertEqual(1, AS5600_COUNTERCLOCK_WISE);
assertEqualFloat(360.0/4095, AS5600_RAW_TO_DEGREES, 0.0001);
assertEqualFloat(PI*2.0/4095, AS5600_RAW_TO_RADIANS, 0.0001);
}


Expand Down

0 comments on commit b9f98c4

Please sign in to comment.