Trajectory Joint Interface example
At first, we create a UrDriver
object as usual:
66 bool headless_mode = true;
67 g_my_robot = std::make_unique<urcl::ExampleRobotWrapper>(robot_ip, OUTPUT_RECIPE, INPUT_RECIPE, headless_mode,
68 "external_control.urp");
69 if (!g_my_robot->isHealthy())
70 {
71 URCL_LOG_ERROR("Something in the robot initialization went wrong. Exiting. Please check the output above.");
72 return 1;
73 }
We use a small helper function to make sure that the reverse interface is active and connected before proceeding.
Initialization
As trajectory execution will be triggered asynchronously, we define a callback function to handle a finished trajectory. A trajectory is considered finished when the robot is no longer executing it, independent of whether it successfully reached its final point. The trajectory result will reflect whether it was executed successfully, was canceled upon request or failed for some reason.
50void trajDoneCallback(const urcl::control::TrajectoryResult& result)
51{
52 URCL_LOG_INFO("Trajectory done with result %s", urcl::control::trajectoryResultToString(result).c_str());
53 g_trajectory_done = true;
54}
That callback can be registered at the UrDriver
object:
76 g_my_robot->getUrDriver()->registerTrajectoryDoneCallback(&trajDoneCallback);
MoveJ Trajectory
Then, in order to execute a trajectory, we need to define a trajectory as a sequence of points and
parameters. The following example shows execution of a 2-point trajectory using URScript’s
movej
function:
80 {
81 g_trajectory_done = false;
82 // Trajectory definition
83 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 } };
84 std::vector<double> motion_durations{ 5.0, 2.0 };
85 std::vector<double> velocities{ 2.0, 2.3 };
86 std::vector<double> accelerations{ 2.5, 2.5 };
87 std::vector<double> blend_radii{ 0.1, 0.1 };
88
89 // Trajectory execution
90 g_my_robot->getUrDriver()->writeTrajectoryControlMessage(urcl::control::TrajectoryControlMessage::TRAJECTORY_START,
91 points.size() * 2);
92 for (size_t i = 0; i < points.size(); i++)
93 {
94 g_my_robot->getUrDriver()->writeTrajectoryPoint(points[i], false, motion_durations[i], blend_radii[i]);
95 }
96
97 // Same motion, but parametrized with acceleration and velocity
98 motion_durations = { 0.0, 0.0 };
99 for (size_t i = 0; i < points.size(); i++)
100 {
101 g_my_robot->getUrDriver()->writeTrajectoryPoint(points[i], accelerations[i], velocities[i], false,
102 motion_durations[i], blend_radii[i]);
103 }
104
105 while (!g_trajectory_done)
106 {
107 std::this_thread::sleep_for(std::chrono::milliseconds(100));
108 g_my_robot->getUrDriver()->writeTrajectoryControlMessage(
109 urcl::control::TrajectoryControlMessage::TRAJECTORY_NOOP);
110 }
111 }
In fact, the path is followed twice, once parametrized by a segment duration and once using maximum
velocity / acceleration settings. If a duration > 0 is given for a segment, the velocity and
acceleration settings will be ignored as in the underlying URScript functions. In the example
above, each of the g_my_robot->getUrDriver()->writeTrajectoryPoint()
calls will be translated into a
movej
command in URScript.
While the trajectory is running, we need to tell the robot program that connection is still active
and we expect the trajectory to be running. This is being done by the
g_my_robot->getUrDriver()->writeTrajectoryControlMessage(urcl::control::TrajectoryControlMessage::TRAJECTORY_NOOP);
call.
MoveL Trajectory
Similar to the movej
-based trajectory, execution can be done interpolating in joint space:
116 {
117 g_trajectory_done = false;
118 // Trajectory definition
119 std::vector<urcl::vector6d_t> points{ { -0.203, 0.263, 0.559, 0.68, -1.083, -2.076 },
120 { -0.203, 0.463, 0.559, 0.68, -1.083, -2.076 } };
121 std::vector<double> motion_durations{ 5.0, 5.0 };
122 std::vector<double> velocities{ 2.0, 2.3 };
123 std::vector<double> accelerations{ 2.5, 2.5 };
124 std::vector<double> blend_radii{ 0.0, 0.0 };
125
126 // Trajectory execution of the path that goes through the points twice.
127 g_my_robot->getUrDriver()->writeTrajectoryControlMessage(urcl::control::TrajectoryControlMessage::TRAJECTORY_START,
128 points.size() * 2);
129 for (size_t i = 0; i < points.size(); i++)
130 {
131 // setting the cartesian parameter makes it interpret the 6d vector as a pose and use movel
132 g_my_robot->getUrDriver()->writeTrajectoryPoint(points[i], true, motion_durations[i], blend_radii[i]);
133 }
134
135 // Same motion, but parametrized with acceleration and velocity
136 motion_durations = { 0.0, 0.0 };
137 for (size_t i = 0; i < points.size(); i++)
138 {
139 g_my_robot->getUrDriver()->writeTrajectoryPoint(points[i], accelerations[i], velocities[i], true,
140 motion_durations[i], blend_radii[i]);
141 }
142
143 while (!g_trajectory_done)
144 {
145 std::this_thread::sleep_for(std::chrono::milliseconds(100));
146 g_my_robot->getUrDriver()->writeTrajectoryControlMessage(
147 urcl::control::TrajectoryControlMessage::TRAJECTORY_NOOP);
148 }
149 }
Spline based interpolation
Similar to the Spline Interpolation example, the trajectory point interface can be used to execute motions using the spline interpolation:
154 {
155 g_trajectory_done = false;
156 // Trajectory definition
157 std::vector<urcl::vector6d_t> positions{ { -1.57, -1.57, 0, 0, 0, 0 },
158 { -1.57, -1.57, -1.57, 0, 0, 0 },
159 { -1.57, -1.57, 0, 0, 0, 0 },
160 { -1.57, -1.6, 1.6, -0.7, 0.7, 0.2 } };
161 std::vector<urcl::vector6d_t> velocities{
162 { 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 }
163 };
164 std::vector<double> motion_durations{ 3.0, 3.0, 3.0, 3.0 };
165
166 // Trajectory execution
167 g_my_robot->getUrDriver()->writeTrajectoryControlMessage(urcl::control::TrajectoryControlMessage::TRAJECTORY_START,
168 positions.size());
169 for (size_t i = 0; i < positions.size(); i++)
170 {
171 g_my_robot->getUrDriver()->writeTrajectorySplinePoint(positions[i], velocities[i], motion_durations[i]);
172 }
173
174 while (!g_trajectory_done)
175 {
176 std::this_thread::sleep_for(std::chrono::milliseconds(100));
177 g_my_robot->getUrDriver()->writeTrajectoryControlMessage(
178 urcl::control::TrajectoryControlMessage::TRAJECTORY_NOOP);
179 }
180 }