Nav2 Navigation Stack - jazzy  jazzy
ROS 2 Navigation Stack
node_utils.cpp
1 // Copyright (c) 2019 Intel Corporation
2 // Copyright (c) 2023 Open Navigation LLC
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 
16 #include "nav2_util/node_utils.hpp"
17 #include <chrono>
18 #include <string>
19 #include <algorithm>
20 #include <cctype>
21 
22 using std::chrono::high_resolution_clock;
23 using std::to_string;
24 using std::string;
25 using std::replace_if;
26 using std::isalnum;
27 
28 namespace nav2_util
29 {
30 
31 string sanitize_node_name(const string & potential_node_name)
32 {
33  string node_name(potential_node_name);
34  // read this as `replace` characters in `node_name` `if` not alphanumeric.
35  // replace with '_'
36  replace_if(
37  begin(node_name), end(node_name),
38  [](auto c) {return !isalnum(c);},
39  '_');
40  return node_name;
41 }
42 
43 string add_namespaces(const string & top_ns, const string & sub_ns)
44 {
45  if (!top_ns.empty() && top_ns.back() == '/') {
46  if (top_ns.front() == '/') {
47  return top_ns + sub_ns;
48  } else {
49  return "/" + top_ns + sub_ns;
50  }
51  }
52 
53  return top_ns + "/" + sub_ns;
54 }
55 
56 std::string time_to_string(size_t len)
57 {
58  string output(len, '0'); // prefill the string with zeros
59  auto timepoint = high_resolution_clock::now();
60  auto timecount = timepoint.time_since_epoch().count();
61  auto timestring = to_string(timecount);
62  if (timestring.length() >= len) {
63  // if `timestring` is shorter, put it at the end of `output`
64  output.replace(
65  0, len,
66  timestring,
67  timestring.length() - len, len);
68  } else {
69  // if `output` is shorter, just copy in the end of `timestring`
70  output.replace(
71  len - timestring.length(), timestring.length(),
72  timestring,
73  0, timestring.length());
74  }
75  return output;
76 }
77 
78 std::string generate_internal_node_name(const std::string & prefix)
79 {
80  return sanitize_node_name(prefix) + "_" + time_to_string(8);
81 }
82 
83 rclcpp::Node::SharedPtr generate_internal_node(const std::string & prefix)
84 {
85  auto options =
86  rclcpp::NodeOptions()
87  .start_parameter_services(false)
88  .start_parameter_event_publisher(false)
89  .arguments({"--ros-args", "-r", "__node:=" + generate_internal_node_name(prefix), "--"});
90  return rclcpp::Node::make_shared("_", options);
91 }
92 
93 void setSoftRealTimePriority()
94 {
95  sched_param sch;
96  sch.sched_priority = 49;
97  if (sched_setscheduler(0, SCHED_FIFO, &sch) == -1) {
98  std::string errmsg(
99  "Cannot set as real-time thread. Users must set: <username> hard rtprio 99 and "
100  "<username> soft rtprio 99 in /etc/security/limits.conf to enable "
101  "realtime prioritization! Error: ");
102  throw std::runtime_error(errmsg + std::strerror(errno));
103  }
104 }
105 
106 } // namespace nav2_util