ROS Topics QUiz

Hello All,

I have encounter a weird error saying that my callback function is bad for the topics quiz. The command prompt output is shown below:

[ERROR] [1592361293.990978, 1951.964000]: bad callback: <function callback at 0x7fe523238050>
Traceback (most recent call last):
File “/opt/ros/kinetic/lib/python2.7/dist-packages/rospy/topics.py”, line 750, in _invoke_callback
cb(msg)
File “/home/user/catkin_ws/src/topics_quiz/src/topics_quiz.py”, line 11, in callback
print LaserScan.ranges[360]
TypeError: ‘member_descriptor’ object has no attribute ‘getitem

(1) Here is what I wrote for the launch file

<!-- My Package launch file -->

<node pkg="topics_quiz" type="topics_quiz.py" name="topics_quiz_node"  output="screen">

</node>

(2) Here is what I wrote for the topics_quiz.py

#! /usr/bin/env python

import rospy

from geometry_msgs.msg import Twist

from sensor_msgs.msg import LaserScan

#create an object for Twist

move=Twist()

def callback(msg):

print LaserScan.ranges[360]

if msg.ranges[360] > 1.0: #go straight

    move.linear.x = 0.1

    move.angular.z = 0.0

if msg.ranges[360] < 1.0: #turn left

    move.linear.x = 0.0

    move.angular.z = 0.2



if msg.ranges[0] <1.0: #turn left again 

    move.linear.x = 0

    move.angular.z = 0.2



if msg.ranges[719] < 1.0:

    move.linear.x = 0

    move.angular.z = -0.2 #turn right

pub.publish(move)

rospy.init_node(‘topics_quiz_node’)

sub = rospy.Subscriber(’/kobuki/laser/scan’,LaserScan,callback)

pub = rospy.Publisher(’/cmd_vel’, Twist)

rospy.spin()

(3) Here is what I wrote in the command prompt
roscd catkin_ws/
source devel/setup.bash
roslaunch topics_quiz topics_quiz_launch_file.launch

I am totally confused with this error…Appreciate anyone who could help me!

The only issue I can see is this line. This should be msg.ranges[360]
Try that and see if it fixes it.

1 Like

Appreciate for the replying! The code works now.

1 Like

Trying this in noetic and it does not work, I get a traceback error stating that msg from
msg.ranges[360] > 1
is not defined. Lost on how to resolve.

Hello @617sullivandw ,

Could you share some more details on the error you are having? Are you getting this error when trying to execute your code? Could you share your code so that can have a look at it?

Best,

I have written the code but there is no output it has no error as such but the robot isn’t moving
the code is
#! /usr/bin/env python3

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

rospy.init_node(‘topics_quiz_node’)
move = Twist()
rate = rospy.Rate(2)
pub = rospy.Publisher(’/cmd_vel’, Twist, queue_size = 1)
def callback(msg):
print (msg.ranges[360])
if(msg.ranges[360] > 1.0): #go straight
move.linear.x = 0.1
move.angular.z = 0.0
if(msg.ranges[360] < 1.0): #turn left
move.linear.x = 0.0
move.angular.z = 0.2
if(msg.ranges[0] < 1.0): #turn left again
move.linear.x = 0
move.angular.z = 0.2
if(msg.ranges[719] < 1.0):
move.linear.x = 0
move.angular.z = -0.2 #turn right
pub.publish(move)
sub = rospy.Subscriber(’/kobuki/laser/scan’, LaserScan, callback)
rospy.spin()

the launch file code is

<node pkg="topics_quiz" type="avoid.py" name="topics_quiz_node"  output="screen">
</node>

Hi, there are a number of things that could be going on. Please, share your code in the correct formatting, otherwise it’s very hard to spot problems. Could you send a screenshot of what is happening? Make sure you make your code executable and that you compile your workspace correctly.