micro-ROS / micro_ros_stm32cubemx_utils

A set of utilities for integrating micro-ROS in a STM32CubeMX project
Apache License 2.0
171 stars 65 forks source link

rcl_publish() failed #118

Closed rusuanjun007 closed 1 year ago

rusuanjun007 commented 1 year ago

Hi,

I am new to micro-ros and I have a problem with rcl_publish(). rcl_publish() failed and I cannot receive data sent back to my PC.
Here is the detail of my setting.

Microcontroller board: Nucleo-f401re

PC: ROS humble STM32CubeIDE

Code is the example code:

void StartDefaultTask(void *argument)
{
     rmw_uros_set_custom_transport(
        true,
        (void *) &huart2,
        cubemx_transport_open,
        cubemx_transport_close,
        cubemx_transport_write,
        cubemx_transport_read);

      rcl_allocator_t freeRTOS_allocator = rcutils_get_zero_initialized_allocator();
      freeRTOS_allocator.allocate = microros_allocate;
      freeRTOS_allocator.deallocate = microros_deallocate;
      freeRTOS_allocator.reallocate = microros_reallocate;
      freeRTOS_allocator.zero_allocate =  microros_zero_allocate;

      if (!rcutils_set_default_allocator(&freeRTOS_allocator)) {
          printf("Error on default allocators (line %d)\n", __LINE__);
      }

      // micro-ROS app

      rcl_publisher_t publisher;
      std_msgs__msg__Int32 msg;
      rclc_support_t support;
      rcl_allocator_t allocator;
      rcl_node_t node;

      allocator = rcl_get_default_allocator();

      //create init_options
      rclc_support_init(&support, 0, NULL, &allocator);

      // create node
      rclc_node_init_default(&node, "cubemx_node", "", &support);

      // create publisher
      rclc_publisher_init_default(
        &publisher,
        &node,
        ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32),
        "cubemx_publisher");

      msg.data = 0;

      for(;;)
      {
        rcl_ret_t ret = rcl_publish(&publisher, &msg, NULL);
        if (ret != RCL_RET_OK)
        {
          printf("Error publishing (line %d)\n", __LINE__);
        }

        msg.data++;
        osDelay(10);
      }
}

Fault behavior:

Does anyone have any suggestions? Thanks for your great help!

pablogs9 commented 1 year ago
rusuanjun007 commented 1 year ago

Hi @pablogs9,

Thanks for your suggestion. I tried the following.

pablogs9 commented 1 year ago

Definitely, the problem is that the datawriter is not being created because rclc_publisher_init_default is failing.

This may be due to many causes, could you debug the publisher creation to check where it is failing?

My first thought is do you have enough heap (for mallocing memory) and stack configured for the FreeRTOS task?

rusuanjun007 commented 1 year ago

Hi @pablogs9,

It seems there is something wrong with the DMA setting. I changed the transport configuration from U(S)ART with DMA to U(S)ART with Interrupts, and it works. Appreciate your help!