Skip to content

Commit 8fabf6c

Browse files
committed
Self-review
1 parent 8a5a746 commit 8fabf6c

5 files changed

Lines changed: 141 additions & 90 deletions

File tree

include/ur_client_library/control/motion_primitives.h

Lines changed: 94 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#include <chrono>
3535
#include <optional>
3636
#include <variant>
37-
#include "ur_client_library/exceptions.h"
3837
#include <ur_client_library/types.h>
3938

4039
namespace urcl
@@ -76,6 +75,45 @@ enum class MotionType : uint8_t
7675
UNKNOWN = 255
7776
};
7877

78+
inline std::string motionTypeToString(const MotionType type)
79+
{
80+
switch (type)
81+
{
82+
case MotionType::MOVEJ:
83+
return "MOVEJ";
84+
case MotionType::MOVEL:
85+
return "MOVEL";
86+
case MotionType::MOVEP:
87+
return "MOVEP";
88+
case MotionType::MOVEC:
89+
return "MOVEC";
90+
case MotionType::OPTIMOVEJ:
91+
return "OPTIMOVEJ";
92+
case MotionType::OPTIMOVEL:
93+
return "OPTIMOVEL";
94+
case MotionType::MOVEJ_POSE:
95+
return "MOVEJ_POSE";
96+
case MotionType::MOVEL_JOINT:
97+
return "MOVEL_JOINT";
98+
case MotionType::MOVEP_JOINT:
99+
return "MOVEP_JOINT";
100+
case MotionType::MOVEC_JOINT:
101+
return "MOVEC_JOINT";
102+
case MotionType::MOVEC_JOINT_POSE:
103+
return "MOVEC_JOINT_POSE";
104+
case MotionType::MOVEC_POSE_JOINT:
105+
return "MOVEC_POSE_JOINT";
106+
case MotionType::OPTIMOVEJ_POSE:
107+
return "OPTIMOVEJ_POSE";
108+
case MotionType::OPTIMOVEL_JOINT:
109+
return "OPTIMOVEL_JOINT";
110+
case MotionType::SPLINE:
111+
return "SPLINE";
112+
default:
113+
return "UNKNOWN";
114+
}
115+
}
116+
79117
/*!
80118
* Spline types
81119
*/
@@ -144,8 +182,8 @@ struct MoveJPrimitive : public MotionPrimitive
144182
target);
145183
}
146184

147-
urcl::vector6d_t target_joint_configuration;
148-
urcl::Pose target_pose;
185+
urcl::vector6d_t target_joint_configuration{};
186+
urcl::Pose target_pose{};
149187
};
150188

151189
struct MoveLPrimitive : public MotionPrimitive
@@ -195,8 +233,8 @@ struct MoveLPrimitive : public MotionPrimitive
195233
target);
196234
}
197235

198-
urcl::Pose target_pose;
199-
urcl::vector6d_t target_joint_configuration;
236+
urcl::Pose target_pose{};
237+
urcl::vector6d_t target_joint_configuration{};
200238
};
201239

202240
struct MovePPrimitive : public MotionPrimitive
@@ -239,8 +277,8 @@ struct MovePPrimitive : public MotionPrimitive
239277
this->blend_radius = blend_radius;
240278
}
241279

242-
urcl::Pose target_pose;
243-
urcl::vector6d_t target_joint_configuration;
280+
urcl::Pose target_pose{};
281+
urcl::vector6d_t target_joint_configuration{};
244282
};
245283

246284
struct MoveCPrimitive : public MotionPrimitive
@@ -261,73 +299,62 @@ struct MoveCPrimitive : public MotionPrimitive
261299
* \brief Construct a MoveC primitive from two \ref urcl::MotionTarget values.
262300
*
263301
* Every combination of \ref urcl::Pose and \ref urcl::Q for the via point and the target is
264-
* supported and mapped to the corresponding \ref MotionType (``MOVEC``,
265-
* ``MOVEC_JOINT``, ``MOVEC_POSE_JOINT``, or ``MOVEC_JOINT_POSE``). When a joint configuration
266-
* is used for either role, only the corresponding joint member is populated and the pose
267-
* member is set to a default-constructed \ref urcl::Pose.
302+
* supported and mapped to the corresponding \ref MotionType (``MOVEC``, ``MOVEC_JOINT``,
303+
* ``MOVEC_POSE_JOINT``, or ``MOVEC_JOINT_POSE``). The naming convention is
304+
* ``MOVEC_<target>_<via>``, i.e. ``MOVEC_POSE_JOINT`` denotes a movec whose target is a pose
305+
* and whose via point is a joint configuration.
268306
*
269-
* \throws urcl::UrException if either variant is ever extended with an alternative that is
270-
* not handled here.
307+
* Unhandled variant alternatives are caught at compile time via ``static_assert``.
271308
*/
272309
MoveCPrimitive(const MotionTarget& via_point, const MotionTarget& target, const double blend_radius = 0,
273310
const double acceleration = 1.4, const double velocity = 1.04, const int32_t mode = 0)
274311
{
275-
if (std::holds_alternative<Q>(via_point))
276-
{
277-
if (std::holds_alternative<Q>(target))
278-
{
279-
type = MotionType::MOVEC_JOINT;
280-
via_point_pose = urcl::Pose();
281-
target_pose = urcl::Pose();
282-
via_point_joint_configuration = std::get<Q>(via_point).values;
283-
target_joint_configuration = std::get<Q>(target).values;
284-
}
285-
else if (std::holds_alternative<urcl::Pose>(target))
286-
{
287-
type = MotionType::MOVEC_POSE_JOINT;
288-
via_point_pose = urcl::Pose();
289-
target_pose = std::get<urcl::Pose>(target);
290-
via_point_joint_configuration = std::get<Q>(via_point).values;
291-
}
292-
else
293-
{
294-
throw urcl::UrException("Unhandled motion target type for target point passed to MoveCPrimitive constructor");
295-
}
296-
}
297-
else if (std::holds_alternative<urcl::Pose>(via_point))
298-
{
299-
if (std::holds_alternative<Q>(target))
300-
{
301-
type = MotionType::MOVEC_JOINT_POSE;
302-
via_point_pose = std::get<urcl::Pose>(via_point);
303-
target_pose = urcl::Pose();
304-
target_joint_configuration = std::get<Q>(target).values;
305-
}
306-
else if (std::holds_alternative<urcl::Pose>(target))
307-
{
308-
type = MotionType::MOVEC;
309-
via_point_pose = std::get<urcl::Pose>(via_point);
310-
target_pose = std::get<urcl::Pose>(target);
311-
}
312-
else
313-
{
314-
throw urcl::UrException("Unhandled motion target type for target point passed to MoveCPrimitive constructor");
315-
}
316-
}
317-
else
318-
{
319-
throw urcl::UrException("Unhandled motion target type for via_point passed to MoveCPrimitive constructor");
320-
}
312+
std::visit(
313+
[&](const auto& via_variant, const auto& target_variant) {
314+
using ViaT = std::decay_t<decltype(via_variant)>;
315+
using TargetT = std::decay_t<decltype(target_variant)>;
316+
static_assert(std::is_same_v<ViaT, Q> || std::is_same_v<ViaT, urcl::Pose>, "Unhandled MotionTarget "
317+
"alternative for via_point");
318+
static_assert(std::is_same_v<TargetT, Q> || std::is_same_v<TargetT, urcl::Pose>, "Unhandled MotionTarget "
319+
"alternative for target");
320+
321+
if constexpr (std::is_same_v<ViaT, urcl::Pose> && std::is_same_v<TargetT, urcl::Pose>)
322+
{
323+
type = MotionType::MOVEC;
324+
via_point_pose = via_variant;
325+
target_pose = target_variant;
326+
}
327+
else if constexpr (std::is_same_v<ViaT, Q> && std::is_same_v<TargetT, Q>)
328+
{
329+
type = MotionType::MOVEC_JOINT;
330+
via_point_joint_configuration = via_variant.values;
331+
target_joint_configuration = target_variant.values;
332+
}
333+
else if constexpr (std::is_same_v<ViaT, Q> && std::is_same_v<TargetT, urcl::Pose>)
334+
{
335+
type = MotionType::MOVEC_POSE_JOINT;
336+
via_point_joint_configuration = via_variant.values;
337+
target_pose = target_variant;
338+
}
339+
else if constexpr (std::is_same_v<ViaT, urcl::Pose> && std::is_same_v<TargetT, Q>)
340+
{
341+
type = MotionType::MOVEC_JOINT_POSE;
342+
via_point_pose = via_variant;
343+
target_joint_configuration = target_variant.values;
344+
}
345+
},
346+
via_point, target);
347+
321348
this->acceleration = acceleration;
322349
this->velocity = velocity;
323350
this->blend_radius = blend_radius;
324351
this->mode = mode;
325352
}
326353

327-
urcl::Pose via_point_pose;
328-
urcl::Pose target_pose;
329-
urcl::vector6d_t via_point_joint_configuration;
330-
urcl::vector6d_t target_joint_configuration;
354+
urcl::Pose via_point_pose{};
355+
urcl::Pose target_pose{};
356+
urcl::vector6d_t via_point_joint_configuration{};
357+
urcl::vector6d_t target_joint_configuration{};
331358
int32_t mode = 0;
332359
};
333360

@@ -406,8 +433,8 @@ struct OptimoveJPrimitive : public MotionPrimitive
406433

407434
bool validate() const override;
408435

409-
urcl::vector6d_t target_joint_configuration;
410-
Pose target_pose;
436+
urcl::vector6d_t target_joint_configuration{};
437+
Pose target_pose{};
411438
};
412439

413440
struct OptimoveLPrimitive : public MotionPrimitive
@@ -452,8 +479,8 @@ struct OptimoveLPrimitive : public MotionPrimitive
452479

453480
bool validate() const override;
454481

455-
urcl::Pose target_pose;
456-
vector6d_t target_joint_configuration;
482+
urcl::Pose target_pose{};
483+
vector6d_t target_joint_configuration{};
457484
};
458485
} // namespace control
459486
} // namespace urcl

include/ur_client_library/types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ struct Q
5050
constexpr Q(double q1, double q2, double q3, double q4, double q5, double q6) : values{ q1, q2, q3, q4, q5, q6 }
5151
{
5252
}
53+
explicit constexpr Q(const vector6d_t& values) : values(values)
54+
{
55+
}
5356

5457
vector6d_t values;
5558
};

resources/external_control.urscript

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -578,7 +578,7 @@ thread trajectoryThread():
578578
acceleration = raw_point[13] / MULT_jointstate
579579
velocity = raw_point[7] / MULT_jointstate
580580
movej(p[q[0], q[1], q[2], q[3], q[4], q[5]], a = acceleration, v = velocity, t = tmptime, r = blend_radius)
581-
581+
582582
# reset old acceleration
583583
spline_qdd = [0, 0, 0, 0, 0, 0]
584584
spline_qd = [0, 0, 0, 0, 0, 0]
@@ -595,12 +595,12 @@ thread trajectoryThread():
595595
elif raw_point[INDEX_POINT_TYPE] == MOTION_TYPE_MOVEL_JOINT:
596596
acceleration = raw_point[13] / MULT_jointstate
597597
velocity = raw_point[7] / MULT_jointstate
598-
movel([q[0], q[1], q[2], q[3], q[4], q[5]], a = acceleration, v = velocity, t = tmptime, r = blend_radius)
598+
movel(q, a = acceleration, v = velocity, t = tmptime, r = blend_radius)
599599

600600
# reset old acceleration
601601
spline_qdd = [0, 0, 0, 0, 0, 0]
602602
spline_qd = [0, 0, 0, 0, 0, 0]
603-
603+
604604
# MoveP point
605605
elif raw_point[INDEX_POINT_TYPE] == MOTION_TYPE_MOVEP:
606606
acceleration = raw_point[13] / MULT_jointstate
@@ -711,7 +711,7 @@ thread trajectoryThread():
711711
elif raw_point[INDEX_POINT_TYPE] == MOTION_TYPE_OPTIMOVEL or raw_point[INDEX_POINT_TYPE] == MOTION_TYPE_OPTIMOVEL_JOINT:
712712
acceleration = raw_point[13] / MULT_jointstate
713713
velocity = raw_point[7] / MULT_jointstate
714-
714+
715715
{% if ROBOT_SOFTWARE_VERSION >= v5.21.0 %}
716716
{% if ROBOT_SOFTWARE_VERSION < v6.0.0 %}
717717
if raw_point[INDEX_POINT_TYPE] == MOTION_TYPE_OPTIMOVEL:
@@ -766,8 +766,8 @@ thread clearTrajectoryPointsThread():
766766
limit = 20
767767
{% else %}
768768
limit = 10
769-
{% endif %}
770-
769+
{% endif %}
770+
771771
while trajectory_points_left > 0:
772772
raw_point = socket_read_binary_integer(TRAJECTORY_DATA_DIMENSION + 2, "trajectory_socket", timeout)
773773
if raw_point[0] <= 0:
@@ -778,7 +778,7 @@ thread clearTrajectoryPointsThread():
778778
trajectory_points_left = trajectory_points_left - 1
779779
reads = reads + 1
780780
if reads >= limit:
781-
sync()
781+
sync()
782782
reads = 0
783783
end
784784
end
@@ -999,17 +999,17 @@ end
999999

10001000
# NODE_CONTROL_LOOP_BEGINS
10011001

1002-
# Attempt to establish the three required socket connections, if at least one socket is still disconnected,
1002+
# Attempt to establish the three required socket connections, if at least one socket is still disconnected,
10031003
# a popup is shown and the loop continues until all three flags are True.
10041004
traj_flag = False
10051005
com_flag = False
10061006
rev_flag = False
10071007
while not (traj_flag and com_flag and rev_flag):
1008-
if not traj_flag:
1008+
if not traj_flag:
10091009
traj_flag = connect_socket_with_retry("{{SERVER_IP_REPLACE}}", {{TRAJECTORY_SERVER_PORT_REPLACE}}, "trajectory_socket")
10101010
end
10111011

1012-
if traj_flag and not com_flag:
1012+
if traj_flag and not com_flag:
10131013
com_flag = connect_socket_with_retry("{{SERVER_IP_REPLACE}}", {{SCRIPT_COMMAND_SERVER_PORT_REPLACE}}, "script_command_socket")
10141014
end
10151015

@@ -1052,7 +1052,7 @@ while control_mode > MODE_STOPPED:
10521052
if params_mult[0] > 0:
10531053

10541054
# Convert read timeout from milliseconds to seconds
1055-
read_timeout = params_mult[1] / 1000.0
1055+
read_timeout = params_mult[1] / 1000.0
10561056

10571057
if control_mode != params_mult[REVERSE_INTERFACE_DATA_DIMENSION]:
10581058
# Clear remaining trajectory points

src/control/trajectory_point_interface.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ bool TrajectoryPointInterface::writeMotionPrimitive(const std::shared_ptr<contro
104104
}
105105
else
106106
{
107-
throw InvalidData("Motion type does not match motion primitive type.");
107+
throw InvalidData("Motion type " + motionTypeToString(movej_primitive->type) +
108+
" is not allowed in a MoveJPrimitive.");
108109
}
109110
second_block.fill(primitive->velocity);
110111
third_block.fill(primitive->acceleration);
@@ -126,7 +127,8 @@ bool TrajectoryPointInterface::writeMotionPrimitive(const std::shared_ptr<contro
126127
}
127128
else
128129
{
129-
throw InvalidData("Motion type does not match motion primitive type.");
130+
throw InvalidData("Motion type " + motionTypeToString(movel_primitive->type) +
131+
" is not allowed in a MoveLPrimitive.");
130132
}
131133
second_block.fill(primitive->velocity);
132134
third_block.fill(primitive->acceleration);
@@ -146,6 +148,11 @@ bool TrajectoryPointInterface::writeMotionPrimitive(const std::shared_ptr<contro
146148
{
147149
first_block = movep_primitive->target_joint_configuration;
148150
}
151+
else
152+
{
153+
throw InvalidData("Motion type " + motionTypeToString(movep_primitive->type) +
154+
" is not allowed in a MovePPrimitive.");
155+
}
149156
second_block.fill(primitive->velocity);
150157
third_block.fill(primitive->acceleration);
151158
break;
@@ -184,6 +191,11 @@ bool TrajectoryPointInterface::writeMotionPrimitive(const std::shared_ptr<contro
184191
movec_primitive->via_point_pose.z, movec_primitive->via_point_pose.rx,
185192
movec_primitive->via_point_pose.ry, movec_primitive->via_point_pose.rz };
186193
}
194+
else
195+
{
196+
throw InvalidData("Motion type " + motionTypeToString(movec_primitive->type) +
197+
" is not allowed in a MoveCPrimitive.");
198+
}
187199
third_block = {
188200
primitive->velocity, primitive->acceleration, static_cast<double>(movec_primitive->mode), 0, 0, 0
189201
};
@@ -214,6 +226,11 @@ bool TrajectoryPointInterface::writeMotionPrimitive(const std::shared_ptr<contro
214226
optimovej_primitive->target_pose.z, optimovej_primitive->target_pose.rx,
215227
optimovej_primitive->target_pose.ry, optimovej_primitive->target_pose.rz };
216228
}
229+
else
230+
{
231+
throw InvalidData("Motion type " + motionTypeToString(optimovej_primitive->type) +
232+
" is not allowed in an OptimoveJPrimitive.");
233+
}
217234
second_block.fill(primitive->velocity);
218235
third_block.fill(primitive->acceleration);
219236
break;
@@ -232,6 +249,11 @@ bool TrajectoryPointInterface::writeMotionPrimitive(const std::shared_ptr<contro
232249
{
233250
first_block = optimovel_primitive->target_joint_configuration;
234251
}
252+
else
253+
{
254+
throw InvalidData("Motion type " + motionTypeToString(optimovel_primitive->type) +
255+
" is not allowed in an OptimoveLPrimitive.");
256+
}
235257
second_block.fill(primitive->velocity);
236258
third_block.fill(primitive->acceleration);
237259
break;

0 commit comments

Comments
 (0)