|
HardFOC Vortex V1 HAL
Unified HAL API for the HardFOC Vortex motor-control platform (ESP32-C6, TMC9660)
|
Singleton for managing multiple AS5047U encoder devices with indexed access. More...
#include <EncoderManager.h>
Public Member Functions | |
| bool | EnsureInitialized () noexcept |
| Ensure the encoder manager system is initialized. | |
| bool | IsInitialized () const noexcept |
| Check if the encoder manager is initialized. | |
| EncoderError | GetLastError () const noexcept |
| Get the most recent error code. | |
| EncoderError | GetSystemDiagnostics (EncoderSystemDiagnostics &diagnostics) const noexcept |
| Fill a diagnostics snapshot with current system state. | |
| bool | Deinitialize () noexcept |
| Deinitialize all encoders and release resources. | |
| As5047uHandler * | GetAs5047uHandler (uint8_t deviceIndex=ENCODER_SLOT_0) noexcept |
| Get access to AS5047U encoder handler by device index. | |
| template<typename Fn > | |
| bool | VisitDriver (uint8_t deviceIndex, Fn &&fn) noexcept |
Run fn on the AS5047U driver of one initialised slot, under both locks. | |
| bool | CreateAs5047uDevice (uint8_t deviceIndex, SpiDeviceId spiDeviceId, const As5047uConfig &config=As5047uHandler::GetDefaultConfig()) noexcept |
| Create an AS5047U encoder device on a given SPI device slot. | |
| bool | CreateAs5047uDevice (uint8_t deviceIndex, BaseSpi &spi_interface, const As5047uConfig &config=As5047uHandler::GetDefaultConfig()) noexcept |
| Create an AS5047U encoder device using a direct BaseSpi interface. | |
| bool | CreateExternalAs5047uDevice (uint8_t deviceIndex, SpiDeviceId spiDeviceId, const As5047uConfig &config=As5047uHandler::GetDefaultConfig()) noexcept |
| bool | CreateExternalAs5047uDevice (uint8_t deviceIndex, BaseSpi &spi_interface, const As5047uConfig &config=As5047uHandler::GetDefaultConfig()) noexcept |
| bool | DeleteDevice (uint8_t deviceIndex) noexcept |
| Delete an encoder device and free its slot. | |
| bool | DeleteExternalDevice (uint8_t deviceIndex) noexcept |
| uint8_t | GetDeviceCount () const noexcept |
| Get the number of active encoder devices. | |
| bool | IsDeviceValid (uint8_t deviceIndex) const noexcept |
| Check if a device index is valid and has an active device. | |
| bool | IsExternalSlotAvailable (uint8_t deviceIndex) const noexcept |
| Check if an external device slot is available for creation. | |
| void | GetActiveDeviceIndices (std::array< uint8_t, MAX_ENCODER_DEVICES > &out, size_t &count) const noexcept |
| Get indices of all active encoder devices. | |
| size_t | InitializeAllDevices (std::array< bool, MAX_ENCODER_DEVICES > &results) noexcept |
| Initialize all active encoder devices. | |
| void | GetInitializationStatus (std::array< bool, MAX_ENCODER_DEVICES > &status) const noexcept |
| Get initialization status of all device slots. | |
| void | GetAvailableDevices (std::array< const char *, MAX_ENCODER_DEVICES > &out, size_t &count) const noexcept |
| Get human-readable names of all available devices. | |
| const char * | GetDeviceType (uint8_t deviceIndex) const noexcept |
| Get device type by index. | |
| EncoderError | ReadAngle (uint8_t deviceIndex, uint16_t &angle) noexcept |
| Read angle from specific encoder device. | |
| EncoderError | ReadAngleDegrees (uint8_t deviceIndex, double &angle_degrees) noexcept |
| Read angle in degrees from specific encoder device. | |
| EncoderError | ReadVelocityRPM (uint8_t deviceIndex, double &velocity_rpm) noexcept |
| Read velocity from specific encoder device. | |
| EncoderError | ReadDiagnostics (uint8_t deviceIndex, As5047uDiagnostics &diagnostics) noexcept |
| Read diagnostics from specific encoder device. | |
| EncoderError | ReadFieldStrength (uint8_t deviceIndex, EncoderFieldStrength &field) noexcept |
| Read the AGC value and the CORDIC magnitude (magnet field strength) of one encoder. | |
| EncoderError | SetZeroPosition (uint8_t deviceIndex, uint16_t zero_position) noexcept |
| Set zero position for specific encoder device. | |
| void | ReadAllAngles (std::array< uint16_t, MAX_ENCODER_DEVICES > &angles, std::array< uint8_t, MAX_ENCODER_DEVICES > &device_indices, std::array< EncoderError, MAX_ENCODER_DEVICES > &errors, size_t &count) noexcept |
| Read angles from all active encoder devices. | |
| void | ReadAllVelocities (std::array< double, MAX_ENCODER_DEVICES > &velocities_rpm, std::array< uint8_t, MAX_ENCODER_DEVICES > &device_indices, std::array< EncoderError, MAX_ENCODER_DEVICES > &errors, size_t &count) noexcept |
| Read velocities from all active encoder devices. | |
| bool | CheckAllDevicesHealth () noexcept |
| Check health status of all active encoder devices. | |
| uint32_t | GetMeasurementCount (uint8_t deviceIndex) const noexcept |
| Get measurement statistics for monitoring specific device. | |
| uint32_t | GetCommunicationErrorCount (uint8_t deviceIndex) const noexcept |
| Get communication error count for specific device. | |
| void | DumpStatistics () const noexcept |
| Dump comprehensive system statistics to log as INFO level. Logs all device statistics, interrupt counts, and system health information. | |
| EncoderManager (const EncoderManager &)=delete | |
| EncoderManager & | operator= (const EncoderManager &)=delete |
| EncoderManager (EncoderManager &&)=delete | |
| EncoderManager & | operator= (EncoderManager &&)=delete |
Static Public Member Functions | |
| static EncoderManager & | GetInstance () noexcept |
| Get the singleton instance of EncoderManager. | |
Static Public Attributes | |
| static constexpr uint8_t | MAX_ENCODER_DEVICES = 4 |
| Maximum supported AS5047U devices. | |
| static constexpr uint8_t | ENCODER_SLOT_0 = 0 |
| static constexpr uint8_t | ENCODER_SLOT_1 = 1 |
| static constexpr uint8_t | ENCODER_SLOT_2 = 2 |
| static constexpr uint8_t | ENCODER_SLOT_3 = 3 |
| static constexpr uint8_t | ONBOARD_ENCODER_INDEX = ENCODER_SLOT_0 |
Private Member Functions | |
| EncoderManager () noexcept | |
| Private constructor for singleton pattern. | |
| ~EncoderManager () noexcept | |
| Private destructor. | |
| bool | Initialize () noexcept |
| Initialize the encoder manager system (no devices auto-created). | |
| As5047uHandler * | ReadyHandlerLocked (uint8_t deviceIndex) noexcept |
| Handler of an active, initialised slot, or nullptr. Caller holds manager_mutex_. | |
| EncoderError | FinishRead (uint8_t deviceIndex, AS5047U_Error sticky, bool count_measurement) noexcept |
| Count a read and map its sticky flags to an error. Caller holds manager_mutex_. | |
Static Private Member Functions | |
| static bool | IsValidDeviceIndex (uint8_t deviceIndex) noexcept |
| Validate that deviceIndex is within [0, MAX_ENCODER_DEVICES). | |
| static EncoderError | mapDriverError (AS5047U_Error sticky_flags) noexcept |
| Map AS5047U driver sticky error flags to generic EncoderError. | |
Private Attributes | |
| std::atomic< bool > | initialized_ {false} |
| System initialization state (atomic for thread safety). | |
| std::atomic< EncoderError > | last_error_ {EncoderError::SUCCESS} |
| Most recent error code for system-level tracking. | |
| RtosMutex | manager_mutex_ |
| Main system mutex for thread-safe operations. Uses RtosMutex for embedded RTOS compatibility. | |
| std::array< std::optional< As5047uHandler >, MAX_ENCODER_DEVICES > | as5047u_handlers_ |
| AS5047U encoder handlers (one per device slot), in place (no heap): emplaced on create, reset on delete/deinit. Protected by manager_mutex_. | |
| std::array< bool, MAX_ENCODER_DEVICES > | device_initialized_ |
| Device initialization status tracking. Tracks which devices have been successfully initialized. | |
| std::array< bool, MAX_ENCODER_DEVICES > | device_active_ |
| Device active status tracking. Tracks which device slots have active devices. | |
| CommChannelsManager * | comm_manager_ = nullptr |
| Reference to communication manager for SPI access. Used for creating encoder devices with proper transport interfaces. | |
| GpioManager * | gpio_manager_ = nullptr |
| Reference to GPIO manager for interrupt pin access. Used for configuring interrupt pins for encoder devices. | |
| std::array< std::atomic< uint32_t >, MAX_ENCODER_DEVICES > | measurement_counts_ {0} |
| Measurement and error tracking for all devices. | |
| std::array< std::atomic< uint32_t >, MAX_ENCODER_DEVICES > | communication_error_counts_ {0} |
| Communication error counts per device. | |
Singleton for managing multiple AS5047U encoder devices with indexed access.
The Vortex V1 board has no onboard encoder. All AS5047U devices are user-provisioned at runtime via CreateAs5047uDevice(). The board exposes a dedicated SPI CS line for the encoder (SpiDeviceId::AS5047U_POSITION_ENCODER, GPIO 20) plus two external CS slots — any of them can host an AS5047U.
Key Features:
Usage Example:
|
delete |
|
delete |
|
privatenoexcept |
Private constructor for singleton pattern.
|
privatenoexcept |
Private destructor.
|
noexcept |
Check health status of all active encoder devices.
|
noexcept |
Create an AS5047U encoder device using a direct BaseSpi interface.
| deviceIndex | Device slot (0–3) |
| spi_interface | Direct reference to BaseSpi interface |
| config | Optional AS5047U configuration (defaults to default config) |
|
noexcept |
Create an AS5047U encoder device on a given SPI device slot.
| deviceIndex | Device slot (0–3) |
| spiDeviceId | SPI device ID for communication (e.g. AS5047U_POSITION_ENCODER) |
| config | Optional AS5047U configuration (defaults to default config) |
|
inlinenoexcept |
|
inlinenoexcept |
|
noexcept |
Deinitialize all encoders and release resources.
|
noexcept |
Delete an encoder device and free its slot.
| deviceIndex | Device slot (0–3) |
|
inlinenoexcept |
|
noexcept |
Dump comprehensive system statistics to log as INFO level. Logs all device statistics, interrupt counts, and system health information.
|
noexcept |
Ensure the encoder manager system is initialized.
|
privatenoexcept |
Count a read and map its sticky flags to an error. Caller holds manager_mutex_.
|
noexcept |
Get indices of all active encoder devices.
| out | Fixed-size output array |
| count | Output: number of entries written |
|
noexcept |
Get access to AS5047U encoder handler by device index.
| deviceIndex | Device slot (0–3) |
|
noexcept |
Get human-readable names of all available devices.
| out | Fixed-size output array of const char* string literals |
| count | Output: number of entries written |
|
noexcept |
Get communication error count for specific device.
| deviceIndex | Device slot (0–3) |
|
noexcept |
Get the number of active encoder devices.
|
noexcept |
Get device type by index.
| deviceIndex | Device index |
|
noexcept |
Get initialization status of all device slots.
| status | Output: per-slot status |
|
staticnoexcept |
Get the singleton instance of EncoderManager.
|
inlinenoexcept |
Get the most recent error code.
|
noexcept |
Get measurement statistics for monitoring specific device.
| deviceIndex | Device slot (0–3) |
|
noexcept |
Fill a diagnostics snapshot with current system state.
| diagnostics | Output structure to populate |
|
privatenoexcept |
Initialize the encoder manager system (no devices auto-created).
|
noexcept |
Initialize all active encoder devices.
| results | Output: per-slot initialization result |
|
noexcept |
Check if a device index is valid and has an active device.
| deviceIndex | Device index to check |
|
noexcept |
Check if an external device slot is available for creation.
| deviceIndex | External device index (1, 2, or 3 only) |
|
noexcept |
Check if the encoder manager is initialized.
|
inlinestaticprivatenoexcept |
Validate that deviceIndex is within [0, MAX_ENCODER_DEVICES).
|
staticprivatenoexcept |
Map AS5047U driver sticky error flags to generic EncoderError.
| sticky_flags | Sticky error flags from AS5047U driver |
|
delete |
|
delete |
|
noexcept |
Read angles from all active encoder devices.
| angles | Output: per-slot angle readings (only valid where errors[i] == SUCCESS) |
| device_indices | Output: indices of active devices |
| errors | Output: per-slot error codes |
| count | Output: number of entries written |
|
noexcept |
Read velocities from all active encoder devices.
| velocities_rpm | Output: per-slot velocity readings |
| device_indices | Output: indices of active devices |
| errors | Output: per-slot error codes |
| count | Output: number of entries written |
|
noexcept |
Read angle from specific encoder device.
| deviceIndex | Device slot (0–3) |
| angle | Output angle value (0-16383 LSB, 14-bit) |
|
noexcept |
Read angle in degrees from specific encoder device.
| deviceIndex | Device slot (0–3) |
| angle_degrees | Output angle in degrees (0.0-359.978°) |
|
noexcept |
Read diagnostics from specific encoder device.
One SPI read of ERRFL (clear-on-read). The counters come from this manager. AGC and magnitude are not part of As5047uDiagnostics; use ReadFieldStrength().
| deviceIndex | Device slot (0–3) |
| diagnostics | Output diagnostic information |
|
noexcept |
Read the AGC value and the CORDIC magnitude (magnet field strength) of one encoder.
| deviceIndex | Device slot (0–3) |
| field | Output AGC (0–255) and magnitude (0–16383) readings |
|
noexcept |
Read velocity from specific encoder device.
| deviceIndex | Device slot (0–3) |
| velocity_rpm | Output velocity in RPM |
|
privatenoexcept |
Handler of an active, initialised slot, or nullptr. Caller holds manager_mutex_.
|
noexcept |
Set zero position for specific encoder device.
| deviceIndex | Device slot (0–3) |
| zero_position | Zero position in LSB (0-16383) |
|
inlinenoexcept |
Run fn on the AS5047U driver of one initialised slot, under both locks.
Holds the manager lock (the slot cannot be deleted) and the handler lock (no other task talks to this AS5047U) for the whole call. This replaces the raw driver pointer the manager used to hand out. Keep fn short: it blocks every other encoder call.
| deviceIndex | Device slot (0–3) |
| fn | Callable taking as5047u::AS5047U<As5047uSpiAdapter>&. |
fn ran; false if the slot is empty or not initialised.
|
private |
AS5047U encoder handlers (one per device slot), in place (no heap): emplaced on create, reset on delete/deinit. Protected by manager_mutex_.
|
private |
Reference to communication manager for SPI access. Used for creating encoder devices with proper transport interfaces.
|
private |
Communication error counts per device.
|
private |
Device active status tracking. Tracks which device slots have active devices.
|
private |
Device initialization status tracking. Tracks which devices have been successfully initialized.
|
staticconstexpr |
|
staticconstexpr |
|
staticconstexpr |
|
staticconstexpr |
|
private |
Reference to GPIO manager for interrupt pin access. Used for configuring interrupt pins for encoder devices.
|
private |
System initialization state (atomic for thread safety).
|
private |
Most recent error code for system-level tracking.
|
mutableprivate |
Main system mutex for thread-safe operations. Uses RtosMutex for embedded RTOS compatibility.
|
staticconstexpr |
Maximum supported AS5047U devices.
|
private |
Measurement and error tracking for all devices.
Measurement counts per device
|
staticconstexpr |