forked from gazebosim/ros_gz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bridge_handle_gz_to_ros.cpp
90 lines (75 loc) · 2.34 KB
/
bridge_handle_gz_to_ros.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// Copyright 2022 Open Source Robotics Foundation, Inc.
//
// 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.
//
#include <memory>
#include "bridge_handle_gz_to_ros.hpp"
namespace ros_gz_bridge
{
BridgeHandleGzToRos::~BridgeHandleGzToRos() = default;
size_t BridgeHandleGzToRos::NumSubscriptions() const
{
// Return number of ROS subscriptions
size_t valid_subscriptions = 0;
if (this->ros_publisher_ != nullptr) {
// Use info_by_topic rather than get_subscription_count
// to filter out potential bidirectional bridge
auto topic_info = this->ros_node_->get_subscriptions_info_by_topic(
this->config_.ros_topic_name);
for (auto & topic : topic_info) {
if (topic.node_name() == this->ros_node_->get_name()) {
continue;
}
valid_subscriptions++;
}
}
return valid_subscriptions;
}
bool BridgeHandleGzToRos::HasPublisher() const
{
return this->ros_publisher_ != nullptr;
}
void BridgeHandleGzToRos::StartPublisher()
{
// Start ROS publisher
this->ros_publisher_ = this->factory_->create_ros_publisher(
this->ros_node_,
this->config_.ros_topic_name,
this->config_.publisher_queue_size);
}
bool BridgeHandleGzToRos::HasSubscriber() const
{
// Return Gazebo subscriber status
return this->gz_subscriber_ != nullptr;
}
void BridgeHandleGzToRos::StartSubscriber()
{
// Start Gazebo subscriber
this->factory_->create_gz_subscriber(
this->gz_node_,
this->config_.gz_topic_name,
this->config_.subscriber_queue_size,
this->ros_publisher_,
this->ros_node_->get_clock()->ros_time_is_active());
this->gz_subscriber_ = this->gz_node_;
}
void BridgeHandleGzToRos::StopSubscriber()
{
// Stop Gazebo subscriber
if (!this->gz_subscriber_) {
return;
}
this->gz_subscriber_->Unsubscribe(this->config_.gz_topic_name);
this->gz_subscriber_.reset();
}
} // namespace ros_gz_bridge