Section 3: topics_quiz Node output terminal correctly but ros2 node list does not appear

This is my feedback of the last attempt

It said that I didn’t subcribe /odom correctly or wrong name of the node. So I use ros2 node list to checking whether topics_quiz_node is working but its not. Even the terminal output is printing.


I don’t know why the topics_quiz_node not init, even though I follow the template in the lesson. And 1 thing weird, why my terminal still running but ros2 node topic cannot regcognize my node.

This is my

import rclpy
from rclpy.node import Node
from geometry_msgs.msg import Twist
from nav_msgs.msg import Odometry
import numpy as np

class TopicsQuizNode(Node):

    def __init__(self):
        self.publisher_ = self.create_publisher(Twist, '/cmd_vel', 10)
        self.subscription_ = self.create_subscription(Odometry, '/odom', self.odom_callback, 10)
        self.is_turned_left = False
        self.start_time = None

    def odom_callback(self, msg):
        # Extract orientation data from odometry message
        orientation = msg.pose.pose.orientation

        # Calculate euler angles from quaternion orientation
        roll, pitch, yaw = self.euler_from_quaternion([orientation.x, orientation.y, orientation.z, orientation.w])

        # Print current orientation
        self.get_logger().info(f"Current orientation: yaw={yaw}")

        # Get the current time in seconds
        current_time = self.get_clock().now().to_msg().sec

        # If start_time is not set, set it to the current time
        if self.start_time is None:
            self.start_time = current_time

        # Logic to follow trajectory
        if current_time - self.start_time < 23:
            self.send_velocity(0.2, 0.0, 0.0, 0.0)  # Move forward for 23 seconds
        elif not self.is_turned_left:
            if yaw < 1.57:  # Rotate until reaching 90 degrees (1.57 radians)
                self.send_velocity(0.0, 0.0, 0.2, 0.0)  # Rotate left (angular_z=0.2)
                self.is_turned_left = True
                self.get_logger().info("Turning left done")
        elif current_time - self.start_time < 63:
            self.send_velocity(0.2, 0.0, 0.0, 0.0)  # Move forward for 5 seconds after left turn
            self.send_velocity(0.0, 0.0, 0.0, 0.0)  # Stop the robot

    def send_velocity(self, linear_x, linear_y, angular_z, duration):
        msg = Twist()
        msg.linear.x = linear_x
        msg.linear.y = linear_y
        msg.angular.z = angular_z
        self.get_logger().info(f"Sending command: linear_x={linear_x}, angular_z={angular_z}")
        self.get_logger().info("Duration: {}".format(duration))

    def euler_from_quaternion(self, quaternion):
        x, y, z, w = quaternion

        sinr_cosp = 2 * (w * x + y * z)
        cosr_cosp = 1 - 2 * (x * x + y * y)
        roll = np.arctan2(sinr_cosp, cosr_cosp)

        sinp = 2 * (w * y - z * x)
        pitch = np.arcsin(sinp)

        siny_cosp = 2 * (w * z + x * y)
        cosy_cosp = 1 - 2 * (y * y + z * z)
        yaw = np.arctan2(siny_cosp, cosy_cosp)

        return roll, pitch, yaw

def main(args=None):
    node = TopicsQuizNode()
    rclpy.logging.set_logger_level("topics_quiz_node", rclpy.logging.LoggingSeverity.INFO)
        node.send_velocity(0.0, 0.0, 0.0, 0.0)  # Make sure the robot stops before shutting down

if __name__ == '__main__':

from setuptools import setup
import os
from glob import glob

package_name = 'topics_quiz'

            ['resource/' + package_name]),
        ('share/' + package_name, ['package.xml']),
        (os.path.join('share', package_name), glob('launch/*'))
    description='TODO: Package description',
    license='TODO: License declaration',
        'console_scripts': [
            'topics_quiz_node = topics_quiz.topics_quiz_node:main'


<?xml version="1.0"?>
<?xml-model href="" schematypens=""?>
<package format="3">
  <description>TODO: Package description</description>
  <maintainer email="user@todo.todo">user</maintainer>
  <license>TODO: License declaration</license>




And lastly file

from launch import LaunchDescription
from launch.actions import DeclareLaunchArgument, LogInfo
from launch.substitutions import LaunchConfiguration
from launch_ros.actions import Node

def generate_launch_description():
    return LaunchDescription([

Thanks for your correcting, I’m figuring out where am I wrong but seem hopeless :smiley:

Can you retry a few times? Also check that there is anything running that you left there. Maybe change of course and come back to be sure that there isn’t anything running there from previous attempts.

PLease let us know if it keeps happening.

> Blockquote

Here it is still happening, but I just think did I miss match something…

Ok please if you are still stuck , tell us. Maybe there is something we ar missing here

Hello @nhnghiaworks ,

Your last failed attempt shows in the logs that there was a problem with the /odom topic itself in the simulation, so not related to your code. This is the error message that appears in the logs:

Unknown topic '/odom'

As you can see, it looks like the /odom topic just died in your simulation for some reason. I’ve just tested your code in my environment and it successfully passed the odometry check, as you can see here:

It failed in the last part because the robot didn’t reach the expected goal position.

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