|
| 1 | +// Copyright 2024 Robotec.AI |
| 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 <graph/NodesRos2.hpp> |
| 16 | +#include <scene/Scene.hpp> |
| 17 | +#include <RGLFields.hpp> |
| 18 | + |
| 19 | +void Ros2PublishLaserScanNode::setParameters(const char* topicName, const char* frameId, |
| 20 | + rgl_qos_policy_reliability_t qosReliability, |
| 21 | + rgl_qos_policy_durability_t qosDurability, rgl_qos_policy_history_t qosHistory, |
| 22 | + int32_t qosHistoryDepth) |
| 23 | +{ |
| 24 | + ros2Message.header.frame_id = frameId; |
| 25 | + auto qos = rclcpp::QoS(qosHistoryDepth); |
| 26 | + qos.reliability(static_cast<rmw_qos_reliability_policy_t>(qosReliability)); |
| 27 | + qos.durability(static_cast<rmw_qos_durability_policy_t>(qosDurability)); |
| 28 | + qos.history(static_cast<rmw_qos_history_policy_t>(qosHistory)); |
| 29 | + ros2Publisher = ros2InitGuard->createUniquePublisher<sensor_msgs::msg::LaserScan>(topicName, qos); |
| 30 | +} |
| 31 | + |
| 32 | +void Ros2PublishLaserScanNode::ros2ValidateImpl() |
| 33 | +{ |
| 34 | + if (input->getHeight() != 1) { |
| 35 | + throw InvalidPipeline("ROS2 publish supports unorganized pointclouds only"); |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +void Ros2PublishLaserScanNode::ros2EnqueueExecImpl() |
| 40 | +{ |
| 41 | + const auto currentTime = Scene::instance().getTime(); |
| 42 | + ros2Message.header.stamp = currentTime.has_value() ? |
| 43 | + currentTime.value().asRos2Msg() : |
| 44 | + static_cast<builtin_interfaces::msg::Time>(ros2InitGuard->getNode().get_clock()->now()); |
| 45 | + |
| 46 | + const size_t pointCount = input->getPointCount(); |
| 47 | + |
| 48 | + angles->copyFrom(input->getFieldDataTyped<AZIMUTH_F32>()); |
| 49 | + auto minAng = angles->at(0); |
| 50 | + auto maxAng = minAng; |
| 51 | + for (size_t i = 1; i < pointCount; ++i) { |
| 52 | + if (angles->at(i) < minAng) { |
| 53 | + minAng = angles->at(i); |
| 54 | + } |
| 55 | + if (angles->at(i) > maxAng) { |
| 56 | + maxAng = angles->at(i); |
| 57 | + } |
| 58 | + } |
| 59 | + ros2Message.angle_min = minAng; |
| 60 | + ros2Message.angle_max = maxAng; |
| 61 | + ros2Message.angle_increment = angles->at(1) - angles->at(0); |
| 62 | + |
| 63 | + times->copyFrom(input->getFieldDataTyped<TIME_STAMP_F64>()); |
| 64 | + ros2Message.time_increment = static_cast<float>(times->at(1) - times->at(0)); |
| 65 | + ros2Message.scan_time = static_cast<float>(times->at(pointCount - 1) - times->at(0)); |
| 66 | + |
| 67 | + ranges->copyFrom(input->getFieldDataTyped<DISTANCE_F32>()); |
| 68 | + ros2Message.ranges.resize(input->getPointCount()); |
| 69 | + size_t size = pointCount * Field<DISTANCE_F32>::size; |
| 70 | + CHECK_CUDA(cudaMemcpyAsync(ros2Message.ranges.data(), ranges->getRawReadPtr(), size, cudaMemcpyDefault, getStreamHandle())); |
| 71 | + CHECK_CUDA(cudaStreamSynchronize(getStreamHandle())); |
| 72 | + |
| 73 | + auto minRange = ranges->at(0); |
| 74 | + auto maxRange = minRange; |
| 75 | + for (size_t i = 1; i < pointCount; ++i) { |
| 76 | + if (ranges->at(i) < minRange) { |
| 77 | + minRange = ranges->at(i); |
| 78 | + } |
| 79 | + if (ranges->at(i) > maxRange) { |
| 80 | + maxRange = ranges->at(i); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + intensities->copyFrom(input->getFieldDataTyped<INTENSITY_F32>()); |
| 85 | + ros2Message.intensities.resize(pointCount); |
| 86 | + size = pointCount * Field<INTENSITY_F32>::size; |
| 87 | + CHECK_CUDA(cudaMemcpyAsync(ros2Message.intensities.data(), intensities->getRawReadPtr(), size, cudaMemcpyDefault, |
| 88 | + getStreamHandle())); |
| 89 | + CHECK_CUDA(cudaStreamSynchronize(getStreamHandle())); |
| 90 | + |
| 91 | + ros2Publisher->publish(ros2Message); |
| 92 | +} |
0 commit comments