Ros1 Noetic(本地)和Ros2 Humble(docker)之间相互通信及设置初始位姿

一、安装Ros1_bridge

参考文章:https://zhuanlan.zhihu.com/p/1913928957679632486

本地环境

  • 主机Ubuntu20.04 + Ros1 Noetic
  • Docker (Ubuntu22.04 + Ros2 Humble)

容器内安装ros1_bridge

注意启动容器的时候设置为--network=host

  1. 容器内(ubuntu安装ros noetic)

    复制代码
    # Add source
    echo "deb [trusted=yes arch=amd64] http://deb.repo.autolabor.com.cn jammy main" | sudo tee /etc/apt/sources.list.d/autolabor.list 
    # Update
    sudo apt update
    # Install
    sudo apt install ros-noetic-autolabor
    
    
    # 验证
    source /opt/ros/noetic/setup.sh
    roscore
  2. git code

    复制代码
    mkdir -p ros_bridge_ws/src && cd ros_bridge_ws/src
    git clone https://github.com/Eku127/ros1_bridge.git
  3. build

    复制代码
    # in the workspace, NO source the setup of ROS1
    # on the ros_bridge_ws do compiling
    cd ..
    colcon build --symlink-install --packages-skip ros1_bridge
    # setup ros1
    source /opt/ros/noetic/setup.sh
    # setup ros2
    source /opt/ros/humble/setup.sh
    # compiling
    colcon build --symlink-install --packages-select ros1_bridge --cmake-force-configure --event-handlers console_direct+
  4. test,执行下面命令能够弹出支持的转换消息类型

    复制代码
    ros2 run ros1_bridge dynamic_bridge --print-pairs

使用

注意:在容器内下载ros-noetic-autolabor后,会自动在容器内的~/.bashrc文件内添加source /opt/ros/noetic/setup.bash,请注释该行,否则会影响容器内ros2相关功能包的正常使用。然后Ros1_bridge是在容器内使用,本地Ros1发布话题容器内可以接受并进行转换。

启动ros1_bridge依赖roscore,启动该节点前可以在本地执行roscore,在容器内执行:

复制代码
# setup ros1
source /opt/ros/noetic/setup.sh
# setup ros2
source /opt/ros/humble/setup.sh

# work_ws
source ./install/setup.bash  

ros2 run ros1_bridge dynamic_bridge

遇到的问题:
1.使用dynamic_bridge,不会自动转换topic话题,但是可以自动转换server相关的话题。

这里暂时不清楚什么原因,这里只使用位姿转换的话题,涉及少量的话题,可以改进simple_bridge_1_to_2.cpp文件,订阅ros1话题然后转化为ros2话题并进行发布,示例程序如下,注意CMakeLists文件要添加相关依赖。

复制代码
#include <iostream>
#include <memory>
#include <utility>
#include <csignal>

// include ROS 1
#ifdef __clang__
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wunused-parameter"
#endif
#include "ros/ros.h"
#include "std_msgs/String.h"
#include "geometry_msgs/PoseWithCovarianceStamped.h"
#ifdef __clang__
# pragma clang diagnostic pop
#endif

// include ROS 2
#include "rclcpp/rclcpp.hpp"
#include "std_msgs/msg/string.hpp"
#include "geometry_msgs/msg/pose_with_covariance_stamped.hpp"

// Publisher 全局变量
rclcpp::Publisher<geometry_msgs::msg::PoseWithCovarianceStamped>::SharedPtr pub;
std::shared_ptr<rclcpp::Node> node_ptr;

void chatterCallback(const geometry_msgs::PoseWithCovarianceStamped::ConstPtr & ros1_msg)
{
  std::cout << "I heard pose at: [" 
            << ros1_msg->pose.pose.position.x << ", " 
            << ros1_msg->pose.pose.position.y << ", "
            << ros1_msg->pose.pose.position.z << "]" << std::endl;
  
  auto ros2_msg = geometry_msgs::msg::PoseWithCovarianceStamped();
  
  // 复制消息内容
  ros2_msg.header.stamp.sec = ros1_msg->header.stamp.sec;
  ros2_msg.header.stamp.nanosec = ros1_msg->header.stamp.nsec;
  ros2_msg.header.frame_id = ros1_msg->header.frame_id;
  
  ros2_msg.pose.pose.position.x = ros1_msg->pose.pose.position.x;
  ros2_msg.pose.pose.position.y = ros1_msg->pose.pose.position.y;
  ros2_msg.pose.pose.position.z = ros1_msg->pose.pose.position.z;
  
  ros2_msg.pose.pose.orientation.x = ros1_msg->pose.pose.orientation.x;
  ros2_msg.pose.pose.orientation.y = ros1_msg->pose.pose.orientation.y;
  ros2_msg.pose.pose.orientation.z = ros1_msg->pose.pose.orientation.z;
  ros2_msg.pose.pose.orientation.w = ros1_msg->pose.pose.orientation.w;
  
  for (size_t i = 0; i < 36; ++i) {
    ros2_msg.pose.covariance[i] = ros1_msg->pose.covariance[i];
  }
  
  std::cout << "Passing along pose to ROS2 and shutting down node" << std::endl;
  pub->publish(ros2_msg);

  // 关闭 ROS1 和 ROS2
  ros::shutdown();
  rclcpp::shutdown();
}

int main(int argc, char * argv[])
{
  // 初始化 ROS 2
  rclcpp::init(argc, argv);
  node_ptr = rclcpp::Node::make_shared("talker");
  
  pub = node_ptr->create_publisher<geometry_msgs::msg::PoseWithCovarianceStamped>("initialpose", 10);
  
  // 初始化 ROS 1
  ros::init(argc, argv, "listener");
  ros::NodeHandle n;
  ros::Subscriber sub = n.subscribe("initialpose", 10, chatterCallback);
  
  // ROS1 spin 阻塞,收到消息后回调将会关闭节点
  ros::spin();
  
  return 0;
}

然后执行:

复制代码
ros2 run ros1_bridge simple_bridge_1_to_2

不过上面还存在一定BUG,暂时不清楚原因:先启动该节点,然后再发布话题,该节点接收不到话题。但是先在本地发布话题,容器内再启动该节点,就能够实现正常功能。

相关推荐
峰顶听歌的鲸鱼5 小时前
Kubernetes介绍和部署
运维·笔记·云原生·容器·kubernetes·学习方法
Ka1Yan6 小时前
Docker:基本概念与快速入门
运维·docker·容器
糠帅傅蓝烧牛肉面9 小时前
单实例多MCP聚合服务:两种实现方案深度对比
前端·docker·ai
汪碧康11 小时前
一文掌握k8s的升级更新策略
云原生·容器·kubernetes·k8s·亲和性·xkube
杨浦老苏12 小时前
离线优先的自托管笔记应用Anchor
笔记·docker·群晖
zcz160712782113 小时前
docker部署 WVP-Pro
容器
AC赳赳老秦15 小时前
Kubernetes 与 DeepSeek:高效 Pod 部署配置与资源调度优化指南
人工智能·云原生·容器·kubernetes·自动化·notepad++·deepseek
阿方索16 小时前
Kubernetes Pod 管理
云原生·容器·kubernetes
哪里不会点哪里.16 小时前
Docker
运维·docker·容器