idyntree
idyntree copied to clipboard
getRotation() does not work as expected in python
The function getRotation()
returns a reference but the python does not have the ownership. Let’s consider these lines of code that are part of a function.
H = self.kindyn.getWorldTransform(imu)
self.world_R_imu = H.getRotation()
the matrix H will not exist once out from the function and as a consequence, the global variable self.world_R_imu = H.getRotation()
will contain garbage.
cc @GiulioRomualdi
Can you provide a complete example (see https://stackoverflow.com/help/minimal-reproducible-example) that reproduces the problem? For example, it is not clear to me if:
H = self.kindyn.getWorldTransform(imu)
self.world_R_imu = iDynTree.Rotation()
self.world_R_imu = H.getRotation()
would have the same problem.
Here is an example. I don't know why, but in this case example, the problem is not occurring.
I highlighted the main differences with my code in the comments.
import numpy as np
import idyntree.bindings as idyn
from scipy.spatial.transform import Rotation
class Example:
def __init__(self):
self.R1 = idyn.Rotation()
self.R2 = idyn.Rotation()
def get_rotation(self):
H = idyn.Transform()
pos = idyn.Position().FromPython(np.ones(3))
R1 = idyn.Rotation().FromPython(Rotation.random().as_matrix())
H.setPosition(pos)
H.setRotation(R1)
self.R1 = H.getRotation()
R2 = idyn.Rotation().FromPython(Rotation.random().as_matrix())
H.setRotation(R2)
self.R2 = H.getRotation()
# In my case, H adn R are computed as
# H = kindyn.getWorldTransform(frame1)
# self.world_R_frame1[frame1] = H.getRotation()
#
# H = kindyn.getWorldTransform(frame2)
# self.world_R_frame2[frame2] = H.getRotation()
#
# where kindyn is a iDynTree.KinDynComputations() object
print("in getRotation:")
print(self.R1.toNumPy())
print(self.R2.toNumPy())
def test_rotation(self):
print("Test rotation:")
print(self.R1.toNumPy())
print(self.R2.toNumPy())
if __name__ == "__main__":
test = Example()
test.get_rotation()
test.test_rotation()