Adafruit Library
WipperSnapper_I2C_Driver_AS5600.h
Go to the documentation of this file.
1 
15 #ifndef WipperSnapper_I2C_Driver_AS5600_H
16 #define WipperSnapper_I2C_Driver_AS5600_H
17 
18 #include <Adafruit_AS5600.h>
19 
21 #include "Wippersnapper.h"
22 
23 /**************************************************************************/
27 /**************************************************************************/
29 public:
30  /*******************************************************************************/
38  /*******************************************************************************/
39  WipperSnapper_I2C_Driver_AS5600(TwoWire *i2c, uint16_t sensorAddress)
40  : WipperSnapper_I2C_Driver(i2c, sensorAddress) {
41  _as5600 = nullptr;
42  _angle = 0;
43  }
44 
45  /*******************************************************************************/
49  /*******************************************************************************/
51  if (_as5600) {
52  delete _as5600;
53  _as5600 = nullptr;
54  }
55  }
56 
57  /*******************************************************************************/
62  /*******************************************************************************/
63  bool begin() {
64  _as5600 = new Adafruit_AS5600();
65  if (!_as5600->begin((uint8_t)_sensorAddress, _i2c)) {
66  WS_DEBUG_PRINTLN("Failed to find AS5600 chip");
67  return false;
68  }
69 
70  if (!configureSensor()) {
71  WS_DEBUG_PRINTLN("Failed to configure AS5600 sensor");
72  return false;
73  }
74  return true;
75  }
76 
77  /*******************************************************************************/
82  /*******************************************************************************/
83  bool configureSensor() {
84  return _as5600->enableWatchdog(false) &&
85  // Normal (high) power mode
86  _as5600->setPowerMode(AS5600_POWER_MODE_NOM) &&
87  // No Hysteresis
88  _as5600->setHysteresis(AS5600_HYSTERESIS_OFF) &&
89  // analog output (0-VCC for 0-360 degrees)
90  _as5600->setOutputStage(AS5600_OUTPUT_STAGE_ANALOG_FULL) &&
91  // setup filters
92  _as5600->setSlowFilter(AS5600_SLOW_FILTER_16X) &&
93  _as5600->setFastFilterThresh(AS5600_FAST_FILTER_THRESH_SLOW_ONLY) &&
94  // Reset position settings to defaults
95  _as5600->setZPosition(0) && _as5600->setMPosition(4095) &&
96  _as5600->setMaxAngle(4095);
97  }
98 
99  /*******************************************************************************/
104  /*******************************************************************************/
105  bool readSensor() {
106  if (!_as5600->isMagnetDetected()) {
107  WS_DEBUG_PRINTLN("Magnet not detected!");
108  return false;
109  }
110 
111  // Continuously read and display angle values
112  uint16_t rawAngle = _as5600->getRawAngle();
113  uint16_t angle = _as5600->getAngle();
114 
115  WS_DEBUG_PRINT("AS5600 Raw: ");
116  WS_DEBUG_PRINT(rawAngle);
117  WS_DEBUG_PRINT(" | Scaled: ");
118  WS_DEBUG_PRINT(angle);
119 
120  // Check status conditions
121  if (_as5600->isAGCminGainOverflow()) {
122  WS_DEBUG_PRINTLN(" | MH: magnet too strong");
123  return false;
124  }
125  if (_as5600->isAGCmaxGainOverflow()) {
126  WS_DEBUG_PRINTLN(" | ML: magnet too weak");
127  return false;
128  }
129  _angle = ((float)angle / 4095.0) * 360.0;
130  return true;
131  }
132 
133  /*******************************************************************************/
141  /*******************************************************************************/
142  bool getEventRaw(sensors_event_t *rawEvent) {
143  if (!readSensor()) {
144  return false;
145  }
146  rawEvent->data[0] = _angle;
147  return true;
148  }
149 
150 protected:
151  float _angle;
152  Adafruit_AS5600 *_as5600;
153 };
154 
155 #endif // WipperSnapper_I2C_Driver_AS5600
#define WS_DEBUG_PRINT(...)
Prints debug output.
Definition: Wippersnapper.h:49
bool begin()
Initializes the AS5600 sensor and begins I2C.
Definition: WipperSnapper_I2C_Driver_AS5600.h:63
bool configureSensor()
Configures the AS5600 sensor.
Definition: WipperSnapper_I2C_Driver_AS5600.h:83
bool readSensor()
Reads the Angle sensor.
Definition: WipperSnapper_I2C_Driver_AS5600.h:105
float _angle
Current angle reading from the AS5600 sensor.
Definition: WipperSnapper_I2C_Driver_AS5600.h:151
Adafruit_AS5600 * _as5600
Pointer to AS5600 sensor object.
Definition: WipperSnapper_I2C_Driver_AS5600.h:152
Base class for I2C Drivers.
Definition: WipperSnapper_I2C_Driver.h:32
uint16_t _sensorAddress
The I2C driver&#39;s unique I2C address.
Definition: WipperSnapper_I2C_Driver.h:1324
~WipperSnapper_I2C_Driver_AS5600()
Destructor for an AS5600 sensor.
Definition: WipperSnapper_I2C_Driver_AS5600.h:50
WipperSnapper_I2C_Driver_AS5600(TwoWire *i2c, uint16_t sensorAddress)
Constructor for the AS5600 sensor.
Definition: WipperSnapper_I2C_Driver_AS5600.h:39
#define WS_DEBUG_PRINTLN(...)
Prints line from debug output.
Definition: Wippersnapper.h:52
bool getEventRaw(sensors_event_t *rawEvent)
Reads the Angle sensor with short wait for data.
Definition: WipperSnapper_I2C_Driver_AS5600.h:142
Class that provides a driver interface for a AS5600 sensor.
Definition: WipperSnapper_I2C_Driver_AS5600.h:28
TwoWire * _i2c
Pointer to the I2C driver&#39;s Wire object.
Definition: WipperSnapper_I2C_Driver.h:1323