45 int error = 0, thread_priority;
47 error += !nh_p.getParam(
"loop_frequency", loop_hz_);
48 error += !nh_p.getParam(
"cycle_time_error_threshold", cycle_time_error_threshold_);
49 error += !nh_p.getParam(
"thread_priority", thread_priority);
52 char error_message[] =
"could not retrieve one of the required parameters\n\trm_hw/loop_hz or "
53 "rm_hw/cycle_time_error_threshold or "
54 "rm_hw/thread_priority";
56 throw std::runtime_error(error_message);
62 hardware_interface_->setCanBusThreadPriority(thread_priority);
63 hardware_interface_->init(nh, nh_p);
68 last_time_ = clock::now();
71 loop_thread_ = std::thread([&]() {
78 sched_param sched{ .sched_priority = thread_priority };
79 if (pthread_setschedparam(loop_thread_.native_handle(), SCHED_FIFO, &sched) != 0)
80 ROS_WARN(
"Failed to set threads priority (one possible reason could be that the user and the group permissions "
81 "are not set properly.).\n");
86 const auto current_time = clock::now();
88 const duration<double> desired_duration(1.0 /
loop_hz_);
90 duration<double> time_span = duration_cast<duration<double>>(current_time -
last_time_);
114 const auto sleep_till = current_time + duration_cast<clock::duration>(desired_duration);
115 std::this_thread::sleep_until(sleep_till);