Best way to extract occupancy values from OccupancyGrid

asked 2020-10-24 05:50:54 -0600

wiyogo gravatar image

Hi,

I write a new package that extract the value of the OccupancyGrid map by subscribing it and lidar scan. In the callback function of the map, shall I copy the pointer of the map or do copy-by-value? As we know the callback is called periodically based on the sensor rate, for instance 10 Hz or each 100ms. Which mean, if after 100ms, the processing in the callback function is not finished yet, will the pointer be overwritten or the callback has different pointer?

This is the example code:

# example.h
public: 
  nav_msgs::OccupancyGrid::Ptr occgrid_;

# ----------------------
# example.cpp with pointer
void Example::map_callback(const nav_msgs::OccupancyGridPtr &occ_grid)
{
    if (occ_grid->data.size() > 0)
    {
        occgrid_ = occ_grid;
    }
}

# ----------------------
# example.cpp with copy-by-value
void Example::copy_to_2dgridmap(const nav_msgs::OccupancyGrid &occ_grid)
{
    for (unsigned int w = 0; w < occ_grid.info.width; ++w)
    {
        for (unsigned int h = 0; h < occ_grid.info.height; ++h)
        {
            Coordinate c(w, h);
            gridmap_[c] = occ_grid.data[w + h * occ_grid.info.width];
        }
    }
}

void Example::map_callback(const nav_msgs::OccupancyGrid &occ_grid)
{
    if (occ_grid->data.size() > 0)
    {
        copy_to_2dgridmap(occ_grid);

    }
}

void Example::scan_callback(const sensor_msgs::LaserScan::ConstPtr& t_lidarscan)
{
    # Extract the map values and other processing. 
    # During this processing the map pointer can be updated.
}
edit retag flag offensive close merge delete