---
tags : DIT 11th 教學 -- ROS
---
{%hackmd BJrTq20hE %}
# ROS tools
## 01. rosbag
### Introduction
rosbag 能夠將當前 topic 的訊息記錄下來,並在之後使用 play 將紀錄下的訊息回放
### Record
```bash=1
$ rosbag record [TOPIC_NAME]
```
- 紀錄所有 topic 的訊息
```bash=1
$ rosbag record -a
```
- 設定紀錄時長
```bash=1
$ rosbag record --duration 10 [TOPIC_NAME]
$ rosbag record --duration 10 /callback_vel
```
- 接收指定 node 所接收到的訊息
```bash=1
$ rosbag record --node=/navMec_node
```
### Play
將 bag file 中的訊息再次回放到該 topic 中
- 開始回放 bag file
```bash=1
$ rosbag play 0125.bag
```
- 用 ```rostopic``` 查看是否有訊息
```bash=1
$ rostopic echo /navMec_node
```
### Example
1. 先簡單寫一個 publisher
```cpp=1
#include "ros/ros.h"
#include "std_msgs/Int64.h"
int main(int argc, char **argv){
ros::init(argc, argv, "pub_tutorial");
ros::NodeHandle nh;
ros::Publisher pub = nh.advertise<std_msgs::Int64>("data_tutorial", 10);
std_msgs::Int64 data;
data.data = 5;
ros::Rate rate(2);
while(ros::ok()){
data.data += 2;
pub.publish(data);
rate.sleep();
}
}
```
2. 執行看看該 publisher ,並開另一個終端機以 ```rostopic echo``` 查看
```bash=1
$ rosrun [PACKAGE NAME] [NODE NAME]
```
```bash=1
$ rostopic echo [TOPIC NAME]
```
3. 在 package 底下創建 bag folder ( 統一環境而已,也可以用自己的方式儲存要用的 bag file )
```bash=1
$ roscd [PACKAGE NAME]
$ mkdir bag
$ cd bag
```
4. ```rosrun``` 剛剛的 publisher 並使用開另一個終端機紀錄訊息 ```rosbag record```
```bash=1
$ rosbag record --duration 10 [TOPIC NAME]
```
5. 使用 ```rosbag play``` 回放剛剛存好的 bag file ,並使用 ```rostopic echo``` 查看該 topic 得到的訊息
```bash=1
$ rosbag play [BAG FILE NAME]
```
```bash=1
$ rostopic echo [TOPIC NAME]
```
---
## 02. rqt
ROS 內建的 GUI 界面,單獨使用的話沒什麼用,需搭配各種 plugins
### 基本概念
- 啟動 rqt
```bash=1
$ rqt
```
啟動時會發現什麼東西都沒有,只有最上面那一列,因為目前沒有跑任何 node 而且也沒有使用任何 plugin

以 GUI 圖形界面直接啟用 plugin 的方式
1. <font color="FF9999">點擊 Plugins</font>

2. <font color="FF9999">選擇要用的 plugin ,例如我想看各個 node 之間的關聯,所以選擇 ```Node Graph```</font>

### Basic command
- 查看所有指令
```bash=1
$ rqt -h
```
- 查看當前所有 plugin
```bash=1
$ rqt --list-plugins
```
- 僅開啟當前 plugin
```bash=1
$ rqt -s [PLUGIN_NAME]
$ rqt -s rqt_graph
```
### Plugin 01 - <font color="yellow">rqt_graph</font>
rqt_graph 將 node 以及 topic 的關係可視化,在大型專案中能更清楚各個 node 或是 topic 間的關係
- 開啟
```rqt``` -> ```Plugin``` -> ```introspection``` -> ```Node Graph```
- 僅開啟 <font color="yellow">rqt_graph</font>
```bash=1
$ rqt_graph # or
$ rqt -s rqt_graph # or
$ rosrun rqt_graph rqt_graph
```
1. <font color="cyan">選擇想查看的類型</font>

2. <font color="cyan">濾出想查看的 topic 或是 node</font>

3. <font color="cyan">查看關係 - EXAMPLE</font>
例如底下的圖,/navMec_node 為 publisher 並傳送資料到 topic /push_vel 上,接著 /encoder_node 為 subscriber 接收這些資訊

### Plugin 02 - <font color="yellow">rqt_bag</font>
將 topic 中的 message 錄製下來,並以圖形化界面加上時間條顯示,尤其是在比賽前實際到比賽場地測試時,將所有資訊錄下來,即便測試時間有限,回去仍舊可以 debug
- 開啟
```rqt``` -> ```Plugin``` -> ```Logging``` -> ```Bag```
- 僅開啟 ```rqt_bag```
```bash=1
$ rqt_bag # or
$ rqt -s rqt_bag # or
$ rosrun rqt_bag rqt_bag
```
1. <font color="cyan">選擇要紀錄的 topic</font>
1. 點擊紅色按鈕

2. 選擇要紀錄的 topic 並開始紀錄

2. <font color="cyan">停止紀錄</font>
再次按下紅色按鈕
3. <font color="cyan">播放</font>
```bash=1
$ rqt_bag [BAG_FILE]
$ rqt_bag hello_world.bag
```
### Plugin 03 - <font color="yellow">rqt_plot</font>
rqt_plot 將 topic 中的資料圖形化,方便查看 message 的變化
- 開啟
```rqt``` -> ```Plugin``` -> ```Visualization``` -> ```Plot```
- 僅開啟 ```rqt_plot```
```bash=1
$ rqt_plot # or
$ rqt -s rqt_plot # or
$ rosrun rqt_plot rqt_plot
```
1. <font color="cyan">選擇要監控的 topic</font>

2. <font color="cyan">將其加入到底下的圖表</font>

3. <font color="cyan">取消監聽某一 topic</font>

4. <font color="cyan">設置圖表邊框距離</font>

5. <font color="cyan">設置圖表的 x-y 軸範圍以及圖表顯示的各種數據</font>

### Plugin 04 - <font color="yellow">rqt_topic</font>
監控 topic 的資料以及 **頻寬** 和 **頻率**,可以想像成 ```rostopic``` 圖形界面
- 開啟
```rqt``` -> ```Plugin``` -> ```Topics``` -> ```Topic Monitor```
- 僅開啟 ```rqt_topic```
```bash=1
$ rqt_topic # or
$ rqt -s rqt_topic # or
$ rosrun rqt_topic rqt_topic
```
1. <font color="cyan">勾選欲監控的 topic</font>

2. <font color="cyan">點擊查看更詳細的資訊</font>

### Plugin 05 - <font color="yellow">rqt_console</font>
取得當前 Node 或 Topic 印出於 console 的東西
相較於使用終端機看,rqt_console 能濾出一些不必要的訊息,加速 debug
- 開啟
```rqt``` -> ```Plugin``` -> ```Logging``` -> ```Console```
- 僅開啟 ```rqt_console```
```bash=1
$ rqt_console # or
$ rqt -s rqt_console # or
$ rosrun rqt_console rqt_console
```
1. <font color="cyan">選擇欲加入的方式</font>

2. <font color="cyan">查看 messages</font>

3. <font color="cyan">用 ```...containing``` 濾掉不要的訊息</font>
- EX : 濾掉所有含有 4 的訊息

### Plugin 06 - <font color="yellow">rqt_launch</font>
圖形化的 launch 界面
- 開啟
```rqt``` -> ```Plugin``` -> ```Configuration``` -> ```Launch```
- 僅開啟 ```rqt_launch```
```bash=1
$ rqt -s rqt_launch
```
1. <font color="cyan">選擇 launch file</font>

2. <font color="cyan">加入該 launch 檔需要的參數</font>

3. <font color="cyan">增加其他 node 需要的 args</font>

4. <font color="cyan">執行全部 / 停止全部</font>

<!-- ### Plugin 07 - <font color="yellow">rqt_reconfigure</font>
- 內容較為複雜,建議熟悉 ROS 架構之後再看,所以就先放 [這了](/s/rJTKVRCsj) -->
---