View Categories

TurtleBot3(ROS1)-Noetic-Chap6-Simulation-模擬

C6. Simulation 模擬 #

補充資料:了解更多有關 TurtleBot3 Simulation。 #

TurtleBot3 支持模擬開發環境,可以在模擬中使用虛擬機器人進行編程開發。有兩種開發環境可以做到這一點,一種是使用帶有 3D 可視化工具 RViz 的假節點 fake node;另一種是使用 3D 機器人模擬器 Gazebo

  • 假節點 fake node 模擬,適合用來測試機器人模型和移動,但它不支援感測器。
  • 如果需要執行 SLAM 或 Navigation,Gazebo 模擬器會是一個可行的解決方案,因為它支持 IMU、LDS 和攝影機等感測器。

在本教程中,將主要介紹在 ROS 開發人員中使用最廣泛的 Gazebo 模擬器。

C6.1. Gazebo Simulation #

C6.1.1. 安裝 Simulation Package #

TurtleBot3 Simulation Package 需要 turtlebot3 及 turtlebot3_msgs packages 為先決條件。沒有安裝這兩個 packages,則無法啟動 Simulation。

如果還沒有安裝所需 packages,可參照 Chap3.1. PC 設定章節來安裝。

[Remote PC]

				
					$ cd ~/catkin_ws/src/
$ git clone -b noetic https://github.com/ROBOTIS-GIT/turtlebot3_simulations.git
$ cd ~/catkin_ws && catkin_make
				
			

C6.1.2. 啟動 Simulation World 世界地圖 #

TB3 有三種模擬的世界地圖可選擇。啟動 Gazebo 時可以任選其一。

1. Empty World 空白世界地圖

turtlebot3 empty world

[Remote PC]

				
					$ export TURTLEBOT3_MODEL=burger
$ roslaunch turtlebot3_gazebo turtlebot3_empty_world.launch
				
			

2. TurtleBot3 World TB3 世界地圖

turtlebot3 world bugger

[Remote PC]

				
					$ export TURTLEBOT3_MODEL=waffle
$ roslaunch turtlebot3_gazebo turtlebot3_world.launch
				
			

3. TurtleBot3 House 房屋地圖

turtlebot3 house

[Remote PC]

				
					$ export TURTLEBOT3_MODEL=waffle_pi
$ roslaunch turtlebot3_gazebo turtlebot3_house.launch
				
			

C6.1.3. 操作 TB3 #

為了使用鍵盤來控制 TB3,需在新的 terminal 啟動 teleoperation node。

[Remote PC]

				
					$ roslaunch turtlebot3_teleop turtlebot3_teleop_key.launch
				
			

補充資料:了解更多有關 如何執行自動避障(Autonomous Collision Avoidance)。 #

在 TB3 simulation packages 中,已包含一個簡單的 collision avoidance node,來讓機器人與障礙物保持安全距離,並轉向避免碰撞。

要在 TurtleBot3 World 世界地圖模擬內自動駕駛 TB3,請依照以下說明操作

1. Ctrl + C 結束 turtlebot3_teleop_key node,關閉 teleop node
2. 在 terminal 輸入指令

[Remote PC]

				
					$ roslaunch turtlebot3_gazebo turtlebot3_simulation.launch
				
			

補充資料:了解更多有關 如何可視化 Simulation data(RViz) #

當 simulation 進行時,在 RViz 中能夠可視化已公布的 topics。你可在新的 terminal 啟動 RViz。

[Remote PC]

				
					$ roslaunch turtlebot3_gazebo turtlebot3_gazebo_rviz.launch
				
			
turtlebot3 gazebo rviz

C6.2. SLAM 模擬 #

Gazebo 模擬器具有 SLAM 功能,你可在虛擬世界地圖中,選擇或建立不同的環境及機器人模型,而無須使用實體的機器人來實作。除了要準備模擬環境,而不需啟動實體機器人外,SLAM 模擬,跟使用實體 TB3 來實作 SLAM 是非常相似的。

下列操作會需要之前章節為先決條件,請回看之前的 Simulation 章節內容。

C6.2.1. 開啟 Simulation World 世界地圖 #

Gazebo 有三個世界地圖可開啟,但因要用 SLAM 來建圖,建議使用 TurtleBot3 World 或 TurtleBot3 House 地圖。

使用下列指令來載入 Gazebo 模擬器。本章會使用 TurtleBot3 World 世界地圖。也請注意 TURTLEBOT3_MODEL 參數,要指定所對應的 TB3 模型 (burger, waffle, waffle_pi)

[Remote PC]

				
					$ export TURTLEBOT3_MODEL=burger
$ roslaunch turtlebot3_gazebo turtlebot3_world.launch
				
			

補充資料:了解更多有關 如何載入 TurtleBot3 House 房屋地圖 #

[Remote PC]

				
					$ export TURTLEBOT3_MODEL=burger
$ roslaunch turtlebot3_gazebo turtlebot3_house.launch
				
			

C6.2.2. 執行 SLAM Node #

在 Remote PC 開新 terminal(Ctrl + Alt + T) 並跑 SLAM node。預設是使用 Gmapping SLAM 方法。也請注意 TURTLEBOT3_MODEL 參數,要指定所對應的 TB3 模型 (burger, waffle, waffle_pi)

[Remote PC]

				
					$ export TURTLEBOT3_MODEL=burger
$ roslaunch turtlebot3_slam turtlebot3_slam.launch slam_methods:=gmapping
				
			

C6.2.3. 執行 Teleoperation Node #

在 Remote PC 開新 terminal(Ctrl + Alt + T) 並跑 teleoperation node。也請注意 TURTLEBOT3_MODEL 參數,要指定所對應的 TB3 模型 (burger, waffle, waffle_pi)

[Remote PC]

				
					$ export TURTLEBOT3_MODEL=burger
$ roslaunch turtlebot3_teleop turtlebot3_teleop_key.launch

 Control Your TurtleBot3!
 ---------------------------
 Moving around:
        w
   a    s    d
        x

 w/x : increase/decrease linear velocity
 a/d : increase/decrease angular velocity
 space key, s : force stop

 CTRL-C to quit
				
			

C6.2.4. 儲存地圖 #

當地圖成功產生後,在 Remote PC 開新 terminal(Ctrl + Alt + T) 並儲存地圖。

virtual slam

[Remote PC]

				
					$ rosrun map_server map_saver -f ~/map
				
			
map
所儲存的 map.pgm 檔

C6.3. Navigation Simulation 模擬導航 #

就如在 Gazebo 模擬器跑虛擬 SLAM,你也可以進行虛擬導航 virtual Navigation,而無須使用實體的機器人來實作。當然在跑 virtual Navigation 前,必須準備好完整合適的地圖環境。虛擬導航跟實作真實導航會很相似。

C6.3.1. 開啟模擬的世界地圖 #

在之前 SLAM 章節,使用 TurtleBot3 World 世界地圖來建地圖。相同的 Gazebo 環境也被使用在模擬 Navigation。

也請注意 TURTLEBOT3_MODEL 參數,要指定所對應的 TB3 模型 (burger, waffle, waffle_pi)

[Remote PC]

				
					$ export TURTLEBOT3_MODEL=burger
$ roslaunch turtlebot3_gazebo turtlebot3_world.launch
				
			

補充資料:了解更多有關 如何載入 TurtleBot3 House 房屋地圖 #

[Remote PC]

				
					$ export TURTLEBOT3_MODEL=burger
$ roslaunch turtlebot3_gazebo turtlebot3_house.launch
				
			

C6.3.2. 執行 Navigation Node #

在 Remote PC 開新 terminal(Ctrl + Alt + T) 並跑 Navigation node。

[Remote PC]

				
					$ export TURTLEBOT3_MODEL=burger
$ roslaunch turtlebot3_navigation turtlebot3_navigation.launch map_file:=$HOME/map.yaml
				
			

C6.3.3. 估計 Initial Pose 初始方位 #

Initial Pose Estimation 必須在跑 Navigation 之前就執行,因為這個程序會初始化 AMCL 參數,這參數對成功的 Navigation 很重要。TurtleBot3 必須正確定位在地圖上,LDS 感測器數據,要與顯示的地圖正確地重疊。

1. 點擊 RViz 選單的 2D Pose Estimate 鍵

2d pose button

2. 在地圖上點擊真正機器人的所在位置,然後拉出大的綠色箭號,並指向機器人正面的方向。

3. 重複步驟 1 跟 2,直到 LDS 感測器的數據,跟儲存的地圖正確的重疊。

tb3 navigation2 rviz 01

4. 啟動 keyboard teleoperation node 遙控節點,以便準確定位在地圖上的機器人位置

[Remote PC]

				
					$ roslaunch turtlebot3_teleop turtlebot3_teleop_key.launch
				
			

5. 來回一點一點移動機器人,以收集周圍環境資訊,和縮小 TurtleBot3 在地圖上的估計位置(地圖上是以小的綠色箭頭來顯示 TB3)。

tb3 amcl particle 01
tb3 amcl particle 02

6. 按 Ctrl + C 來結束 keyboard teleoperation node,這可以防止在導航期間,從多個節點發布不同的 cmd_vel 值。

C6.3.4. Set Navigation Goal 設定導航目標 #

1. 在 RViz 選單點擊 2D Nav Goal 鍵

2d nav goal button

2. 在地圖上點擊來設定機器人的目的地,並拉出綠色箭號,作為機器人抵達目的地後要面對的方向

  • 綠色箭號是一個 marker 標記,是指定機器人的目的地
  • 箭頭的根部是機器人目的地的 x 和 y 座標位置,箭頭指向的方位,則決定機器人的 theta (θ) 角度。
  • 當 x, y, θ 都決定後,TB3 就會開始往目的地移動。
2d nav goal

C6.4. Fake Node Simulation 假節點的模擬 #

要使用 turtlebot3_fake_node,你需要安裝 turtlebot3_simulation 套件。按照以下指示安裝套件。

[Remote PC]

				
					$ cd ~/catkin_ws/src/
$ git clone -b noetic https://github.com/ROBOTIS-GIT/turtlebot3_simulations.git
$ cd ~/catkin_ws && catkin_make
				
			

要啟動虛擬機器人,請執行 turtlebot3_fake 套件中的 turtlebot3_fake.launch 檔,如下所示。 turtlebot3_fake 是一個非常簡單的模擬節點,可以在沒有實體機器人的情況下,來模擬機器人運行。你甚至可以在 RViz 工具中,用 teleoperation node 遙控節點,來遙控虛擬的 TurtleBot3 機器人。

[Remote PC]

				
					$ export TURTLEBOT3_MODEL=burger
$ roslaunch turtlebot3_fake turtlebot3_fake.launch
				
			

[Remote PC]

				
					$ export TURTLEBOT3_MODEL=burger
$ roslaunch turtlebot3_teleop turtlebot3_teleop_key.launch
				
			

C6.5. 單獨的 Gazebo 模擬 #