|
| 1 | +/* Modified from https://github.com/facebookresearch/vrs |
| 2 | + * and https://github.com/facebookresearch/projectaria_tools |
| 3 | + * |
| 4 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +#include <array> |
| 20 | +#include <memory> |
| 21 | +#include <sstream> |
| 22 | + |
| 23 | +#include <vrs/DataLayout.h> |
| 24 | +#include <vrs/utils/PixelFrame.h> |
| 25 | + |
| 26 | +#include "IMUPlayer.h" |
| 27 | +#include "utils.h" |
| 28 | + |
| 29 | +namespace rerun_vrs { |
| 30 | + |
| 31 | + struct IMUConfigurationDataLayout : public vrs::AutoDataLayout { |
| 32 | + vrs::DataPieceValue<vrs::Bool> hasAccelerometer_{"has_accelerometer"}; |
| 33 | + vrs::DataPieceValue<vrs::Bool> hasGyroscope_{"has_gyroscope"}; |
| 34 | + vrs::DataPieceValue<vrs::Bool> hasMagnetometer_{"has_magnetometer"}; |
| 35 | + |
| 36 | + vrs::AutoDataLayoutEnd endLayout; |
| 37 | + }; |
| 38 | + |
| 39 | + struct IMUDataDataLayout : public vrs::AutoDataLayout { |
| 40 | + vrs::DataPieceArray<float> accelMSec2{"accelerometer", 3}; |
| 41 | + vrs::DataPieceArray<float> gyroRadSec{"gyroscope", 3}; |
| 42 | + vrs::DataPieceArray<float> magTesla{"magnetometer", 3}; |
| 43 | + |
| 44 | + vrs::AutoDataLayoutEnd endLayout; |
| 45 | + }; |
| 46 | + |
| 47 | + IMUPlayer::IMUPlayer(vrs::StreamId id, std::shared_ptr<rerun::RecordingStream> rec) |
| 48 | + : id_{id}, rec_{rec}, entityPath_{add_quotes(id.getName())} {} |
| 49 | + |
| 50 | + bool IMUPlayer::onDataLayoutRead( |
| 51 | + const vrs::CurrentRecord& record, size_t blockIndex, vrs::DataLayout& layout |
| 52 | + ) { |
| 53 | + if (!enabled_) |
| 54 | + return false; |
| 55 | + |
| 56 | + std::ostringstream buffer; |
| 57 | + layout.printLayoutCompact(buffer); |
| 58 | + const auto& layout_str = buffer.str(); |
| 59 | + |
| 60 | + rec_->set_time_seconds("timestamp", record.timestamp); |
| 61 | + |
| 62 | + if (record.recordType == vrs::Record::Type::CONFIGURATION) { |
| 63 | + // NOTE this is meta data from the sensor that doesn't change over time and only comes |
| 64 | + // in once in the beginning |
| 65 | + rec_->log_timeless( |
| 66 | + (entityPath_ + "/configuration").c_str(), |
| 67 | + rerun::TextDocument(layout_str) |
| 68 | + ); |
| 69 | + |
| 70 | + // read properties required for logging |
| 71 | + auto& config = getExpectedLayout<IMUConfigurationDataLayout>(layout, blockIndex); |
| 72 | + hasAccelerometer_ = config.hasAccelerometer_.get(); |
| 73 | + hasGyroscope_ = config.hasGyroscope_.get(); |
| 74 | + hasMagnetometer_ = config.hasMagnetometer_.get(); |
| 75 | + } |
| 76 | + |
| 77 | + if (record.recordType == vrs::Record::Type::DATA) { |
| 78 | + auto& data = getExpectedLayout<IMUDataDataLayout>(layout, blockIndex); |
| 79 | + |
| 80 | + if (hasAccelerometer_) { |
| 81 | + std::array<float, 3> accelMSec2; |
| 82 | + if (data.accelMSec2.get(accelMSec2.data(), accelMSec2.size())) |
| 83 | + logAccelerometer(accelMSec2); |
| 84 | + } |
| 85 | + if (hasGyroscope_) { |
| 86 | + std::array<float, 3> gyroRadSec; |
| 87 | + if (data.gyroRadSec.get(gyroRadSec.data(), gyroRadSec.size())) |
| 88 | + logGyroscope(gyroRadSec); |
| 89 | + } |
| 90 | + if (hasMagnetometer_) { |
| 91 | + std::array<float, 3> magTesla; |
| 92 | + if (data.magTesla.get(magTesla.data(), magTesla.size())) |
| 93 | + logMagnetometer(magTesla); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + return false; |
| 98 | + } |
| 99 | + |
| 100 | + void IMUPlayer::logAccelerometer(const std::array<float, 3>& accelMSec2) { |
| 101 | + rec_->log( |
| 102 | + (entityPath_ + "/accelerometer").c_str(), |
| 103 | + rerun::Arrows3D::from_vectors({rerun::datatypes::Vec3D(accelMSec2)}) |
| 104 | + ); |
| 105 | + rec_->log( |
| 106 | + (entityPath_ + "/accelerometer/x").c_str(), |
| 107 | + rerun::TimeSeriesScalar(accelMSec2[0]) |
| 108 | + ); |
| 109 | + rec_->log( |
| 110 | + (entityPath_ + "/accelerometer/y").c_str(), |
| 111 | + rerun::TimeSeriesScalar(accelMSec2[1]) |
| 112 | + ); |
| 113 | + rec_->log( |
| 114 | + (entityPath_ + "/accelerometer/z").c_str(), |
| 115 | + rerun::TimeSeriesScalar(accelMSec2[2]) |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + void IMUPlayer::logGyroscope(const std::array<float, 3>& gyroRadSec) { |
| 120 | + rec_->log((entityPath_ + "/gyroscope/x").c_str(), rerun::TimeSeriesScalar(gyroRadSec[0])); |
| 121 | + rec_->log((entityPath_ + "/gyroscope/y").c_str(), rerun::TimeSeriesScalar(gyroRadSec[1])); |
| 122 | + rec_->log((entityPath_ + "/gyroscope/z").c_str(), rerun::TimeSeriesScalar(gyroRadSec[2])); |
| 123 | + } |
| 124 | + |
| 125 | + void IMUPlayer::logMagnetometer(const std::array<float, 3>& magTesla) { |
| 126 | + rec_->log((entityPath_ + "/magnetometer/x").c_str(), rerun::TimeSeriesScalar(magTesla[0])); |
| 127 | + rec_->log((entityPath_ + "/magnetometer/y").c_str(), rerun::TimeSeriesScalar(magTesla[1])); |
| 128 | + rec_->log((entityPath_ + "/magnetometer/z").c_str(), rerun::TimeSeriesScalar(magTesla[2])); |
| 129 | + } |
| 130 | + |
| 131 | + bool mightContainIMUData(const vrs::StreamId& id) { |
| 132 | + return id.getTypeId() == vrs::RecordableTypeId::SlamImuData || |
| 133 | + id.getTypeId() == vrs::RecordableTypeId::SlamMagnetometerData || |
| 134 | + id.getTypeId() == vrs::RecordableTypeId::ImuRecordableClass; |
| 135 | + } |
| 136 | +} // namespace rerun_vrs |
0 commit comments