使用服务控制相机曝光

This commit is contained in:
cyy_mac
2026-03-28 04:46:33 +08:00
parent 5a7e8425f6
commit 1b24c9c566
3 changed files with 29 additions and 8 deletions

View File

@@ -34,6 +34,7 @@
#include <image_transport/image_transport.hpp> #include <image_transport/image_transport.hpp>
#include <rm_interfaces/msg/armors.hpp> #include <rm_interfaces/msg/armors.hpp>
#include <rm_interfaces/srv/set_mode.hpp> #include <rm_interfaces/srv/set_mode.hpp>
#include <std_msgs/msg/float32.hpp>
#include <cv_bridge/cv_bridge.h> #include <cv_bridge/cv_bridge.h>
#include <tf2_ros/buffer.h> #include <tf2_ros/buffer.h>
@@ -95,6 +96,7 @@ private:
rclcpp::Subscription<sensor_msgs::msg::CameraInfo>::SharedPtr cam_info_sub_; rclcpp::Subscription<sensor_msgs::msg::CameraInfo>::SharedPtr cam_info_sub_;
rclcpp::Publisher<rm_interfaces::msg::Armors>::SharedPtr armors_pub_; rclcpp::Publisher<rm_interfaces::msg::Armors>::SharedPtr armors_pub_;
rclcpp::Publisher<visualization_msgs::msg::MarkerArray>::SharedPtr marker_pub_; rclcpp::Publisher<visualization_msgs::msg::MarkerArray>::SharedPtr marker_pub_;
rclcpp::Publisher<std_msgs::msg::Float32>::SharedPtr auto_exposure_pub_;
rclcpp::Service<rm_interfaces::srv::SetMode>::SharedPtr set_mode_srv_; rclcpp::Service<rm_interfaces::srv::SetMode>::SharedPtr set_mode_srv_;
// Image transport for debug // Image transport for debug

View File

@@ -118,6 +118,10 @@ ArmorYoloDetectorNode::ArmorYoloDetectorNode(const rclcpp::NodeOptions& options)
marker_pub_ = marker_pub_ =
this->create_publisher<visualization_msgs::msg::MarkerArray>("armor_detector/marker", 10); this->create_publisher<visualization_msgs::msg::MarkerArray>("armor_detector/marker", 10);
// Auto exposure publisher
auto_exposure_pub_ =
this->create_publisher<std_msgs::msg::Float32>("auto_exposure/set_exposure", 10);
// Debug // Debug
debug_ = this->declare_parameter("debug", false); debug_ = this->declare_parameter("debug", false);
debug_log_interval_frames_ = debug_log_interval_frames_ =
@@ -1000,14 +1004,11 @@ double ArmorYoloDetectorNode::computeBrightnessInRois(
} }
void ArmorYoloDetectorNode::callSetExposureService(double exposure_time) { void ArmorYoloDetectorNode::callSetExposureService(double exposure_time) {
// Set exposure time via parameter - camera driver should subscribe to this parameter // Publish exposure time to camera driver via topic
// or this node should be in same container with camera driver to share parameter std_msgs::msg::Float32 msg;
auto result = this->set_parameter(rclcpp::Parameter("exposure_time", static_cast<int>(exposure_time))); msg.data = exposure_time;
if (result.successful) { auto_exposure_pub_->publish(msg);
FYT_DEBUG("armor_yolo_detect", "Set exposure time to {:.1f}", exposure_time); FYT_DEBUG("armor_yolo_detect", "Auto exposure set exposure_time: {:.1f}", exposure_time);
} else {
FYT_WARN("armor_yolo_detect", "Failed to set exposure time: {}", result.reason);
}
} }
} // namespace armor_yolo_detect } // namespace armor_yolo_detect

View File

@@ -11,6 +11,7 @@
#include <rclcpp/rclcpp.hpp> #include <rclcpp/rclcpp.hpp>
#include <sensor_msgs/msg/camera_info.hpp> #include <sensor_msgs/msg/camera_info.hpp>
#include <sensor_msgs/msg/image.hpp> #include <sensor_msgs/msg/image.hpp>
#include <std_msgs/msg/float32.hpp>
#include <string> #include <string>
#include <thread> #include <thread>
@@ -65,6 +66,20 @@ public:
// Heartbeat // Heartbeat
heartbeat_ = fyt::HeartBeatPublisher::create(this); heartbeat_ = fyt::HeartBeatPublisher::create(this);
// Auto exposure subscriber - receives target exposure time from armor detector
auto_exposure_sub_ = this->create_subscription<std_msgs::msg::Float32>(
"auto_exposure/set_exposure", rclcpp::SensorDataQoS(),
[this](const std_msgs::msg::Float32::SharedPtr msg) {
if (msg->data > 0) {
std::lock_guard<std::mutex> lock(camera_mutex_);
exposure_time_ = static_cast<int>(msg->data);
if (camera_handle_ != nullptr) {
(void)MV_CC_SetFloatValue(camera_handle_, "ExposureTime", static_cast<float>(exposure_time_));
FYT_DEBUG("camera_driver", "Auto exposure set exposure_time: {}", exposure_time_);
}
}
});
// Param set callback // Param set callback
params_callback_handle_ = this->add_on_set_parameters_callback( params_callback_handle_ = this->add_on_set_parameters_callback(
std::bind(&HikCameraNode::parametersCallback, this, std::placeholders::_1)); std::bind(&HikCameraNode::parametersCallback, this, std::placeholders::_1));
@@ -482,6 +497,9 @@ private:
// Heartbeat // Heartbeat
fyt::HeartBeatPublisher::SharedPtr heartbeat_; fyt::HeartBeatPublisher::SharedPtr heartbeat_;
// Auto exposure subscriber
rclcpp::Subscription<std_msgs::msg::Float32>::SharedPtr auto_exposure_sub_;
// Params // Params
int frame_rate_{210}; int frame_rate_{210};
int exposure_time_{5000}; int exposure_time_{5000};