Skip to content

Commit 6421cfc

Browse files
author
Felix Exner
committed
Allow resetting the RTDE client
1 parent 65b0e9d commit 6421cfc

File tree

2 files changed

+67
-12
lines changed

2 files changed

+67
-12
lines changed

include/ur_client_library/ur/ur_driver.h

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ class UrDriver
195195

196196
uint32_t getControlFrequency() const
197197
{
198-
return rtde_frequency_;
198+
return rtde_client_->getTargetFrequency();
199199
}
200200

201201
/*!
@@ -487,10 +487,42 @@ class UrDriver
487487
script_command_interface_->setToolContactResultCallback(tool_contact_result_cb);
488488
}
489489

490+
/**
491+
* \brief Reset the RTDE client. As during initialization the driver will start RTDE communication
492+
* with the maximum available frequency, this enables users to start RTDE communication with a
493+
* custom rate.
494+
*
495+
* Note: After calling this function startRTDECommunication() has to be called again to actually
496+
* start RTDE communication.
497+
*
498+
* \param output_recipe Vector containing the output recipe
499+
* \param input_recipe Vector containing the input recipe
500+
* \param target_frequency Frequency to run the RTDE client at. Defaults to 0.0 which means maximum frequency.
501+
*/
502+
void resetRTDEClient(const std::vector<std::string>& output_recipe, const std::vector<std::string>& input_recipe,
503+
double target_frequency = 0.0);
504+
505+
/**
506+
* \brief Reset the RTDE client. As during initialization the driver will start RTDE communication
507+
* with the maximum available frequency, this enables users to start RTDE communication with a
508+
* custom rate.
509+
*
510+
* Note: After calling this function startRTDECommunication() has to be called again to actually
511+
* start RTDE communication.
512+
*
513+
* \param output_recipe_filename Filename where the output recipe is stored in.
514+
* \param input_recipe_filename Filename where the input recipe is stored in.
515+
* \param target_frequency Frequency to run the RTDE client at. Defaults to 0.0 which means maximum frequency.
516+
*/
517+
void resetRTDEClient(const std::string& output_recipe_filename, const std::string& input_recipe_filename,
518+
double target_frequency = 0.0);
519+
490520
private:
491521
static std::string readScriptFile(const std::string& filename);
492522

493-
int rtde_frequency_;
523+
void initRTDE();
524+
void setupReverseInterface(const uint32_t reverse_port);
525+
494526
comm::INotifier notifier_;
495527
std::unique_ptr<rtde_interface::RTDEClient> rtde_client_;
496528
std::unique_ptr<control::ReverseInterface> reverse_interface_;
@@ -502,7 +534,6 @@ class UrDriver
502534

503535
uint32_t servoj_gain_;
504536
double servoj_lookahead_time_;
505-
std::chrono::milliseconds step_time_;
506537

507538
std::function<void(bool)> handle_program_state_;
508539

src/ur/ur_driver.cpp

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,6 @@ urcl::UrDriver::UrDriver(const std::string& robot_ip, const std::string& script_
6161
const uint32_t script_command_port, double force_mode_damping, double force_mode_gain_scaling)
6262
: servoj_gain_(servoj_gain)
6363
, servoj_lookahead_time_(servoj_lookahead_time)
64-
, step_time_(std::chrono::milliseconds(8))
6564
, handle_program_state_(handle_program_state)
6665
, robot_ip_(robot_ip)
6766
{
@@ -78,13 +77,8 @@ urcl::UrDriver::UrDriver(const std::string& robot_ip, const std::string& script_
7877
non_blocking_read_ = non_blocking_read;
7978
get_packet_timeout_ = non_blocking_read_ ? 0 : 100;
8079

81-
if (!rtde_client_->init())
82-
{
83-
throw UrException("Initialization of RTDE client went wrong.");
84-
}
85-
86-
rtde_frequency_ = rtde_client_->getMaxFrequency();
87-
step_time_ = std::chrono::milliseconds(1000 / rtde_frequency_);
80+
initRTDE();
81+
setupReverseInterface(reverse_port);
8882

8983
// Figure out the ip automatically if the user didn't provide it
9084
std::string local_ip = reverse_ip.empty() ? rtde_client_->getIP() : reverse_ip;
@@ -210,7 +204,6 @@ urcl::UrDriver::UrDriver(const std::string& robot_ip, const std::string& script_
210204
URCL_LOG_DEBUG("Created script sender");
211205
}
212206

213-
reverse_interface_.reset(new control::ReverseInterface(reverse_port, handle_program_state, step_time_));
214207
trajectory_interface_.reset(new control::TrajectoryPointInterface(trajectory_port));
215208
script_command_interface_.reset(new control::ScriptCommandInterface(script_command_port));
216209

@@ -593,4 +586,35 @@ void UrDriver::setKeepaliveCount(const uint32_t count)
593586
reverse_interface_->setKeepaliveCount(count);
594587
}
595588

589+
void UrDriver::resetRTDEClient(const std::vector<std::string>& output_recipe,
590+
const std::vector<std::string>& input_recipe, double target_frequency)
591+
{
592+
rtde_client_.reset(
593+
new rtde_interface::RTDEClient(robot_ip_, notifier_, output_recipe, input_recipe, target_frequency));
594+
initRTDE();
595+
}
596+
597+
void UrDriver::resetRTDEClient(const std::string& output_recipe_filename, const std::string& input_recipe_filename,
598+
double target_frequency)
599+
{
600+
rtde_client_.reset(new rtde_interface::RTDEClient(robot_ip_, notifier_, output_recipe_filename, input_recipe_filename,
601+
target_frequency));
602+
initRTDE();
603+
}
604+
605+
void UrDriver::initRTDE()
606+
{
607+
if (!rtde_client_->init())
608+
{
609+
throw UrException("Initialization of RTDE client went wrong.");
610+
}
611+
}
612+
613+
void UrDriver::setupReverseInterface(const uint32_t reverse_port)
614+
{
615+
auto rtde_frequency = rtde_client_->getTargetFrequency();
616+
auto step_time = std::chrono::milliseconds(static_cast<int>(1000 / rtde_frequency));
617+
reverse_interface_.reset(new control::ReverseInterface(reverse_port, handle_program_state_, step_time));
618+
}
619+
596620
} // namespace urcl

0 commit comments

Comments
 (0)