# ros 通訊設置(Linux) 在進行 ROS 的通訊設置前要先注意自己 container 的設定是否正確,==這在建立 container 之前就必須設置好==,沒辦法中途更改設定。 1. 將管理 USB 接口的資料夾 ==/dev== mount 進 container 中: 建立 container 時加入此訊息,如此一來 container 要使用 USB 接口時才找的到。由於 window 系統和 Linux 系統管理接口的方式與接口都不同(windows 接口是 COM ; Linux 是 tty),==所以使用 windows 時需要使用其他方法==。未來的專題或比賽有許多部份會在 Linux 系統下進行(例如:樹莓派),到時候就能派上用場。 ```yaml volumes: - /dev:/dev ``` 2. 給予 container 真正的 root 權限 ```yaml privileged: true ``` 以上是直接使用 docker-compose 建立 container 時需要添加的部份,找找看最初提供給你們的 yaml file 可以找到。 ## 下載通訊的資源包 在 workspace/src 中下載 git clone rosserial 的資源包。 ``` git clone https://github.com/ros-drivers/rosserial.git --branch noetic-devel ``` 之後將 **rosserial/rosserial_python** 與 **rosserial/rosserial_msgs** 兩檔案移到 rosserial 外,之後就可以刪除 rosserial 。rosserial_python 是使用 python 的方式通訊 ; 而 rosserial_server 則是使用 cpp 的方式,==這不影響你通訊檔案的形式是 python 還是 c== ,接下來使用的通訊方式是 python。  再來就建立 package 並且放入測試檔案 ``` catkin_create_pkg <package_name> roscpp rospy std_msgs ``` 範例程式: :::spoiler **stm_communication.cpp** ros 和 stm 端的通訊和 ros 自己的 publisher 與 subscriber 一樣,發訊息就用 pub ; 收訊息就用 sub。 ```cpp= //stm_communication.cpp #include <iostream> #include "ros/ros.h" #include <std_msgs/Int64.h> using namespace std; int main(int argc, char** argv){ ros::init(argc, argv, "stm_communication"); ros::NodeHandle nh; ros::Publisher pub = nh.advertise<std_msgs::Int64>("counting", 1); std_msgs::Int64 hello; hello.data = 0; while(ros::ok()){ pub.publish(hello); hello.data++; ROS_INFO("publish %d", hello.data); ros::Duration(0.5).sleep(); } ROS_INFO("== Terminate ! =="); return 0; } ``` ::: 修改 CMakeLists.txt 後就可以編譯了。 ## 運行通訊程式 1. 確認接口名稱 ``` sudo sysctl kernel.dmesg_restrict=0 dmesg | grep tty ``` 或 ``` ls /dev/tty* ``` 通常是 ttyUSB0 或 ttyACM0 2. 打開接口權限 **將<usb>替換USB你通訊所使用的接口** ``` sudo chmod 777 /dev/<usb> ``` 3. 安裝必要的 module ``` pip install serial pip install pyserial ``` 4. 啟動通訊節點(==記得先 roscore==) ``` rosrun rosserial_python serial_node.py _baud:=115200 _port:=/dev/<usb> ``` 出現以下內容表示成功通訊:  **通訊的 node 要留著,如果要 rosrun 其他程式要開新視窗,除非不需要通訊了。** 5. 跑 stm_communication.cpp  :::spoiler **作業(自行練習)** 試著做出 ros 端和 stm32 的雙向通訊,未來會用的到的。 :::
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up