Where am I in the map?

How do I get the occupancy value from the map at my robots position? The map is increasing dynamically as my robot moves around, right? Hence the (0,0) index of the data array may shift as my map expands. What I want is the occupancy value at the very position of my robot. How can I achieve this?

edit retag close merge delete

Sort by ยป oldest newest most voted

To know where the robot is in the map, you want to transform the origin of the base_link frame into the map frame.

In addition to the map, SLAM systems (gmapping and karto) publish transform data. In particular, they provide the transform from the map frame to the odom frame. Another node (which one will depend on what kind of robot you're using) should already be publishing the transform from the odom frame to the base_link frame.

The tf library will chain those transforms together for you. The details will depend on whether you're writing in C++ or Python; check the tf documentation. As an example, from the command-line, you can do this while the SLAM system is running:

rosrun tf tf_echo map base_link


Note: frames names are configurable, and your system may use different names from the ones I gave above. But the semantics of them is the same.

Given the robot's pose in the map frame, if you want the corresponding index into the occupancy grid map, you'd do something like this:

grid_x = (unsigned int)((map_x - map.info.origin.position.x) / map.info.resolution)
grid_y = (unsigned int)((map_y - map.info.origin.position.y) / map.info.resolution)


In principle, you should also account for the orientation of the origin (map.info.origin.orientation), but in practice there's no rotation.

more

Allright. So all I have to do is transform the base_link into the map frame. Further I just take the pose, devide the coordinate components by the map resolution and there I have the correct indices for the map data array? what if they are negative? shouldn't I somehow account for the map origin?
( 2011-06-17 03:18:07 -0500 )edit
I updated the answer with some code for that pose-to-grid conversion.
( 2011-06-20 04:41:02 -0500 )edit

Are these coordinates grid_x and grid_y in the /map frame. I have an object with its location static w.r.t the /map frame. So as the map changes dynamically (so does the location of object but static w.r.t map), will these coordinates also remain static?

( 2015-06-08 19:05:41 -0500 )edit

Where do map_x and map_y come from? From map or from base_link? How can I get map_x or map_y what should I "calculate"?

( 2019-01-17 09:10:46 -0500 )edit