Skip to content

Commit 3f00c2c

Browse files
committed
Updated ur_driver to be compatible with new primary client. The ur_driver now also checks if the checksum is correct when an instance is made, as it makes an instance of the primary client, which checks when it is instantiated.
Also added debugging message to tcp socket, which specifies which socket is already connected.
1 parent 92eb009 commit 3f00c2c

File tree

3 files changed

+42
-88
lines changed

3 files changed

+42
-88
lines changed

include/ur_client_library/ur/ur_driver.h

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "ur_client_library/ur/tool_communication.h"
3939
#include "ur_client_library/ur/version_information.h"
4040
#include "ur_client_library/primary/robot_message/version_message.h"
41+
#include "ur_client_library/primary/primary_client.h"
4142
#include "ur_client_library/rtde/rtde_writer.h"
4243

4344
namespace urcl
@@ -112,6 +113,7 @@ class UrDriver
112113
* \param tool_comm_setup Configuration for using the tool communication.
113114
* \param calibration_checksum Expected checksum of calibration. Will be matched against the
114115
* calibration reported by the robot.
116+
* \param simulated Set the robot as simulated or real.
115117
* \param reverse_port Port that will be opened by the driver to allow direct communication between the driver
116118
* and the robot controller.
117119
* \param script_sender_port The driver will offer an interface to receive the program's URScript on this port. If
@@ -129,9 +131,10 @@ class UrDriver
129131
UrDriver(const std::string& robot_ip, const std::string& script_file, const std::string& output_recipe_file,
130132
const std::string& input_recipe_file, std::function<void(bool)> handle_program_state, bool headless_mode,
131133
std::unique_ptr<ToolCommSetup> tool_comm_setup, const std::string& calibration_checksum = "",
132-
const uint32_t reverse_port = 50001, const uint32_t script_sender_port = 50002, int servoj_gain = 2000,
133-
double servoj_lookahead_time = 0.03, bool non_blocking_read = false, const std::string& reverse_ip = "",
134-
const uint32_t trajectory_port = 50003, const uint32_t script_command_port = 50004);
134+
const bool& simulated = false, const uint32_t reverse_port = 50001,
135+
const uint32_t script_sender_port = 50002, int servoj_gain = 2000, double servoj_lookahead_time = 0.03,
136+
bool non_blocking_read = false, const std::string& reverse_ip = "", const uint32_t trajectory_port = 50003,
137+
const uint32_t script_command_port = 50004);
135138
/*!
136139
* \brief Constructs a new UrDriver object.
137140
*
@@ -145,6 +148,7 @@ class UrDriver
145148
* \param headless_mode Parameter to control if the driver should be started in headless mode.
146149
* \param calibration_checksum Expected checksum of calibration. Will be matched against the
147150
* calibration reported by the robot.
151+
* \param simulated Set the robot as simulated or real.
148152
* \param reverse_port Port that will be opened by the driver to allow direct communication between the driver
149153
* and the robot controller
150154
* \param script_sender_port The driver will offer an interface to receive the program's URScript on this port.
@@ -161,13 +165,13 @@ class UrDriver
161165
*/
162166
UrDriver(const std::string& robot_ip, const std::string& script_file, const std::string& output_recipe_file,
163167
const std::string& input_recipe_file, std::function<void(bool)> handle_program_state, bool headless_mode,
164-
const std::string& calibration_checksum = "", const uint32_t reverse_port = 50001,
165-
const uint32_t script_sender_port = 50002, int servoj_gain = 2000, double servoj_lookahead_time = 0.03,
166-
bool non_blocking_read = false, const std::string& reverse_ip = "", const uint32_t trajectory_port = 50003,
167-
const uint32_t script_command_port = 50004)
168+
const std::string& calibration_checksum = "", const bool& simulated = false,
169+
const uint32_t reverse_port = 50001, const uint32_t script_sender_port = 50002, int servoj_gain = 2000,
170+
double servoj_lookahead_time = 0.03, bool non_blocking_read = false, const std::string& reverse_ip = "",
171+
const uint32_t trajectory_port = 50003, const uint32_t script_command_port = 50004)
168172
: UrDriver(robot_ip, script_file, output_recipe_file, input_recipe_file, handle_program_state, headless_mode,
169-
std::unique_ptr<ToolCommSetup>{}, calibration_checksum, reverse_port, script_sender_port, servoj_gain,
170-
servoj_lookahead_time, non_blocking_read, reverse_ip)
173+
std::unique_ptr<ToolCommSetup>{}, calibration_checksum, simulated, reverse_port, script_sender_port,
174+
servoj_gain, servoj_lookahead_time, non_blocking_read, reverse_ip)
171175
{
172176
}
173177

@@ -351,6 +355,16 @@ class UrDriver
351355
trajectory_interface_->setTrajectoryEndCallback(trajectory_done_cb);
352356
}
353357

358+
/*!
359+
* \brief Getter for the primary client
360+
*
361+
* \returns The used primary client
362+
*/
363+
std::shared_ptr<primary_interface::PrimaryClient> getPrimaryClient()
364+
{
365+
return primary_client_;
366+
}
367+
354368
private:
355369
std::string readScriptFile(const std::string& filename);
356370

@@ -361,8 +375,7 @@ class UrDriver
361375
std::unique_ptr<control::TrajectoryPointInterface> trajectory_interface_;
362376
std::unique_ptr<control::ScriptCommandInterface> script_command_interface_;
363377
std::unique_ptr<control::ScriptSender> script_sender_;
364-
std::unique_ptr<comm::URStream<primary_interface::PrimaryPackage>> primary_stream_;
365-
std::unique_ptr<comm::URStream<primary_interface::PrimaryPackage>> secondary_stream_;
378+
std::shared_ptr<primary_interface::PrimaryClient> primary_client_;
366379

367380
double servoj_time_;
368381
uint32_t servoj_gain_;

src/comm/tcp_socket.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ void TCPSocket::setOptions(int socket_fd)
5858
bool TCPSocket::setup(std::string& host, int port)
5959
{
6060
if (state_ == SocketState::Connected)
61+
{
62+
URCL_LOG_DEBUG("Socket already connected on: %s:%d", host.c_str(), port);
6163
return false;
64+
}
6265

6366
URCL_LOG_DEBUG("Setting up connection: %s:%d", host.c_str(), port);
6467

@@ -208,6 +211,5 @@ void TCPSocket::setReceiveTimeout(const timeval& timeout)
208211
setOptions(socket_fd_);
209212
}
210213
}
211-
212214
} // namespace comm
213215
} // namespace urcl

src/ur/ur_driver.cpp

Lines changed: 15 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@ static const std::string SCRIPT_COMMAND_PORT_REPLACE("{{SCRIPT_COMMAND_SERVER_PO
5353
urcl::UrDriver::UrDriver(const std::string& robot_ip, const std::string& script_file,
5454
const std::string& output_recipe_file, const std::string& input_recipe_file,
5555
std::function<void(bool)> handle_program_state, bool headless_mode,
56-
std::unique_ptr<ToolCommSetup> tool_comm_setup, const uint32_t reverse_port,
57-
const uint32_t script_sender_port, int servoj_gain, double servoj_lookahead_time,
58-
bool non_blocking_read, const std::string& reverse_ip, const uint32_t trajectory_port,
56+
std::unique_ptr<ToolCommSetup> tool_comm_setup, const std::string& calibration_checksum,
57+
const bool& simulated, const uint32_t reverse_port, const uint32_t script_sender_port,
58+
int servoj_gain, double servoj_lookahead_time, bool non_blocking_read,
59+
const std::string& reverse_ip, const uint32_t trajectory_port,
5960
const uint32_t script_command_port)
6061
: servoj_time_(0.008)
6162
, servoj_gain_(servoj_gain)
@@ -66,12 +67,9 @@ urcl::UrDriver::UrDriver(const std::string& robot_ip, const std::string& script_
6667
URCL_LOG_DEBUG("Initializing urdriver");
6768
URCL_LOG_DEBUG("Initializing RTDE client");
6869
rtde_client_.reset(new rtde_interface::RTDEClient(robot_ip_, notifier_, output_recipe_file, input_recipe_file));
69-
70-
primary_stream_.reset(
71-
new comm::URStream<primary_interface::PrimaryPackage>(robot_ip_, urcl::primary_interface::UR_PRIMARY_PORT));
72-
secondary_stream_.reset(
73-
new comm::URStream<primary_interface::PrimaryPackage>(robot_ip_, urcl::primary_interface::UR_SECONDARY_PORT));
74-
secondary_stream_->connect();
70+
URCL_LOG_DEBUG("Initializing Primary client");
71+
primary_client_.reset(new primary_interface::PrimaryClient(robot_ip, calibration_checksum));
72+
primary_client_->setSimulated(simulated);
7573

7674
non_blocking_read_ = non_blocking_read;
7775
get_packet_timeout_ = non_blocking_read_ ? 0 : 100;
@@ -179,31 +177,14 @@ urcl::UrDriver::UrDriver(const std::string& robot_ip, const std::string& script_
179177
urcl::UrDriver::UrDriver(const std::string& robot_ip, const std::string& script_file,
180178
const std::string& output_recipe_file, const std::string& input_recipe_file,
181179
std::function<void(bool)> handle_program_state, bool headless_mode,
182-
std::unique_ptr<ToolCommSetup> tool_comm_setup, const std::string& calibration_checksum,
183-
const uint32_t reverse_port, const uint32_t script_sender_port, int servoj_gain,
184-
double servoj_lookahead_time, bool non_blocking_read, const std::string& reverse_ip,
185-
const uint32_t trajectory_port, const uint32_t script_command_port)
180+
std::unique_ptr<ToolCommSetup> tool_comm_setup, const uint32_t reverse_port,
181+
const uint32_t script_sender_port, int servoj_gain, double servoj_lookahead_time,
182+
bool non_blocking_read, const std::string& reverse_ip, const uint32_t trajectory_port,
183+
const uint32_t script_command_port)
186184
: UrDriver(robot_ip, script_file, output_recipe_file, input_recipe_file, handle_program_state, headless_mode,
187-
std::move(tool_comm_setup), reverse_port, script_sender_port, servoj_gain, servoj_lookahead_time,
188-
non_blocking_read, reverse_ip, trajectory_port)
185+
std::move(tool_comm_setup), "", false, reverse_port, script_sender_port, servoj_gain,
186+
servoj_lookahead_time, non_blocking_read, reverse_ip, trajectory_port, script_command_port)
189187
{
190-
URCL_LOG_WARN("DEPRECATION NOTICE: Passing the calibration_checksum to the UrDriver's constructor has been "
191-
"deprecated. Instead, use the checkCalibration(calibration_checksum) function separately. This "
192-
"notice is for application developers using this library. If you are only using an application using "
193-
"this library, you can ignore this message.");
194-
if (checkCalibration(calibration_checksum))
195-
{
196-
URCL_LOG_INFO("Calibration checked successfully.");
197-
}
198-
else
199-
{
200-
URCL_LOG_ERROR("The calibration parameters of the connected robot don't match the ones from the given kinematics "
201-
"config file. Please be aware that this can lead to critical inaccuracies of tcp positions. Use "
202-
"the ur_calibration tool to extract the correct calibration from the robot and pass that into the "
203-
"description. See "
204-
"[https://github.com/UniversalRobots/Universal_Robots_ROS_Driver#extract-calibration-information] "
205-
"for details.");
206-
}
207188
}
208189

209190
std::unique_ptr<rtde_interface::DataPackage> urcl::UrDriver::getDataPackage()
@@ -339,27 +320,7 @@ std::string UrDriver::readScriptFile(const std::string& filename)
339320

340321
bool UrDriver::checkCalibration(const std::string& checksum)
341322
{
342-
if (primary_stream_ == nullptr)
343-
{
344-
throw std::runtime_error("checkCalibration() called without a primary interface connection being established.");
345-
}
346-
primary_interface::PrimaryParser parser;
347-
comm::URProducer<primary_interface::PrimaryPackage> prod(*primary_stream_, parser);
348-
prod.setupProducer();
349-
350-
CalibrationChecker consumer(checksum);
351-
352-
comm::INotifier notifier;
353-
354-
comm::Pipeline<primary_interface::PrimaryPackage> pipeline(prod, &consumer, "Pipeline", notifier);
355-
pipeline.run();
356-
357-
while (!consumer.isChecked())
358-
{
359-
std::this_thread::sleep_for(std::chrono::seconds(1));
360-
}
361-
URCL_LOG_DEBUG("Got calibration information from robot.");
362-
return consumer.checkSuccessful();
323+
return primary_client_->getCalibrationChecker()->checkCalibration(checksum);
363324
}
364325

365326
rtde_interface::RTDEWriter& UrDriver::getRTDEWriter()
@@ -369,29 +330,7 @@ rtde_interface::RTDEWriter& UrDriver::getRTDEWriter()
369330

370331
bool UrDriver::sendScript(const std::string& program)
371332
{
372-
if (secondary_stream_ == nullptr)
373-
{
374-
throw std::runtime_error("Sending script to robot requested while there is no primary interface established. "
375-
"This "
376-
"should not happen.");
377-
}
378-
379-
// urscripts (snippets) must end with a newline, or otherwise the controller's runtime will
380-
// not execute them. To avoid problems, we always just append a newline here, even if
381-
// there may already be one.
382-
auto program_with_newline = program + '\n';
383-
384-
size_t len = program_with_newline.size();
385-
const uint8_t* data = reinterpret_cast<const uint8_t*>(program_with_newline.c_str());
386-
size_t written;
387-
388-
if (secondary_stream_->write(data, len, written))
389-
{
390-
URCL_LOG_DEBUG("Sent program to robot:\n%s", program_with_newline.c_str());
391-
return true;
392-
}
393-
URCL_LOG_ERROR("Could not send program to robot");
394-
return false;
333+
return primary_client_->sendScript(program);
395334
}
396335

397336
bool UrDriver::sendRobotProgram()

0 commit comments

Comments
 (0)