Calculate Pose and Transform differences in Python with ROS

Functions that calculate differences in Geometry_msgs using Python

--You may want to take the difference between Pose and Transform. --If registered in tf tree, you should definitely use tf lookup transform --Otherwise, I didn't seem to have a suitable package when I didn't use C ++, so I created a set of functions.

** It was a reinvention of the wheel, but I had to write it myself because I couldn't find a package that was easy to use. ** ** We plan to add the necessary functions as soon as they come up.

If there is demand, manage it with Git.

List

--Functions related to Geometry_msgs.msg.Transform () --transform2homogeneousM: Transform the transform message into a homogeneous transformation matrix (Homogeneous Matrix) --homogeneous2transform: Retransform the homogeneous transformation matrix into a transform message --transform_diff (tf1, tf2): Output the difference between the two transform messages as a transform message --Functions related to Geometry_msgs.msg.Pose () --pose2homogeneousM: Converts a pose message into a homogeneous transformation matrix (Homogeneous Matrix) --homogeneous2pose: Reconvert the homogeneous transformation matrix into a pose message --pose_diff (p1, p2): Output the difference between two pose messages as a pose message

code

import rospy
import geometry_msg
import tf

# Transform to homogeneous matrix
def transform2homogeneousM(tfobj):
    #It says Quat to euler sxyz, but the order of XYZW is fine. Isn't it a little confusing?
    tfeul= tf.transformations.euler_from_quaternion([tfobj.rotation.x,tfobj.rotation.y,tfobj.rotation.z,tfobj.rotation.w],axes='sxyz')
    #Description of translation amount
    tftrans = [ tfobj.translation.x,tfobj.translation.y,tfobj.translation.z]
    tfobjM = tf.transformations.compose_matrix(angles=tfeul,translate=tftrans)
    # return
    return  tfobjM

def homogeneous2transform(Mat):
    scale, shear, angles, trans, persp = tf.transformations.decompose_matrix(Mat)
    quat = tf.transformations.quaternion_from_euler(angles[0],angles[1],angles[2])
    tfobj = geometry_msgs.msg.Transform()
    tfobj.rotation.x = quat[0]
    tfobj.rotation.y = quat[1]
    tfobj.rotation.z = quat[2]
    tfobj.rotation.w = quat[3]
    tfobj.translation.x = trans[0]
    tfobj.translation.y = trans[1]
    tfobj.translation.z = trans[2]
    return tfobj
    
# Transform diff tf1 to 2

def transform_diff(tf1,tf2):
    tf1M = transform2homogeneousM(tf1)
    tf2M = transform2homogeneousM(tf2)
    return  homogeneous2transform(tf2M.dot(tf.transformations.inverse_matrix(tf1M)))
    

#Also make a Pose version
def pose2homogeneousM(poseobj):
    try:
        #It says Quat to euler sxyz, but the order of XYZW is fine. Isn't it a little confusing?
        tfeul= tf.transformations.euler_from_quaternion([poseobj.orientation.x,poseobj.orientation.y,poseobj.orientation.z,poseobj.orientation.w],axes='sxyz')
        #Description of translation amount
        tftrans = [ poseobj.position.x,poseobj.position.y,poseobj.position.z]
        poseobjM = tf.transformations.compose_matrix(angles=tfeul,translate=tftrans)
        return poseobjM
    except:
        print("Input must be a pose object!")

        
def homogeneous2pose(Mat):
    scale, shear, angles, trans, persp = tf.transformations.decompose_matrix(Mat)
    quat = tf.transformations.quaternion_from_euler(angles[0],angles[1],angles[2])
    poseobj = geometry_msgs.msg.Pose()
    poseobj.orientation.x = quat[0]
    poseobj.orientation.y = quat[1]
    poseobj.orientation.z = quat[2]
    poseobj.orientation.w = quat[3]
    poseobj.position.x = trans[0]
    poseobj.position.y = trans[1]
    poseobj.position.z = trans[2]
    return poseobj

def pose_diff(p1,p2):
    p1M = pose2homogeneousM(p1)
    p2M = pose2homogeneousM(p2)
    return  homogeneous2pose(p2M.dot(tf.transformations.inverse_matrix(p1M)))

Recommended Posts

Calculate Pose and Transform differences in Python with ROS
Calculate and display standard weight with python
Differences in authenticity between Python and JavaScript
Differences between Ruby and Python in scope
Differences in syntax between Python and Java
Dealing with "years and months" in Python
Differences in multithreading between Python and Jython
Calculate and draw bounded (closed) Voronoi diagrams in Python
Precautions when dealing with ROS MultiArray types in Python
Start numerical calculation in Python (with Homebrew and pip)
Programming with Python and Tkinter
Python and hardware-Using RS232C with Python-
Working with LibreOffice in Python
Scraping with chromedriver in python
Working with sounds in Python
Scraping with Selenium in Python
Scraping with Tor in Python
Tweet with image in Python
Combined with permutations in Python
Stack and Queue in Python
python with pyenv and venv
Unittest and CI in Python
Works with Python and R
Design and test Verilog in Python only with Veriloggen and cocotb.
Calculate the square root of 2 in millions of digits with python
Play with Mastodon's archive in Python 2 Count replies and favourites
Install CaboCha in Ubuntu environment and call it with Python.
How to log in to AtCoder with Python and submit automatically
Communicate with FX-5204PS with Python and PyUSB
Shining life with Python and OpenCV
Signal processing in Python (1): Fourier transform
Robot running with Arduino and python
Testing with random numbers in Python
Install Python 2.7.9 and Python 3.4.x with pip.
Difference between list () and [] in Python
Neural network with OpenCV 3 and Python 3
Difference between == and is in python
Scraping with Node, Ruby and Python
GOTO in Python with Sublime Text 3
Working with LibreOffice in Python: import
Scraping with Selenium in Python (Basic)
Scraping with Python, Selenium and Chromedriver
I want to solve APG4b with Python (only 4.01 and 4.04 in Chapter 4)
Scraping with Python and Beautiful Soup
Manipulate files and folders in Python
Calculate free-space path loss in Python
Numer0n with items made in Python
Open UTF-8 with BOM in Python
Assignments and changes in Python objects
Try to calculate Trace in Python
JSON encoding and decoding with python
Differences between Python, stftime and strptime
Use rospy with virtualenv in Python3
Check and move directories in Python
Hadoop introduction and MapReduce with Python
[GUI with Python] PyQt5-Drag and drop-
Draw a watercolor illusion with edge detection in Python3 and openCV3
Ciphertext in Python: IND-CCA2 and RSA-OAEP
Hashing data in R and Python
Reading and writing NetCDF with Python
Use Python in pyenv with NeoVim