Unit3 - ModuleNotFoundError: No module named 'tf2'

Hi, I have a problem.

I check I already have the tf2 package in the environment.
I have already used “colcon build…” and "source … "
But when I import tf2 in the .py file. I got the error: No module named ‘tf2’



import rclpy
from rclpy.node import Node

from geometry_msgs.msg import Twist

from nav_msgs.msg import Odometry
from rclpy.qos import ReliabilityPolicy, QoSProfile

import tf2


class TopicQuiz(Node):
    def __init__(self, nodeName):  # example36
        super().__init__(nodeName)

        self.subscriber = self.create_subscription(
            Odometry, '/odom', self.listenerCallback,
            QoSProfile(depth=10, reliability=ReliabilityPolicy.RELIABLE)
        )
        self.position = 0
        self.orientation = 0
        self.euler = 0

        self.publisher = self.create_publisher(Twist, 'cmd_vel', 10)
        self.cmdVel = Twist()
        self.timerPeriod = 0.5
        self.timer = self.create_timer(self.timerPeriod, self.timerCallback)

    def listenerCallback(self, msg):
        self.position = msg.pose.pose.position
        self.orientation = msg.pose.pose.orientation
        # self.get_logger().info(f'''
        #     pose - {msg.pose.pose}
        #     position - {self.position}
        #     orientation - {self.orientation}
        # ''')

        self.euler = tf2.transformations.euler_from_quaternion([
            self.orientation.x, self.orientation.y, self.orientation.z, self.orientation.w
        ])
        self.get_logger().info(f'euler: {self.euler}')

    def timerCallback(self):
        pass


def main(args=None):
    rclpy.init(args=args)

    topicQuiz = TopicQuiz('topics_quiz_node')

    rclpy.spin(topicQuiz)

    topicQuiz.destroy_node()

    rclpy.shutdown()


if __name__ == '__main__':
    main()

<?xml version="1.0"?>
<?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?>
<package format="3">
  <name>topics_quiz</name>
  <version>0.0.0</version>
  <description>TODO: Package description</description>
  <maintainer email="user@todo.todo">user</maintainer>
  <license>TODO: License declaration</license>

  <depend>rclpy</depend>
  <depend>std_msgs</depend>
  <depend>geometry_msgs</depend>
  <depend>nav_msgs</depend>
  <depend>tf2</depend>

  <test_depend>ament_copyright</test_depend>
  <test_depend>ament_flake8</test_depend>
  <test_depend>ament_pep257</test_depend>
  <test_depend>python3-pytest</test_depend>

  <export>
    <build_type>ament_python</build_type>
  </export>
</package>

Thanks

Hi @NguyenDuyDuc ,

You need to import tf_transformations for euler_from_quaternion. I believe you cannot use tf2.

from tf_transformations import euler_from_quaternion

Refer: ROS Index : ROS2 Humble under Migration.

This should solve your problem. Let me know if this does not work.

Regards,
Girish

I followed your instruction but It does not work.
Please, check

Hi @NguyenDuyDuc ,

In that case, tf2 and its related packages are probably not installed in the course environment.

But, why do you want to use the tf2 library when you have euler_from_quaternion function given to you directly on the course notes? Just copy-paste it! [I can see the function on your screenshot!]

tf2 package was actually not fully implemented during Foxy version of ROS2. But you are now using Humble. I believe after Galactic (or Crystal) version, tf2 was not distributed along with ROS2 package. You must download it separately. Since you won’t be requiring tf2 for this entire course, probably the admins of this website decided not to add it to the course environment. This would be my understanding. So, don’t try to download ros2-humble-tf2-* packages.

So, yeah, just copy-paste the function and be done with this part!

Regards,
Girish

1 Like

Because I want to use libraries to get familiar with them.
I got it.
Thanks

Hi @NguyenDuyDuc ,

Thanks for understanding!

Don’t worry about this now. You can take the ROS2 TF2 course to learn about tf’s completely!

Regards,
Girish

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.