Skip to content

Commit baa5b68

Browse files
Modifies the ROS Node map in a map of tuple which includes:
- The ROS Node - A related callback group with reentrant type.
1 parent 7e2a495 commit baa5b68

File tree

2 files changed

+44
-18
lines changed

2 files changed

+44
-18
lines changed

include/dynamic_graph_bridge/ros.hpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,28 @@ typedef rclcpp::Service<std_srvs::srv::Empty>::SharedPtr EmptyServicePtr;
6060
typedef std_srvs::srv::Empty EmptyServiceType;
6161

6262
/**
63-
* @brief rosInit is a global method that uses the structure GlobalRos.
64-
* Its role is to create the ros::nodeHandle and the ros::spinner once at the
65-
* first call. It always returns a reference to the node hanlde.
66-
* @return the reference of GLOBAL_ROS_VAR.node_.
63+
* @brief If the node named node_name does not exist create it with a reentrant
64+
* group
65+
* @param node_name
66+
*/
67+
void create_ros_node(std::string& node_name);
68+
69+
/**
70+
* @brief Get the node shared pointer providing the node name.
71+
* @param node_name
72+
* @return A pointer towards the node.
6773
*/
6874
RosNodePtr get_ros_node(std::string node_name);
6975

76+
/**
77+
* @brief Returns a Reentrant Callback group initialized with the ros node
78+
* specifed by node_name
79+
* @param node_name
80+
* @param Returns a shared pointer towards the described callback group of type
81+
* reentrant.
82+
*/
83+
rclcpp::CallbackGroup::SharedPtr get_callback_group(std::string node_name);
84+
7085
size_t ros_executor_get_nb_threads();
7186

7287
void ros_display_list_of_nodes();

src/ros.cpp

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ namespace dynamic_graph_bridge {
3333
/**
3434
* @brief Shortcut.
3535
*/
36-
typedef std::map<std::string, RosNodePtr> GlobalListOfRosNodeType;
36+
typedef std::tuple<RosNodePtr, rclcpp::CallbackGroup::SharedPtr> NodeInfo;
37+
typedef std::map<std::string, NodeInfo> GlobalListOfRosNodeType;
3738

3839
/**
3940
* @brief GLOBAL_LIST_OF_ROS_NODE is global variable that acts as a singleton on
@@ -57,7 +58,7 @@ static GlobalListOfRosNodeType GLOBAL_LIST_OF_ROS_NODE;
5758
*/
5859
class Executor {
5960
public:
60-
Executor() : ros_executor_(rclcpp::ExecutorOptions(), 4) {
61+
Executor() : ros_executor_(rclcpp::ExecutorOptions(), 30) {
6162
is_thread_running_ = false;
6263
is_spinning_ = false;
6364
list_node_added_.clear();
@@ -234,7 +235,7 @@ bool ros_node_exists(std::string node_name) {
234235
GLOBAL_LIST_OF_ROS_NODE.end()) {
235236
return false;
236237
}
237-
if (GLOBAL_LIST_OF_ROS_NODE.at(node_name) == nullptr) {
238+
if (std::get<0>(GLOBAL_LIST_OF_ROS_NODE.at(node_name)) == nullptr) {
238239
return false;
239240
}
240241
return true;
@@ -248,19 +249,29 @@ ExecutorPtr get_ros_executor() {
248249
return EXECUTOR;
249250
}
250251

251-
RosNodePtr get_ros_node(std::string node_name) {
252-
ros_init();
252+
void create_ros_node(std::string& node_name) {
253253
if (!ros_node_exists(node_name)) {
254-
GLOBAL_LIST_OF_ROS_NODE[node_name] = RosNodePtr(nullptr);
255-
}
256-
if (!GLOBAL_LIST_OF_ROS_NODE[node_name] ||
257-
GLOBAL_LIST_OF_ROS_NODE[node_name].get() == nullptr) {
258-
/** RosNode instanciation */
259-
GLOBAL_LIST_OF_ROS_NODE[node_name] =
254+
RosNodePtr a_ros_node_ptr =
260255
std::make_shared<RosNode>(node_name, "dynamic_graph_bridge");
256+
rclcpp::CallbackGroup::SharedPtr a_cb_group =
257+
a_ros_node_ptr->create_callback_group(
258+
rclcpp::CallbackGroupType::Reentrant);
259+
/** RosNode instanciation */
260+
GLOBAL_LIST_OF_ROS_NODE[node_name] = NodeInfo(a_ros_node_ptr, a_cb_group);
261261
}
262+
}
263+
RosNodePtr get_ros_node(std::string node_name) {
264+
ros_init();
265+
create_ros_node(node_name);
266+
/** Return a reference to the node handle so any function can use it */
267+
return std::get<0>(GLOBAL_LIST_OF_ROS_NODE[node_name]);
268+
}
269+
270+
rclcpp::CallbackGroup::SharedPtr get_callback_group(std::string node_name) {
271+
ros_init();
272+
create_ros_node(node_name);
262273
/** Return a reference to the node handle so any function can use it */
263-
return GLOBAL_LIST_OF_ROS_NODE[node_name];
274+
return std::get<1>(GLOBAL_LIST_OF_ROS_NODE[node_name]);
264275
}
265276

266277
void ros_add_node_to_executor(const std::string& node_name) {
@@ -290,8 +301,8 @@ void ros_display_list_of_nodes() {
290301
while (ros_node_it != GLOBAL_LIST_OF_ROS_NODE.end()) {
291302
RCLCPP_INFO(rclcpp::get_logger("dynamic_graph_bridge"),
292303
"ros_display_list_of_nodes: %s/%s",
293-
ros_node_it->second->get_namespace(),
294-
ros_node_it->second->get_name());
304+
std::get<0>(ros_node_it->second)->get_namespace(),
305+
std::get<0>(ros_node_it->second)->get_name());
295306
ros_node_it++;
296307
}
297308
}

0 commit comments

Comments
 (0)