# ROS 桥接

## 简介

DepthAI ROS 桥接（depthai_bridge 包）是一个 ROS 包，允许使用 C++ DepthAI API 和 ROS 消息转换器以更可控的方式创建管道。

## 可用的转换器

| **转换器** | **描述** |
| --- | --- |
| 视差转换器 | 转换立体视差消息 |
| 图像转换器 | 用于将各种类型的图像消息与 DAI/OpenCV 格式相互转换 |
| 图像检测转换器 | 转换 2D 检测结果 |
| IMU 转换器 | 转换 IMU 数据，可选择插值 IMU 读数 |
| 空间检测转换器 | 转换 3D 空间检测结果 |
| 跟踪特征转换器 | 转换跟踪特征 |
| 跟踪检测转换器 | 转换 DAI 轨迹片段 |
| 变换发布器 | 用于转换 DAI 校准数据并以 ROS 变换数据形式发布 |

## 用法

您可以参考 depthai_examples 包，了解如何设置管道并使用 BridgePublisher 类和 ImageConverters 以 ROS 格式发布数据。 以下是一个简单的 RGB 发布示例：

```cpp
#include <cstdio>

#include "depthai/device/Device.hpp"
#include "depthai/pipeline/Pipeline.hpp"
#include "depthai/pipeline/node/Camera.hpp"
#include "depthai_bridge/BridgePublisher.hpp"
#include "depthai_bridge/ImageConverter.hpp"
#include "depthai_bridge/TFPublisher.hpp"
#include "depthai_bridge/depthaiUtility.hpp"
#include "rclcpp/node.hpp"

int main(int argc, char** argv) {
    int width = 1280;
    int height = 720;
    std::string tfPrefix = "oak";
    rclcpp::init(argc, argv);
    auto node = rclcpp::Node::make_shared("rgb_publisher");

    auto device = std::make_shared<dai::Device>();
    dai::Pipeline pipeline(device);

    // Define sources and outputs 定义源和输出
    auto rgbCamera = pipeline.create<dai::node::Camera>()->build(dai::CameraBoardSocket::CAM_A);

    // Create output queue 创建输出队列
    auto rgbOutputQueue = rgbCamera->requestOutput({width, height})->createOutputQueue(8, false);

    pipeline.start();

    // Create a bridge publisher for RGB images 为RGB图像创建桥接发布器
    auto rgbConverter = std::make_shared<depthai_bridge::ImageConverter>(
        depthai_bridge::getOpticalFrameName(tfPrefix, depthai_bridge::getSocketName(dai::CameraBoardSocket::CAM_A, device->getDeviceName())), false);

    auto calibrationHandler = device->readCalibration();
    auto tfPub =
        std::make_unique<depthai_bridge::TFPublisher>(node, calibrationHandler, device->getConnectedCameraFeatures(), tfPrefix, device->getDeviceName());
    auto rgbCameraInfo = rgbConverter->calibrationToCameraInfo(calibrationHandler, dai::CameraBoardSocket::CAM_A, width, height);

    auto rgbPub = std::make_unique<depthai_bridge::BridgePublisher<sensor_msgs::msg::Image, dai::ImgFrame>>(
        rgbOutputQueue,
        node,
        "rgb/image",
        [rgbConverter](std::shared_ptr<dai::ImgFrame> msg, std::deque<sensor_msgs::msg::Image>& rosMsgs) { rgbConverter->toRosMsg(msg, rosMsgs); },
        30,
        rgbCameraInfo,
        "rgb");

    rgbPub->addPublisherCallback();

    while(rclcpp::ok() && pipeline.isRunning()) {
        rclcpp::spin(node);
    }

    return 0;
}
```
