Kaynağa Gözat

fix: rcl_publish odom_publisher fail. (#30)

Lv XinLiang 1 yıl önce
ebeveyn
işleme
b3f23c979e

+ 18 - 8
docs/humble/chapt16/15.采用MicroROS发布里程计.md

@@ -159,14 +159,24 @@ void microros_task(void *param)
    // 等待 2 秒,以便网络连接得到建立。
    delay(2000);
 
-+  rclc_publisher_init_best_effort(
-+      &odom_publisher,
-+      &node,
-+      ROSIDL_GET_MSG_TYPE_SUPPORT(nav_msgs, msg, Odometry),
-+      "odom");
-   // 设置 micro-ROS 执行器,并将订阅添加到其中。
-   rclc_executor_init(&executor, &support.context, 1, &allocator);
-   rclc_executor_add_subscription(&executor, &subscriber, &sub_msg, &twist_callback, ON_NEW_DATA);
+   // 设置 micro-ROS 支持结构、节点和订阅。
+   allocator = rcl_get_default_allocator();
+   rclc_support_init(&support, 0, NULL, &allocator);
+   rclc_node_init_default(&node, "esp32_car", "", &support);
+   rclc_subscription_init_default(
+       &subscriber,
+       &node,
+       ROSIDL_GET_MSG_TYPE_SUPPORT(geometry_msgs, msg, Twist),
+       "/cmd_vel");
+
++    rclc_publisher_init_best_effort(
++        &odom_publisher,
++        &node,
++        ROSIDL_GET_MSG_TYPE_SUPPORT(nav_msgs, msg, Odometry),
++        "odom");
+     // 设置 micro-ROS 执行器,并将订阅添加到其中。
+     rclc_executor_init(&executor, &support.context, 1, &allocator);
+     rclc_executor_add_subscription(&executor, &subscriber, &sub_msg, &twist_callback, ON_NEW_DATA);
 
       // 循环运行 micro-ROS 执行器以处理传入的消息。
       while (true)