--- title: 【韌體】STM x ROS1 tags: TTennis Pickup Robot disqus: hackmd --- <h1 style="text-align: center; color: orange;"> 🛠️ 【韌體】STM32CubeIDE 🛠️ </h1> <h2 style="text-align: center; color: skyblue;">STM x ROS1</h2> <center> 此篇是跟著 [C/C++ 混編專案架構](/@925/stm-project) 走的哦 通訊路徑: **STM32** -> **USART** -> **TTL 模組 (cp2102)** -> **USB** -> **運行 ROS 的上位機** :::success 如果你拿 STM 官方開發版,必須從 USART 口開始接杜邦線。 訊號傳輸被拉長且沒屏蔽,通訊品質會蠻容易被拉低的。 **建議把 USART 走線、cp2102 晶片、type C 端子座都 lay 在 PCB 上。** 只需要拿 USB 線接 STM32 和運行 ROS 的上位機就好。 ::: </center> </br> <h3><font color="magenza">1. STM端:修改 .ioc </font> </h3> 1. <font color="yellow">設定 USART 腳位。</font> 可以設定你自己想要的 USART 腳位編號。 :::spoiler pic  ::: * <font color = "greenish">Mode:</font>Asynchronous。 * <font color = "greenish">DMA Settings:</font>Add USART3_TX 和 USART3_RX,將 priority 設為 High。 * <font color = "greenish">NVIC Settings:</font>勾選 USART3 global interrupt。 * <font color = "greenish">Parameter settings:</font>115200 Bits/s (建議)。 2. <font color = "yellow">System Core -> DMA -> USART1_RX -> Circular。</font> :::spoiler pic  ::: 3. <font color = "yellow">System Core -> RCC -> HSE 選「Crystal/Ceramic Resonator」。 勾選「RCC Global Interrupt」。</font> 沒有外部晶振的話,才選「BYPASS Clock Source」。 :::spoiler pic  ::: 4. <font color = "yellow">時鐘樹中設定 input frequency 8Mhz 即可。</font> :::spoiler pic  ::: </br> <h3><font color="magenza">2. STM端:安裝 rosserial 包</font> </h3> > 以下一樣搭配 [Github](https://github.com/pomelo925/TTennis-Pickup-Robot/tree/main/2.Firmware/Core-STM32H7) 服用。 > 注意以下步驟是在有 C/C++ 混編架構後進行的。 1. <font color="yellow">下載 [雲端]( https://drive.google.com/drive/folders/1j_iB8L6iR58N32y-ehP-o2JRwnhKRFhs) 上的 Inc 資料夾,記得解壓縮。</font> :::spoiler 這檔案怎麼來的 ? 參考影片:[Interfacing STM32 boards to ROS using rosserial protocol](https://www.youtube.com/watch?v=cq0HmKrIOt8&t=680s&ab_channel=LearnembeddedsystemswithGeorge) ::: 2. <font color="yellow">複製 Inc 資料夾,進到 `~/[STM Project]/Core` 後直接貼上。</font> (它會自動合併,不用擔心覆蓋原檔案。) :::spoiler pic  ::: 3. <font color="yellow">回 STM32 CubeIDE,Refresh `/Inc`。</font> :::spoiler pic  ::: 4. <font color="yellow">修改 `Core/Inc/STM32HardWare.h`。</font> 如下圖三粉色處。  5. <font color="yellow">程式碼。</font> a. 新增 [Github](https://github.com/pomelo925/TTennis-Pickup-Robot/tree/main/2.Firmware/Core-STM32H7/ROS1) 中的 `ROS1` Source Folder。 b. 並在 `mainpp.cpp` 中引用 ROS1 函式庫。 c. 修改代碼直至編譯成功。 </br> <h3><font color="magenza">3. ROS端:安裝 rosserial 包</font> </h3> 1. <font color="yellow">偷懶的話可以直接複製 [Github裡的兩個檔案](https://github.com/pomelo925/TTennis-Pickup-Robot/tree/main/3.Software/E.Integration/comm-ws/src/firmware_commun/src)。</font> :::spoiler 我不懶 1. 下載 package ```= sudo apt-get install ros-noetic-rosserial ros-noetic-rosserial-arduino ``` 2. 在 ${catkin_ws}/src 資料夾中 git clone rosserial 的資源包。 ```python= cd ${catkin_ws}/src git clone https://github.com/ros-drivers/rosserial.git --branch noetic-devel ``` 3. 將 `rosserial`、`rosserial_python`、`rosserial_msgs`(圖片忘記移)移至 `${catkin_ws}/src`。左圖移動前,右圖移動後。  $\;\;\;$=>  4. 修改 `rosserial_server/include/session.h`。 ```cpp= uint8_t overhead_bytes = 8; uint16_t length = overhead_bytes; BufferPtr buffer_ptr(new Buffer(length)); ros::serialization::OStream stream(&buffer_ptr->at(0), buffer_ptr->size()); stream << (uint16_t)0xfeff << (uint16_t)0x0000 << (uint16_t)0x0bff << (uint16_t)0xf400 ; boost::asio::async_write(socket_, boost::asio::buffer(*buffer_ptr), boost::bind(&Session::write_completion_cb, this, boost::asio::placeholders::error, buffer_ptr)); ```  ● `rosserial_python`(.py) 或 `rosserial_server`(.cpp) 都可以通訊。 ● 個人 prefer `.cpp`,所以不會用到 `rosserial_python`。 ::: 2. <font color="yellow">新增 `rosserial_server/launch/stm32.launch`。 其中 `publishers` 和 `subscribers` 的主詞是 STM32。</font> 檔案一樣在 [Github](https://github.com/pomelo925/TTennis-Pickup-Robot/blob/main/3.Software/E.Integration/comm-ws/src/firmware_commun/src/rosserial_server/launch/stm32.launch)。 </br> <h3><font color="magenza">4. 測試通訊</font> </h3> 1. 確認 STM32 插著 USB 線到電腦。 2. 燒錄完成後,執行並開現場表達式,等待從 ROS 傳遞的變數。 :::spoiler Ubuntu USB 連接阜設定事項 1. 確認接口名稱:`dmesg | grep tty`。  如黃色框框所示,接口是 ttyUSB0。 2. 接著賦予端口權限:`sudo chmod 777 /dev/ttyUSB0`。 3. 確認 launch 檔案的端口參數。 ```xml= <launch> <!-- ... --> <param name="port" value="/dev/ttyUSB0"/> <param name="baud" value="115200" /> <!-- ... --> </launch> ``` ::: 3. 開啟 ROS 通訊節點。(通訊成功不會跳 Warning) ```s= roscore & roslaunch rosserial_server stm32.launch ``` 4. 在 ROS 終端 publish message,並看 STM32 現場表達式。 ```sh= rostopic pub /cmd_vel geometry_msgs/Twist "{linear: {x: 2.5, y: 2.0, z: 1.0}, angular: {x: 5.0, y: 3.0, z: 2.5}}" ```
×
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