(三)、STM32代码中加入Micro-ROS支持

1、下载 micro_ros_stm32cubemx_utils 到我们生成的代码文件夹中

2、输入如下命名,git代码到本地文件夹 

git clone https://github.com/micro-ROS/micro_ros_stm32cubemx_utils.git

 

进入micro-ros文件夹中 

cd micro_ros_stm32cubemx_utils/

 切换ros版本至humble

git checkout humble

然后查看当前版本是否为humble

git status

返回上一级目录 

cd ..

安装 dos2unix,将 makefile 文件从DOS或Windows格式转换为Unix格式(视情况而定,为了保险起见,可以转换一下)

sudo apt install dos2unix
dos2unix Makefile

编辑Makefile文件,在如图位置添加如下配置代码(配置文件告诉编译器在哪里找到 micro-ROS 的头文件,还自定义了一个DMA的传输文件)

#######################################
# micro-ROS addons
#######################################
LDFLAGS += micro_ros_stm32cubemx_utils/microros_static_library/libmicroros/libmicroros.a
C_INCLUDES += -Imicro_ros_stm32cubemx_utils/microros_static_library/libmicroros/microros_include

# Add micro-ROS utils
C_SOURCES += micro_ros_stm32cubemx_utils/extra_sources/custom_memory_manager.c
C_SOURCES += micro_ros_stm32cubemx_utils/extra_sources/microros_allocators.c
C_SOURCES += micro_ros_stm32cubemx_utils/extra_sources/microros_time.c

# Set here the custom transport implementation
C_SOURCES += micro_ros_stm32cubemx_utils/extra_sources/microros_transports/dma_transport.c

print_cflags:
   @echo $(CFLAGS)

如图所示

从 Docker Hub 上拉取名为 microros/micro_ros_static_library_builder 的镜像,并指定标签为 humble。 安装之前需要安装docker

  设置 Docker 的apt存储库

# Add Docker's official GPG key:
sudo apt-get update
sudo apt-get install ca-certificates curl
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
sudo chmod a+r /etc/apt/keyrings/docker.asc

# Add the repository to Apt sources:
echo \
  "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] https://download.docker.com/linux/ubuntu \
  $(. /etc/os-release && echo "$VERSION_CODENAME") stable" | \
  sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update

安装 Docker 软件包 

sudo apt-get install docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin

拉取镜像

sudo docker pull microros/micro_ros_static_library_builder:humble

运行docker(此过程可能因为网络原因报错,有时候需要多次执行,我这边经过多次执行后成功

sudo docker run -it --rm -v $(pwd):/project --env MICROROS_LIBRARY_FOLDER=micro_ros_stm32cubemx_utils/microros_static_library microros/micro_ros_static_library_builder:humble

如图所示编译过程中询问直接填y,最后会报一个什么包没找到,实际测试无须理会即可。 

打开 micro_ros_stm32cubemx_utils 目录使用文本编辑命令打开 sample_main.c 文件

cd micro_ros_stm32cubemx_utils/
gedit sample_main.c

复制 sample_main.c中的头文件到我们的工程可以使用CubeIDE直接打开或者其他文本编辑器,我这边直接使用了CubeIDE

#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>

复制到如下位置 freertos.c

 复制如下代码

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);

复制到freertos.c位置

 复制如下代码到freertos.c文件中

  rmw_uros_set_custom_transport(
    true,
    (void *) &huart3,
    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);
  }

如图所示覆盖原来的delay文件

在freertos.c中添加 对 bool和串口 支持的头文件 

#include <stdbool.h>
#include "usart.h"

 

修改串口编号(根据自己的配置修改我们使用的是串口1)

 保存并退出CubeIDE

在主工程目录开始编译报错

gedit Makefile

根据提示,将188行的前面删除  使用TAB键进行缩进

再次编译

make -j8

 发现缺少编译器 arm-none-eabi-gcc

我们安装

sudo apt install gcc-arm-none-eabi

继续编译没有报错了,看到生成了hex和bin文件了,我们可以烧录到我们的单片机中了

这里我们可以通过Jflash(这个是Jlink)或者ST-Link Utility(这个需要使用ST-link),可以根据自己的需求进行下载

  • 26
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值