|
|
|
@ -15,6 +15,11 @@ static constexpr float MPU_MAG_TO_FLUX = (4912.f) / (32760.f);
@@ -15,6 +15,11 @@ static constexpr float MPU_MAG_TO_FLUX = (4912.f) / (32760.f);
|
|
|
|
|
|
|
|
|
|
static const char *TAG = "ugv_io_mpu"; |
|
|
|
|
|
|
|
|
|
Vec3f::Vec3f() : x(0), y(0), z(0) {} |
|
|
|
|
Vec3f::Vec3f(float x, float y, float z) : x(x), y(y), z(z) {} |
|
|
|
|
Vec3f::Vec3f(const mpud::float_axes_t &axes) |
|
|
|
|
: x(axes.x), y(axes.y), z(axes.z) {} |
|
|
|
|
|
|
|
|
|
MPU::MPU() { mpu_ = new mpud::MPU(); } |
|
|
|
|
|
|
|
|
|
MPU::~MPU() { delete mpu_; } |
|
|
|
@ -33,20 +38,46 @@ void MPU::Init() {
@@ -33,20 +38,46 @@ void MPU::Init() {
|
|
|
|
|
} |
|
|
|
|
mpu_->setAccelFullScale(MPU_ACCEL_FS); |
|
|
|
|
mpu_->setGyroFullScale(MPU_GYRO_FS); |
|
|
|
|
ESP_LOGI(TAG, "MPU initialized"); |
|
|
|
|
|
|
|
|
|
mutex_ = xSemaphoreCreateMutex(); |
|
|
|
|
mpu_data_ = MpuData{}; |
|
|
|
|
|
|
|
|
|
BaseType_t xRet = |
|
|
|
|
xTaskCreate(MPU::MPU_Task, "ugv_io_mpu", 2 * 1024, this, 3, &this->task_); |
|
|
|
|
if (xRet != pdTRUE) { |
|
|
|
|
ESP_LOGE(TAG, "error creating MPU task"); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
ESP_LOGI(TAG, "MPU initialized and task started"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void MPU::GetData(MpuData &data) { |
|
|
|
|
esp_err_t ret = mpu_->motion(&accel_, &gyro_, &mag_); |
|
|
|
|
if (ret != ESP_OK) { |
|
|
|
|
ESP_LOGE(TAG, "error reading MPU"); |
|
|
|
|
xSemaphoreTake(mutex_, pdMS_TO_TICKS(1000)); |
|
|
|
|
data = MpuData(mpu_data_); |
|
|
|
|
xSemaphoreGive(mutex_); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void MPU::DoTask() { |
|
|
|
|
esp_err_t ret; |
|
|
|
|
while (true) { |
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(50)); |
|
|
|
|
ret = mpu_->motion(&accel_, &gyro_, &mag_); |
|
|
|
|
if (ret != ESP_OK) { |
|
|
|
|
ESP_LOGE(TAG, "error reading MPU"); |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
xSemaphoreTake(mutex_, pdMS_TO_TICKS(1000)); |
|
|
|
|
mpu_data_.accel = mpud::accelGravity(accel_, MPU_ACCEL_FS); |
|
|
|
|
mpu_data_.gyro_rate = mpud::gyroDegPerSec(gyro_, MPU_GYRO_FS); |
|
|
|
|
mpu_data_.mag.x = ((float)mag_.x) * MPU_MAG_TO_FLUX; |
|
|
|
|
mpu_data_.mag.y = ((float)mag_.y) * MPU_MAG_TO_FLUX; |
|
|
|
|
mpu_data_.mag.z = ((float)mag_.z) * MPU_MAG_TO_FLUX; |
|
|
|
|
xSemaphoreGive(mutex_); |
|
|
|
|
} |
|
|
|
|
data.accel = mpud::accelGravity(accel_, MPU_ACCEL_FS); |
|
|
|
|
data.gyro_rate = mpud::gyroDegPerSec(gyro_, MPU_GYRO_FS); |
|
|
|
|
data.mag.x = ((float)mag_.x) * MPU_MAG_TO_FLUX; |
|
|
|
|
data.mag.y = ((float)mag_.y) * MPU_MAG_TO_FLUX; |
|
|
|
|
data.mag.z = ((float)mag_.z) * MPU_MAG_TO_FLUX; |
|
|
|
|
vTaskDelete(NULL); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void MPU::MPU_Task(void *arg) { ((MPU *)arg)->DoTask(); } |
|
|
|
|
|
|
|
|
|
}; // namespace io
|
|
|
|
|
}; // namespace ugv
|
|
|
|
|