一、安装Ros1_bridge
参考文章:https://zhuanlan.zhihu.com/p/1913928957679632486
本地环境
- 主机Ubuntu20.04 + Ros1 Noetic
- Docker (Ubuntu22.04 + Ros2 Humble)
容器内安装ros1_bridge
注意启动容器的时候设置为--network=host
-
容器内(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 -
git code
mkdir -p ros_bridge_ws/src && cd ros_bridge_ws/src git clone https://github.com/Eku127/ros1_bridge.git -
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+ -
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,暂时不清楚原因:先启动该节点,然后再发布话题,该节点接收不到话题。但是先在本地发布话题,容器内再启动该节点,就能够实现正常功能。