Ask Your Question
0

How to access the value from the subscriber callback

asked 2021-11-11 08:40:41 -0500

Azeem gravatar image

i want to use the variable value from subscriber callback function on other function.

 #! /usr/bin/env python3
import rospy
from sensor_msgs.msg import LaserScan


class path_planning:
    def __init__(self): 
        print("initalizing publisher and subscriber")
        rospy.init_node("path_plan", anonymous=True)
        self.sub = rospy.Subscriber(
            "/ebot/laser/scan", LaserScan, self.callback)
        rospy.spin()
        rospy.sleep(10)

def use(self):
    self.gg = 2
    return self.gg

def callback(self,msg):
    print("getting into callback")
    self.part_1 = msg.ranges[:145]
    self.part_2 = msg.ranges[145:289]
    self.part_3 = msg.ranges[289:433]
    self.part_4 = msg.ranges[433:577]
        self.part_5 = msg.ranges[577:]
        # print(self.part_1)

        # print(self.part_1)
        # values = splitter(self.part_1)



if __name__ =="__main__":
    path = path_planning()
    print(path.use())

i tried various ways still couldn't find one that works for me and also saw similar question on various forum. i dont know what am i doing wrong. can someone help me with that.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2021-11-11 10:41:03 -0500

Ranjit Kathiriya gravatar image

updated 2021-11-12 01:37:26 -0500

Hello @Azeem,

I think your code indentation is wrong. You need to Just put a tab in your use and a callback function.

Your code should be in this form:

#! /usr/bin/env python3
import rospy
from sensor_msgs.msg import LaserScan


class path_planning:
    def __init__(self): 
        print("initalizing publisher and subscriber")
        rospy.init_node("path_plan", anonymous=True)
        self.sub = rospy.Subscriber(
            "/ebot/laser/scan", LaserScan, self.callback)
        rospy.spin()
        rospy.sleep(10)

    def use(self):
        self.gg = 2
        return self.gg

    def callback(self,msg):
        print("getting into callback")
        self.part_1 = msg.ranges[:145]
        self.part_2 = msg.ranges[145:289]
        self.part_3 = msg.ranges[289:433]
        self.part_4 = msg.ranges[433:577]
        self.part_5 = msg.ranges[577:]
            # print(self.part_1)

            # print(self.part_1)
            # values = splitter(self.part_1)



if __name__ =="__main__":
    path = path_planning()
    print(path.use())

I think now you will be able to access your callback and use functions.

i want to use the variable value from subscriber callback function on other function.

And if you want to access the value of callback() and want to use it into use() function you need to declare it as self.variable_name at the time of initializing the class.

edit flag offensive delete link more

Comments

1

The following parts should have the same indentation as self.part_4 = msg.ranges[433:577].

    self.part_5 = msg.ranges[577:]
    # print(self.part_1)

    # print(self.part_1)
    # values = splitter(self.part_1)
miura gravatar image miura  ( 2021-11-11 17:36:06 -0500 )edit
1

I am sorry! I will edit my answer. Thanks for pointing my mistake @miura.

Ranjit Kathiriya gravatar image Ranjit Kathiriya  ( 2021-11-12 01:37:15 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2021-11-11 08:40:41 -0500

Seen: 101 times

Last updated: Nov 12 '21