Ask Your Question

when I make path planning, how do I konw a map cell is obstacle?

asked 2019-12-20 20:56:49 -0500

hubery524 gravatar image

updated 2020-02-06 20:37:43 -0500

jayess gravatar image

image description when implement the A* algorithm, I will get neighbors, but how can I konw whether the neighbor is an obstacle or free?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2020-02-06 19:18:55 -0500

atas gravatar image

updated 2020-02-09 20:08:18 -0500

I think you want to make a path planning using OccupancyGrid, the data types defined in this message type are ;

# This represents a 2-D grid map, in which each cell represents the probability of
# occupancy.

Header header 

#MetaData for the map
MapMetaData info

# The map data, in row-major order, starting with (0,0).  Occupancy
# probabilities are in the range [0,100].  Unknown is -1.
int8[] data

data array indicates whether a cell is occupied or not,. Note that this is a 1D array which you need to think/convert it to 2D.

You can get x,y position of each cell using meta data of map inside info

for example;

void YourClass::QueryMapCellInfo(const nav_msgs::OccupancyGridConstPtr &grid){
  for(int i=0; i<grid->info.height; i++){
    for(int j =0; j<grid->info.width; j++){
         int current_cell_value = grid->data[i+ (grid->info.width * j)];
         // do some processing according to cell value you got
edit flag offensive delete link more


There are several things to say about your loop : You forgot the iteration expression so you are doing the same calcul for ever since i and j won't change ( it should be for(int i=0; i<grid->info.height; i++)). Your loop is not covering all the cells, for example if height = 4 and width = 2 then size is 8 so the last index should be 7 (since it starts at index 0), but with your loop for the max i and j (which are respectively height - 1 and width - 1) you have :

max_i + width * max_j = (4-1) + 2 * (2 - 1) = 5

To avoid that why don't you just use one loop from 0 to the size of the array ?

for (uint index = 0; index < grid->data.size(); index++)
    int cell_value = grid->data[index];
    //do something
Delb gravatar image Delb  ( 2020-02-07 03:33:30 -0500 )edit

So,what data value represent free cell, what value represent the obstacle cell

hubery524 gravatar image hubery524  ( 2020-02-07 05:08:00 -0500 )edit

@atas I'm sorry to be picky with you but as you suggested an answer it should contains correct information not to mislead other people seeking for help, that being said I still encourage you to try helping people on this site and I thank you for your involvement in the ros community.

You still have an issue with your loop, now when i and j are at 0 you will have negative values with i+ (grid->info.width * j-1) thus you will end up having a segmentation fault (because you can't have negative indexes for an array).

I see you want to get this working with 2 for loops but you would use this to calculate an index from coordinates , here OP just want to access the cell data so you should consider my suggestion.

Delb gravatar image Delb  ( 2020-02-07 05:26:01 -0500 )edit

@hubery524 There are 3 differents values :

  1. -1 if the cell is unknown
  2. 0 if the cell is free
  3. 100 if there is an obstacle
Delb gravatar image Delb  ( 2020-02-07 05:27:01 -0500 )edit

@Delb, thanks for you sugesstions, I will edit my answer soon, the reason i want to particularly go with 2 for loops is to make OP clear about the 2D coordinates being represented in 1D array. As he asks about “neighbors” of the cells eventually he will need to use 2 for loops to get (x,y) coordinates of each individual cell and from there the cost value of that cell

atas gravatar image atas  ( 2020-02-07 05:46:49 -0500 )edit

That would be a correct assumption indeed. Your implementation though would be required if you want to find the index if you are considering rows and columns, if you want an index from coordinates that would be :

index = floor((x - x_origin)/resolution) + floor((y - y_origin)/resolution)*map_width

Which, if you want to check all the grid, can be translated with rows and columns to :

for(int row=0; row<grid->info.width; row++)
    for(int column =0; column<grid->info.height; column++)
        int current_cell_value = grid->data[row+ (grid->info.width * column)];

It is similar to your first suggestion, you just needed to invert your i and j. In that case finding neighbors would be easy and simply done by added/substracting 1 to row and/or column.

Delb gravatar image Delb  ( 2020-02-07 06:53:34 -0500 )edit

Thanks, what's more, Can you introduce some path planning book for the beginner(like me)? I want to form a solid foundation for robot path planning.

hubery524 gravatar image hubery524  ( 2020-02-20 01:26:44 -0500 )edit

Please do not answer a question to ask another one, ROS answers is not a forum, please use ROS discourse instead.

Delb gravatar image Delb  ( 2020-02-20 01:57:32 -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


Asked: 2019-12-20 20:56:49 -0500

Seen: 283 times

Last updated: Feb 20 '20