小鱼 ROS 2 新书上线!点击链接查看, 新书配套视频点击链接查看。
提问前必看的发帖注意事项—— 提问前必看!不符合要求的问题拒绝回答!!
社区使用指南—如何添加标签修改密码
microros通信问题
-
ros版本:foxy
库:根据网上教程自己生成的foxy版本的库链接文本,所以应该不是版本问题。
硬件:STM32F407ZGT6
程序:```
/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"
#include "usart.h"
#include "microros_tasks.h"
#include "microros_allocators.h"
#include "STM_custom_transport.h"
#include "std_msgs/msg/int32.h"uint8_t ch = 'c';
int i=0;/* Private includes ----------------------------------------------------------/
/ USER CODE BEGIN Includes *//* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------/
typedef StaticTask_t osStaticThreadDef_t;
/ 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 Variables *//* USER CODE END Variables /
/ Definitions for defaultTask /
osThreadId_t defaultTaskHandle;
const osThreadAttr_t defaultTask_attributes = {
.name = "defaultTask",
.stack_size = 128 * 4,
.priority = (osPriority_t) osPriorityNormal,
};
/ Definitions for LED1 /
osThreadId_t LED1Handle;
const osThreadAttr_t LED1_attributes = {
.name = "LED1",
.stack_size = 128 * 4,
.priority = (osPriority_t) osPriorityAboveNormal,
};
/ Definitions for Microros */
osThreadId_t MicrorosHandle;
uint32_t MicrorosBuffer[ 3000 ];
osStaticThreadDef_t MicrorosControlBlock;
const osThreadAttr_t Microros_attributes = {
.name = "Microros",
.cb_mem = &MicrorosControlBlock,
.cb_size = sizeof(MicrorosControlBlock),
.stack_mem = &MicrorosBuffer[0],
.stack_size = sizeof(MicrorosBuffer),
.priority = (osPriority_t) osPriorityHigh,
};/* Private function prototypes -----------------------------------------------/
/ USER CODE BEGIN FunctionPrototypes *//* USER CODE END FunctionPrototypes */
void StartDefaultTask(void *argument);
void LED1_Task(void *argument);
void Microros_Task(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) {
defaultTaskHandle = osThreadNew(StartDefaultTask, NULL, &defaultTask_attributes);
/* creation of LED1 /
LED1Handle = osThreadNew(LED1_Task, NULL, &LED1_attributes);
// / creation of Microros */
MicrorosHandle = osThreadNew(Microros_Task, NULL, &Microros_attributes);
}/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void argument)
{
while(1){
HAL_GPIO_TogglePin(LED_2_GPIO_Port, LED_2_Pin);
osDelay(1000);
/ USER CODE END StartDefaultTask */
}
}/* USER CODE END Header_LED1_Task */
void LED1_Task(void *argument)
{/* USER CODE BEGIN LED1_Task /
/ Infinite loop /
while(1){
HAL_GPIO_TogglePin(LED_1_GPIO_Port, LED_1_Pin);
osDelay(1000);
/ USER CODE END LED1_Task */
}
}/* USER CODE END Header_Microros_Task */
void Microros_Task(void argument)
{
/ USER CODE BEGIN StartDefaultTask */
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;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);
}
/* USER CODE END StartDefaultTask */
}问题:当运行代理时可以检测到信息,但是运行命令无法查询到创建的话题,具体如下图 ![屏幕截图 2023-07-13 150702.png](/forum/assets/uploads/files/1689236095760-屏幕截图-2023-07-13-150702-resized.png) ![屏幕截图 2023-07-13 150650.png](/forum/assets/uploads/files/1689236103715-屏幕截图-2023-07-13-150650.png)
-
@2918095496
上面两张图好像有问题,补充一下 -
@2918095496 这个经过测试,找到了问题所在,在程序运行过程中,有几个语句的初始化出现了问题,导致了以上的问题出现,具体问题如下:
void StartDefaultTask(void *argument)
{
//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;allocator = rcl_get_default_allocator();
//create init_options
rcl_ret_t ret1 = rclc_support_init(&support, 0, NULL, &allocator);
// create node
rcl_ret_t ret2 = rclc_node_init_default(&node, "cubemx_node", "", &support);
// //create publisher
rclc_publisher_init_best_effort(
&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);
}
/* USER CODE END 5 */
}
在以上几个初始化的时候会报错,rclc_support_init()在这个初始化时有时,返回数字1报一般性错误,rclc_node_init_default()这个会因为上一个初始化报错,返回数字101,上下文错误,还有就是 rcl_publish()会返回数字300,RCL_RET_PUBLISHER_INVALID指返回rcl_publisher_t无效。
目前问题还没有解决,希望能给些指导,谢谢。 -
@2918095496 请问解决了吗?