1+ /* !
2+ * @file WipperSnapper_I2C_Driver_AS5600.h
3+ *
4+ * Device driver for the AS5600 Magnetic Angle sensor.
5+ *
6+ * Adafruit invests time and resources providing this open source code,
7+ * please support Adafruit and open-source hardware by purchasing
8+ * products from Adafruit!
9+ *
10+ * Copyright (c) Tyeth Gundry 2024 for Adafruit Industries.
11+ *
12+ * MIT license, all text here must be included in any redistribution.
13+ *
14+ */
15+ #ifndef WipperSnapper_I2C_Driver_AS5600_H
16+ #define WipperSnapper_I2C_Driver_AS5600_H
17+
18+ #include " WipperSnapper_I2C_Driver.h"
19+ #include " Wippersnapper.h"
20+ #include < Adafruit_AS5600.h>
21+
22+ /* *************************************************************************/
23+ /* !
24+ @brief Class that provides a driver interface for a AS5600 sensor.
25+ */
26+ /* *************************************************************************/
27+ class WipperSnapper_I2C_Driver_AS5600 : public WipperSnapper_I2C_Driver {
28+ public:
29+ /* ******************************************************************************/
30+ /* !
31+ @brief Constructor for the AS5600 sensor.
32+ @param i2c
33+ The I2C interface.
34+ @param sensorAddress
35+ 7-bit device address.
36+ */
37+ /* ******************************************************************************/
38+ WipperSnapper_I2C_Driver_AS5600 (TwoWire *i2c, uint16_t sensorAddress)
39+ : WipperSnapper_I2C_Driver(i2c, sensorAddress) {
40+ _i2c = i2c;
41+ _sensorAddress = sensorAddress;
42+ _as5600 = nullptr ;
43+ _angle = 0 ;
44+ }
45+
46+ /* ******************************************************************************/
47+ /* !
48+ @brief Destructor for an AS5600 sensor.
49+ */
50+ /* ******************************************************************************/
51+ ~WipperSnapper_I2C_Driver_AS5600 () { delete _as5600; }
52+
53+ /* ******************************************************************************/
54+ /* !
55+ @brief Initializes the AS5600 sensor and begins I2C.
56+ @returns True if initialized successfully, False otherwise.
57+ */
58+ /* ******************************************************************************/
59+ bool begin () {
60+ _as5600 = new Adafruit_AS5600 ();
61+ if (!_as5600->begin ((uint8_t )_sensorAddress, _i2c)) {
62+ WS_DEBUG_PRINTLN (" Failed to find AS5600 chip" );
63+ return false ;
64+ }
65+
66+ if (!configureSensor ()) {
67+ WS_DEBUG_PRINTLN (" Failed to configure AS5600 sensor" );
68+ return false ;
69+ }
70+ return true ;
71+ }
72+
73+ /* ******************************************************************************/
74+ /* !
75+ @brief Configures the AS5600 sensor.
76+ @returns True if the sensor was configured successfully, False otherwise.
77+ */
78+ /* ******************************************************************************/
79+ bool configureSensor () {
80+ return _as5600->enableWatchdog (false ) || // Normal (high) power mode
81+ _as5600->setPowerMode (AS5600_POWER_MODE_NOM) ||
82+ // No Hysteresis
83+ _as5600->setHysteresis (AS5600_HYSTERESIS_OFF) ||
84+
85+ // analog output (0-VCC for 0-360 degrees)
86+ _as5600->setOutputStage (AS5600_OUTPUT_STAGE_ANALOG_FULL) ||
87+
88+ // setup filters
89+ _as5600->setSlowFilter (AS5600_SLOW_FILTER_16X) ||
90+ _as5600->setFastFilterThresh (AS5600_FAST_FILTER_THRESH_SLOW_ONLY) ||
91+
92+ // Reset position settings to defaults
93+ _as5600->setZPosition (0 ) ||
94+ _as5600->setMPosition (4095 ) ||
95+ _as5600->setMaxAngle (4095 );
96+
97+ return true ;
98+ }
99+
100+
101+
102+ bool readSensor () {
103+ if (!_as5600->isMagnetDetected ()) {
104+ return false ;
105+ }
106+
107+ // Continuously read and display angle values
108+ uint16_t rawAngle = _as5600->getRawAngle ();
109+ uint16_t angle = _as5600->getAngle ();
110+
111+ WS_DEBUG_PRINT (" Raw: " );
112+ WS_DEBUG_PRINT (rawAngle);
113+ WS_DEBUG_PRINT (" (0x" );
114+ WS_DEBUG_PRINT (rawAngle, HEX);
115+ WS_DEBUG_PRINT (" ) | Scaled: " );
116+ WS_DEBUG_PRINT (angle);
117+ WS_DEBUG_PRINT (" (0x" );
118+ WS_DEBUG_PRINT (angle, HEX);
119+ WS_DEBUG_PRINT (" )" );
120+
121+ // Check status conditions
122+ if (_as5600->isAGCminGainOverflow ()) {
123+ WS_DEBUG_PRINTLN (" | MH: magnet too strong" );
124+ return false ;
125+ }
126+ if (_as5600->isAGCmaxGainOverflow ()) {
127+ WS_DEBUG_PRINTLN (" | ML: magnet too weak" );
128+ return false ;
129+ }
130+ return true ;
131+ }
132+
133+
134+ /* ******************************************************************************/
135+ /* !
136+ @brief Reads the Angle sensor with short wait for data.
137+ @param rawEvent
138+ Angle sensor reading
139+ @returns True if the sensor event was obtained successfully, False
140+ otherwise.
141+ */
142+ /* ******************************************************************************/
143+ bool getEventRaw (sensors_event_t *rawEvent) {
144+ ulong start = millis ();
145+ if (!readSensor ()) {
146+ return false ;
147+ }
148+ rawEvent->data [0 ] = _angle;
149+ return true ;
150+ }
151+
152+ protected:
153+ float _angle; // /< Current angle reading from the AS5600 sensor
154+ Adafruit_AS5600 *_as5600; // /< Pointer to AS5600 sensor object
155+ };
156+
157+ #endif // WipperSnapper_I2C_Driver_AS5600
0 commit comments