Skip to content

Commit 28d3d4c

Browse files
committed
Added command to set friction compensation for the torque command
1 parent ebe30d2 commit 28d3d4c

File tree

6 files changed

+113
-7
lines changed

6 files changed

+113
-7
lines changed

include/ur_client_library/control/script_command_interface.h

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,17 @@ class ScriptCommandInterface : public ReverseInterface
147147
*/
148148
bool endToolContact();
149149

150+
/*!
151+
* \brief Set friction compensation for the torque_command. If true the torque command will compensate for friction,
152+
* if false it will not.
153+
*
154+
* \param friction_compesation Will set a friction_compensation variable in urscript, which will be used when calling
155+
* torque_command
156+
*
157+
* \returns True, if the write was performed successfully, false otherwise.
158+
*/
159+
bool setFrictionCompensation(const bool friction_compensation);
160+
150161
/*!
151162
* \brief Returns whether a client/robot is connected to this server.
152163
*
@@ -177,13 +188,14 @@ class ScriptCommandInterface : public ReverseInterface
177188
enum class ScriptCommand : int32_t
178189
{
179190

180-
ZERO_FTSENSOR = 0, ///< Zero force torque sensor
181-
SET_PAYLOAD = 1, ///< Set payload
182-
SET_TOOL_VOLTAGE = 2, ///< Set tool voltage
183-
START_FORCE_MODE = 3, ///< Start force mode
184-
END_FORCE_MODE = 4, ///< End force mode
185-
START_TOOL_CONTACT = 5, ///< Start detecting tool contact
186-
END_TOOL_CONTACT = 6, ///< End detecting tool contact
191+
ZERO_FTSENSOR = 0, ///< Zero force torque sensor
192+
SET_PAYLOAD = 1, ///< Set payload
193+
SET_TOOL_VOLTAGE = 2, ///< Set tool voltage
194+
START_FORCE_MODE = 3, ///< Start force mode
195+
END_FORCE_MODE = 4, ///< End force mode
196+
START_TOOL_CONTACT = 5, ///< Start detecting tool contact
197+
END_TOOL_CONTACT = 6, ///< End detecting tool contact
198+
SET_FRICTION_COMPENSATION = 7, ///< Set friction compensation
187199
};
188200

189201
bool client_connected_;

include/ur_client_library/ur/ur_driver.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,17 @@ class UrDriver
714714
*/
715715
bool endToolContact();
716716

717+
/*!
718+
* \brief Set friction compensation for the torque_command. If true the torque command will compensate for friction,
719+
* if false it will not.
720+
*
721+
* \param friction_compesation Will set a friction_compensation variable in urscript, which will be used when calling
722+
* torque_command
723+
*
724+
* \returns True, if the write was performed successfully, false otherwise.
725+
*/
726+
bool setFrictionCompensation(const bool friction_compensation);
727+
717728
/*!
718729
* \brief Write a keepalive signal only.
719730
*

resources/external_control.urscript

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ START_FORCE_MODE = 3
4949
END_FORCE_MODE = 4
5050
START_TOOL_CONTACT = 5
5151
END_TOOL_CONTACT = 6
52+
SET_FRICTION_COMPENSATION = 7
5253
SCRIPT_COMMAND_DATA_DIMENSION = 28
5354

5455
FREEDRIVE_MODE_START = 1
@@ -81,6 +82,7 @@ global spline_qdd = [0, 0, 0, 0, 0, 0]
8182
global spline_qd = [0, 0, 0, 0, 0, 0]
8283
global tool_contact_running = False
8384
global trajectory_result = 0
85+
global friction_compensation = True
8486

8587
# Global thread variables
8688
thread_move = 0
@@ -691,6 +693,12 @@ thread script_commands():
691693
socket_send_int(UNTIL_TOOL_CONTACT_RESULT_CANCELED, "script_command_socket")
692694
end
693695
tool_contact_running = False
696+
elif command == SET_FRICTION_COMPENSATION:
697+
if raw_command[2] == 0:
698+
friction_compensation = False
699+
else:
700+
friction_compensation = True
701+
end
694702
end
695703
end
696704
end

src/control/script_command_interface.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,29 @@ bool ScriptCommandInterface::endToolContact()
222222
return server_.write(client_fd_, buffer, sizeof(buffer), written);
223223
}
224224

225+
bool ScriptCommandInterface::setFrictionCompensation(const bool friction_compensation)
226+
{
227+
const int message_length = 2;
228+
uint8_t buffer[sizeof(int32_t) * MAX_MESSAGE_LENGTH];
229+
uint8_t* b_pos = buffer;
230+
231+
int32_t val = htobe32(toUnderlying(ScriptCommand::SET_FRICTION_COMPENSATION));
232+
b_pos += append(b_pos, val);
233+
234+
val = htobe32(friction_compensation);
235+
b_pos += append(b_pos, val);
236+
237+
// writing zeros to allow usage with other script commands
238+
for (size_t i = message_length; i < MAX_MESSAGE_LENGTH; i++)
239+
{
240+
val = htobe32(0);
241+
b_pos += append(b_pos, val);
242+
}
243+
size_t written;
244+
245+
return server_.write(client_fd_, buffer, sizeof(buffer), written);
246+
}
247+
225248
bool ScriptCommandInterface::clientConnected()
226249
{
227250
return client_connected_;

src/ur/ur_driver.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,6 +527,19 @@ bool UrDriver::endToolContact()
527527
}
528528
}
529529

530+
bool UrDriver::setFrictionCompensation(const bool friction_compensation)
531+
{
532+
if (script_command_interface_->clientConnected())
533+
{
534+
return script_command_interface_->setFrictionCompensation(friction_compensation);
535+
}
536+
else
537+
{
538+
URCL_LOG_ERROR("Script command interface is not running. Unable to set friction compensation.");
539+
return 0;
540+
}
541+
}
542+
530543
bool UrDriver::writeKeepalive(const RobotReceiveTimeout& robot_receive_timeout)
531544
{
532545
vector6d_t* fake = nullptr;

tests/test_script_command_interface.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,45 @@ TEST_F(ScriptCommandInterfaceTest, test_tool_contact_callback)
382382
EXPECT_EQ(toUnderlying(received_result_), toUnderlying(send_result));
383383
}
384384

385+
TEST_F(ScriptCommandInterfaceTest, test_set_friction_compensation)
386+
{
387+
// Wait for the client to connect to the server
388+
waitForClientConnection();
389+
390+
script_command_interface_->setFrictionCompensation(true);
391+
392+
int32_t command;
393+
std::vector<int32_t> message;
394+
client_->readMessage(command, message);
395+
396+
// 7 is set friction compensation
397+
int32_t expected_command = 7;
398+
EXPECT_EQ(command, expected_command);
399+
400+
int32_t expected_friction_compensation = 1;
401+
EXPECT_EQ(message[0], expected_friction_compensation);
402+
403+
// The rest of the message should be zero
404+
int32_t message_sum = std::accumulate(std::begin(message) + 1, std::end(message), 0);
405+
int32_t expected_message_sum = 0;
406+
EXPECT_EQ(message_sum, expected_message_sum);
407+
408+
script_command_interface_->setFrictionCompensation(false);
409+
410+
message.clear();
411+
client_->readMessage(command, message);
412+
413+
EXPECT_EQ(command, expected_command);
414+
415+
expected_friction_compensation = 0;
416+
EXPECT_EQ(message[0], expected_friction_compensation);
417+
418+
// The rest of the message should be zero
419+
message_sum = std::accumulate(std::begin(message) + 1, std::end(message), 0);
420+
expected_message_sum = 0;
421+
EXPECT_EQ(message_sum, expected_message_sum);
422+
}
423+
385424
int main(int argc, char* argv[])
386425
{
387426
::testing::InitGoogleTest(&argc, argv);

0 commit comments

Comments
 (0)