|
| 1 | +#include "infinite_sense.h" |
| 2 | +#include "video_cam.h" |
| 3 | + |
| 4 | +#include "rclcpp/rclcpp.hpp" |
| 5 | + |
| 6 | +#include <image_transport/image_transport.hpp> |
| 7 | +#include <cv_bridge/cv_bridge.h> |
| 8 | + |
| 9 | +#include <sensor_msgs/msg/imu.hpp> |
| 10 | + |
| 11 | +// 继承自 rclcpp::Nod |
| 12 | +class CamDriver final : public rclcpp::Node { |
| 13 | + public: |
| 14 | + CamDriver() |
| 15 | + : Node("ros2_cam_driver"), node_handle_(std::shared_ptr<CamDriver>(this, [](auto *) {})), transport_(node_handle_) { |
| 16 | + // 相机名称,注意和video_cam.cpp 中的相机名称保持一致 |
| 17 | + std::string camera_name_1 = "video_cam"; |
| 18 | + // IMU 设备名称,自定义即可 |
| 19 | + const std::string imu_name = "imu_1"; |
| 20 | + // 使用串口设备进行同步,这里可以选择不同的设备 |
| 21 | + synchronizer_.SetUsbLink("/dev/ttyACM0", 921600); |
| 22 | + // synchronizer_.SetNetLink("192.168.1.188", 8888); |
| 23 | + // 构建 VideoCam 类 |
| 24 | + const auto video_cam = std::make_shared<infinite_sense::VideoCam>(); |
| 25 | + // 重要!!!注册相机名称video_cam, 并且绑定 CAM_1 接口,也就是 video_cam对应的设备必须要接到 CAM_1 |
| 26 | + video_cam->SetParams({{camera_name_1, infinite_sense::CAM_1} |
| 27 | + }); |
| 28 | + // 同步类设置添加相机类 |
| 29 | + synchronizer_.UseSensor(video_cam); |
| 30 | + |
| 31 | + // 逐个调用开始函数 |
| 32 | + synchronizer_.Start(); |
| 33 | + |
| 34 | + // IMU 和 图像信息的发布函数 |
| 35 | + imu_pub_ = this->create_publisher<sensor_msgs::msg::Imu>(imu_name, 10); |
| 36 | + img1_pub_ = transport_.advertise(camera_name_1, 10); |
| 37 | + // SDK 获取IMU和图像信息之后的回调函数 |
| 38 | + { |
| 39 | + using namespace std::placeholders; |
| 40 | + infinite_sense::Messenger::GetInstance().SubStruct(imu_name, std::bind(&CamDriver::ImuCallback, this, _1, _2)); |
| 41 | + infinite_sense::Messenger::GetInstance().SubStruct(camera_name_1, |
| 42 | + std::bind(&CamDriver::ImageCallback1, this, _1, _2)); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + // SDK 获取IMU信息后的回调函数 |
| 47 | + void ImuCallback(const void *msg, size_t) const { |
| 48 | + // 按照ROS2的格式发布的IMU数据 |
| 49 | + const auto *imu_data = static_cast<const infinite_sense::ImuData *>(msg); |
| 50 | + sensor_msgs::msg::Imu imu_msg; |
| 51 | + // 得到的时间单位us,需要转换为ms |
| 52 | + imu_msg.header.stamp = rclcpp::Time(imu_data->time_stamp_us * 1000); |
| 53 | + imu_msg.header.frame_id = "map"; |
| 54 | + imu_msg.linear_acceleration.x = imu_data->a[0]; |
| 55 | + imu_msg.linear_acceleration.y = imu_data->a[1]; |
| 56 | + imu_msg.linear_acceleration.z = imu_data->a[2]; |
| 57 | + imu_msg.angular_velocity.x = imu_data->g[0]; |
| 58 | + imu_msg.angular_velocity.y = imu_data->g[1]; |
| 59 | + imu_msg.angular_velocity.z = imu_data->g[2]; |
| 60 | + imu_msg.orientation.w = imu_data->q[0]; |
| 61 | + imu_msg.orientation.x = imu_data->q[1]; |
| 62 | + imu_msg.orientation.y = imu_data->q[2]; |
| 63 | + imu_msg.orientation.z = imu_data->q[3]; |
| 64 | + imu_pub_->publish(imu_msg); |
| 65 | + } |
| 66 | + |
| 67 | + // SDK 获取图像信息后的回调函数 |
| 68 | + void ImageCallback1(const void *msg, size_t) const { |
| 69 | + // 原始的图像数据 |
| 70 | + const auto *cam_data = static_cast<const infinite_sense::CamData *>(msg); |
| 71 | + std_msgs::msg::Header header; |
| 72 | + // 得到的时间单位us,需要转换为ms |
| 73 | + header.stamp = rclcpp::Time(cam_data->time_stamp_us * 1000); |
| 74 | + header.frame_id = "map"; |
| 75 | + // 构造opencv图像,这里是彩色图片因此是 CV_8UC3 和 bgr8 |
| 76 | + const cv::Mat image_mat(cam_data->image.rows, cam_data->image.cols, CV_8UC3, cam_data->image.data); |
| 77 | + const sensor_msgs::msg::Image::SharedPtr image_msg = cv_bridge::CvImage(header, "bgr8", image_mat).toImageMsg(); |
| 78 | + img1_pub_.publish(image_msg); |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + private: |
| 83 | + infinite_sense::Synchronizer synchronizer_; |
| 84 | + SharedPtr node_handle_; |
| 85 | + image_transport::ImageTransport transport_; |
| 86 | + image_transport::Publisher img1_pub_,img2_pub_; |
| 87 | + rclcpp::Publisher<sensor_msgs::msg::Imu>::SharedPtr imu_pub_; |
| 88 | +}; |
| 89 | + |
| 90 | +// main 函数 |
| 91 | +int main(const int argc, char *argv[]) { |
| 92 | + rclcpp::init(argc, argv); |
| 93 | + // 构造 CamDriver 类,用于读取相机、IMU等信息 |
| 94 | + rclcpp::spin(std::make_shared<CamDriver>()); |
| 95 | + return 0; |
| 96 | +} |
0 commit comments