{"id":23268451,"url":"https://github.com/dimitriskatos/robot_loc_nav","last_synced_at":"2025-04-06T08:41:19.573Z","repository":{"id":224900493,"uuid":"738922384","full_name":"DimitrisKatos/robot_loc_nav","owner":"DimitrisKatos","description":"Localization and navigation for a differential drive robot using Ros2, Nav2 and Gazebo Classic.","archived":false,"fork":false,"pushed_at":"2024-01-18T10:00:32.000Z","size":1705,"stargazers_count":4,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-02-12T14:17:38.166Z","etag":null,"topics":["gazebo","localization","mapping","nav2","nav2-plugins","navigation","ros2-humble","rviz2"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/DimitrisKatos.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2024-01-04T11:06:23.000Z","updated_at":"2024-12-31T14:06:02.000Z","dependencies_parsed_at":"2024-02-28T10:37:38.962Z","dependency_job_id":null,"html_url":"https://github.com/DimitrisKatos/robot_loc_nav","commit_stats":null,"previous_names":["dimitriskatos/robot_loc_nav"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DimitrisKatos%2Frobot_loc_nav","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DimitrisKatos%2Frobot_loc_nav/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DimitrisKatos%2Frobot_loc_nav/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/DimitrisKatos%2Frobot_loc_nav/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/DimitrisKatos","download_url":"https://codeload.github.com/DimitrisKatos/robot_loc_nav/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247457727,"owners_count":20941905,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["gazebo","localization","mapping","nav2","nav2-plugins","navigation","ros2-humble","rviz2"],"created_at":"2024-12-19T17:18:39.599Z","updated_at":"2025-04-06T08:41:19.556Z","avatar_url":"https://github.com/DimitrisKatos.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Create an autonomous differential drive robot.\nIn this tutorial we will walk through some interest part of robotics. We will add sensors to our models such as lidar, a depht camera and an imu.\nWe will use the nav2 package to navigate, to avoid obstacles and create paths. Also we will localize our robot and finally the robot will be able to create his own map. Finally the map will be saved and re-used.\n\nYou can find a presentation that will guide you through this tutorial. In the presentation, you can find information on how to build this robot.\n- [Use nav2 package to your robot]() Greek launguage.\n- [Use nav2 package to your robot]() English language(comming soon)\n\nMore information about the Nav2 package can be found [here](https://navigation.ros.org/index.html).\n\nBefore starting creating the robot model and all the necessery components, ensure you have install some packages. Run the next commands.\n```\nsudo apt install ros-humble-robot-localization\nsudo apt install ros-humble-navigation2  \nsudo apt install ros-humble-nav2-bringup\nsudo apt install ros-humble-turtlebot3-gazebo\n```\n\n## Create package and a URDF robot model.\n\nFirst of we will create a new ros package\n```\ncd ~/ros2_ws/src\nros2 pkg create --build-type ament-python robot_loc_nav\n```\nCreate folder for better organization in the package.\n```\ncd ~/ros2_ws/src/\nmkdir launch urdf meshes worlds\n```\nAfter creating the package and the folders, you must informes the setup.py file of the package.\nAdd the following modules to the python script\n```py\nimport os\nfrom glob import glob\n```\n\nNow the data_file list must be informed. So add the following to the list.\n```py\n        (os.path.join('share',package_name,'launch'),\n         glob(os.path.join('launch','*.launch.py'))),\n        (os.path.join('share',package_name,'urdf'),\n         glob(os.path.join('urdf','*.xacro'))),\n         (os.path.join('share',package_name,'urdf'),\n         glob(os.path.join('urdf','*.gazebo'))),\n        (os.path.join('share',package_name,'worlds'),\n         glob(os.path.join('worlds','*.world'))),\n        (os.path.join('share',package_name,'meshes'),\n         glob(os.path.join('meshes','*.dae'))),\n        (os.path.join('share',package_name,'config'),\n         glob(os.path.join('config','*.yaml'))),\n        (os.path.join('share',package_name,'maps'),\n         glob(os.path.join('maps','*.yaml'))),\n        (os.path.join('share',package_name,'maps'),\n         glob(os.path.join('maps','*.pgm'))),\n        (os.path.join('share',package_name,'rviz'),\n         glob(os.path.join('rviz','*.rviz'))),\n```\n\nNow build the package using the colcon tool.\n```\ncd ~/ros2_ws/\ncolcon build --packages-select robot_loc_nav\n```\n\nFor the two wheel robot model we use many xacro features to make easier the contruction of the robot. Create a new file in the urdf file with the name [robot_loc_nav.xacro](https://github.com/DimitrisKatos/robot_loc_nav/blob/master/urdf/robot_loc_nav.xacro). We also create [my_robot.gazebo](https://github.com/DimitrisKatos/robot_loc_nav/blob/master/urdf/my_robot.gazebo) file where we define the color of every link and we enable the differential drive control for Gazebo simulator.\n\nOne difference from the [dd_robot](https://github.com/DimitrisKatos/dd_robot) and [my_robot](https://github.com/DimitrisKatos/my_robot) tutorials is one extra link in robot model. We need to add footprint for the robot because many algorithms in Nav2 package use it for navigating through the workplace of the robot.\n```xml\n\u003c!-- Robot Footprint --\u003e\n    \u003clink name=\"base_footprint\"\u003e\n        \u003cxacro:default_inertia_boxes mass=\"0\" width=\"0\" depth=\"0\" height=\"0\"/\u003e\n    \u003c/link\u003e\n\n    \u003cjoint name=\"base_joint\" type=\"fixed\"\u003e\n        \u003cparent link=\"base_link\"/\u003e\n        \u003cchild link=\"base_footprint\"/\u003e\n        \u003corigin xyz=\"0.0 0.0 ${-(radius+0.05)}\" rpy=\"0 0 0\"/\u003e\n    \u003c/joint\u003e\n\n```\n\nNow you can visualize the robot in Rviz and Gazebo. Create a new lanch file [display.launch.py](https://github.com/DimitrisKatos/robot_loc_nav/blob/master/launch/display.launch.py). Then build the pakage and start the launch file.\n\n## Robot Localization. \n\nLocating the robot at its workplace is very important for an autonomous robotic system. Two sensors are primarily used to locate the robot. These are the odometry sensors and imu sensors. The odometry sensor has been activate from the differential drive control plugin for Gazebo. For imu sensor, we need to add a new link to robot model and finally activate the sensor by adding a new Gazebo plugin. \n\nIn your two wheel robot add the following XML code\n```xml\n\u003c!-- imu link--\u003e\n    \u003clink name=\"imu_link\"\u003e\n        \u003cvisual\u003e\n            \u003cgeometry\u003e\n                \u003cbox size=\"0.1 0.1 0.1\"/\u003e\n            \u003c/geometry\u003e\n        \u003c/visual\u003e\n        \u003ccollision\u003e\n            \u003cgeometry\u003e\n                \u003cbox size=\"0.1 0.1 0.1\"/\u003e\n            \u003c/geometry\u003e\n        \u003c/collision\u003e\n        \u003cxacro:default_inertia_boxes mass=\"0.1\" width=\"0.1\" depth=\"0.1\" height=\"0.1\"/\u003e\n    \u003c/link\u003e\n\n    \u003cjoint name=\"imu_joint\" type=\"fixed\"\u003e\n        \u003cparent link=\"base_link\"/\u003e\n        \u003cchild link=\"imu_link\"/\u003e\n        \u003corigin xyz=\"0 0 0.01\"/\u003e\n    \u003c/joint\u003e\n\n    \u003c!-- Camera Link--\u003e\n    \u003clink name=\"camera_link\"\u003e\n        \u003cvisual\u003e\n            \u003corigin xyz=\"0 0 0\" rpy=\"0 0 0\"/\u003e\n            \u003cgeometry\u003e\n                \u003cbox size=\"0.015 0.130 0.022\"/\u003e\n            \u003c/geometry\u003e\n        \u003c/visual\u003e\n\n        \u003ccollision\u003e\n            \u003corigin xyz=\"0 0 0\" rpy=\"0 0 0\"/\u003e\n            \u003cgeometry\u003e\n                \u003cbox size=\"0.015 0.130 0.022\"/\u003e\n            \u003c/geometry\u003e\n        \u003c/collision\u003e\n\n        \u003cinertial\u003e\n            \u003corigin xyz=\"0 0 0\" rpy=\"0 0 0\"/\u003e\n            \u003cmass value=\"0.035\"/\u003e\n            \u003cinertia ixx=\"0.001\"  ixy=\"0\"  ixz=\"0\" iyy=\"0.001\" iyz=\"0\" izz=\"0.001\" /\u003e\n        \u003c/inertial\u003e\n    \u003c/link\u003e\n```\n\nFor activation of the imu sensor, we create a new file in the urdf file. The name of this file is [imu.gazebo](https://github.com/DimitrisKatos/robot_loc_nav/blob/master/urdf/imu.gazebo) and here we activate the plugin for the robot model. Finally, in the robot_loc_nav.xacro we use the xacro include feature to import the imu.gazebo to the robot model.\nAfter succesfully add the imlu sensor, we move forward to localize the robot model. In the config folder, create the [ekf_filter.yaml](https://github.com/DimitrisKatos/robot_loc_nav/blob/master/config/ekf_filter.yaml) file. In this file we define the parameters which the localization we start.\n\nNow update the [display.launch.py](https://github.com/DimitrisKatos/robot_loc_nav/blob/master/launch/display.launch.py) file by adding the following.\n```py\nsim_time= DeclareLaunchArgument(name='use_sim_time', default_value='True',\n                                   description='Flag to enable use_sim_time')\n\nrobot_localization_node = Node(\n       package='robot_localization',  executable='ekf_node',\n       name='ekf_filter_node', output='screen',\n       parameters=[os.path.join(robot_path, 'config/ekf_filter.yaml'), \n{'use_sim_time': LaunchConfiguration('use_sim_time')}])\n```\n\nIn the LaunchDescription add robot_localization. Build the package and launch the Rviz and gazebo. Run the next command and you will see the location of the robot base on the two sensors.\n![Poll Mockup](./images/image1.png)\n\n## Robots mapping\nMapping is very important for the robots. The map of the robot is vital because of the fact that we the knownledge of the workplace can make many tasks. Mapping can be created by the distance measurements sensors such as depth camers and lidars. In the [my_robot](https://github.com/DimitrisKatos/my_robot) tutorial we have added a lidar, so for more information on how to setup lidar can be found in this tutorial. \n\nNow we move forward on adding a depth camera to the robot. Add new link in robot model for the camera:\n```xml\n \u003c!-- Camera Link--\u003e\n    \u003clink name=\"camera_link\"\u003e\n        \u003cvisual\u003e\n            \u003corigin xyz=\"0 0 0\" rpy=\"0 0 0\"/\u003e\n            \u003cgeometry\u003e\n                \u003cbox size=\"0.015 0.130 0.022\"/\u003e\n            \u003c/geometry\u003e\n        \u003c/visual\u003e\n\n        \u003ccollision\u003e\n            \u003corigin xyz=\"0 0 0\" rpy=\"0 0 0\"/\u003e\n            \u003cgeometry\u003e\n                \u003cbox size=\"0.015 0.130 0.022\"/\u003e\n            \u003c/geometry\u003e\n        \u003c/collision\u003e\n\n        \u003cinertial\u003e\n            \u003corigin xyz=\"0 0 0\" rpy=\"0 0 0\"/\u003e\n            \u003cmass value=\"0.035\"/\u003e\n            \u003cinertia ixx=\"0.001\"  ixy=\"0\"  ixz=\"0\" iyy=\"0.001\" iyz=\"0\" izz=\"0.001\" /\u003e\n        \u003c/inertial\u003e\n    \u003c/link\u003e\n\n    \u003cjoint name=\"camera_joint\" type=\"fixed\"\u003e\n        \u003cparent link=\"base_link\"/\u003e\n        \u003cchild link=\"camera_link\"/\u003e\n        \u003corigin xyz=\"0.215 0 0.05\" rpy=\"0 0 0\"/\u003e\n    \u003c/joint\u003e\n\n    \u003clink name=\"camera_depth_frame\"/\u003e\n    \u003cjoint name=\"camera_depth_joint\" type=\"fixed\"\u003e\n        \u003corigin xyz=\"0 0 0\" rpy=\"${-pi/2} 0 ${-pi/2}\"/\u003e\n        \u003cparent link=\"camera_link\"/\u003e\n        \u003cchild link=\"camera_depth_frame\"/\u003e\n    \u003c/joint\u003e\n```\n\nActivate the depth camera by creating a new gazebo plugin and then import it to the robot model. Create [depth_camera.gazebo](https://github.com/DimitrisKatos/robot_loc_nav/blob/master/urdf/depth_camera.gazebo) and use xacro include method.\nRun the next command to visualize the depth camera.\n```\ncd ~/ros2_ws\ncolcon build --packages-select robot_loc_nav \nexport LIBGL_ALWAYS_SOFTWARE=1 LIBGL_ALWAYS_INDIRECT=0\nros2 launch robot_loc_nav display.launch.py\n```\nWhen Gazebo starts, add an obstacle in front of the robot. In Rviz, add the robot model and PointCloud.\n![Poll Mockup](./images/image2.png)\n\n![Poll Mockup](./images/image3.png)\n\nLet's move forward on creating a map for the robot. Create a new configuration file for slam toolbox. In the config file create the [nav2_params.yaml]() file. Now run the next commands.\n```\ncd ~/ros2_ws\ncolcon build --packages-select robot_loc_nav \nexport LIBGL_ALWAYS_SOFTWARE=1 LIBGL_ALWAYS_INDIRECT=0\nros2 launch robot_loc_nav display.launch.py\n```\nMove to a new terminal and run the following.\n```\nros2 launch slam_toolbox online_async_launch.py params_file:=src/robot_loc_nav/config/nav2_params.yaml use_sim_tim:=true\n```\n\nIn Rviz, add the robot model and Map feature. In simulator put the robot in a building and you can see that the robot is starting creating a map of his workplace.\n![Poll Mockup](./images/image4.png)\n\n![Poll Mockup](./images/image5.png)\n\n![Poll Mockup](./images/image6.png)\n\n## Reusing the map \n\nThe map is very important for the robot. But creating map every time is difficult for the robot's computer. That's the reason why we want to save this map and reuse it any time.\nFor saving the map we will use the SlamToolboxPlugin. Select Panels from window settings and the Add panel. From the new gui select SlamToolboxPlugin.\n\nNow you can notice a new plugin in Displays Panel. In Serial Map give the name my_map and saved it. After saving the map, create the map folder in your package. Move all the map files to this folder. Now you must update the setup.py file by adding some paths to data_files.\n```py\n(os.path.join('share',package_name,'maps'),\n         glob(os.path.join('maps','*.yaml'))),\n(os.path.join('share',package_name,'maps'),\n         glob(os.path.join('maps','*.pgm'))),\n```\n\nAlso run the next commands\n```\ncd ~/ros2_ws/src/robot_loc_nav\ncp /opt/ros/humble/share/nav2_bringup/laumch/navigation_launch.py launch/\ncp /opt/ros/humble/share/nav2_bringup/laumch/localization_launch.py launch/\ncp /opt/ros/humble/share/nav2_bringup/params/nav2_params.yaml config/\n```\n\nNow run the next command to reuse the map.\n```\ncd ~/ros2_w\ncolcon build --packages-select robot_loc_nav\nexport LIBGL_ALWAYS_SOFTWARE=1 LIBGL_ALWAYS_INDIRECT=0\nros2 launch robot_loc_nav display.launch.py \n        # Open new terminal\ncd src/robot_loc_nav\nros2 launch robot_loc_nav localization.launch.py  map:= /maps/my_map.yaml  \n```\nIn rviz add the map feature and you will see the following.\n\n![Poll Mockup](./images/image7.png)\n\nWhen we launching the map in Rviz, the robot have no idea where he is this map. Select the **2D Pose Estimat** and give him the starting position. Now you will see the following.\n\n![Poll Mockup](./images/image8.png)\n\nAfter giving the starting position the robot model we be appeared and know he will know where he is in the map.\n\n![Poll Mockup](./images/image9.png)\n\n## Autonomous driving\nAfter launching the map and define the starting position of the robot you can give a desired position to the robot.\nRun the following command to a new terminal. \n```\nros2 launch robot_loc_nav navigation.launch.py use_sim_time:=true\n```\n\nSelesct the **2D Goal Pose** and give a desired position.\n\n![Poll Mockup](./images/image10.png)\n\nAfter that the robot will create a path to this position and start moving. On his road to achieve his goal the robot is capable of avoiding all the obstacles even those he doesn't know that exist.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdimitriskatos%2Frobot_loc_nav","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdimitriskatos%2Frobot_loc_nav","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdimitriskatos%2Frobot_loc_nav/lists"}