From 7db5479a9f91ff0932e3f457ae5cd386e2112907 Mon Sep 17 00:00:00 2001
From: mithunvarma <diddi.m@husky.neu.edu>
Date: Tue, 24 Sep 2019 15:33:10 -0400
Subject: [PATCH 1/2] will add auto exposure time upper and lower limits, so we
 can acheive faster fps in auto exposure mode by limiting upper limit, removed
 old master_fps params

---
 README.md                                     |  6 ++++-
 include/spinnaker_sdk_camera_driver/capture.h |  3 ++-
 launch/acquisition.launch                     |  8 +++++--
 launch/node_acquisition.launch                |  8 +++++--
 params/test_params.yaml                       |  4 ++--
 src/capture.cpp                               | 22 ++++++++++++++++---
 6 files changed, 40 insertions(+), 11 deletions(-)

diff --git a/README.md b/README.md
index b28e4ad..2741a13 100644
--- a/README.md
+++ b/README.md
@@ -65,7 +65,11 @@ All the parameters can be set via the launch file or via the yaml config_file.
   Should color images be used (only works on models that support color images)
 * ~exposure_time (int, default: 0, 0:auto)  
   Exposure setting for cameras, also available as dynamic reconfiguarble parameter.
-  * ~external_trigger (bool, default: false)  
+* ~auto_exposure_lower_limit (int, default: 100 us)  
+  Auto Exposure lower limit setting to be used in auto_exposure mode, min:6 for blackfly s model.
+* ~auto_exposure_upper_limit (int, default: 2000 us)  
+  Auto Exposure upper limit setting to be used in auto_exposure mode.
+* ~external_trigger (bool, default: false)  
   Camera triggering setting when using an external trigger.  In this mode, none of the cameras would be set as a master camera. All cameras are setup to use external trigger.  In this mode the main loop runs at rate set by soft_framerate, so if the external trigger rate is higher than the soft_framerate, the buffer will get filled and images will have a lag. Also in this mode, the getnextimage timeout is set to infinite so that the node dosen't die if a trigger is not received for a while.
 * ~target_grey_value (double, default: 0 , 0:Continous/auto)
   Setting target_grey_value > 4 (min:4 , max:99) will turn AutoExposureTargetGreyValueAuto to 'off' and set AutoExposureTargetGreyValue to target_grey_value. Also available as dynamic reconfigurable parameter. see below in Dynamic reconfigurable parameter section.
diff --git a/include/spinnaker_sdk_camera_driver/capture.h b/include/spinnaker_sdk_camera_driver/capture.h
index 741d286..a0a4be4 100755
--- a/include/spinnaker_sdk_camera_driver/capture.h
+++ b/include/spinnaker_sdk_camera_driver/capture.h
@@ -105,12 +105,13 @@ namespace acquisition {
         int nframes_;
         float init_delay_;
         int skip_num_;
-        float master_fps_;
+        //float master_fps_;
         int binning_;
         bool color_;
         string dump_img_;
         string ext_;
         float exposure_time_;
+        float auto_exposure_lower_limit_,auto_exposure_upper_limit_;
         double target_grey_value_;
         // int decimation_;
         string tf_prefix_;        
diff --git a/launch/acquisition.launch b/launch/acquisition.launch
index 8d8fd31..ea4c4bf 100644
--- a/launch/acquisition.launch
+++ b/launch/acquisition.launch
@@ -3,9 +3,11 @@
   <env name="ROSCONSOLE_CONFIG_FILE" value="$(find spinnaker_sdk_camera_driver)/cfg/std_console.conf"/>
 
   <!-- acquisition spinnaker params-->
-  <arg name="binning"		    default="1"	doc="Binning for cameras, when changing from 2 to 1 cameras need to be unplugged and replugged"/>
+  <arg name="binning"		      default="1"	doc="Binning for cameras, when changing from 2 to 1 cameras need to be unplugged and replugged"/>
   <arg name="color"		        default="false"	doc="Should color images be used (only works on models that support color images)"/>
-  <arg name="exposure_time"     default="0"	doc="Exposure_time setting for cameras"/>
+  <arg name="exposure_time"   default="0"	doc="Exposure_time setting for cameras"/>
+  <arg name="auto_exposure_lower_limit" default="100"  doc="auto_exposure lower limit setting for auto_exposure mode, min:6 for blackfly s"/>
+  <arg name="auto_exposure_upper_limit" default="2000"  doc="auto_exposure upper limit setting for auto_exposure mode"/>
   <arg name="external_trigger"  default="false"	doc="External trigger (No camera is master)"/>
   <arg name="target_grey_value"   default="0"   doc="AutoExposureTargetGreyValue min: 4 max: 99 if set below 4, AutoExposureTargetGreyValueAuto will be set to continuous(auto) also available as dynamic reconfigurable parameter" />
   <arg name="frames"            default="3400"	doc="Numer of frames to save/view 0=ON"/>
@@ -43,6 +45,8 @@
 
     <!-- Load parameters onto server using argument or default values above -->
     <param name="exposure_time"     value="$(arg exposure_time)" />
+    <param name="auto_exposure_lower_limit" value="$(arg auto_exposure_lower_limit)" />
+    <param name="auto_exposure_upper_limit" value="$(arg auto_exposure_upper_limit)" />
     <param name="external_trigger"     value="$(arg external_trigger)" />
     <param name="target_grey_value" value="$(arg target_grey_value)" />
     <param name="binning"           value="$(arg binning)" />
diff --git a/launch/node_acquisition.launch b/launch/node_acquisition.launch
index 83d3aec..d276466 100644
--- a/launch/node_acquisition.launch
+++ b/launch/node_acquisition.launch
@@ -5,8 +5,10 @@
   <!-- acquisition.launch -->
 
   <arg name="binning"		    default="1"	doc="Binning for cameras, when changing from 2 to 1 cameras need to be unplugged and replugged"/>
-  <arg name="color"		        default="false"	doc="Should color images be used (only works on models that support color images)"/>
-  <arg name="exposure_time"     default="0" doc="Exposure_time setting for cameras"/>
+  <arg name="color"		      default="false"	doc="Should color images be used (only works on models that support color images)"/>
+  <arg name="exposure_time" default="0" doc="Exposure_time setting for cameras"/>
+  <arg name="auto_exposure_lower_limit" default="100"  doc="auto_exposure lower limit setting for auto_exposure mode, min:6 for blackfly s"/>
+  <arg name="auto_exposure_upper_limit" default="2000"  doc="auto_exposure upper limit setting for auto_exposure mode"/>
   <arg name="external_trigger"  default="false"	doc="External trigger (No camera is master)"/>
   <arg name="target_grey_value"   default="0"   doc="AutoExposureTargetGreyValue min: 4 max: 99 if set below 4, AutoExposureTargetGreyValueAuto will be set to continuous(auto) also available as dynamic reconfigurable parameter" />
   <arg name="frames"            default="3400"	doc="Numer of frames to save/view 0=ON"/>
@@ -36,6 +38,8 @@
 
     <!-- Load parameters onto server using argument or default values above -->
     <param name="exposure_time"     value="$(arg exposure_time)" />
+    <param name="auto_exposure_lower_limit" value="$(arg auto_exposure_lower_limit)" />
+    <param name="auto_exposure_upper_limit" value="$(arg auto_exposure_upper_limit)" />
     <param name="external_trigger"  value="$(arg external_trigger)" />
     <param name="target_grey_value" value="$(arg target_grey_value)" />
     <param name="binning"           value="$(arg binning)" />
diff --git a/params/test_params.yaml b/params/test_params.yaml
index c359b2c..084e36f 100644
--- a/params/test_params.yaml
+++ b/params/test_params.yaml
@@ -1,8 +1,8 @@
 cam_ids:
-- 17197556
+- 17197554
 cam_aliases:
 - cam0
-master_cam: 17197556
+master_cam: 17197554
 skip: 20
 delay: 1.0
 
diff --git a/src/capture.cpp b/src/capture.cpp
index 95586a8..dc9eedb 100755
--- a/src/capture.cpp
+++ b/src/capture.cpp
@@ -82,6 +82,8 @@ void acquisition::Capture::init_variables_register_to_ros() {
 
     // default values for the parameters are set here. Should be removed eventually!!
     exposure_time_ = 0 ; // default as 0 = auto exposure
+    auto_exposure_lower_limit_ = 100; // 0.1 ms(min value blackfly s=6us)
+    auto_exposure_upper_limit_ = 2000; //  2ms
     soft_framerate_ = 20; //default soft framrate
     ext_ = ".bmp";
     SOFT_FRAME_RATE_CTRL_ = false;
@@ -99,7 +101,7 @@ void acquisition::Capture::init_variables_register_to_ros() {
     region_of_interest_set_ = false;
     skip_num_ = 20;
     init_delay_ = 1;
-    master_fps_ = 20.0;
+    //master_fps_ = 20.0;
     binning_ = 1;
     SPINNAKER_GET_NEXT_IMAGE_TIMEOUT_ = 2000;
     todays_date_ = todays_date();
@@ -425,6 +427,8 @@ void acquisition::Capture::read_parameters() {
         }
     } else ROS_WARN("  'delay' Parameter not set, using default behavior: delay=%f",init_delay_);
 
+
+    /*
     if (nh_pvt_.getParam("fps", master_fps_)){
         if (master_fps_>=0) ROS_INFO("  Master cam fps set to : %0.2f",master_fps_);
         else {
@@ -433,12 +437,21 @@ void acquisition::Capture::read_parameters() {
         }
     }
         else ROS_WARN("  'fps' Parameter not set, using default behavior: fps=%0.2f",master_fps_);
-
+    */
     if (nh_pvt_.getParam("exposure_time", exposure_time_)){
         if (exposure_time_ >0) ROS_INFO("  Exposure set to: %.1f",exposure_time_);
         else ROS_INFO("  'exposure_time'=%0.f, Setting autoexposure",exposure_time_);
     } else ROS_WARN("  'exposure_time' Parameter not set, using default behavior: Automatic Exposure ");
-
+    if (exposure_time_ == 0) {
+        // if exposure is auto, exposure_time param is 0
+        //  min exposure is usually > 5  fro blackfly s and varies with camera model
+        if (nh_pvt_.getParam("auto_exposure_lower_limit", auto_exposure_lower_limit_)) ROS_INFO("  auto_exposure_lower_limit set to: %.1f",auto_exposure_lower_limit_);
+        else ROS_WARN("  'auto_exposure_lower_limit' Parameter not set, using default behavior: auto_exposure_lower_limit=%.1f",auto_exposure_lower_limit_);
+
+        if (nh_pvt_.getParam("auto_exposure_upper_limit", auto_exposure_upper_limit_)) ROS_INFO("  auto_exposure_upper_limit set to: %.1f",auto_exposure_upper_limit_);
+        else ROS_WARN("  'auto_exposure_upper_limit' Parameter not set, using default behavior: auto_exposure_upper_limit=%.1f",auto_exposure_upper_limit_);
+    }
+            
     if (nh_pvt_.getParam("target_grey_value", target_grey_value_)){
         if (target_grey_value_ >0) ROS_INFO("  target_grey_value set to: %.1f",target_grey_value_);
         else ROS_INFO("  'target_grey_value'=%0.f, Setting AutoExposureTargetGreyValueAuto to Continuous/ auto",target_grey_value_);} 
@@ -658,7 +671,10 @@ void acquisition::Capture::init_cameras(bool soft = false) {
                     cams[i].setFloatValue("ExposureTime", exposure_time_);
                 } else {
                     cams[i].setEnumValue("ExposureAuto", "Continuous");
+                    cams[i].setFloatValue("AutoExposureExposureTimeLowerLimit",auto_exposure_lower_limit_);
+                    cams[i].setFloatValue("AutoExposureExposureTimeUpperLimit",auto_exposure_upper_limit_);
                 }
+
                 if (target_grey_value_ > 4.0) {
                     cams[i].setEnumValue("AutoExposureTargetGreyValueAuto", "Off");
                     cams[i].setFloatValue("AutoExposureTargetGreyValue", target_grey_value_);

From 2e7fccf652b998c4fbb722a16541bd97ae9fda86 Mon Sep 17 00:00:00 2001
From: mithunvarma <diddi.m@husky.neu.edu>
Date: Tue, 24 Sep 2019 17:46:16 -0400
Subject: [PATCH 2/2] update Readme about effect of auto_exposure_limits

---
 README.md                         | 27 +++++++++++++++++++++------
 params/stereo_camera_example.yaml |  4 ++--
 2 files changed, 23 insertions(+), 8 deletions(-)

diff --git a/README.md b/README.md
index 2741a13..76e65da 100644
--- a/README.md
+++ b/README.md
@@ -58,17 +58,32 @@ rqt_image_view
 ## Parmeters
 All the parameters can be set via the launch file or via the yaml config_file.  It is good practice to specify all the 'task' specific parameters via launch file and all the 'system configuration' specific parameters via a config_file.  
 
-### Task Specific Parameters
-* ~binning (int, default: 1)  
-  Binning for cameras, when changing from 2 to 1 cameras need to be unplugged and replugged
-* ~color (bool, default: false)  
-  Should color images be used (only works on models that support color images)
+
+### Auto Exposure Parameters 
 * ~exposure_time (int, default: 0, 0:auto)  
   Exposure setting for cameras, also available as dynamic reconfiguarble parameter.
+
+
+* For auto_exposure mode (~exposure_time=0), following parameters will be used:
+
+
+The upper and lower limits are the time duration for auto exposure algorithm on chip. On spinview software for Blackfly s, default upper limit = 15000 us
+default lower limits = 100 us and acceptable minimum value >= 6 us
+
+
+Setting an higher auto_exposure_upper_limit could potentially slow down the desired fps based on scene. Especially when collecting data greater than 45 fps in auto_exposure it is recommended to have a lower value on auto_exposure_upper_limit.
+
+
 * ~auto_exposure_lower_limit (int, default: 100 us)  
-  Auto Exposure lower limit setting to be used in auto_exposure mode, min:6 for blackfly s model.
+  Auto Exposure lower limit setting to be used in auto_exposure mode, min:6 us for blackfly s model.
 * ~auto_exposure_upper_limit (int, default: 2000 us)  
   Auto Exposure upper limit setting to be used in auto_exposure mode.
+
+### Task Specific Parameters
+* ~binning (int, default: 1)  
+  Binning for cameras, when changing from 2 to 1 cameras need to be unplugged and replugged
+* ~color (bool, default: false)  
+  Should color images be used (only works on models that support color images)
 * ~external_trigger (bool, default: false)  
   Camera triggering setting when using an external trigger.  In this mode, none of the cameras would be set as a master camera. All cameras are setup to use external trigger.  In this mode the main loop runs at rate set by soft_framerate, so if the external trigger rate is higher than the soft_framerate, the buffer will get filled and images will have a lag. Also in this mode, the getnextimage timeout is set to infinite so that the node dosen't die if a trigger is not received for a while.
 * ~target_grey_value (double, default: 0 , 0:Continous/auto)
diff --git a/params/stereo_camera_example.yaml b/params/stereo_camera_example.yaml
index 1368a5c..786fcca 100644
--- a/params/stereo_camera_example.yaml
+++ b/params/stereo_camera_example.yaml
@@ -1,10 +1,10 @@
 cam_ids:
+- 17197555
 - 17197554
-- 17197556
 cam_aliases:
 - cam0
 - cam1
-master_cam: 17197554
+master_cam: 17197555
 skip: 20
 delay: 1.0