bdaiinstitute / spatialmath-python

Create, manipulate and convert representations of position and orientation in 2D or 3D using Python
MIT License
497 stars 82 forks source link

T[-1] not working for SE3 objects / fkine() #100

Closed anineand closed 9 months ago

anineand commented 10 months ago

Hi,

I need to only get the position coordinate a from spatialmath.pose3d.SE3 object, so I tried to run T[-1] as it says in the robotics toolbox documentation. However that gives me the entire T matrix, not just the last column (with or without 1 at the end). I have also tried getting T[3,0], T[-1,0] and so on, but then I get warnings.

Example:

T = robot.fkine(q)   # for a joint configuration q and robot = rtb.models.Panda()
print(type(T[-1]), np.shape(T[-1]))
>>> <class 'spatialmath.pose3d.SE3'> (4, 4)

I am running spatialmath version 1.1.8 (and robotics toolbox 1.1.0).

anineand commented 10 months ago

Fix found: I read the spatialmath.pose3d code, and found a fix by running T.A[0, 3], T.A[1, 3] and T.A[2, 3] for x, y and z, respectively. This was based on the SE3 class methods x(), y() and z(), which got the following error when I attempted to run e.g. T.x(): AttributeError: 'SE3' object has no attribute 'x' I believe that there is use for some debugging here, but at least I found a work around. Also, if one is not supposed to be able to just run T[-1], then the documentation of robotics toolbox for python should be updated. I also think it would be nice if it's easier to access any of the matrix elements, but it's not something I need personally.

Thanks for all your work making and keeping these packages up to date!

jcao-bdai commented 9 months ago

Hi @anineand thanks for posting this. Unfortunately I am not familiar with the robotics toolbox - did a quick search and I assume you were talking about this particular method https://github.com/petercorke/robotics-toolbox-matlab/blob/bd7a9d75176c660f43fc799b24d838f70b02250c/%40SerialLink/fkine.m#L48C46-L49C1 ? Again, I am not familiar with it but looking at the description, the fkine method returns a tuple (pair): t, allt, each being a spatialmath.pose3d.SE3 object, which is what you found out, T[-1] would be the second one in the tuple (pair).

In any case, once you have an SE3 object, you will have access to many methods provided by this spatialmath(-python) package, e.g. as described in https://bdaiinstitute.github.io/spatialmath-python/func_nd.html?highlight=tr2rt#spatialmath.base.transformsNd.tr2rt you can easily extract the rotation and translation from it.

petercorke commented 9 months ago

Hi both.

Within the Robotics Toolbox fkine returns an SE3 object that contains a single transform value. It does not return a tuple, that's done by the method fkine_all.

So, in your case, T or T[-1] will be the same thing since there is only one value. To access just the translational part use the .t property which results in a 1D array with 3 elements.

petercorke commented 9 months ago

There are also properties for translation components, eg. T.x, which is quite concise.