Skip to content

Commit edcb472

Browse files
authored
Link rotation cost (moveit#726)
Add TrajectoryCostTerm measuring total orientation change of a link along the trajectory
1 parent 9d126e4 commit edcb472

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

core/include/moveit/task_constructor/cost_terms.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,18 @@ class LinkMotion : public TrajectoryCostTerm
177177
double operator()(const SubTrajectory& s, std::string& comment) const override;
178178
};
179179

180+
/** total orientation change of a link through the trajectory */
181+
class LinkRotation : public TrajectoryCostTerm
182+
{
183+
public:
184+
LinkRotation(std::string link_name);
185+
186+
std::string link_name;
187+
188+
using TrajectoryCostTerm::operator();
189+
double operator()(const SubTrajectory& s, std::string& comment) const override;
190+
};
191+
180192
/** inverse distance to collision
181193
*
182194
* \arg with_world check distances to world objects or look at self-collisions

core/python/bindings/src/core.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,10 @@ void export_core(pybind11::module& m) {
172172
"Computes Cartesian path length of given link along trajectory")
173173
.def(py::init<std::string>(), "link_name"_a);
174174

175+
py::classh<cost::LinkRotation, TrajectoryCostTerm>(
176+
m, "LinkRotation", "Computes total orientation change of given link along trajectory")
177+
.def(py::init<std::string>(), "link_name"_a);
178+
175179
py::classh<cost::Clearance, TrajectoryCostTerm>(m, "Clearance", "Computes inverse distance to collision objects")
176180
.def(py::init<bool, bool, std::string, TrajectoryCostTerm::Mode>(), "with_world"_a = true,
177181
"cumulative"_a = false, "group_property"_a = "group", "mode"_a = TrajectoryCostTerm::Mode::AUTO);

core/src/cost_terms.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,30 @@ double LinkMotion::operator()(const SubTrajectory& s, std::string& comment) cons
220220
return distance;
221221
}
222222

223+
LinkRotation::LinkRotation(std::string link) : link_name{ std::move(link) } {}
224+
225+
double LinkRotation::operator()(const SubTrajectory& s, std::string& comment) const {
226+
const auto& traj{ s.trajectory() };
227+
228+
if (traj == nullptr || traj->getWayPointCount() == 0)
229+
return 0.0;
230+
231+
if (!traj->getWayPoint(0).knowsFrameTransform(link_name)) {
232+
comment = fmt::format("LinkRotationCost: frame '{}' unknown in trajectory", link_name);
233+
return std::numeric_limits<double>::infinity();
234+
}
235+
236+
double angular_distance{ 0.0 };
237+
238+
Eigen::Quaterniond q{ traj->getWayPoint(0).getFrameTransform(link_name).linear() };
239+
for (size_t i{ 1 }; i < traj->getWayPointCount(); ++i) {
240+
const Eigen::Quaterniond next_q{ traj->getWayPoint(i).getFrameTransform(link_name).linear() };
241+
angular_distance += q.angularDistance(next_q);
242+
q = next_q;
243+
}
244+
return angular_distance;
245+
}
246+
223247
Clearance::Clearance(bool with_world, bool cumulative, std::string group_property, Mode mode)
224248
: with_world{ with_world }
225249
, cumulative{ cumulative }

demo/src/alternative_path_costs.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ int main(int argc, char** argv) {
6161
connect->setCostTerm(std::make_unique<cost::LinkMotion>("panda_link4"));
6262
alternatives->add(std::move(connect));
6363
}
64+
{
65+
auto connect{ std::make_unique<stages::Connect>(
66+
"eef rotation", stages::Connect::GroupPlannerVector{ { "panda_arm", pipeline } }) };
67+
connect->setCostTerm(std::make_unique<cost::LinkRotation>("panda_hand"));
68+
alternatives->add(std::move(connect));
69+
}
6470

6571
t.add(std::move(alternatives));
6672

0 commit comments

Comments
 (0)