#! /usr/bin/env python
import rospy
from geometry_msgs.msg import Twist
from std_msgs.msg import Int32
from sensor_msgs.msg import LaserScan
def readings(msg):
front = msg.ranges[359]
right = msg.ranges[0]
left = msg.ranges[719]
if (front < 1.0):
move.linear.x = 0.5
move.angular.z = 0.5
elif (right < 1.0):
move.linear.x = 0.5
move.angular.z = 0.5
pub.publish(move)
rospy.init_node('topics_quiz_node')
sub = rospy.Subscriber('/kobuki/laser/scan', LaserScan, readings)
pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
#move = Twist()
#move.linear.x = 0.5
while pub.get_num_connections() < 1:
#pub.publish(move)
readings(LaserScan)
pass
What i doing wrong in function readings().?
front = msg.ranges[359]
TypeError: 'member_descriptor' object is not subscriptable
This the error i get.
How can i access laserReadings?
I can print laserReading without any issues but cant work with this data.
For example :
print msg.ranges[359]
will print the range to a wall, but when i trying to make comparison in if block ,its trow error.
You don’t need to call the readings function yourself. The subscriber does that automatically. Remove that line.
Also, it looks like your code is not properly indented in the readings function. For the benefit of the doubt, it should be something like this:
#! /usr/bin/env python
import rospy
from geometry_msgs.msg import Twist
from sensor_msgs.msg import LaserScan
# Use the same indentation (amount of spaces) in the same Python file
# You can just use the tab for convenience
def readings(msg):
front = msg.ranges[359]
right = msg.ranges[0]
left = msg.ranges[719]
# Looks like you still need to fine-tune this logic
# You see to that. Let's get the structure right first
if (front < 1.0):
move.linear.x = 0.5
move.angular.z = 0.5
elif (right < 1.0):
move.linear.x = 0.5
move.angular.z = 0.5
# This should be on the same level as the if/elif statements
pub.publish(move)
rospy.init_node('topics_quiz_node')
# Remove the `sub` variable, since we are not using it anywhere
# This subscriber will automatically call the `readings` function
rospy.Subscriber('/kobuki/laser/scan', LaserScan, readings)
pub = rospy.Publisher('/cmd_vel', Twist, queue_size=1)
move = Twist()
move.linear.x = 0.2 # go slow first!
while pub.get_num_connections() < 1:
# We don't need any actual logic here
# Just waiting for the publisher to be ready
print("Waiting for the publisher to be ready...")
# Now that we are out of the loop, it means
# the publisher is ready. Now shoot the first shot by publishing to /cmd_vel.
# Then use rospy.spin() so that the program does not end there!
pub.publish(move)
rospy.spin() # --> Loop until manually stopped by Ctrl + C.