0
$\begingroup$

Rosanswers logo

How do I publish this test data to the /map topic. I would like to use this routine to test other things I'm working on. I think I'm doing it right but when I type 'rostopic echo /map' I see nothing. If I run the script at the command prompt after typing 'roscore' in another terminal I see the test OccupancyGrid on the screen. I don't know what I'm doing wrong.

#!/usr/bin/env python

import rospy
import sys


from nav_msgs.msg import *
from std_msgs.msg import *

def map_stuff():
    rospy.init_node('turtlebot_map', anonymous=True)
    if not rospy.is_shutdown():
        create_map()
        rospy.spin()

def create_map( ):
    test_map = OccupancyGrid()
    test_map.info.resolution = 1.0 
    test_map.info.width = 10
    test_map.info.height = 10
    test_map.info.origin.position.x = 0.0 
    test_map.info.origin.position.y = 1.0 
    test_map.info.origin.position.z = 2.0 
    test_map.info.origin.orientation.x = 3.0 
    test_map.info.origin.orientation.y = 4.0 
    test_map.info.origin.orientation.z = 5.0 
    test_map.info.origin.orientation.w = 6.0 
    test_map.data = []
    for i in range(0, 100):
        test_map.data.append(i)
    print test_map
    map_pub = rospy.Publisher('/map', OccupancyGrid)
    map_pub.publish(test_map);
    return 

if __name__ == '__main__':
    try:
        map_stuff()
    except rospy.ROSInterruptException:
        pass
    

I followed online tutorials as closely as I could. Any help would be appreciated.


Originally posted by david.c.liebman on ROS Answers with karma: 125 on 2014-02-01

Post score: 0


Original comments

Comment by Higor on 2020-04-24:
You can help me build my map, it is not working?

$\endgroup$

1 Answer 1

0
$\begingroup$

Rosanswers logo

You are publishing just once in create_map(). Try these changes:

def map_stuff():
     global map_pub
     map_pub = rospy.Publisher('/map', OccupancyGrid) # remove this line from create_map()
     rospy.init_node('turtlebot_map', anonymous=True)
     while not rospy.is_shutdown():
        create_map()
        rospy.spin()

Originally posted by gustavo.velascoh with karma: 756 on 2014-02-01

This answer was ACCEPTED on the original site

Post score: 0


Original comments

Comment by david.c.liebman on 2014-02-02:
this will work if you make map_pub global somehow. You're right, the thing is I'm doing it just once in my code. If I publish it repeatedly (not sure if that's a good idea) then you can see it on 'rostopic echo /map'...

Comment by gustavo.velascoh on 2014-02-02:
Sorry, I forgot global label. Why do you think that publish repeatedly could be a bad idea? Maybe you want a service instead a topic? or have you tried map_server?

Comment by david.c.liebman on 2014-02-02:
I added a line that reads like

map_pub = rospy.Publisher('/map', OccupancyGrid, latch=True)
and it seems to work better. Hopefully this will help me. Thanks for your help!!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.