-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathreverse_interface.cpp
More file actions
281 lines (241 loc) · 9.27 KB
/
Copy pathreverse_interface.cpp
File metadata and controls
281 lines (241 loc) · 9.27 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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
// -- BEGIN LICENSE BLOCK ----------------------------------------------
// Copyright 2021 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 2021-06-01
*
*/
//----------------------------------------------------------------------
#include <ur_client_library/control/reverse_interface.h>
#include <urcl_3rdparty/portable_endian.h>
#include <math.h>
namespace urcl
{
namespace control
{
ReverseInterface::ReverseInterface(uint32_t port, std::function<void(bool)> handle_program_state,
std::chrono::milliseconds step_time)
: ReverseInterface(ReverseInterfaceConfig{ port, handle_program_state, step_time })
{
}
ReverseInterface::ReverseInterface(const ReverseInterfaceConfig& config)
: client_fd_(INVALID_SOCKET)
, server_(config.port)
, robot_software_version_(config.robot_software_version)
, handle_program_state_(config.handle_program_state)
, step_time_(config.step_time)
, keep_alive_count_modified_deprecated_(false)
{
if (handle_program_state_)
{
handle_program_state_(false);
}
server_.setMessageCallback(std::bind(&ReverseInterface::messageCallback, this, std::placeholders::_1,
std::placeholders::_2, std::placeholders::_3));
server_.setConnectCallback(std::bind(&ReverseInterface::connectionCallback, this, std::placeholders::_1));
server_.setDisconnectCallback(std::bind(&ReverseInterface::disconnectionCallback, this, std::placeholders::_1));
server_.setMaxClientsAllowed(1);
server_.start();
}
bool ReverseInterface::write(const vector6d_t* positions, const comm::ControlMode control_mode,
const RobotReceiveTimeout& robot_receive_timeout)
{
const int message_length = 7;
if (client_fd_ == INVALID_SOCKET)
{
return false;
}
uint8_t buffer[sizeof(int32_t) * MAX_MESSAGE_LENGTH];
uint8_t* b_pos = buffer;
int read_timeout = 100;
// If control mode is stopped, we shouldn't verify robot receive timeout
if (control_mode != comm::ControlMode::MODE_STOPPED)
{
read_timeout = robot_receive_timeout.verifyRobotReceiveTimeout(control_mode, step_time_);
}
// This can be removed once we remove the setkeepAliveCount() method
auto read_timeout_resolved = read_timeout;
if (keep_alive_count_modified_deprecated_)
{
// Translate keep alive count into read timeout. 20 milliseconds was the "old read timeout"
read_timeout_resolved = 20 * keepalive_count_;
}
// The first element is always the read timeout.
int32_t val = read_timeout_resolved;
val = htobe32(val);
b_pos += append(b_pos, val);
if (positions != nullptr)
{
for (auto const& pos : *positions)
{
int32_t joint_val = static_cast<int32_t>(round(pos * MULT_JOINTSTATE));
joint_val = htobe32(joint_val);
b_pos += append(b_pos, joint_val);
}
}
else
{
b_pos += 6 * sizeof(int32_t);
}
// writing zeros to allow usage with other script commands
for (size_t i = message_length; i < MAX_MESSAGE_LENGTH - 1; i++)
{
val = htobe32(0);
b_pos += append(b_pos, val);
}
val = htobe32(toUnderlying(control_mode));
b_pos += append(b_pos, val);
size_t written;
return server_.write(client_fd_, buffer, sizeof(buffer), written);
}
bool ReverseInterface::writeTrajectoryControlMessage(const TrajectoryControlMessage trajectory_action,
const int point_number,
const RobotReceiveTimeout& robot_receive_timeout)
{
const int message_length = 3;
if (client_fd_ == INVALID_SOCKET)
{
return false;
}
uint8_t buffer[sizeof(int32_t) * MAX_MESSAGE_LENGTH];
uint8_t* b_pos = buffer;
int read_timeout = robot_receive_timeout.verifyRobotReceiveTimeout(comm::ControlMode::MODE_FORWARD, step_time_);
// This can be removed once we remove the setkeepAliveCount() method
auto read_timeout_resolved = read_timeout;
if (keep_alive_count_modified_deprecated_)
{
// Translate keep alive count into read timeout. 20 milliseconds was the "old read timeout"
read_timeout_resolved = 20 * keepalive_count_;
}
// The first element is always the read timeout.
int32_t val = read_timeout_resolved;
val = htobe32(val);
b_pos += append(b_pos, val);
val = htobe32(toUnderlying(trajectory_action));
b_pos += append(b_pos, val);
val = htobe32(point_number);
b_pos += append(b_pos, val);
// writing zeros to allow usage with other script commands
for (size_t i = message_length; i < MAX_MESSAGE_LENGTH - 1; i++)
{
val = htobe32(0);
b_pos += append(b_pos, val);
}
val = htobe32(toUnderlying(comm::ControlMode::MODE_FORWARD));
b_pos += append(b_pos, val);
size_t written;
return server_.write(client_fd_, buffer, sizeof(buffer), written);
}
bool ReverseInterface::writeFreedriveControlMessage(const FreedriveControlMessage freedrive_action,
const RobotReceiveTimeout& robot_receive_timeout,
const std::array<int32_t, 6>& free_axes,
const std::array<double, 6>& feature_pose)
{
const int message_length = 14;
if (client_fd_ == INVALID_SOCKET)
{
return false;
}
uint8_t buffer[sizeof(int32_t) * MAX_MESSAGE_LENGTH];
uint8_t* b_pos = buffer;
int read_timeout = robot_receive_timeout.verifyRobotReceiveTimeout(comm::ControlMode::MODE_FREEDRIVE, step_time_);
// This can be removed once we remove the setkeepAliveCount() method
auto read_timeout_resolved = read_timeout;
if (keep_alive_count_modified_deprecated_)
{
// Translate keep alive count into read timeout. 20 milliseconds was the "old read timeout"
read_timeout_resolved = 20 * keepalive_count_;
}
// The first element is always the read timeout.
int32_t val = read_timeout_resolved;
val = htobe32(val);
b_pos += append(b_pos, val);
val = htobe32(toUnderlying(freedrive_action));
b_pos += append(b_pos, val);
// Add allowed axes for movement
for (int32_t axis : free_axes)
{
val = htobe32(axis);
b_pos += append(b_pos, val);
}
// Add feature pose scaled to microns microradians to avoid precision loss in integer conversion
for (double p : feature_pose)
{
val = htobe32(static_cast<int32_t>(round(p * MULT_JOINTSTATE)));
b_pos += append(b_pos, val);
}
// writing zeros to allow usage with other script commands
for (size_t i = message_length; i < MAX_MESSAGE_LENGTH - 1; i++)
{
val = htobe32(0);
b_pos += append(b_pos, val);
}
val = htobe32(toUnderlying(comm::ControlMode::MODE_FREEDRIVE));
b_pos += append(b_pos, val);
size_t written;
return server_.write(client_fd_, buffer, sizeof(buffer), written);
}
void ReverseInterface::setKeepaliveCount(const uint32_t count)
{
URCL_LOG_WARN("DEPRECATION NOTICE: Setting the keepalive count has been deprecated. Instead you should set the "
"timeout directly in the write commands. Please change your code to set the read timeout in the write "
"commands "
"directly. This keepalive count will overwrite the timeout passed to the write functions.");
keepalive_count_ = count;
keep_alive_count_modified_deprecated_ = true;
}
void ReverseInterface::connectionCallback(const socket_t filedescriptor)
{
if (client_fd_ == INVALID_SOCKET)
{
URCL_LOG_INFO("Robot connected to reverse interface. Ready to receive control commands.");
client_fd_ = filedescriptor;
if (handle_program_state_)
{
handle_program_state_(true);
}
}
else
{
URCL_LOG_ERROR("Connection request to ReverseInterface received while connection already established. Only one "
"connection is allowed at a time. Ignoring this request.");
}
}
void ReverseInterface::disconnectionCallback(const socket_t filedescriptor)
{
URCL_LOG_INFO("Connection to reverse interface dropped.", filedescriptor);
client_fd_ = INVALID_SOCKET;
if (handle_program_state_)
{
handle_program_state_(false);
}
for (auto handler : disconnect_callbacks_)
{
handler.function(filedescriptor);
}
}
void ReverseInterface::messageCallback([[maybe_unused]] const socket_t filedescriptor, [[maybe_unused]] char* buffer,
[[maybe_unused]] int nbytesrecv)
{
URCL_LOG_WARN("Message on ReverseInterface received. The reverse interface currently does not support any message "
"handling. This message will be ignored.");
}
} // namespace control
} // namespace urcl