Skip to content

Commit 7b57b66

Browse files
dkar-stodavid.karlsboeck
andauthored
Added ScriptCommandInterface command setTcpOffset (#444)
simply calls the urscript command set_tcp() --------- Co-authored-by: david.karlsboeck <david.karlsboeck@gmail.com>
1 parent 4dd918b commit 7b57b66

7 files changed

Lines changed: 132 additions & 1 deletion

File tree

include/ur_client_library/control/script_command_interface.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,15 @@ class ScriptCommandInterface : public ReverseInterface
167167
*/
168168
bool endToolContact();
169169

170+
/*!
171+
* \brief Set the TCP offset
172+
*
173+
* \param offset TCP offset as [x, y, z, rx, ry, rz] given in the flange coordinate system.
174+
*
175+
* \returns True, if the write was performed successfully, false otherwise.
176+
*/
177+
bool setTcpOffset(const vector6d_t& offset);
178+
170179
/*!
171180
* \brief Set friction compensation for the torque_command. If true the torque command will compensate for friction,
172181
* if false it will not.
@@ -238,6 +247,7 @@ class ScriptCommandInterface : public ReverseInterface
238247
SET_FRICTION_COMPENSATION = 7, ///< Set friction compensation
239248
FT_RTDE_INPUT_ENABLE = 8, ///< Enable FT RTDE input
240249
SET_GRAVITY = 9, ///< Set gravity vector
250+
SET_TCP_OFFSET = 10, ///< Set TCP offset
241251
};
242252

243253
/*!
@@ -259,7 +269,7 @@ class ScriptCommandInterface : public ReverseInterface
259269
bool robotVersionSupportsCommandOrWarn(const VersionInformation& min_polyscope5,
260270
const VersionInformation& min_polyscopeX, const std::string& command_name);
261271

262-
bool client_connected_;
272+
std::atomic<bool> client_connected_;
263273
static const int MAX_MESSAGE_LENGTH = 28;
264274

265275
std::function<void(ToolContactResult)> handle_tool_contact_result_;

include/ur_client_library/ur/ur_driver.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -763,6 +763,13 @@ class UrDriver
763763
*/
764764
bool endToolContact();
765765

766+
/*!
767+
* \brief Set the TCP offset of the robot.
768+
*
769+
* \returns True, if the write was performed successfully, false otherwise.
770+
*/
771+
bool setTcpOffset(const vector6d_t& tcp_offset);
772+
766773
/*!
767774
* \brief Set friction compensation for the torque_command. If true the torque command will compensate for friction,
768775
* if false it will not.

resources/external_control.urscript

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ END_TOOL_CONTACT = 6
5757
SET_FRICTION_COMPENSATION = 7
5858
FT_RTDE_INPUT_ENABLE = 8
5959
SET_GRAVITY = 9
60+
SET_TCP_OFFSET = 10
6061
SCRIPT_COMMAND_DATA_DIMENSION = 28
6162

6263
FREEDRIVE_MODE_START = 1
@@ -822,6 +823,9 @@ thread script_commands():
822823
# This has a known error that the resulting torques are computed with opposite sign.
823824
enable_external_ft_sensor(enabled, sensor_mass, sensor_offset, sensor_cog)
824825
{% endif %}
826+
elif command == SET_TCP_OFFSET:
827+
tcp_offset = p[raw_command[2] / MULT_jointstate, raw_command[3] / MULT_jointstate, raw_command[4] / MULT_jointstate, raw_command[5] / MULT_jointstate, raw_command[6] / MULT_jointstate, raw_command[7] / MULT_jointstate]
828+
set_tcp(tcp_offset)
825829
end
826830
end
827831
end

src/control/script_command_interface.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,32 @@ bool ScriptCommandInterface::ftRtdeInputEnable(const bool enabled, const double
318318
return server_.write(client_fd_, buffer, sizeof(buffer), written);
319319
}
320320

321+
bool ScriptCommandInterface::setTcpOffset(const vector6d_t& offset)
322+
{
323+
const int message_length = 7;
324+
uint8_t buffer[sizeof(int32_t) * MAX_MESSAGE_LENGTH];
325+
uint8_t* b_pos = buffer;
326+
327+
int32_t val = htobe32(toUnderlying(ScriptCommand::SET_TCP_OFFSET));
328+
b_pos += append(b_pos, val);
329+
330+
for (auto const& frame : offset)
331+
{
332+
val = htobe32(static_cast<int32_t>(round(frame * MULT_JOINTSTATE)));
333+
b_pos += append(b_pos, val);
334+
}
335+
336+
// writing zeros to allow usage with other script commands
337+
for (size_t i = message_length; i < MAX_MESSAGE_LENGTH; i++)
338+
{
339+
val = htobe32(0);
340+
b_pos += append(b_pos, val);
341+
}
342+
size_t written;
343+
344+
return server_.write(client_fd_, buffer, sizeof(buffer), written);
345+
}
346+
321347
bool ScriptCommandInterface::clientConnected()
322348
{
323349
return client_connected_;

src/ur/ur_driver.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,19 @@ bool UrDriver::ftRtdeInputEnable(const bool enabled, const double sensor_mass,
591591
}
592592
}
593593

594+
bool UrDriver::setTcpOffset(const vector6d_t& tcp_offset)
595+
{
596+
if (script_command_interface_->clientConnected())
597+
{
598+
return script_command_interface_->setTcpOffset(tcp_offset);
599+
}
600+
else
601+
{
602+
URCL_LOG_ERROR("Script command interface is not running. Unable to set TCP offset.");
603+
return false;
604+
}
605+
}
606+
594607
bool UrDriver::writeKeepalive(const RobotReceiveTimeout& robot_receive_timeout)
595608
{
596609
vector6d_t* fake = nullptr;

tests/test_script_command_interface.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,36 @@ TEST_F(ScriptCommandInterfaceTest, test_set_gravity)
511511
EXPECT_EQ(message_sum, expected_message_sum);
512512
}
513513

514+
TEST_F(ScriptCommandInterfaceTest, test_set_tcp_offset)
515+
{
516+
// Wait for the client to connect to the server
517+
waitForClientConnection();
518+
519+
vector6d_t tcp_offset = { 0.1, 0.2, 0.3, 0.4, 0.5, 0.6 };
520+
script_command_interface_->setTcpOffset(tcp_offset);
521+
522+
int32_t command;
523+
std::vector<int32_t> message;
524+
client_->readMessage(command, message);
525+
526+
// 10 is set tcp offset
527+
int32_t expected_command = 10;
528+
EXPECT_EQ(command, expected_command);
529+
530+
// Test tcp offset
531+
vector6d_t received_tcp_offset;
532+
for (unsigned int i = 0; i < tcp_offset.size(); ++i)
533+
{
534+
received_tcp_offset[i] = static_cast<double>(message[i]) / script_command_interface_->MULT_JOINTSTATE;
535+
EXPECT_EQ(received_tcp_offset[i], tcp_offset[i]);
536+
}
537+
538+
// The rest of the message should be zero
539+
int32_t message_sum = std::accumulate(std::begin(message) + 6, std::end(message), 0);
540+
int32_t expected_message_sum = 0;
541+
EXPECT_EQ(message_sum, expected_message_sum);
542+
}
543+
514544
int main(int argc, char* argv[])
515545
{
516546
::testing::InitGoogleTest(&argc, argv);

tests/test_ur_driver.cpp

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <ur_client_library/ur/ur_driver.h>
3535
#include <ur_client_library/example_robot_wrapper.h>
3636
#include <algorithm>
37+
#include <thread>
3738
#include "test_utils.h"
3839

3940
using namespace urcl;
@@ -331,6 +332,46 @@ TEST_F(UrDriverTest, read_error_code)
331332
EXPECT_NO_THROW(g_my_robot->getPrimaryClient()->commandUnlockProtectiveStop());
332333
}
333334

335+
TEST_F(UrDriverTest, set_tcp_offset)
336+
{
337+
ASSERT_TRUE(g_my_robot->getUrDriver()->setTcpOffset({ 0, 0, 0, 0, 0, 0 }));
338+
339+
vector6d_t tcp_offset = { 0.1, 0.2, 0.3, 0.4, 0.5, 0.6 };
340+
ASSERT_TRUE(g_my_robot->getUrDriver()->setTcpOffset(tcp_offset));
341+
342+
rtde_interface::DataPackage data_pkg(g_my_robot->getUrDriver()->getRTDEOutputRecipe());
343+
vector6d_t tcp_offset_received;
344+
for (int i = 0; i < 10; ++i)
345+
{
346+
ASSERT_TRUE(g_my_robot->getUrDriver()->getDataPackage(data_pkg));
347+
348+
data_pkg.getData("tcp_offset", tcp_offset_received);
349+
if (tcp_offset_received == tcp_offset)
350+
{
351+
break;
352+
}
353+
std::this_thread::sleep_for(std::chrono::milliseconds(2));
354+
}
355+
g_my_robot->getUrDriver()->getDataPackage(data_pkg);
356+
for (unsigned int i = 0; i < tcp_offset.size(); ++i)
357+
{
358+
EXPECT_DOUBLE_EQ(tcp_offset_received[i], tcp_offset[i]);
359+
}
360+
361+
// Stop program on robot
362+
g_my_robot->getUrDriver()->stopControl();
363+
g_my_robot->waitForProgramNotRunning(1000);
364+
365+
// TODO (feex): We cannot see from the outside whether the script command interface is connected. There is a race
366+
// condition between the reverse interface disconnection callback and the script_command_interface disconnect
367+
// callback. For now, we will just have to wait a bit to ensure that the script command interface
368+
// is disconnected before trying to use the script command interface.
369+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
370+
371+
// Check that we can't set TCP offset when the program isn't running
372+
ASSERT_FALSE(g_my_robot->getUrDriver()->setTcpOffset(tcp_offset));
373+
}
374+
334375
TEST(UrDriverInitTest, setting_connection_limits_works_correctly)
335376
{
336377
UrDriverConfiguration config;

0 commit comments

Comments
 (0)