Skip to content

Commit b4c6c8f

Browse files
authored
Initialize ReverseInterface with a config struct (#351)
This way, we will be able to supply additional information such as the robot's software version to the ReverseInterface without breaking the API.
1 parent 96b6381 commit b4c6c8f

File tree

8 files changed

+57
-11
lines changed

8 files changed

+57
-11
lines changed

include/ur_client_library/control/reverse_interface.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "ur_client_library/types.h"
3535
#include "ur_client_library/log.h"
3636
#include "ur_client_library/ur/robot_receive_timeout.h"
37+
#include "ur_client_library/ur/version_information.h"
3738
#include <cstring>
3839
#include <endian.h>
3940
#include <condition_variable>
@@ -62,6 +63,17 @@ enum class FreedriveControlMessage : int32_t
6263
FREEDRIVE_START = 1, ///< Represents command to start freedrive mode.
6364
};
6465

66+
struct ReverseInterfaceConfig
67+
{
68+
uint32_t port = 50001; //!< Port the server is started on
69+
std::function<void(bool)> handle_program_state = [](bool) {
70+
return;
71+
}; //!< Function handle to a callback on program state changes.
72+
std::chrono::milliseconds step_time = std::chrono::milliseconds(8); //!< The robots step time
73+
uint32_t keepalive_count = 0; //!< Number of allowed timeout reads on the robot.
74+
VersionInformation robot_software_version = VersionInformation(); //!< The robot software version.
75+
};
76+
6577
/*!
6678
* \brief The ReverseInterface class handles communication to the robot. It starts a server and
6779
* waits for the robot to connect via its URCaps program.
@@ -79,9 +91,12 @@ class ReverseInterface
7991
* \param handle_program_state Function handle to a callback on program state changes.
8092
* \param step_time The robots step time
8193
*/
94+
[[deprecated("Use ReverseInterfaceConfig instead of port, handle_program_state and step_time parameters")]]
8295
ReverseInterface(uint32_t port, std::function<void(bool)> handle_program_state,
8396
std::chrono::milliseconds step_time = std::chrono::milliseconds(8));
8497

98+
ReverseInterface(const ReverseInterfaceConfig& config);
99+
85100
/*!
86101
* \brief Disconnects possible clients so the reverse interface object can be safely destroyed.
87102
*/
@@ -167,6 +182,8 @@ class ReverseInterface
167182
socket_t client_fd_;
168183
comm::TCPServer server_;
169184

185+
VersionInformation robot_software_version_;
186+
170187
template <typename T>
171188
size_t append(uint8_t* buffer, T& val)
172189
{

include/ur_client_library/control/script_command_interface.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,17 @@ class ScriptCommandInterface : public ReverseInterface
6161
*
6262
* \param port Port to start the server on
6363
*/
64+
[[deprecated("Use ReverseInterfaceConfig instead of port, handle_program_state and step_time parameters")]]
6465
ScriptCommandInterface(uint32_t port);
6566

67+
/*!
68+
* \brief Creates a ScriptCommandInterface object, including a new TCPServer
69+
*
70+
* \param config Configuration for the ReverseInterface, including the port to start the server
71+
* on.
72+
*/
73+
ScriptCommandInterface(const ReverseInterfaceConfig& config);
74+
6675
/*!
6776
* \brief Zero the force torque sensor
6877
*
@@ -195,4 +204,4 @@ class ScriptCommandInterface : public ReverseInterface
195204
} // namespace control
196205
} // namespace urcl
197206

198-
#endif // UR_CLIENT_LIBRARY_SCRIPT_COMMAND_INTERFACE_H_INCLUDED
207+
#endif // UR_CLIENT_LIBRARY_SCRIPT_COMMAND_INTERFACE_H_INCLUDED

src/control/reverse_interface.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,16 @@ namespace control
3535
{
3636
ReverseInterface::ReverseInterface(uint32_t port, std::function<void(bool)> handle_program_state,
3737
std::chrono::milliseconds step_time)
38+
: ReverseInterface(ReverseInterfaceConfig{ port, handle_program_state, step_time })
39+
{
40+
}
41+
42+
ReverseInterface::ReverseInterface(const ReverseInterfaceConfig& config)
3843
: client_fd_(INVALID_SOCKET)
39-
, server_(port)
40-
, handle_program_state_(handle_program_state)
41-
, step_time_(step_time)
44+
, server_(config.port)
45+
, robot_software_version_(config.robot_software_version)
46+
, handle_program_state_(config.handle_program_state)
47+
, step_time_(config.step_time)
4248
, keep_alive_count_modified_deprecated_(false)
4349
{
4450
handle_program_state_(false);

src/control/script_command_interface.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,11 @@ namespace urcl
3333
{
3434
namespace control
3535
{
36-
ScriptCommandInterface::ScriptCommandInterface(uint32_t port) : ReverseInterface(port, [](bool foo) { return foo; })
36+
ScriptCommandInterface::ScriptCommandInterface(uint32_t port) : ScriptCommandInterface(ReverseInterfaceConfig{ port })
37+
{
38+
}
39+
40+
ScriptCommandInterface::ScriptCommandInterface(const ReverseInterfaceConfig& config) : ReverseInterface(config)
3741
{
3842
client_connected_ = false;
3943
}

src/control/trajectory_point_interface.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ std::string trajectoryResultToString(const TrajectoryResult result)
5656
}
5757
}
5858

59-
TrajectoryPointInterface::TrajectoryPointInterface(uint32_t port) : ReverseInterface(port, [](bool foo) { return foo; })
59+
TrajectoryPointInterface::TrajectoryPointInterface(uint32_t port) : ReverseInterface(ReverseInterfaceConfig{ port })
6060
{
6161
}
6262

src/ur/ur_driver.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,10 @@ void UrDriver::init(const UrDriverConfiguration& config)
8989
std::string local_ip = config.reverse_ip.empty() ? rtde_client_->getIP() : config.reverse_ip;
9090

9191
trajectory_interface_.reset(new control::TrajectoryPointInterface(config.trajectory_port));
92-
script_command_interface_.reset(new control::ScriptCommandInterface(config.script_command_port));
92+
control::ReverseInterfaceConfig script_command_config;
93+
script_command_config.port = config.script_command_port;
94+
script_command_config.robot_software_version = rtde_client_->getVersion();
95+
script_command_interface_.reset(new control::ScriptCommandInterface(script_command_config));
9396

9497
startPrimaryClientCommunication();
9598

@@ -622,7 +625,12 @@ void UrDriver::setupReverseInterface(const uint32_t reverse_port)
622625
{
623626
auto rtde_frequency = rtde_client_->getTargetFrequency();
624627
auto step_time = std::chrono::milliseconds(static_cast<int>(1000 / rtde_frequency));
625-
reverse_interface_.reset(new control::ReverseInterface(reverse_port, handle_program_state_, step_time));
628+
control::ReverseInterfaceConfig config;
629+
config.step_time = step_time;
630+
config.port = reverse_port;
631+
config.handle_program_state = handle_program_state_;
632+
config.robot_software_version = robot_version_;
633+
reverse_interface_.reset(new control::ReverseInterface(config));
626634
}
627635

628636
void UrDriver::startPrimaryClientCommunication()

tests/test_reverse_interface.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,10 @@ class ReverseIntefaceTest : public ::testing::Test
150150

151151
void SetUp()
152152
{
153-
reverse_interface_.reset(new control::ReverseInterface(
154-
50001, std::bind(&ReverseIntefaceTest::handleProgramState, this, std::placeholders::_1)));
153+
control::ReverseInterfaceConfig config;
154+
config.port = 50001;
155+
config.handle_program_state = std::bind(&ReverseIntefaceTest::handleProgramState, this, std::placeholders::_1);
156+
reverse_interface_.reset(new control::ReverseInterface(config));
155157
client_.reset(new Client(50001));
156158
}
157159

tests/test_script_command_interface.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ class ScriptCommandInterfaceTest : public ::testing::Test
102102

103103
void SetUp()
104104
{
105-
script_command_interface_.reset(new control::ScriptCommandInterface(50004));
105+
script_command_interface_.reset(new control::ScriptCommandInterface(control::ReverseInterfaceConfig{ 50004 }));
106106
client_.reset(new Client(50004));
107107
}
108108

0 commit comments

Comments
 (0)