# ROS tutorial
#### 主講人 : 張羽辰
----
開始前確認一下有人還沒下載好virtual box跟我們給的image的嗎?
https://reurl.cc/0dkXKb
----
### 簡報:
https://reurl.cc/A4e8z3
----
### 參考講義 : ROS Tutorial Introduction
https://reurl.cc/x6mXnL
---
# Outline
+ 認識ROS
+ turtlesim
+ ROS的架構
+ turtlebot and minibot
+ ROS的檔案結構
+ 寫一個node
---
# 認識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 支援的作業系統
### Ubuntu及Linux基本簡介
+ ROS主要配合的作業系統是==Ubuntu==
+ Ubuntu是以桌面應用為主的Linux發行版。
+ 待會使用的作業系統是Ubuntu
----
講個笑話:
為什麼太空人使用linux而不使用windows?
----
因為他們打不開windows。
----
linux 基本指令 :
+ cd:用以移動到目標路徑
+ ls:顯示當前路徑下的資料夾與檔案<!-- + ls -a:看到所有檔案,包含隱藏檔案 -->
+ gedit:一種文字編輯軟體
+ chmod:變更檔案權限
+ 更多指令:https://blog.ossii.com.tw/?p=2241#
+ 再更多:自己上網查~~
----
終端機介面介紹:
+ 畫面說明:
![](https://hackmd.io/_uploads/SJCG98vga.png =70%x)
+ 搞清楚在哪台電腦下指令很重要!!!(後面可能同時運行兩台電腦)
+ 這邊我的電腦名稱與使用者名稱打相反了
----
## 終端機的幾個快捷鍵
+ `ctrl + T`: 開啟終端機
+ `ctrl + shift + T`: 開啟終端機tab頁面
+ `ctrl + C`: 可停止大部分正在執行的程序
---
# turtlesim
+ 設定ros網路
+ 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
----
## turtlesim demo
----
```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==機器人的網路規模通常很小,所以不太會有這個問題
----
![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
----
![image](https://hackmd.io/_uploads/rkf5C56T0.png)
###### image source:RSL ROS Tutorial
----
## 架構及名詞解釋
+ Node & Master
+ ***rosnode***
+ Publisher & Subscriber
+ Messages & Topic
+ ***rostopic***
+ package
+ ***roscd***
----
### 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
----
### Publisher & Subscriber
![](https://i.imgur.com/0BAmV13.png)
###### image source:ROS Tutorial Introduction
一些重要的概念:
+ 1個Pub Node可以發多個topic
+ 1個Sub Node可以訂閱多個topic
+ 一個node可以同時是Pub和Sub
----
### Messages & Topic
+ Node 間要傳遞資料靠 Messages 來溝通。
+ Message 在發布時我們會給它加上 Topic,妳可以把 Message 想像成一個箱子,Node 間要傳遞資料時會把資料放到這個箱子裡面,並在這個箱子上面貼上一個標籤,這個標籤就是 Topic。
![](https://i.imgur.com/0BAmV13.png=80x)
###### image source:ROS Tutorial Introduction
----
#### [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
----
+ 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
用google收尋
*for example:* LaserScan
<!-- ----
### 資訊的傳遞
[![](https://i.imgur.com/I1vVyBH.png =100%x)](https://i.imgur.com/I1vVyBH.png) -->
----
### Package
Package 在建立專案時的一個基本單位。
因為妳的專案很可能是程式與程式之間在溝通的,妳把那些 Node 整合起來,配合妳自己寫的函式庫,蒐集的資料等等的,整個包裝起來成一個能讓別人使用的程式,這樣的東西就是一個 Package。
----
#### roscd
```shell=
roscd #移動到指定package的位置
```
----
## ROS 一些基本指令
### rosrun & roslaunch & roscore
----
#### [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`
----
## 連接機器人
```
# 開啟新的終端機
# ssh連接機器人
ssh pi@10.0.0.1 ##passward=mrlrobot
```
[機器人連線debug流程](/HFL8a7CWQjCcTr70n4VAfw)
尚未完成近期會完成他
----
## 執行機器人身上開啟sensor的node
仔細看現在的電腦與使用者名稱是pi@mrl-robot
```
# 假如是minibot下
roslaunch hypharos_minibot project_sample.launch
# 假如是turtlebot下
roslaunch turtlebot3_bringup turtlebot3_robot.launch
```
----
## 起動 rviz 視覺化套件
```
# 開啟新的終端機
rviz
```
----
## 遙控機器人
```
#Extra moving !!!
roslaunch teleop teleop_key.launch
```
<!-- ## Minibot & Turtlebot
參見講義
https://hackmd.io/@Mes/RosTutorial_Intro#Minibot-amp-Turtlebot
-->
----
### 用圖形介面看機器人裡面的資料:sftp
![](https://hackmd.io/_uploads/Bk8syuvxT.png)
---
# 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
``` -->
---
# 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,"title":"2024/03/08 ROS tutorial","description":"image","contributors":"[{\"id\":\"9cfbb511-72a2-4618-81e9-5431830d83fa\",\"add\":21181,\"del\":11139}]"}