Simple-Robotics / aligator

A versatile and efficient framework for constrained trajectory optimization
https://simple-robotics.github.io/aligator/
BSD 2-Clause "Simplified" License
121 stars 18 forks source link

Cannot set reference for residual function in Python #209

Closed edantec closed 1 month ago

edantec commented 1 month ago

Bug description

In Python, the SetReference function of residuals does not work in some cases, probably because the bindings return a copy of the object somewhere instead of a reference.

Expected behavior

When holding a CostStack object in Python containing a residual, we should be able to modify the corresponding residual reference.

Code

import numpy as np
import aligator
import pinocchio as pin
import example_robot_data as erd

robot = erd.load("talos")
rmodel = robot.model
RF_id = rmodel.getFrameId("right_sole_link")
nv = rmodel.nv
nu = nv - 6

space = aligator.manifolds.MultibodyPhaseSpace(rmodel)

posref = pin.SE3.Identity()
frame_fn_RF = aligator.FramePlacementResidual(space.ndx, nu, rmodel, posref, RF_id)
rcost = aligator.CostStack(space, nu)
residual = aligator.QuadraticResidualCost(space, frame_fn_RF, np.eye(6))
rcost.addCost(residual)

newpos = pin.SE3.Identity()
newpos.translation[2] = 0.2
print(
    "Translation stored = ",
    str(rcost.components[0][0].residual.getReference().translation),
)
rcost.components[0][0].residual.setReference(newpos)
print(
    "Set reference translation to ",
    str(newpos.translation),
)
print(
    "New translation stored = ",
    str(rcost.components[0][0].residual.getReference().translation),
)

Additional context

Macro ALIGATOR_EIGENPY_HAS_MAP_SUPPORT is on.

System

edantec commented 1 month ago

Addendum: this problem has been encountered before and was supposedly solved by this commit: 81dc2332a05cb71bad4d3bf49cb81a2fd25c62dd The switch from vector to map of components probably caused the problem to arise again.

ManifoldFR commented 1 month ago

This is my fault, I forgot to overload the base_get_item in the map visitor... 🤦🏼‍♂️ This is an upstream problem in eigenpy now

ManifoldFR commented 1 month ago

Okay, we need to fix this like today

ManifoldFR commented 1 month ago

I just pushed on devel @edantec

ManifoldFR commented 1 month ago

Resolved by 36c4639c