The subscriber callback is not used

I created a subscriber and a callback function, however, it is not used. Can anyone help me take a look and see what is the issue?

import rclpy
from rclpy.node import Node
from sensor_msgs.msg import LaserScan
from geometry_msgs.msg import Twist
from rclpy.qos import ReliabilityPolicy, QoSProfile

class WallFollower(Node):
    def __init__(self):

        self.publisher_ = self.create_publisher(Twist, 'cmd_vel', 10)
        self.subscriber = self.create_subscription(
            QoSProfile(depth=10, reliability=ReliabilityPolicy.BEST_EFFORT)
        self.laser_forward = 0
        self.laser_right = 0
        self.laser_back = 0
        self.laser_left = 0
        self.cmd = Twist()

        self.timer_period = 0.05
        self.timer = self.create_timer(self.timer_period, self.motion)
    def laser_scan_callback(self, msg):        
        self.get_logger().info('I receive: "%s"' % str(msg))
    def motion(self):

def main(args=None):
    wall_follower = WallFollower()

if __name__ == '__main__':

Hello @bd16 ,

What do you mean by “it’s not used”? The code looks ok on a 1st look. Are you getting any errors?

I suppose you mean that the callback was not called?

First, you don’t need to call it manually like you are doing in __init__. It’s automatically called when the subscriber gets a LaserScan message.

Second, (maybe this is the issue), are you shutting the node down (as I see in main) so fast that the subscriber does not have the opportunity to work?

It looks like your program is just running once then exiting. If you want the subscriber to work, the program needs to stay open until you stop it manually or based on a given condition in your logic.

I am not getting any errors. As @bayodesegun said, my callback function is not called. I thought using rclpy.spin(wall_follower) will keep the node running until ctrl+C.

Also, currently is the server for the ros2 5days python rosject down? I can’t access the rosject. If you have any information on that would you also let me know?


I thought rclpy.spin(wall_follower) will keep the node running?

That’s right, my mistake. I was confusing it with ROS1.

Two more things you can check is:

  • Is the /scan topic available in the simulation or real robot? Is it the correct topic?
  • Is your node recognized as a subscriber to the topic?
ros2 topic list
user:~$ ros2 topic info /scan
Type: sensor_msgs/msg/LaserScan
Publisher count: 1
Subscription count: 0   # should be > 0

Are you getting any errors?

No errors, just when I open the rosject what appears in the IDE’s file tree is the files I created in the courses not the rosject.

Oh, I see. Apologies for that glitch.

Please delete that rosject and make another copy.