Skip to content

Commit c35be11

Browse files
Allow updating robot gravity (#415)
These changes expose the `set_gravity` URScript functionality through the `Client Library`, allowing a user to update the gravity vector of the robot during live operation. --------- Signed-off-by: AdamPettinger <[email protected]>
1 parent 0c96c75 commit c35be11

File tree

7 files changed

+111
-0
lines changed

7 files changed

+111
-0
lines changed

doc/architecture/script_command_interface.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ At the time of writing the ``ScriptCommandInterface`` provides the following fun
2222
for more information.
2323
- ``setFrictionCompensation()``: Set friction compensation for torque command.
2424
- ``ftRtdeInputEnable()``: Enable/disable FT RTDE input processing.
25+
- ``setGravity()``: Set the gravity vector for the robot.
2526

2627
Communication protocol
2728
----------------------
@@ -52,6 +53,7 @@ The robot reads from the "script_command_socket" expecting a 32 bit integer repr
5253
- 6: endToolContact
5354
- 7: setFrictionCompensation
5455
- 8: ftRtdeInputEnable
56+
- 9: setGravity
5557
1-27 data fields specific to the command
5658
===== =====
5759

@@ -146,6 +148,15 @@ The robot reads from the "script_command_socket" expecting a 32 bit integer repr
146148
6-9 sensor_cog in m, displacement from the tool flange (3d floating point)
147149
===== =====
148150

151+
.. table:: With setGravity command
152+
:widths: auto
153+
154+
===== =====
155+
index meaning
156+
===== =====
157+
1-3 The gravity vector (towards the Earth's center), represented in robot's base frame (floating point)
158+
===== =====
159+
149160
.. note::
150161
In URScript the ``socket_read_binary_integer()`` function is used to read the data from the
151162
script command socket. The first index in that function's return value is the number of integers read,

include/ur_client_library/control/script_command_interface.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,16 @@ class ScriptCommandInterface : public ReverseInterface
9191
*/
9292
bool setPayload(const double mass, const vector3d_t* cog);
9393

94+
/*!
95+
* \brief Set the gravity vector
96+
*
97+
* \param gravity Gravity, a vector [x, y, z] specifying the gravity vector (pointing towards
98+
* the Earth's center) given in the robot's base frame
99+
*
100+
* \returns True, if the write was performed successfully, false otherwise.
101+
*/
102+
bool setGravity(const vector3d_t* gravity);
103+
94104
/*!
95105
* \brief Set the tool voltage.
96106
*
@@ -227,6 +237,7 @@ class ScriptCommandInterface : public ReverseInterface
227237
END_TOOL_CONTACT = 6, ///< End detecting tool contact
228238
SET_FRICTION_COMPENSATION = 7, ///< Set friction compensation
229239
FT_RTDE_INPUT_ENABLE = 8, ///< Enable FT RTDE input
240+
SET_GRAVITY = 9, ///< Set gravity vector
230241
};
231242

232243
/*!

include/ur_client_library/ur/ur_driver.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,17 @@ class UrDriver
596596
*/
597597
bool setPayload(const float mass, const vector3d_t& cog);
598598

599+
/*!
600+
* \brief Set the gravity vector. Note: It requires the external control script to be running or
601+
* the robot to be in headless mode.
602+
*
603+
* \param gravity Gravity, a vector [x, y, z] specifying the gravity vector (pointing towards
604+
* the Earth's center) given in the robot's base frame
605+
*
606+
* \returns True on successful write.
607+
*/
608+
bool setGravity(const vector3d_t& gravity);
609+
599610
/*!
600611
* \brief Set the tool voltage. Note: It requires the external control script to be running or the robot to be in
601612
* headless mode.

resources/external_control.urscript

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ START_TOOL_CONTACT = 5
5656
END_TOOL_CONTACT = 6
5757
SET_FRICTION_COMPENSATION = 7
5858
FT_RTDE_INPUT_ENABLE = 8
59+
SET_GRAVITY = 9
5960
SCRIPT_COMMAND_DATA_DIMENSION = 28
6061

6162
FREEDRIVE_MODE_START = 1
@@ -756,6 +757,9 @@ thread script_commands():
756757
mass = raw_command[2] / MULT_jointstate
757758
cog = [raw_command[3] / MULT_jointstate, raw_command[4] / MULT_jointstate, raw_command[5] / MULT_jointstate]
758759
set_payload(mass, cog)
760+
elif command == SET_GRAVITY:
761+
gravity = [raw_command[2] / MULT_jointstate, raw_command[3] / MULT_jointstate, raw_command[4] / MULT_jointstate]
762+
set_gravity(gravity)
759763
elif command == SET_TOOL_VOLTAGE:
760764
tool_voltage = raw_command[2] / MULT_jointstate
761765
set_tool_voltage(tool_voltage)

src/control/script_command_interface.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,31 @@ bool ScriptCommandInterface::setPayload(const double mass, const vector3d_t* cog
8989
return server_.write(client_fd_, buffer, sizeof(buffer), written);
9090
}
9191

92+
bool ScriptCommandInterface::setGravity(const vector3d_t* gravity)
93+
{
94+
const int message_length = 4;
95+
uint8_t buffer[sizeof(int32_t) * MAX_MESSAGE_LENGTH];
96+
uint8_t* b_pos = buffer;
97+
int32_t val = htobe32(toUnderlying(ScriptCommand::SET_GRAVITY));
98+
b_pos += append(b_pos, val);
99+
100+
for (auto const& grav_component : *gravity)
101+
{
102+
val = htobe32(static_cast<int32_t>(round(grav_component * MULT_JOINTSTATE)));
103+
b_pos += append(b_pos, val);
104+
}
105+
106+
// writing zeros to allow usage with other script commands
107+
for (size_t i = message_length; i < MAX_MESSAGE_LENGTH; i++)
108+
{
109+
val = htobe32(0);
110+
b_pos += append(b_pos, val);
111+
}
112+
size_t written;
113+
114+
return server_.write(client_fd_, buffer, sizeof(buffer), written);
115+
}
116+
92117
bool ScriptCommandInterface::setToolVoltage(const ToolVoltage voltage)
93118
{
94119
const int message_length = 2;

src/ur/ur_driver.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,26 @@ bool UrDriver::setPayload(const float mass, const vector3d_t& cog)
298298
}
299299
}
300300

301+
bool UrDriver::setGravity(const vector3d_t& gravity)
302+
{
303+
if (script_command_interface_->clientConnected())
304+
{
305+
return script_command_interface_->setGravity(&gravity);
306+
}
307+
else
308+
{
309+
URCL_LOG_WARN("Script command interface is not running. Falling back to sending plain script code. On e-Series "
310+
"robots this will only work, if the robot is in remote_control mode.");
311+
std::stringstream cmd;
312+
cmd.imbue(std::locale::classic()); // Make sure, decimal divider is actually '.'
313+
cmd << "sec setup():" << std::endl
314+
<< " set_gravity([" << gravity[0] << ", " << gravity[1] << ", " << gravity[2] << "])" << std::endl
315+
<< "end";
316+
return sendScript(cmd.str());
317+
return true;
318+
}
319+
}
320+
301321
bool UrDriver::setToolVoltage(const ToolVoltage voltage)
302322
{
303323
// Test that the tool voltage is either 0, 12 or 24.

tests/test_script_command_interface.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,35 @@ TEST_F(ScriptCommandInterfaceTest, test_ft_rtde_input_enable)
482482
EXPECT_EQ(received_enabled, false);
483483
}
484484

485+
TEST_F(ScriptCommandInterfaceTest, test_set_gravity)
486+
{
487+
// Wait for the client to connect to the server
488+
waitForClientConnection();
489+
490+
vector3d_t gravity = { 0.1, 0.2, -9.81 };
491+
script_command_interface_->setGravity(&gravity);
492+
int32_t command;
493+
std::vector<int32_t> message;
494+
client_->readMessage(command, message);
495+
496+
// 9 is set gravity
497+
int32_t expected_command = 9;
498+
EXPECT_EQ(command, expected_command);
499+
500+
// Test gravity
501+
vector3d_t received_gravity;
502+
for (unsigned int i = 0; i < gravity.size(); ++i)
503+
{
504+
received_gravity[i] = (double)message[i] / script_command_interface_->MULT_JOINTSTATE;
505+
EXPECT_EQ(received_gravity[i], gravity[i]);
506+
}
507+
508+
// The rest of the message should be zero
509+
int32_t message_sum = std::accumulate(std::begin(message) + 3, std::end(message), 0);
510+
int32_t expected_message_sum = 0;
511+
EXPECT_EQ(message_sum, expected_message_sum);
512+
}
513+
485514
int main(int argc, char* argv[])
486515
{
487516
::testing::InitGoogleTest(&argc, argv);

0 commit comments

Comments
 (0)