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

It would be really helpful to share the error message, but I guess that it is related to a type issue since the msg->data is an int8[] not a nav_msgs::OccupancyGrid.

Please refer to OccupancyGrid message definition and see this occupancygrid.cpp code The mapCallback function in it would be very useful as example and since you need to loop over cells and sum them up note that you need to use msg->data[x+ info.width * y] to access the cell [x,y] and not data(x,y)

Good luck!

It would be really helpful to share the error message, but I guess that it is related to a type issue since the msg->data is an int8[] not a nav_msgs::OccupancyGrid.

Please refer to OccupancyGrid message definition and see this occupancygrid.cpp code The mapCallback function in it would be very useful as example and since you need to loop over cells and sum them up note that you need to can use msg->data[x+ info.width * y] to access the cell [x,y] and not data(x,y)

Edit1:

When looping over the width then height values,

index=x+info.width*y is a formula that will give you access to all the cells of the map column by column when you loop over x , y. for example for x=0 you will have [0,1,...,info.height], for x=1 , [info.height+1,...,2*info.height] and so on ... finally we will get [0,...,info.width*info.height].

This way to access cells is more useful if we have a special computation where we will make use of the current width and height (x,y) but in your case you can just use a simple loop since you just need to count the unexplored cells,

for(int i=0; i<info.height*info.width; i++)
    {
        if(msg->data[i]==-1)
            Ngray++;
    }

Good luck!