GY-63_MS5611/libraries/AS5600/AS5600.h

271 lines
7.3 KiB
C
Raw Normal View History

2022-05-29 16:07:48 -04:00
#pragma once
//
// FILE: AS5600.h
// AUTHOR: Rob Tillaart
2023-02-01 10:57:56 -05:00
// VERSION: 0.3.5
2022-05-29 16:07:48 -04:00
// PURPOSE: Arduino library for AS5600 magnetic rotation meter
// DATE: 2022-05-28
// URL: https://github.com/RobTillaart/AS5600
#include "Arduino.h"
#include "Wire.h"
2023-02-01 10:57:56 -05:00
#define AS5600_LIB_VERSION (F("0.3.5"))
2022-12-21 15:44:41 -05:00
// default addresses
const uint8_t AS5600_DEFAULT_ADDRESS = 0x36;
const uint8_t AS5600L_DEFAULT_ADDRESS = 0x40;
2022-05-29 16:07:48 -04:00
2022-06-27 05:27:48 -04:00
// setDirection
const uint8_t AS5600_CLOCK_WISE = 0; // LOW
const uint8_t AS5600_COUNTERCLOCK_WISE = 1; // HIGH
2022-05-29 16:07:48 -04:00
2022-07-04 06:10:00 -04:00
// 0.087890625;
const float AS5600_RAW_TO_DEGREES = 360.0 / 4096;
// 0.00153398078788564122971808758949;
const float AS5600_RAW_TO_RADIANS = PI * 2.0 / 4096;
2022-12-21 15:44:41 -05:00
// 4.06901041666666e-6
const float AS5600_RAW_TO_RPM = 60.0 / 4096;
2022-06-27 05:27:48 -04:00
// getAngularSpeed
const uint8_t AS5600_MODE_DEGREES = 0;
const uint8_t AS5600_MODE_RADIANS = 1;
2022-12-21 15:44:41 -05:00
const uint8_t AS5600_MODE_RPM = 2;
2022-06-27 05:27:48 -04:00
2022-07-04 06:10:00 -04:00
// CONFIGURE CONSTANTS
// check datasheet for details
2022-06-27 05:27:48 -04:00
// setOutputMode
const uint8_t AS5600_OUTMODE_ANALOG_100 = 0;
const uint8_t AS5600_OUTMODE_ANALOG_90 = 1;
const uint8_t AS5600_OUTMODE_PWM = 2;
// setPowerMode
const uint8_t AS5600_POWERMODE_NOMINAL = 0;
const uint8_t AS5600_POWERMODE_LOW1 = 1;
const uint8_t AS5600_POWERMODE_LOW2 = 2;
const uint8_t AS5600_POWERMODE_LOW3 = 3;
// setPWMFrequency
const uint8_t AS5600_PWM_115 = 0;
const uint8_t AS5600_PWM_230 = 1;
const uint8_t AS5600_PWM_460 = 2;
const uint8_t AS5600_PWM_920 = 3;
2022-07-04 06:10:00 -04:00
// setHysteresis
const uint8_t AS5600_HYST_OFF = 0;
const uint8_t AS5600_HYST_LSB1 = 1;
const uint8_t AS5600_HYST_LSB2 = 2;
const uint8_t AS5600_HYST_LSB3 = 3;
// setSlowFilter
const uint8_t AS5600_SLOW_FILT_16X = 0;
const uint8_t AS5600_SLOW_FILT_8X = 1;
const uint8_t AS5600_SLOW_FILT_4X = 2;
const uint8_t AS5600_SLOW_FILT_2X = 3;
// setFastFilter
const uint8_t AS5600_FAST_FILT_NONE = 0;
const uint8_t AS5600_FAST_FILT_LSB6 = 1;
const uint8_t AS5600_FAST_FILT_LSB7 = 2;
const uint8_t AS5600_FAST_FILT_LSB9 = 3;
const uint8_t AS5600_FAST_FILT_LSB18 = 4;
const uint8_t AS5600_FAST_FILT_LSB21 = 5;
const uint8_t AS5600_FAST_FILT_LSB24 = 6;
const uint8_t AS5600_FAST_FILT_LSB10 = 7;
2022-06-27 05:27:48 -04:00
// setWatchDog
const uint8_t AS5600_WATCHDOG_OFF = 0;
const uint8_t AS5600_WATCHDOG_ON = 1;
2022-06-26 07:58:33 -04:00
2022-05-29 16:07:48 -04:00
class AS5600
{
public:
AS5600(TwoWire *wire = &Wire);
#if defined (ESP8266) || defined(ESP32)
2022-07-04 06:10:00 -04:00
// 255 is software controlled direction pin
2022-09-01 05:31:32 -04:00
bool begin(int dataPin, int clockPin, uint8_t directionPin = 255);
2022-05-29 16:07:48 -04:00
#endif
2022-07-04 06:10:00 -04:00
// 255 is software controlled direction pin
2022-12-21 15:44:41 -05:00
bool begin(uint8_t directionPin = 255); // MAGIC NR => const int?
2022-05-29 16:07:48 -04:00
bool isConnected();
2022-12-21 15:44:41 -05:00
uint8_t getAddress(); // 0x36 for AS5600, 0x40 for AS5600L
2022-05-29 16:07:48 -04:00
2022-06-02 06:07:52 -04:00
// SET CONFIGURE REGISTERS
2022-05-29 16:07:48 -04:00
// read datasheet first
2022-07-07 06:53:02 -04:00
// 0 = AS5600_CLOCK_WISE
// 1 = AS5600_COUNTERCLOCK_WISE
// all other = AS5600_COUNTERCLOCK_WISE
2022-05-29 16:07:48 -04:00
void setDirection(uint8_t direction = AS5600_CLOCK_WISE);
uint8_t getDirection();
uint8_t getZMCO();
2022-07-07 06:53:02 -04:00
// 0 .. 4095
// returns false if parameter out of range
bool setZPosition(uint16_t value);
2022-05-29 16:07:48 -04:00
uint16_t getZPosition();
2022-07-07 06:53:02 -04:00
// 0 .. 4095
// returns false if parameter out of range
bool setMPosition(uint16_t value);
2022-05-29 16:07:48 -04:00
uint16_t getMPosition();
2022-07-07 06:53:02 -04:00
// 0 .. 4095
// returns false if parameter out of range
bool setMaxAngle(uint16_t value);
2022-05-29 16:07:48 -04:00
uint16_t getMaxAngle();
// access the whole configuration register
2022-07-04 06:10:00 -04:00
// check datasheet for bit fields
2022-07-07 06:53:02 -04:00
// returns false if parameter out of range
bool setConfigure(uint16_t value);
2022-05-29 16:07:48 -04:00
uint16_t getConfigure();
2022-06-02 06:07:52 -04:00
// access details of the configuration register
// 0 = Normal
// 1,2,3 are low power mode - check datasheet
2022-07-07 06:53:02 -04:00
// returns false if parameter out of range
bool setPowerMode(uint8_t powerMode);
2022-05-29 16:07:48 -04:00
uint8_t getPowerMode();
2022-07-04 06:10:00 -04:00
// 0 = off 1 = lsb1 2 = lsb2 3 = lsb3
2022-07-07 06:53:02 -04:00
// returns false if parameter out of range
// suppresses noise when the magnet is not moving.
bool setHysteresis(uint8_t hysteresis);
2022-05-29 16:07:48 -04:00
uint8_t getHysteresis();
2022-06-02 06:07:52 -04:00
// 0 = analog 0-100%
// 1 = analog 10-90%
2022-06-27 05:27:48 -04:00
// 2 = PWM
2022-07-07 06:53:02 -04:00
// returns false if parameter out of range
bool setOutputMode(uint8_t outputMode);
2022-05-29 16:07:48 -04:00
uint8_t getOutputMode();
2022-06-02 06:07:52 -04:00
// 0 = 115 1 = 230 2 = 460 3 = 920 (Hz)
2022-07-07 06:53:02 -04:00
// returns false if parameter out of range
bool setPWMFrequency(uint8_t pwmFreq);
2022-05-29 16:07:48 -04:00
uint8_t getPWMFrequency();
2022-07-04 06:10:00 -04:00
// 0 = 16x 1 = 8x 2 = 4x 3 = 2x
2022-07-07 06:53:02 -04:00
// returns false if parameter out of range
bool setSlowFilter(uint8_t mask);
2022-05-29 16:07:48 -04:00
uint8_t getSlowFilter();
2022-07-04 06:10:00 -04:00
// 0 = none 1 = LSB6 2 = LSB7 3 = LSB9
// 4 = LSB18 5 = LSB21 6 = LSB24 7 = LSB10
2022-07-07 06:53:02 -04:00
// returns false if parameter out of range
bool setFastFilter(uint8_t mask);
2022-05-29 16:07:48 -04:00
uint8_t getFastFilter();
2022-06-02 06:07:52 -04:00
// 0 = OFF
// 1 = ON (auto low power mode)
2022-07-07 06:53:02 -04:00
// returns false if parameter out of range
bool setWatchDog(uint8_t mask);
2022-05-29 16:07:48 -04:00
uint8_t getWatchDog();
2022-06-02 06:07:52 -04:00
// READ OUTPUT REGISTERS
2022-05-29 16:07:48 -04:00
uint16_t rawAngle();
uint16_t readAngle();
2022-07-04 06:10:00 -04:00
// software based offset.
2022-07-07 06:53:02 -04:00
// degrees = -359.99 .. 359.99 (preferred)
// returns false if abs(parameter) > 36000
// => expect loss of precision
bool setOffset(float degrees);
2022-07-04 06:10:00 -04:00
float getOffset();
2022-05-29 16:07:48 -04:00
2022-06-02 06:07:52 -04:00
// READ STATUS REGISTERS
2022-05-29 16:07:48 -04:00
uint8_t readStatus();
uint8_t readAGC();
uint16_t readMagnitude();
2022-06-02 06:07:52 -04:00
// access detail status register
2022-05-29 16:07:48 -04:00
bool detectMagnet();
2022-09-01 05:31:32 -04:00
bool magnetTooStrong();
bool magnetTooWeak();
2022-05-29 16:07:48 -04:00
// BURN COMMANDS
// DO NOT UNCOMMENT - USE AT OWN RISK - READ DATASHEET
// void burnAngle();
// void burnSetting();
2022-07-04 06:10:00 -04:00
// Experimental 0.1.2 - to be tested.
2022-06-02 14:10:23 -04:00
// approximation of the angular speed in rotations per second.
2022-06-26 07:58:33 -04:00
// mode == 1: radians /second
// mode == 0: degrees /second (default)
2022-06-27 05:27:48 -04:00
float getAngularSpeed(uint8_t mode = AS5600_MODE_DEGREES);
2022-06-02 14:10:23 -04:00
2022-12-21 15:44:41 -05:00
// EXPERIMENTAL CUMULATIVE POSITION
2022-12-22 08:03:01 -05:00
// reads sensor and updates cumulative position
2022-12-21 15:44:41 -05:00
int32_t getCumulativePosition();
2022-12-22 08:03:01 -05:00
// converts last position to whole revolutions.
2022-12-21 15:44:41 -05:00
int32_t getRevolutions();
2022-12-22 08:03:01 -05:00
// resets position, returns last position.
int32_t resetPosition();
2022-12-21 15:44:41 -05:00
2022-05-29 16:07:48 -04:00
2022-09-01 05:31:32 -04:00
protected:
2022-06-02 06:07:52 -04:00
uint8_t readReg(uint8_t reg);
uint16_t readReg2(uint8_t reg);
uint8_t writeReg(uint8_t reg, uint8_t value);
2022-06-27 05:27:48 -04:00
uint8_t writeReg2(uint8_t reg, uint16_t value);
2022-05-29 16:07:48 -04:00
2022-12-21 15:44:41 -05:00
uint8_t _address = AS5600_DEFAULT_ADDRESS;
uint8_t _directionPin = 255;
uint8_t _direction = AS5600_CLOCK_WISE;
uint8_t _error = 0;
2022-05-29 16:07:48 -04:00
TwoWire* _wire;
2022-06-27 05:27:48 -04:00
2022-07-04 06:10:00 -04:00
// for getAngularSpeed()
2022-06-02 14:10:23 -04:00
uint32_t _lastMeasurement = 0;
2022-07-04 06:10:00 -04:00
int16_t _lastAngle = 0;
// for readAngle() and rawAngle()
uint16_t _offset = 0;
2022-12-21 15:44:41 -05:00
// EXPERIMENTAL
// cumulative position counter
// works only if the sensor is read often enough.
int32_t _position = 0;
int16_t _lastPosition = 0;
2022-05-29 16:07:48 -04:00
};
2022-09-01 05:31:32 -04:00
/////////////////////////////////////////////////////////////////////////////
//
// AS5600L
//
class AS5600L : public AS5600
{
public:
2022-12-21 15:44:41 -05:00
AS5600L(uint8_t address = AS5600L_DEFAULT_ADDRESS, TwoWire *wire = &Wire);
2022-09-01 05:31:32 -04:00
bool setAddress(uint8_t address);
// UPDT = UPDATE
// are these two needed?
bool setI2CUPDT(uint8_t value);
uint8_t getI2CUPDT();
};
2022-05-29 16:07:48 -04:00
// -- END OF FILE --