forked from UniversalRobots/Universal_Robots_Client_Library
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.cpp
More file actions
206 lines (190 loc) · 5.68 KB
/
Copy pathhelpers.cpp
File metadata and controls
206 lines (190 loc) · 5.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
// -- BEGIN LICENSE BLOCK ----------------------------------------------
// Copyright 2022 FZI Forschungszentrum Informatik
// Created on behalf of Universal Robots A/S
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// -- END LICENSE BLOCK ------------------------------------------------
//----------------------------------------------------------------------
/*!\file
*
* \author Felix Exner exner@fzi.de
* \date 2022-12-15
*
*/
//----------------------------------------------------------------------
#include <ur_client_library/exceptions.h>
#include <ur_client_library/helpers.h>
#include <ur_client_library/log.h>
#include <algorithm>
#include <cctype>
#include <cstring>
#include <fstream>
#include <iostream>
#include <thread>
// clang-format off
// We want to keep the URL in one line to avoid formatting issues. This will make it easier to
// extract the URL for an automatic check.
const std::string RT_DOC_URL = "https://docs.universal-robots.com/Universal_Robots_ROS_Documentation/doc/ur_client_library/doc/real_time.html";
// clang-format on
namespace urcl
{
bool setFiFoScheduling(pthread_t& thread, const int priority)
{
#ifdef _WIN32
return ::SetThreadPriority(thread, priority);
#else // _WIN32
struct sched_param params;
params.sched_priority = priority;
int ret = pthread_setschedparam(thread, SCHED_FIFO, ¶ms);
if (ret != 0)
{
switch (ret)
{
case EPERM:
{
URCL_LOG_WARN("Your system/user seems not to be setup for FIFO scheduling. We recommend using a lowlatency "
"kernel with FIFO scheduling. See "
"%s for details.",
RT_DOC_URL.c_str());
break;
}
default:
{
URCL_LOG_ERROR("Unsuccessful in setting thread to FIFO scheduling with priority %i. %s", priority,
strerror_portable(ret).c_str());
}
}
return false;
}
// Now verify the change in thread priority
int policy = 0;
ret = pthread_getschedparam(thread, &policy, ¶ms);
if (ret != 0)
{
URCL_LOG_ERROR("Couldn't retrieve scheduling parameters");
return false;
}
// Check the correct policy was applied
if (policy != SCHED_FIFO)
{
URCL_LOG_ERROR("Scheduling is NOT SCHED_FIFO!");
return false;
}
else
{
URCL_LOG_INFO("SCHED_FIFO OK, priority %i", params.sched_priority);
if (params.sched_priority != priority)
{
URCL_LOG_ERROR("Thread priority is %i instead of the expected %i", params.sched_priority, priority);
return false;
}
}
return true;
#endif
}
void waitFor(std::function<bool()> condition, const std::chrono::milliseconds timeout,
const std::chrono::milliseconds check_interval)
{
auto start_time = std::chrono::system_clock::now();
while (std::chrono::system_clock::now() - start_time < timeout)
{
if (condition())
{
return;
}
URCL_LOG_DEBUG("Waiting for condition to be met...");
std::this_thread::sleep_for(check_interval);
}
throw urcl::TimeoutException("Timeout while waiting for condition to be met", timeout);
}
bool parseBoolean(const std::string& str)
{
std::string lower = str;
std::transform(lower.begin(), lower.end(), lower.begin(),
[](unsigned char c) { return static_cast<char>(std::tolower(c)); });
if (lower == "true" || lower == "1" || lower == "yes" || lower == "on")
{
return true;
}
else if (lower == "false" || lower == "0" || lower == "no" || lower == "off")
{
return false;
}
else
{
std::stringstream ss;
ss << "Invalid boolean value: '" << str << "'. Expected 'true', 'false', '1', '0', 'yes', 'no', 'on', or 'off'.";
URCL_LOG_ERROR(ss.str().c_str());
throw UrException(ss.str().c_str());
}
}
std::vector<std::string> splitString(const std::string& input, const std::string& delimiter)
{
std::vector<std::string> result;
size_t pos = 0;
size_t pos_end = pos;
std::string substring;
while ((pos_end = input.find(delimiter, pos)) != std::string::npos)
{
substring = input.substr(pos, pos_end - pos);
result.push_back(substring);
pos = pos_end + delimiter.length();
}
substring = input.substr(pos);
result.push_back(substring);
return result;
}
RobotSeries robotSeriesFromTypeAndVersion(const RobotType type, const VersionInformation& version_info)
{
switch (type)
{
case RobotType::UR3:
case RobotType::UR5:
case RobotType::UR10:
if (version_info.major >= 5)
{
return RobotSeries::E_SERIES;
}
else
{
return RobotSeries::CB3;
}
case RobotType::UR16:
if (version_info.major >= 5)
{
return RobotSeries::E_SERIES;
}
else
{
return RobotSeries::UNDEFINED;
}
case RobotType::UR15:
case RobotType::UR18:
case RobotType::UR20:
case RobotType::UR30:
case RobotType::UR8LONG:
if (version_info.major >= 5)
{
return RobotSeries::UR_SERIES;
}
else
{
return RobotSeries::UNDEFINED;
}
case RobotType::UNDEFINED:
return RobotSeries::UNDEFINED;
}
return RobotSeries::UNDEFINED;
}
} // namespace urcl