Skip to content

Commit a56451a

Browse files
committed
Added a simple trajecotory point example
1 parent f479a05 commit a56451a

File tree

2 files changed

+218
-0
lines changed

2 files changed

+218
-0
lines changed

examples/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,8 @@ add_executable(script_sender_example
5959
script_sender.cpp)
6060
target_compile_options(script_sender_example PUBLIC ${CXX17_FLAG})
6161
target_link_libraries(script_sender_example ur_client_library::urcl)
62+
63+
add_executable(trajectory_point_interface_example
64+
trajectory_point_interface.cpp)
65+
target_compile_options(trajectory_point_interface_example PUBLIC ${CXX17_FLAG})
66+
target_link_libraries(trajectory_point_interface_example ur_client_library::urcl)
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
// -- BEGIN LICENSE BLOCK ----------------------------------------------
2+
// Copyright 2024 Universal Robots A/S
3+
//
4+
// Redistribution and use in source and binary forms, with or without
5+
// modification, are permitted provided that the following conditions are met:
6+
//
7+
// * Redistributions of source code must retain the above copyright
8+
// notice, this list of conditions and the following disclaimer.
9+
//
10+
// * Redistributions in binary form must reproduce the above copyright
11+
// notice, this list of conditions and the following disclaimer in the
12+
// documentation and/or other materials provided with the distribution.
13+
//
14+
// * Neither the name of the {copyright_holder} nor the names of its
15+
// contributors may be used to endorse or promote products derived from
16+
// this software without specific prior written permission.
17+
//
18+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19+
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20+
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21+
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22+
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23+
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24+
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25+
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26+
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27+
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28+
// POSSIBILITY OF SUCH DAMAGE.
29+
// -- END LICENSE BLOCK ------------------------------------------------
30+
31+
#include <chrono>
32+
#include <string>
33+
#include <thread>
34+
35+
#include "ur_client_library/types.h"
36+
#include "ur_client_library/ur/ur_driver.h"
37+
#include "ur_client_library/log.h"
38+
#include "ur_client_library/control/trajectory_point_interface.h"
39+
#include "ur_client_library/ur/dashboard_client.h"
40+
41+
const std::string DEFAULT_ROBOT_IP = "192.168.56.101";
42+
const std::string SCRIPT_FILE = "resources/external_control.urscript";
43+
const std::string OUTPUT_RECIPE = "examples/resources/rtde_output_recipe.txt";
44+
const std::string INPUT_RECIPE = "examples/resources/rtde_input_recipe.txt";
45+
const std::string CALIBRATION_CHECKSUM = "calib_12788084448423163542";
46+
47+
std::unique_ptr<urcl::DashboardClient> g_my_dashboard;
48+
std::unique_ptr<urcl::UrDriver> g_my_driver;
49+
50+
std::atomic<bool> g_trajectory_done = false;
51+
52+
// We need a callback function to register. See UrDriver's parameters for details.
53+
void handleRobotProgramState(bool program_running)
54+
{
55+
// Print the text in green so we see it better
56+
std::cout << "\033[1;32mProgram running: " << std::boolalpha << program_running << "\033[0m\n" << std::endl;
57+
}
58+
59+
void trajDoneCallback(const urcl::control::TrajectoryResult& result)
60+
{
61+
URCL_LOG_INFO("Trajectory done with result %d", result);
62+
;
63+
g_trajectory_done = true;
64+
}
65+
66+
int main(int argc, char* argv[])
67+
{
68+
urcl::setLogLevel(urcl::LogLevel::INFO);
69+
// Parse the ip arguments if given
70+
std::string robot_ip = DEFAULT_ROBOT_IP;
71+
if (argc > 1)
72+
{
73+
robot_ip = std::string(argv[1]);
74+
}
75+
76+
// --------------- INITIALIZATION BEGIN -------------------
77+
// Making the robot ready for the program by:
78+
// Connect the robot Dashboard
79+
g_my_dashboard.reset(new urcl::DashboardClient(robot_ip));
80+
if (!g_my_dashboard->connect())
81+
{
82+
URCL_LOG_ERROR("Could not connect to dashboard");
83+
return 1;
84+
}
85+
86+
// // Stop program, if there is one running
87+
if (!g_my_dashboard->commandStop())
88+
{
89+
URCL_LOG_ERROR("Could not send stop program command");
90+
return 1;
91+
}
92+
93+
// Power it off
94+
if (!g_my_dashboard->commandPowerOff())
95+
{
96+
URCL_LOG_ERROR("Could not send Power off command");
97+
return 1;
98+
}
99+
100+
// Power it on
101+
if (!g_my_dashboard->commandPowerOn())
102+
{
103+
URCL_LOG_ERROR("Could not send Power on command");
104+
return 1;
105+
}
106+
107+
// Release the brakes
108+
if (!g_my_dashboard->commandBrakeRelease())
109+
{
110+
URCL_LOG_ERROR("Could not send BrakeRelease command");
111+
return 1;
112+
}
113+
114+
std::unique_ptr<urcl::ToolCommSetup> tool_comm_setup;
115+
const bool headless = true;
116+
g_my_driver.reset(new urcl::UrDriver(robot_ip, SCRIPT_FILE, OUTPUT_RECIPE, INPUT_RECIPE, &handleRobotProgramState,
117+
headless, std::move(tool_comm_setup), CALIBRATION_CHECKSUM));
118+
// --------------- INITIALIZATION END -------------------
119+
120+
g_my_driver->registerTrajectoryDoneCallback(&trajDoneCallback);
121+
122+
URCL_LOG_INFO("Running MoveJ motion");
123+
// --------------- MOVEJ TRAJECTORY -------------------
124+
{
125+
g_trajectory_done = false;
126+
// Trajectory definition
127+
std::vector<urcl::vector6d_t> points{ { -1.57, -1.57, 0, 0, 0, 0 }, { -1.57, -1.6, 1.6, -0.7, 0.7, 0.2 } };
128+
std::vector<double> motion_durations{ 5.0, 2.0 };
129+
std::vector<double> blend_radii{ 0.1, 0.1 };
130+
131+
// Trajectory execution
132+
g_my_driver->writeTrajectoryControlMessage(urcl::control::TrajectoryControlMessage::TRAJECTORY_START,
133+
points.size() * 2);
134+
for (size_t i = 0; i < points.size(); i++)
135+
{
136+
g_my_driver->writeTrajectoryPoint(points[i], false, motion_durations[i], blend_radii[i]);
137+
}
138+
139+
// Same motion, but parametrized with acceleration and velocity
140+
motion_durations = { 0.0, 0.0 };
141+
for (size_t i = 0; i < points.size(); i++)
142+
{
143+
g_my_driver->writeTrajectoryPoint(points[i], 2.0, 2.0, false, motion_durations[i], blend_radii[i]);
144+
}
145+
146+
while (!g_trajectory_done)
147+
{
148+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
149+
g_my_driver->writeTrajectoryControlMessage(urcl::control::TrajectoryControlMessage::TRAJECTORY_NOOP);
150+
}
151+
}
152+
// --------------- END MOVEJ TRAJECTORY -------------------
153+
154+
URCL_LOG_INFO("Running MoveL motion");
155+
// --------------- MOVEL TRAJECTORY -------------------
156+
{
157+
g_trajectory_done = false;
158+
// Trajectory definition
159+
std::vector<urcl::vector6d_t> points{ { -0.203, 0.263, 0.559, 0.68, -1.083, -2.076 },
160+
{ -0.203, 0.463, 0.559, 0.68, -1.083, -2.076 } };
161+
std::vector<double> motion_durations{ 5.0, 5.0 };
162+
std::vector<double> blend_radii{ 0.0, 0.0 };
163+
164+
// Trajectory execution
165+
g_my_driver->writeTrajectoryControlMessage(urcl::control::TrajectoryControlMessage::TRAJECTORY_START,
166+
points.size());
167+
for (size_t i = 0; i < points.size(); i++)
168+
{
169+
// setting the cartesian parameter makes it interpret the 6d vector as a pose and use movel
170+
g_my_driver->writeTrajectoryPoint(points[i], true, motion_durations[i], blend_radii[i]);
171+
}
172+
173+
while (!g_trajectory_done)
174+
{
175+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
176+
g_my_driver->writeTrajectoryControlMessage(urcl::control::TrajectoryControlMessage::TRAJECTORY_NOOP);
177+
}
178+
}
179+
// --------------- END MOVEL TRAJECTORY -------------------
180+
181+
URCL_LOG_INFO("Running a spline motion");
182+
// --------------- SPLINE TRAJECTORY -------------------
183+
{
184+
g_trajectory_done = false;
185+
// Trajectory definition
186+
std::vector<urcl::vector6d_t> positions{ { -1.57, -1.57, 0, 0, 0, 0 },
187+
{ -1.57, -1.57, -1.57, 0, 0, 0 },
188+
{ -1.57, -1.57, 0, 0, 0, 0 },
189+
{ -1.57, -1.6, 1.6, -0.7, 0.7, 0.2 } };
190+
std::vector<urcl::vector6d_t> velocities{
191+
{ 0, 0, 0.0, 0, 0, 0 }, { 0, 0, 0.0, 0, 0, 0 }, { 0, 0, 1.5, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0 }
192+
};
193+
std::vector<double> motion_durations{ 3.0, 3.0, 3.0, 3.0 };
194+
195+
// Trajectory execution
196+
g_my_driver->writeTrajectoryControlMessage(urcl::control::TrajectoryControlMessage::TRAJECTORY_START,
197+
positions.size());
198+
for (size_t i = 0; i < positions.size(); i++)
199+
{
200+
g_my_driver->writeTrajectorySplinePoint(positions[i], velocities[i], motion_durations[i]);
201+
}
202+
203+
while (!g_trajectory_done)
204+
{
205+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
206+
g_my_driver->writeTrajectoryControlMessage(urcl::control::TrajectoryControlMessage::TRAJECTORY_NOOP);
207+
}
208+
}
209+
// --------------- END MOVEJ TRAJECTORY -------------------
210+
211+
g_my_driver->stopControl();
212+
return 0;
213+
}

0 commit comments

Comments
 (0)