ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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

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.

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.

And if you want to use the value of call back from use() function you need to declare the variable in the class init function and then you can be able to access all functions. You should have a look at just the basics of python before starting ros with python..

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.

And if you want to use the value of call back from use() function you need to declare the variable in the class init function and then you can be able to access all functions. You should have a look at just the basics of python before starting ros with python..

functions.

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 use access the value of call back from callback() and want to use it into use() function you need to declare it as self.variable_name at the variable in time of initializing the class init function and then you can be able to access all functions.

class.

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.