How to access and send occupancy grid data from a callback function subscribing /map to another function

asked 2018-04-23 22:34:49 -0600

akshaybj0221 gravatar image

So I made a map using the gmapping tool and I made a callback function to get the occupancy grid. But the problem is that I am not able to send that data to other function as I want to convert that into a grid.

I have 2 subscribers, one subscribing to the topic /map and the other subscribing to the topic /map_metadata. In order to do that I have to make 2 callback functions, callback and callback2() as each callback function is getting the data from each topic (Do not mind the indentation of 'o' in callback2 function. The functions are as follows:

def callback(data):
    global occupancyMap 
    occupancyMap = data.data

def callback2(data):
    res = data.resolution
    w = data.width
    h = data.height
o = data.origin

Now, I want to send the data that I got from the above mentioned callback() and callback2() into a function which will use this data to convert the list of occupancy grid into a 2-D grid. But I find myself stuck here and have not been able to send the data to main or any other function that I made.

def getOccMap(occupancyMap, res):
?????

The main function is as follows:

if __name__ == '__main__':
    try:
        rospy.init_node('map_listener', anonymous=True) #initialze node named"map_listener"

        rospy.Subscriber("/map", OccupancyGrid, callback) #subscribe to /map    

        rospy.Subscriber("/map_metadata", MapMetaData, callback2)

        print(occupancyMap) 

        rospy.spin()   # spin() simply keeps python from exiting until this node is stopped


    except rospy.ROSInterruptException:
        pass

On accessing the occupancyMap variable in the main it says there is no global variable named 'occupancyMap' defined, which I do not understand why.

I also tried the following

data = rospy.Subscriber("/map", OccupancyGrid, callback)
print (data)

But it gives me a object type value (of subscriber) rather than the actual value of the occupancyGrid.

Any help would be really appreciated! Thank you so much

edit retag flag offensive close merge delete

Comments

hello. Did you solve your problem?

Perrary gravatar image Perrary  ( 2019-09-23 13:58:33 -0600 )edit