# Quiz 4.3. Cant access laser readings inside the function

Hello community.

``````#! /usr/bin/env python

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

front = msg.ranges
right = msg.ranges
left = msg.ranges

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')

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)
pass

``````

What i doing wrong in function readings().?

``````front = msg.ranges
TypeError: 'member_descriptor' object is not subscriptable
``````

This the error i get.
I can print laserReading without any issues but cant work with this data.
For example :
print msg.ranges
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

front = msg.ranges
right = msg.ranges
left = msg.ranges

# 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

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.

``````

thanks for the comprehensive aswer.
Now its work

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.
{"ft_lang_label":"__label__en","ft_lang_prob":0.6947446,"math_prob":0.53758484,"size":2672,"snap":"2023-40-2023-50","text_gpt3_token_len":718,"char_repetition_ratio":0.1221889,"word_repetition_ratio":0.17302799,"special_character_ratio":0.27657187,"punctuation_ratio":0.19210054,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96505475,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-12-01T15:34:27Z\",\"WARC-Record-ID\":\"<urn:uuid:60ba14eb-2326-4d28-86c1-e50010acb89a>\",\"Content-Length\":\"27895\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:15a77b20-9bfd-4099-a3ab-ed94b299bd8f>\",\"WARC-Concurrent-To\":\"<urn:uuid:5231e506-74a1-4149-b173-460c12c41c51>\",\"WARC-IP-Address\":\"52.215.239.48\",\"WARC-Target-URI\":\"https://get-help.robotigniteacademy.com/t/quiz-4-3-cant-access-laser-readings-inside-the-function/24130\",\"WARC-Payload-Digest\":\"sha1:LQJNRQOFBMRQ2EULPLU43QCTLANAI5NC\",\"WARC-Block-Digest\":\"sha1:FH3Z35ABNUBJUSFD5HR4PCTUGFPFCA4E\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679100290.24_warc_CC-MAIN-20231201151933-20231201181933-00404.warc.gz\"}"}