HF-AS5047U Driver  0.1.0-dev
HF-AS5047U C++ Driver
Loading...
Searching...
No Matches
HF-AS5047U Driver

Portable C++20 driver for the AS5047U magnetic encoder from ams with SPI interface, CRC protection, DAEC, and DFSβ„’

C++ License CI Docs

πŸ“š Table of Contents

  1. Overview
  2. Features
  3. Quick Start
  4. Installation
  5. API Reference
  6. Examples
  7. Documentation
  8. Contributing
  9. License

πŸ“¦ Overview

πŸ“– πŸ“šπŸŒ Live Complete Documentation - Interactive guides, examples, and step-by-step tutorials

HF-AS5047U is a portable C++20 driver for the AS5047U magnetic encoder from ams. It delivers fast 14‑bit absolute angle readings over SPI, optional CRC protection and advanced features like Dynamic Angle Error Compensation (DAEC) and an adaptive Dynamic Filter System (DFSβ„’). The sensor can also output incremental (A/B/I) and 3‑phase commutation (UVW) signals or a PWM encoded angle, making it a drop‑in replacement for optical encoders in high-performance motor control and robotics.

The driver uses a CRTP-based SpiInterface for hardware abstraction, allowing it to run on any platform (ESP32, STM32, Arduino, etc.) with zero runtime overhead. It implements all major features from the AS5047U datasheet including absolute angle readout with/without DAEC, velocity measurement, AGC and magnetic field diagnostics, ABI/UVW/PWM interface configuration, error/status flag handling, and full OTP programming sequence.

✨ Features

  • βœ… 14-bit absolute angle with optional CRC check
  • βœ… DAEC (Dynamic Angle Error Compensation) for low-latency angle correction
  • βœ… DFSβ„’ (Dynamic Filter System) adaptive noise filtering
  • βœ… Stray-field immunity for robust operation
  • βœ… Incremental ABI outputs up to 4096 PPR
  • βœ… UVW commutation with programmable pole pairs
  • βœ… PWM output option
  • βœ… Diagnostics for AGC, magnitude and error flags
  • βœ… OTP memory for permanent configuration
  • βœ… Hardware Agnostic: SPI interface for platform independence
  • βœ… Modern C++: C++20 with CRTP-based design
  • βœ… Zero Overhead: CRTP-based design for compile-time polymorphism

πŸš€ Quick Start

#include "as5047u.hpp"
// 1. Implement the SPI interface (see platform_integration.md)
class MySpi : public as5047u::SpiInterface<MySpi> {
public:
void transfer(const uint8_t* tx, uint8_t* rx, size_t len) {
// Your SPI transfer implementation
}
};
// 2. Create driver instance
MySpi spi;
as5047u::AS5047U encoder(spi, FrameFormat::SPI_24); // 24-bit frames with CRC
// 3. Read angle / velocity (unit-selected API)
float angle_deg = encoder.GetAngle(as5047u::AngleUnit::Degrees); // 0..360
float angle_rad = encoder.GetAngle(as5047u::AngleUnit::Radians); // 0..2Ο€
float vel_rpm = encoder.GetVelocity(as5047u::VelocityUnit::Rpm);
float vel_dps = encoder.GetVelocity(as5047u::VelocityUnit::DegPerSec);
// 4. Diagnostics
uint8_t agc = encoder.GetAGC(); // automatic gain control
uint16_t mag = encoder.GetMagnitude(); // magnetic magnitude
uint16_t errors = encoder.GetErrorFlags(); // current error flags
Driver for AMS AS5047U Magnetic Rotary Position Sensor (C++21)
AS5047U magnetic rotary sensor driver class.
Definition as5047u.hpp:83
CRTP-based template interface for SPI bus operations.
Definition as5047u_spi_interface.hpp:36
void transfer(const uint8_t *tx, uint8_t *rx, std::size_t len)
Perform a full-duplex SPI data transfer.
Definition as5047u_spi_interface.hpp:51

For detailed setup, see Installation and Quick Start Guide.

πŸ”§ Installation

  1. Clone or copy the driver files into your project
  2. Implement the SPI interface for your platform (see Platform Integration)
  3. Include the header in your code:
    #include "as5047u.hpp"
  4. Compile with a C++20 or newer compiler

For detailed installation instructions, see docs/installation.md.

πŸ“– API Reference

Method Description
GetAngle(AngleUnit) Read absolute angle in selected unit (LSB/deg/rad)
GetVelocity(VelocityUnit) Read velocity in selected unit (LSB/deg/s/rad/s/RPM)
GetAngle() Read 14-bit compensated absolute angle
GetRawAngle() Read 14-bit raw absolute angle
GetVelocity() Read signed 14-bit velocity (LSB units)
GetVelocityDegPerSec() Velocity in degrees/sec
GetVelocityRPM() Velocity in revolutions per minute
GetAGC() Read AGC (0–255) value
GetMagnitude() Read magnetic field magnitude
GetErrorFlags() Read and clear error/status flags
SetZeroPosition() Set new zero offset
SetDirection() Set rotation direction (CW or CCW)
SetABIResolution() Set ABI output resolution (10–14 bits)
SetUVWPolePairs() Set UVW pole pairs (1–7)
ConfigureInterface() Enable/disable ABI, UVW, PWM
SetDynamicAngleCompensation() Enable/disable DAEC
SetAdaptiveFilter() Enable/disable adaptive filter (DFS)
SetFilterPreset() Set filter from preset (LowNoise, Balanced, HighBandwidth)
GetAdaptiveFilterEnabled() Read whether adaptive filter is enabled
GetFilterParameters() Read current K_min and K_max register codes
ProgramOTP() Program current settings into OTP

For complete API documentation, see docs/api_reference.md.

Compatibility note: the unit-specific helpers (GetAngleDegrees(), GetVelocityRPM(), etc.) remain available for existing code, but new code should prefer the unit-enum overloads.

πŸ“Š Examples

For ESP32 examples and integration tests, see the examples/esp32 directory.

Detailed example walkthroughs are available in docs/examples.md.

πŸ“š Documentation

For complete documentation, see the docs directory.

🀝 Contributing

Pull requests and suggestions are welcome! Please follow the existing code style and include tests for new features.

πŸ“„ License

This project is licensed under the GNU General Public License v3.0. See the [LICENSE](LICENSE) file for details.