The support library nottf2#
Warning
This topic is under construction and this might not even be its final form. Please feel free to open an issue if you spot any typos or other problems.
Danger
You do not need this support library if you’re just creating basic rotations. You can create easy rotations directly using
math.cos, math.sin, and the class Quaternion part of geometry_msgs.
The support library becomes useful if you need to, for instance, multiply quaternions to obtain relative rotations with quaternion_multiply.
In rclpy, tf2 does not (currently?) have quaternion operations. But I got you covered. Kinda. It’s more use at your
own risk type of arrangement.
For the library to be properly found in our environment, it needs to be installed in the system’s Python. We can do so as follows.
python3 -m pip install nottf2 --break-system-packages
Caution
The nottf2 library is provided as-is, with no warranty. Use at your own risk (See MIT license for more details).
The creator of nottf2, me, will not accept any blame or liability for any issues, including but limited to broken
robots or broken code in your assignments. If you feel it’s insufficient or unreliable, please create and use your own
implementation, or someone else’s that fits your needs.
Create ros2 package that uses nottf2#
Let’s first create our sample package, as follows, that will depend on geometry_msgs.
cd ~/ros2_tutorial_workspace/src
ros2 pkg create python_package_that_uses_nottf2 \
--build-type ament_python \
--dependencies geometry_msgs
We will be presented with the usual output.
ros2 pkg create output
going to create a new package
package name: python_package_that_uses_nottf2
destination directory: ~/ros2_tutorial_workspace/src
package format: 3
version: 0.0.0
description: TODO: Package description
maintainer: ['root <murilo.marinho@manchester.ac.uk>']
licenses: ['TODO: License declaration']
build type: ament_python
dependencies: []
creating folder ./python_package_that_uses_nottf2
creating ./python_package_that_uses_nottf2/package.xml
creating source folder
creating folder ./python_package_that_uses_nottf2/python_package_that_uses_nottf2
creating ./python_package_that_uses_nottf2/setup.py
creating ./python_package_that_uses_nottf2/setup.cfg
creating folder ./python_package_that_uses_nottf2/resource
creating ./python_package_that_uses_nottf2/resource/python_package_that_uses_nottf2
creating ./python_package_that_uses_nottf2/python_package_that_uses_nottf2/__init__.py
creating folder ./python_package_that_uses_nottf2/test
creating ./python_package_that_uses_nottf2/test/test_copyright.py
creating ./python_package_that_uses_nottf2/test/test_flake8.py
creating ./python_package_that_uses_nottf2/test/test_pep257.py
[WARNING]: Unknown license 'TODO: License declaration'. This has been set in the package.xml, but no LICENSE file has been created.
It is recommended to use one of the ament license identifiers:
Apache-2.0
BSL-1.0
BSD-2.0
BSD-2-Clause
BSD-3-Clause
GPL-3.0-only
LGPL-3.0-only
MIT
MIT-0
Package structure#
Highlighted below are the files that we will create or modify.
python_package_that_uses_nottf2/
|-- package.xml
|-- python_package_that_uses_nottf2
| |-- __init__.py
| `-- operations_showcase.py
|-- resource
| `-- python_package_that_uses_nottf2
|-- setup.cfg
|-- setup.py
`-- test
|-- test_copyright.py
|-- test_flake8.py
`-- test_pep257.py
Add sample code for nottf2#
Create the following sample Python script. It will serve to show the operations we did earlier mathematically.
1from math import pi
2from geometry_msgs.msg import Quaternion
3import marinholab.nottf2 as ntf2
4
5def main():
6 phi: float = pi
7
8 r1: Quaternion = ntf2.rx(phi=phi)
9 print(f"The rotation of {phi} radians about the x-axis is r1={r1}.")
10
11 r1_conj : Quaternion = ntf2.rotation_inverse(r1)
12 print(f"The inverse rotation of r1 is r1_conj={r1_conj}.")
13
14 r2: Quaternion = ntf2.rz(phi=phi)
15 print(f"The rotation of {phi} radians about the z-axis is r2={r2}.")
16
17 r12: Quaternion = ntf2.quaternion_multiply(r1, r2)
18 print(f"The quaternion multiplication of r12=r1r2 is r12={r12}.")
19
20if __name__ == '__main__':
21 main()
Update the setup.py#
As usual, we add the necessary entry point in setup.py. We also add nottf2 as an usual Python dependency, although,
in colcon, this currently seems to be mostly cosmetic.
from rosdistro.cli.rosdistro import depends
from setuptools import find_packages, setup
package_name = 'python_package_that_uses_nottf2'
setup(
name=package_name,
version='0.0.0',
packages=find_packages(exclude=['test']),
data_files=[
('share/ament_index/resource_index/packages',
['resource/' + package_name]),
('share/' + package_name, ['package.xml']),
],
install_requires=['setuptools', 'nottf2'],
zip_safe=True,
maintainer='root',
maintainer_email='murilo.marinho@manchester.ac.uk',
description='TODO: Package description',
license='TODO: License declaration',
tests_require=['pytest'],
entry_points={
'console_scripts': [
'operations_showcase = python_package_that_uses_nottf2.operations_showcase:main',
],
},
)
Build and source#
Before we proceed, let us build and source once.
cd ~/ros2_tutorial_workspace
colcon build
source install/setup.bash
Note
For additional explanation and troubleshooting tips, see Always source after you build.
Warning
colcon will not work properly if your terminal has an active venv.
Run Example#
We run our newly created program as follows.
ros2 run python_package_that_uses_nottf2 operations_showcase
The result will be as follows.
The rotation of 3.141592653589793 radians about the x-axis is r1=geometry_msgs.msg.Quaternion(x=1.0, y=0, z=0, w=6.123233995736766e-17).
The inverse rotation of r1 is r1_conj=geometry_msgs.msg.Quaternion(x=-1.0, y=0, z=0, w=6.123233995736766e-17).
The rotation of 3.141592653589793 radians about the z-axis is r2=geometry_msgs.msg.Quaternion(x=0, y=0, z=1.0, w=6.123233995736766e-17).
The quaternion multiplication of r12=r1r2 is r12=geometry_msgs.msg.Quaternion(x=6.123233995736766e-17, y=-1.0, z=6.123233995736766e-17, w=3.749399456654644e-33).
Note that the results are reasonably close to the ones we calculated mathematically. However, given the limitations on current computers related to floating point accuracy, you can always expect a level of inaccuracy. This is not limited or affected by the use of quaternions, this is an inherent limitation of our computers.