88
99#include < sensor_msgs/msg/imu.hpp>
1010
11+
12+ struct video_config {
13+ bool onboard_imu = true ;// 使用内部IMU
14+ bool extern_imu = false ;// 使用外部IMU,TODO
15+ std::string imu_name = " imu_1" ;
16+
17+ // 相机名称 + 设备ID
18+ std::pair<std::string, int > CAM3_Video = {" left_cam" , 0 };
19+ std::pair<std::string, int > CAM4_Video = {" right_cam" , -1 };
20+ std::pair<std::string, int > CAM1_Video = {" front_cam" , -1 };
21+ std::pair<std::string, int > CAM2_Video = {" rear_cam" , -1 }; // -1 代表没有
22+
23+ // 通信方式
24+ int type = 0 ; // 0 --> 串口通信 1 --> 网口通信
25+ std::string uart_dev = " /dev/ttyACM0" ;
26+ std::string net_ip = " 192.168.1.188" ;
27+ int net_port = 8888 ;
28+ };
29+ video_config cfg;
30+
31+ using namespace infinite_sense ;
32+
1133// 继承自 rclcpp::Nod
1234class CamDriver final : public rclcpp::Node {
1335 public:
1436 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);
37+ : Node(" ros2_video_demo" ), node_handle_(std::shared_ptr<CamDriver>(this , [](auto *) {})), transport_(node_handle_){
38+
39+ // 相机通信方式
40+ if (cfg.type == 0 ) {
41+ synchronizer_.SetUsbLink (cfg.uart_dev , 921600 );
42+ } else if (cfg.type == 1 ) {
43+ synchronizer_.SetNetLink (cfg.net_ip , cfg.net_port );
44+ }
45+
46+ std::vector<std::pair<std::string, int >> cam_list;
47+ if (cfg.CAM1_Video .second >= 0 ) cam_list.push_back (cfg.CAM1_Video );
48+ if (cfg.CAM2_Video .second >= 0 ) cam_list.push_back (cfg.CAM2_Video );
49+ if (cfg.CAM3_Video .second >= 0 ) cam_list.push_back (cfg.CAM3_Video );
50+ if (cfg.CAM4_Video .second >= 0 ) cam_list.push_back (cfg.CAM4_Video );
51+
52+ video_cam_ = std::make_shared<VideoCam>(cam_list);
53+ synchronizer_.UseSensor (video_cam_);
54+
55+ if (cfg.onboard_imu ) {
56+ imu_pub_ = this ->create_publisher <sensor_msgs::msg::Imu>(cfg.imu_name , 1000 );
57+ }
58+
59+ if (cfg.CAM1_Video .second >= 0 ) image_pubs_[cfg.CAM1_Video .first ] = transport_.advertise (cfg.CAM1_Video .first , 30 );
60+ if (cfg.CAM2_Video .second >= 0 ) image_pubs_[cfg.CAM2_Video .first ] = transport_.advertise (cfg.CAM2_Video .first , 30 );
61+ if (cfg.CAM3_Video .second >= 0 ) image_pubs_[cfg.CAM3_Video .first ] = transport_.advertise (cfg.CAM3_Video .first , 30 );
62+ if (cfg.CAM4_Video .second >= 0 ) image_pubs_[cfg.CAM4_Video .first ] = transport_.advertise (cfg.CAM4_Video .first , 30 );
63+
64+ // 设置触发参数(如果有的话)
65+ // 这里用相机名作为 key
66+ if (cfg.CAM1_Video .second >= 0 ) video_cam_->SetParams ({{cfg.CAM1_Video .first , CAM_1}});
67+ if (cfg.CAM2_Video .second >= 0 ) video_cam_->SetParams ({{cfg.CAM2_Video .first , CAM_2}});
68+ if (cfg.CAM3_Video .second >= 0 ) video_cam_->SetParams ({{cfg.CAM3_Video .first , CAM_3}});
69+ if (cfg.CAM4_Video .second >= 0 ) video_cam_->SetParams ({{cfg.CAM4_Video .first , CAM_4}});
3070
3171 // 逐个调用开始函数
3272 synchronizer_.Start ();
3373
34- // IMU 和 图像信息的发布函数
35- imu_pub_ = this ->create_publisher <sensor_msgs::msg::Imu>(imu_name, 10 );
36- img1_pub_ = transport_.advertise (camera_name_1, 10 );
74+
3775 // SDK 获取IMU和图像信息之后的回调函数
3876 {
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));
77+ Messenger::GetInstance ().SubStruct (
78+ cfg.imu_name , std::bind (&CamDriver::ImuCallback, this , std::placeholders::_1, std::placeholders::_2));
79+
80+ if (cfg.CAM1_Video .second >= 0 ) Messenger::GetInstance ().SubStruct (cfg.CAM1_Video .first , std::bind (&CamDriver::ImageCallback, this , std::placeholders::_1, std::placeholders::_2));
81+ if (cfg.CAM2_Video .second >= 0 ) Messenger::GetInstance ().SubStruct (cfg.CAM2_Video .first , std::bind (&CamDriver::ImageCallback, this , std::placeholders::_1, std::placeholders::_2));
82+ if (cfg.CAM3_Video .second >= 0 ) Messenger::GetInstance ().SubStruct (cfg.CAM3_Video .first , std::bind (&CamDriver::ImageCallback, this , std::placeholders::_1, std::placeholders::_2));
83+ if (cfg.CAM4_Video .second >= 0 ) Messenger::GetInstance ().SubStruct (cfg.CAM4_Video .first , std::bind (&CamDriver::ImageCallback, this , std::placeholders::_1, std::placeholders::_2));
4384 }
4485 }
4586
@@ -65,7 +106,7 @@ class CamDriver final : public rclcpp::Node {
65106 }
66107
67108 // SDK 获取图像信息后的回调函数
68- void ImageCallback1 (const void *msg, size_t ) const {
109+ void ImageCallback (const void *msg, size_t ) {
69110 // 原始的图像数据
70111 const auto *cam_data = static_cast <const infinite_sense::CamData *>(msg);
71112 std_msgs::msg::Header header;
@@ -74,16 +115,17 @@ class CamDriver final : public rclcpp::Node {
74115 header.frame_id = " map" ;
75116 // 构造opencv图像,这里是彩色图片因此是 CV_8UC3 和 bgr8
76117 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);
118+ sensor_msgs::msg::Image::SharedPtr image_msg = cv_bridge::CvImage (header, " bgr8" , image_mat).toImageMsg ();
119+ image_pubs_[cam_data-> name ] .publish (image_msg);
79120 }
80121
81122
82123 private:
83124 infinite_sense::Synchronizer synchronizer_;
84125 SharedPtr node_handle_;
85126 image_transport::ImageTransport transport_;
86- image_transport::Publisher img1_pub_,img2_pub_;
127+ std::unordered_map<std::string, image_transport::Publisher> image_pubs_;
128+ std::shared_ptr<VideoCam> video_cam_;
87129 rclcpp::Publisher<sensor_msgs::msg::Imu>::SharedPtr imu_pub_;
88130};
89131
0 commit comments