functional multi-report enable with sh2 HAL
This commit is contained in:
parent
91a9ad143f
commit
54ceea449f
|
|
@ -32,50 +32,102 @@
|
|||
class BNO08x
|
||||
{
|
||||
public:
|
||||
inline static sh2_SensorConfig default_sensor_cfg = {.changeSensitivityEnabled = false, ///<TODO
|
||||
.changeSensitivityRelative = false,
|
||||
.wakeupEnabled = false,
|
||||
.alwaysOnEnabled = false,
|
||||
.changeSensitivity = 0,
|
||||
.reportInterval_us = 0,
|
||||
.batchInterval_us = 0,
|
||||
.sensorSpecific = 0};
|
||||
|
||||
BNO08x(bno08x_config_t imu_config = bno08x_config_t());
|
||||
~BNO08x();
|
||||
bool initialize();
|
||||
|
||||
void hard_reset();
|
||||
|
||||
bool enable_gravity(uint32_t time_between_reports, sh2_SensorConfig_t sensor_cfg = default_sensor_cfg);
|
||||
bool enable_linear_accelerometer(uint32_t time_between_reports, sh2_SensorConfig_t sensor_cfg = default_sensor_cfg);
|
||||
|
||||
void get_gravity(float& x, float& y, float& z);
|
||||
float get_gravity_X();
|
||||
float get_gravity_Y();
|
||||
float get_gravity_Z();
|
||||
|
||||
void get_linear_accel(float& x, float& y, float& z);
|
||||
float get_linear_accel_X();
|
||||
float get_linear_accel_Y();
|
||||
float get_linear_accel_Z();
|
||||
|
||||
void register_cb(std::function<void()> cb_fxn);
|
||||
void print_product_ids();
|
||||
void print_product_ids();
|
||||
|
||||
private:
|
||||
/// @brief Holds info about which functionality has been successfully initialized (used by deconstructor during cleanup).
|
||||
typedef struct bno08x_init_status_t
|
||||
{
|
||||
bool gpio_outputs; ///< True if GPIO outputs have been initialized.
|
||||
bool gpio_inputs; ///< True if GPIO inputs have been initialized.
|
||||
bool isr_service; ///< True if global ISR service has been initialized.
|
||||
bool isr_handler; ///< True if HINT ISR handler has been initialized.
|
||||
uint8_t task_count; ///< How many successfully initialized tasks (max of TSK_CNT)
|
||||
bool data_proc_task; ///< True if xTaskCreate has been called successfully for data_proc_task.
|
||||
bool spi_task; ///< True if xTaskCreate has been called successfully for spi_task.
|
||||
bool spi_bus; ///< True if spi_bus_initialize() has been called successfully.
|
||||
bool spi_device; ///< True if spi_bus_add_device() has been called successfully.
|
||||
bool sh2_HAL; ///< True if sh2_open() has been called successfully.
|
||||
bool gpio_outputs; ///< True if GPIO outputs have been initialized.
|
||||
bool gpio_inputs; ///< True if GPIO inputs have been initialized.
|
||||
bool isr_service; ///< True if global ISR service has been initialized.
|
||||
bool isr_handler; ///< True if HINT ISR handler has been initialized.
|
||||
bool data_proc_task; ///< True if xTaskCreate has been called successfully for data_proc_task.
|
||||
bool sh2_HAL_service_task; ///< True if xTaskCreate has been called successfully for sh2_HAL_service_task.
|
||||
bool spi_bus; ///< True if spi_bus_initialize() has been called successfully.
|
||||
bool spi_device; ///< True if spi_bus_add_device() has been called successfully.
|
||||
bool sh2_HAL; ///< True if sh2_open() has been called successfully.
|
||||
|
||||
bno08x_init_status_t()
|
||||
: gpio_outputs(false)
|
||||
, gpio_inputs(false)
|
||||
, isr_service(false)
|
||||
, isr_handler(false)
|
||||
, task_count(0)
|
||||
, data_proc_task(false)
|
||||
, spi_task(false)
|
||||
, spi_bus(false)
|
||||
, spi_device(false)
|
||||
{
|
||||
}
|
||||
} bno08x_init_status_t;
|
||||
|
||||
/// @brief Holds data returned from sensor reports.
|
||||
typedef struct bno08x_data_t
|
||||
{
|
||||
sh2_Accelerometer_t gravity;
|
||||
sh2_Accelerometer_t linear_acceleration;
|
||||
} bno08x_data_t;
|
||||
|
||||
typedef struct bno08x_usr_report_periods_t
|
||||
{
|
||||
uint32_t gravity;
|
||||
uint32_t linear_accelerometer;
|
||||
} bno08x_usr_report_periods_t;
|
||||
|
||||
bno08x_data_t data; ///< Holds all data returned from enabled reports.
|
||||
bno08x_usr_report_periods_t user_report_periods; ///< Holds periods for reports enabled by user (0 == disabled report)
|
||||
|
||||
// data processing task
|
||||
TaskHandle_t data_proc_task_hdl; ///<data_proc_task() task handle
|
||||
static void data_proc_task_trampoline(void* arg);
|
||||
void data_proc_task();
|
||||
|
||||
// sh2 service task
|
||||
TaskHandle_t sh2_HAL_service_task_hdl; ///<sh2_HAL_service_task() task handle
|
||||
static void sh2_HAL_service_task_trampoline(void* arg);
|
||||
void sh2_HAL_service_task();
|
||||
|
||||
SemaphoreHandle_t sh2_HAL_lock;
|
||||
|
||||
void handle_sensor_report(sh2_SensorValue_t* sensor_val);
|
||||
void update_gravity_data(sh2_SensorValue_t* sensor_val);
|
||||
void update_linear_accelerometer_data(sh2_SensorValue_t* sensor_val);
|
||||
|
||||
esp_err_t init_config_args();
|
||||
esp_err_t init_gpio();
|
||||
esp_err_t init_gpio_inputs();
|
||||
esp_err_t init_gpio_outputs();
|
||||
esp_err_t init_hint_isr();
|
||||
esp_err_t init_spi();
|
||||
esp_err_t init_tasks();
|
||||
esp_err_t init_sh2_HAL();
|
||||
|
||||
esp_err_t deinit_gpio();
|
||||
|
|
@ -83,14 +135,20 @@ class BNO08x
|
|||
esp_err_t deinit_gpio_outputs();
|
||||
esp_err_t deinit_hint_isr();
|
||||
esp_err_t deinit_spi();
|
||||
esp_err_t deinit_tasks();
|
||||
esp_err_t deinit_sh2_HAL();
|
||||
|
||||
esp_err_t wait_for_hint();
|
||||
|
||||
sh2_Hal_t sh2_HAL; ///< SH2 hardware abstraction layer struct for use with SH-2 lib.
|
||||
esp_err_t enable_report(sh2_SensorId_t sensor_ID, uint32_t time_between_reports, sh2_SensorConfig_t sensor_cfg);
|
||||
esp_err_t re_enable_reports();
|
||||
|
||||
EventGroupHandle_t
|
||||
evt_grp_spi; ///<Event group for indicating when bno08x hint pin has triggered and when new data has been processed. Used by calls to sending or receiving functions.
|
||||
sh2_Hal_t sh2_HAL; ///< SH2 hardware abstraction layer struct for use with SH2 lib.
|
||||
|
||||
EventGroupHandle_t evt_grp_spi; ///<Event group for indicating when bno08x hint pin has triggered and when new data has been processed.
|
||||
EventGroupHandle_t evt_grp_report_en; ///<Event group for indicating which reports are currently enabled.
|
||||
|
||||
QueueHandle_t queue_rx_sensor_event; ///< TODO
|
||||
|
||||
std::vector<std::function<void()>> cb_list; // Vector for storing any call-back functions added with register_cb()
|
||||
|
||||
|
|
@ -102,8 +160,7 @@ class BNO08x
|
|||
bno08x_init_status_t
|
||||
init_status; ///<Initialization status of various functionality, used by deconstructor during cleanup, set during initialization.
|
||||
|
||||
sh2_ProductIds_t product_ID; ///< TODO
|
||||
sh2_SensorValue_t* sensor_report_val = NULL; ///< TODO
|
||||
sh2_ProductIds_t product_IDs; ///< TODO
|
||||
|
||||
bool reset_occurred = false;
|
||||
|
||||
|
|
@ -112,7 +169,7 @@ class BNO08x
|
|||
static const constexpr uint16_t RX_DATA_LENGTH = 300U; ///<length buffer containing data received over spi
|
||||
|
||||
static const constexpr TickType_t HOST_INT_TIMEOUT_DEFAULT_MS =
|
||||
3000UL /
|
||||
500UL /
|
||||
portTICK_PERIOD_MS; ///<Max wait between HINT being asserted by BNO08x before transaction is considered failed (in miliseconds), when no reports are enabled (ie during reset)
|
||||
|
||||
static const constexpr TickType_t HARD_RESET_DELAY_MS =
|
||||
|
|
@ -128,6 +185,32 @@ class BNO08x
|
|||
(1U << 1U); ///<When this bit is set it indicates a receive procedure has completed.
|
||||
static const constexpr EventBits_t EVT_GRP_SPI_TX_DONE_BIT = (1U << 2U); ///<When this bit is set, it indicates a queued packet has been sent.
|
||||
|
||||
// evt_grp_report_en bits
|
||||
static const constexpr EventBits_t EVT_GRP_RPT_ROTATION_VECTOR_BIT_EN = (1 << 0); ///< When set, rotation vector reports are active.
|
||||
static const constexpr EventBits_t EVT_GRP_RPT_GAME_ROTATION_VECTOR_BIT_EN = (1 << 1); ///< When set, game rotation vector reports are active.
|
||||
static const constexpr EventBits_t EVT_GRP_RPT_ARVR_S_ROTATION_VECTOR_BIT_EN =
|
||||
(1U << 2U); ///< When set, ARVR stabilized rotation vector reports are active.
|
||||
static const constexpr EventBits_t EVT_GRP_RPT_ARVR_S_GAME_ROTATION_VECTOR_BIT_EN =
|
||||
(1U << 3U); ///< When set, ARVR stabilized game rotation vector reports are active.
|
||||
static const constexpr EventBits_t EVT_GRP_RPT_GYRO_ROTATION_VECTOR_BIT_EN =
|
||||
(1U << 4U); ///< When set, gyro integrator rotation vector reports are active.
|
||||
static const constexpr EventBits_t EVT_GRP_RPT_ACCELEROMETER_BIT_EN = (1U << 5U); ///< When set, accelerometer reports are active.
|
||||
static const constexpr EventBits_t EVT_GRP_RPT_LINEAR_ACCELEROMETER_BIT_EN =
|
||||
(1U << 6U); ///< When set, linear accelerometer reports are active.
|
||||
static const constexpr EventBits_t EVT_GRP_RPT_GRAVITY_BIT_EN = (1U << 7U); ///< When set, gravity reports are active.
|
||||
static const constexpr EventBits_t EVT_GRP_RPT_GYRO_BIT_EN = (1U << 8U); ///< When set, gyro reports are active.
|
||||
static const constexpr EventBits_t EVT_GRP_RPT_GYRO_UNCALIBRATED_BIT_EN = (1U << 9U); ///< When set, uncalibrated gyro reports are active.
|
||||
static const constexpr EventBits_t EVT_GRP_RPT_MAGNETOMETER_BIT_EN = (1U << 10U); ///< When set, magnetometer reports are active.
|
||||
static const constexpr EventBits_t EVT_GRP_RPT_TAP_DETECTOR_BIT_EN = (1U << 11U); ///< When set, tap detector reports are active.
|
||||
static const constexpr EventBits_t EVT_GRP_RPT_STEP_COUNTER_BIT_EN = (1U << 12U); ///< When set, step counter reports are active.
|
||||
static const constexpr EventBits_t EVT_GRP_RPT_STABILITY_CLASSIFIER_BIT_EN =
|
||||
(1U << 13U); ///< When set, stability classifier reports are active.
|
||||
static const constexpr EventBits_t EVT_GRP_RPT_ACTIVITY_CLASSIFIER_BIT_EN =
|
||||
(1U << 14U); ///< When set, activity classifier reports are active.
|
||||
static const constexpr EventBits_t EVT_GRP_RPT_RAW_ACCELEROMETER_BIT_EN = (1U << 15U); ///< When set, raw accelerometer reports are active.
|
||||
static const constexpr EventBits_t EVT_GRP_RPT_RAW_GYRO_BIT_EN = (1U << 16U); ///< When set, raw gyro reports are active.
|
||||
static const constexpr EventBits_t EVT_GRP_RPT_RAW_MAGNETOMETER_BIT_EN = (1U << 17U); ///< When set, raw magnetometer reports are active.
|
||||
|
||||
static const constexpr char* TAG = "BNO08x"; ///< Class tag used for serial print statements
|
||||
|
||||
friend class BNO08xSH2HAL;
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ class BNO08xSH2HAL
|
|||
static int spi_write(sh2_Hal_t* self, uint8_t* pBuffer, unsigned len);
|
||||
static uint32_t get_time_us(sh2_Hal_t* self);
|
||||
static void hal_cb(void* cookie, sh2_AsyncEvent_t* pEvent);
|
||||
static void sensor_report_cb(void* cookie, sh2_SensorEvent_t* event);
|
||||
static void sensor_event_cb(void* cookie, sh2_SensorEvent_t* event);
|
||||
|
||||
private:
|
||||
static BNO08x* imu;
|
||||
|
|
@ -35,4 +35,6 @@ class BNO08xSH2HAL
|
|||
static bool spi_wait_for_int();
|
||||
static uint16_t spi_read_sh2_packet_header(uint8_t* pBuffer);
|
||||
static int spi_read_sh2_packet_body(uint8_t* pBuffer, uint16_t packet_sz);
|
||||
|
||||
static const constexpr char *TAG = "BNO08xSH2HAL";
|
||||
};
|
||||
|
|
@ -10,7 +10,12 @@
|
|||
* @return void, nothing to return
|
||||
*/
|
||||
BNO08x::BNO08x(bno08x_config_t imu_config)
|
||||
: evt_grp_spi(xEventGroupCreate())
|
||||
: data_proc_task_hdl(NULL)
|
||||
, sh2_HAL_service_task_hdl(NULL)
|
||||
, sh2_HAL_lock(xSemaphoreCreateMutex())
|
||||
, evt_grp_spi(xEventGroupCreate())
|
||||
, evt_grp_report_en(xEventGroupCreate())
|
||||
, queue_rx_sensor_event(xQueueCreate(5, sizeof(sh2_SensorEvent_t)))
|
||||
, imu_config(imu_config)
|
||||
{
|
||||
}
|
||||
|
|
@ -39,8 +44,15 @@ BNO08x::~BNO08x()
|
|||
// deinitialize GPIO if they have been initialized
|
||||
ESP_ERROR_CHECK(deinit_gpio());
|
||||
|
||||
// deinitialize tasks if they have been initialized
|
||||
ESP_ERROR_CHECK(deinit_tasks());
|
||||
|
||||
// delete event groups
|
||||
vEventGroupDelete(evt_grp_spi);
|
||||
vEventGroupDelete(evt_grp_report_en);
|
||||
|
||||
// delete all queues
|
||||
vQueueDelete(queue_rx_sensor_event);
|
||||
|
||||
// clear callback list
|
||||
cb_list.clear();
|
||||
|
|
@ -77,6 +89,10 @@ bool BNO08x::initialize()
|
|||
if (init_sh2_HAL() != ESP_OK)
|
||||
return false;
|
||||
|
||||
// initialize tasks
|
||||
if (init_tasks() != ESP_OK)
|
||||
return false;
|
||||
|
||||
// clang-format off
|
||||
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
||||
ESP_LOGI(TAG, "Successfully initialized....");
|
||||
|
|
@ -86,6 +102,113 @@ bool BNO08x::initialize()
|
|||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @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)
|
||||
{
|
||||
BNO08x* imu = (BNO08x*) arg; // cast argument received by xTaskCreate ("this" pointer to imu object created by constructor call)
|
||||
imu->data_proc_task(); // launch data processing task task from object
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Task responsible for handling sensor events sent by SH2 HAL.
|
||||
*
|
||||
* @return void, nothing to return
|
||||
*/
|
||||
void BNO08x::data_proc_task()
|
||||
{
|
||||
sh2_SensorEvent_t sensor_evt;
|
||||
sh2_SensorValue_t sensor_val;
|
||||
|
||||
while (1)
|
||||
{
|
||||
if (xQueueReceive(queue_rx_sensor_event, &sensor_evt, portMAX_DELAY) == pdTRUE)
|
||||
{
|
||||
if (sh2_decodeSensorEvent(&sensor_val, &sensor_evt) != SH2_ERR)
|
||||
handle_sensor_report(&sensor_val);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void BNO08x::sh2_HAL_service_task_trampoline(void* arg)
|
||||
{
|
||||
BNO08x* imu = (BNO08x*) arg; // cast argument received by xTaskCreate ("this" pointer to imu object created by constructor call)
|
||||
imu->sh2_HAL_service_task(); // launch data processing task task from object
|
||||
}
|
||||
|
||||
void BNO08x::sh2_HAL_service_task()
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
xEventGroupWaitBits(evt_grp_spi, EVT_GRP_SPI_HINT_ASSERTED_BIT, pdFALSE, pdFALSE, portMAX_DELAY);
|
||||
|
||||
if (reset_occurred)
|
||||
{
|
||||
reset_occurred = false;
|
||||
re_enable_reports();
|
||||
}
|
||||
|
||||
xSemaphoreTake(sh2_HAL_lock, portMAX_DELAY);
|
||||
sh2_service();
|
||||
xSemaphoreGive(sh2_HAL_lock);
|
||||
}
|
||||
}
|
||||
|
||||
void BNO08x::handle_sensor_report(sh2_SensorValue_t* sensor_val)
|
||||
{
|
||||
switch (sensor_val->sensorId)
|
||||
{
|
||||
case SH2_GRAVITY:
|
||||
update_gravity_data(sensor_val);
|
||||
ESP_LOGW(TAG, "grav: %.3lf %.3lf %.3lf", sensor_val->un.gravity.x, sensor_val->un.gravity.y, sensor_val->un.gravity.z);
|
||||
break;
|
||||
|
||||
case SH2_LINEAR_ACCELERATION:
|
||||
update_linear_accelerometer_data(sensor_val);
|
||||
ESP_LOGW(TAG, "accl: %.3lf %.3lf %.3lf", sensor_val->un.linearAcceleration.x, sensor_val->un.linearAcceleration.y,
|
||||
sensor_val->un.linearAcceleration.z);
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Updates gravity data from decoded sensor event.
|
||||
*
|
||||
* @param sensor_val The sh2_SensorValue_t struct used in sh2_decodeSensorEvent() call.
|
||||
*
|
||||
* @return void, nothing to return
|
||||
*/
|
||||
void BNO08x::update_gravity_data(sh2_SensorValue_t* sensor_val)
|
||||
{
|
||||
data.gravity.x = sensor_val->un.gravity.x;
|
||||
data.gravity.y = sensor_val->un.gravity.y;
|
||||
data.gravity.z = sensor_val->un.gravity.z;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Updates linear accelerometer data from decoded sensor event.
|
||||
*
|
||||
* @param sensor_val The sh2_SensorValue_t struct used in sh2_decodeSensorEvent() call.
|
||||
*
|
||||
* @return void, nothing to return
|
||||
*/
|
||||
void BNO08x::update_linear_accelerometer_data(sh2_SensorValue_t* sensor_val)
|
||||
{
|
||||
data.linear_acceleration.x = sensor_val->un.linearAcceleration.x;
|
||||
data.linear_acceleration.y = sensor_val->un.linearAcceleration.y;
|
||||
data.linear_acceleration.z = sensor_val->un.linearAcceleration.z;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Initializes required esp-idf SPI data structures with values from user passed bno08x_config_t struct.
|
||||
*
|
||||
|
|
@ -330,6 +453,57 @@ esp_err_t BNO08x::init_hint_isr()
|
|||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Initializes data_proc_task.
|
||||
*
|
||||
* @return ESP_OK if initialization was success.
|
||||
*/
|
||||
esp_err_t BNO08x::init_tasks()
|
||||
{
|
||||
BaseType_t task_created = pdFALSE;
|
||||
|
||||
vTaskDelay(100 / portTICK_PERIOD_MS);
|
||||
|
||||
// launch data processing task
|
||||
task_created = xTaskCreate(
|
||||
&data_proc_task_trampoline, "bno08x_data_processing_task", CONFIG_ESP32_BNO08X_DATA_PROC_TASK_SZ, this, 7, &data_proc_task_hdl);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
// launch data processing task
|
||||
task_created = xTaskCreate(&sh2_HAL_service_task_trampoline, "bno08x_sh2_HAL_service_task", 4095U, this, 7, &sh2_HAL_service_task_hdl);
|
||||
|
||||
if (task_created != pdTRUE)
|
||||
{
|
||||
// clang-format off
|
||||
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
||||
ESP_LOGE(TAG, "Initialization failed, sh2_HAL_service_task failed to launch.");
|
||||
#endif
|
||||
// clang-format on
|
||||
|
||||
return ESP_FAIL;
|
||||
}
|
||||
else
|
||||
{
|
||||
init_status.sh2_HAL_service_task = true;
|
||||
}
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Initializes SPI.
|
||||
*
|
||||
|
|
@ -396,14 +570,30 @@ esp_err_t BNO08x::init_sh2_HAL()
|
|||
hard_reset();
|
||||
|
||||
if (sh2_open(&sh2_HAL, BNO08xSH2HAL::hal_cb, NULL) != SH2_OK)
|
||||
{
|
||||
// clang-format off
|
||||
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
||||
ESP_LOGE(TAG, "Initialization failed, sh2_open() call failed.");
|
||||
#endif
|
||||
// clang-format on
|
||||
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
init_status.sh2_HAL = true;
|
||||
|
||||
memset(&product_ID, 0, sizeof(product_ID));
|
||||
memset(&product_IDs, 0, sizeof(product_IDs));
|
||||
|
||||
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
|
||||
|
||||
if (sh2_getProdIds(&product_ID) != SH2_OK)
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
// clang-format off
|
||||
#ifdef CONFIG_ESP32_BNO08x_LOG_STATEMENTS
|
||||
|
|
@ -411,7 +601,8 @@ esp_err_t BNO08x::init_sh2_HAL()
|
|||
#endif
|
||||
// clang-format on
|
||||
|
||||
sh2_setSensorCallback(BNO08xSH2HAL::sensor_report_cb, NULL);
|
||||
if (sh2_setSensorCallback(BNO08xSH2HAL::sensor_event_cb, NULL) != SH2_OK)
|
||||
return ESP_FAIL;
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
|
@ -593,6 +784,22 @@ esp_err_t BNO08x::deinit_spi()
|
|||
return ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Deinitializes tasks used by BNO08x driver.
|
||||
*
|
||||
* @return ESP_OK if deinitialization was success.
|
||||
*/
|
||||
esp_err_t BNO08x::deinit_tasks()
|
||||
{
|
||||
if (init_status.data_proc_task)
|
||||
vTaskDelete(data_proc_task_hdl);
|
||||
|
||||
if (init_status.sh2_HAL_service_task)
|
||||
vTaskDelete(sh2_HAL_service_task_hdl);
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Deinitializes sh2 HAL.
|
||||
*
|
||||
|
|
@ -627,6 +834,95 @@ void BNO08x::hard_reset()
|
|||
gpio_set_level(imu_config.io_rst, 1); // bring out of reset
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Sends command to enable gravity reports. (See Ref. Manual 6.5.11)
|
||||
*
|
||||
* @param report_period_us The period/interval of the report in microseconds.
|
||||
* @param sensor_cfg Sensor special configuration (optional), see default_sensor_cfg for defaults.
|
||||
*
|
||||
* @return ESP_OK if report was successfully enabled.
|
||||
*/
|
||||
bool BNO08x::enable_gravity(uint32_t time_between_reports, sh2_SensorConfig_t sensor_cfg)
|
||||
{
|
||||
if (enable_report(SH2_GRAVITY, time_between_reports, sensor_cfg) != ESP_OK)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
ESP_LOGE(TAG, "ENABLED");
|
||||
user_report_periods.gravity = time_between_reports;
|
||||
xEventGroupSetBits(evt_grp_report_en, EVT_GRP_RPT_GRAVITY_BIT_EN);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Sends command to enable linear accelerometer reports. (See Ref. Manual 6.5.10)
|
||||
*
|
||||
* @param report_period_us The period/interval of the report in microseconds.
|
||||
* @param sensor_cfg Sensor special configuration (optional), see default_sensor_cfg for defaults.
|
||||
*
|
||||
* @return ESP_OK if report was successfully enabled.
|
||||
*/
|
||||
bool BNO08x::enable_linear_accelerometer(uint32_t time_between_reports, sh2_SensorConfig_t sensor_cfg)
|
||||
{
|
||||
if (enable_report(SH2_LINEAR_ACCELERATION, time_between_reports, sensor_cfg) != ESP_OK)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
user_report_periods.linear_accelerometer = time_between_reports;
|
||||
xEventGroupSetBits(evt_grp_report_en, EVT_GRP_RPT_LINEAR_ACCELEROMETER_BIT_EN);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
void BNO08x::get_gravity(float& x, float& y, float& z)
|
||||
{
|
||||
x = data.gravity.x;
|
||||
y = data.gravity.y;
|
||||
z = data.gravity.z;
|
||||
}
|
||||
|
||||
float BNO08x::get_gravity_X()
|
||||
{
|
||||
return data.gravity.x;
|
||||
}
|
||||
|
||||
float BNO08x::get_gravity_Y()
|
||||
{
|
||||
return data.gravity.y;
|
||||
}
|
||||
|
||||
float BNO08x::get_gravity_Z()
|
||||
{
|
||||
return data.gravity.z;
|
||||
}
|
||||
|
||||
void BNO08x::get_linear_accel(float& x, float& y, float& z)
|
||||
{
|
||||
x = data.linear_acceleration.x;
|
||||
y = data.linear_acceleration.y;
|
||||
z = data.linear_acceleration.z;
|
||||
}
|
||||
|
||||
float BNO08x::get_linear_accel_X()
|
||||
{
|
||||
return data.linear_acceleration.x;
|
||||
}
|
||||
|
||||
float BNO08x::get_linear_accel_Y()
|
||||
{
|
||||
return data.linear_acceleration.y;
|
||||
}
|
||||
|
||||
float BNO08x::get_linear_accel_Z()
|
||||
{
|
||||
return data.linear_acceleration.z;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Waits for HINT pin assertion or HOST_INT_TIMEOUT_DEFAULT_MS to elapse.
|
||||
*
|
||||
|
|
@ -645,6 +941,59 @@ esp_err_t BNO08x::wait_for_hint()
|
|||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Enables a sensor report such that the BNO08x begins sending it.
|
||||
*
|
||||
* @param sensor_ID The ID of the sensor for the respective report to be enabled.
|
||||
* @param report_period_us The period/interval of the report in microseconds.
|
||||
* @param sensor_cfg Sensor special configuration.
|
||||
*
|
||||
* @return ESP_OK if report was successfully enabled.
|
||||
*/
|
||||
esp_err_t BNO08x::enable_report(sh2_SensorId_t sensor_ID, uint32_t time_between_reports, sh2_SensorConfig_t sensor_cfg)
|
||||
{
|
||||
|
||||
static sh2_SensorConfig_t config;
|
||||
|
||||
xSemaphoreTake(sh2_HAL_lock, portMAX_DELAY);
|
||||
// These sensor options are disabled or not used in most cases
|
||||
config.changeSensitivityEnabled = false;
|
||||
config.wakeupEnabled = false;
|
||||
config.changeSensitivityRelative = false;
|
||||
config.alwaysOnEnabled = false;
|
||||
config.changeSensitivity = 0;
|
||||
config.batchInterval_us = 0;
|
||||
config.sensorSpecific = 0;
|
||||
|
||||
config.reportInterval_us = time_between_reports;
|
||||
|
||||
if (sh2_setSensorConfig(sensor_ID, &config) != SH2_OK)
|
||||
{
|
||||
xSemaphoreGive(sh2_HAL_lock);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
else
|
||||
{
|
||||
xSemaphoreGive(sh2_HAL_lock);
|
||||
return ESP_OK;
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t BNO08x::re_enable_reports()
|
||||
{
|
||||
EventBits_t report_en_bits = xEventGroupGetBits(evt_grp_report_en);
|
||||
|
||||
if (report_en_bits & EVT_GRP_RPT_GRAVITY_BIT_EN)
|
||||
if (!enable_gravity(user_report_periods.gravity))
|
||||
return ESP_FAIL;
|
||||
|
||||
if (report_en_bits & EVT_GRP_RPT_LINEAR_ACCELEROMETER_BIT_EN)
|
||||
if (!enable_linear_accelerometer(user_report_periods.linear_accelerometer))
|
||||
return ESP_FAIL;
|
||||
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Registers a callback to execute when new data from a report is received.
|
||||
*
|
||||
|
|
@ -664,7 +1013,7 @@ void BNO08x::register_cb(std::function<void()> cb_fxn)
|
|||
*/
|
||||
void BNO08x::print_product_ids()
|
||||
{
|
||||
for (int i = 0; i < product_ID.numEntries; i++)
|
||||
for (int i = 0; i < product_IDs.numEntries; i++)
|
||||
{
|
||||
ESP_LOGI(TAG,
|
||||
"Product ID %d Info: \n\r"
|
||||
|
|
@ -675,8 +1024,8 @@ void BNO08x::print_product_ids()
|
|||
" SW Build Number: 0x%" PRIx32 "\n\r"
|
||||
" SW Version Patch: 0x%" PRIx16 "\n\r"
|
||||
" ---------------------------\n\r",
|
||||
i, product_ID.entry->swPartNumber, product_ID.entry->swVersionMajor, product_ID.entry->swVersionMinor,
|
||||
product_ID.entry->swBuildNumber, product_ID.entry->swVersionPatch);
|
||||
i, product_IDs.entry->swPartNumber, product_IDs.entry->swVersionMajor, product_IDs.entry->swVersionMinor,
|
||||
product_IDs.entry->swBuildNumber, product_IDs.entry->swVersionPatch);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -84,9 +84,9 @@ void BNO08xSH2HAL::hal_cb(void* cookie, sh2_AsyncEvent_t* pEvent)
|
|||
imu->reset_occurred = true;
|
||||
}
|
||||
|
||||
void BNO08xSH2HAL::sensor_report_cb(void* cookie, sh2_SensorEvent_t* event)
|
||||
void BNO08xSH2HAL::sensor_event_cb(void* cookie, sh2_SensorEvent_t* event)
|
||||
{
|
||||
sh2_decodeSensorEvent(imu->sensor_report_val, event);
|
||||
xQueueSend(imu->queue_rx_sensor_event, event, 0);
|
||||
}
|
||||
|
||||
void BNO08xSH2HAL::hardware_reset()
|
||||
|
|
|
|||
Loading…
Reference in New Issue