ROS2 Project topics problem

HI,

I am doing the topics project in ROS2 python course. But, the following error happens when I try to subscribe to the /scan or publish in /cmd_vel:

user:~/ros2_ws$ ros2 launch wall_follower start_wall_following.launch.py
[INFO] [launch]: All log files can be found below /home/user/.ros/log/2023-05-09-21-10-12-024513-2_xterm-24267
[INFO] [launch]: Default logging verbosity is set to INFO
[INFO] [wall_following_node-1]: process started with pid [24318]
[wall_following_node-1] Traceback (most recent call last):
[wall_following_node-1] File “/home/user/ros2_ws/install/wall_follower/lib/wall_follower/wall_following_node”, line 33, in
[wall_following_node-1] sys.exit(load_entry_point(‘wall-follower==0.0.0’, ‘console_scripts’, ‘wall_following_node’)())
[wall_following_node-1] File “/home/user/ros2_ws/install/wall_follower/lib/python3.8/site-packages/wall_follower/wall_following.py”, line 92, in main
[wall_following_node-1] wall_following_node = WallFollowing()
[wall_following_node-1] File “/home/user/ros2_ws/install/wall_follower/lib/python3.8/site-packages/wall_follower/wall_following.py”, line 19, in init
[wall_following_node-1] self.publisher_ = self.create_publisher(Twist, ‘cmd_vel’, 2)
[wall_following_node-1] File “/opt/ros/foxy/lib/python3.8/site-packages/rclpy/node.py”, line 1140, increate_publisher
[wall_following_node-1] check_for_type_support(msg_type)
[wall_following_node-1] File “/opt/ros/foxy/lib/python3.8/site-packages/rclpy/type_support.py”, line 20, in check_for_type_support
[wall_following_node-1] ts = msg_type.class._TYPE_SUPPORT
[wall_following_node-1] AttributeError: type object ‘type’ has no attribute ‘_TYPE_SUPPORT’ This might be a ROS 1 message type but it should be a ROS 2 message type. Make sure to source your ROS 2 workspace after your ROS 1 workspace.
[ERROR] [wall_following_node-1]: process has died [pid 24318, exit code 1, cmd ‘/home/user/ros2_ws/install/wall_follower/lib/wall_follower/wall_following_node --ros-args’].

The ROS1 is sourced first, then the ROS2. The topics can be viewed using ROS1 and ROS2 command line instructions, but when running the python scripts it does not work.

Please help,

Thanks.

Hi @eblady ,

I am assuming that you have not added geometry_msgs as a dependency in your package.

If you do not mind sharing your code, please post the python program as a code-block.
Along with it, also post your package.xml and your setup.py as code-clocks.

Regards,
Girish

Hi,

Thanks for the reply. I included the geometry_msgs into the package.xml, but same error. This my package.xml and setup.py files:


package.xml

<?xml version="1.0"?> <?xml-model href="http://download.ros.org/schema/package_format3.xsd" schematypens="http://www.w3.org/2001/XMLSchema"?> wall_follower 0.0.0 TODO: Package description user TODO: License declaration

rclpy
std_msgs
sensor_msgs
geometry_msgs

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

ament_python

setup.py.

from setuptools import setup
import os
from glob import glob

package_name = ‘wall_follower’

setup(
name=‘wall_follower’,
version=‘0.0.0’,
packages=[package_name],
data_files=[
(‘share/ament_index/resource_index/packages’,
[‘resource/’ + package_name]),
(‘share/’ + package_name, [‘package.xml’]),
(os.path.join(‘share’, package_name), glob(‘launch/*.launch.py’))
],
install_requires=[‘setuptools’],
zip_safe=True,
maintainer=‘user’,
maintainer_email=‘user@todo.todo’,
description=‘TODO: Package description’,
license=‘TODO: License declaration’,
tests_require=[‘pytest’],
entry_points={
‘console_scripts’: [
‘wall_following_node = wall_follower.wall_following:main’
],
},
)


thanks.

Hi @eblady ,

I wanted to check your program for any inconsistencies.

Also I cannot exactly verify the package.xml if the contents are not enclosed in “preformatted-text” format.

Please edit your previous post so that your package.xml and setup.py is properly formatted as a code-block.

Also, don’t forget to post your main code, I believe the error is in your main program.

Please refer here on how to format a text into a code-block.

Regards,
Girish

Hi,

This is the package.xml

<?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>wall_follower</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>sensor_msgs</depend>
  <depend>geometry_msgs</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>

This is the setup.py

from setuptools import setup
import os
from glob import glob

package_name = 'wall_follower'

setup(
    name='wall_follower',
    version='0.0.0',
    packages=[package_name],
    data_files=[
        ('share/ament_index/resource_index/packages',
            ['resource/' + package_name]),
        ('share/' + package_name, ['package.xml']),
        (os.path.join('share', package_name), glob('launch/*.launch.py'))
    ],
    install_requires=['setuptools'],
    zip_safe=True,
    maintainer='user',
    maintainer_email='user@todo.todo',
    description='TODO: Package description',
    license='TODO: License declaration',
    tests_require=['pytest'],
    entry_points={
        'console_scripts': [
            'wall_following_node = wall_follower.wall_following:main'
        ],
    },
)

An this is the wall_following.py

import rclpy
import numpy as np
import math
# import the ROS2 python libraries
from rclpy.node import Node
# import the LaserScan module from sensor_msgs interface
from sensor_msgs.msg import LaserScan
from geometry_msgs.msg import Twist
# import Quality of Service library, to set the correct profile and reliability to read sensor data.
from rclpy.qos import ReliabilityPolicy, QoSProfile

########################################
class WallFollowing(Node):

    def __init__(self):
        super().__init__('wall_following_node')
        # publiser
        self.publisher_ = self.create_publisher(Twist, 'cmd_vel', 2)
        # twist
        self.myCMDTwist = Twist()
        self.myCMDTwist.linear.x = 0.0
        self.myCMDTwist.angular.z = 0.0

        self.myFlagFollow = 0

        self.subscriber = self.create_subscription(
            LaserScan,
            'scan',
            self.listener_scan_callback,
            QoSProfile(depth=2, reliability=ReliabilityPolicy.RELIABLE))

    def listener_scan_callback(self, msg):

        minRightDist = min(msg.ranges[90:110])
        minRightFrontDist = min(msg.ranges[125:145])
        minFrontDist = min(msg.ranges[170:190])
        minLeftFrontDist = min(msg.ranges[215:235])
        minLeftDist = min(msg.ranges[250:270])
        minBackDist = min(min(msg.ranges[350:359]), min(msg.ranges[0:9]))

        print("r: %.3f rf: %.3f f: %.4f lf: %.3f l: %.3f b: %.3f fl: %d" % (minRightDist, minRightFrontDist, minFrontDist, minLeftFrontDist, minLeftDist, minBackDist, myFlagFollow))

        # dMin = 0.2 / np.cos(45*np.pi/180)
        # dMax = 0.3 / np.cos(45*np.pi/180)
        # dFront = 0.5
        # linVel = 0.025
        # angVel = 0.15
        # angVelFront = 0.3

        # if self.myFlagFollow == 0 and minRightFrontDist > dMax:
        #     self.myFlagFollow = 2 # Follow left wall
        #     print('Following left wall...')
        # elif self.myFlagFollow == 0 and minLeftFrontDist > dMax: 
        #     self.myFlagFollow = 1 # Follow right wall
        #     print('Following right wall...')

        # if self.myFlagFollow == 1:
        #     if minFrontDist > dFront and minRightFrontDist > dMin and minRightFrontDist < dMax:
        #         self.myCMDTwist.linear.x = linVel
        #         self.myCMDTwist.angular.z = 0.0
        #     elif minFrontDist < dFront and minRightFrontDist > dMin and minRightFrontDist < dMax:
        #         self.myCMDTwist.linear.x = linVel
        #         self.myCMDTwist.angular.z = angVelFront
        #     elif minFrontDist > dFront and minRightFrontDist > dMax:
        #         self.myCMDTwist.linear.x = linVel
        #         self.myCMDTwist.angular.z = -angVel
        #     elif minFrontDist > dFront and minRightFrontDist < dMin:
        #         self.myCMDTwist.linear.x = linVel
        #         self.myCMDTwist.angular.z = angVel
        # elif self.myFlagFollow == 2:
        #     if minFrontDist > dFront and minLeftFrontDist > dMin and minLeftFrontDist < dMax:
        #         self.myCMDTwist.linear.x = linVel
        #         self.myCMDTwist.angular.z = 0.0
        #     elif minFrontDist < dFront and minLeftFrontDist > dMin and minLeftFrontDist < dMax:
        #         self.myCMDTwist.linear.x = linVel
        #         self.myCMDTwist.angular.z = -angVelFront
        #     elif minFrontDist > dFront and  minLeftFrontDist > dMax:
        #         self.myCMDTwist.linear.x = linVel
        #         self.myCMDTwist.angular.z = angVel
        #     elif minFrontDist > dFront and  minLeftFrontDist < dMin:
        #         self.myCMDTwist.linear.x = linVel
        #         self.myCMDTwist.angular.z = -angVel

        self.publisher_.publish(self.myCMDTwist)

########################################
def main(args=None):
    # initialize the ROS communication
    rclpy.init(args=args)
    # declare the node constructor topics_quiz_node
    wall_following_node = WallFollowing()
    # pause the program execution, waits for a request to kill the node (ctrl+c)
    rclpy.spin(wall_following_node)
    # Explicity destroy the node
    wall_following_node.destroy_node()
    # shutdown the ROS communication
    rclpy.shutdown()

########################################

if __name__ == '__main__':
    main()

Thanks.

Hi @eblady ,

Your program and files seems to be correct. Although I believe there are two culprits here.

  1. Add the ‘/’ in front of the topic name:
self.publisher_ = self.create_publisher(Twist, '/cmd_vel', 2)  # added / before cmd_vel
  1. Delete the build, install and log folders. Execute the following commands:
cd ~/ros2_ws
rm -f ./build ./install ./log
cd ~
source /opt/ros/noetic/setup.bash
source /opt/ros/foxy/setup.bash
cd ~/ros2_ws
colcon build --packages-select wall_follower && source install/setup.bash
ros2 launch wall_follower <your_launch_file_name>.launch.py

Try this and let me know if this fixed your problem.

Regards,
Girish

Hi,

Nop, not working:

[INFO] [launch]: All log files can be found below /home/user/.ros/log/2023-05-10-16-51-28-344162-4_xterm-14759
[INFO] [launch]: Default logging verbosity is set to INFO
[INFO] [wall_following_node-1]: process started with pid [14824]
[wall_following_node-1] Traceback (most recent call last):
[wall_following_node-1]   File "/home/user/ros2_ws/install/wall_follower/lib/wall_follower/wall_following_node", line 33, in <module>
[wall_following_node-1]     sys.exit(load_entry_point('wall-follower==0.0.0', 'console_scripts', 'wall_following_node')())
[wall_following_node-1]   File "/home/user/ros2_ws/install/wall_follower/lib/python3.8/site-packages/wall_follower/wall_following.py", line 91, in main
[wall_following_node-1]     wall_following_node = WallFollowing()
[wall_following_node-1]   File "/home/user/ros2_ws/install/wall_follower/lib/python3.8/site-packages/wall_follower/wall_following.py", line 18, in __init__
[wall_following_node-1]     self.publisher_ = self.create_publisher(Twist, '/cmd_vel', 2)
[wall_following_node-1]   File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/node.py", line 1140, in create_publisher
[wall_following_node-1]     check_for_type_support(msg_type)
[wall_following_node-1]   File "/opt/ros/foxy/lib/python3.8/site-packages/rclpy/type_support.py", line 20, in check_for_type_support
[wall_following_node-1]     ts = msg_type.__class__._TYPE_SUPPORT
[wall_following_node-1] AttributeError: type object 'type' has no attribute '_TYPE_SUPPORT'This might be a ROS 1 message type but it should be a ROS 2 message type. Make sure to source your ROS 2 workspace after your ROS 1 workspace.
[ERROR] [wall_following_node-1]: process has died [pid 14824, exit code 1, cmd '/home/user/ros2_ws/install/wall_follower/lib/wall_follower/wall_following_node --ros-args'].

In addition, using the command line I try to check publishing in the cmd_vel:

user:~/ros2_ws$ ros2 topic pub --once /cmd_vel geometry_msgs/msg/Twist "{linear: {x: 0.0, y: 0.0, z: 0.0}, angular: {x: 0.0, y: 0.0, z: 0.1}}"
Traceback (most recent call last):
  File "/opt/ros/foxy/bin/ros2", line 11, in <module>
    load_entry_point('ros2cli==0.9.9', 'console_scripts', 'ros2')()
  File "/opt/ros/foxy/lib/python3.8/site-packages/ros2cli/cli.py", line 67, in main
    rc = extension.main(parser=parser, args=args)
  File "/opt/ros/foxy/lib/python3.8/site-packages/ros2topic/command/topic.py", line 41, in main
    return extension.main(args=args)
  File "/opt/ros/foxy/lib/python3.8/site-packages/ros2topic/verb/pub.py", line 97, in main
    return main(args)
  File "/opt/ros/foxy/lib/python3.8/site-packages/ros2topic/verb/pub.py", line 107, in main
    return publisher(
  File "/opt/ros/foxy/lib/python3.8/site-packages/ros2topic/verb/pub.py", line 131, in publisher
    msg_module = get_message(message_type)
  File "/opt/ros/foxy/lib/python3.8/site-packages/rosidl_runtime_py/utilities.py", line 30, in get_message
    raise ValueError("Expected the full name of a message, got '{}'".format(identifier))
ValueError: Expected the full name of a message, got 'geometry_msgs/msg/Twist'

Same happens with the scan topic…

Any ideas?

thanks

Hi @eblady ,

This post could be the solution: ROS2 Topics "ros2 topic echo /cmd_vel" fails - #7 by albertoezquerro

What you have to do is:

  1. Reset all the terminals on the course, that is, by clicking the red X button on every webshell.
  2. Then on every webshell that you will be using, you must type in these two commands before executing any other commands:
source /opt/ros/foxy/setup.bash
source /home/simulations/ros2_sims_ws/install/setup.bash

I believe this should definitely fix your problem. Let me know if you still have issues.

Regards,
Girish

Hi Girish,

Thanks for your advice, it worked !!! I really appreciate your time in solving this.

my best regards,

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