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!