I am starting with the wall follower code for the project, and I am trying to make sure my subscriber is working. But when I try to print anything in the code any where, nothing is being output. I tried pasting my code in the code assistant but found nothing.
Here is the code:
import rclpy
from rclpy.node import Node
from sensor_msgs.msg import LaserScan
from geometry_msgs.msg import Twist
from rclpy.callback_groups import ReentrantCallbackGroup
from rclpy.executors import MultiThreadedExecutor
class WallFollower(Node):
def __init__(self):
super().__init__('wall_follower')
self.scan = LaserScan()
self.laser_sub = self.create_subscription(
LaserScan,
'scan',
self.get_laser,
10,
callback_group=ReentrantCallbackGroup()
)
self.move_pub = self.create_publisher(Twist, 'cmd_vel', 10)
self.move = Twist()
def get_laser(self, msg):
self.scan = msg.ranges
self.get_logger().debug(f"Length of scan: {len(self.scan)}")
def main(args=None):
rclpy.init(args=args)
follower_node = WallFollower()
executor = MultiThreadedExecutor(num_threads=4)
executor.add_node(follower_node)
executor.spin()
if __name__ == '__main__':
main()
Hello Abboud!
I am a bit rusty on ROS2, but I would like to know if you call this node using a launch file.
If this is the case, I remember if you want to see the output of the node, you would have to set an argument typically
Thank you for the help but I am not familiar with argument setting, from what I know my args is None. The thing is this never happened before so I am abit lost.
There are no problems in your launch file or your setup.py file.
I just noted something after taking a second look into your code.
Your code does not print anything in the info logger. The only working function you have at the moment is the scan callback that prints the length in debug loggger.
Since you have not set the logging level to debug, you are unable to see the debug messages on your console output.
Fix:
Change this line: self.get_logger().debug(f"Length of scan: {len(self.scan)}")
to this line: self.get_logger().info(f"Length of scan: {len(self.scan)}")
I guess your main function is being triggered, since the message was debug you were unable to see them, which made you conclude that the program did not enter the main function.
Try this fix and let me know if it worked for you.
Also by the way, you can also enable the printing of debug messages in your console output.
You need to set logging level to debug in your program. This is explained in the ROS2 Basics course.
Here is a quick-look:
class MyNode(Node):
def __init__(self):
super().__init__("my_node_name")
# the line below sets the logger level
rclpy.logging.set_logger_level("my_node_name",
rclpy.logging.LoggingSeverity.DEBUG)
# other code stuff
return None
Also set colors for the console output so debug statements will have different colors.
In your launch file:
from launch import LaunchDescription
from launch_ros.actions import Node
def generate_launch_description():
return LaunchDescription([
Node(
package='wall_follower',
executable='follow_node',
emulate_tty=True, # <--- add this line for colored output
output='screen'),
])