2024-11-27 23:54:47 +00:00
|
|
|
/**
|
|
|
|
|
* @file BNO08x.cpp
|
|
|
|
|
* @author Myles Parfeniuk
|
|
|
|
|
*/
|
|
|
|
|
|
2023-11-07 05:10:02 +00:00
|
|
|
#include "BNO08x.hpp"
|
2024-12-05 02:12:10 +00:00
|
|
|
#include "BNO08xPrivateTypes.hpp"
|
|
|
|
|
|
|
|
|
|
using namespace BNO08xPrivateTypes;
|
2023-11-07 05:10:02 +00:00
|
|
|
|
|
|
|
|
/**
|
2023-11-16 08:43:45 +00:00
|
|
|
* @brief BNO08x imu constructor.
|
|
|
|
|
*
|
2023-11-07 05:10:02 +00:00
|
|
|
* Construct a BNO08x object for managing a BNO08x sensor.
|
2023-11-16 08:43:45 +00:00
|
|
|
*
|
2024-12-05 04:07:40 +00:00
|
|
|
* @param imu_config Configuration settings (optional), default settings can be seen in
|
|
|
|
|
* bno08x_config_t
|
2023-11-07 05:10:02 +00:00
|
|
|
* @return void, nothing to return
|
|
|
|
|
*/
|
2023-11-16 08:43:45 +00:00
|
|
|
BNO08x::BNO08x(bno08x_config_t imu_config)
|
2024-12-05 06:09:15 +00:00
|
|
|
: rpt(bno08x_reports_t(&sync_ctx))
|
2024-11-23 01:05:03 +00:00
|
|
|
, data_proc_task_hdl(NULL)
|
2024-11-20 05:32:31 +00:00
|
|
|
, sh2_HAL_service_task_hdl(NULL)
|
2024-11-23 01:05:03 +00:00
|
|
|
, cb_task_hdl(NULL)
|
2024-11-20 19:12:11 +00:00
|
|
|
, sem_kill_tasks(NULL)
|
2024-11-27 23:54:47 +00:00
|
|
|
, queue_rx_sensor_event(xQueueCreate(10, sizeof(sh2_SensorEvent_t)))
|
2024-12-01 04:34:58 +00:00
|
|
|
, queue_cb_report_id(xQueueCreate(CONFIG_ESP32_BNO08X_CB_QUEUE_SZ, sizeof(uint8_t)))
|
2024-03-02 10:55:33 +00:00
|
|
|
, imu_config(imu_config)
|
2024-11-14 06:42:21 +00:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-17 05:36:11 +00:00
|
|
|
/**
|
|
|
|
|
* @brief BNO08x imu deconstructor.
|
|
|
|
|
*
|
|
|
|
|
* Deconstructs a BNO08x object and releases any utilized resources.
|
|
|
|
|
*
|
2024-11-18 00:37:32 +00:00
|
|
|
* @return void, nothing to return.
|
2024-11-17 05:36:11 +00:00
|
|
|
*/
|
2024-11-14 06:42:21 +00:00
|
|
|
BNO08x::~BNO08x()
|
|
|
|
|
{
|
2024-11-27 23:54:47 +00:00
|
|
|
// deinitialize tasks if they have been initialized
|
|
|
|
|
ESP_ERROR_CHECK(deinit_tasks());
|
2024-11-14 06:42:21 +00:00
|
|
|
|
2024-11-20 01:02:16 +00:00
|
|
|
// deinitialize sh2 HAL if it has been initialized
|
|
|
|
|
ESP_ERROR_CHECK(deinit_sh2_HAL());
|
2024-11-14 09:36:28 +00:00
|
|
|
|
|
|
|
|
// deinitialize hint ISR if it has been initialized
|
|
|
|
|
ESP_ERROR_CHECK(deinit_hint_isr());
|
|
|
|
|
|
2024-11-27 23:54:47 +00:00
|
|
|
// deinitialize spi if has been initialized
|
|
|
|
|
ESP_ERROR_CHECK(deinit_spi());
|
|
|
|
|
|
2024-11-14 09:36:28 +00:00
|
|
|
// deinitialize GPIO if they have been initialized
|
|
|
|
|
ESP_ERROR_CHECK(deinit_gpio());
|
2024-11-14 06:42:21 +00:00
|
|
|
|
2024-11-20 19:12:11 +00:00
|
|
|
// delete all semaphores
|
2024-12-05 06:09:15 +00:00
|
|
|
vSemaphoreDelete(sync_ctx.sh2_HAL_lock);
|
|
|
|
|
vSemaphoreDelete(sync_ctx.data_lock);
|
2024-11-20 19:12:11 +00:00
|
|
|
if (sem_kill_tasks != NULL)
|
|
|
|
|
vSemaphoreDelete(sem_kill_tasks);
|
|
|
|
|
|
2024-11-14 06:42:21 +00:00
|
|
|
// delete event groups
|
2024-12-05 06:09:15 +00:00
|
|
|
vEventGroupDelete(sync_ctx.evt_grp_task);
|
|
|
|
|
vEventGroupDelete(sync_ctx.evt_grp_rpt_en);
|
|
|
|
|
vEventGroupDelete(sync_ctx.evt_grp_rpt_data_available);
|
2024-11-20 05:32:31 +00:00
|
|
|
|
|
|
|
|
// delete all queues
|
|
|
|
|
vQueueDelete(queue_rx_sensor_event);
|
2024-11-23 01:05:03 +00:00
|
|
|
vQueueDelete(queue_cb_report_id);
|
2024-11-14 06:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Initializes BNO08x sensor
|
|
|
|
|
*
|
2024-11-17 05:36:11 +00:00
|
|
|
* Resets sensor and goes through initialization process.
|
2024-12-05 04:07:40 +00:00
|
|
|
* Configures GPIO, required ISRs, and launches two tasks, one to manage SPI transactions, another
|
|
|
|
|
* to process any received data.
|
2024-11-14 06:42:21 +00:00
|
|
|
*
|
2024-11-18 00:37:32 +00:00
|
|
|
* @return True if initialization was success, false if otherwise.
|
2024-11-14 06:42:21 +00:00
|
|
|
*/
|
|
|
|
|
bool BNO08x::initialize()
|
|
|
|
|
{
|
2024-11-20 01:02:16 +00:00
|
|
|
|
2024-11-14 09:36:28 +00:00
|
|
|
// initialize configuration arguments
|
|
|
|
|
if (init_config_args() != ESP_OK)
|
|
|
|
|
return false;
|
2024-11-14 06:42:21 +00:00
|
|
|
|
2024-11-14 09:36:28 +00:00
|
|
|
// initialize GPIO
|
|
|
|
|
if (init_gpio() != ESP_OK)
|
|
|
|
|
return false;
|
2024-11-14 06:42:21 +00:00
|
|
|
|
2024-11-14 09:36:28 +00:00
|
|
|
// initialize HINT ISR
|
|
|
|
|
if (init_hint_isr() != ESP_OK)
|
2024-11-14 06:42:21 +00:00
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
// initialize SPI
|
2024-11-14 09:36:28 +00:00
|
|
|
if (init_spi() != ESP_OK)
|
2024-11-14 06:42:21 +00:00
|
|
|
return false;
|
|
|
|
|
|
2024-11-20 01:02:16 +00:00
|
|
|
// initialize SH2 HAL
|
|
|
|
|
if (init_sh2_HAL() != ESP_OK)
|
2024-11-14 06:42:21 +00:00
|
|
|
return false;
|
|
|
|
|
|
2024-11-20 05:32:31 +00:00
|
|
|
// initialize tasks
|
|
|
|
|
if (init_tasks() != ESP_OK)
|
|
|
|
|
return false;
|
|
|
|
|
|
2025-04-12 22:13:07 +01:00
|
|
|
// clang-format off
|
2024-11-18 00:37:32 +00:00
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
2024-11-14 09:36:28 +00:00
|
|
|
ESP_LOGI(TAG, "Successfully initialized....");
|
2024-11-18 00:37:32 +00:00
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
2024-11-14 09:36:28 +00:00
|
|
|
return true;
|
2024-11-14 06:42:21 +00:00
|
|
|
}
|
2024-03-04 06:55:01 +00:00
|
|
|
|
2024-11-20 05:32:31 +00:00
|
|
|
/**
|
|
|
|
|
* @brief Static function used to launch data processing task.
|
|
|
|
|
*
|
|
|
|
|
* Used such that data_proc_task() can be non-static class member.
|
|
|
|
|
*
|
|
|
|
|
* @param arg void pointer to BNO08x imu object
|
|
|
|
|
* @return void, nothing to return
|
|
|
|
|
*/
|
|
|
|
|
void BNO08x::data_proc_task_trampoline(void* arg)
|
|
|
|
|
{
|
2024-12-05 04:07:40 +00:00
|
|
|
BNO08x* imu = (BNO08x*) arg; // cast argument received by xTaskCreate ("this" pointer to imu
|
|
|
|
|
// object created by constructor call)
|
2024-11-20 05:32:31 +00:00
|
|
|
imu->data_proc_task(); // launch data processing task task from object
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-12-05 04:07:40 +00:00
|
|
|
* @brief Task responsible for parsing/handling sensor events sent by SH2 HAL and updating data that
|
|
|
|
|
* is returned to user.
|
2024-11-20 05:32:31 +00:00
|
|
|
*
|
|
|
|
|
* @return void, nothing to return
|
|
|
|
|
*/
|
|
|
|
|
void BNO08x::data_proc_task()
|
|
|
|
|
{
|
2024-11-20 19:12:11 +00:00
|
|
|
EventBits_t evt_grp_bno08x_task_bits = 0U;
|
2024-11-27 23:54:47 +00:00
|
|
|
BaseType_t queue_rx_success = pdFALSE;
|
2024-11-20 05:32:31 +00:00
|
|
|
sh2_SensorEvent_t sensor_evt;
|
|
|
|
|
sh2_SensorValue_t sensor_val;
|
|
|
|
|
|
2024-11-27 23:54:47 +00:00
|
|
|
do
|
2024-11-20 05:32:31 +00:00
|
|
|
{
|
2024-11-27 23:54:47 +00:00
|
|
|
|
|
|
|
|
if (queue_rx_success == pdTRUE)
|
2024-11-20 05:32:31 +00:00
|
|
|
{
|
2024-11-27 23:54:47 +00:00
|
|
|
if (sh2_decodeSensorEvent(&sensor_val, &sensor_evt) != SH2_ERR)
|
|
|
|
|
handle_sensor_report(&sensor_val);
|
|
|
|
|
}
|
2024-11-20 19:12:11 +00:00
|
|
|
|
2024-11-27 23:54:47 +00:00
|
|
|
queue_rx_success = xQueueReceive(queue_rx_sensor_event, &sensor_evt, portMAX_DELAY);
|
2024-12-05 06:09:15 +00:00
|
|
|
evt_grp_bno08x_task_bits = xEventGroupGetBits(sync_ctx.evt_grp_task);
|
2024-11-20 19:12:11 +00:00
|
|
|
|
2024-11-27 23:54:47 +00:00
|
|
|
} while (evt_grp_bno08x_task_bits & EVT_GRP_BNO08x_TASKS_RUNNING);
|
2024-11-20 19:12:11 +00:00
|
|
|
|
|
|
|
|
xSemaphoreGive(sem_kill_tasks); // signal to deconstructor deletion is completed
|
2024-11-27 23:54:47 +00:00
|
|
|
init_status.data_proc_task = false;
|
2024-11-20 19:12:11 +00:00
|
|
|
vTaskDelete(NULL);
|
2024-11-20 05:32:31 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-23 01:05:03 +00:00
|
|
|
/**
|
|
|
|
|
* @brief Static function used to launch sh2 HAL service task.
|
|
|
|
|
*
|
|
|
|
|
* Used such that sh2_HAL_service_task() can be non-static class member.
|
|
|
|
|
*
|
|
|
|
|
* @param arg void pointer to BNO08x imu object
|
|
|
|
|
* @return void, nothing to return
|
|
|
|
|
*/
|
2024-11-20 05:32:31 +00:00
|
|
|
void BNO08x::sh2_HAL_service_task_trampoline(void* arg)
|
|
|
|
|
{
|
2024-12-05 04:07:40 +00:00
|
|
|
BNO08x* imu = (BNO08x*) arg; // cast argument received by xTaskCreate ("this" pointer to imu
|
|
|
|
|
// object created by constructor call)
|
2024-11-20 05:32:31 +00:00
|
|
|
imu->sh2_HAL_service_task(); // launch data processing task task from object
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-23 01:05:03 +00:00
|
|
|
/**
|
2024-12-05 04:07:40 +00:00
|
|
|
* @brief Task responsible for calling shtp_service() when HINT is asserted to dispatch any sh2 HAL
|
|
|
|
|
* lib callbacks.
|
2024-11-23 01:05:03 +00:00
|
|
|
*
|
|
|
|
|
* @return void, nothing to return
|
|
|
|
|
*/
|
2024-11-20 05:32:31 +00:00
|
|
|
void BNO08x::sh2_HAL_service_task()
|
|
|
|
|
{
|
2024-11-20 19:12:11 +00:00
|
|
|
EventBits_t evt_grp_bno08x_task_bits = 0U;
|
2025-04-12 22:13:07 +01:00
|
|
|
|
2024-12-26 23:39:01 +00:00
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_DEBUG_STATEMENTS
|
|
|
|
|
int64_t last_hint_time = esp_timer_get_time();
|
|
|
|
|
int64_t current_hint_time;
|
|
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
2024-11-20 19:12:11 +00:00
|
|
|
|
2024-11-27 23:54:47 +00:00
|
|
|
do
|
2024-11-20 05:32:31 +00:00
|
|
|
{
|
2024-11-20 19:12:11 +00:00
|
|
|
|
2024-11-27 23:54:47 +00:00
|
|
|
if (evt_grp_bno08x_task_bits & EVT_GRP_BNO08x_TASK_RESET_OCCURRED)
|
2024-11-20 05:32:31 +00:00
|
|
|
{
|
2024-12-26 23:39:01 +00:00
|
|
|
if (re_enable_reports() != ESP_OK)
|
2024-11-27 23:54:47 +00:00
|
|
|
{
|
|
|
|
|
// clang-format off
|
2024-12-26 23:39:01 +00:00
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
2024-11-27 23:54:47 +00:00
|
|
|
ESP_LOGE(TAG, "Failed to re-enable enabled reports after IMU reset.");
|
|
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-11-20 19:12:11 +00:00
|
|
|
|
2024-11-27 23:54:47 +00:00
|
|
|
if (evt_grp_bno08x_task_bits & EVT_GRP_BNO08x_TASK_HINT_ASSRT_BIT)
|
|
|
|
|
{
|
2024-11-20 19:12:11 +00:00
|
|
|
lock_sh2_HAL();
|
|
|
|
|
sh2_service();
|
|
|
|
|
unlock_sh2_HAL();
|
|
|
|
|
}
|
2024-11-27 23:54:47 +00:00
|
|
|
|
2024-12-05 06:09:15 +00:00
|
|
|
evt_grp_bno08x_task_bits = xEventGroupWaitBits(sync_ctx.evt_grp_task,
|
|
|
|
|
EVT_GRP_BNO08x_TASK_HINT_ASSRT_BIT | EVT_GRP_BNO08x_TASK_RESET_OCCURRED, pdFALSE, pdFALSE, portMAX_DELAY);
|
2024-11-27 23:54:47 +00:00
|
|
|
|
2024-12-26 23:39:01 +00:00
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_DEBUG_STATEMENTS
|
|
|
|
|
current_hint_time = esp_timer_get_time();
|
|
|
|
|
ESP_LOGW(TAG, "HINT Asserted, time since last assertion: %lldus", current_hint_time - last_hint_time);
|
|
|
|
|
last_hint_time = current_hint_time;
|
|
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
2024-11-27 23:54:47 +00:00
|
|
|
} while (evt_grp_bno08x_task_bits & EVT_GRP_BNO08x_TASKS_RUNNING);
|
2024-11-20 19:12:11 +00:00
|
|
|
|
|
|
|
|
xSemaphoreGive(sem_kill_tasks); // signal to deconstructor deletion is completed
|
2024-11-27 23:54:47 +00:00
|
|
|
init_status.sh2_HAL_service_task = false;
|
2024-11-20 19:12:11 +00:00
|
|
|
vTaskDelete(NULL);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-11-23 01:05:03 +00:00
|
|
|
* @brief Static function used to launch cb_task task.
|
2024-11-20 19:12:11 +00:00
|
|
|
*
|
2024-11-23 01:05:03 +00:00
|
|
|
* Used such that cb_task() can be non-static class member.
|
2024-11-20 19:12:11 +00:00
|
|
|
*
|
2024-11-23 01:05:03 +00:00
|
|
|
* @param arg void pointer to BNO08x imu object
|
2024-11-20 19:12:11 +00:00
|
|
|
* @return void, nothing to return
|
|
|
|
|
*/
|
2024-11-23 01:05:03 +00:00
|
|
|
void BNO08x::cb_task_trampoline(void* arg)
|
2024-11-20 19:12:11 +00:00
|
|
|
{
|
2024-12-05 04:07:40 +00:00
|
|
|
BNO08x* imu = (BNO08x*) arg; // cast argument received by xTaskCreate ("this" pointer to imu
|
|
|
|
|
// object created by constructor call)
|
2024-11-23 01:05:03 +00:00
|
|
|
imu->cb_task(); // launch data processing task task from object
|
2024-11-20 19:12:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-11-23 01:05:03 +00:00
|
|
|
* @brief Task responsible for executing callbacks registered with register_cb().
|
2024-11-20 19:12:11 +00:00
|
|
|
*
|
|
|
|
|
* @return void, nothing to return
|
|
|
|
|
*/
|
2024-11-23 01:05:03 +00:00
|
|
|
void BNO08x::cb_task()
|
2024-11-20 05:32:31 +00:00
|
|
|
{
|
2024-11-23 01:05:03 +00:00
|
|
|
EventBits_t evt_grp_bno08x_task_bits = 0U;
|
2024-11-27 23:54:47 +00:00
|
|
|
uint8_t rpt_ID = 0;
|
2024-11-21 07:47:29 +00:00
|
|
|
|
2024-11-27 23:54:47 +00:00
|
|
|
do
|
2024-11-20 05:32:31 +00:00
|
|
|
{
|
2024-11-27 23:54:47 +00:00
|
|
|
// execute callbacks
|
2024-12-05 06:09:15 +00:00
|
|
|
for (auto& cb_entry : sync_ctx.cb_list)
|
2024-12-05 02:12:10 +00:00
|
|
|
{
|
|
|
|
|
BNO08xCbGeneric* cb_ptr = nullptr;
|
|
|
|
|
|
|
|
|
|
if (auto* ptr = etl::get_if<BNO08xCbParamVoid>(&cb_entry))
|
|
|
|
|
cb_ptr = ptr;
|
|
|
|
|
|
|
|
|
|
else if (auto* ptr = etl::get_if<BNO08xCbParamRptID>(&cb_entry))
|
|
|
|
|
cb_ptr = ptr;
|
|
|
|
|
|
|
|
|
|
if (cb_ptr != nullptr)
|
|
|
|
|
handle_cb(rpt_ID, cb_ptr);
|
|
|
|
|
}
|
2024-11-27 23:54:47 +00:00
|
|
|
|
2024-11-23 01:05:03 +00:00
|
|
|
xQueueReceive(queue_cb_report_id, &rpt_ID, portMAX_DELAY);
|
2024-11-21 09:01:35 +00:00
|
|
|
|
2024-12-05 06:09:15 +00:00
|
|
|
evt_grp_bno08x_task_bits = xEventGroupGetBits(sync_ctx.evt_grp_task);
|
2024-11-21 09:01:35 +00:00
|
|
|
|
2024-11-27 23:54:47 +00:00
|
|
|
} while (evt_grp_bno08x_task_bits & EVT_GRP_BNO08x_TASKS_RUNNING);
|
2024-11-20 05:32:31 +00:00
|
|
|
|
2024-11-23 01:05:03 +00:00
|
|
|
xSemaphoreGive(sem_kill_tasks); // signal to deconstructor deletion is completed
|
2024-11-27 23:54:47 +00:00
|
|
|
init_status.cb_task = false;
|
2024-11-23 01:05:03 +00:00
|
|
|
vTaskDelete(NULL);
|
2024-11-20 19:12:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-11-23 01:05:03 +00:00
|
|
|
* @brief Locks sh2 HAL lib to only allow the calling task to call its APIs.
|
2024-11-20 19:12:11 +00:00
|
|
|
*
|
|
|
|
|
* @return void, nothing to return
|
|
|
|
|
*/
|
2024-11-23 01:05:03 +00:00
|
|
|
void BNO08x::lock_sh2_HAL()
|
2024-11-20 19:12:11 +00:00
|
|
|
{
|
2024-12-05 06:09:15 +00:00
|
|
|
xSemaphoreTake(sync_ctx.sh2_HAL_lock, portMAX_DELAY);
|
2024-11-21 07:47:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-11-23 01:05:03 +00:00
|
|
|
* @brief Unlocks sh2 HAL lib to allow other tasks to call its APIs.
|
2024-11-21 07:47:29 +00:00
|
|
|
*
|
|
|
|
|
* @return void, nothing to return
|
|
|
|
|
*/
|
2024-11-23 01:05:03 +00:00
|
|
|
void BNO08x::unlock_sh2_HAL()
|
2024-11-21 07:47:29 +00:00
|
|
|
{
|
2024-12-05 06:09:15 +00:00
|
|
|
xSemaphoreGive(sync_ctx.sh2_HAL_lock);
|
2024-11-21 07:47:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-11-23 01:05:03 +00:00
|
|
|
* @brief Locks locks user data to only allow the calling task to read/modify it.
|
2024-11-21 07:47:29 +00:00
|
|
|
*
|
|
|
|
|
* @return void, nothing to return
|
|
|
|
|
*/
|
2024-11-23 01:05:03 +00:00
|
|
|
void BNO08x::lock_user_data()
|
2024-11-21 07:47:29 +00:00
|
|
|
{
|
2024-12-05 06:09:15 +00:00
|
|
|
xSemaphoreTake(sync_ctx.data_lock, portMAX_DELAY);
|
2024-11-21 07:47:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-11-23 01:05:03 +00:00
|
|
|
* @brief Unlocks user data to allow other tasks to read/modify it.
|
2024-11-21 07:47:29 +00:00
|
|
|
*
|
|
|
|
|
* @return void, nothing to return
|
|
|
|
|
*/
|
2024-11-23 01:05:03 +00:00
|
|
|
void BNO08x::unlock_user_data()
|
2024-11-21 07:47:29 +00:00
|
|
|
{
|
2024-12-05 06:09:15 +00:00
|
|
|
xSemaphoreGive(sync_ctx.data_lock);
|
2024-11-21 07:47:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-11-23 01:05:03 +00:00
|
|
|
* @brief Parses receieved report and updates uer data with it.
|
2024-11-21 07:47:29 +00:00
|
|
|
*
|
|
|
|
|
* @return void, nothing to return
|
|
|
|
|
*/
|
2024-11-23 01:05:03 +00:00
|
|
|
void BNO08x::handle_sensor_report(sh2_SensorValue_t* sensor_val)
|
2024-11-21 07:47:29 +00:00
|
|
|
{
|
2024-11-23 01:05:03 +00:00
|
|
|
uint8_t rpt_ID = sensor_val->sensorId;
|
2024-11-21 07:47:29 +00:00
|
|
|
|
2024-12-26 23:39:01 +00:00
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_DEBUG_STATEMENTS
|
|
|
|
|
ESP_LOGE(TAG, "Report RX'd, ID: %d", sensor_val->sensorId);
|
|
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
2024-12-05 02:12:10 +00:00
|
|
|
// check if report implementation exists within map
|
|
|
|
|
if (rpt_ID == SH2_RESERVED)
|
2024-12-01 04:34:58 +00:00
|
|
|
return;
|
2024-11-26 21:28:27 +00:00
|
|
|
|
2024-12-01 04:52:00 +00:00
|
|
|
auto& rpt = usr_reports.at(rpt_ID);
|
|
|
|
|
|
2024-12-05 02:12:10 +00:00
|
|
|
if (rpt == nullptr)
|
2024-12-01 04:52:00 +00:00
|
|
|
return;
|
2024-12-01 04:34:58 +00:00
|
|
|
|
|
|
|
|
// send report ids to cb_task for callback execution (only if this report is enabled)
|
2024-12-05 06:09:15 +00:00
|
|
|
if (rpt->rpt_bit & xEventGroupGetBits(sync_ctx.evt_grp_rpt_en))
|
2024-12-01 04:34:58 +00:00
|
|
|
{
|
|
|
|
|
// update respective report with new data
|
|
|
|
|
rpt->update_data(sensor_val);
|
2024-11-27 23:54:47 +00:00
|
|
|
|
2024-12-05 06:09:15 +00:00
|
|
|
if (sync_ctx.cb_list.size() != 0)
|
2024-12-01 04:34:58 +00:00
|
|
|
if (xQueueSend(queue_cb_report_id, &rpt_ID, 0) != pdTRUE)
|
|
|
|
|
{
|
|
|
|
|
// clang-format off
|
2025-04-12 22:13:07 +01:00
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
|
|
|
|
ESP_LOGE(TAG, "Callback queue full, callback execution for report missed.");
|
|
|
|
|
#endif
|
2024-12-01 04:34:58 +00:00
|
|
|
// clang-format on
|
|
|
|
|
}
|
2024-11-24 04:08:15 +00:00
|
|
|
}
|
2024-11-20 05:32:31 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-27 23:54:47 +00:00
|
|
|
/**
|
|
|
|
|
* @brief Determines the flavor of a passed callback and executes it appropriately.
|
|
|
|
|
*
|
|
|
|
|
* @return void, nothing to return
|
|
|
|
|
*/
|
2024-12-01 04:34:58 +00:00
|
|
|
void BNO08x::handle_cb(uint8_t rpt_ID, BNO08xCbGeneric* cb_entry)
|
2024-11-27 23:54:47 +00:00
|
|
|
{
|
|
|
|
|
// only execute callback if it is registered to this report or all reports
|
2024-12-01 04:34:58 +00:00
|
|
|
if ((cb_entry->rpt_ID == 0) || (cb_entry->rpt_ID == rpt_ID))
|
2024-11-27 23:54:47 +00:00
|
|
|
{
|
2024-12-01 04:34:58 +00:00
|
|
|
cb_entry->invoke(rpt_ID);
|
2024-11-27 23:54:47 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-17 05:36:11 +00:00
|
|
|
/**
|
2024-12-05 04:07:40 +00:00
|
|
|
* @brief Initializes required esp-idf SPI data structures with values from user passed
|
|
|
|
|
* bno08x_config_t struct.
|
2024-11-17 05:36:11 +00:00
|
|
|
*
|
|
|
|
|
* @return ESP_OK if initialization was success.
|
|
|
|
|
*/
|
2024-11-14 09:36:28 +00:00
|
|
|
esp_err_t BNO08x::init_config_args()
|
2024-03-02 10:55:33 +00:00
|
|
|
{
|
2024-11-14 09:36:28 +00:00
|
|
|
if ((imu_config.io_cs == GPIO_NUM_NC))
|
2024-11-14 06:42:21 +00:00
|
|
|
{
|
2024-11-18 00:37:32 +00:00
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
2024-11-14 09:36:28 +00:00
|
|
|
ESP_LOGE(TAG, "Initialization failed, CS GPIO cannot be unassigned.");
|
2024-11-18 00:37:32 +00:00
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
2024-11-14 09:36:28 +00:00
|
|
|
return ESP_ERR_INVALID_ARG;
|
2024-11-14 06:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-14 09:36:28 +00:00
|
|
|
if ((imu_config.io_miso == GPIO_NUM_NC))
|
2024-11-14 06:42:21 +00:00
|
|
|
{
|
2024-11-18 00:37:32 +00:00
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
2024-11-14 09:36:28 +00:00
|
|
|
ESP_LOGE(TAG, "Initialization failed, MISO GPIO cannot be unassigned.");
|
2024-11-18 00:37:32 +00:00
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
2024-11-14 09:36:28 +00:00
|
|
|
return ESP_ERR_INVALID_ARG;
|
2024-11-14 06:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-14 09:36:28 +00:00
|
|
|
if ((imu_config.io_mosi == GPIO_NUM_NC))
|
2024-11-14 06:42:21 +00:00
|
|
|
{
|
2024-11-18 00:37:32 +00:00
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
2024-11-14 09:36:28 +00:00
|
|
|
ESP_LOGE(TAG, "Initialization failed, MOSI GPIO cannot be unassigned.");
|
2024-11-18 00:37:32 +00:00
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
2024-11-14 09:36:28 +00:00
|
|
|
return ESP_ERR_INVALID_ARG;
|
2024-11-14 06:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-14 09:36:28 +00:00
|
|
|
if ((imu_config.io_sclk == GPIO_NUM_NC))
|
2024-11-14 06:42:21 +00:00
|
|
|
{
|
2024-11-18 00:37:32 +00:00
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
2024-11-14 09:36:28 +00:00
|
|
|
ESP_LOGE(TAG, "Initialization failed, SCLK GPIO cannot be unassigned.");
|
2024-11-18 00:37:32 +00:00
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
2024-11-14 09:36:28 +00:00
|
|
|
return ESP_ERR_INVALID_ARG;
|
2024-11-14 06:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-14 09:36:28 +00:00
|
|
|
if ((imu_config.io_rst == GPIO_NUM_NC))
|
2024-11-14 06:42:21 +00:00
|
|
|
{
|
2024-11-18 00:37:32 +00:00
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
2024-11-14 06:42:21 +00:00
|
|
|
ESP_LOGE(TAG, "RST GPIO cannot be unassigned.");
|
2024-11-18 00:37:32 +00:00
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
2024-11-14 09:36:28 +00:00
|
|
|
return ESP_ERR_INVALID_ARG;
|
2024-11-14 06:42:21 +00:00
|
|
|
}
|
2023-11-07 05:10:02 +00:00
|
|
|
|
2023-11-16 08:43:45 +00:00
|
|
|
// SPI bus config
|
2024-03-02 10:55:33 +00:00
|
|
|
bus_config.mosi_io_num = imu_config.io_mosi; // assign mosi gpio pin
|
|
|
|
|
bus_config.miso_io_num = imu_config.io_miso; // assign miso gpio pin
|
|
|
|
|
bus_config.sclk_io_num = imu_config.io_sclk; // assign sclk gpio pin
|
|
|
|
|
bus_config.quadhd_io_num = -1; // hold signal gpio (not used)
|
|
|
|
|
bus_config.quadwp_io_num = -1; // write protect signal gpio (not used)
|
2023-11-07 05:10:02 +00:00
|
|
|
|
2023-11-16 08:43:45 +00:00
|
|
|
// SPI slave device specific config
|
2024-12-05 04:07:40 +00:00
|
|
|
imu_spi_config.mode = 0x3; // set mode to 3 as per BNO08x datasheet (CPHA second edge, CPOL bus
|
|
|
|
|
// high when idle)
|
2023-11-07 05:10:02 +00:00
|
|
|
|
2024-11-14 06:42:21 +00:00
|
|
|
if (imu_config.sclk_speed > SCLK_MAX_SPEED) // max sclk speed of 3MHz for BNO08x
|
2023-11-07 05:10:02 +00:00
|
|
|
{
|
2024-11-18 00:37:32 +00:00
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
2024-11-14 06:42:21 +00:00
|
|
|
ESP_LOGE(TAG, "Max SPI clock speed exceeded, %ld overwritten with 3MHz", imu_config.sclk_speed);
|
2024-11-18 00:37:32 +00:00
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
2024-11-14 06:42:21 +00:00
|
|
|
imu_config.sclk_speed = SCLK_MAX_SPEED;
|
2023-11-16 08:43:45 +00:00
|
|
|
}
|
|
|
|
|
|
2023-11-07 05:10:02 +00:00
|
|
|
imu_spi_config.clock_source = SPI_CLK_SRC_DEFAULT;
|
2024-03-02 10:55:33 +00:00
|
|
|
imu_spi_config.clock_speed_hz = imu_config.sclk_speed; // assign SCLK speed
|
|
|
|
|
imu_spi_config.address_bits = 0; // 0 address bits, not using this system
|
|
|
|
|
imu_spi_config.command_bits = 0; // 0 command bits, not using this system
|
2024-12-05 06:09:15 +00:00
|
|
|
imu_spi_config.spics_io_num = -1; // due to esp32 silicon issue, chip select cannot be used with full-duplex mode
|
|
|
|
|
// driver, it must be handled via calls to gpio pins
|
|
|
|
|
imu_spi_config.queue_size = static_cast<int>(CONFIG_ESP32_BNO08X_SPI_QUEUE_SZ); // set max allowable queued SPI transactions
|
2024-11-14 06:42:21 +00:00
|
|
|
|
2024-11-14 09:36:28 +00:00
|
|
|
return ESP_OK;
|
2024-11-14 06:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-17 05:36:11 +00:00
|
|
|
/**
|
|
|
|
|
* @brief Initializes required gpio inputs.
|
|
|
|
|
*
|
|
|
|
|
* @return ESP_OK if initialization was success.
|
|
|
|
|
*/
|
2024-11-14 09:36:28 +00:00
|
|
|
esp_err_t BNO08x::init_gpio_inputs()
|
2024-11-14 06:42:21 +00:00
|
|
|
{
|
|
|
|
|
esp_err_t ret = ESP_OK;
|
|
|
|
|
|
2024-11-14 09:36:28 +00:00
|
|
|
// configure input(s) (HINT)
|
|
|
|
|
gpio_config_t inputs_config;
|
|
|
|
|
inputs_config.pin_bit_mask = (1ULL << imu_config.io_int);
|
|
|
|
|
inputs_config.mode = GPIO_MODE_INPUT;
|
|
|
|
|
inputs_config.pull_up_en = GPIO_PULLUP_DISABLE;
|
|
|
|
|
inputs_config.pull_down_en = GPIO_PULLDOWN_DISABLE;
|
|
|
|
|
inputs_config.intr_type = GPIO_INTR_NEGEDGE;
|
|
|
|
|
|
|
|
|
|
ret = gpio_config(&inputs_config);
|
|
|
|
|
|
|
|
|
|
if (ret != ESP_OK)
|
2024-11-18 00:37:32 +00:00
|
|
|
{
|
|
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
2024-11-14 09:36:28 +00:00
|
|
|
ESP_LOGE(TAG, "Initialization failed, failed to configure HINT gpio.");
|
2024-11-18 00:37:32 +00:00
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
}
|
2024-11-14 09:36:28 +00:00
|
|
|
else
|
2024-11-18 00:37:32 +00:00
|
|
|
{
|
2024-12-05 04:07:40 +00:00
|
|
|
init_status.gpio_inputs = true; // set gpio_inputs to initialized such that deconstructor
|
|
|
|
|
// knows to clean them up
|
2024-11-18 00:37:32 +00:00
|
|
|
}
|
2024-11-14 09:36:28 +00:00
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-17 05:36:11 +00:00
|
|
|
/**
|
|
|
|
|
* @brief Initializes required gpio outputs.
|
|
|
|
|
*
|
|
|
|
|
* @return ESP_OK if initialization was success.
|
|
|
|
|
*/
|
2024-11-14 09:36:28 +00:00
|
|
|
esp_err_t BNO08x::init_gpio_outputs()
|
|
|
|
|
{
|
|
|
|
|
esp_err_t ret = ESP_OK;
|
2023-11-16 08:43:45 +00:00
|
|
|
|
2024-12-01 04:34:58 +00:00
|
|
|
// configure output(s) (CS, RST)
|
2023-11-07 05:10:02 +00:00
|
|
|
gpio_config_t outputs_config;
|
2024-02-29 03:54:57 +00:00
|
|
|
|
2024-12-01 04:34:58 +00:00
|
|
|
outputs_config.pin_bit_mask = ((1ULL << imu_config.io_cs) | (1ULL << imu_config.io_rst));
|
2024-02-29 03:54:57 +00:00
|
|
|
|
2023-11-16 08:43:45 +00:00
|
|
|
outputs_config.mode = GPIO_MODE_OUTPUT;
|
2023-11-07 05:10:02 +00:00
|
|
|
outputs_config.pull_down_en = GPIO_PULLDOWN_DISABLE;
|
|
|
|
|
outputs_config.pull_up_en = GPIO_PULLUP_DISABLE;
|
|
|
|
|
outputs_config.intr_type = GPIO_INTR_DISABLE;
|
|
|
|
|
|
2024-11-14 06:42:21 +00:00
|
|
|
ret = gpio_config(&outputs_config);
|
|
|
|
|
if (ret != ESP_OK)
|
2024-11-18 00:37:32 +00:00
|
|
|
{
|
|
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
2024-12-01 04:34:58 +00:00
|
|
|
ESP_LOGE(TAG, "Initialization failed, failed to configure CS, and RST gpio.");
|
2024-11-18 00:37:32 +00:00
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
}
|
2024-11-14 09:36:28 +00:00
|
|
|
else
|
2024-11-18 00:37:32 +00:00
|
|
|
{
|
2024-12-05 04:07:40 +00:00
|
|
|
init_status.gpio_outputs = true; // set gpio_inputs to initialized such that deconstructor
|
|
|
|
|
// knows to clean them up
|
2024-11-18 00:37:32 +00:00
|
|
|
}
|
2024-02-29 03:54:57 +00:00
|
|
|
|
2024-11-14 09:36:28 +00:00
|
|
|
return ret;
|
|
|
|
|
}
|
2023-11-07 05:10:02 +00:00
|
|
|
|
2024-11-17 05:36:11 +00:00
|
|
|
/**
|
|
|
|
|
* @brief Initializes required gpio.
|
|
|
|
|
*
|
|
|
|
|
* @return ESP_OK if initialization was success.
|
|
|
|
|
*/
|
2024-11-14 09:36:28 +00:00
|
|
|
esp_err_t BNO08x::init_gpio()
|
|
|
|
|
{
|
|
|
|
|
esp_err_t ret = ESP_OK;
|
2024-11-14 06:42:21 +00:00
|
|
|
|
2024-11-14 09:36:28 +00:00
|
|
|
/*GPIO config for pins not controlled by SPI peripheral*/
|
|
|
|
|
|
|
|
|
|
ret = init_gpio_outputs();
|
|
|
|
|
if (ret != ESP_OK)
|
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
|
|
ret = init_gpio_inputs();
|
2024-11-14 06:42:21 +00:00
|
|
|
if (ret != ESP_OK)
|
|
|
|
|
return ret;
|
2023-11-07 05:10:02 +00:00
|
|
|
|
2024-11-14 06:42:21 +00:00
|
|
|
gpio_set_level(imu_config.io_cs, 1);
|
|
|
|
|
gpio_set_level(imu_config.io_rst, 1);
|
2023-11-07 05:10:02 +00:00
|
|
|
|
2024-11-14 06:42:21 +00:00
|
|
|
return ret;
|
2023-11-07 05:10:02 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-17 05:36:11 +00:00
|
|
|
/**
|
|
|
|
|
* @brief Initializes host interrupt ISR.
|
|
|
|
|
*
|
|
|
|
|
* @return ESP_OK if initialization was success.
|
|
|
|
|
*/
|
2024-11-14 09:36:28 +00:00
|
|
|
esp_err_t BNO08x::init_hint_isr()
|
2024-11-14 06:42:21 +00:00
|
|
|
{
|
|
|
|
|
esp_err_t ret = ESP_OK;
|
2024-11-14 02:21:16 +00:00
|
|
|
|
2024-11-14 06:42:21 +00:00
|
|
|
// check if installation of ISR service has been requested by user (default is true)
|
|
|
|
|
if (imu_config.install_isr_service)
|
|
|
|
|
ret = gpio_install_isr_service(0); // install isr service
|
2024-11-14 02:21:16 +00:00
|
|
|
|
2024-11-14 06:42:21 +00:00
|
|
|
if (ret != ESP_OK)
|
2024-11-14 02:21:16 +00:00
|
|
|
{
|
2024-11-18 00:37:32 +00:00
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
2024-11-14 09:36:28 +00:00
|
|
|
ESP_LOGE(TAG, "Initialization failed, failed to install global ISR service.");
|
2024-11-18 00:37:32 +00:00
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
2024-11-14 06:42:21 +00:00
|
|
|
return ret;
|
2024-11-14 02:21:16 +00:00
|
|
|
}
|
2024-11-14 09:36:28 +00:00
|
|
|
else
|
|
|
|
|
{
|
2024-12-05 06:09:15 +00:00
|
|
|
init_status.isr_service = true; // set isr service to initialized such that deconstructor knows to clean it up
|
|
|
|
|
// (this will be ignored if imu_config.install_isr_service == false)
|
2024-11-14 09:36:28 +00:00
|
|
|
}
|
2024-11-14 02:21:16 +00:00
|
|
|
|
2024-11-14 06:42:21 +00:00
|
|
|
ret = gpio_isr_handler_add(imu_config.io_int, hint_handler, (void*) this);
|
|
|
|
|
if (ret != ESP_OK)
|
|
|
|
|
{
|
2024-11-18 00:37:32 +00:00
|
|
|
|
|
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
2024-11-14 09:36:28 +00:00
|
|
|
ESP_LOGE(TAG, "Initialization failed, failed to add hint_handler ISR.");
|
2024-11-18 00:37:32 +00:00
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
2024-11-14 06:42:21 +00:00
|
|
|
return ret;
|
|
|
|
|
}
|
2024-11-14 09:36:28 +00:00
|
|
|
else
|
|
|
|
|
{
|
2024-12-05 06:09:15 +00:00
|
|
|
init_status.isr_handler = true; // set isr handler to initialized such that deconstructor knows to clean it up
|
2024-11-14 09:36:28 +00:00
|
|
|
}
|
2024-11-14 02:21:16 +00:00
|
|
|
|
2024-11-14 06:42:21 +00:00
|
|
|
return ret;
|
|
|
|
|
}
|
2024-11-14 02:21:16 +00:00
|
|
|
|
2024-11-20 05:32:31 +00:00
|
|
|
/**
|
|
|
|
|
* @brief Initializes data_proc_task.
|
|
|
|
|
*
|
|
|
|
|
* @return ESP_OK if initialization was success.
|
|
|
|
|
*/
|
|
|
|
|
esp_err_t BNO08x::init_tasks()
|
|
|
|
|
{
|
|
|
|
|
BaseType_t task_created = pdFALSE;
|
|
|
|
|
|
2024-12-05 06:09:15 +00:00
|
|
|
xEventGroupSetBits(sync_ctx.evt_grp_task, EVT_GRP_BNO08x_TASKS_RUNNING);
|
2024-11-20 05:32:31 +00:00
|
|
|
|
2025-04-20 10:06:24 +01:00
|
|
|
// launch data processing task 6
|
|
|
|
|
task_created = xTaskCreatePinnedToCore(
|
|
|
|
|
&data_proc_task_trampoline, "bno08x_data_processing_task",
|
|
|
|
|
DATA_PROC_TASK_SZ,
|
|
|
|
|
this,
|
|
|
|
|
DATA_PROC_TASK_PRIORITY,
|
|
|
|
|
&data_proc_task_hdl,
|
|
|
|
|
DATA_PROC_TASK_AFFINITY);
|
2024-11-20 05:32:31 +00:00
|
|
|
|
|
|
|
|
if (task_created != pdTRUE)
|
|
|
|
|
{
|
|
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
|
|
|
|
ESP_LOGE(TAG, "Initialization failed, data_proc_task failed to launch.");
|
|
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
|
|
return ESP_FAIL;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
init_status.data_proc_task = true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-20 10:06:24 +01:00
|
|
|
// launch cb task 5
|
|
|
|
|
task_created = xTaskCreatePinnedToCore(&cb_task_trampoline, "bno08x_cb_task",
|
|
|
|
|
CB_TASK_SZ,
|
|
|
|
|
this,
|
|
|
|
|
CB_TASK_PRIORITY,
|
|
|
|
|
&cb_task_hdl,
|
|
|
|
|
CB_TASK_AFFINITY);
|
|
|
|
|
|
2024-11-20 05:32:31 +00:00
|
|
|
if (task_created != pdTRUE)
|
|
|
|
|
{
|
|
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
2024-11-27 23:54:47 +00:00
|
|
|
ESP_LOGE(TAG, "Initialization failed, cb_task failed to launch.");
|
2024-11-20 05:32:31 +00:00
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
|
|
return ESP_FAIL;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-11-27 23:54:47 +00:00
|
|
|
init_status.cb_task = true;
|
2024-11-20 05:32:31 +00:00
|
|
|
}
|
|
|
|
|
|
2025-04-20 10:06:24 +01:00
|
|
|
// launch sh2 hal service task 7
|
|
|
|
|
task_created = xTaskCreatePinnedToCore(&sh2_HAL_service_task_trampoline, "bno08x_sh2_HAL_service_task",
|
|
|
|
|
SH2_HAL_SERVICE_TASK_SZ,
|
|
|
|
|
this,
|
|
|
|
|
SH2_HAL_SERVICE_TASK_PRIORITY,
|
|
|
|
|
&sh2_HAL_service_task_hdl,
|
|
|
|
|
SH2_HAL_SERVICE_TASK_AFFINITY);
|
2024-11-23 01:05:03 +00:00
|
|
|
|
|
|
|
|
if (task_created != pdTRUE)
|
|
|
|
|
{
|
|
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
2024-11-27 23:54:47 +00:00
|
|
|
ESP_LOGE(TAG, "Initialization failed, sh2_HAL_service_task failed to launch.");
|
2024-11-23 01:05:03 +00:00
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
|
|
return ESP_FAIL;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-11-27 23:54:47 +00:00
|
|
|
init_status.sh2_HAL_service_task = true;
|
2024-11-23 01:05:03 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-20 05:32:31 +00:00
|
|
|
return ESP_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-17 05:36:11 +00:00
|
|
|
/**
|
|
|
|
|
* @brief Initializes SPI.
|
|
|
|
|
*
|
|
|
|
|
* @return ESP_OK if initialization was success.
|
|
|
|
|
*/
|
2024-11-14 09:36:28 +00:00
|
|
|
esp_err_t BNO08x::init_spi()
|
2024-03-02 10:55:33 +00:00
|
|
|
{
|
2024-11-14 06:42:21 +00:00
|
|
|
esp_err_t ret = ESP_OK;
|
2024-07-22 21:14:03 +01:00
|
|
|
|
2024-11-14 06:42:21 +00:00
|
|
|
// initialize the spi peripheral
|
|
|
|
|
ret = spi_bus_initialize(imu_config.spi_peripheral, &bus_config, SPI_DMA_CH_AUTO);
|
|
|
|
|
if (ret != ESP_OK)
|
|
|
|
|
{
|
2024-11-18 00:37:32 +00:00
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
2024-11-14 09:36:28 +00:00
|
|
|
ESP_LOGE(TAG, "Initialization failed, SPI bus failed to initialize.");
|
2024-11-18 00:37:32 +00:00
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
2024-11-14 06:42:21 +00:00
|
|
|
return ret;
|
|
|
|
|
}
|
2024-11-14 09:36:28 +00:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
init_status.spi_bus = true;
|
|
|
|
|
}
|
2023-11-16 08:43:45 +00:00
|
|
|
|
2024-11-14 06:42:21 +00:00
|
|
|
// add the imu device to the bus
|
|
|
|
|
ret = spi_bus_add_device(imu_config.spi_peripheral, &imu_spi_config, &spi_hdl);
|
|
|
|
|
if (ret != ESP_OK)
|
2024-03-02 10:55:33 +00:00
|
|
|
{
|
2024-11-18 00:37:32 +00:00
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
2024-11-14 09:36:28 +00:00
|
|
|
ESP_LOGE(TAG, "Initialization failed, failed to add device to SPI bus.");
|
2024-11-18 00:37:32 +00:00
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
2024-11-14 06:42:21 +00:00
|
|
|
return ret;
|
2023-11-07 05:10:02 +00:00
|
|
|
}
|
2024-11-14 09:36:28 +00:00
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
init_status.spi_device = true;
|
|
|
|
|
}
|
2024-11-20 01:02:16 +00:00
|
|
|
return ret;
|
|
|
|
|
}
|
2023-11-07 05:10:02 +00:00
|
|
|
|
2024-11-20 01:02:16 +00:00
|
|
|
/**
|
|
|
|
|
* @brief Initializes sh2 HAL.
|
|
|
|
|
*
|
|
|
|
|
* @return ESP_OK if initialization was success.
|
|
|
|
|
*/
|
|
|
|
|
esp_err_t BNO08x::init_sh2_HAL()
|
|
|
|
|
{
|
|
|
|
|
// use this IMU in sh2 HAL callbacks
|
|
|
|
|
BNO08xSH2HAL::set_hal_imu(this);
|
2024-11-14 06:42:21 +00:00
|
|
|
|
2024-11-20 01:02:16 +00:00
|
|
|
// register sh2 HAL callbacks
|
|
|
|
|
sh2_HAL.open = BNO08xSH2HAL::spi_open;
|
|
|
|
|
sh2_HAL.close = BNO08xSH2HAL::spi_close;
|
|
|
|
|
sh2_HAL.read = BNO08xSH2HAL::spi_read;
|
|
|
|
|
sh2_HAL.write = BNO08xSH2HAL::spi_write;
|
|
|
|
|
sh2_HAL.getTimeUs = BNO08xSH2HAL::get_time_us;
|
|
|
|
|
|
|
|
|
|
// reset BNO08x
|
2024-11-26 08:47:01 +00:00
|
|
|
toggle_reset();
|
2024-11-20 01:02:16 +00:00
|
|
|
|
|
|
|
|
if (sh2_open(&sh2_HAL, BNO08xSH2HAL::hal_cb, NULL) != SH2_OK)
|
2024-11-20 05:32:31 +00:00
|
|
|
{
|
|
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
|
|
|
|
ESP_LOGE(TAG, "Initialization failed, sh2_open() call failed.");
|
|
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
2024-11-20 01:02:16 +00:00
|
|
|
return ESP_FAIL;
|
2024-11-20 05:32:31 +00:00
|
|
|
}
|
2024-11-20 01:02:16 +00:00
|
|
|
|
|
|
|
|
init_status.sh2_HAL = true;
|
|
|
|
|
|
2024-11-26 08:47:01 +00:00
|
|
|
memset(&product_IDs, 0, sizeof(sh2_ProductIds_t));
|
2024-11-20 05:32:31 +00:00
|
|
|
|
|
|
|
|
if (sh2_getProdIds(&product_IDs) != SH2_OK)
|
|
|
|
|
{
|
|
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
|
|
|
|
ESP_LOGE(TAG, "Initialization failed, sh2_getProdIds() call failed.");
|
|
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
2024-11-20 01:02:16 +00:00
|
|
|
|
|
|
|
|
return ESP_FAIL;
|
2024-11-20 05:32:31 +00:00
|
|
|
}
|
2024-11-20 01:02:16 +00:00
|
|
|
|
|
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
|
|
|
|
print_product_ids();
|
|
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
2024-11-20 05:32:31 +00:00
|
|
|
if (sh2_setSensorCallback(BNO08xSH2HAL::sensor_event_cb, NULL) != SH2_OK)
|
|
|
|
|
return ESP_FAIL;
|
2024-11-20 01:02:16 +00:00
|
|
|
|
|
|
|
|
return ESP_OK;
|
2024-07-23 01:26:07 +01:00
|
|
|
}
|
2024-07-22 21:14:03 +01:00
|
|
|
|
2024-11-17 05:36:11 +00:00
|
|
|
/**
|
|
|
|
|
* @brief Deinitializes GPIO, called from deconstructor.
|
|
|
|
|
*
|
|
|
|
|
* @return ESP_OK if deinitialization was success.
|
|
|
|
|
*/
|
2024-11-14 09:36:28 +00:00
|
|
|
esp_err_t BNO08x::deinit_gpio()
|
|
|
|
|
{
|
|
|
|
|
esp_err_t ret = ESP_OK;
|
|
|
|
|
|
|
|
|
|
if (init_status.gpio_inputs)
|
|
|
|
|
{
|
|
|
|
|
ret = deinit_gpio_inputs();
|
|
|
|
|
if (ret != ESP_OK)
|
|
|
|
|
return ret;
|
2024-11-27 23:54:47 +00:00
|
|
|
|
|
|
|
|
init_status.gpio_inputs = false;
|
2024-11-14 09:36:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (init_status.gpio_outputs)
|
|
|
|
|
{
|
|
|
|
|
ret = deinit_gpio_outputs();
|
|
|
|
|
if (ret != ESP_OK)
|
|
|
|
|
return ret;
|
2024-11-27 23:54:47 +00:00
|
|
|
|
|
|
|
|
init_status.gpio_outputs = false;
|
2024-11-14 09:36:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-17 05:36:11 +00:00
|
|
|
/**
|
|
|
|
|
* @brief Deinitializes GPIO inputs, called from deconstructor.
|
|
|
|
|
*
|
|
|
|
|
* @return ESP_OK if deinitialization was success.
|
|
|
|
|
*/
|
2024-11-14 09:36:28 +00:00
|
|
|
esp_err_t BNO08x::deinit_gpio_inputs()
|
|
|
|
|
{
|
|
|
|
|
esp_err_t ret = ESP_OK;
|
|
|
|
|
|
|
|
|
|
ret = gpio_reset_pin(imu_config.io_int);
|
|
|
|
|
if (ret != ESP_OK)
|
2024-11-18 00:37:32 +00:00
|
|
|
{
|
|
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
|
|
|
|
ESP_LOGE(TAG, "Initialization failed, failed to add device to SPI bus.");
|
|
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
}
|
2024-11-14 09:36:28 +00:00
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-17 05:36:11 +00:00
|
|
|
/**
|
|
|
|
|
* @brief Deinitializes GPIO outputs, called from deconstructor.
|
|
|
|
|
*
|
|
|
|
|
* @return ESP_OK if deinitialization was success.
|
|
|
|
|
*/
|
2024-11-14 09:36:28 +00:00
|
|
|
esp_err_t BNO08x::deinit_gpio_outputs()
|
|
|
|
|
{
|
|
|
|
|
esp_err_t ret = ESP_OK;
|
|
|
|
|
|
|
|
|
|
ret = gpio_reset_pin(imu_config.io_cs);
|
|
|
|
|
if (ret != ESP_OK)
|
|
|
|
|
{
|
2024-11-18 00:37:32 +00:00
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
2024-11-14 09:36:28 +00:00
|
|
|
ESP_LOGE(TAG, "Deconstruction failed, could reset gpio CS pin to default state.");
|
2024-11-18 00:37:32 +00:00
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
2024-11-14 09:36:28 +00:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ret = gpio_reset_pin(imu_config.io_rst);
|
|
|
|
|
if (ret != ESP_OK)
|
|
|
|
|
{
|
2024-11-18 00:37:32 +00:00
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
2024-11-14 09:36:28 +00:00
|
|
|
ESP_LOGE(TAG, "Deconstruction failed, could reset gpio RST pin to default state.");
|
2024-11-18 00:37:32 +00:00
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
2024-11-14 09:36:28 +00:00
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-17 05:36:11 +00:00
|
|
|
/**
|
|
|
|
|
* @brief Deinitializes host interrupt ISR, called from deconstructor.
|
|
|
|
|
*
|
|
|
|
|
* @return ESP_OK if deinitialization was success.
|
|
|
|
|
*/
|
2024-11-14 09:36:28 +00:00
|
|
|
esp_err_t BNO08x::deinit_hint_isr()
|
|
|
|
|
{
|
|
|
|
|
esp_err_t ret = ESP_OK;
|
|
|
|
|
|
|
|
|
|
if (init_status.isr_handler)
|
|
|
|
|
{
|
|
|
|
|
ret = gpio_isr_handler_remove(imu_config.io_int);
|
|
|
|
|
if (ret != ESP_OK)
|
|
|
|
|
{
|
2024-11-18 00:37:32 +00:00
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
2024-11-14 09:36:28 +00:00
|
|
|
ESP_LOGE(TAG, "Deconstruction failed, could not remove hint ISR handler.");
|
2024-11-18 00:37:32 +00:00
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
2024-11-14 09:36:28 +00:00
|
|
|
return ret;
|
|
|
|
|
}
|
2024-11-27 23:54:47 +00:00
|
|
|
|
|
|
|
|
init_status.isr_handler = false;
|
2024-11-14 09:36:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (init_status.isr_service)
|
|
|
|
|
{
|
|
|
|
|
// only remove the ISR service if it was requested to be installed by user
|
|
|
|
|
if (imu_config.install_isr_service)
|
|
|
|
|
{
|
|
|
|
|
gpio_uninstall_isr_service();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-17 05:36:11 +00:00
|
|
|
/**
|
|
|
|
|
* @brief Deinitializes SPI.
|
|
|
|
|
*
|
|
|
|
|
* @return ESP_OK if deinitialization was success.
|
|
|
|
|
*/
|
2024-11-14 09:36:28 +00:00
|
|
|
esp_err_t BNO08x::deinit_spi()
|
|
|
|
|
{
|
|
|
|
|
esp_err_t ret = ESP_OK;
|
|
|
|
|
|
|
|
|
|
if (init_status.spi_device)
|
|
|
|
|
{
|
|
|
|
|
ret = spi_bus_remove_device(spi_hdl);
|
|
|
|
|
if (ret != ESP_OK)
|
|
|
|
|
{
|
2024-11-18 00:37:32 +00:00
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
2024-11-14 09:36:28 +00:00
|
|
|
ESP_LOGE(TAG, "Deconstruction failed, could not remove spi device.");
|
2024-11-18 00:37:32 +00:00
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
2024-11-14 09:36:28 +00:00
|
|
|
return ret;
|
|
|
|
|
}
|
2024-11-27 23:54:47 +00:00
|
|
|
|
|
|
|
|
init_status.spi_device = false;
|
2024-11-14 09:36:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (init_status.spi_bus)
|
|
|
|
|
{
|
|
|
|
|
ret = spi_bus_free(imu_config.spi_peripheral);
|
|
|
|
|
if (ret != ESP_OK)
|
|
|
|
|
{
|
2024-11-18 00:37:32 +00:00
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
2024-11-14 09:36:28 +00:00
|
|
|
ESP_LOGE(TAG, "Deconstruction failed, could free SPI peripheral.");
|
2024-11-18 00:37:32 +00:00
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
2024-11-14 09:36:28 +00:00
|
|
|
return ret;
|
|
|
|
|
}
|
2024-11-27 23:54:47 +00:00
|
|
|
|
|
|
|
|
init_status.spi_bus = false;
|
2024-11-14 09:36:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-20 05:32:31 +00:00
|
|
|
/**
|
|
|
|
|
* @brief Deinitializes tasks used by BNO08x driver.
|
|
|
|
|
*
|
|
|
|
|
* @return ESP_OK if deinitialization was success.
|
|
|
|
|
*/
|
|
|
|
|
esp_err_t BNO08x::deinit_tasks()
|
|
|
|
|
{
|
2024-11-27 23:54:47 +00:00
|
|
|
static const constexpr uint8_t TASK_DELETE_TIMEOUT_MS = HOST_INT_TIMEOUT_DEFAULT_MS;
|
2024-11-20 19:12:11 +00:00
|
|
|
uint8_t kill_count = 0;
|
2024-11-27 23:54:47 +00:00
|
|
|
uint8_t init_count = 0;
|
2024-11-20 19:12:11 +00:00
|
|
|
sh2_SensorEvent_t empty_event;
|
2024-11-23 01:05:03 +00:00
|
|
|
uint8_t empty_ID = 0;
|
2024-11-20 19:12:11 +00:00
|
|
|
|
2024-11-27 23:54:47 +00:00
|
|
|
// disable interrupts before beginning so we can ensure SPI transaction doesn't attempt to run
|
|
|
|
|
gpio_intr_disable(imu_config.io_int);
|
|
|
|
|
|
2024-12-05 06:09:15 +00:00
|
|
|
init_count += (static_cast<uint8_t>(init_status.cb_task) + static_cast<uint8_t>(init_status.data_proc_task) +
|
2024-11-27 23:54:47 +00:00
|
|
|
static_cast<uint8_t>(init_status.sh2_HAL_service_task));
|
2024-11-20 19:12:11 +00:00
|
|
|
|
2024-11-27 23:54:47 +00:00
|
|
|
if (init_count != 0)
|
|
|
|
|
{
|
|
|
|
|
sem_kill_tasks = xSemaphoreCreateCounting(init_count, 0);
|
2024-12-05 06:09:15 +00:00
|
|
|
xEventGroupClearBits(sync_ctx.evt_grp_task,
|
2024-12-05 04:07:40 +00:00
|
|
|
EVT_GRP_BNO08x_TASKS_RUNNING); // clear task running bit request deletion of tasks
|
2024-11-20 19:12:11 +00:00
|
|
|
|
2024-11-27 23:54:47 +00:00
|
|
|
if (init_status.cb_task)
|
|
|
|
|
xQueueSend(queue_cb_report_id, &empty_ID, 0);
|
2024-11-20 05:32:31 +00:00
|
|
|
|
2024-11-27 23:54:47 +00:00
|
|
|
if (init_status.data_proc_task)
|
|
|
|
|
xQueueSend(queue_rx_sensor_event, &empty_event, 0);
|
2024-11-20 19:12:11 +00:00
|
|
|
|
2024-11-27 23:54:47 +00:00
|
|
|
if (init_status.sh2_HAL_service_task)
|
2024-12-05 06:09:15 +00:00
|
|
|
xEventGroupSetBits(sync_ctx.evt_grp_task, EVT_GRP_BNO08x_TASK_HINT_ASSRT_BIT);
|
2024-11-23 01:05:03 +00:00
|
|
|
|
2024-11-27 23:54:47 +00:00
|
|
|
for (uint8_t i = 0; i < init_count; i++)
|
|
|
|
|
if (xSemaphoreTake(sem_kill_tasks, TASK_DELETE_TIMEOUT_MS) == pdTRUE)
|
|
|
|
|
kill_count++;
|
2024-11-20 19:12:11 +00:00
|
|
|
|
2024-11-27 23:54:47 +00:00
|
|
|
if (kill_count != init_count)
|
|
|
|
|
{
|
|
|
|
|
// clang-format off
|
2024-12-26 23:39:01 +00:00
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
2024-11-27 23:54:47 +00:00
|
|
|
ESP_LOGE(TAG, "Task deletion timed out in deconstructor call.");
|
|
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
2024-11-20 19:12:11 +00:00
|
|
|
|
2024-11-27 23:54:47 +00:00
|
|
|
return ESP_FAIL;
|
|
|
|
|
}
|
2024-11-20 19:12:11 +00:00
|
|
|
}
|
2024-11-20 05:32:31 +00:00
|
|
|
|
|
|
|
|
return ESP_OK;
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-07 05:10:02 +00:00
|
|
|
/**
|
2024-11-20 01:02:16 +00:00
|
|
|
* @brief Deinitializes sh2 HAL.
|
2023-11-16 08:43:45 +00:00
|
|
|
*
|
2024-11-20 01:02:16 +00:00
|
|
|
* @return ESP_OK if deinitialization was success.
|
2023-11-07 05:10:02 +00:00
|
|
|
*/
|
2024-11-20 01:02:16 +00:00
|
|
|
esp_err_t BNO08x::deinit_sh2_HAL()
|
2024-03-02 10:55:33 +00:00
|
|
|
{
|
2024-11-20 01:02:16 +00:00
|
|
|
if (init_status.sh2_HAL)
|
2024-11-27 23:54:47 +00:00
|
|
|
{
|
|
|
|
|
init_status.sh2_HAL = false;
|
2024-11-20 01:02:16 +00:00
|
|
|
sh2_close();
|
2024-11-27 23:54:47 +00:00
|
|
|
}
|
2023-11-07 05:10:02 +00:00
|
|
|
|
2024-11-20 01:02:16 +00:00
|
|
|
return ESP_OK;
|
2023-11-07 05:10:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-11-26 08:47:01 +00:00
|
|
|
* @brief Hard resets BNO08x device.
|
2023-11-16 08:43:45 +00:00
|
|
|
*
|
2024-11-26 08:47:01 +00:00
|
|
|
* @return True if reset was success.
|
2023-11-07 05:10:02 +00:00
|
|
|
*/
|
2024-11-26 08:47:01 +00:00
|
|
|
bool BNO08x::hard_reset()
|
2024-11-16 01:50:42 +00:00
|
|
|
{
|
2024-11-26 21:28:27 +00:00
|
|
|
// toggle reset gpio
|
2024-11-26 08:47:01 +00:00
|
|
|
toggle_reset();
|
2023-11-07 05:10:02 +00:00
|
|
|
|
2024-11-26 21:28:27 +00:00
|
|
|
// wait for reset to be detected by SH2 HAL lib
|
2024-11-26 08:47:01 +00:00
|
|
|
if (wait_for_reset() == ESP_OK)
|
|
|
|
|
{
|
2024-11-26 21:28:27 +00:00
|
|
|
// run service to dispatch callbacks
|
2024-11-26 08:47:01 +00:00
|
|
|
lock_sh2_HAL();
|
|
|
|
|
sh2_service();
|
|
|
|
|
unlock_sh2_HAL();
|
2024-11-18 19:35:02 +00:00
|
|
|
|
2024-11-26 08:47:01 +00:00
|
|
|
// get product ids and check reset reason
|
2024-11-26 21:28:27 +00:00
|
|
|
if (get_reset_reason() == BNO08xResetReason::EXT_RST)
|
2024-11-26 08:47:01 +00:00
|
|
|
{
|
2024-11-26 21:28:27 +00:00
|
|
|
return true;
|
2024-11-26 08:47:01 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// clang-format off
|
2024-11-26 21:28:27 +00:00
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
|
|
|
|
ESP_LOGE(TAG, "Hard reset failure, incorrect reset reason returned: %d.", product_IDs.entry[0].resetCause);
|
|
|
|
|
#endif
|
2024-11-26 08:47:01 +00:00
|
|
|
// clang-format on
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// clang-format off
|
2025-04-12 22:13:07 +01:00
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
|
|
|
|
ESP_LOGE(TAG, "Hard reset failure, reset never detected after toggling reset pin");
|
|
|
|
|
#endif
|
2024-11-26 08:47:01 +00:00
|
|
|
// clang-format on
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Soft resets BNO08x device by sending RESET (1) command on "device" channel.
|
|
|
|
|
*
|
|
|
|
|
* @return True if soft reset operation succeeded.
|
|
|
|
|
*/
|
|
|
|
|
bool BNO08x::soft_reset()
|
|
|
|
|
{
|
|
|
|
|
int op_success = SH2_ERR;
|
|
|
|
|
|
2024-11-26 21:28:27 +00:00
|
|
|
// send reset command
|
2024-11-26 08:47:01 +00:00
|
|
|
lock_sh2_HAL();
|
|
|
|
|
op_success = sh2_devReset();
|
|
|
|
|
unlock_sh2_HAL();
|
|
|
|
|
|
|
|
|
|
if (op_success == SH2_OK)
|
|
|
|
|
{
|
2024-11-26 21:28:27 +00:00
|
|
|
// wait for reset to be detected by SH2 HAL lib
|
2024-11-26 08:47:01 +00:00
|
|
|
if (wait_for_reset() == ESP_OK)
|
|
|
|
|
{
|
2024-11-26 21:28:27 +00:00
|
|
|
// run service to dispatch callbacks
|
2024-11-26 08:47:01 +00:00
|
|
|
lock_sh2_HAL();
|
|
|
|
|
sh2_service();
|
|
|
|
|
unlock_sh2_HAL();
|
|
|
|
|
|
2024-11-26 21:28:27 +00:00
|
|
|
if (get_reset_reason() == BNO08xResetReason::EXT_RST)
|
2024-11-26 08:47:01 +00:00
|
|
|
{
|
2024-11-26 21:28:27 +00:00
|
|
|
return true;
|
2024-11-26 08:47:01 +00:00
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
2024-11-26 21:28:27 +00:00
|
|
|
ESP_LOGE(TAG, "Soft reset failure, incorrect reset reason returned.");
|
2024-11-26 08:47:01 +00:00
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
|
|
|
|
ESP_LOGE(TAG, "Soft reset failure, reset never detected after sending command.");
|
|
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
|
|
|
|
ESP_LOGE(TAG, "Soft reset failure, failed to send reset command");
|
|
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
2024-11-16 01:50:42 +00:00
|
|
|
}
|
2023-11-07 05:10:02 +00:00
|
|
|
|
2024-12-26 23:39:01 +00:00
|
|
|
/**
|
|
|
|
|
* @brief Disables all currently enabled reports.
|
|
|
|
|
*
|
|
|
|
|
* @return True if all currently enabled reports were disabled successfully.
|
|
|
|
|
*/
|
|
|
|
|
bool BNO08x::disable_all_reports()
|
|
|
|
|
{
|
|
|
|
|
int attempts = 0;
|
|
|
|
|
|
|
|
|
|
xEventGroupClearBits(sync_ctx.evt_grp_rpt_en, EVT_GRP_RPT_ALL);
|
|
|
|
|
|
|
|
|
|
while (sync_ctx.en_report_ids.size() != 0 && (attempts < TOTAL_RPT_COUNT))
|
|
|
|
|
{
|
|
|
|
|
uint8_t rpt_ID = sync_ctx.en_report_ids.back();
|
|
|
|
|
BNO08xRpt* rpt = usr_reports.at(rpt_ID);
|
|
|
|
|
if (rpt == nullptr)
|
|
|
|
|
{
|
|
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
|
|
|
|
ESP_LOGE(TAG, "NULL pointer detected in usr_reports map for enabled report.");
|
|
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!rpt->disable())
|
|
|
|
|
{
|
|
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
|
|
|
|
ESP_LOGE(TAG, "Failed to disable: %d", rpt->ID);
|
|
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
attempts++;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-27 00:42:48 +00:00
|
|
|
if (attempts < TOTAL_RPT_COUNT)
|
|
|
|
|
return true;
|
|
|
|
|
else
|
|
|
|
|
return false;
|
2024-12-26 23:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-26 21:28:27 +00:00
|
|
|
/**
|
|
|
|
|
* @brief Returns reason for previous reset via product ID report.
|
|
|
|
|
*
|
|
|
|
|
* @return Enum object containing reset reason, BNO08xResetReason::UNDEFINED if failure.
|
|
|
|
|
*/
|
|
|
|
|
BNO08xResetReason BNO08x::get_reset_reason()
|
|
|
|
|
{
|
|
|
|
|
int op_success = SH2_ERR;
|
|
|
|
|
BNO08xResetReason rr = BNO08xResetReason::UNDEFINED;
|
|
|
|
|
|
|
|
|
|
memset(&product_IDs, 0, sizeof(sh2_ProductIds_t));
|
|
|
|
|
lock_sh2_HAL();
|
|
|
|
|
op_success = sh2_getProdIds(&product_IDs);
|
|
|
|
|
unlock_sh2_HAL();
|
|
|
|
|
|
|
|
|
|
if (op_success == SH2_OK)
|
|
|
|
|
{
|
|
|
|
|
rr = static_cast<BNO08xResetReason>(product_IDs.entry[0].resetCause);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
|
|
|
|
ESP_LOGE(TAG, "Get reset reason failure, failed to get prodIDs.");
|
|
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return rr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Places BNO08x device in on state by sending ON (2) command on "device" channel.
|
|
|
|
|
*
|
|
|
|
|
* @return True if on operation succeeded.
|
|
|
|
|
*/
|
|
|
|
|
bool BNO08x::on()
|
|
|
|
|
{
|
|
|
|
|
int op_success = SH2_ERR;
|
|
|
|
|
|
|
|
|
|
lock_sh2_HAL();
|
|
|
|
|
op_success = sh2_devOn();
|
|
|
|
|
unlock_sh2_HAL();
|
|
|
|
|
|
|
|
|
|
return (op_success == SH2_OK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Places BNO08x device in sleep state by sending SLEEP (3) command on "device" channel.
|
|
|
|
|
*
|
|
|
|
|
* @return True if sleep operation succeeded.
|
|
|
|
|
*/
|
|
|
|
|
bool BNO08x::sleep()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
int op_success = SH2_ERR;
|
|
|
|
|
|
|
|
|
|
lock_sh2_HAL();
|
|
|
|
|
op_success = sh2_devSleep();
|
|
|
|
|
unlock_sh2_HAL();
|
|
|
|
|
|
|
|
|
|
return (op_success == SH2_OK);
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-05 06:09:15 +00:00
|
|
|
/**
|
|
|
|
|
* @brief Starts simple calibration, see ref. manual 6.4.10.1
|
|
|
|
|
*
|
|
|
|
|
* @param period_us This interval should be set to whatever rate the sensor hub is expected to run
|
|
|
|
|
* at after calibration.
|
|
|
|
|
*
|
|
|
|
|
* After the calibration is started, the IMU should be rotated 180 degrees.
|
|
|
|
|
* After the IMU has been rotated call calibration_end().
|
|
|
|
|
* See ref. manual 6.4.10 for more detailed instructions.
|
|
|
|
|
*
|
|
|
|
|
* @return True if start simple calibration operation succeeded.
|
|
|
|
|
*/
|
2025-04-12 22:13:07 +01:00
|
|
|
/*
|
|
|
|
|
bool BNO08x::calibration_turntable_start(uint32_t period_us)
|
2024-12-05 06:09:15 +00:00
|
|
|
{
|
2025-04-12 22:13:07 +01:00
|
|
|
// currently broken, correct packet is sent over SPI but IMU responds
|
|
|
|
|
// with unsolicited initialize response instead of the expected Turntable Cal response (0x0C)
|
2024-12-05 06:09:15 +00:00
|
|
|
int op_success = SH2_ERR;
|
|
|
|
|
|
|
|
|
|
lock_sh2_HAL();
|
|
|
|
|
op_success = sh2_startCal(period_us);
|
|
|
|
|
unlock_sh2_HAL();
|
|
|
|
|
|
|
|
|
|
return (op_success == SH2_OK);
|
|
|
|
|
}
|
2025-04-12 22:13:07 +01:00
|
|
|
*/
|
2024-12-05 06:09:15 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Ends turn-table calibration, see ref. manual 6.4.10.2
|
|
|
|
|
*
|
|
|
|
|
* @param status Returned status bits indicating result of turntable calibration.
|
|
|
|
|
*
|
|
|
|
|
* @return True if enable start turn-table calibration operation succeeded.
|
|
|
|
|
*/
|
2025-04-12 22:13:07 +01:00
|
|
|
/*bool BNO08x::calibration_turntable_end(sh2_CalStatus_t& status)
|
2024-12-05 06:09:15 +00:00
|
|
|
{
|
|
|
|
|
int op_success = SH2_ERR;
|
|
|
|
|
|
|
|
|
|
lock_sh2_HAL();
|
|
|
|
|
op_success = sh2_finishCal(&status);
|
|
|
|
|
unlock_sh2_HAL();
|
|
|
|
|
|
|
|
|
|
return (op_success == SH2_OK);
|
|
|
|
|
}
|
2025-04-12 22:13:07 +01:00
|
|
|
*/
|
2024-12-05 06:09:15 +00:00
|
|
|
|
2024-11-26 22:56:33 +00:00
|
|
|
/**
|
2024-12-05 04:07:40 +00:00
|
|
|
* @brief Enables dynamic/motion engine calibration for specified sensor(s), see ref. manual 6.4.6.1
|
|
|
|
|
*
|
|
|
|
|
* @param sensor The sensor(s) to enable dynamic/ME calibration for.
|
|
|
|
|
*
|
|
|
|
|
* @return True if enable dynamic/ME calibration succeeded.
|
|
|
|
|
*/
|
2024-12-05 06:09:15 +00:00
|
|
|
bool BNO08x::dynamic_calibration_enable(BNO08xCalSel sensor)
|
2024-12-05 04:07:40 +00:00
|
|
|
{
|
|
|
|
|
int op_success = SH2_ERR;
|
|
|
|
|
|
|
|
|
|
lock_sh2_HAL();
|
|
|
|
|
op_success = sh2_setCalConfig(static_cast<uint8_t>(sensor));
|
|
|
|
|
unlock_sh2_HAL();
|
|
|
|
|
|
|
|
|
|
return (op_success == SH2_OK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Disables dynamic/motion engine calibration for specified sensor(s), see ref.
|
|
|
|
|
* manual 6.4.6.1
|
|
|
|
|
*
|
|
|
|
|
* @param sensor The sensor(s) to disable dynamic/ME calibration for.
|
|
|
|
|
*
|
|
|
|
|
* @return True if disable dynamic/ME calibration succeeded.
|
|
|
|
|
*/
|
2024-12-05 06:09:15 +00:00
|
|
|
bool BNO08x::dynamic_calibration_disable(BNO08xCalSel sensor)
|
2024-12-05 04:07:40 +00:00
|
|
|
{
|
|
|
|
|
int op_success = SH2_ERR;
|
|
|
|
|
uint8_t active_sensors = 0U;
|
|
|
|
|
|
|
|
|
|
lock_sh2_HAL();
|
|
|
|
|
op_success = sh2_getCalConfig(&active_sensors);
|
|
|
|
|
unlock_sh2_HAL();
|
|
|
|
|
|
|
|
|
|
if (op_success == SH2_OK)
|
|
|
|
|
{
|
|
|
|
|
active_sensors &= ~static_cast<uint8_t>(sensor);
|
|
|
|
|
|
|
|
|
|
lock_sh2_HAL();
|
|
|
|
|
op_success = sh2_setCalConfig(active_sensors);
|
|
|
|
|
unlock_sh2_HAL();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (op_success == SH2_OK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Enables the automatic saving of dynamic/ME calibration data to BNO08x internal flash See
|
|
|
|
|
* ref manual 6.4.7.1.
|
|
|
|
|
*
|
|
|
|
|
* @return True if dynamic/ME calibration autosave data enable succeeded.
|
|
|
|
|
*/
|
2024-12-05 06:09:15 +00:00
|
|
|
bool BNO08x::dynamic_calibration_autosave_enable()
|
2024-12-05 04:07:40 +00:00
|
|
|
{
|
|
|
|
|
int op_success = SH2_ERR;
|
|
|
|
|
|
|
|
|
|
lock_sh2_HAL();
|
|
|
|
|
op_success = sh2_setDcdAutoSave(true);
|
|
|
|
|
unlock_sh2_HAL();
|
|
|
|
|
|
|
|
|
|
return (op_success == SH2_OK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Disables the automatic saving of dynamic/ME calibration data to BNO08x internal flash See
|
|
|
|
|
* ref manual 6.4.7.1.
|
|
|
|
|
*
|
|
|
|
|
* @return True if dynamic/ME calibration autosave data enable succeeded.
|
|
|
|
|
*/
|
2024-12-05 06:09:15 +00:00
|
|
|
bool BNO08x::dynamic_calibration_autosave_disable()
|
2024-12-05 04:07:40 +00:00
|
|
|
{
|
|
|
|
|
int op_success = SH2_ERR;
|
|
|
|
|
|
|
|
|
|
lock_sh2_HAL();
|
|
|
|
|
op_success = sh2_setDcdAutoSave(false);
|
|
|
|
|
unlock_sh2_HAL();
|
|
|
|
|
|
|
|
|
|
return (op_success == SH2_OK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Saves dynamic/motion engine calibration data to BNO08x internal flash immediately. See ref
|
|
|
|
|
* manual 6.4.5.1
|
|
|
|
|
*
|
|
|
|
|
* @return True if save dynamic/ME calibration data succeeded.
|
|
|
|
|
*/
|
2025-04-12 22:13:07 +01:00
|
|
|
bool BNO08x::dynamic_calibration_save()
|
2024-12-05 04:07:40 +00:00
|
|
|
{
|
|
|
|
|
int op_success = SH2_ERR;
|
|
|
|
|
|
|
|
|
|
lock_sh2_HAL();
|
|
|
|
|
op_success = sh2_saveDcdNow();
|
|
|
|
|
unlock_sh2_HAL();
|
|
|
|
|
|
|
|
|
|
return (op_success == SH2_OK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Clears dynamic/motion engine calibration data and resets BNO08x device. See ref
|
|
|
|
|
* manual 6.4.9.1
|
|
|
|
|
*
|
|
|
|
|
* @return True if save dynamic/ME calibration data succeeded.
|
|
|
|
|
*/
|
2025-04-12 22:13:07 +01:00
|
|
|
bool BNO08x::dynamic_calibration_clear()
|
2024-12-05 04:07:40 +00:00
|
|
|
{
|
|
|
|
|
int op_success = SH2_ERR;
|
|
|
|
|
|
|
|
|
|
// send clear DCD and reset command
|
|
|
|
|
lock_sh2_HAL();
|
|
|
|
|
op_success = sh2_clearDcdAndReset();
|
|
|
|
|
unlock_sh2_HAL();
|
|
|
|
|
|
|
|
|
|
if (op_success == SH2_OK)
|
|
|
|
|
{
|
|
|
|
|
// wait for reset to be detected by SH2 HAL lib
|
|
|
|
|
if (wait_for_reset() == ESP_OK)
|
|
|
|
|
{
|
|
|
|
|
// run service to dispatch callbacks
|
|
|
|
|
lock_sh2_HAL();
|
|
|
|
|
sh2_service();
|
|
|
|
|
unlock_sh2_HAL();
|
|
|
|
|
|
|
|
|
|
if (get_reset_reason() == BNO08xResetReason::EXT_RST)
|
|
|
|
|
{
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
|
|
|
|
ESP_LOGE(TAG, "Clear dynamic calibration failure, incorrect reset reason returned.");
|
|
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
|
|
|
|
ESP_LOGE(TAG, "Clear dynamic calibration failure, reset never detected after sending command.");
|
|
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
|
|
|
|
ESP_LOGE(TAG, "Clear dynamic calibration failure, failed to send reset command");
|
|
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-12 22:13:07 +01:00
|
|
|
/**
|
|
|
|
|
* @brief Example calibration routine using dynamic/ME calibration commands.
|
|
|
|
|
*
|
|
|
|
|
* Routine does the following:alignas
|
|
|
|
|
*
|
|
|
|
|
* 1) disables all enabled reports
|
|
|
|
|
* 2) sends a command to enable dynamic/motion engine calibration for all possible options (SH2_CAL_ACCEL | SH2_CAL_GYRO | SH2_CAL_MAG | SH2_CAL_PLANAR)
|
|
|
|
|
* 3) enables game rotation vector reports and calibrated magnetic field reports
|
|
|
|
|
* 4) moving window average for accuracy received through reports
|
|
|
|
|
* 5) deems calibration accuracy threshold met when magf accuracy avg is >=2 (MED) and quat accuracy avg >=3 (HIGH) for longer than 5 seconds
|
|
|
|
|
* 6) sends command to save dynamic calibration data
|
|
|
|
|
* 7) disables all enabled reports
|
|
|
|
|
*
|
|
|
|
|
* Note the DCD commands don't have to be used this way, this is just an example,
|
|
|
|
|
* but the dynamic_calibration_autosave_enable() allows calibration to be run and
|
|
|
|
|
* saved constantly even while data is used for other operations.
|
|
|
|
|
*
|
|
|
|
|
* @return True if calibration routine succeeded.
|
|
|
|
|
*/
|
|
|
|
|
bool BNO08x::dynamic_calibration_run_routine()
|
|
|
|
|
{
|
|
|
|
|
constexpr size_t WINDOW_SZ = 50;
|
|
|
|
|
constexpr int64_t STABLE_TIME_CRITERIA_US = 5000000LL; // meet accuracy criteria for 5 seconds
|
|
|
|
|
|
|
|
|
|
bno08x_quat_t quat_data;
|
|
|
|
|
bno08x_magf_t magf_data;
|
|
|
|
|
uint8_t quat_accuracy_window[WINDOW_SZ] = {0U};
|
|
|
|
|
uint8_t magf_accuracy_window[WINDOW_SZ] = {0U};
|
|
|
|
|
uint8_t quat_window_idx = 0U;
|
|
|
|
|
uint8_t magf_window_idx = 0U;
|
|
|
|
|
uint16_t quat_window_sum = 0U;
|
|
|
|
|
uint16_t magf_window_sum = 0U;
|
|
|
|
|
BNO08xAccuracy quat_accuracy_avg = BNO08xAccuracy::UNRELIABLE;
|
|
|
|
|
BNO08xAccuracy magf_accuracy_avg = BNO08xAccuracy::UNRELIABLE;
|
|
|
|
|
int64_t stable_time = 0LL;
|
|
|
|
|
int64_t stable_start_time = 0ULL;
|
|
|
|
|
|
|
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
|
|
|
|
ESP_LOGI(TAG, "dynamic_calibration_run_routine(): disabling all currently enabled reports and starting calibration...");
|
|
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
|
|
if (!disable_all_reports())
|
|
|
|
|
{
|
|
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
|
|
|
|
ESP_LOGE(TAG, "dynamic_calibration_run_routine(): failed to disable all reports before calibration");
|
|
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!dynamic_calibration_enable(BNO08xCalSel::all))
|
|
|
|
|
{
|
|
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
|
|
|
|
ESP_LOGE(TAG, "dynamic_calibration_run_routine(): failed to enable dynamic calibration");
|
|
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!rpt.rv_game.enable(100000UL))
|
|
|
|
|
{
|
|
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
|
|
|
|
ESP_LOGE(TAG, "dynamic_calibration_run_routine(): failed to enable game rotation vector report");
|
|
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!rpt.cal_magnetometer.enable(100000UL))
|
|
|
|
|
{
|
|
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
|
|
|
|
ESP_LOGE(TAG, "dynamic_calibration_run_routine(): failed to enable calibrated magnetometer report");
|
|
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
|
{
|
|
|
|
|
if (data_available())
|
|
|
|
|
{
|
|
|
|
|
if (rpt.rv_game.has_new_data())
|
|
|
|
|
{
|
|
|
|
|
quat_data = rpt.rv_game.get_quat();
|
|
|
|
|
|
|
|
|
|
quat_accuracy_window[quat_window_idx] = static_cast<uint8_t>(quat_data.accuracy);
|
|
|
|
|
quat_window_idx++;
|
|
|
|
|
|
|
|
|
|
if (quat_window_idx >= WINDOW_SZ)
|
|
|
|
|
quat_window_idx = 0U;
|
|
|
|
|
|
|
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
|
|
|
|
ESP_LOGI(TAG, "dynamic_calibration_run_routine(): quat_data: accuracy: %d i: %.3f j: %.3f k: %.3f real: %.3f",
|
|
|
|
|
static_cast<uint8_t>(quat_data.accuracy), quat_data.i, quat_data.j, quat_data.k, quat_data.real);
|
|
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (rpt.cal_magnetometer.has_new_data())
|
|
|
|
|
{
|
|
|
|
|
magf_data = rpt.cal_magnetometer.get();
|
|
|
|
|
|
|
|
|
|
magf_accuracy_window[magf_window_idx] = static_cast<uint8_t>(magf_data.accuracy);
|
|
|
|
|
magf_window_idx++;
|
|
|
|
|
|
|
|
|
|
if (magf_window_idx >= WINDOW_SZ)
|
|
|
|
|
magf_window_idx = 0U;
|
|
|
|
|
|
|
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
|
|
|
|
ESP_LOGI(TAG, "dynamic_calibration_run_routine(): magf_data: accuracy: %d x: %.3f y: %.3f z: %.3f ",
|
|
|
|
|
static_cast<uint8_t>(magf_data.accuracy), magf_data.x, magf_data.y, magf_data.z);
|
|
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
quat_window_sum = 0U;
|
|
|
|
|
magf_window_sum = 0U;
|
|
|
|
|
|
|
|
|
|
// sum windows and take average
|
|
|
|
|
for (int i = 0U; i < WINDOW_SZ; i++)
|
|
|
|
|
{
|
|
|
|
|
quat_window_sum += quat_accuracy_window[i];
|
|
|
|
|
magf_window_sum += magf_accuracy_window[i];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
quat_accuracy_avg = static_cast<BNO08xAccuracy>(quat_window_sum / WINDOW_SZ);
|
|
|
|
|
magf_accuracy_avg = static_cast<BNO08xAccuracy>(magf_window_sum / WINDOW_SZ);
|
|
|
|
|
|
|
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
|
|
|
|
ESP_LOGI(TAG, "dynamic_calibration_run_routine(): magf_avg_acc: %s quat_avg_acc: %s", accuracy_to_str(magf_accuracy_avg), accuracy_to_str(quat_accuracy_avg));
|
|
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
|
|
if ((quat_accuracy_avg >= BNO08xAccuracy::HIGH) && (magf_accuracy_avg >= BNO08xAccuracy::MED))
|
|
|
|
|
{
|
|
|
|
|
// start timer if accuracy criteria is met
|
|
|
|
|
if (stable_start_time == 0LL)
|
|
|
|
|
stable_start_time = esp_timer_get_time();
|
|
|
|
|
|
|
|
|
|
// calculate time for which accuracy criteria has been met
|
|
|
|
|
stable_time = esp_timer_get_time() - stable_start_time;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// reset timer if accuracy criteria is not met
|
|
|
|
|
stable_time = 0LL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// check if average accuracy has been stable for required time
|
|
|
|
|
if (stable_time >= STABLE_TIME_CRITERIA_US)
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
|
|
|
|
ESP_LOGI(TAG, "dynamic_calibration_run_routine(): calibration accuracy threshold reached, sending command to save calibration data...");
|
|
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
|
|
if (!dynamic_calibration_save())
|
|
|
|
|
{
|
|
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
|
|
|
|
ESP_LOGI(TAG, "dynamic_calibration_run_routine(): failed to save calibration data");
|
|
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!dynamic_calibration_disable(BNO08xCalSel::all))
|
|
|
|
|
{
|
|
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
|
|
|
|
ESP_LOGI(TAG, "dynamic_calibration_run_routine(): failed to disable calibration");
|
|
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!disable_all_reports())
|
|
|
|
|
{
|
|
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
|
|
|
|
ESP_LOGE(TAG, "dynamic_calibration_run_routine(): failed to disable all reports after calibration");
|
|
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
|
|
|
|
ESP_LOGI(TAG, "dynamic_calibration_run_routine(): calibration success");
|
|
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-25 16:07:18 +01:00
|
|
|
/**
|
|
|
|
|
* @brief Deletes dynamic calibration data from BNO08x internal flash and resets the device.
|
|
|
|
|
* Follows the steps outlined in ref. manual 6.4.9
|
|
|
|
|
* @return True if delete dynamic calibration data operation succeeded.
|
|
|
|
|
*/
|
2025-05-25 15:58:44 +01:00
|
|
|
bool BNO08x::delete_calibration_data()
|
|
|
|
|
{
|
|
|
|
|
// 1. Reset hub (using hard_reset)
|
|
|
|
|
if (!hard_reset()) {
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
|
|
|
|
ESP_LOGE(TAG, "delete_calibration_data(): failed to hard reset hub");
|
|
|
|
|
#endif
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 2. Delete flash copy of DCD via FRS
|
|
|
|
|
int op_success = SH2_ERR;
|
|
|
|
|
lock_sh2_HAL();
|
|
|
|
|
// Deleting FRS record: use sh2_setFrs with nullptr and 0 words
|
|
|
|
|
op_success = sh2_setFrs(DYNAMIC_CALIBRATION, nullptr, 0U);
|
|
|
|
|
unlock_sh2_HAL();
|
|
|
|
|
if (op_success != SH2_OK) {
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
|
|
|
|
ESP_LOGE(TAG, "delete_calibration_data(): failed to delete DCD FRS record, op_success: %li", (int32_t)op_success);
|
|
|
|
|
#endif
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 3. Issue Clear DCD and Reset Command (atomic clear DCD from RAM and reset)
|
|
|
|
|
lock_sh2_HAL();
|
|
|
|
|
op_success = sh2_clearDcdAndReset();
|
|
|
|
|
unlock_sh2_HAL();
|
|
|
|
|
if (op_success != SH2_OK) {
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
|
|
|
|
ESP_LOGE(TAG, "delete_calibration_data(): failed to clear DCD and reset, op_success: %li", (int32_t)op_success);
|
|
|
|
|
#endif
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
|
|
|
|
ESP_LOGI(TAG, "delete_calibration_data(): calibration data cleared successfully");
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-12-05 04:07:40 +00:00
|
|
|
/**
|
2025-04-13 19:34:41 +01:00
|
|
|
* @brief Retrieves a record from flash record system (if your goal is to retrieve sensor specific meta data use the
|
2024-12-05 04:07:40 +00:00
|
|
|
* BNO08xRpt:get_meta_data() method instead)
|
2024-11-26 22:56:33 +00:00
|
|
|
*
|
2025-04-13 19:34:41 +01:00
|
|
|
* For more details on returned and data and frs_IDs see ref. manual 6.3.7 & 4.3
|
2024-11-26 22:56:33 +00:00
|
|
|
*
|
|
|
|
|
* @param frs_ID The ID of the desired record to retrieve from flash.
|
|
|
|
|
* @param data Buffer of 16 uint32_t to store retrieved data.
|
|
|
|
|
* @param rx_data_sz Reference to store number of 32 bit words retrieved from flash.
|
|
|
|
|
*
|
|
|
|
|
* @return True if get flash record system operation succeeded.
|
|
|
|
|
*/
|
2025-04-13 19:34:41 +01:00
|
|
|
bool BNO08x::get_frs(BNO08xFrsID frs_ID, uint32_t (&data)[16], uint16_t& rx_data_sz)
|
2024-11-26 21:28:27 +00:00
|
|
|
{
|
|
|
|
|
int op_success = SH2_ERR;
|
|
|
|
|
|
|
|
|
|
lock_sh2_HAL();
|
2025-04-13 19:34:41 +01:00
|
|
|
op_success = sh2_getFrs(static_cast<uint16_t>(frs_ID), data, &rx_data_sz);
|
2024-11-26 21:28:27 +00:00
|
|
|
unlock_sh2_HAL();
|
|
|
|
|
|
|
|
|
|
return (op_success == SH2_OK);
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-13 19:34:41 +01:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Writes a record to flash record system.
|
|
|
|
|
*
|
|
|
|
|
* For more details on flash records and frs_IDs see ref. manual 6.3.6 & 4.3
|
|
|
|
|
*
|
|
|
|
|
* @param frs_ID The ID of the desired to write to flash.
|
|
|
|
|
* @param data Buffer of 16 uint32_t to store data to send.
|
|
|
|
|
* @param tx_data_sz Length of data, amount of 32 bit words to write to flash.
|
|
|
|
|
*
|
|
|
|
|
* @return True if get flash record system operation succeeded.
|
|
|
|
|
*/
|
|
|
|
|
bool BNO08x::write_frs(BNO08xFrsID frs_ID, uint32_t *data, const uint16_t tx_data_sz)
|
|
|
|
|
{
|
|
|
|
|
int op_success = SH2_ERR;
|
|
|
|
|
|
|
|
|
|
lock_sh2_HAL();
|
|
|
|
|
op_success = sh2_setFrs(static_cast<uint16_t>(frs_ID), data, tx_data_sz);
|
|
|
|
|
unlock_sh2_HAL();
|
|
|
|
|
|
|
|
|
|
return (op_success == SH2_OK);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2024-12-05 04:07:40 +00:00
|
|
|
/**
|
|
|
|
|
* @brief Returns product ID info sent by IMU at initialization.
|
|
|
|
|
*
|
|
|
|
|
* @return The product ID info returned at initialization.
|
|
|
|
|
*/
|
|
|
|
|
sh2_ProductIds_t BNO08x::get_product_IDs()
|
|
|
|
|
{
|
|
|
|
|
return product_IDs;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-21 07:47:29 +00:00
|
|
|
/**
|
2024-11-23 01:05:03 +00:00
|
|
|
* @brief Waits for HINT pin assertion or HOST_INT_TIMEOUT_DEFAULT_MS to elapse.
|
2024-11-21 07:47:29 +00:00
|
|
|
*
|
|
|
|
|
*
|
2024-11-23 01:05:03 +00:00
|
|
|
* @return ESP_OK if HINT was asserted.
|
2024-11-21 07:47:29 +00:00
|
|
|
*/
|
2024-11-23 01:05:03 +00:00
|
|
|
esp_err_t BNO08x::wait_for_hint()
|
2024-11-21 07:47:29 +00:00
|
|
|
{
|
2024-11-23 01:05:03 +00:00
|
|
|
EventBits_t spi_evt_bits;
|
|
|
|
|
|
2024-12-05 06:09:15 +00:00
|
|
|
spi_evt_bits = xEventGroupWaitBits(
|
|
|
|
|
sync_ctx.evt_grp_task, EVT_GRP_BNO08x_TASK_HINT_ASSRT_BIT, pdTRUE, pdFALSE, HOST_INT_TIMEOUT_DEFAULT_MS);
|
2024-11-23 01:05:03 +00:00
|
|
|
|
|
|
|
|
if (spi_evt_bits & EVT_GRP_BNO08x_TASK_HINT_ASSRT_BIT)
|
|
|
|
|
return ESP_OK;
|
2024-11-21 07:47:29 +00:00
|
|
|
else
|
2024-11-23 01:05:03 +00:00
|
|
|
return ESP_ERR_TIMEOUT;
|
2024-11-21 07:47:29 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-26 08:47:01 +00:00
|
|
|
/**
|
|
|
|
|
* @brief Waits for SH2 HAL lib to detect reset or HOST_INT_TIMEOUT_DEFAULT_MS to elapse.
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @return ESP_OK if reset was detected by SH2 HAL lib.
|
|
|
|
|
*/
|
|
|
|
|
esp_err_t BNO08x::wait_for_reset()
|
|
|
|
|
{
|
2024-12-05 06:09:15 +00:00
|
|
|
if (xEventGroupWaitBits(
|
|
|
|
|
sync_ctx.evt_grp_task, EVT_GRP_BNO08x_TASK_RESET_OCCURRED, pdFALSE, pdFALSE, HOST_INT_TIMEOUT_DEFAULT_MS) &
|
2024-11-26 08:47:01 +00:00
|
|
|
EVT_GRP_BNO08x_TASK_RESET_OCCURRED)
|
|
|
|
|
return ESP_OK;
|
|
|
|
|
else
|
|
|
|
|
return ESP_ERR_TIMEOUT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Toggles reset gpio pin for hard reset of BNO08x device.
|
|
|
|
|
*
|
|
|
|
|
*
|
|
|
|
|
* @return void, nothing to return
|
|
|
|
|
*/
|
|
|
|
|
void BNO08x::toggle_reset()
|
|
|
|
|
{
|
|
|
|
|
gpio_intr_disable(imu_config.io_int); // disable interrupts before reset
|
|
|
|
|
|
|
|
|
|
gpio_set_level(imu_config.io_cs, 1);
|
|
|
|
|
|
|
|
|
|
gpio_set_level(imu_config.io_rst, 0); // set reset pin low
|
2024-12-05 06:09:15 +00:00
|
|
|
vTaskDelay(HARD_RESET_DELAY_MS); // 10ns min, set to larger delay to let things stabilize(Anton)
|
2024-11-26 08:47:01 +00:00
|
|
|
gpio_intr_enable(imu_config.io_int); // enable interrupts before bringing out of reset
|
|
|
|
|
gpio_set_level(imu_config.io_rst, 1); // bring out of reset
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-21 09:01:35 +00:00
|
|
|
/**
|
2024-12-05 04:07:40 +00:00
|
|
|
* @brief Re-enables all reports enabled by user (called when BNO08x reset is detected by sh2 HAL
|
|
|
|
|
* lib).
|
2024-11-21 09:01:35 +00:00
|
|
|
*
|
2024-11-23 01:05:03 +00:00
|
|
|
* @return ESP_OK if enabled reports were successfuly re-enabled.
|
2024-11-21 09:01:35 +00:00
|
|
|
*/
|
2024-11-23 01:05:03 +00:00
|
|
|
esp_err_t BNO08x::re_enable_reports()
|
2024-11-21 09:01:35 +00:00
|
|
|
{
|
2024-12-05 06:09:15 +00:00
|
|
|
EventBits_t report_en_bits = xEventGroupGetBits(sync_ctx.evt_grp_rpt_en);
|
2024-11-23 01:05:03 +00:00
|
|
|
|
2024-12-05 06:09:15 +00:00
|
|
|
for (const auto& rpt_ID : sync_ctx.en_report_ids)
|
2024-11-21 09:01:35 +00:00
|
|
|
{
|
2024-12-05 02:12:10 +00:00
|
|
|
BNO08xRpt* rpt = usr_reports.at(rpt_ID);
|
|
|
|
|
if (rpt == nullptr)
|
|
|
|
|
{
|
|
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
|
|
|
|
ESP_LOGE(TAG, "NULL pointer detected in usr_reports map for enabled report.");
|
|
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2024-12-01 04:52:00 +00:00
|
|
|
|
2024-11-23 01:05:03 +00:00
|
|
|
if (rpt->rpt_bit & report_en_bits)
|
|
|
|
|
{
|
|
|
|
|
if (!rpt->enable(rpt->period_us))
|
2024-11-26 08:47:01 +00:00
|
|
|
{
|
|
|
|
|
// clang-format off
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
|
|
|
|
ESP_LOGE(TAG, "Failed to re-enable: %d", rpt->ID);
|
|
|
|
|
#endif
|
|
|
|
|
// clang-format on
|
2024-11-23 01:05:03 +00:00
|
|
|
return ESP_FAIL;
|
2024-11-26 08:47:01 +00:00
|
|
|
}
|
2024-11-23 01:05:03 +00:00
|
|
|
}
|
2024-11-21 09:01:35 +00:00
|
|
|
}
|
2024-11-23 01:05:03 +00:00
|
|
|
|
2024-12-05 06:09:15 +00:00
|
|
|
xEventGroupClearBits(sync_ctx.evt_grp_task, EVT_GRP_BNO08x_TASK_RESET_OCCURRED);
|
2024-11-23 01:05:03 +00:00
|
|
|
|
|
|
|
|
return ESP_OK;
|
2024-11-21 09:01:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2024-11-23 01:05:03 +00:00
|
|
|
* @brief Polls for new data/report to become available.
|
2024-11-21 09:01:35 +00:00
|
|
|
*
|
2024-11-23 01:05:03 +00:00
|
|
|
* @return True if new data/report became available before DATA_AVAILABLE_TIMEOUT_MS.
|
2024-11-21 09:01:35 +00:00
|
|
|
*/
|
2024-11-23 01:05:03 +00:00
|
|
|
bool BNO08x::data_available()
|
2024-11-21 09:01:35 +00:00
|
|
|
{
|
2024-11-23 01:05:03 +00:00
|
|
|
|
2024-12-05 06:09:15 +00:00
|
|
|
if (xEventGroupWaitBits(
|
|
|
|
|
sync_ctx.evt_grp_task, EVT_GRP_BNO08x_TASK_DATA_AVAILABLE, pdTRUE, pdFALSE, DATA_AVAILABLE_TIMEOUT_MS) &
|
2024-11-24 03:22:06 +00:00
|
|
|
EVT_GRP_BNO08x_TASK_DATA_AVAILABLE)
|
2024-11-21 09:01:35 +00:00
|
|
|
return true;
|
2024-11-23 01:05:03 +00:00
|
|
|
|
|
|
|
|
return false;
|
2024-11-21 09:01:35 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-20 05:32:31 +00:00
|
|
|
/**
|
2024-11-23 01:05:03 +00:00
|
|
|
* @brief Registers a callback to execute when new data from a report is received.
|
2024-11-20 05:32:31 +00:00
|
|
|
*
|
2024-11-23 01:05:03 +00:00
|
|
|
* @param cb_fxn Pointer to the call-back function should be of void return type void input param.
|
2024-11-20 05:32:31 +00:00
|
|
|
*
|
2024-11-23 01:05:03 +00:00
|
|
|
* @return void, nothing to return
|
2024-11-20 05:32:31 +00:00
|
|
|
*/
|
2024-12-05 02:12:10 +00:00
|
|
|
bool BNO08x::register_cb(std::function<void(void)> cb_fxn)
|
2024-11-20 05:32:31 +00:00
|
|
|
{
|
2024-12-05 02:12:10 +00:00
|
|
|
|
2024-12-05 06:09:15 +00:00
|
|
|
if (sync_ctx.cb_list.size() < CONFIG_ESP32_BNO08X_CB_MAX)
|
2024-12-05 02:12:10 +00:00
|
|
|
{
|
2024-12-05 06:09:15 +00:00
|
|
|
sync_ctx.cb_list.push_back(BNO08xCbParamVoid(cb_fxn, 0U));
|
2024-12-05 02:12:10 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2024-11-20 05:32:31 +00:00
|
|
|
}
|
|
|
|
|
|
2024-11-20 19:12:11 +00:00
|
|
|
/**
|
2024-12-05 04:07:40 +00:00
|
|
|
* @brief Registers a callback to execute when new data from a report is received, overloaded with
|
|
|
|
|
* callback param for most recent report ID.
|
2024-11-20 19:12:11 +00:00
|
|
|
*
|
2024-12-05 04:07:40 +00:00
|
|
|
* @param cb_fxn Pointer to the call-back function should be of void return type with single input
|
|
|
|
|
* param of uint8_t for most recent report ID.
|
2024-11-20 19:12:11 +00:00
|
|
|
*
|
2024-11-23 01:05:03 +00:00
|
|
|
* @return void, nothing to return
|
2024-11-20 19:12:11 +00:00
|
|
|
*/
|
2024-12-05 02:12:10 +00:00
|
|
|
bool BNO08x::register_cb(std::function<void(uint8_t report_ID)> cb_fxn)
|
2024-11-20 19:12:11 +00:00
|
|
|
{
|
2024-12-05 06:09:15 +00:00
|
|
|
if (sync_ctx.cb_list.size() < CONFIG_ESP32_BNO08X_CB_MAX)
|
2024-12-05 02:12:10 +00:00
|
|
|
{
|
2024-12-05 06:09:15 +00:00
|
|
|
sync_ctx.cb_list.push_back(BNO08xCbParamRptID(cb_fxn, 0U));
|
2024-12-05 02:12:10 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2024-11-20 19:12:11 +00:00
|
|
|
}
|
|
|
|
|
|
2024-07-22 21:14:03 +01:00
|
|
|
/**
|
2024-11-20 01:02:16 +00:00
|
|
|
* @brief Prints product IDs received at initialization.
|
2024-07-22 21:14:03 +01:00
|
|
|
*
|
2023-11-07 05:10:02 +00:00
|
|
|
* @return void, nothing to return
|
|
|
|
|
*/
|
2024-11-20 01:02:16 +00:00
|
|
|
void BNO08x::print_product_ids()
|
2024-11-14 06:42:21 +00:00
|
|
|
{
|
2024-11-20 05:32:31 +00:00
|
|
|
for (int i = 0; i < product_IDs.numEntries; i++)
|
2024-11-20 01:02:16 +00:00
|
|
|
{
|
|
|
|
|
ESP_LOGI(TAG,
|
|
|
|
|
"Product ID %d Info: \n\r"
|
|
|
|
|
" ---------------------------\n\r"
|
|
|
|
|
" Product ID: 0x%" PRIx32 "\n\r"
|
|
|
|
|
" SW Version Major: 0x%" PRIx8 "\n\r"
|
|
|
|
|
" SW Version Minor: 0x%" PRIx8 "\n\r"
|
|
|
|
|
" SW Build Number: 0x%" PRIx32 "\n\r"
|
|
|
|
|
" SW Version Patch: 0x%" PRIx16 "\n\r"
|
|
|
|
|
" ---------------------------\n\r",
|
2024-12-05 06:09:15 +00:00
|
|
|
i, product_IDs.entry->swPartNumber, product_IDs.entry->swVersionMajor, product_IDs.entry->swVersionMinor,
|
|
|
|
|
product_IDs.entry->swBuildNumber, product_IDs.entry->swVersionPatch);
|
2024-11-14 09:36:28 +00:00
|
|
|
}
|
2024-11-14 06:42:21 +00:00
|
|
|
}
|
|
|
|
|
|
2025-05-25 15:58:44 +01:00
|
|
|
|
|
|
|
|
// Converts a 32-bit signed Q30 fixed-point value to float
|
|
|
|
|
static inline float q30_to_float(int32_t q)
|
|
|
|
|
{
|
|
|
|
|
return ((float)q) / (float)(1UL << 30);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Converts a float to 32-bit signed Q30 fixed-point value
|
|
|
|
|
static inline int32_t float_to_q30(float f)
|
|
|
|
|
{
|
|
|
|
|
if (f > 1.0f) f = 1.0f;
|
|
|
|
|
if (f < -1.0f) f = -1.0f;
|
|
|
|
|
return (int32_t)(f * (float)(1UL << 30));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void BNO08x::print_system_orientation()
|
|
|
|
|
{
|
|
|
|
|
float w, x, y, z;
|
|
|
|
|
if (get_system_orientation(w, x, y, z)) {
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
|
|
|
|
ESP_LOGI(TAG, "Mounting orientation (float): W: %.6f X: %.6f Y: %.6f Z: %.6f", w, x, y, z);
|
|
|
|
|
#endif
|
|
|
|
|
} else {
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
|
|
|
|
ESP_LOGE(TAG, "Failed to get mounting orientation");
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Sets the system orientation of the BNO08x device and persist it in flash (FRS).
|
|
|
|
|
* use SQRT2OVER2 as a constant for sqrt(2)/2
|
|
|
|
|
* see Datasheet Figure 4.3 for reference
|
|
|
|
|
* Note that a reset is required to apply changes.
|
|
|
|
|
* Note also that this configuration seems only to work if reports are already enabled.
|
|
|
|
|
* e.g. set .rpt.rv.enable(true) prior this call
|
|
|
|
|
*/
|
|
|
|
|
bool BNO08x::set_system_orientation(float w, float x, float y, float z)
|
|
|
|
|
{
|
|
|
|
|
uint32_t orientation_raw[4] = {
|
|
|
|
|
static_cast<uint32_t>(float_to_q30(x)), // X component
|
|
|
|
|
static_cast<uint32_t>(float_to_q30(y)), // Y component
|
|
|
|
|
static_cast<uint32_t>(float_to_q30(z)), // Z component
|
|
|
|
|
static_cast<uint32_t>(float_to_q30(w)) // W component
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
int op_success = SH2_ERR;
|
|
|
|
|
lock_sh2_HAL();
|
|
|
|
|
op_success = sh2_setFrs(SYSTEM_ORIENTATION, (uint32_t*)&orientation_raw, 4);
|
|
|
|
|
unlock_sh2_HAL();
|
|
|
|
|
|
|
|
|
|
if (op_success != SH2_OK)
|
|
|
|
|
{
|
|
|
|
|
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
|
|
|
|
ESP_LOGE(TAG, "Failed to set mounting orientation, op_success: %li", (int32_t)op_success);
|
|
|
|
|
#endif
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool BNO08x::get_system_orientation(float& w, float& x, float& y, float& z)
|
|
|
|
|
{
|
|
|
|
|
uint16_t words = 4;
|
|
|
|
|
uint32_t raw[4] = {0};
|
|
|
|
|
|
|
|
|
|
lock_sh2_HAL();
|
|
|
|
|
int op_success = sh2_getFrs(SYSTEM_ORIENTATION, raw, &words);
|
|
|
|
|
unlock_sh2_HAL();
|
|
|
|
|
|
|
|
|
|
if (op_success != SH2_OK) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
x = q30_to_float((int32_t)raw[0]);
|
|
|
|
|
y = q30_to_float((int32_t)raw[1]);
|
|
|
|
|
z = q30_to_float((int32_t)raw[2]);
|
|
|
|
|
w = q30_to_float((int32_t)raw[3]);
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-24 03:22:06 +00:00
|
|
|
/**
|
|
|
|
|
* @brief Converts a BNO08xActivity enum to string.
|
|
|
|
|
*
|
|
|
|
|
* @return The resulting string conversion of the enum.
|
|
|
|
|
*/
|
|
|
|
|
const char* BNO08x::activity_to_str(BNO08xActivity activity)
|
|
|
|
|
{
|
|
|
|
|
switch (activity)
|
|
|
|
|
{
|
|
|
|
|
case BNO08xActivity::UNKNOWN:
|
|
|
|
|
return "UNKNOWN";
|
|
|
|
|
case BNO08xActivity::IN_VEHICLE:
|
|
|
|
|
return "IN_VEHICLE";
|
|
|
|
|
case BNO08xActivity::ON_BICYCLE:
|
|
|
|
|
return "ON_BICYCLE";
|
|
|
|
|
case BNO08xActivity::ON_FOOT:
|
|
|
|
|
return "ON_FOOT";
|
|
|
|
|
case BNO08xActivity::STILL:
|
|
|
|
|
return "STILL";
|
|
|
|
|
case BNO08xActivity::TILTING:
|
|
|
|
|
return "TILTING";
|
|
|
|
|
case BNO08xActivity::WALKING:
|
|
|
|
|
return "WALKING";
|
|
|
|
|
case BNO08xActivity::RUNNING:
|
|
|
|
|
return "RUNNING";
|
|
|
|
|
case BNO08xActivity::ON_STAIRS:
|
|
|
|
|
return "ON_STAIRS";
|
|
|
|
|
case BNO08xActivity::UNDEFINED:
|
|
|
|
|
return "UNDEFINED";
|
|
|
|
|
default:
|
|
|
|
|
return "UNDEFINED";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @brief Converts a BNO08xStability enum to string.
|
|
|
|
|
*
|
|
|
|
|
* @return The resulting string conversion of the enum.
|
|
|
|
|
*/
|
|
|
|
|
const char* BNO08x::stability_to_str(BNO08xStability stability)
|
|
|
|
|
{
|
|
|
|
|
switch (stability)
|
|
|
|
|
{
|
|
|
|
|
case BNO08xStability::UNKNOWN:
|
|
|
|
|
return "UNKNOWN";
|
|
|
|
|
case BNO08xStability::ON_TABLE:
|
|
|
|
|
return "ON_TABLE";
|
|
|
|
|
case BNO08xStability::STATIONARY:
|
|
|
|
|
return "STATIONARY";
|
|
|
|
|
case BNO08xStability::STABLE:
|
|
|
|
|
return "STABLE";
|
|
|
|
|
case BNO08xStability::MOTION:
|
|
|
|
|
return "MOTION";
|
|
|
|
|
case BNO08xStability::RESERVED:
|
|
|
|
|
return "RESERVED";
|
|
|
|
|
case BNO08xStability::UNDEFINED:
|
|
|
|
|
return "UNDEFINED";
|
|
|
|
|
default:
|
|
|
|
|
return "UNDEFINED";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-26 21:28:27 +00:00
|
|
|
const char* BNO08x::accuracy_to_str(BNO08xAccuracy accuracy)
|
|
|
|
|
{
|
|
|
|
|
switch (accuracy)
|
|
|
|
|
{
|
|
|
|
|
case BNO08xAccuracy::UNRELIABLE:
|
|
|
|
|
return "UNRELIABLE";
|
|
|
|
|
case BNO08xAccuracy::LOW:
|
|
|
|
|
return "LOW";
|
|
|
|
|
case BNO08xAccuracy::MED:
|
|
|
|
|
return "MED";
|
|
|
|
|
case BNO08xAccuracy::HIGH:
|
|
|
|
|
return "HIGH";
|
|
|
|
|
case BNO08xAccuracy::UNDEFINED:
|
|
|
|
|
return "UNDEFINED";
|
|
|
|
|
default:
|
|
|
|
|
return "UNDEFINED";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-07 05:10:02 +00:00
|
|
|
/**
|
|
|
|
|
* @brief HINT interrupt service routine, handles falling edge of BNO08x HINT pin.
|
2023-11-16 08:43:45 +00:00
|
|
|
*
|
|
|
|
|
* ISR that launches SPI task to perform transaction upon assertion of BNO08x interrupt pin.
|
|
|
|
|
*
|
2023-11-07 05:10:02 +00:00
|
|
|
* @return void, nothing to return
|
|
|
|
|
*/
|
2024-03-02 10:55:33 +00:00
|
|
|
void IRAM_ATTR BNO08x::hint_handler(void* arg)
|
|
|
|
|
{
|
2023-11-07 05:10:02 +00:00
|
|
|
BaseType_t xHighPriorityTaskWoken = pdFALSE;
|
2024-12-05 04:07:40 +00:00
|
|
|
BNO08x* imu = (BNO08x*) arg; // cast argument received by gpio_isr_handler_add ("this" pointer
|
|
|
|
|
// to imu object created by constructor call)
|
2023-11-07 05:10:02 +00:00
|
|
|
|
2024-11-20 01:02:16 +00:00
|
|
|
// notify any tasks/function calls waiting for HINT assertion
|
2024-12-05 06:09:15 +00:00
|
|
|
xEventGroupSetBitsFromISR(imu->sync_ctx.evt_grp_task, EVT_GRP_BNO08x_TASK_HINT_ASSRT_BIT, &xHighPriorityTaskWoken);
|
2024-11-20 01:02:16 +00:00
|
|
|
portYIELD_FROM_ISR(xHighPriorityTaskWoken); // perform context switch if necessary
|
2023-11-07 05:10:02 +00:00
|
|
|
}
|