- C8.1. Getting Started
- C8.2. 攝影機校準 Calibration
- C8.3. Lane Detection 賽道偵測
- C8.4. 交通號誌 Traffic Sign 偵測
- C8.5. AutoRace 關卡任務說明
- 補充 Youtube 影片

C8.1. Getting Started #
March, 2025 校正
請參考 TurtleBot3 線上手冊(英)
C8.1.1. 先決條件 #
Remote PC 筆電/桌電
- 安裝 ROS 2 Humble
- 本節內容主要是基於 Gazebo 的模擬,故只需要電腦即可完成,但也可以自行移植到實體 TB3 上實作看看。
C8.1.2. 安裝 Autorace Package 套件包 #
1. [Remote PC] 安裝 AutoRace meta packages
$ cd ~/turtlebot3_ws/src/
$ git clone https://github.com/ROBOTIS-GIT/turtlebot3_autorace.git
$ cd ~/turtlebot3_ws && colcon build --symlink-install
2. [Remote PC] 安裝額外的相關套件
$ sudo apt install ros-humble-image-transport ros-humble-cv-bridge ros-humble-vision-opencv python3-opencv libopencv-dev ros-humble-image-pipeline
C8.1.3. 設定 World 插件 #
1. [Remote PC] 在你的 .bashrc 檔中新增一行匯出內容,將你的工作 workspace 名稱放入 {your_ws} 中。該插件可在你的世界中,製作環境的動態動作。
$ echo 'export GAZEBO_PLUGIN_PATH=$HOME/{your_ws}/build/turtlebot3_gazebo:$GAZEBO_PLUGIN_PATH' >> ~/.bashrc
C8.2. 攝影機校準 Calibration #
攝影機校準對於自動駕駛至關重要,因為它可以確保攝影機,可以提供機器人周遭環境的準確數據。雖然 Gazebo 模擬簡化了一些校準步驟,但了解校準過程,對於過渡到真實世界的機器人非常重要。相機校準通常包括兩個步驟:內部校準(處理內部相機屬性參數)和外部校準(將相機的視角與機器人的座標系統保持一致)。在 Gazebo 模擬中,這些步驟不是必需的,因為模擬使用了預先定義的相機參數,但這些說明,將幫助你了解實際的硬體部署的整體流程。
C8.2.1. 攝影機影像校準 #
在 Gazebo 模擬中,由於被模擬的相機並不會鏡頭失真,因此不需要進行相機成像校準。
[Remote PC] 首先,執行以下命令啟動 Gazebo 模擬:
$ ros2 launch turtlebot3_gazebo turtlebot3_autorace_2020.launch.py
C8.2.2. 攝影機內部校準 #
內部校準著重於校正鏡頭失真並確定相機的內部屬性,例如焦距和光學中心。在實體的機器人中,這個過程是必不可少的,但在 Gazebo 模擬中,不需要內部校準,因為被模擬的相機,已經不會失真,並可提供理想的影像。但是,包含此內部校準步驟,是為了幫助使用者了解實際硬體部署的過程。
執行內部校準程序,如在真實硬體上也相同,請啟動:
$ ros2 launch turtlebot3_autorace_camera intrinsic_camera_calibration.launch.py
此步驟不會修改影像輸出,但會確保正確的 topic(/camera/image_rect 或 /camera/image_rect_color/compressed)可用於後續的處理。
C8.2.3. camera 外部參數校準 #
外部校準將攝影機的視角與機器人的座標系統保持一致,確保在攝影機視野中偵測到的物體,與其在機器人環境中的實際位置相對應。在真實的機器人中,這個過程至關重要,但在 Gazebo 模擬中,校準是為了保持一致性,並讓使用者熟悉現實世界的工作流程。
一旦開始模擬,啟動外部校準過程:
$ ros2 launch turtlebot3_autorace_camera extrinsic_camera_calibration.launch.py calibration_mode:=True
以上將啟動節點,來負責 camera-to-ground 投影 and compensation補償。
可視化與參數調整 #
1. 在 Remote PC 開新的 terminal 執行 rqt
$ rqt
2. 點擊 Plugins > Visualization > Image view,新增二個攝影機視角圖的視窗
3. 在兩個視窗分別選擇 /camera/image_extrinsic_calib 及 /camera/image_projected topic
- 第一個視窗,將顯示紅色梯形影像,第二個將顯示地面投射視圖(鳥瞰視圖)。

4. 點擊 Plugins > Configuration > Dynamic Reconfigure.
5. 調整在 /camera/image_projection 和 /camera/image_compensation 的參數,來微調攝影機校準
- 更改 /camera/image_projection 的參數值,會影響 /camera/image_extrinsic_calib topic
- 內部攝影機校準,將會修改被紅色梯形所包圍內的影像
- 調整 /camera/image_compensation 來微調 /camera/image_projected 的鳥瞰視角

$ cd ~/turtlebot3_ws/src/turtlebot3_autorace/turtlebot3_autorace_camera/calibration/extrinsic_calibration/
2. 文字編輯器打開 YAML 檔(例如 projection.yaml)
$ gedit projection.yaml
3. 在檔案內修改投影參數,以符合從上面動態重新配置的過程中,所獲得的參數值。
此方法可確保正確的保存外部校準參數,以供將來的運行。


C8.2.4. 檢查校準的結果 #
在完成所有校準後,請在 Remote PC 逐步的執行以下指令,來檢查校準結果。
1. 停止外部校準流程
如果 calibration_mode:=True 有啟動外部校準流程,則請關閉 terminal or Ctrl + C 關閉 terminal。
2. 啟動外部校準節點,但不要 calibration_mode 校準選項
這可確保系統套用已儲存的校準參數,來進行驗證。
$ ros2 launch turtlebot3_autorace_camera extrinsic_camera_calibration.launch.py
3. 執行 rqt 並點擊 Plugins > Visualization > Image view
$ rqt
4. 如校準設定成功,則選擇 /camera/image_projected topic 時,鳥瞰視野會出現如下圖。

C8.3. Lane Detection 賽道偵測 #
Lane Detection 賽道偵測使 TurtleBot3 能夠識別車道標記,並自主跟隨。系統可處理來自真實 TurtleBot3 或 Gazebo 所模擬的攝影機影像、套用顏色過濾,並識別賽道邊界。
本節介紹如何啟動賽道偵測系統、可視化偵測到的賽道標記,以及校準參數以確保能準確追蹤。
在模擬中啟動賽道偵測 #
首先,使用預先定義的賽道追蹤路線啟動 Gazebo 模擬:
$ ros2 launch turtlebot3_gazebo turtlebot3_autorace_2020.launch.py
接下來,執行攝影機校準過程,確保偵測到的車道,準確地映射到機器人的視角:
$ ros2 launch turtlebot3_autorace_camera intrinsic_camera_calibration.launch.py
$ ros2 launch turtlebot3_autorace_camera extrinsic_camera_calibration.launch.py
以上步驟會啟動內部和外部校準程序,以糾正由攝影機回送的任何失真資料。
最後,在校準模式下啟動賽道偵測節點,開始偵測車道:
$ ros2 launch turtlebot3_autorace_camera detect_lane.launch.py calibration_mode:=True
可視化車道偵測輸出 #
若要檢查偵測到的車道,請在 Remote PC 上開啟 rqt:
$ rqt
然後選擇 Plugins > Visualization > Image view,啟動三個 image viewer,來同時顯示不同車道偵測結果。
- /detect/image_lane/compressed

- /detect/image_yellow_lane_marker/compressed: 黃色範圍顏色的過濾影像圖

- /detect/image_white_lane_marker/compressed: 白色範圍顏色的過濾影像圖

這些視覺化圖,有助於確認車道偵測演算法能夠正確識別車道邊界。
校正賽道偵測參數 #
為了獲得最佳準確度,需要調整檢測的參數。調整這些參數,可確保機器人在不同的光照和環境條件下,能夠正確識別車道。
1. 開啟位於 turtlebot3_autorace_detect/param/lane/ 的 lane.yaml 檔案,並將修改後的參數值寫入此檔案。這將確保攝影機在未來啟動時,就使用修改後的參數值。
$ cd ~/turtlebot3_ws/src/turtlebot3_autorace/turtlebot3_autorace_detect/param/lane
$ gedit lane.yaml

賽道追蹤 #
校準完成後,重新啟動不含校準選項的賽道偵測節點:
$ ros2 launch turtlebot3_autorace_detect detect_lane.launch.py
然後,啟動 lane following control 車道跟隨控制節點,使 TurtleBot3 能自動跟隨偵測到的車道:
$ ros2 launch turtlebot3_autorace_driving control_lane.launch.py
C8.4. 交通號誌 Traffic Sign 偵測 #
Traffic sign 交通標誌偵測使 TurtleBot3 在自動駕駛時,能夠識別交通標誌並做出回應。
此功能特色,是使用 SIFT(Scale-Invariant Feature Transform)演算法,此演算法檢測影像中的關鍵特徵點,並將其與儲存的參考影像進行比較,及做識別。邊緣更清晰的標誌,往往會產生更好的識別結果。
本節介紹如何擷取和儲存交通標誌影像、配置偵測參數,以及在 Gazebo 模擬中執行偵測的流程。
- SIFT 文件(https://docs.opencv.org/master/da/df5/tutorial_py_sift_intro.html)
在模擬中啟動交通標誌偵測 #
啟動 Autorace Gazebo 模擬來設定環境:
$ ros2 launch turtlebot3_autorace_camera intrinsic_camera_calibration.launch.py
然後,使用鍵盤手動控制 TurtleBot3,使 TB3 駛向交通標誌:
$ ros2 run turtlebot3_teleop teleop_keyboard
將機器人放置在,能讓機器人攝影機清楚看見交通標誌的位置。
擷取並儲存交通標誌影像 #
為了確保能準確識別,系統需要預先擷取的交通標誌影像,作為參考資料。雖然儲存庫有提供了預設圖像,但識別準確性可能因條件而異。如果 SIFT 演算法在提供的影像上表現不佳,則擷取並使用你自己的交通標誌影像,可以改善辨識結果。
1. 開啟 rqt,然後至 Plugins > Visualization > Image View。
2. 建立新的 image view 視窗,並選擇 topic:/camera/image_compensated,以顯示攝影機回送影像。
3. 調整 TurtleBot3 的位置,使得交通標誌,在攝影機視野中清晰可見。
4. 擷取每個交通標誌的圖像,並裁剪掉任何不必要的背景,只專注於標誌本身。
5. 為了獲得最佳性能,請盡可能使用賽道原有的交通標誌。
將影像儲存在 turtlebot3_autorace_detect 套件 /turtlebot3_autorace/turtlebot3_autorace_detect/image/ 中。
請確保交通號誌檔案名稱,與原始程式碼中使用的名稱相匹配,系統引用了這些預設名稱:
- 預設使用 construction.png、intersection.png、left.png、right.png、parking.png、stop.png 和 tunnel.png 檔案名稱。
如果辨識效能與預設影像不一致,則手動的擷取交通標誌影像,可能會提高準確性並提高整體偵測的可靠性。
執行 Traffic Sign Detection 交通標誌偵測 #
在啟動偵測節點之前,執行相機校準程序,以確保相機回送影像正確一致:
$ ros2 launch turtlebot3_autorace_camera intrinsic_camera_calibration.launch.py
$ ros2 launch turtlebot3_autorace_camera extrinsic_camera_calibration.launch.py
然後,啟動 traffic sign detection 交通標誌偵測節點,並指定任務關卡類型:
必須從以下選項中,選擇一個具體的 mission 任務:
- intersection, construction, parking, level_crossing, tunnel
$ ros2 launch turtlebot3_autorace_detect detect_sign.launch.py mission:=SELECT_MISSION
此指令啟動偵測流程,並允許 TurtleBot3 識別並回應所選的交通標誌。
可視化檢測結果 #
若要檢查偵測到的交通標誌,請開啟 rqt,然後導至:Plugins > Visualization > Image View
建立一個新的 image view 視窗並選擇 topic:/detect/image_traffic_sign/compressed
這將即時顯示交通標誌偵測的結果。偵測到的交通標誌,將根據指定的任務疊加在螢幕上。
以下範例,是在不同任務成功偵測出交通標誌:







C8.5. AutoRace 關卡任務說明 #
如要參加比賽,請務必閱讀完本章節的全部內容。
C8.5.1. 第一關: Traffic Light 紅綠燈 #
本節介紹如何讓 TurtleBot3 辨識交通號誌並走完路線,從而完成交通號誌燈任務。
紅綠燈偵測 #
1. 開新 terminal 並啟動 Autorace Gazebo 模擬
$ ros2 launch turtlebot3_gazebo turtlebot3_autorace_2020.launch.py
2. 開新的 terminal 啟動內部攝影機校準節點
$ ros2 launch turtlebot3_autorace_camera intrinsic_camera_calibration.launch.py
3. 開新的 terminal 啟動外部攝影機校準節點
$ ros2 launch turtlebot3_autorace_camera extrinsic_camera_calibration.launch.py
4. 開新的 terminal 啟動 traffic light detection node 要加上 calibration 選項
$ ros2 launch turtlebot3_autorace_detect detect_traffic_light.launch.py calibration_mode:=True
5. 在 remote PC 執行 rqt
$ rqt
6. 滑鼠點擊 Plugins > Visualization > Image view,開 2 個 image view 視窗
7. 在一個視窗中,選擇 /detect/image_traffic_light/compressed 主題。在另一個視窗中,選擇四個 topics 之一,來查看被遮罩的影像:
/detect/image_red_light, /detect/image_yellow_light, /detect/image_green_light, /detect/image_traffic_light。



8. 點擊 Plugins > Configuration > Dynamic Reconfigure
9. 調整 /detect/traffic_light 中的參數,適當調整各個遮罩影像 topic 的配置。以便紅綠燈顏色可以清楚偵測並分辨。

儲存校正後的資料 #
1. 開啟位於 turtlebot3_autorace_detect/param/traffic_light/ 的 Traffic_light.yaml 檔案。
$ gedit ~/turtlebot3_ws/src/turtlebot3_autorace_2020/turtlebot3_autorace_detect/param/traffic_light/traffic_light.yaml

2. 將調整後的數值覆寫,然後存檔
測試紅綠燈偵測 #
1. Ctrl + C 關閉所有 terminal
2. 開新的 terminal 執行 Autorace Gazebo simulation。
$ ros2 launch turtlebot3_gazebo turtlebot3_autorace_2020.launch.py
3. 開新的 terminal 啟動內部攝影機校準節點
$ ros2 launch turtlebot3_autorace_camera intrinsic_camera_calibration.launch.py
4. 開新的 terminal 啟動外部攝影機校準節點
$ ros2 launch turtlebot3_autorace_camera extrinsic_camera_calibration.launch.py
5. 開新的 terminal 啟動 traffic light detection node
$ ros2 launch turtlebot3_autorace_detect detect_traffic_light.launch.py
6. 開新的 terminal 執行 rqt_image_view
$ rqt_image_view
7. 檢查每個 topics: /detect/image_red_light, /detect/image_yellow_light, /detect/image_green_light.
C8.5.2. 第二關: Intersection 左右轉 #
C8.5.3. 第三關: Construction/Obstacles 避障 #
本節介紹如何完成避障任務。如果 TurtleBot3 在沿著車道行駛時遇到障礙物,它就會轉向相反車道以避開該物體,然後再返回原來的車道。
避障流程 #
1. TurtleBot3 正在沿著賽道行駛,它判斷路徑上可能存在障礙物。
2. 如果在危險區域內偵測到障礙物,Turtlebot3 就會轉向相反車道以避開障礙物。
3. TurtleBot3 再次返回到原來的車道並繼續行駛。
如何進行 Construction/ Obstacles 避障任務 #
1. Ctrl + C 關閉所有 terminal
2. 開新的 terminal 執行 Autorace Gazebo simulation。
$ ros2 launch turtlebot3_gazebo turtlebot3_autorace_2020.launch.py
3. 開新的 terminal 啟動內部攝影機校準節點
$ ros2 launch turtlebot3_autorace_camera intrinsic_camera_calibration.launch.py
4. 開新的 terminal 啟動外部攝影機校準節點
$ ros2 launch turtlebot3_autorace_camera extrinsic_camera_calibration.launch.py
5. 開新的 terminal 啟動 construction mission node 節點
$ ros2 launch turtlebot3_autorace_construction mission_construction.launch.py
6. 在影像視窗上,你可以觀看 LiDAR 光達視覺化圖。顯示偵測到的光達點和危險區域。

C8.5.4. 第四關: Parking 停車 #
C8.5.5. 第五關: Level Crossing/STOP Bar 柵欄 #
本節介紹如何偵測柵欄。TurtleBot3 應該會偵測停車標誌並等待柵欄打開
柵欄偵測流程 #
1. 過濾影像以提取出紅色遮罩影像。
2. 在遮罩圖像中找到長方形。
3. 連接三個正方形,使之形成一條直線。
4. 透過測量直線的斜率,來確定柵欄是開啟 or 關閉。
柵欄升降如何偵測 Level Crossing Detection #
1. Ctrl + C 關閉所有 terminal
2. 開新的 terminal 執行 Autorace Gazebo simulation。
$ ros2 launch turtlebot3_gazebo turtlebot3_autorace_2020.launch.py
3. 開新的 terminal 啟動內部攝影機校準節點
$ ros2 launch turtlebot3_autorace_camera intrinsic_camera_calibration.launch.py
4. 開新的 terminal 啟動外部攝影機校準節點
$ ros2 launch turtlebot3_autorace_camera extrinsic_camera_calibration.launch.py
5. 開新的 terminal 啟動 level crossing detection node 要加上 calibration 選項
$ ros2 launch turtlebot3_autorace_detect detect_level_crossing.launch.py calibration_mode:=True
6. 開新的 terminal 執行 rqt,開 2 個 rqt_image_view plugin 視窗
$ rqt
7. 選擇 2 個 topics: /detect/image_level_color_filtered/compressed, /detect/image_level/compressed

8. 在 Dynamic Reconfigure Plugin 插件中,調整 detect_level_crossing 內的參數

9. 開啟 level.yaml 檔,位於 turtlebot3_autorace_detect/param/level/

10. 將調整後的數值覆寫,然後存檔
測試 柵欄升降偵測 #
1. Ctrl + C 關閉所有 terminal
2. 開新的 terminal 執行 Autorace Gazebo simulation。
$ ros2 launch turtlebot3_gazebo turtlebot3_autorace_2020.launch.py
3. 開新的 terminal 啟動內部攝影機校準節點
$ ros2 launch turtlebot3_autorace_camera intrinsic_camera_calibration.launch.py
4. 開新的 terminal 啟動外部攝影機校準節點
$ ros2 launch turtlebot3_autorace_camera extrinsic_camera_calibration.launch.py
5. 開新的 terminal 啟動 level crossing detection node
$ ros2 launch turtlebot3_autorace_detect detect_level_crossing.launch.py
6. 開新的 terminal 執行 rqt_image_view
$ rqt
7. 在 Image View Plugin 插件視窗檢查 image topics: /detect/image_level/compressed
C8.5.6. 第六關: Tunnel 隧道 #
本節介紹如何完成 Tunnel 隧道任務。TurtleBot3 必須使用地圖和導航,才能穿越沒有車道的障礙區域。
如何進行 Tunnel 隧道任務 #
1. Ctrl + C 關閉所有 terminal
2. 開新的 terminal 執行 Autorace Gazebo simulation。
$ ros2 launch turtlebot3_gazebo turtlebot3_autorace_2020.launch.py
3. 開新的 terminal 並啟動 tunnel mission node 節點。此節點會執行 navigation 導航,並指定初始位置和目標位置。
$ ros2 launch turtlebot3_autorace_tunnel mission_tunnel.launch.py
4. 在 Rviz2 畫面上,您可以即時觀看 TurtleBot3 生成圖並遵循路徑。

設定初始位置和目標位置 #
你可以修改初始位置和目標位置,來符合你的規劃。
1. 開啟位於 turtlebot3_autorace_mission/param/ 的 navigation.yaml 文件
$ gedit ~/turtlebot3_ws/src/turtlebot3_autorace/turtlebot3_autorace_mission/param/navigation.yaml

2. 將調整後的數值覆寫,然後存檔