welcom ! Handel home

2025年4月14日 星期一

micro-ROS(ESP32) ↔ ROS 2(主機) 透過 UART 的通訊測試流程

 

下是完整的 micro-ROS(ESP32) ↔ ROS 2(主機) 透過 UART 的通訊測試流程,包含硬體接法、程式架構、Agent 啟動與 topic 測試,幫助你確認 UART 通訊是否正常建立。


🎯 測試目標

ESP32(透過 UART)使用 micro-ROS client 發布 ROS 訊息 →
ROS 2 主機接收這些訊息並使用 ros2 topic echo 印出內容


🛠️ 硬體準備

裝置功能備註
ESP32micro-ROS clientUSB/UART 與主機連接
Ubuntu PC / PiROS 2 + micro-ROS Agent可為主機或容器執行環境
UART 線ESP32 <-> HostRX ↔ TX, TX ↔ RX, GND 接一起

🧱 整體架構圖

plaintext
[ESP32] FreeRTOS + micro-ROS client Publish: /esp32_pub (Int32) ⇅ UART (115200) [PC Host or Docker] micro-ROS Agent (serial mode) ↳ ROS 2 Topic 接收

🔧 步驟 1:ESP32 韌體製作

修改 firmware/apps/app.c 發布 topic:

c
#include <rcl/rcl.h> #include <rclc/rclc.h> #include <std_msgs/msg/int32.h> #include <stdio.h> #include <unistd.h> rcl_publisher_t publisher; std_msgs__msg__Int32 msg; rcl_timer_t timer; void timer_callback(rcl_timer_t *timer, int64_t last_call_time) { (void)last_call_time; if (timer != NULL) { msg.data++; rcl_publish(&publisher, &msg, NULL); } } void appMain(void *arg) { rcl_allocator_t allocator = rcl_get_default_allocator(); rclc_support_t support; rclc_support_init(&support, 0, NULL, &allocator); rcl_node_t node; rclc_node_init_default(&node, "esp32_node", "", &support); rclc_publisher_init_default( &publisher, &node, ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32), "esp32_pub"); rclc_timer_init_default( &timer, &support, RCL_MS_TO_NS(1000), timer_callback); rclc_executor_t executor; rclc_executor_init(&executor, &support.context, 1, &allocator); rclc_executor_add_timer(&executor, &timer); msg.data = 0; while (1) { rclc_executor_spin_some(&executor, RCL_MS_TO_NS(100)); usleep(10000); } }

設定 UART 傳輸方式:

bash
ros2 run micro_ros_setup configure_firmware.sh serial --dev /dev/ttyUSB0

編譯 + 燒錄:

bash
ros2 run micro_ros_setup build_firmware.sh ros2 run micro_ros_setup flash_firmware.sh

🖥️ 步驟 2:主機端啟動 micro-ROS Agent

在 Host 或容器內執行以下指令:

bash
ros2 run micro_ros_agent micro_ros_agent serial --dev /dev/ttyUSB0 -v6
  • 若有成功連線,會看到:

    pgsql
    [INFO] Connection successful [INFO] Received message from client

🧪 步驟 3:驗證 ROS 2 topic

打開另一個終端機:

bash
ros2 topic list # 應會出現:/esp32_pub ros2 topic echo /esp32_pub # 顯示: # data: 1 # data: 2 # ...

✅ 補充小技巧

項目指令或備註
查看連接埠ls /dev/ttyUSB*
改 UART 波特率configure_firmware.sh 內可修改
Agent log 更多細節加上 -v6--debug

✅ 成功驗證代表你已建立:

  • ESP32 ➜ UART ➜ micro-ROS Agent ➜ DDS ➜ ROS2 topic 完整鏈路!

==============================================================

以下是完整的 ESP32 micro-ROS Subscriber 範例,可以讓 ESP32 接收來自 ROS 2 主機端送出的 topic 資料,並在 ESP32 上執行對應動作(例如改變 LED 狀態、數值顯示、控制馬達等)。


🎯 目標

ESP32 透過 UART 接收 ROS 2 主機傳送的 topic 資料(std_msgs/msg/Int32),並透過 callback 函式做處理,例如顯示收到的數值。


📁 修改 firmware/apps/app.c

✅ app.c(ESP32 micro-ROS Subscriber)

c
#include <rcl/rcl.h> #include <rclc/rclc.h> #include <std_msgs/msg/int32.h> #include <stdio.h> #include <unistd.h> rcl_subscription_t subscriber; std_msgs__msg__Int32 received_msg; void subscription_callback(const void * msgin) { const std_msgs__msg__Int32 * msg = (const std_msgs__msg__Int32 *)msgin; printf("Received value from ROS 2: %d\n", msg->data); } void appMain(void *arg) { rcl_allocator_t allocator = rcl_get_default_allocator(); rclc_support_t support; rclc_support_init(&support, 0, NULL, &allocator); rcl_node_t node; rclc_node_init_default(&node, "esp32_sub_node", "", &support); rclc_subscription_init_default( &subscriber, &node, ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32), "esp32_sub" ); rclc_executor_t executor; rclc_executor_init(&executor, &support.context, 1, &allocator); rclc_executor_add_subscription(&executor, &subscriber, &received_msg, &subscription_callback, ON_NEW_DATA); while (1) { rclc_executor_spin_some(&executor, RCL_MS_TO_NS(100)); usleep(10000); } }

🔧 編譯 + 燒錄

bash
ros2 run micro_ros_setup build_firmware.sh ros2 run micro_ros_setup flash_firmware.sh

🖥️ ROS 2 主機端發送測試指令

在主機端執行以下指令傳送數值到 ESP32:

bash
ros2 topic pub /esp32_sub std_msgs/msg/Int32 "{data: 42}"
  • ESP32 端會列印:

    csharp
    Received value from ROS 2: 42

🧪 可以進一步做的應用

收到數值ESP32 行為範例
1開啟 LED
0關閉 LED
其他數值控制馬達、顯示器、蜂鳴器等

✅ 測試完成條件

你看到:

  • Agent 顯示 ESP32 連線成功

  • ESP32 成功列印收到的 ROS topic 資料

代表「Subscriber from ROS2 ➜ ESP32」通訊鏈路運作正常!



沒有留言: