-
Notifications
You must be signed in to change notification settings - Fork 53
As5600 #798
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
As5600 #798
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
0a43f57
Add AS5600
tyeth 78367af
clang format
tyeth dda19cd
fix(as5600): set angle correctly and log if no magnet
tyeth fc71187
fix(as5600): correct fail logic in configureSensor()
tyeth 9fc7c60
fix(as5600): change angle to be relative to 360
tyeth d2d51db
PR feedback and clang format
tyeth 7a275e5
fix(as5600): check before delete
tyeth 4258c12
chore(as5600): doxygen for readSensor
tyeth 8c0e980
fix(partitions): 4MB NoOTA in platformIO.ini
tyeth e60be99
Merge branch 'main' into as5600
tyeth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
src/components/i2c/drivers/WipperSnapper_I2C_Driver_AS5600.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
/*! | ||
* @file WipperSnapper_I2C_Driver_AS5600.h | ||
* | ||
* Device driver for the AS5600 Magnetic Angle sensor. | ||
* | ||
* Adafruit invests time and resources providing this open source code, | ||
* please support Adafruit and open-source hardware by purchasing | ||
* products from Adafruit! | ||
* | ||
* Copyright (c) Tyeth Gundry 2024 for Adafruit Industries. | ||
* | ||
* MIT license, all text here must be included in any redistribution. | ||
* | ||
*/ | ||
#ifndef WipperSnapper_I2C_Driver_AS5600_H | ||
#define WipperSnapper_I2C_Driver_AS5600_H | ||
|
||
#include <Adafruit_AS5600.h> | ||
|
||
#include "WipperSnapper_I2C_Driver.h" | ||
#include "Wippersnapper.h" | ||
|
||
/**************************************************************************/ | ||
/*! | ||
@brief Class that provides a driver interface for a AS5600 sensor. | ||
*/ | ||
/**************************************************************************/ | ||
class WipperSnapper_I2C_Driver_AS5600 : public WipperSnapper_I2C_Driver { | ||
public: | ||
/*******************************************************************************/ | ||
/*! | ||
@brief Constructor for the AS5600 sensor. | ||
@param i2c | ||
The I2C interface. | ||
@param sensorAddress | ||
7-bit device address. | ||
*/ | ||
/*******************************************************************************/ | ||
WipperSnapper_I2C_Driver_AS5600(TwoWire *i2c, uint16_t sensorAddress) | ||
: WipperSnapper_I2C_Driver(i2c, sensorAddress) { | ||
_as5600 = nullptr; | ||
_angle = 0; | ||
} | ||
|
||
/*******************************************************************************/ | ||
/*! | ||
@brief Destructor for an AS5600 sensor. | ||
*/ | ||
/*******************************************************************************/ | ||
~WipperSnapper_I2C_Driver_AS5600() { | ||
if (_as5600) { | ||
delete _as5600; | ||
_as5600 = nullptr; | ||
} | ||
} | ||
|
||
/*******************************************************************************/ | ||
/*! | ||
@brief Initializes the AS5600 sensor and begins I2C. | ||
@returns True if initialized successfully, False otherwise. | ||
*/ | ||
/*******************************************************************************/ | ||
bool begin() { | ||
_as5600 = new Adafruit_AS5600(); | ||
if (!_as5600->begin((uint8_t)_sensorAddress, _i2c)) { | ||
WS_DEBUG_PRINTLN("Failed to find AS5600 chip"); | ||
return false; | ||
} | ||
|
||
if (!configureSensor()) { | ||
WS_DEBUG_PRINTLN("Failed to configure AS5600 sensor"); | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
/*******************************************************************************/ | ||
/*! | ||
@brief Configures the AS5600 sensor. | ||
@returns True if the sensor was configured successfully, False otherwise. | ||
*/ | ||
/*******************************************************************************/ | ||
bool configureSensor() { | ||
return _as5600->enableWatchdog(false) && | ||
// Normal (high) power mode | ||
_as5600->setPowerMode(AS5600_POWER_MODE_NOM) && | ||
// No Hysteresis | ||
_as5600->setHysteresis(AS5600_HYSTERESIS_OFF) && | ||
// analog output (0-VCC for 0-360 degrees) | ||
_as5600->setOutputStage(AS5600_OUTPUT_STAGE_ANALOG_FULL) && | ||
// setup filters | ||
_as5600->setSlowFilter(AS5600_SLOW_FILTER_16X) && | ||
_as5600->setFastFilterThresh(AS5600_FAST_FILTER_THRESH_SLOW_ONLY) && | ||
// Reset position settings to defaults | ||
_as5600->setZPosition(0) && _as5600->setMPosition(4095) && | ||
_as5600->setMaxAngle(4095); | ||
} | ||
|
||
/*******************************************************************************/ | ||
/*! | ||
@brief Reads the Angle sensor. | ||
@returns True if the sensor was read successfully, False otherwise. | ||
*/ | ||
/*******************************************************************************/ | ||
bool readSensor() { | ||
if (!_as5600->isMagnetDetected()) { | ||
WS_DEBUG_PRINTLN("Magnet not detected!"); | ||
return false; | ||
} | ||
|
||
// Continuously read and display angle values | ||
uint16_t rawAngle = _as5600->getRawAngle(); | ||
uint16_t angle = _as5600->getAngle(); | ||
|
||
WS_DEBUG_PRINT("AS5600 Raw: "); | ||
WS_DEBUG_PRINT(rawAngle); | ||
WS_DEBUG_PRINT(" | Scaled: "); | ||
WS_DEBUG_PRINT(angle); | ||
|
||
// Check status conditions | ||
if (_as5600->isAGCminGainOverflow()) { | ||
WS_DEBUG_PRINTLN(" | MH: magnet too strong"); | ||
return false; | ||
} | ||
if (_as5600->isAGCmaxGainOverflow()) { | ||
WS_DEBUG_PRINTLN(" | ML: magnet too weak"); | ||
return false; | ||
} | ||
_angle = ((float)angle / 4095.0) * 360.0; | ||
return true; | ||
} | ||
|
||
/*******************************************************************************/ | ||
/*! | ||
@brief Reads the Angle sensor with short wait for data. | ||
@param rawEvent | ||
Angle sensor reading | ||
@returns True if the sensor event was obtained successfully, False | ||
otherwise. | ||
*/ | ||
/*******************************************************************************/ | ||
bool getEventRaw(sensors_event_t *rawEvent) { | ||
if (!readSensor()) { | ||
return false; | ||
} | ||
rawEvent->data[0] = _angle; | ||
return true; | ||
} | ||
|
||
protected: | ||
float _angle; ///< Current angle reading from the AS5600 sensor | ||
Adafruit_AS5600 *_as5600; ///< Pointer to AS5600 sensor object | ||
}; | ||
|
||
#endif // WipperSnapper_I2C_Driver_AS5600 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.