Skip to content

Commit f2661ba

Browse files
committed
Add collision constraint to ur5_reach example + fix minor mistake in relaxedLogBarrierCost
1 parent e054db5 commit f2661ba

File tree

5 files changed

+69
-29
lines changed

5 files changed

+69
-29
lines changed

bindings/python/src/modelling/expose-composite-costs.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ void exposeComposites() {
5252
bp::class_<RelaxedLogCost, bp::bases<CostAbstract>>(
5353
"RelaxedLogBarrierCost", "Relaxed log-barrier composite cost.",
5454
bp::init<PolyManifold, PolyFunction, const ConstVectorRef &,
55-
const Scalar>(bp::args("self", "space", "function",
56-
"barrier_weights", "threshold")))
55+
const Scalar>(
56+
bp::args("self", "space", "function", "weights", "threshold")))
5757
.def(bp::init<PolyManifold, PolyFunction, const Scalar, const Scalar>(
58-
bp::args("self", "space", "function", "scale", "threshold")))
58+
bp::args("self", "space", "function", "weights", "threshold")))
5959
.def_readwrite("residual", &RelaxedLogCost::residual_)
6060
.def_readwrite("weights", &RelaxedLogCost::barrier_weights_)
6161
.def(CopyableVisitor<RelaxedLogCost>())

examples/ur5_reach.py

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
from aligator import constraints, manifolds, dynamics # noqa
1010
from pinocchio.visualize import MeshcatVisualizer
11+
import hppfcl
1112

1213
from utils import ArgsBase, get_endpoint_traj
1314

@@ -32,6 +33,30 @@ class Args(ArgsBase):
3233
vizer.initViewer(open=args.display, loadModel=True)
3334
vizer.setBackgroundColor()
3435

36+
frname = "universe"
37+
fr_id = rmodel.getFrameId(frname)
38+
joint_id = rmodel.frames[fr_id].parentJoint
39+
obstacle_loc = pin.SE3.Identity()
40+
obstacle_loc.translation[0] = 0.3
41+
obstacle_loc.translation[1] = 0.5
42+
obstacle_loc.translation[2] = 0.3
43+
geom_object = pin.GeometryObject(
44+
"capsule", fr_id, joint_id, hppfcl.Capsule(0.05, 0.4), obstacle_loc
45+
)
46+
47+
fr_id2 = rmodel.getFrameId("wrist_3_joint")
48+
joint_id2 = rmodel.frames[fr_id2].parentJoint
49+
geom_object2 = pin.GeometryObject(
50+
"sphere2", fr_id2, joint_id2, hppfcl.Sphere(0.1), pin.SE3.Identity()
51+
)
52+
53+
geometry = pin.GeometryModel()
54+
ig_frame = geometry.addGeometryObject(geom_object)
55+
ig_frame2 = geometry.addGeometryObject(geom_object2)
56+
geometry.addCollisionPair(pin.CollisionPair(ig_frame, ig_frame2))
57+
58+
vizer.addGeometryObject(geom_object, [1.0, 1.0, 0.5, 1.0])
59+
3560

3661
x0 = space.neutral()
3762

@@ -61,6 +86,12 @@ class Args(ArgsBase):
6186
tool_name = "tool0"
6287
tool_id = rmodel.getFrameId(tool_name)
6388
target_pos = np.array([0.15, 0.65, 0.5])
89+
target_place = pin.SE3.Identity()
90+
target_place.translation = target_pos
91+
target_object = pin.GeometryObject(
92+
"target", fr_id, joint_id, hppfcl.Sphere(0.05), target_place
93+
)
94+
vizer.addGeometryObject(target_object, [0.5, 0.5, 1.0, 1.0])
6495
print(target_pos)
6596

6697
frame_fn = aligator.FrameTranslationResidual(ndx, nu, rmodel, target_pos, tool_id)
@@ -71,14 +102,18 @@ class Args(ArgsBase):
71102
)
72103
wt_x_term = wt_x.copy()
73104
wt_x_term[:] = 1e-4
74-
wt_frame_pos = 10.0 * np.eye(frame_fn.nr)
105+
wt_frame_pos = 100.0 * np.eye(frame_fn.nr)
75106
wt_frame_vel = 100.0 * np.ones(frame_vel_fn.nr)
76107
wt_frame_vel = np.diag(wt_frame_vel)
77108

78109
term_cost = aligator.CostStack(space, nu)
79-
term_cost.addCost(aligator.QuadraticCost(wt_x_term, wt_u * 0))
80-
term_cost.addCost(aligator.QuadraticResidualCost(space, frame_fn, wt_frame_pos))
81-
term_cost.addCost(aligator.QuadraticResidualCost(space, frame_vel_fn, wt_frame_vel))
110+
term_cost.addCost("reg", aligator.QuadraticCost(wt_x_term, wt_u * 0))
111+
term_cost.addCost(
112+
"frame", aligator.QuadraticResidualCost(space, frame_fn, wt_frame_pos)
113+
)
114+
term_cost.addCost(
115+
"vel", aligator.QuadraticResidualCost(space, frame_vel_fn, wt_frame_vel)
116+
)
82117

83118
u_max = rmodel.effortLimit
84119
u_min = -u_max
@@ -103,12 +138,15 @@ def computeQuasistatic(model: pin.Model, x0, a):
103138

104139

105140
stages = []
106-
107141
for i in range(nsteps):
108142
rcost = aligator.CostStack(space, nu)
109-
rcost.addCost(aligator.QuadraticCost(wt_x * dt, wt_u * dt))
143+
rcost.addCost("reg", aligator.QuadraticCost(wt_x * dt, wt_u * dt))
110144

111145
stm = aligator.StageModel(rcost, discrete_dynamics)
146+
# Distance to obstacle constrained between 0.1 and 100 m
147+
cstr_set = constraints.BoxConstraint(np.array([0.1]), np.array([100]))
148+
frame_col = aligator.FrameCollisionResidual(ndx, nu, rmodel, geometry, 0)
149+
stm.addConstraint(frame_col, cstr_set)
112150
if args.bounds:
113151
stm.addConstraint(*make_control_bounds())
114152
stages.append(stm)
@@ -119,7 +157,7 @@ def computeQuasistatic(model: pin.Model, x0, a):
119157

120158
mu_init = 1e-7
121159
verbose = aligator.VerboseLevel.VERBOSE
122-
max_iters = 40
160+
max_iters = 500
123161
solver = aligator.SolverProxDDP(tol, mu_init, max_iters=max_iters, verbose=verbose)
124162
solver.rollout_type = aligator.ROLLOUT_NONLINEAR
125163
solver.sa_strategy = aligator.SA_LINESEARCH_NONMONOTONE

include/aligator/modelling/costs/relaxed-log-barrier.hpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,16 @@ struct RelaxedLogBarrierCostTpl : CostAbstractTpl<Scalar> {
1818

1919
VectorXs barrier_weights_;
2020
xyz::polymorphic<StageFunction> residual_;
21-
double threshold_;
21+
Scalar threshold_;
2222

2323
RelaxedLogBarrierCostTpl(xyz::polymorphic<Manifold> space,
2424
xyz::polymorphic<StageFunction> function,
25-
const ConstVectorRef &scale, const Scalar threshold);
25+
const ConstVectorRef &weight,
26+
const Scalar threshold);
2627

2728
RelaxedLogBarrierCostTpl(xyz::polymorphic<Manifold> space,
2829
xyz::polymorphic<StageFunction> function,
29-
const Scalar scale, const Scalar threshold);
30+
const Scalar weight, const Scalar threshold);
3031

3132
void evaluate(const ConstVectorRef &x, const ConstVectorRef &u,
3233
CostDataAbstract &data) const;

include/aligator/modelling/costs/relaxed-log-barrier.hxx

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ namespace aligator {
99
template <typename Scalar>
1010
RelaxedLogBarrierCostTpl<Scalar>::RelaxedLogBarrierCostTpl(
1111
xyz::polymorphic<Manifold> space, xyz::polymorphic<StageFunction> function,
12-
const ConstVectorRef &scale, const Scalar threshold)
13-
: Base(space, function->nu), barrier_weights_(scale), residual_(function),
12+
const ConstVectorRef &weight, const Scalar threshold)
13+
: Base(space, function->nu), barrier_weights_(weight), residual_(function),
1414
threshold_(threshold) {
15-
if (scale.size() != function->nr) {
15+
if (weight.size() != function->nr) {
1616
ALIGATOR_RUNTIME_ERROR(
17-
"scale argument dimension ({:d}) != function codimension ({:d})",
18-
scale.size(), function->nr);
17+
"weight argument dimension ({:d}) != function codimension ({:d})",
18+
weight.size(), function->nr);
1919
}
20-
bool negs = (scale.array() <= 0.0).any();
20+
bool negs = (weight.array() <= 0.0).any();
2121
if (negs) {
22-
ALIGATOR_RUNTIME_ERROR("scale coefficients must be > 0.");
22+
ALIGATOR_RUNTIME_ERROR("weight coefficients must be > 0.");
2323
}
2424
if (threshold_ <= 0.) {
2525
ALIGATOR_RUNTIME_ERROR("threshold must be > 0.");
@@ -29,10 +29,10 @@ RelaxedLogBarrierCostTpl<Scalar>::RelaxedLogBarrierCostTpl(
2929
template <typename Scalar>
3030
RelaxedLogBarrierCostTpl<Scalar>::RelaxedLogBarrierCostTpl(
3131
xyz::polymorphic<Manifold> space, xyz::polymorphic<StageFunction> function,
32-
const Scalar scale, const Scalar threshold)
33-
: RelaxedLogBarrierCostTpl(
34-
space, function, VectorXs::Constant(function->nr, scale), threshold) {
35-
}
32+
const Scalar weight, const Scalar threshold)
33+
: RelaxedLogBarrierCostTpl(space, function,
34+
VectorXs::Constant(function->nr, weight),
35+
threshold) {}
3636

3737
template <typename Scalar>
3838
void RelaxedLogBarrierCostTpl<Scalar>::evaluate(const ConstVectorRef &x,
@@ -45,7 +45,7 @@ void RelaxedLogBarrierCostTpl<Scalar>::evaluate(const ConstVectorRef &x,
4545
for (size_t i = 0; i < nrows; i++) {
4646
if (d.residual_data->value_[i] < threshold_) {
4747
Scalar sq = (d.residual_data->value_[i] - 2 * threshold_) / threshold_;
48-
d.value_ += 0.5 * barrier_weights_(i) * (sq * sq - 1) - log(threshold_);
48+
d.value_ += barrier_weights_(i) * (0.5 * (sq * sq - 1) - log(threshold_));
4949
} else {
5050
d.value_ -= barrier_weights_(i) * log(d.residual_data->value_[i]);
5151
}
@@ -65,7 +65,7 @@ void RelaxedLogBarrierCostTpl<Scalar>::computeGradients(
6565
const int nrows = residual_->nr;
6666
for (int i = 0; i < nrows; i++) {
6767
auto g_i = J.row(i);
68-
if (d.residual_data->value_[i] < threshold_) {
68+
if (v(i) < threshold_) {
6969
d.grad_.noalias() += barrier_weights_(i) * g_i * (v(i) - 2 * threshold_) /
7070
(threshold_ * threshold_);
7171
} else {
@@ -87,7 +87,7 @@ void RelaxedLogBarrierCostTpl<Scalar>::computeHessians(
8787
const int nrows = residual_->nr;
8888
for (int i = 0; i < nrows; i++) {
8989
auto g_i = J.row(i); // row vector
90-
if (d.residual_data->value_[i] < threshold_) {
90+
if (v(i) < threshold_) {
9191
Scalar sq = (v(i) - 2 * threshold_) / (threshold_ * threshold_);
9292
d.hess_.noalias() += barrier_weights_(i) * barrier_weights_(i) *
9393
(g_i.transpose() * g_i) * sq * sq;

include/aligator/modelling/multibody/frame-collision.hxx

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <pinocchio/algorithm/frames.hpp>
55
#include <pinocchio/algorithm/kinematics.hpp>
66
#include <pinocchio/collision/distance.hpp>
7+
#include <iostream>
78

89
namespace aligator {
910

@@ -60,8 +61,8 @@ void FrameCollisionResidualTpl<Scalar>::computeJacobians(const ConstVectorRef &,
6061
// compute the residual derivatives
6162
d.Jx_.setZero();
6263
d.Jx_.leftCols(pin_model_.nv) =
63-
-d.geometry_.distanceResults[frame_pair_id_].normal.transpose() *
64-
(d.Jcol_.template topRows<3>() - d.Jcol2_.template topRows<3>());
64+
d.geometry_.distanceResults[frame_pair_id_].normal.transpose() *
65+
(d.Jcol2_.template topRows<3>() - d.Jcol_.template topRows<3>());
6566
}
6667

6768
template <typename Scalar>

0 commit comments

Comments
 (0)