Skip to content

Commit 557a23b

Browse files
authored
Merge pull request UniversalRobots#70 from fmauch/refactor_reverse_interface
Refactor reverse interface
2 parents 349e82e + 0d70c8a commit 557a23b

10 files changed

+269
-141
lines changed

CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -pedantic")
1919
add_library(urcl SHARED
2020
src/comm/tcp_socket.cpp
2121
src/comm/tcp_server.cpp
22+
src/control/reverse_interface.cpp
23+
src/control/script_sender.cpp
2224
src/primary/primary_package.cpp
2325
src/primary/robot_message.cpp
2426
src/primary/robot_state.cpp

doc/dataflow.graphml

+2-2
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
<y:Geometry height="97.4609375" width="192.0" x="995.0" y="404.84375"/>
5555
<y:Fill color="#F2F0D8" transparent="false"/>
5656
<y:BorderStyle color="#000000" type="line" width="1.0"/>
57-
<y:NodeLabel alignment="right" autoSizePolicy="node_width" backgroundColor="#B7B69E" borderDistance="0.0" fontFamily="Dialog" fontSize="15" fontStyle="plain" hasLineColor="false" height="21.4609375" horizontalTextPosition="center" iconTextGap="4" modelName="internal" modelPosition="t" textColor="#000000" verticalTextPosition="bottom" visible="true" width="192.0" x="0.0" xml:space="preserve" y="0.0">comm::ReverseInterface</y:NodeLabel>
57+
<y:NodeLabel alignment="right" autoSizePolicy="node_width" backgroundColor="#B7B69E" borderDistance="0.0" fontFamily="Dialog" fontSize="15" fontStyle="plain" hasLineColor="false" height="21.4609375" horizontalTextPosition="center" iconTextGap="4" modelName="internal" modelPosition="t" textColor="#000000" verticalTextPosition="bottom" visible="true" width="192.0" x="0.0" xml:space="preserve" y="0.0">control::ReverseInterface</y:NodeLabel>
5858
<y:Shape type="rectangle"/>
5959
<y:DropShadow color="#D2D2D2" offsetX="4" offsetY="4"/>
6060
<y:State closed="false" closedHeight="50.0" closedWidth="50.0" innerGraphDisplayEnabled="false"/>
@@ -98,7 +98,7 @@
9898
<y:Geometry height="97.4609375" width="192.0" x="5.0" y="264.84375"/>
9999
<y:Fill color="#F2F0D8" transparent="false"/>
100100
<y:BorderStyle color="#000000" type="line" width="1.0"/>
101-
<y:NodeLabel alignment="right" autoSizePolicy="node_width" backgroundColor="#B7B69E" borderDistance="0.0" fontFamily="Dialog" fontSize="15" fontStyle="plain" hasLineColor="false" height="21.4609375" horizontalTextPosition="center" iconTextGap="4" modelName="internal" modelPosition="t" textColor="#000000" verticalTextPosition="bottom" visible="true" width="192.0" x="0.0" xml:space="preserve" y="0.0">comm::ScriptSender</y:NodeLabel>
101+
<y:NodeLabel alignment="right" autoSizePolicy="node_width" backgroundColor="#B7B69E" borderDistance="0.0" fontFamily="Dialog" fontSize="15" fontStyle="plain" hasLineColor="false" height="21.4609375" horizontalTextPosition="center" iconTextGap="4" modelName="internal" modelPosition="t" textColor="#000000" verticalTextPosition="bottom" visible="true" width="192.0" x="0.0" xml:space="preserve" y="0.0">control::ScriptSender</y:NodeLabel>
102102
<y:Shape type="rectangle"/>
103103
<y:DropShadow color="#D2D2D2" offsetX="4" offsetY="4"/>
104104
<y:State closed="false" closedHeight="50.0" closedWidth="50.0" innerGraphDisplayEnabled="false"/>

doc/dataflow.svg

+2-2
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2+
3+
// -- BEGIN LICENSE BLOCK ----------------------------------------------
4+
// Copyright 2021 FZI Forschungszentrum Informatik
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
// -- END LICENSE BLOCK ------------------------------------------------
18+
19+
//----------------------------------------------------------------------
20+
/*!\file
21+
*
22+
* \author Felix Exner [email protected]
23+
* \date 2021-06-01
24+
*
25+
*/
26+
//----------------------------------------------------------------------
27+
28+
#ifndef UR_CLIENT_LIBRARY_CONTROL_MODE_H_INCLUDED
29+
#define UR_CLIENT_LIBRARY_CONTROL_MODE_H_INCLUDED
30+
#endif // ifndef UR_CLIENT_LIBRARY_CONTROL_MODE_H_INCLUDED
31+
32+
namespace urcl
33+
{
34+
namespace comm
35+
{
36+
/*!
37+
* \brief Control modes as interpreted from the script runnning on the robot.
38+
*/
39+
enum class ControlMode : int32_t
40+
{
41+
MODE_STOPPED = -2, ///< When this is set, the program is expected to stop and exit.
42+
MODE_UNINITIALIZED = -1, ///< Startup default until another mode is sent to the script.
43+
MODE_IDLE = 0, ///< Set when no controller is currently active controlling the robot.
44+
MODE_SERVOJ = 1, ///< Set when servoj control is active.
45+
MODE_SPEEDJ = 2 ///< Set when speedj control is active.
46+
};
47+
} // namespace comm
48+
} // namespace urcl

include/ur_client_library/comm/reverse_interface.h include/ur_client_library/control/reverse_interface.h

+12-89
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#define UR_CLIENT_LIBRARY_REVERSE_INTERFACE_H_INCLUDED
3030

3131
#include "ur_client_library/comm/tcp_server.h"
32+
#include "ur_client_library/comm/control_mode.h"
3233
#include "ur_client_library/types.h"
3334
#include "ur_client_library/log.h"
3435
#include <cstring>
@@ -37,20 +38,8 @@
3738

3839
namespace urcl
3940
{
40-
namespace comm
41+
namespace control
4142
{
42-
/*!
43-
* \brief Control modes as interpreted from the script runnning on the robot.
44-
*/
45-
enum class ControlMode : int32_t
46-
{
47-
MODE_STOPPED = -2, ///< When this is set, the program is expected to stop and exit.
48-
MODE_UNINITIALIZED = -1, ///< Startup default until another mode is sent to the script.
49-
MODE_IDLE = 0, ///< Set when no controller is currently active controlling the robot.
50-
MODE_SERVOJ = 1, ///< Set when servoj control is active.
51-
MODE_SPEEDJ = 2 ///< Set when speedj control is active.
52-
};
53-
5443
/*!
5544
* \brief The ReverseInterface class handles communication to the robot. It starts a server and
5645
* waits for the robot to connect via its URCaps program.
@@ -65,67 +54,23 @@ class ReverseInterface
6554
* \param port Port the Server is started on
6655
* \param handle_program_state Function handle to a callback on program state changes.
6756
*/
68-
ReverseInterface(uint32_t port, std::function<void(bool)> handle_program_state)
69-
: client_fd_(-1), server_(port), handle_program_state_(handle_program_state), keepalive_count_(1)
70-
{
71-
handle_program_state_(false);
72-
server_.setMessageCallback(
73-
std::bind(&ReverseInterface::messageCallback, this, std::placeholders::_1, std::placeholders::_2));
74-
server_.setConnectCallback(std::bind(&ReverseInterface::connectionCallback, this, std::placeholders::_1));
75-
server_.setDisconnectCallback(std::bind(&ReverseInterface::disconnectionCallback, this, std::placeholders::_1));
76-
server_.setMaxClientsAllowed(1);
77-
server_.start();
78-
}
57+
ReverseInterface(uint32_t port, std::function<void(bool)> handle_program_state);
58+
7959
/*!
8060
* \brief Disconnects possible clients so the reverse interface object can be safely destroyed.
8161
*/
82-
~ReverseInterface()
83-
{
84-
}
62+
~ReverseInterface() = default;
8563

8664
/*!
8765
* \brief Writes needed information to the robot to be read by the URCaps program.
8866
*
8967
* \param positions A vector of joint targets for the robot
90-
* \param control_mode Control mode assigned to this command. See documentation of ::ControlMode
68+
* \param control_mode Control mode assigned to this command. See documentation of comm::ControlMode
9169
* for details on possible values.
9270
*
9371
* \returns True, if the write was performed successfully, false otherwise.
9472
*/
95-
bool write(const vector6d_t* positions, const ControlMode control_mode = ControlMode::MODE_IDLE)
96-
{
97-
if (client_fd_ == -1)
98-
{
99-
return false;
100-
}
101-
uint8_t buffer[sizeof(int32_t) * 8];
102-
uint8_t* b_pos = buffer;
103-
104-
// The first element is always the keepalive signal.
105-
int32_t val = htobe32(keepalive_count_);
106-
b_pos += append(b_pos, val);
107-
108-
if (positions != nullptr)
109-
{
110-
for (auto const& pos : *positions)
111-
{
112-
int32_t val = static_cast<int32_t>(pos * MULT_JOINTSTATE);
113-
val = htobe32(val);
114-
b_pos += append(b_pos, val);
115-
}
116-
}
117-
else
118-
{
119-
b_pos += 6 * sizeof(int32_t);
120-
}
121-
122-
val = htobe32(toUnderlying(control_mode));
123-
b_pos += append(b_pos, val);
124-
125-
size_t written;
126-
127-
return server_.write(client_fd_, buffer, sizeof(buffer), written);
128-
}
73+
bool write(const vector6d_t* positions, const comm::ControlMode control_mode = comm::ControlMode::MODE_IDLE);
12974

13075
/*!
13176
* \brief Set the Keepalive count. This will set the number of allowed timeout reads on the robot.
@@ -138,36 +83,14 @@ class ReverseInterface
13883
}
13984

14085
private:
141-
void connectionCallback(const int filedescriptor)
142-
{
143-
if (client_fd_ < 0)
144-
{
145-
URCL_LOG_INFO("Robot connected to reverse interface. Ready to receive control commands.");
146-
client_fd_ = filedescriptor;
147-
handle_program_state_(true);
148-
}
149-
else
150-
{
151-
URCL_LOG_ERROR("Connection request to ReverseInterface received while connection already established. Only one "
152-
"connection is allowed at a time. Ignoring this request.");
153-
}
154-
}
86+
void connectionCallback(const int filedescriptor);
15587

156-
void disconnectionCallback(const int filedescriptor)
157-
{
158-
URCL_LOG_INFO("Connection to reverse interface dropped.", filedescriptor);
159-
client_fd_ = -1;
160-
handle_program_state_(false);
161-
}
88+
void disconnectionCallback(const int filedescriptor);
16289

163-
void messageCallback(const int filedescriptor, char* buffer)
164-
{
165-
URCL_LOG_WARN("Messge on ReverseInterface received. The reverse interface currently does not support any message "
166-
"handling. This message will be ignored.");
167-
}
90+
void messageCallback(const int filedescriptor, char* buffer);
16891

16992
int client_fd_;
170-
TCPServer server_;
93+
comm::TCPServer server_;
17194

17295
static const int32_t MULT_JOINTSTATE = 1000000;
17396

@@ -183,7 +106,7 @@ class ReverseInterface
183106
uint32_t keepalive_count_;
184107
};
185108

186-
} // namespace comm
109+
} // namespace control
187110
} // namespace urcl
188111

189112
#endif // UR_CLIENT_LIBRARY_REVERSE_INTERFACE_H_INCLUDED

include/ur_client_library/comm/script_sender.h include/ur_client_library/control/script_sender.h

+8-42
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636

3737
namespace urcl
3838
{
39-
namespace comm
39+
namespace control
4040
{
4141
/*!
4242
* \brief The ScriptSender class starts a TCPServer for a robot to connect to and waits for a
@@ -52,59 +52,25 @@ class ScriptSender
5252
* \param port Port to start the server on
5353
* \param program Program to send to the robot upon request
5454
*/
55-
ScriptSender(uint32_t port, const std::string& program) : server_(port), script_thread_(), program_(program)
56-
{
57-
server_.setMessageCallback(
58-
std::bind(&ScriptSender::messageCallback, this, std::placeholders::_1, std::placeholders::_2));
59-
server_.setConnectCallback(std::bind(&ScriptSender::connectionCallback, this, std::placeholders::_1));
60-
server_.setDisconnectCallback(std::bind(&ScriptSender::disconnectionCallback, this, std::placeholders::_1));
61-
server_.start();
62-
}
55+
ScriptSender(uint32_t port, const std::string& program);
6356

6457
private:
65-
TCPServer server_;
58+
comm::TCPServer server_;
6659
std::thread script_thread_;
6760
std::string program_;
6861

6962
const std::string PROGRAM_REQUEST_ = std::string("request_program\n");
7063

71-
void connectionCallback(const int filedescriptor)
72-
{
73-
URCL_LOG_DEBUG("New client connected at FD %d.", filedescriptor);
74-
}
64+
void connectionCallback(const int filedescriptor);
7565

76-
void disconnectionCallback(const int filedescriptor)
77-
{
78-
URCL_LOG_DEBUG("Client at FD %d disconnected.", filedescriptor);
79-
}
66+
void disconnectionCallback(const int filedescriptor);
8067

81-
void messageCallback(const int filedescriptor, char* buffer)
82-
{
83-
if (std::string(buffer) == PROGRAM_REQUEST_)
84-
{
85-
URCL_LOG_INFO("Robot requested program");
86-
sendProgram(filedescriptor);
87-
}
88-
}
68+
void messageCallback(const int filedescriptor, char* buffer);
8969

90-
void sendProgram(const int filedescriptor)
91-
{
92-
size_t len = program_.size();
93-
const uint8_t* data = reinterpret_cast<const uint8_t*>(program_.c_str());
94-
size_t written;
95-
96-
if (server_.write(filedescriptor, data, len, written))
97-
{
98-
URCL_LOG_INFO("Sent program to robot");
99-
}
100-
else
101-
{
102-
URCL_LOG_ERROR("Could not send program to robot");
103-
}
104-
}
70+
void sendProgram(const int filedescriptor);
10571
};
10672

107-
} // namespace comm
73+
} // namespace control
10874
} // namespace urcl
10975

11076
#endif // UR_CLIENT_LIBRARY_SCRIPT_SENDER_H_INCLUDED

include/ur_client_library/ur/ur_driver.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
#include <functional>
3131

3232
#include "ur_client_library/rtde/rtde_client.h"
33-
#include "ur_client_library/comm/reverse_interface.h"
34-
#include "ur_client_library/comm/script_sender.h"
33+
#include "ur_client_library/control/reverse_interface.h"
34+
#include "ur_client_library/control/script_sender.h"
3535
#include "ur_client_library/ur/tool_communication.h"
3636
#include "ur_client_library/ur/version_information.h"
3737
#include "ur_client_library/primary/robot_message/version_message.h"
@@ -271,8 +271,8 @@ class UrDriver
271271
int rtde_frequency_;
272272
comm::INotifier notifier_;
273273
std::unique_ptr<rtde_interface::RTDEClient> rtde_client_;
274-
std::unique_ptr<comm::ReverseInterface> reverse_interface_;
275-
std::unique_ptr<comm::ScriptSender> script_sender_;
274+
std::unique_ptr<control::ReverseInterface> reverse_interface_;
275+
std::unique_ptr<control::ScriptSender> script_sender_;
276276
std::unique_ptr<comm::URStream<primary_interface::PrimaryPackage>> primary_stream_;
277277
std::unique_ptr<comm::URStream<primary_interface::PrimaryPackage>> secondary_stream_;
278278

0 commit comments

Comments
 (0)