|
HardFOC Vortex V1 HAL
Unified HAL API for the HardFOC Vortex motor-control platform (ESP32-C6, TMC9660)
|
Singleton for managing multiple IMU devices with indexed access and flexible device management. More...
#include <ImuManager.h>
Public Member Functions | |
| bool | EnsureInitialized () noexcept |
| Ensure the IMU manager system is initialized. | |
| bool | IsInitialized () const noexcept |
| Check if the IMU manager is initialized. | |
| ImuError | GetLastError () const noexcept |
| Get the most recent error code. | |
| ImuError | GetSystemDiagnostics (ImuSystemDiagnostics &diagnostics) const noexcept |
| Fill a diagnostics snapshot with current system state. | |
| bool | Deinitialize () noexcept |
| Deinitialize all IMUs and release resources. | |
| Bno08xHandler * | GetBno08xHandler (uint8_t deviceIndex=ONBOARD_IMU_INDEX) noexcept |
| Get access to BNO08x IMU handler by device index. | |
| template<typename Fn > | |
| bool | VisitSensor (uint8_t deviceIndex, Fn &&fn) noexcept |
Run fn on the BNO08x driver of one initialised device, under both locks. | |
| ImuError | CreateExternalBno08xDevice (uint8_t deviceIndex, uint8_t i2c_address, uint32_t i2c_speed_hz=400000, const Bno08xConfig &config=Bno08xHandler::GetDefaultConfig()) noexcept |
| Create an external BNO08x IMU device on I2C interface with specified address. | |
| ImuError | CreateExternalBno08xDevice (uint8_t deviceIndex, SpiDeviceId spiDeviceId, const Bno08xConfig &config=Bno08xHandler::GetDefaultConfig()) noexcept |
| Create an external BNO08x IMU device on SPI interface. | |
| ImuError | CreateExternalBno08xDevice (uint8_t deviceIndex, BaseI2c &i2c_interface, const Bno08xConfig &config=Bno08xHandler::GetDefaultConfig()) noexcept |
| Create an external BNO08x IMU device using direct BaseI2c interface. | |
| ImuError | CreateExternalBno08xDevice (uint8_t deviceIndex, BaseSpi &spi_interface, const Bno08xConfig &config=Bno08xHandler::GetDefaultConfig()) noexcept |
| Create an external BNO08x IMU device using direct BaseSpi interface. | |
| ImuError | DeleteExternalDevice (uint8_t deviceIndex) noexcept |
| Delete an external IMU device. | |
| uint8_t | GetDeviceCount () const noexcept |
| Get the number of active IMU 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_IMU_DEVICES > &out, size_t &count) const noexcept |
| Get indices of all active IMU devices. | |
| size_t | InitializeAllDevices (std::array< bool, MAX_IMU_DEVICES > &results) noexcept |
| Initialize all active IMU devices. | |
| void | GetInitializationStatus (std::array< bool, MAX_IMU_DEVICES > &status) const noexcept |
| Get initialization status of all device slots. | |
| void | GetAvailableDevices (std::array< const char *, MAX_IMU_DEVICES > &out, size_t &count) const noexcept |
| Get human-readable names of all available IMU devices. | |
| const char * | GetDeviceType (uint8_t deviceIndex) const noexcept |
| Get device type by index. | |
| bool | ConfigureInterrupt (uint8_t deviceIndex, ImuInterruptCallback callback=nullptr, void *user_data=nullptr) noexcept |
| Configure GPIO interrupt for BNO08x INT pin on specific device. | |
| bool | EnableInterrupt (uint8_t deviceIndex) noexcept |
| Enable BNO08x GPIO interrupt for specific device. | |
| bool | DisableInterrupt (uint8_t deviceIndex) noexcept |
| Disable BNO08x GPIO interrupt for specific device. | |
| bool | IsInterruptEnabled (uint8_t deviceIndex) const noexcept |
| Check if interrupt is configured and enabled for specific device. | |
| bool | WaitForInterrupt (uint8_t deviceIndex, uint32_t timeout_ms=0) noexcept |
| Wait for interrupt signal with timeout for specific device. | |
| uint32_t | GetInterruptCount (uint8_t deviceIndex) const noexcept |
| Get interrupt statistics for monitoring 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. | |
| ImuManager (const ImuManager &)=delete | |
| ImuManager & | operator= (const ImuManager &)=delete |
| ImuManager (ImuManager &&)=delete | |
| ImuManager & | operator= (ImuManager &&)=delete |
Static Public Member Functions | |
| static ImuManager & | GetInstance () noexcept |
| Get the singleton instance of ImuManager. | |
Static Public Attributes | |
| static constexpr uint8_t | MAX_IMU_DEVICES = 4 |
| Maximum supported IMU devices. | |
| static constexpr uint8_t | ONBOARD_IMU_INDEX = 0 |
| Onboard IMU device index (BNO08x) | |
| static constexpr uint8_t | EXTERNAL_IMU_1_INDEX = 1 |
| External IMU device 1 index. | |
| static constexpr uint8_t | EXTERNAL_IMU_2_INDEX = 2 |
| External IMU device 2 index. | |
| static constexpr uint8_t | EXTERNAL_IMU_3_INDEX = 3 |
| External IMU device 3 index. | |
Private Member Functions | |
| ImuManager () noexcept | |
| Private constructor for singleton pattern. | |
| ~ImuManager () noexcept | |
| Private destructor. | |
| bool | Initialize () noexcept |
| Initialize the IMU manager system. | |
| bool | InitializeOnboardBno08xDevice () noexcept |
| Initialize the onboard BNO08x IMU device with I2C transport. | |
| bool | InitializeInterruptGpio () noexcept |
| Initialize GPIO interrupt for BNO08x INT pin. | |
| bool | IsExternalDeviceIndex (uint8_t deviceIndex) const noexcept |
| Validate if device index is for external device. | |
| void | UpdateLastError (ImuError error, int deviceIndex=-1) noexcept |
| Store a new error code and update per-device counter. | |
Static Private Member Functions | |
| static void | GpioInterruptHandler (BaseGpio *gpio, hf_gpio_interrupt_trigger_t trigger, void *user_data) noexcept |
| GPIO interrupt handler for BNO08x INT pin. | |
Private Attributes | |
| std::atomic< bool > | initialized_ {false} |
| System initialization state (atomic for thread safety). | |
| std::atomic< ImuError > | last_error_ {ImuError::SUCCESS} |
| Most recent error code for system-level tracking. | |
| std::array< std::atomic< uint32_t >, MAX_IMU_DEVICES > | device_error_counts_ {} |
| Per-device cumulative error counters. | |
| RtosMutex | manager_mutex_ |
| Main system mutex for thread-safe operations. Uses RtosMutex for embedded RTOS compatibility. | |
| std::array< std::optional< Bno08xHandler >, MAX_IMU_DEVICES > | bno08x_handlers_ |
| Array of BNO08x IMU handlers (one per device slot). Uses unique_ptr for exclusive ownership of each handler. Protected by RtosMutex for thread-safe access. | |
| std::array< bool, MAX_IMU_DEVICES > | device_initialized_ |
| Device initialization status tracking. Tracks which devices have been successfully initialized. | |
| std::array< bool, MAX_IMU_DEVICES > | device_active_ |
| Device active status tracking. Tracks which device slots have active devices. | |
| bool | onboard_device_created_ = false |
| Onboard device creation tracking. Tracks if onboard device has been created. | |
| std::array< int, MAX_IMU_DEVICES > | i2c_device_indices_ |
| Track I2C device indices created by ImuManager for proper cleanup. This is needed because external I2C devices are managed by ImuManager, but their actual I2C handles are managed by CommChannelsManager. We need to keep track of which external I2C devices were created by ImuManager to ensure they are deleted when the ImuManager is deinitialized. | |
| CommChannelsManager * | comm_manager_ = nullptr |
| Reference to communication manager for I2C/SPI access. Used for creating IMU devices with proper transport interfaces. | |
| GpioManager * | gpio_manager_ = nullptr |
| Reference to GPIO manager for interrupt pin access. Used for configuring interrupt pins for IMU devices. | |
| BaseGpio * | onboard_bno_reset_gpio_ {nullptr} |
| PCAL95555 nets for onboard BNO08x (Vortex): RSTN, BOOTN, INT — kept alive for handler lifetime. | |
| BaseGpio * | onboard_bno_boot_gpio_ {nullptr} |
| BaseGpio * | onboard_bno_int_gpio_ {nullptr} |
| BaseGpio * | interrupt_gpio_ = nullptr |
| GPIO interrupt support for onboard device. External devices may have their own interrupt configurations. | |
| ImuInterruptCallback | interrupt_callback_ {nullptr} |
| Plain function pointer (no heap) | |
| void * | interrupt_callback_user_ {nullptr} |
| bool | interrupt_configured_ = false |
| bool | interrupt_enabled_ = false |
| std::atomic< uint32_t > | interrupt_count_ {0} |
| SignalSemaphore | interrupt_semaphore_ {"VxImuInt"} |
| Signalled per INT edge; WaitForInterrupt() takes it. | |
| bool | interrupt_semaphore_ready_ = false |
Singleton for managing multiple IMU devices with indexed access and flexible device management.
This manager provides a global singleton for managing IMU devices, following the same pattern as MotorController:
Key Features:
Device Indexing:
Usage Example:
|
delete |
|
delete |
|
privatenoexcept |
Private constructor for singleton pattern.
|
privatenoexcept |
Private destructor.
|
noexcept |
Configure GPIO interrupt for BNO08x INT pin on specific device.
| deviceIndex | Device index (0=onboard, 1-3=external) |
| callback | Optional callback function executed in interrupt context (keep minimal) |
|
noexcept |
Create an external BNO08x IMU device using direct BaseI2c interface.
| deviceIndex | External device index (1, 2, or 3 only) |
| i2c_interface | Direct reference to BaseI2c interface |
| config | Optional BNO08x configuration (defaults to default config) |
|
noexcept |
Create an external BNO08x IMU device using direct BaseSpi interface.
| deviceIndex | External device index (1, 2, or 3 only) |
| spi_interface | Direct reference to BaseSpi interface |
| config | Optional BNO08x configuration (defaults to default config) |
|
noexcept |
Create an external BNO08x IMU device on SPI interface.
| deviceIndex | External device index (1, 2, or 3 only) |
| spiDeviceId | SPI device ID for communication |
| config | Optional BNO08x configuration (defaults to default config) |
|
noexcept |
Create an external BNO08x IMU device on I2C interface with specified address.
| deviceIndex | External device index (1, 2, or 3 only) |
| i2c_address | 7-bit I2C device address (0x08-0x77) |
| i2c_speed_hz | I2C speed in Hz (default: 400000 for 400kHz) |
| config | Optional BNO08x configuration (defaults to default config) |
|
noexcept |
Deinitialize all IMUs and release resources.
|
noexcept |
Delete an external IMU device.
| deviceIndex | External device index (1, 2, or 3 only) |
|
noexcept |
Disable BNO08x GPIO interrupt for specific device.
| deviceIndex | Device index (0=onboard, 1-3=external) |
|
noexcept |
Dump comprehensive system statistics to log as INFO level. Logs all device statistics, interrupt counts, and system health information.
|
noexcept |
Enable BNO08x GPIO interrupt for specific device.
| deviceIndex | Device index (0=onboard, 1-3=external) |
|
noexcept |
Ensure the IMU manager system is initialized.
|
noexcept |
Get indices of all active IMU devices.
| out | Fixed-size output array |
| count | Output: number of entries written |
|
noexcept |
Get human-readable names of all available IMU devices.
| out | Fixed-size output array of const char* string literals |
| count | Output: number of entries written |
|
noexcept |
Get access to BNO08x IMU handler by device index.
| deviceIndex | Device index (0=onboard, 1-3=external) |
|
noexcept |
Get the number of active IMU 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 ImuManager.
|
noexcept |
Get interrupt statistics for monitoring for specific device.
| deviceIndex | Device index (0=onboard, 1-3=external) |
|
inlinenoexcept |
Get the most recent error code.
|
noexcept |
Fill a diagnostics snapshot with current system state.
| diagnostics | Output structure to populate |
|
staticprivatenoexcept |
GPIO interrupt handler for BNO08x INT pin.
| gpio | Pointer to the GPIO that triggered interrupt |
| trigger | Interrupt trigger type |
| user_data | Pointer to user data (ImuManager instance) |
|
privatenoexcept |
Initialize the IMU manager system.
|
noexcept |
Initialize all active IMU devices.
| results | Output: per-slot initialization result |
|
privatenoexcept |
Initialize GPIO interrupt for BNO08x INT pin.
|
privatenoexcept |
Initialize the onboard BNO08x IMU device with I2C transport.
|
noexcept |
Check if a device index is valid and has an active device.
| deviceIndex | Device index to check |
|
privatenoexcept |
Validate if device index is for external device.
| deviceIndex | Device index to validate |
|
noexcept |
Check if an external device slot is available for creation.
| deviceIndex | External device index (1, 2, or 3 only) |
|
noexcept |
Check if the IMU manager is initialized.
|
noexcept |
Check if interrupt is configured and enabled for specific device.
| deviceIndex | Device index (0=onboard, 1-3=external) |
|
delete |
|
delete |
|
privatenoexcept |
Store a new error code and update per-device counter.
| error | The error to record |
| deviceIndex | Optional device index to bump its error counter |
|
inlinenoexcept |
Run fn on the BNO08x driver of one initialised device, under both locks.
Holds the manager lock (the device cannot be deleted) and the handler lock (no other task talks to this BNO08x) for the whole call. Use it for driver calls the manager does not wrap: Update(), EnableSensor(), HasNewData(), GetLatest(), ... This replaces the raw GetSensor() pointer, which bypassed the handler mutex.
| deviceIndex | Device index (0=onboard, 1-3=external) |
| fn | Callable taking IBno08xDriverOps&. |
fn ran; false if the device is absent or not initialised.
|
noexcept |
Wait for interrupt signal with timeout for specific device.
| deviceIndex | Device index (0=onboard, 1-3=external) |
| timeout_ms | Timeout in milliseconds (0 = wait indefinitely) |
|
private |
Array of BNO08x IMU handlers (one per device slot). Uses unique_ptr for exclusive ownership of each handler. Protected by RtosMutex for thread-safe access.
Handlers in place (no heap): emplaced on create, reset on delete/deinit.
|
private |
Reference to communication manager for I2C/SPI access. Used for creating IMU devices with proper transport interfaces.
|
private |
Device active status tracking. Tracks which device slots have active devices.
|
private |
Per-device cumulative error counters.
|
private |
Device initialization status tracking. Tracks which devices have been successfully initialized.
|
staticconstexpr |
External IMU device 1 index.
|
staticconstexpr |
External IMU device 2 index.
|
staticconstexpr |
External IMU device 3 index.
|
private |
Reference to GPIO manager for interrupt pin access. Used for configuring interrupt pins for IMU devices.
|
private |
Track I2C device indices created by ImuManager for proper cleanup. This is needed because external I2C devices are managed by ImuManager, but their actual I2C handles are managed by CommChannelsManager. We need to keep track of which external I2C devices were created by ImuManager to ensure they are deleted when the ImuManager is deinitialized.
|
private |
System initialization state (atomic for thread safety).
|
private |
Plain function pointer (no heap)
|
private |
|
private |
|
private |
|
private |
|
private |
GPIO interrupt support for onboard device. External devices may have their own interrupt configurations.
Raw pointer for compatibility
|
private |
Signalled per INT edge; WaitForInterrupt() takes it.
|
private |
|
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 IMU devices.
|
private |
|
private |
|
private |
PCAL95555 nets for onboard BNO08x (Vortex): RSTN, BOOTN, INT — kept alive for handler lifetime.
|
private |
Onboard device creation tracking. Tracks if onboard device has been created.
|
staticconstexpr |
Onboard IMU device index (BNO08x)