The code for this section is located in navigation_bot_04 section of the code archive. This bot allows us to do pretty much the same as navigation_bot_03 did (drive around and save the map), however, the entire system of launch files is now altered to make launch more flexible. We are going to start the changes here, and finish them in the navigation_bot_05 section (next one). Overall, the launch structure will now mimic what official ROS2 documentation has for sam_bot, except some problems are going to be fixed.
The map we saved in the previous section is of a costmap 2D type. The costmap 2D package uses the sensor information to create a representation of the robot's environment in the form of an occupancy grid. The cells in the occupancy grid store cost values between 0-254, representing a cost to travelling through these zones. A cost of 0 means the cell is free while a cost of 254 means that the cell is lethally occupied. Values in between these extremes are used by navigation algorithms to steer your robot away from obstacles as a potential field. Costmaps in Nav2 are implemented through the nav2_costmap_2d package.
The costmap implementation consists of multiple layers, each of which has a certain function that contributes to a cell's overall cost. The package consists of the following layers, but is plugin-based to allow customization and new layers to be used as well: static layer, inflation layer, range layer, obstacle layer, and voxel layer. The static layer represents the map section of the costmap, obtained from the messages published to the /map topic like those produced by SLAM. The obstacle layer includes the objects detected by sensors that publish either or both the LaserScan and PointCloud2 messages. The voxel layer is similar to the obstacle layer such that it can use either or both the LaserScan and PointCloud2 sensor information but handles 3D data instead. The range layer allows for the inclusion of information provided by sonar and infrared sensors. Lastly, the inflation layer represents the added cost values around lethal obstacles such that our robot avoids navigating into obstacles due to the robot's geometry.
The layers are integrated into the costmap through a plugin interface and then inflated using a user-specified inflation radius, if the inflation layer is enabled. For a deeper discussion on costmap concepts, you can have a look at the ROS1 costmap_2D documentation. Note that the nav2_costmap_2d package is mostly a straightforward ROS2 port of the ROS1 navigation stack version with minor changes required for ROS2 support and some new layer plugins.