|
| 1 | +// Copyright 2025 Gezp (https://github.com/gezp). |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "ssct_common/dummy_sensor/dummy_imu_node.hpp" |
| 16 | + |
| 17 | +#include <fstream> |
| 18 | +#include <sstream> |
| 19 | + |
| 20 | +namespace ssct_common |
| 21 | +{ |
| 22 | + |
| 23 | +DummyImuNode::DummyImuNode(const rclcpp::NodeOptions & options) |
| 24 | +{ |
| 25 | + node_ = std::make_shared<rclcpp::Node>("dummy_imu_node", options); |
| 26 | + node_->declare_parameter("frame_id", frame_id_); |
| 27 | + node_->declare_parameter("data_file", data_file_); |
| 28 | + node_->declare_parameter("rate", rate_); |
| 29 | + node_->declare_parameter("timestamp", timestamp_); |
| 30 | + node_->declare_parameter("loop", loop_); |
| 31 | + |
| 32 | + node_->get_parameter("frame_id", frame_id_); |
| 33 | + node_->get_parameter("data_file", data_file_); |
| 34 | + node_->get_parameter("rate", rate_); |
| 35 | + node_->get_parameter("timestamp", timestamp_); |
| 36 | + node_->get_parameter("loop", loop_); |
| 37 | + // read data |
| 38 | + if (data_file_.empty()) { |
| 39 | + RCLCPP_FATAL(node_->get_logger(), "data_file is empty."); |
| 40 | + return; |
| 41 | + } |
| 42 | + if (!read_data()) { |
| 43 | + RCLCPP_FATAL(node_->get_logger(), "no imus."); |
| 44 | + return; |
| 45 | + } |
| 46 | + // publisher |
| 47 | + std::string topic_name = "/sensor/" + frame_id_ + "/imu"; |
| 48 | + imu_pub_ = std::make_shared<ImuPublisher>(node_, topic_name, 100, frame_id_); |
| 49 | + // create timer |
| 50 | + auto period_ms = std::chrono::milliseconds(static_cast<int64_t>(1000.0 / rate_)); |
| 51 | + auto timer_callback = [this]() { |
| 52 | + if (loop_ && current_idx_ == imus_.size()) { |
| 53 | + current_idx_ = 0; |
| 54 | + } |
| 55 | + if (current_idx_ < imus_.size()) { |
| 56 | + imu_pub_->publish(imus_[current_idx_]); |
| 57 | + current_idx_++; |
| 58 | + } |
| 59 | + }; |
| 60 | + timer_ = node_->create_wall_timer(period_ms, timer_callback); |
| 61 | +} |
| 62 | + |
| 63 | +bool DummyImuNode::read_data() |
| 64 | +{ |
| 65 | + std::ifstream ifs(data_file_); |
| 66 | + if (!ifs.is_open()) { |
| 67 | + RCLCPP_FATAL(node_->get_logger(), "failed to open file %s.", data_file_.c_str()); |
| 68 | + return false; |
| 69 | + } |
| 70 | + std::string line; |
| 71 | + while (std::getline(ifs, line)) { |
| 72 | + if (line.empty() || !std::isdigit(line[0])) { |
| 73 | + // skip head line and empty line |
| 74 | + continue; |
| 75 | + } |
| 76 | + std::istringstream ss(line); |
| 77 | + std::string token; |
| 78 | + ImuData imu; |
| 79 | + try { |
| 80 | + std::getline(ss, token, ','); |
| 81 | + imu.time = timestamp_ + std::stod(token); |
| 82 | + std::getline(ss, token, ','); |
| 83 | + imu.angular_velocity.x() = std::stod(token); |
| 84 | + std::getline(ss, token, ','); |
| 85 | + imu.angular_velocity.y() = std::stod(token); |
| 86 | + std::getline(ss, token, ','); |
| 87 | + imu.angular_velocity.z() = std::stod(token); |
| 88 | + std::getline(ss, token, ','); |
| 89 | + imu.linear_acceleration.x() = std::stod(token); |
| 90 | + std::getline(ss, token, ','); |
| 91 | + imu.linear_acceleration.y() = std::stod(token); |
| 92 | + std::getline(ss, token, ','); |
| 93 | + imu.linear_acceleration.z() = std::stod(token); |
| 94 | + imus_.push_back(imu); |
| 95 | + } catch (const std::exception & e) { |
| 96 | + RCLCPP_FATAL( |
| 97 | + node_->get_logger(), "failed parse line: [%s], error: %s", line.c_str(), e.what()); |
| 98 | + return false; |
| 99 | + } |
| 100 | + } |
| 101 | + ifs.close(); |
| 102 | + return !imus_.empty(); |
| 103 | +} |
| 104 | + |
| 105 | +} // namespace ssct_common |
0 commit comments