Skip to content

Commit 4d8c6ee

Browse files
saikishormergify[bot]
authored andcommitted
Fix the CPU affinity of the ros2_control_node (#2509)
(cherry picked from commit 94e610a) # Conflicts: # controller_manager/src/ros2_control_node.cpp
1 parent 5c36741 commit 4d8c6ee

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

controller_manager/src/ros2_control_node.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,13 @@ int main(int argc, char ** argv)
6767
}
6868
}
6969

70+
<<<<<<< HEAD
71+
=======
72+
// wait for the clock to be available
73+
cm->get_clock()->wait_until_started();
74+
cm->get_clock()->sleep_for(rclcpp::Duration::from_seconds(1.0 / cm->get_update_rate()));
75+
76+
>>>>>>> 94e610a (Fix the CPU affinity of the ros2_control_node (#2509))
7077
RCLCPP_INFO(cm->get_logger(), "update rate is %d Hz", cm->get_update_rate());
7178
const int thread_priority = cm->get_parameter_or<int>("thread_priority", kSchedPriority);
7279
RCLCPP_INFO(
@@ -76,6 +83,30 @@ int main(int argc, char ** argv)
7683
std::thread cm_thread(
7784
[cm, thread_priority, use_sim_time]()
7885
{
86+
rclcpp::Parameter cpu_affinity_param;
87+
if (cm->get_parameter("cpu_affinity", cpu_affinity_param))
88+
{
89+
std::vector<int> cpus = {};
90+
if (cpu_affinity_param.get_type() == rclcpp::ParameterType::PARAMETER_INTEGER)
91+
{
92+
cpus = {static_cast<int>(cpu_affinity_param.as_int())};
93+
}
94+
else if (cpu_affinity_param.get_type() == rclcpp::ParameterType::PARAMETER_INTEGER_ARRAY)
95+
{
96+
const auto cpu_affinity_param_array = cpu_affinity_param.as_integer_array();
97+
std::for_each(
98+
cpu_affinity_param_array.begin(), cpu_affinity_param_array.end(),
99+
[&cpus](int cpu) { cpus.push_back(static_cast<int>(cpu)); });
100+
}
101+
const auto affinity_result = realtime_tools::set_current_thread_affinity(cpus);
102+
if (!affinity_result.first)
103+
{
104+
RCLCPP_WARN(
105+
cm->get_logger(), "Unable to set the CPU affinity : '%s'",
106+
affinity_result.second.c_str());
107+
}
108+
}
109+
79110
if (!realtime_tools::configure_sched_fifo(thread_priority))
80111
{
81112
RCLCPP_WARN(

0 commit comments

Comments
 (0)