helpers.cpp
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 
3 // -- BEGIN LICENSE BLOCK ----------------------------------------------
4 // Copyright 2022 FZI Forschungszentrum Informatik
5 // Created on behalf of Universal Robots A/S
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 // -- END LICENSE BLOCK ------------------------------------------------
19 
20 //----------------------------------------------------------------------
27 //----------------------------------------------------------------------
28 
31 #include <ur_client_library/log.h>
32 
33 #include <cstring>
34 #include <fstream>
35 #include <iostream>
36 #include <thread>
37 
38 // clang-format off
39 // We want to keep the URL in one line to avoid formatting issues. This will make it easier to
40 // extract the URL for an automatic check.
41 const std::string RT_DOC_URL = "https://docs.universal-robots.com/Universal_Robots_ROS_Documentation/doc/ur_client_library/doc/real_time.html";
42 // clang-format on
43 
44 namespace urcl
45 {
46 bool setFiFoScheduling(pthread_t& thread, const int priority)
47 {
48 #ifdef _WIN32
49  return ::SetThreadPriority(thread, priority);
50 #else // _WIN32
51  struct sched_param params;
52  params.sched_priority = priority;
53  int ret = pthread_setschedparam(thread, SCHED_FIFO, &params);
54  if (ret != 0)
55  {
56  switch (ret)
57  {
58  case EPERM:
59  {
60  URCL_LOG_WARN("Your system/user seems not to be setup for FIFO scheduling. We recommend using a lowlatency "
61  "kernel with FIFO scheduling. See "
62  "%s for details.",
63  RT_DOC_URL.c_str());
64  break;
65  }
66  default:
67 
68  {
69  URCL_LOG_ERROR("Unsuccessful in setting thread to FIFO scheduling with priority %i. %s", priority,
70  strerror(ret));
71  }
72  }
73 
74  return false;
75  }
76 
77  // Now verify the change in thread priority
78  int policy = 0;
79  ret = pthread_getschedparam(thread, &policy, &params);
80  if (ret != 0)
81  {
82  URCL_LOG_ERROR("Couldn't retrieve scheduling parameters");
83  return false;
84  }
85 
86  // Check the correct policy was applied
87  if (policy != SCHED_FIFO)
88  {
89  URCL_LOG_ERROR("Scheduling is NOT SCHED_FIFO!");
90  return false;
91  }
92  else
93  {
94  URCL_LOG_INFO("SCHED_FIFO OK, priority %i", params.sched_priority);
95  if (params.sched_priority != priority)
96  {
97  URCL_LOG_ERROR("Thread priority is %i instead of the expected %i", params.sched_priority, priority);
98  return false;
99  }
100  }
101  return true;
102 #endif
103 }
104 
105 void waitFor(std::function<bool()> condition, const std::chrono::milliseconds timeout,
106  const std::chrono::milliseconds check_interval)
107 {
108  auto start_time = std::chrono::system_clock::now();
109  while (std::chrono::system_clock::now() - start_time < timeout)
110  {
111  if (condition())
112  {
113  return;
114  }
115  URCL_LOG_DEBUG("Waiting for condition to be met...");
116  std::this_thread::sleep_for(check_interval);
117  }
118  throw urcl::TimeoutException("Timeout while waiting for condition to be met", timeout);
119 }
120 } // namespace urcl
urcl::setFiFoScheduling
bool setFiFoScheduling(pthread_t &thread, const int priority)
Definition: helpers.cpp:46
RT_DOC_URL
const std::string RT_DOC_URL
Definition: helpers.cpp:41
exceptions.h
urcl::waitFor
void waitFor(std::function< bool()> condition, const std::chrono::milliseconds timeout, const std::chrono::milliseconds check_interval=std::chrono::milliseconds(50))
Wait for a condition to be true.
Definition: helpers.cpp:105
urcl
Definition: bin_parser.h:36
URCL_LOG_ERROR
#define URCL_LOG_ERROR(...)
Definition: log.h:26
URCL_LOG_DEBUG
#define URCL_LOG_DEBUG(...)
Definition: log.h:23
helpers.h
urcl::TimeoutException
A specialized exception representing that communication to the tool is not possible.
Definition: exceptions.h:121
URCL_LOG_INFO
#define URCL_LOG_INFO(...)
Definition: log.h:25
log.h
URCL_LOG_WARN
#define URCL_LOG_WARN(...)
Definition: log.h:24


ur_client_library
Author(s): Thomas Timm Andersen, Simon Rasmussen, Felix Exner, Lea Steffen, Tristan Schnell
autogenerated on Mon May 26 2025 02:35:58