Subscriber isn't reading anything from LaserScan topic

Hi, I’m a little confused as to where I’m going wrong with the Topics Quiz.

I’ve only gotten the robot to move and turn left for now, but I think I’m not actually using the data from the subscriber? When I do rostopic echo /kobuki/laser/scan, nothing is returned.

Can someone check my code and see where I’m going wrong or what I’ve misunderstood about the quiz? My launcher is defined correctly.

#! /usr/bin/env python

import rospy
from geometry_msgs.msg import Twist
from sensor_msgs.msg import LaserScan

rospy.init_node('tp')
pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1)                                           
rate = rospy.Rate(2)
move = Twist()

#function that recieves parameter msg and prints data in msg
def callback(msg):
    a = msg.ranges[360]
    print(a)
    if a > 1 :
        move.linear.x = 0.5
        move.angular.z = 0
    elif a < 1:
        move.angular.z = 0.5
        move.linear.x = 0


#sub object listens to topic and calls callback every time it reads
sub = rospy.Subscriber('/kobuki/laser/scan', LaserScan, callback)

while not rospy.is_shutdown():
  # Publish the message
  pub.publish(move)
  # Make sure the publish rate maintains at 2 Hz
  rate.sleep()

Thank you!

Hi,

Try it with “rospy.spin()” instead of the while loop first.

Also , everytime you have ssieus with topics, check with the rostopic echo /topic, that the messages are arriving, then try it in the python script, just in case the robot for some reason is not publishing or you are subscribed to the wrong topic

rospy.spin() fixed my issue. thanks!

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