2018-05-10 09:46:34 +03:00
|
|
|
// Copyright 2018 Slightech Co., Ltd. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2018-04-12 06:48:09 +03:00
|
|
|
#include "internal/motions.h"
|
|
|
|
|
|
|
|
#include <glog/logging.h>
|
|
|
|
|
|
|
|
#include "internal/channels.h"
|
|
|
|
|
|
|
|
MYNTEYE_BEGIN_NAMESPACE
|
|
|
|
|
|
|
|
Motions::Motions(std::shared_ptr<Channels> channels)
|
|
|
|
: channels_(channels),
|
|
|
|
motion_callback_(nullptr),
|
|
|
|
motion_datas_enabled_(false),
|
|
|
|
is_imu_tracking(false) {
|
|
|
|
CHECK_NOTNULL(channels_);
|
|
|
|
VLOG(2) << __func__;
|
|
|
|
}
|
|
|
|
|
|
|
|
Motions::~Motions() {
|
|
|
|
VLOG(2) << __func__;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Motions::SetMotionCallback(motion_callback_t callback) {
|
|
|
|
motion_callback_ = callback;
|
2018-06-07 06:19:58 +03:00
|
|
|
if (motion_callback_) {
|
2018-09-29 11:36:14 +03:00
|
|
|
accel_range = channels_->GetControlValue(Option::ACCELEROMETER_RANGE);
|
|
|
|
gyro_range = channels_->GetControlValue(Option::GYROSCOPE_RANGE);
|
2018-06-07 06:19:58 +03:00
|
|
|
channels_->SetImuCallback([this](const ImuPacket &packet) {
|
2018-04-12 06:48:09 +03:00
|
|
|
if (!motion_callback_ && !motion_datas_enabled_) {
|
|
|
|
LOG(WARNING) << "";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (auto &&seg : packet.segments) {
|
|
|
|
auto &&imu = std::make_shared<ImuData>();
|
2018-07-21 10:34:07 +03:00
|
|
|
// imu->frame_id = seg.frame_id;
|
2018-04-12 15:21:40 +03:00
|
|
|
// if (seg.offset < 0 &&
|
|
|
|
// static_cast<uint32_t>(-seg.offset) > packet.timestamp) {
|
|
|
|
// LOG(WARNING) << "Imu timestamp offset is incorrect";
|
|
|
|
// }
|
2018-08-03 14:31:36 +03:00
|
|
|
imu->serial_number = seg.serial_number;
|
2018-07-21 10:34:07 +03:00
|
|
|
imu->timestamp = seg.timestamp;
|
|
|
|
imu->flag = seg.flag;
|
2018-04-12 06:48:09 +03:00
|
|
|
imu->temperature = seg.temperature / 326.8f + 25;
|
2018-04-12 07:22:55 +03:00
|
|
|
|
2018-08-03 14:31:36 +03:00
|
|
|
if (imu->flag == 1) {
|
2018-09-26 05:48:57 +03:00
|
|
|
imu->accel[0] = seg.accel_or_gyro[0] * 1.f * accel_range / 0x10000;
|
|
|
|
imu->accel[1] = seg.accel_or_gyro[1] * 1.f * accel_range / 0x10000;
|
|
|
|
imu->accel[2] = seg.accel_or_gyro[2] * 1.f * accel_range / 0x10000;
|
2018-07-21 12:09:47 +03:00
|
|
|
imu->gyro[0] = 0;
|
|
|
|
imu->gyro[1] = 0;
|
|
|
|
imu->gyro[2] = 0;
|
2018-08-03 14:31:36 +03:00
|
|
|
} else if (imu->flag == 2) {
|
2018-07-21 12:09:47 +03:00
|
|
|
imu->accel[0] = 0;
|
|
|
|
imu->accel[1] = 0;
|
|
|
|
imu->accel[2] = 0;
|
2018-09-26 05:48:57 +03:00
|
|
|
imu->gyro[0] = seg.accel_or_gyro[0] * 1.f * gyro_range / 0x10000;
|
|
|
|
imu->gyro[1] = seg.accel_or_gyro[1] * 1.f * gyro_range / 0x10000;
|
|
|
|
imu->gyro[2] = seg.accel_or_gyro[2] * 1.f * gyro_range / 0x10000;
|
2018-07-21 12:09:47 +03:00
|
|
|
} else {
|
|
|
|
imu->Reset();
|
2018-07-21 10:34:07 +03:00
|
|
|
}
|
2018-08-03 14:31:36 +03:00
|
|
|
|
2018-04-12 07:22:55 +03:00
|
|
|
std::lock_guard<std::mutex> _(mtx_datas_);
|
|
|
|
motion_data_t data = {imu};
|
2018-11-08 09:31:15 +02:00
|
|
|
if (motion_datas_enabled_) {
|
|
|
|
motion_datas_.push_back(data);
|
|
|
|
}
|
2018-06-07 06:19:58 +03:00
|
|
|
|
|
|
|
motion_callback_(data);
|
2018-04-12 06:48:09 +03:00
|
|
|
}
|
|
|
|
});
|
2018-06-07 06:19:58 +03:00
|
|
|
} else {
|
|
|
|
channels_->SetImuCallback(nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Motions::DoMotionTrack() {
|
|
|
|
channels_->DoImuTrack();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Motions::StartMotionTracking() {
|
|
|
|
if (!is_imu_tracking) {
|
|
|
|
channels_->StartImuTracking();
|
2018-04-12 06:48:09 +03:00
|
|
|
is_imu_tracking = true;
|
|
|
|
} else {
|
|
|
|
LOG(WARNING) << "Imu is tracking already";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Motions::StopMotionTracking() {
|
|
|
|
if (is_imu_tracking) {
|
|
|
|
channels_->StopImuTracking();
|
|
|
|
is_imu_tracking = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Motions::EnableMotionDatas(std::size_t max_size) {
|
|
|
|
if (max_size <= 0) {
|
|
|
|
LOG(WARNING) << "Could not enable motion datas with max_size <= 0";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
motion_datas_enabled_ = true;
|
|
|
|
motion_datas_max_size = max_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
Motions::motion_datas_t Motions::GetMotionDatas() {
|
|
|
|
if (!motion_datas_enabled_) {
|
|
|
|
LOG(FATAL) << "Must enable motion datas before getting them, or you set "
|
|
|
|
"motion callback instead";
|
|
|
|
}
|
2018-04-12 07:22:55 +03:00
|
|
|
std::lock_guard<std::mutex> _(mtx_datas_);
|
|
|
|
motion_datas_t datas = motion_datas_;
|
|
|
|
motion_datas_.clear();
|
|
|
|
return datas;
|
2018-04-12 06:48:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
MYNTEYE_END_NAMESPACE
|