16 #include "nav2_util/node_utils.hpp"
22 using std::chrono::high_resolution_clock;
25 using std::replace_if;
31 string sanitize_node_name(
const string & potential_node_name)
33 string node_name(potential_node_name);
37 begin(node_name), end(node_name),
38 [](
auto c) {
return !isalnum(c);},
43 string add_namespaces(
const string & top_ns,
const string & sub_ns)
45 if (!top_ns.empty() && top_ns.back() ==
'/') {
46 if (top_ns.front() ==
'/') {
47 return top_ns + sub_ns;
49 return "/" + top_ns + sub_ns;
53 return top_ns +
"/" + sub_ns;
56 std::string time_to_string(
size_t len)
58 string output(len,
'0');
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) {
67 timestring.length() - len, len);
71 len - timestring.length(), timestring.length(),
73 0, timestring.length());
78 std::string generate_internal_node_name(
const std::string & prefix)
80 return sanitize_node_name(prefix) +
"_" + time_to_string(8);
83 rclcpp::Node::SharedPtr generate_internal_node(
const std::string & prefix)
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);
93 void setSoftRealTimePriority()
96 sch.sched_priority = 49;
97 if (sched_setscheduler(0, SCHED_FIFO, &sch) == -1) {
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));