diff --git a/doc/tutorials/modules/devices/camera.md b/doc/tutorials/modules/devices/camera.md index 7272607b..f2358c5f 100644 --- a/doc/tutorials/modules/devices/camera.md +++ b/doc/tutorials/modules/devices/camera.md @@ -216,7 +216,7 @@ capture.set(CAMERA_TRIGGER_PERIOD, 100); ```cpp capture.set(CAMERA_ONCE_WB); // 执行一次白平衡操作,仅在手动白平衡模式下有效 -capture.set(CAMERA_SOFT_TRIGGER); // 执行一次软触发,仅在软触发模式下有效 +capture.set(CAMERA_TRIGGER_SOFT); // 执行一次软触发,仅在软触发模式下有效 ``` ## 2. para 参数加载 @@ -364,7 +364,7 @@ int main() while (run) { std::this_thread::sleep_for(std::chrono::milliseconds(10)); - capture.set(rm::CAMERA_SOFT_TRIGGER); // 触发 + capture.set(rm::CAMERA_TRIGGER_SOFT); // 触发 } }); diff --git a/modules/camera/CMakeLists.txt b/modules/camera/CMakeLists.txt index da085fa3..d9d79803 100755 --- a/modules/camera/CMakeLists.txt +++ b/modules/camera/CMakeLists.txt @@ -1,13 +1,11 @@ -find_package(OpenCV REQUIRED) - rmvl_generate_para( camera MODULE camera ) + rmvl_add_module( camera DEPENDS rmath - EXTERNAL ${OpenCV_LIBS} ) # -------------------------------------------------------------------------- diff --git a/modules/camera/include/rmvl/camera/camutils.hpp b/modules/camera/include/rmvl/camera/camutils.hpp index cf3470c9..c67049ac 100644 --- a/modules/camera/include/rmvl/camera/camutils.hpp +++ b/modules/camera/include/rmvl/camera/camutils.hpp @@ -93,33 +93,29 @@ struct CameraConfig enum CameraProperties : uint16_t { // ---------------- 设备属性 ---------------- - CAMERA_AUTO_EXPOSURE = 0x1001, //!< 自动曝光 - CAMERA_AUTO_WB = 0x1002, //!< 自动白平衡 - CAMERA_MANUAL_EXPOSURE = 0x1003, //!< 手动曝光 - CAMERA_MANUAL_WB = 0x1004, //!< 手动白平衡 - CAMERA_EXPOSURE = 0x1005, //!< 曝光值 - CAMERA_GAIN = 0x1006, //!< 模拟增益 - CAMERA_GAMMA = 0x1007, //!< Gamma 值 - CAMERA_WB_RGAIN = 0x1008, //!< 白平衡红色分量 - CAMERA_WB_GGAIN = 0x1009, //!< 白平衡绿色分量 - CAMERA_WB_BGAIN = 0x100a, //!< 白平衡蓝色分量 - CAMERA_CONTRAST = 0x100c, //!< 对比度 - CAMERA_SATURATION = 0x100d, //!< 饱和度 - CAMERA_SHARPNESS = 0x100e, //!< 锐度 - CAMERA_FRAME_HEIGHT = 0x100f, //!< 图像帧高度 - CAMERA_FRAME_WIDTH = 0x1010, //!< 图像帧宽度 + CAMERA_AUTO_EXPOSURE = 0x1, //!< 自动曝光 + CAMERA_MANUAL_EXPOSURE = 0x2, //!< 手动曝光 + CAMERA_ONCE_EXPOSURE = 0x3, //!< 单次曝光 + CAMERA_AUTO_WB = 0x10, //!< 自动白平衡 + CAMERA_MANUAL_WB = 0x11, //!< 手动白平衡 + CAMERA_ONCE_WB = 0x12, //!< 单次白平衡 + CAMERA_EXPOSURE = 0x20, //!< 曝光值 + CAMERA_GAIN = 0x21, //!< 模拟增益 + CAMERA_GAMMA = 0x22, //!< Gamma 值 + CAMERA_WB_RGAIN = 0x23, //!< 白平衡红色分量 + CAMERA_WB_GGAIN = 0x24, //!< 白平衡绿色分量 + CAMERA_WB_BGAIN = 0x25, //!< 白平衡蓝色分量 + CAMERA_CONTRAST = 0x26, //!< 对比度 + CAMERA_SATURATION = 0x27, //!< 饱和度 + CAMERA_SHARPNESS = 0x28, //!< 锐度 + CAMERA_FRAME_HEIGHT = 0x30, //!< 图像帧高度 + CAMERA_FRAME_WIDTH = 0x31, //!< 图像帧宽度 // ---------------- 处理属性 ---------------- - CAMERA_TRIGGER_DELAY = 0x1101, //!< 硬触发采集延迟(微秒\f$μs\f$) - CAMERA_TRIGGER_COUNT = 0x1102, //!< 单次触发时的触发帧数 - CAMERA_TRIGGER_PERIOD = 0x1103, //!< 单次触发时多次采集的周期(微秒\f$μs\f$) -}; - -//! 相机运行时事件 -enum CameraEvents : uint16_t -{ - CAMERA_ONCE_WB = 0x2001, //!< 执行一次白平衡 - CAMERA_SOFT_TRIGGER = 0x2002 //!< 执行软触发 + CAMERA_TRIGGER_DELAY = 0x40, //!< 硬触发采集延迟(微秒\f$μs\f$) + CAMERA_TRIGGER_COUNT = 0x41, //!< 单次触发时的触发帧数 + CAMERA_TRIGGER_PERIOD = 0x42, //!< 单次触发时多次采集的周期(微秒\f$μs\f$) + CAMERA_TRIGGER_SOFT = 0x43, //!< 执行软触发 }; //! 相机外参 diff --git a/modules/camera/src/hik_camera/hik_camera.cpp b/modules/camera/src/hik_camera/hik_camera.cpp index c315e4ec..50c46f39 100644 --- a/modules/camera/src/hik_camera/hik_camera.cpp +++ b/modules/camera/src/hik_camera/hik_camera.cpp @@ -32,7 +32,7 @@ static const MV_CC_PIXEL_CONVERT_PARAM MV_CC_PIXEL_CONVERT_PARAM_Init = {0, 0, PixelType_Gvsp_Undefined, nullptr, 0, PixelType_Gvsp_Undefined, nullptr, 0, 0, {0}}; HikCamera::Impl::Impl(CameraConfig init_mode, std::string_view serial) noexcept - : _grab_mode(init_mode.grab_mode), _retrieve_mode(init_mode.retrieve_mode), _serial(serial) { _opened = open(); } + : _init_mode(init_mode), _serial(serial) { _opened = open(); } HikCamera::Impl::~Impl() noexcept { release(); } @@ -63,6 +63,9 @@ void HikCamera::Impl::release() noexcept bool HikCamera::Impl::open() noexcept { + // 提取初始化模式 + auto grab_mode = _init_mode.grab_mode; + auto trigger_chn = _init_mode.trigger_channel; // ----------------------- 设备枚举 ----------------------- int ret = MV_OK; ret = MV_CC_EnumDevices(MV_USB_DEVICE, &_devices); @@ -108,16 +111,41 @@ bool HikCamera::Impl::open() noexcept return false; } // --------------------- 设置工作模式 --------------------- - if (_grab_mode == GrabMode::Continuous) - ret = MV_CC_SetEnumValue(_handle, "TriggerMode", 0); // 连续采样 + if (grab_mode == GrabMode::Continuous) + ret = MV_CC_SetEnumValue(_handle, "TriggerMode", MV_TRIGGER_MODE_OFF); // 连续采样 else - ret = MV_CC_SetEnumValue(_handle, "TriggerMode", 1); // 触发模式 + ret = MV_CC_SetEnumValue(_handle, "TriggerMode", MV_TRIGGER_MODE_ON); // 触发模式 if (ret != MV_OK) { ERROR_("hik - failed to set trigger mode (error: \"%s\")", errorCode2Str(ret)); return false; } - // 开始取流 + // ---------------------- 设置触发源 ---------------------- + if (grab_mode == GrabMode::Software) + ret = MV_CC_SetEnumValue(_handle, "TriggerSource", MV_TRIGGER_SOURCE_SOFTWARE); // 软触发 + else if (grab_mode == GrabMode::Hardware) + { + // 硬触发通道选择 + switch (trigger_chn) + { + case TriggerChannel::Chn0: + ret = MV_CC_SetEnumValue(_handle, "TriggerSource", MV_TRIGGER_SOURCE_LINE0); + break; + case TriggerChannel::Chn1: + ret = MV_CC_SetEnumValue(_handle, "TriggerSource", MV_TRIGGER_SOURCE_LINE1); + break; + case TriggerChannel::Chn2: + ret = MV_CC_SetEnumValue(_handle, "TriggerSource", MV_TRIGGER_SOURCE_LINE2); + break; + case TriggerChannel::Chn3: + ret = MV_CC_SetEnumValue(_handle, "TriggerSource", MV_TRIGGER_SOURCE_LINE3); + break; + default: + ERROR_("hik - invalid trigger channel: %d", static_cast(trigger_chn)); + return false; + } + } + // ----------------------- 开始取流 ----------------------- ret = MV_CC_StartGrabbing(_handle); if (ret != MV_OK) { @@ -208,7 +236,7 @@ bool HikCamera::Impl::retrieve(cv::OutputArray image, RetrieveMode flag) noexcep } // 无效参数 else - ERROR_("hik - failed to retrieve, invalid retrieve mode: %d.", static_cast(_retrieve_mode)); + ERROR_("hik - failed to retrieve, invalid retrieve mode: %d.", static_cast(_init_mode.retrieve_mode)); // 处理失败默认操作 image.assign(cv::Mat()); return false; @@ -219,11 +247,12 @@ bool HikCamera::Impl::read(cv::OutputArray image) noexcept // 获取图像地址 auto ret = MV_CC_GetImageBuffer(_handle, &_p_out, 1000); if (ret == MV_OK) - retrieve(image, _retrieve_mode); + retrieve(image, _init_mode.retrieve_mode); else { WARNING_("hik - No data in getting image buffer"); reconnect(); + return false; } // 释放图像缓存 if (_p_out.pBufAddr != nullptr) diff --git a/modules/camera/src/hik_camera/hik_camera_impl.h b/modules/camera/src/hik_camera/hik_camera_impl.h index 1109212c..5edbf2cf 100644 --- a/modules/camera/src/hik_camera/hik_camera_impl.h +++ b/modules/camera/src/hik_camera/hik_camera_impl.h @@ -23,10 +23,9 @@ class HikCamera::Impl // -------------------------- 相机信息 -------------------------- void *_handle; //!< 相机设备句柄 MV_CC_DEVICE_INFO_LIST _devices; //!< 设备信息列表 - GrabMode _grab_mode; //!< 采集模式 - RetrieveMode _retrieve_mode; //!< 处理模式 + CameraConfig _init_mode; //!< 初始化配置模式 std::string _serial; //!< 相机序列号 S/N - bool _opened = false; //!< 相机是否打开 + bool _opened{}; //!< 相机是否打开 // -------------------------- 图像信息 -------------------------- MV_FRAME_OUT _p_out; //!< 输出图像的数据及信息 diff --git a/modules/camera/src/hik_camera/hik_properties.cpp b/modules/camera/src/hik_camera/hik_properties.cpp index c54f1acd..f339e980 100644 --- a/modules/camera/src/hik_camera/hik_properties.cpp +++ b/modules/camera/src/hik_camera/hik_properties.cpp @@ -21,17 +21,21 @@ bool HikCamera::Impl::set(int propId, double value) noexcept { // Properties case CAMERA_AUTO_EXPOSURE: - return MV_CC_SetEnumValue(_handle, "ExposureAuto", 2) == MV_OK; // Continuous + return MV_CC_SetEnumValue(_handle, "ExposureAuto", MV_EXPOSURE_AUTO_MODE_CONTINUOUS) == MV_OK; case CAMERA_MANUAL_EXPOSURE: - return MV_CC_SetEnumValue(_handle, "ExposureAuto", 0) == MV_OK; // Off + return MV_CC_SetEnumValue(_handle, "ExposureAuto", MV_EXPOSURE_AUTO_MODE_OFF) == MV_OK; + case CAMERA_ONCE_EXPOSURE: + return MV_CC_SetEnumValue(_handle, "ExposureAuto", MV_EXPOSURE_AUTO_MODE_ONCE) == MV_OK; + case CAMERA_AUTO_WB: + return MV_CC_SetEnumValue(_handle, "BalanceWhiteAuto", MV_BALANCEWHITE_AUTO_CONTINUOUS) == MV_OK; + case CAMERA_MANUAL_WB: + return MV_CC_SetEnumValue(_handle, "BalanceWhiteAuto", MV_BALANCEWHITE_AUTO_OFF) == MV_OK; + case CAMERA_ONCE_WB: + return MV_CC_SetEnumValue(_handle, "BalanceWhiteAuto", MV_BALANCEWHITE_AUTO_ONCE) == MV_OK; case CAMERA_EXPOSURE: return MV_CC_SetFloatValue(_handle, "ExposureTime", static_cast(value)) == MV_OK; case CAMERA_GAIN: return MV_CC_SetFloatValue(_handle, "Gain", static_cast(value)) == MV_OK; - case CAMERA_AUTO_WB: - return MV_CC_SetEnumValue(_handle, "BalanceWhiteAuto", 1) == MV_OK; // Continuous - case CAMERA_MANUAL_WB: - return MV_CC_SetEnumValue(_handle, "BalanceWhiteAuto", 0) == MV_OK; // Off case CAMERA_WB_BGAIN: return MV_CC_SetEnumValue(_handle, "BalanceRatioSelector", 2) == MV_OK && // Blue MV_CC_SetIntValue(_handle, "BalanceRatio", static_cast(value)) == MV_OK; @@ -48,10 +52,7 @@ bool HikCamera::Impl::set(int propId, double value) noexcept case CAMERA_SATURATION: return MV_CC_SetBoolValue(_handle, "SaturationEnable", true) == MV_OK && MV_CC_SetFloatValue(_handle, "Saturation", static_cast(value)) == MV_OK; - // Activities - case CAMERA_ONCE_WB: - return MV_CC_SetEnumValue(_handle, "BalanceWhiteAuto", 2) == MV_OK; // Once - case CAMERA_SOFT_TRIGGER: + case CAMERA_TRIGGER_SOFT: return MV_CC_SetCommandValue(_handle, "TriggerSoftware") == MV_OK; default: ERROR_("Try to set undefined variable, id: %d.", propId); diff --git a/modules/camera/src/mv_camera/mv_properties.cpp b/modules/camera/src/mv_camera/mv_properties.cpp index 470d519f..624777bb 100644 --- a/modules/camera/src/mv_camera/mv_properties.cpp +++ b/modules/camera/src/mv_camera/mv_properties.cpp @@ -74,7 +74,7 @@ bool MvCamera::Impl::set(int propId, double value) noexcept // Activities case CAMERA_ONCE_WB: return CameraSetOnceWB(_handle) == CAMERA_STATUS_SUCCESS; - case CAMERA_SOFT_TRIGGER: + case CAMERA_TRIGGER_SOFT: if (_grab_mode != GrabMode::Continuous) return CameraSoftTrigger(_handle) == CAMERA_STATUS_SUCCESS; else