鱼香ROS社区
    • 版块
    • 最新
    • 未解决
    • 已解决
    • 群组
    • 注册
    • 登录
    紧急通知:禁止一切关于政治&VPN翻墙等话题,发现相关帖子会立马删除封号
    提问前必看的发帖注意事项: 社区问答规则(小鱼个人)更新 | 高质量帖子发布指南

    micro_ros 与ros通讯的问题

    已定时 已固定 已锁定 已移动 已解决
    综合问题
    ros2通信 microros ros2 humble
    2
    2
    311
    正在加载更多帖子
    • 从旧到新
    • 从新到旧
    • 最多赞同
    回复
    • 在新帖中回复
    登录后回复
    此主题已被删除。只有拥有主题管理权限的用户可以查看。
    • 14477904841
      不惑
      最后由 1447790484 编辑

      软件版本:ros-humble+ubuntu22.04
      硬件:stm32f405rg
      按照官方教程将micro_ros移植到stm32后,运行micro_ros_agent产生如下报错:
      截图 2024-01-03 09-52-22.png
      换了个串口,又发生如下情况:
      截图 2024-01-03 10-20-01.png
      代码如下,
      freertos.c

      /* USER CODE BEGIN Header */
      /**
        ******************************************************************************
        * File Name          : freertos.c
        * Description        : Code for freertos applications
        ******************************************************************************
        * @attention
        *
        * Copyright (c) 2023 STMicroelectronics.
        * All rights reserved.
        *
        * This software is licensed under terms that can be found in the LICENSE file
        * in the root directory of this software component.
        * If no LICENSE file comes with this software, it is provided AS-IS.
        *
        ******************************************************************************
        */
      /* USER CODE END Header */
      
      /* Includes ------------------------------------------------------------------*/
      #include "FreeRTOS.h"
      #include "task.h"
      #include "main.h"
      #include "cmsis_os.h"
      
      /* Private includes ----------------------------------------------------------*/
      /* USER CODE BEGIN Includes */
      #include "usart.h"
      #include <rcl/rcl.h>
      #include <rcl/error_handling.h>
      #include <rclc/rclc.h>
      #include <rclc/executor.h>
      #include <uxr/client/transport.h>
      #include <rmw_microxrcedds_c/config.h>
      #include <rmw_microros/rmw_microros.h>
      
      #include <std_msgs/msg/int32.h>
      /* USER CODE END Includes */
      
      /* Private typedef -----------------------------------------------------------*/
      /* USER CODE BEGIN PTD */
      
      /* USER CODE END PTD */
      
      /* Private define ------------------------------------------------------------*/
      /* USER CODE BEGIN PD */
      #define RCCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){printf("Failed status on line %d: %d. Aborting.\n",__LINE__,(int)temp_rc);vTaskDelete(NULL);}}
      #define RCSOFTCHECK(fn) { rcl_ret_t temp_rc = fn; if((temp_rc != RCL_RET_OK)){printf("Failed status on line %d: %d. Continuing.\n",__LINE__,(int)temp_rc);}}
      
      /* USER CODE END PD */
      
      /* Private macro -------------------------------------------------------------*/
      /* USER CODE BEGIN PM */
      
      /* USER CODE END PM */
      
      /* Private variables ---------------------------------------------------------*/
      /* USER CODE BEGIN Variables */
      bool cubemx_transport_open(struct uxrCustomTransport * transport);
      bool cubemx_transport_close(struct uxrCustomTransport * transport);
      size_t cubemx_transport_write(struct uxrCustomTransport* transport, const uint8_t * buf, size_t len, uint8_t * err);
      size_t cubemx_transport_read(struct uxrCustomTransport* transport, uint8_t* buf, size_t len, int timeout, uint8_t* err);
      
      void * microros_allocate(size_t size, void * state);
      void microros_deallocate(void * pointer, void * state);
      void * microros_reallocate(void * pointer, size_t size, void * state);
      void * microros_zero_allocate(size_t number_of_elements, size_t size_of_element, void * state);
      /* USER CODE END Variables */
      /* Definitions for defaultTask */
      osThreadId_t defaultTaskHandle;
      const osThreadAttr_t defaultTask_attributes = {
        .name = "defaultTask",
        .stack_size = 3000 * 4,
        .priority = (osPriority_t) osPriorityNormal,
      };
      
      /* Private function prototypes -----------------------------------------------*/
      /* USER CODE BEGIN FunctionPrototypes */
      void subscription_callback(const void * msgin)
      {
        // Cast received message to used type
        const std_msgs__msg__Int32 * msg = (const std_msgs__msg__Int32 *)msgin;
        // Process message
      
      }
      /* USER CODE END FunctionPrototypes */
      
      void StartDefaultTask(void *argument);
      
      void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */
      
      /**
        * @brief  FreeRTOS initialization
        * @param  None
        * @retval None
        */
      void MX_FREERTOS_Init(void) {
        /* USER CODE BEGIN Init */
      
        /* USER CODE END Init */
      
        /* USER CODE BEGIN RTOS_MUTEX */
        /* add mutexes, ... */
        /* USER CODE END RTOS_MUTEX */
      
        /* USER CODE BEGIN RTOS_SEMAPHORES */
        /* add semaphores, ... */
        /* USER CODE END RTOS_SEMAPHORES */
      
        /* USER CODE BEGIN RTOS_TIMERS */
        /* start timers, add new ones, ... */
        /* USER CODE END RTOS_TIMERS */
      
        /* USER CODE BEGIN RTOS_QUEUES */
        /* add queues, ... */
        /* USER CODE END RTOS_QUEUES */
      
        /* Create the thread(s) */
        /* creation of defaultTask */
        defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);
      
        /* USER CODE BEGIN RTOS_THREADS */
        /* add threads, ... */
        /* USER CODE END RTOS_THREADS */
      
        /* USER CODE BEGIN RTOS_EVENTS */
        /* add events, ... */
        /* USER CODE END RTOS_EVENTS */
      
      }
      
      /* USER CODE BEGIN Header_StartDefaultTask */
      /**
        * @brief  Function implementing the defaultTask thread.
        * @param  argument: Not used
        * @retval None
        */
      /* USER CODE END Header_StartDefaultTask */
      void StartDefaultTask(void *argument)
      {
        /* USER CODE BEGIN StartDefaultTask */
      
      	  // micro-ROS configuration
      
        rmw_uros_set_custom_transport(
      	true,
      	(void *) &huart1,
      	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;
        rcl_subscription_t hx_subscriber;
        rclc_executor_t hx_executor;
        std_msgs__msg__Int32 hxmsg;
      
        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;
        const char * subscriber_name = "hx_topic";
        rclc_subscription_init_default(
        		  &hx_subscriber, &node,
        		  ROSIDL_GET_MSG_TYPE_SUPPORT(std_msgs, msg, Int32), subscriber_name);
        RCCHECK(rclc_executor_init(&hx_executor, &support.context, 1, &allocator));
      
        RCCHECK(rclc_executor_add_subscription(&hx_executor, &hx_subscriber, &hxmsg, &subscription_callback, ON_NEW_DATA));
        /* Infinite loop */
        for(;;)
        {
      	rcl_ret_t ret = rcl_publish(&publisher, &msg, NULL);
      	if (ret != RCL_RET_OK)
      	{
      	  printf("Error publishing (line %d)\n", __LINE__);
      	}
      	rclc_executor_spin_some(&hx_executor, RCL_MS_TO_NS(100));
      	msg.data++;
      	osDelay(10);
        }
        /* USER CODE END StartDefaultTask */
      }
      
      /* Private application code --------------------------------------------------*/
      /* USER CODE BEGIN Application */
      
      /* USER CODE END Application */
      
      

      main.c

      /* USER CODE BEGIN Header */
      /**
        ******************************************************************************
        * @file           : main.c
        * @brief          : Main program body
        ******************************************************************************
        * @attention
        *
        * Copyright (c) 2023 STMicroelectronics.
        * All rights reserved.
        *
        * This software is licensed under terms that can be found in the LICENSE file
        * in the root directory of this software component.
        * If no LICENSE file comes with this software, it is provided AS-IS.
        *
        ******************************************************************************
        */
      /* USER CODE END Header */
      /* Includes ------------------------------------------------------------------*/
      #include "main.h"
      #include "cmsis_os.h"
      #include "dma.h"
      #include "usart.h"
      #include "usb_otg.h"
      #include "gpio.h"
      
      /* Private includes ----------------------------------------------------------*/
      /* USER CODE BEGIN Includes */
      
      /* USER CODE END Includes */
      
      /* Private typedef -----------------------------------------------------------*/
      /* USER CODE BEGIN PTD */
      
      /* USER CODE END PTD */
      
      /* Private define ------------------------------------------------------------*/
      /* USER CODE BEGIN PD */
      
      /* USER CODE END PD */
      
      /* Private macro -------------------------------------------------------------*/
      /* USER CODE BEGIN PM */
      
      /* USER CODE END PM */
      
      /* Private variables ---------------------------------------------------------*/
      
      /* USER CODE BEGIN PV */
      
      /* USER CODE END PV */
      
      /* Private function prototypes -----------------------------------------------*/
      void SystemClock_Config(void);
      void MX_FREERTOS_Init(void);
      /* USER CODE BEGIN PFP */
      
      /* USER CODE END PFP */
      
      /* Private user code ---------------------------------------------------------*/
      /* USER CODE BEGIN 0 */
      
      /* USER CODE END 0 */
      
      /**
        * @brief  The application entry point.
        * @retval int
        */
      int main(void)
      {
        /* USER CODE BEGIN 1 */
      
        /* USER CODE END 1 */
      
        /* MCU Configuration--------------------------------------------------------*/
      
        /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
        HAL_Init();
      
        /* USER CODE BEGIN Init */
      
        /* USER CODE END Init */
      
        /* Configure the system clock */
        SystemClock_Config();
      
        /* USER CODE BEGIN SysInit */
      
        /* USER CODE END SysInit */
      
        /* Initialize all configured peripherals */
        MX_GPIO_Init();
        MX_DMA_Init();
        MX_USB_OTG_FS_PCD_Init();
        MX_USART1_UART_Init();
        /* USER CODE BEGIN 2 */
      
        /* USER CODE END 2 */
      
        /* Init scheduler */
        osKernelInitialize();
      
        /* Call init function for freertos objects (in freertos.c) */
        MX_FREERTOS_Init();
      
        /* Start scheduler */
        osKernelStart();
      
        /* We should never get here as control is now taken by the scheduler */
        /* Infinite loop */
        /* USER CODE BEGIN WHILE */
        while (1)
        {
          /* USER CODE END WHILE */
      
          /* USER CODE BEGIN 3 */
        }
        /* USER CODE END 3 */
      }
      
      /**
        * @brief System Clock Configuration
        * @retval None
        */
      void SystemClock_Config(void)
      {
        RCC_OscInitTypeDef RCC_OscInitStruct = {0};
        RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
      
        /** Configure the main internal regulator output voltage
        */
        __HAL_RCC_PWR_CLK_ENABLE();
        __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
      
        /** Initializes the RCC Oscillators according to the specified parameters
        * in the RCC_OscInitTypeDef structure.
        */
        RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
        RCC_OscInitStruct.HSEState = RCC_HSE_ON;
        RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
        RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
        RCC_OscInitStruct.PLL.PLLM = 8;
        RCC_OscInitStruct.PLL.PLLN = 336;
        RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
        RCC_OscInitStruct.PLL.PLLQ = 7;
        if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
        {
          Error_Handler();
        }
      
        /** Initializes the CPU, AHB and APB buses clocks
        */
        RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                                    |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
        RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
        RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV2;
        RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
        RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
      
        if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
        {
          Error_Handler();
        }
      }
      
      /* USER CODE BEGIN 4 */
      
      /* USER CODE END 4 */
      
      /**
        * @brief  Period elapsed callback in non blocking mode
        * @note   This function is called  when TIM14 interrupt took place, inside
        * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
        * a global variable "uwTick" used as application time base.
        * @param  htim : TIM handle
        * @retval None
        */
      void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
      {
        /* USER CODE BEGIN Callback 0 */
      
        /* USER CODE END Callback 0 */
        if (htim->Instance == TIM14) {
          HAL_IncTick();
        }
        /* USER CODE BEGIN Callback 1 */
      
        /* USER CODE END Callback 1 */
      }
      
      /**
        * @brief  This function is executed in case of error occurrence.
        * @retval None
        */
      void Error_Handler(void)
      {
        /* USER CODE BEGIN Error_Handler_Debug */
        /* User can add his own implementation to report the HAL error return state */
        __disable_irq();
        while (1)
        {
        }
        /* USER CODE END Error_Handler_Debug */
      }
      
      #ifdef  USE_FULL_ASSERT
      /**
        * @brief  Reports the name of the source file and the source line number
        *         where the assert_param error has occurred.
        * @param  file: pointer to the source file name
        * @param  line: assert_param error line source number
        * @retval None
        */
      void assert_failed(uint8_t *file, uint32_t line)
      {
        /* USER CODE BEGIN 6 */
        /* User can add his own implementation to report the file name and line number,
           ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
        /* USER CODE END 6 */
      }
      #endif /* USE_FULL_ASSERT */
      
      
      小鱼小 1 条回复 最后回复 回复 引用 0
      • 小鱼小
        小鱼 技术大佬 @1447790484
        最后由 编辑

        @1447790484 在 micro_ros 与ros通讯的问题 中说:

        while (1)
        {
        /* USER CODE END WHILE */

        /* USER CODE BEGIN 3 */
        

        }

        这里死循环,会不会影响调度,价格 task delay ?


        暂时关闭,还有问题再打开

        新书配套视频:https://www.bilibili.com/video/BV1GW42197Ck/

        1 条回复 最后回复 回复 引用 0
        • 小鱼小 小鱼 将这个主题标记为已解决,在
        • 第一个帖子
          最后一个帖子
        皖ICP备16016415号-7
        Powered by NodeBB | 鱼香ROS