From 3df08afbbd8a30696dcee528e74b0aeff98320b5 Mon Sep 17 00:00:00 2001 From: jonathanhechtbauer-bc Date: Thu, 1 Aug 2019 15:30:16 -0700 Subject: [PATCH] Add balance white mode --- config/default.yaml | 5 +++ .../internal/impl/pylon_camera_base.hpp | 39 +++++++++++++++++++ .../internal/impl/pylon_camera_gige.hpp | 7 ++++ .../internal/impl/pylon_camera_usb.hpp | 7 ++++ include/pylon_camera/internal/pylon_camera.h | 3 ++ include/pylon_camera/pylon_camera.h | 6 +++ include/pylon_camera/pylon_camera_parameter.h | 12 ++++++ src/pylon_camera/pylon_camera_parameter.cpp | 11 ++++++ 8 files changed, 90 insertions(+) diff --git a/config/default.yaml b/config/default.yaml index 6e517ac6..5207ae61 100644 --- a/config/default.yaml +++ b/config/default.yaml @@ -19,6 +19,11 @@ camera_info_url: "" # Default values are 'mono8' and 'rgb8' # image_encoding: "mono8" +# Mode of white balance. +# The supported modes are "continuous", "once" or "off" +# Default value is "" (empty) means default_balance_white_mode +# balance_white: "continuous" + # Binning factor to get downsampled images. It refers here to any camera # setting which combines rectangular neighborhoods of pixels into larger # "super-pixels." It reduces the resolution of the output image to diff --git a/include/pylon_camera/internal/impl/pylon_camera_base.hpp b/include/pylon_camera/internal/impl/pylon_camera_base.hpp index 973fc87d..07d1a53c 100644 --- a/include/pylon_camera/internal/impl/pylon_camera_base.hpp +++ b/include/pylon_camera/internal/impl/pylon_camera_base.hpp @@ -331,6 +331,8 @@ bool PylonCameraImpl::startGrabbing(const PylonCameraParameter& pa return false; } + setBalanceWhite(parameters.balanceWhite()); + cam_->StartGrabbing(); user_output_selector_enums_ = detectAndCountNumUserOutputs(); device_user_id_ = cam_->DeviceUserID.GetValue(); @@ -443,6 +445,43 @@ bool PylonCameraImpl::grab(Pylon::CGrabResultPtr& grab_result) return true; } +template +void PylonCameraImpl::setBalanceWhite(const std::string& target_balance_white) +{ + if ( !target_balance_white.empty() ) + { + if ( GenApi::IsAvailable(cam_->BalanceWhiteAuto) ) + { + BalanceWhiteAutoEnums balance_white; + + if ( target_balance_white == "off" ) + { + balance_white = BalanceWhiteAutoEnums::BalanceWhiteAuto_Off; + } + else if ( target_balance_white == "continuous" ) + { + balance_white = BalanceWhiteAutoEnums::BalanceWhiteAuto_Continuous; + } + else if ( target_balance_white == "once" ) + { + balance_white = BalanceWhiteAutoEnums::BalanceWhiteAuto_Once; + } + else + { + ROS_WARN_STREAM("Desired image encoding parameter: '" << balance_white + << "' is not supported"); + return; + } + cam_->BalanceWhiteAuto.SetValue(balance_white); + } + else + { + ROS_WARN_STREAM("Trying to enable BalanceWhiteAuto mode, but " + << "the camera has no Auto Balance White"); + } + } +} + template std::vector PylonCameraImpl::detectAvailableImageEncodings() { diff --git a/include/pylon_camera/internal/impl/pylon_camera_gige.hpp b/include/pylon_camera/internal/impl/pylon_camera_gige.hpp index a4600e86..ed5c9ad4 100644 --- a/include/pylon_camera/internal/impl/pylon_camera_gige.hpp +++ b/include/pylon_camera/internal/impl/pylon_camera_gige.hpp @@ -43,6 +43,7 @@ namespace pylon_camera struct GigECameraTrait { typedef Pylon::CBaslerGigEInstantCamera CBaslerInstantCameraT; + typedef Basler_GigECameraParams::BalanceWhiteAutoEnums BalanceWhiteAutoEnums; typedef Basler_GigECameraParams::ExposureAutoEnums ExposureAutoEnums; typedef Basler_GigECameraParams::GainAutoEnums GainAutoEnums; typedef Basler_GigECameraParams::PixelFormatEnums PixelFormatEnums; @@ -125,6 +126,12 @@ bool PylonGigECamera::applyCamSpecificStartupSettings(const PylonCameraParameter { try { + if ( GenApi::IsAvailable(cam_->BalanceWhiteAuto) ) + { + // Make sure BalanceWhite is off, otherwise the camera might fail to start + cam_->BalanceWhiteAuto.SetValue(Basler_GigECameraParams::BalanceWhiteAuto_Off); + } + // Remove all previous settings (sequencer etc.) // Default Setting = Free-Running cam_->UserSetSelector.SetValue(Basler_GigECameraParams::UserSetSelector_Default); diff --git a/include/pylon_camera/internal/impl/pylon_camera_usb.hpp b/include/pylon_camera/internal/impl/pylon_camera_usb.hpp index 7d309a7d..2505f144 100644 --- a/include/pylon_camera/internal/impl/pylon_camera_usb.hpp +++ b/include/pylon_camera/internal/impl/pylon_camera_usb.hpp @@ -43,6 +43,7 @@ namespace pylon_camera struct USBCameraTrait { typedef Pylon::CBaslerUsbInstantCamera CBaslerInstantCameraT; + typedef Basler_UsbCameraParams::BalanceWhiteAutoEnums BalanceWhiteAutoEnums; typedef Basler_UsbCameraParams::ExposureAutoEnums ExposureAutoEnums; typedef Basler_UsbCameraParams::GainAutoEnums GainAutoEnums; typedef Basler_UsbCameraParams::PixelFormatEnums PixelFormatEnums; @@ -66,6 +67,12 @@ bool PylonUSBCamera::applyCamSpecificStartupSettings(const PylonCameraParameter& { try { + if ( GenApi::IsAvailable(cam_->BalanceWhiteAuto) ) + { + // Make sure BalanceWhite is off, otherwise the camera might fail to start + cam_->BalanceWhiteAuto.SetValue(Basler_UsbCameraParams::BalanceWhiteAuto_Off); + } + // Remove all previous settings (sequencer etc.) // Default Setting = Free-Running cam_->UserSetSelector.SetValue(Basler_UsbCameraParams::UserSetSelector_Default); diff --git a/include/pylon_camera/internal/pylon_camera.h b/include/pylon_camera/internal/pylon_camera.h index 91c99e67..6719a5f5 100644 --- a/include/pylon_camera/internal/pylon_camera.h +++ b/include/pylon_camera/internal/pylon_camera.h @@ -71,6 +71,8 @@ class PylonCameraImpl : public PylonCamera virtual bool setShutterMode(const pylon_camera::SHUTTER_MODE& mode); + virtual void setBalanceWhite(const std::string& balance_white); + virtual bool setROI(const sensor_msgs::RegionOfInterest target_roi, sensor_msgs::RegionOfInterest& reached_roi); @@ -147,6 +149,7 @@ class PylonCameraImpl : public PylonCamera protected: typedef typename CameraTraitT::CBaslerInstantCameraT CBaslerInstantCameraT; + typedef typename CameraTraitT::BalanceWhiteAutoEnums BalanceWhiteAutoEnums; typedef typename CameraTraitT::ExposureAutoEnums ExposureAutoEnums; typedef typename CameraTraitT::GainAutoEnums GainAutoEnums; typedef typename CameraTraitT::PixelFormatEnums PixelFormatEnums; diff --git a/include/pylon_camera/pylon_camera.h b/include/pylon_camera/pylon_camera.h index 4073898f..7aff5fc9 100644 --- a/include/pylon_camera/pylon_camera.h +++ b/include/pylon_camera/pylon_camera.h @@ -128,6 +128,12 @@ class PylonCamera */ virtual bool setShutterMode(const pylon_camera::SHUTTER_MODE& mode) = 0; + /** + * Mode of white balance. + * @param balance_white the target white balance + */ + virtual void setBalanceWhite(const std::string& balance_white) = 0; + /** * Update area of interest in the camera image * @param target_roi the target roi diff --git a/include/pylon_camera/pylon_camera_parameter.h b/include/pylon_camera/pylon_camera_parameter.h index eb50cf8e..11350bce 100644 --- a/include/pylon_camera/pylon_camera_parameter.h +++ b/include/pylon_camera/pylon_camera_parameter.h @@ -94,6 +94,11 @@ class PylonCameraParameter */ const std::string& imageEncoding() const; + /** + * Getter for the balance_white_ read from ros-parameter server + */ + const std::string& balanceWhite() const; + /** * Setter for the frame_rate_ initially set from ros-parameter server * The frame rate needs to be updated with the value the camera supports @@ -326,6 +331,13 @@ class PylonCameraParameter * 'bayer_gbrg8', 'bayer_rggb8' and 'yuv422' */ std::string image_encoding_; + + /** + * Mode of white balance. + * The supported modes are "continuous", "once" or "off" + * Default value is "" (empty) means default_balance_white_mode + */ + std::string balance_white_; }; } // namespace pylon_camera diff --git a/src/pylon_camera/pylon_camera_parameter.cpp b/src/pylon_camera/pylon_camera_parameter.cpp index 75a372d8..6fdc7929 100644 --- a/src/pylon_camera/pylon_camera_parameter.cpp +++ b/src/pylon_camera/pylon_camera_parameter.cpp @@ -39,6 +39,7 @@ PylonCameraParameter::PylonCameraParameter() : frame_rate_(5.0), camera_info_url_(""), image_encoding_(""), + balance_white_(""), binning_x_(1), binning_y_(1), binning_x_given_(false), @@ -145,6 +146,11 @@ void PylonCameraParameter::readFromRosParameterServer(const ros::NodeHandle& nh) image_encoding_ = encoding; } + if ( nh.hasParam("balance_white") ) + { + nh.getParam("balance_white", balance_white_); + std::cout << "balance white is given and has value " << balance_white_ << std::endl; + } // ########################## // image intensity settings // ########################## @@ -336,6 +342,11 @@ const std::string& PylonCameraParameter::imageEncoding() const return image_encoding_; } +const std::string& PylonCameraParameter::balanceWhite() const +{ + return balance_white_; +} + const std::string& PylonCameraParameter::cameraFrame() const { return camera_frame_;