# ROS tutorial #### 主講人 : 張羽辰 ---- ### 簡報: https://reurl.cc/A4e8z3 ---- ### 參考講義 : ROS Tutorial Introduction https://reurl.cc/x6mXnL ---- 開始前確認一下有人還沒下載好virtual box跟我們給的image的嗎? https://reurl.cc/0dkXKb ---- 下載好的人幫我設定好網路介面卡 {%youtube MpIcWeW5swM %} --- # Outline + 認識ROS + Ubuntu終端機的基本認識 + turtlesim demo + ROS的架構 + turtlebot and minibot + Coding Time --- # 認識ROS + 什麼是 ROS? + 為什麼需要 ROS? + ROS 支援的作業系統 ---- ## 什麼是 ROS? ### ROS(Robot Operation System) p.s. 嚴格來說,ROS並不是一個作業系統,而是一個==中介軟體==,但因為不影響後面的理解,我這裡不多提,有興趣的可以參閱講義 ---- ## 為什麼需要ROS? ### Do not Reinvent the Wheel ![](https://i.imgur.com/0FaLpge.png =50%x) ###### 圖片擷取自 : [A History of ROS (Robot Operating System)(https://www.theconstructsim.com/history-ros/)](https://www.theconstructsim.com/history-ros/) <!--附圖來自ROS創立者的幻燈片--> ---- ### 甚麼叫 Reinvent the Wheel? 舉例 : 研究避障機器人的避障演算法 + 需求: + 移動元件(馬達) + 驅動程式 + 測距感測器(光達) + 驅動程式 + 元件的溝通 + 避障演算法(傳統式) + 避障演算法(新研究) ---- 以上需求中: ***驅動程式、元件的溝通、避障演算法(傳統式)*** 都是與研究無關的,都是Reinvent the Wheel ---- ## ROS 如何解決這些問題? ![](https://i.imgur.com/XOh8faB.png) ###### 圖片擷取自 : [The ROS Ecosystem(https://www.ros.org/blog/ecosystem/)](https://www.ros.org/blog/ecosystem/) ---- ### 元件的溝通 -> plumbing ROS擁有自己的訊息傳遞系統 publish/subscribe pattern ---- ### 驅動程式以及演算法 -> community 由於ROS是免費且開源的,所以很容易的養起了一大群創作者。 社群的創作者們,再將自己的研究丟到github,讓大家可以不必再重造輪子。 <!--希望講到這裡花約20分鐘--> ---- ## ROS 支援的作業系統 + ROS主要配合的作業系統是==Ubuntu== + Ubuntu是以桌面應用為主的Linux發行版。 + 待會使用的作業系統是Ubuntu ---- 講個笑話: 為什麼太空人使用linux而不使用windows? ---- 因為他們打不開windows。 --- ## Ubuntu終端機的基本認識 + 終端機的幾個快捷鍵 + 終端機的介面介紹 + 基本指令 ---- ## 終端機的幾個快捷鍵 + `Ctrl + Alt + T`: 開啟終端機 + `Ctrl + Shift + T`: 開啟終端機tab頁面 + `Ctrl + C`: 可停止大部分正在執行的程序 + `Tab`: 列出你可能想打的指令或自動補全 ---- ## 終端機介面介紹: + 畫面說明: ![](https://hackmd.io/_uploads/SJCG98vga.png =70%x) + 搞清楚在哪台電腦下指令很重要!!!(後面可能同時運行兩台電腦) + 這邊我的電腦名稱與使用者名稱打相反了 ---- ## 基本指令 : + cd:用以移動到目標路徑 + ls:顯示當前路徑下的資料夾與檔案<!-- + ls -a:看到所有檔案,包含隱藏檔案 --> + gedit:一種文字編輯軟體 + 更多指令:https://blog.ossii.com.tw/?p=2241# + 再更多:自己上網查~~ --- # turtlesim demo <!-- ## 設定ros網路 ---- 開啟終端機->打開.bashrc(輸入gedit .bashrc指令) ![image](https://hackmd.io/_uploads/HkRmE7CT0.png) ---- 將 ![image](https://hackmd.io/_uploads/HJUFE7Rp0.png) 改成 ![image](https://hackmd.io/_uploads/BJNs4mCTR.png) 然後`ctrl + s`儲存 回到終端機`ctrl + c`關閉現在正在執行的程序(`gedit .bashrc`) 然後`source .bashrc`執行.bashrc檔案 ---- 解釋.bashrc簡單理解是開啟終端機時會執行的一個腳本,所以其實你不執行`source .bashrc`直接從開終端機也會有同樣的效果。 裡面可以寫一些簡單的腳本,比如 ![image](https://hackmd.io/_uploads/B1yV8mAaA.png) ---- 你想增加甚麼功能也可以自己去問chatGPT比如 https://chatgpt.com/share/66f0a663-9bb4-8008-82d0-5eec58e6913e --> ---- ```shell= # initialize ros master roscore # 開一個新的終端機 # launch turtlesim_node rosrun turtlesim turtlesim_node # 開一個新的終端機 # using keyboard to control the turtle rosrun turtlesim turtle_teleop_key ``` ---- ## turtlesim demo ![](https://i.imgur.com/4tyqX7k.png) ---- ## turtlesim demo 剛剛這三個終端機先別關掉,等等講解會用到。 --- # ROS 的架構 + Peer to peer (P2P) + 架構及名詞解釋 + 資訊的傳遞 + ROS 一些基本指令 + ROS 檔案架構 ---- ## Peer to peer (P2P) ---- ## 傳統主從式架構 ![image](https://hackmd.io/_uploads/S135znqV6.png) ###### image source:https://en.wikipedia.org/wiki/Client%E2%80%93server_model ---- ## 缺點 + 假設今天發送請求的人非常多,且請求發送的非常頻繁,那麼伺服器的速度可能就會變得非常慢 ---- ## Peer to peer (P2P) ![image](https://hackmd.io/_uploads/BkWIXn9N6.png) ###### image source:https://ithelp.ithome.com.tw/articles/10216158 ---- + 一種分散式系統架構 + 每台電腦(節點)都同時是客戶端與伺服器端 + 所有人都負責儲存了全部或部分的所有資料,並且也都會處理收到的請求。 + 優點 + 擁有很好的平行處理能力,效能也會比較好 + 缺點 + 如果規模太大整個網路會很亂,且會很吃網路 + ==BUT==機器人的網路規模通常很小,所以不太會有這個問題 ---- ## 不同類型的p2p ![image](https://hackmd.io/_uploads/rJrPKqp6A.png) ###### image source:https://www.researchgate.net/figure/P2P-architectures-at-a-glance-a-Centralized-architecture-b-Pure-P2P-architecture_fig2_332539196 ---- ## ROS1採用的是中心化p2p + 中心化p2p的中心server做的事情並不向主從式架構的server一樣進行訊息的傳遞,他的工作是將兩個節點進行牽網路線的動作 + 小實驗demo ---- ## 架構及名詞解釋 + 節點(node) + 訊息(messenger) + 節點身分 + 包(package) ---- ## 節點(node) ### Node & Master + node 其實只不過是 ROS 套件中的一個可執行檔。 + 節點的多寡取決於開發者 + 舉例:避障機器人 + Master 是一個特殊的 Node,也就是我們前面所說的 Server。 + node 開啟時需要跟 Master 進行註冊的動作。 ---- #### [rosnode](http://wiki.ros.org/rosnode) 在rosnode中提供以下幾種動作可以使用 ```shell= rosnode info <node_name> #print information about node rosnode kill <node_name> #kill a running node rosnode list #list active nodes rosnode machine <machine-name> #list nodes running on a particular machine or list machines rosnode ping <node_name> #test connectivity to node rosnode cleanup #purge registration information of unreachable nodes ``` 常用: + rosnode list ---- ### 訊息(messenger) ### Messages & Topic + Node 間要傳遞資料靠 Messages 來溝通。 + Message 在發布時我們會給它加上 Topic,妳可以把 Message 想像成一個箱子,Node 間要傳遞資料時會把資料放到這個箱子裡面,並在這個箱子上面貼上一個標籤,這個標籤就是 Topic。 ![](https://i.imgur.com/0BAmV13.png=80x) ###### image source:ROS Tutorial Introduction ---- ### 節點身分----Publisher & Subscriber ![](https://i.imgur.com/0BAmV13.png) ###### image source:ROS Tutorial Introduction 一些重要的概念: + 1個Pub Node可以發多個topic + 1個Sub Node可以訂閱多個topic + 一個node可以同時是Pub和Sub ---- + Message 是一種數據結構(much like C structs) ![image](https://hackmd.io/_uploads/BJy_Nh6a0.png =50%x) ###### image source:https://medium.com/@neouma29/structures-in-c-71bc2a078c21 ---- #### message file 如果是ros預設的可以使用google收尋 *for example:* LaserScan ![image](https://hackmd.io/_uploads/Syx56Gx91x.png) ##### https://docs.ros.org/en/noetic/api/sensor_msgs/html/msg/LaserScan.html ##### https://docs.ros.org/en/noetic/api/geometry_msgs/html/msg/Twist.html ---- #### [rostopic](http://wiki.ros.org/rostopic) 在rostopic中提供以下幾種動作可以使用 ```shell= rostopic bw <topic_name> #display bandwidth used by topic rostopic delay <topic_name> #display delay for topic which has header rostopic echo <topic_name> #print messages to screen rostopic find <msg-type> #find topics by type rostopic hz <topic_name> #display publishing rate of topic rostopic info <topic_name> #print information about active topic rostopic list #print information about active topics rostopic pub <topic-name> <msg-type> [data...] #publish data to topic rostopic type <topic-name> #print topic type ``` ---- 常用: + rostopic echo + rostopic info + rostopic list + rostopic pub + rostopic hz <!-- ---- ### 資訊的傳遞 [![](https://i.imgur.com/I1vVyBH.png =100%x)](https://i.imgur.com/I1vVyBH.png) --> ---- ### 包(package) Package 在建立專案時的一個基本單位。 因為妳的專案很可能是程式與程式之間在溝通的,妳把那些 Node 整合起來,配合妳自己寫的函式庫,蒐集的資料等等的,整個包裝起來成一個能讓別人使用的程式,這樣的東西就是一個 Package。 + 跟node一樣package的細分程度取決開發者與需求 ---- #### roscd ```shell= roscd #移動到指定package的位置 ``` ---- ## ROS 一些基本指令 ### roscore & rosrun & roslaunch ---- #### [roscore](http://wiki.ros.org/roscore) roscore可以讓你啟動master ```shell= roscore ``` ---- #### [rosrun](http://wiki.ros.org/rosbash#rosrun) rosrun可以讓你去執行特定package下的code,使用方法如下 ```shell= rosrun <package> <executable> ## example rosrun turtlesim turtlesim_node rosrun turtlesim turtle_teleop_key ``` ---- #### [roslaunch](http://wiki.ros.org/roslaunch) roslaunch則透過預先設定的launch file幫你一次開啟很多程式 並且會自動偵測有無開啟roscore,若是沒有並且你是rosmaster便會自動幫你開啟。 ```shell= ## 因為launch file 是放在某個package底下,還是要加package roslaunch <package> <launch file> ## example roslaunch hypharos_minibot project_sample.launch ``` --- # Minibot & Turtlebot + 連線 + 起動 rviz 視覺化套件 + 遙控機器人 ---- ## 連線 <!-- ---- 每兩人領 + 一台turtlebot3或minibot + 一顆電池 + 一條網路線 --> <!-- ---- ## 更改網路設定 執行`gedit .bashrc` 將 ![image](https://hackmd.io/_uploads/BJNs4mCTR.png) 改成 ![image](https://hackmd.io/_uploads/HJUFE7Rp0.png) 然後`ctrl + s`儲存 回到終端機`ctrl + c`關閉現在正在執行的程序(`gedit .bashrc`) 然後將所有終端機關閉載重開一個執行`roscore` --> ---- ## 連接機器人 ```shell= # 開啟新的終端機 # ssh連接機器人 ssh pi@10.0.0.1 ##passward=mrlrobot ``` ---- ## 執行機器人身上開啟sensor的node 仔細看現在的電腦與使用者名稱是pi@mrl-robot ```shell= # 假如是minibot下 roslaunch hypharos_minibot project_sample.launch # 假如是turtlebot下 roslaunch turtlebot3_bringup turtlebot3_robot.launch ``` ---- ## 起動 rviz 視覺化套件 ```shell= # 開啟新的終端機 rviz ``` ---- ## 遙控機器人 ```shell= #Extra moving !!! # 假如是minibot下 roslaunch teleop teleop_key.launch model:=minibot # 假如是turtlebot下 roslaunch teleop teleop_key.launch model:=turtlebot ``` 開啟後這個位置可以確認你是否用對機器人 ![image](https://hackmd.io/_uploads/H1M4MmgqJg.png) ---- 如果你長時間都會使用某一台機器人,你可以透過更改環境變數來設定預設機器人。 ```shell= gedit ~/.bashrc ``` 找到以下這兩行(139行以及140行) 可以看到==現在預設是使用minibot==,你若是==想預設turtlebot==只要將139行反註解、140行註解掉即可。 ![image](https://hackmd.io/_uploads/By_AZGl51x.png) --- ## Coding Time ---- 由於你們要學、要熟悉的東西太多了,現階段我們直接給你們sample code,你們直接改裡面的code就好,對於新的package的環境架設這邊就暫且跳過。 ---- ## package 結構 ![](https://hackmd.io/_uploads/rkJaM_vg6.png =500x) ###### 出處:https://medium.com/swlh/7-simple-steps-to-create-and-build-our-first-ros-package-7e3080d36faa ![](https://i.imgur.com/A8HFSr5.png =340x) ---- ## package 結構 約定 + scripts + 放可執行檔,現階段可以直接理解成python檔放裡面 + src + 放需要編譯的code,現階段可以直接理解成cpp檔放裡面 + launch + 放launch文件 ---- ## Code 位置 如果要寫c++檔案位置在 ~/catkin_ws/src/mrlrobot_sample_code/sample_code /src/cpp_sample.cpp 如果要寫python檔案位置在 ~/catkin_ws/src/mrlrobot_sample_code/sample_code /scripts/py_sample.py ---- ![image](https://hackmd.io/_uploads/SkVrtQec1e.png) ---- ## 執行sample code 無關turtlebot或minibot,因為我已經將他們整合在一起了。 只需要知道你要使用python還是c++ ```shell= #先確保roscore與機器人sensor已開啟 #開啟新的終端機 #run the c++ sample code rosrun sample_code cpp_sample #run the python sample code rosrun sample_code py_sample.py ``` ---- #### 執行sample code練習~ ---- ## 執行sample launch file ```shell= #先確保roscore與機器人sensor已開啟 #開啟新的終端機 #run the c++ sample launch file roslaunch sample_code cpp_sample.launch #run the python sample launch file roslaunch sample_code py_sample.launch ``` ---- #### 執行sample launch file練習~ ---- ## 編譯 你更動了cpp檔記得存檔後進行編譯,編譯的方法是: 使用終端機移動到catkin_ws然後下 `catkin_make` ```shell= cd ~/catkin_ws catkin_make ``` 或是幫你寫好的快捷指令`cm` 如果更動python只需要進行存檔即可 ---- #### 編譯練習~ ---- ## 解釋code #### https://docs.ros.org/en/noetic/api/sensor_msgs/html/msg/LaserScan.html ---- ### 用圖形介面看機器人裡面的資料:sftp ![](https://hackmd.io/_uploads/Bk8syuvxT.png) 使用者:pi 密碼: mrlrobot <!-- # ROS的檔案結構 + catkin 簡介 + package 結構 + package的建立 + 編譯package ---- ## catkin 簡介 + catkin 是一個開發環境整合套件 + 讓你整個專案的開發有個統一的格式。 + 分為以下三個區塊 * src 用來存放 source code 以及各個 project 的地方,src 中的存放單位為 package * build 用來從放程式碼編譯過程中的中間產物 * devel 編譯完的執行檔跟一些環境變數設定檔都會放置在這 ---- ![](https://i.imgur.com/A8HFSr5.png) ---- ## package 結構 ![](https://hackmd.io/_uploads/rkJaM_vg6.png) ###### 出處:https://medium.com/swlh/7-simple-steps-to-create-and-build-our-first-ros-package-7e3080d36faa ---- ## package的建立 ```shell= catkin_create_pkg your_pkg_name depends # example catkin_create_pkg test_pkg roscpp rospy std_msgs ``` ---- ## 編譯package ```shell= cd ~/catkin_ws catkin_make # 編譯所有package source ~/catkin_ws/devel/setup.bash # 加入環境變數 ``` --- ## 寫一個node + write a node + Publisher + Subscriber ---- ## write a node 參見講義 https://hackmd.io/IdGmLr0HSHGGMXT2aDLdAQ?view#roscpp ---- ## 小提醒 + 假如用python寫code + 第一次寫(新增) + 記得編輯Cmakelist.txt檔 + 記得給權限 + 假如用C++寫code + 第一次寫(新增) + 記得編輯Cmakelist.txt檔 + 每次編輯都要重新catkin_make進行編譯 --> <!-- ## turtlesim_try ```shell= cd ~/catkin_ws/src catkin_create_pkg turtlesim_try roscpp geometry_msgs cd turtlesim_try mkdir src cd src touch pubnode.cpp gedit pubnode.cpp ``` <!-- ![image](https://hackmd.io/_uploads/SkjZ4a9Ep.png) --> <!-- ![image](https://hackmd.io/_uploads/H14QVa5VT.png) --> <!-- ```shell= cd ~/catkin_ws/src/turtlesim_try gedit CMakeLists.txt ``` ![image](https://hackmd.io/_uploads/rkw2Va9Va.png) ```shell= cd ~/catkin_ws catkin_make ``` --> --- # 機器人正確關機順序 ---- ```shell= # 前往機器人的終端機(確認電腦名稱與使用者名稱為pi@mrl-robot) sudo poweroff #對機器人下關機指令 password: mrlrobot ``` 然後確認pi沒有亮黃燈之後才可將電源關閉。 最後才將電池移除。 --- # MRL 器材位置介紹 ---- # 機器人區 ![](https://hackmd.io/_uploads/BybeDKvga.jpg =50%x) ---- # 網路線存放位置 ![](https://hackmd.io/_uploads/SJbxDtDga.jpg =50%x) ---- # 電池存放位置 #### 左邊有電 右邊沒電 #### 用完請充電 ![](https://hackmd.io/_uploads/BkNdUFDxa.jpg =50%x) ---- # 充電器 #### 一次一台充一顆 #### 藍色電池插右邊銀色左邊 ![](https://hackmd.io/_uploads/SkNu8twe6.jpg =50%x)
{"metaMigratedAt":"2023-06-18T02:58:10.667Z","metaMigratedFrom":"YAML","breaks":true,"description":"image","title":"2025/02/17 ROS tutorial","contributors":"[{\"id\":\"9cfbb511-72a2-4618-81e9-5431830d83fa\",\"add\":26827,\"del\":14489}]"}
    981 views