From 6deab46b87916b2899cadaa9a8c1764e885941d3 Mon Sep 17 00:00:00 2001 From: Jan Lukas Gernert Date: Thu, 21 Dec 2017 12:59:21 +0100 Subject: [PATCH 01/32] Convert first option to a metricam parameter --- BetaCameras/RealSense2/RealSense2.cs | 47 ++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/BetaCameras/RealSense2/RealSense2.cs b/BetaCameras/RealSense2/RealSense2.cs index 4f521d8b..a3674024 100644 --- a/BetaCameras/RealSense2/RealSense2.cs +++ b/BetaCameras/RealSense2/RealSense2.cs @@ -84,6 +84,53 @@ ParamDesc DepthFPSDesc } } + + //RealSense2API.IsOptionSupported(_pipeline, sensorName, option) + //RealSense2API.GetOption(_pipeline, sensorName, option) + //RealSense2API.SetOption(_pipeline, sensorName, option, value) + //RealSense2API.QueryOptionInfo(_pipeline, sensorName, option, out min, out max, out step, out def, out desc) + + #region RealSense Options + + public bool BacklightCompensation + { + get + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.BACKLIGHT_COMPENSATION)) + throw new Exception("Option 'BacklightCompensation' is not supported by the color sensor of this camera."); + + float res = RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.BACKLIGHT_COMPENSATION); + + if (res == 1.0f) + return true; + + return false; + } + + set + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.BACKLIGHT_COMPENSATION)) + throw new Exception("Option 'BacklightCompensation' is not supported by the color sensor of this camera."); + + RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.BACKLIGHT_COMPENSATION, value ? 1.0f : 0.0f); + } + } + + ParamDesc BacklightCompensationDesc + { + get + { + ParamDesc res = new ParamDesc(); + res.Unit = "Boolean"; + res.Description = "Enable / disable color backlight compensation"; + res.ReadableWhen = ParamDesc.ConnectionStates.Connected; + res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + return res; + } + } + + #endregion + public void Dispose() { Dispose(true); From 378f7ce388fb12829b223308c822eba70d25717d Mon Sep 17 00:00:00 2001 From: Jan Lukas Gernert Date: Thu, 21 Dec 2017 13:34:44 +0100 Subject: [PATCH 02/32] Add brightness option for the color sensor --- BetaCameras/RealSense2/RealSense2.cs | 41 ++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/BetaCameras/RealSense2/RealSense2.cs b/BetaCameras/RealSense2/RealSense2.cs index a3674024..d3db6f61 100644 --- a/BetaCameras/RealSense2/RealSense2.cs +++ b/BetaCameras/RealSense2/RealSense2.cs @@ -129,6 +129,47 @@ ParamDesc BacklightCompensationDesc } } + public int Brightness + { + get + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.BRIGHTNESS)) + throw new Exception("Option 'Brightness' is not supported by the color sensor of this camera."); + + return (int)RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.BRIGHTNESS); + } + + set + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.BRIGHTNESS)) + throw new Exception("Option 'BacklightCompensation' is not supported by the color sensor of this camera."); + + RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.BRIGHTNESS, (float)value); + } + } + + RangeParamDesc BrightnessDesc + { + get + { + RealSense2API.QueryOptionInfo( + _pipeline, + RealSense2API.SensorName.COLOR, + RealSense2API.Option.BRIGHTNESS, + out float min, + out float max, + out float step, + out float def, + out string desc); + + RangeParamDesc res = new RangeParamDesc((int)min, (int)max); + res.Description = "Color image brightness"; + res.ReadableWhen = ParamDesc.ConnectionStates.Connected; + res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + return res; + } + } + #endregion public void Dispose() From 8c70bae3be36cd135140bd3aa9117cef8111c7dd Mon Sep 17 00:00:00 2001 From: Jan Lukas Gernert Date: Thu, 21 Dec 2017 13:38:44 +0100 Subject: [PATCH 03/32] Add contrast option for the color sensor --- BetaCameras/RealSense2/RealSense2.cs | 43 +++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/BetaCameras/RealSense2/RealSense2.cs b/BetaCameras/RealSense2/RealSense2.cs index d3db6f61..492437d9 100644 --- a/BetaCameras/RealSense2/RealSense2.cs +++ b/BetaCameras/RealSense2/RealSense2.cs @@ -142,7 +142,7 @@ public int Brightness set { if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.BRIGHTNESS)) - throw new Exception("Option 'BacklightCompensation' is not supported by the color sensor of this camera."); + throw new Exception("Option 'Brightness' is not supported by the color sensor of this camera."); RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.BRIGHTNESS, (float)value); } @@ -170,6 +170,47 @@ RangeParamDesc BrightnessDesc } } + public int Contrast + { + get + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.CONTRAST)) + throw new Exception("Option 'Contrast' is not supported by the color sensor of this camera."); + + return (int)RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.CONTRAST); + } + + set + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.CONTRAST)) + throw new Exception("Option 'Contrast' is not supported by the color sensor of this camera."); + + RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.CONTRAST, (float)value); + } + } + + RangeParamDesc ContrastDesc + { + get + { + RealSense2API.QueryOptionInfo( + _pipeline, + RealSense2API.SensorName.COLOR, + RealSense2API.Option.BRIGHTNESS, + out float min, + out float max, + out float step, + out float def, + out string desc); + + RangeParamDesc res = new RangeParamDesc((int)min, (int)max); + res.Description = "Color image contrast"; + res.ReadableWhen = ParamDesc.ConnectionStates.Connected; + res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + return res; + } + } + #endregion public void Dispose() From dcc3ecb56a92b14c358e56deedc5ad3f87b8dc97 Mon Sep 17 00:00:00 2001 From: Jan Lukas Gernert Date: Thu, 21 Dec 2017 14:09:25 +0100 Subject: [PATCH 04/32] Add exposure options for color and stereo sensor --- BetaCameras/RealSense2/RealSense2.cs | 173 +++++++++++++++++++++++++-- 1 file changed, 166 insertions(+), 7 deletions(-) diff --git a/BetaCameras/RealSense2/RealSense2.cs b/BetaCameras/RealSense2/RealSense2.cs index 492437d9..e68f5862 100644 --- a/BetaCameras/RealSense2/RealSense2.cs +++ b/BetaCameras/RealSense2/RealSense2.cs @@ -84,12 +84,6 @@ ParamDesc DepthFPSDesc } } - - //RealSense2API.IsOptionSupported(_pipeline, sensorName, option) - //RealSense2API.GetOption(_pipeline, sensorName, option) - //RealSense2API.SetOption(_pipeline, sensorName, option, value) - //RealSense2API.QueryOptionInfo(_pipeline, sensorName, option, out min, out max, out step, out def, out desc) - #region RealSense Options public bool BacklightCompensation @@ -196,7 +190,7 @@ RangeParamDesc ContrastDesc RealSense2API.QueryOptionInfo( _pipeline, RealSense2API.SensorName.COLOR, - RealSense2API.Option.BRIGHTNESS, + RealSense2API.Option.CONTRAST, out float min, out float max, out float step, @@ -211,6 +205,171 @@ RangeParamDesc ContrastDesc } } + public int ExposureColor + { + get + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.EXPOSURE)) + throw new Exception("Option 'Exposure' is not supported by the color sensor of this camera."); + + return (int)RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.EXPOSURE); + } + + set + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.EXPOSURE)) + throw new Exception("Option 'Exposure' is not supported by the color sensor of this camera."); + + RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.EXPOSURE, (float)value); + } + } + + RangeParamDesc ExposureColorDesc + { + get + { + RealSense2API.QueryOptionInfo( + _pipeline, + RealSense2API.SensorName.COLOR, + RealSense2API.Option.EXPOSURE, + out float min, + out float max, + out float step, + out float def, + out string desc); + + RangeParamDesc res = new RangeParamDesc((int)min, (int)max); + res.Description = "Controls exposure time of color camera. Setting any value will disable auto exposure"; + res.ReadableWhen = ParamDesc.ConnectionStates.Connected; + res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + return res; + } + } + + public bool AutoExposureColor + { + get + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.ENABLE_AUTO_EXPOSURE)) + throw new Exception("Option 'AutoExposure' is not supported by the color sensor of this camera."); + + return RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.ENABLE_AUTO_EXPOSURE) == 1.0f ? true : false; + } + + set + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.ENABLE_AUTO_EXPOSURE)) + throw new Exception("Option 'AutoExposure' is not supported by the color sensor of this camera."); + + RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.ENABLE_AUTO_EXPOSURE, value ? 1.0f : 0.0f); + } + } + + ParamDesc AutoExposureColorDesc + { + get + { + ParamDesc res = new ParamDesc(); + res.Description = "Enable / disable color image auto-exposure"; + res.ReadableWhen = ParamDesc.ConnectionStates.Connected; + res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + return res; + } + } + + public int ExposureDepth + { + get + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.EXPOSURE)) + throw new Exception("Option 'Exposure' is not supported by the stereo sensor of this camera."); + + return (int)RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.EXPOSURE); + } + + set + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.EXPOSURE)) + throw new Exception("Option 'Exposure' is not supported by the stereo sensor of this camera."); + + RealSense2API.QueryOptionInfo( + _pipeline, + RealSense2API.SensorName.STEREO, + RealSense2API.Option.EXPOSURE, + out float min, + out float max, + out float step, + out float def, + out string desc); + + + // step size for depth exposure is 20 + float adjusted_value = (float)value; + int rounding = value - (int)min % (int)step; + adjusted_value -= (float)rounding; + + if (rounding > step / 2) + { + adjusted_value += step; + } + + RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.EXPOSURE, adjusted_value); + } + } + + RangeParamDesc ExposureDepthDesc + { + get + { + RealSense2API.QueryOptionInfo( + _pipeline, + RealSense2API.SensorName.STEREO, + RealSense2API.Option.EXPOSURE, + out float min, + out float max, + out float step, + out float def, + out string desc); + + RangeParamDesc res = new RangeParamDesc((int)min, (int)max); + res.Description = "Controls exposure time of depth camera. Setting any value will disable auto exposure"; + res.ReadableWhen = ParamDesc.ConnectionStates.Connected; + res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + return res; + } + } + + public bool AutoExposureDepth + { + get + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.ENABLE_AUTO_EXPOSURE)) + throw new Exception("Option 'AutoExposure' is not supported by the stereo sensor of this camera."); + + return RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.ENABLE_AUTO_EXPOSURE) == 1.0f ? true : false; + } + + set + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.ENABLE_AUTO_EXPOSURE)) + throw new Exception("Option 'AutoExposure' is not supported by the stereo sensor of this camera."); + + RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.ENABLE_AUTO_EXPOSURE, value ? 1.0f : 0.0f); + } + } + + ParamDesc AutoExposureDepthDesc + { + get + { + ParamDesc res = new ParamDesc(); + res.Description = "Enable / disable depth image auto-exposure"; + res.ReadableWhen = ParamDesc.ConnectionStates.Connected; + res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + return res; + } + } + #endregion public void Dispose() From 089f59446a2d2215146a8f3a30d5c829f53fad96 Mon Sep 17 00:00:00 2001 From: Jan Lukas Gernert Date: Thu, 21 Dec 2017 15:01:02 +0100 Subject: [PATCH 05/32] Add gain options for color and stereo sensor --- BetaCameras/RealSense2/RealSense2.cs | 82 ++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/BetaCameras/RealSense2/RealSense2.cs b/BetaCameras/RealSense2/RealSense2.cs index e68f5862..e647a4be 100644 --- a/BetaCameras/RealSense2/RealSense2.cs +++ b/BetaCameras/RealSense2/RealSense2.cs @@ -370,6 +370,88 @@ ParamDesc AutoExposureDepthDesc } } + public int GainColor + { + get + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.GAIN)) + throw new Exception("Option 'Gain' is not supported by the color sensor of this camera."); + + return (int)RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.GAIN); + } + + set + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.GAIN)) + throw new Exception("Option 'Gain' is not supported by the color sensor of this camera."); + + RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.GAIN, (float)value); + } + } + + RangeParamDesc GainColorDesc + { + get + { + RealSense2API.QueryOptionInfo( + _pipeline, + RealSense2API.SensorName.COLOR, + RealSense2API.Option.GAIN, + out float min, + out float max, + out float step, + out float def, + out string desc); + + RangeParamDesc res = new RangeParamDesc((int)min, (int)max); + res.Description = "Color image gain"; + res.ReadableWhen = ParamDesc.ConnectionStates.Connected; + res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + return res; + } + } + + public int GainDepth + { + get + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.GAIN)) + throw new Exception("Option 'Gain' is not supported by the stereo sensor of this camera."); + + return (int)RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.GAIN); + } + + set + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.GAIN)) + throw new Exception("Option 'Gain' is not supported by the stereo sensor of this camera."); + + RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.GAIN, (float)value); + } + } + + RangeParamDesc GainDepthDesc + { + get + { + RealSense2API.QueryOptionInfo( + _pipeline, + RealSense2API.SensorName.STEREO, + RealSense2API.Option.GAIN, + out float min, + out float max, + out float step, + out float def, + out string desc); + + RangeParamDesc res = new RangeParamDesc((int)min, (int)max); + res.Description = "Depth image gain"; + res.ReadableWhen = ParamDesc.ConnectionStates.Connected; + res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + return res; + } + } + #endregion public void Dispose() From bc6f9a9c7a7d7199b9004485aaa79f2938e1fcad Mon Sep 17 00:00:00 2001 From: Jan Lukas Gernert Date: Thu, 21 Dec 2017 15:33:11 +0100 Subject: [PATCH 06/32] Add auto exposure priority and gain options for color and stereo sensor --- BetaCameras/RealSense2/RealSense2.cs | 72 ++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/BetaCameras/RealSense2/RealSense2.cs b/BetaCameras/RealSense2/RealSense2.cs index e647a4be..7c1e518b 100644 --- a/BetaCameras/RealSense2/RealSense2.cs +++ b/BetaCameras/RealSense2/RealSense2.cs @@ -277,6 +277,37 @@ ParamDesc AutoExposureColorDesc } } + public bool AutoExposurePriorityColor + { + get + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.AUTO_EXPOSURE_PRIORITY)) + throw new Exception("Option 'AutoExposurePriority' is not supported by the color sensor of this camera."); + + return RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.AUTO_EXPOSURE_PRIORITY) == 1.0f ? true : false; + } + + set + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.AUTO_EXPOSURE_PRIORITY)) + throw new Exception("Option 'AutoExposurePriority' is not supported by the color sensor of this camera."); + + RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.AUTO_EXPOSURE_PRIORITY, value ? 1.0f : 0.0f); + } + } + + ParamDesc AutoExposurePriorityColorDesc + { + get + { + ParamDesc res = new ParamDesc(); + res.Description = "Limit exposure time when auto-exposure is ON to preserve constant fps rate"; + res.ReadableWhen = ParamDesc.ConnectionStates.Connected; + res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + return res; + } + } + public int ExposureDepth { get @@ -452,6 +483,47 @@ RangeParamDesc GainDepthDesc } } + public int Gamma + { + get + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.GAMMA)) + throw new Exception("Option 'Gamma' is not supported by the color sensor of this camera."); + + return (int)RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.GAMMA); + } + + set + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.GAMMA)) + throw new Exception("Option 'Gamma' is not supported by the color sensor of this camera."); + + RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.GAMMA, (float)value); + } + } + + RangeParamDesc GammaDesc + { + get + { + RealSense2API.QueryOptionInfo( + _pipeline, + RealSense2API.SensorName.COLOR, + RealSense2API.Option.GAMMA, + out float min, + out float max, + out float step, + out float def, + out string desc); + + RangeParamDesc res = new RangeParamDesc((int)min, (int)max); + res.Description = "Color image Gamma"; + res.ReadableWhen = ParamDesc.ConnectionStates.Connected; + res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + return res; + } + } + #endregion public void Dispose() From bb8bbe7a0397c4b94059f455f7d206bd2ffe459d Mon Sep 17 00:00:00 2001 From: Jan Lukas Gernert Date: Thu, 21 Dec 2017 15:42:06 +0100 Subject: [PATCH 07/32] add hue, saturation and sharpness properties for color sensor --- BetaCameras/RealSense2/RealSense2.cs | 123 +++++++++++++++++++++++++++ 1 file changed, 123 insertions(+) diff --git a/BetaCameras/RealSense2/RealSense2.cs b/BetaCameras/RealSense2/RealSense2.cs index 7c1e518b..b42454e8 100644 --- a/BetaCameras/RealSense2/RealSense2.cs +++ b/BetaCameras/RealSense2/RealSense2.cs @@ -524,6 +524,129 @@ RangeParamDesc GammaDesc } } + public int Hue + { + get + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.HUE)) + throw new Exception("Option 'Hue' is not supported by the color sensor of this camera."); + + return (int)RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.HUE); + } + + set + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.HUE)) + throw new Exception("Option 'Hue' is not supported by the color sensor of this camera."); + + RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.HUE, (float)value); + } + } + + RangeParamDesc HueDesc + { + get + { + RealSense2API.QueryOptionInfo( + _pipeline, + RealSense2API.SensorName.COLOR, + RealSense2API.Option.HUE, + out float min, + out float max, + out float step, + out float def, + out string desc); + + RangeParamDesc res = new RangeParamDesc((int)min, (int)max); + res.Description = "Color image Hue"; + res.ReadableWhen = ParamDesc.ConnectionStates.Connected; + res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + return res; + } + } + + public int Saturation + { + get + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.SATURATION)) + throw new Exception("Option 'Saturation' is not supported by the color sensor of this camera."); + + return (int)RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.SATURATION); + } + + set + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.SATURATION)) + throw new Exception("Option 'Saturation' is not supported by the color sensor of this camera."); + + RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.SATURATION, (float)value); + } + } + + RangeParamDesc SaturationDesc + { + get + { + RealSense2API.QueryOptionInfo( + _pipeline, + RealSense2API.SensorName.COLOR, + RealSense2API.Option.SATURATION, + out float min, + out float max, + out float step, + out float def, + out string desc); + + RangeParamDesc res = new RangeParamDesc((int)min, (int)max); + res.Description = "Color image Saturation"; + res.ReadableWhen = ParamDesc.ConnectionStates.Connected; + res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + return res; + } + } + + public int Sharpness + { + get + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.SHARPNESS)) + throw new Exception("Option 'Sharpness' is not supported by the color sensor of this camera."); + + return (int)RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.SHARPNESS); + } + + set + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.SHARPNESS)) + throw new Exception("Option 'Sharpness' is not supported by the color sensor of this camera."); + + RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.SHARPNESS, (float)value); + } + } + + RangeParamDesc SharpnessDesc + { + get + { + RealSense2API.QueryOptionInfo( + _pipeline, + RealSense2API.SensorName.COLOR, + RealSense2API.Option.SHARPNESS, + out float min, + out float max, + out float step, + out float def, + out string desc); + + RangeParamDesc res = new RangeParamDesc((int)min, (int)max); + res.Description = "Color image Sharpness"; + res.ReadableWhen = ParamDesc.ConnectionStates.Connected; + res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + return res; + } + } + #endregion public void Dispose() From a5344d9416e64ba4467058499a5da421a503832e Mon Sep 17 00:00:00 2001 From: Jan Lukas Gernert Date: Thu, 21 Dec 2017 16:19:58 +0100 Subject: [PATCH 08/32] whitebalance and laser options to property --- BetaCameras/RealSense2/RealSense2.cs | 198 +++++++++++++++++++++++++++ 1 file changed, 198 insertions(+) diff --git a/BetaCameras/RealSense2/RealSense2.cs b/BetaCameras/RealSense2/RealSense2.cs index b42454e8..ab029df3 100644 --- a/BetaCameras/RealSense2/RealSense2.cs +++ b/BetaCameras/RealSense2/RealSense2.cs @@ -24,6 +24,13 @@ public class RealSense2 : Camera, IDisposable private float _depthScale = 0.0f; private bool _disposed = false; + public enum EmitterMode + { + OFF = 0, + ON = 1, + AUTO = 2 + } + public Point2i ColorResolution { get; set; } = new Point2i(640, 480); ParamDesc ColorResolutionDesc @@ -647,6 +654,197 @@ RangeParamDesc SharpnessDesc } } + public int WhiteBalance + { + get + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.WHITE_BALANCE)) + throw new Exception("Option 'WhiteBalance' is not supported by the color sensor of this camera."); + + return (int)RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.WHITE_BALANCE); + } + + set + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.WHITE_BALANCE)) + throw new Exception("Option 'WhiteBalance' is not supported by the color sensor of this camera."); + + RealSense2API.QueryOptionInfo( + _pipeline, + RealSense2API.SensorName.COLOR, + RealSense2API.Option.WHITE_BALANCE, + out float min, + out float max, + out float step, + out float def, + out string desc); + + + // step size for depth white balance is 10 + float adjusted_value = (float)value; + int rounding = value - (int)min % (int)step; + adjusted_value -= (float)rounding; + + if (rounding > step / 2) + { + adjusted_value += step; + } + + RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.WHITE_BALANCE, adjusted_value); + } + } + + RangeParamDesc WhiteBalanceDesc + { + get + { + RealSense2API.QueryOptionInfo( + _pipeline, + RealSense2API.SensorName.COLOR, + RealSense2API.Option.WHITE_BALANCE, + out float min, + out float max, + out float step, + out float def, + out string desc); + + RangeParamDesc res = new RangeParamDesc((int)min, (int)max); + res.Description = "Controls white balance of color image.Setting any value will disable auto white balance"; + res.ReadableWhen = ParamDesc.ConnectionStates.Connected; + res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + return res; + } + } + + public bool AutoWhiteBalance + { + get + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.ENABLE_AUTO_WHITE_BALANCE)) + throw new Exception("Option 'AutoWhiteBalance' is not supported by the color sensor of this camera."); + + return RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.ENABLE_AUTO_WHITE_BALANCE) == 1.0f ? true : false; + } + + set + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.ENABLE_AUTO_WHITE_BALANCE)) + throw new Exception("Option 'AutoWhiteBalance' is not supported by the color sensor of this camera."); + + RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.ENABLE_AUTO_WHITE_BALANCE, value ? 1.0f : 0.0f); + } + } + + ParamDesc AutoWhiteBalanceDesc + { + get + { + ParamDesc res = new ParamDesc(); + res.Description = "Enable / disable auto-white-balance"; + res.ReadableWhen = ParamDesc.ConnectionStates.Connected; + res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + return res; + } + } + + public int LaserPower + { + get + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.LASER_POWER)) + throw new Exception("Option 'LaserPower' is not supported by the stereo sensor of this camera."); + + return (int)RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.LASER_POWER); + } + + set + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.LASER_POWER)) + throw new Exception("Option 'LaserPower' is not supported by the stereo sensor of this camera."); + + RealSense2API.QueryOptionInfo( + _pipeline, + RealSense2API.SensorName.STEREO, + RealSense2API.Option.LASER_POWER, + out float min, + out float max, + out float step, + out float def, + out string desc); + + + // step size for depth laser power is 30 + float adjusted_value = (float)value; + int rounding = value - (int)min % (int)step; + adjusted_value -= (float)rounding; + + if (rounding > step / 2) + { + adjusted_value += step; + } + + RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.LASER_POWER, adjusted_value); + } + } + + RangeParamDesc LaserPowerDesc + { + get + { + RealSense2API.QueryOptionInfo( + _pipeline, + RealSense2API.SensorName.STEREO, + RealSense2API.Option.LASER_POWER, + out float min, + out float max, + out float step, + out float def, + out string desc); + + RangeParamDesc res = new RangeParamDesc((int)min, (int)max); + res.Description = "Manual laser power in mw. applicable only when laser power mode is set to Manual"; + res.ReadableWhen = ParamDesc.ConnectionStates.Connected; + res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + return res; + } + } + + //"Power of the DS5 projector, 0 meaning projector off, 1 meaning projector on, 2 meaning projector in auto mode" + + public EmitterMode LaserMode + { + get + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.EMITTER_ENABLED)) + throw new Exception("Option 'LaserMode' is not supported by the stereo sensor of this camera."); + + return (EmitterMode)RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.EMITTER_ENABLED); + } + + set + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.EMITTER_ENABLED)) + throw new Exception("Option 'LaserMode' is not supported by the stereo sensor of this camera."); + + RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.LASER_POWER, (float)value); + } + } + + ParamDesc EmmiterModeDesc + { + get + { + ParamDesc res = new ParamDesc() + { + Description = "Power of the DS5 projector", + ReadableWhen = ParamDesc.ConnectionStates.Connected, + WritableWhen = ParamDesc.ConnectionStates.Connected, + }; + + return res; + } + } + #endregion public void Dispose() From e56a72a98bfa7927e4a1bdebc8cdafbb264eb82b Mon Sep 17 00:00:00 2001 From: Jan Lukas Gernert Date: Thu, 21 Dec 2017 17:12:33 +0100 Subject: [PATCH 09/32] more --- BetaCameras/RealSense2/RealSense2.cs | 242 +++++++++++++++++++++++- BetaCameras/RealSense2/RealSense2API.cs | 11 ++ 2 files changed, 246 insertions(+), 7 deletions(-) diff --git a/BetaCameras/RealSense2/RealSense2.cs b/BetaCameras/RealSense2/RealSense2.cs index ab029df3..2562ae56 100644 --- a/BetaCameras/RealSense2/RealSense2.cs +++ b/BetaCameras/RealSense2/RealSense2.cs @@ -31,6 +31,14 @@ public enum EmitterMode AUTO = 2 } + public enum PowerLineMode + { + OFF = 0, + FIFTY_HZ = 1, + SIXTY_HZ = 2, + AUTO = 3 + } + public Point2i ColorResolution { get; set; } = new Point2i(640, 480); ParamDesc ColorResolutionDesc @@ -91,8 +99,37 @@ ParamDesc DepthFPSDesc } } + + public string Firmware + { + get + { + return RealSense2API.GetFirmwareVersion(_pipeline); + } + } + + ParamDesc FirmwareDesc + { + get + { + ParamDesc res = new ParamDesc(); + res.Description = "Current Firmware version of the connected camera."; + res.ReadableWhen = ParamDesc.ConnectionStates.Connected; + return res; + } + } + #region RealSense Options + // Not implemented options: + // - VISUAL_PRESET (functionallity provided by profiles built into metricam) + // - ACCURACY + // - MOTION_RANGE + // - FILTER_OPTION + // - CONFIDENCE_THRESHOLD + // - TOTAL_FRAME_DROPS + // - AUTO_EXPOSURE_MODE + public bool BacklightCompensation { get @@ -809,8 +846,6 @@ RangeParamDesc LaserPowerDesc } } - //"Power of the DS5 projector, 0 meaning projector off, 1 meaning projector on, 2 meaning projector in auto mode" - public EmitterMode LaserMode { get @@ -845,6 +880,199 @@ ParamDesc EmmiterModeDesc } } + public int FrameQueueSizeColor + { + get + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.FRAMES_QUEUE_SIZE)) + throw new Exception("Option 'FrameQueueSize' is not supported by the color sensor of this camera."); + + return (int)RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.FRAMES_QUEUE_SIZE); + } + + set + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.FRAMES_QUEUE_SIZE)) + throw new Exception("Option 'FrameQueueSize' is not supported by the color sensor of this camera."); + + RealSense2API.QueryOptionInfo( + _pipeline, + RealSense2API.SensorName.COLOR, + RealSense2API.Option.FRAMES_QUEUE_SIZE, + out float min, + out float max, + out float step, + out float def, + out string desc); + + + RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.FRAMES_QUEUE_SIZE, (float)value); + } + } + + RangeParamDesc FrameQueueSizeColorDesc + { + get + { + RealSense2API.QueryOptionInfo( + _pipeline, + RealSense2API.SensorName.COLOR, + RealSense2API.Option.FRAMES_QUEUE_SIZE, + out float min, + out float max, + out float step, + out float def, + out string desc); + + RangeParamDesc res = new RangeParamDesc((int)min, (int)max); + res.Description = "Max number of frames you can hold at a given time. Increasing this number will reduce frame drops but increase latency, and vice versa"; + res.ReadableWhen = ParamDesc.ConnectionStates.Connected; + res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + return res; + } + } + + public int FrameQueueSizeDepth + { + get + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.FRAMES_QUEUE_SIZE)) + throw new Exception("Option 'FrameQueueSize' is not supported by the stereo sensor of this camera."); + + return (int)RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.FRAMES_QUEUE_SIZE); + } + + set + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.FRAMES_QUEUE_SIZE)) + throw new Exception("Option 'FrameQueueSize' is not supported by the stereo sensor of this camera."); + + RealSense2API.QueryOptionInfo( + _pipeline, + RealSense2API.SensorName.STEREO, + RealSense2API.Option.FRAMES_QUEUE_SIZE, + out float min, + out float max, + out float step, + out float def, + out string desc); + + + RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.FRAMES_QUEUE_SIZE, (float)value); + } + } + + RangeParamDesc FrameQueueSizeDepthDesc + { + get + { + RealSense2API.QueryOptionInfo( + _pipeline, + RealSense2API.SensorName.STEREO, + RealSense2API.Option.FRAMES_QUEUE_SIZE, + out float min, + out float max, + out float step, + out float def, + out string desc); + + RangeParamDesc res = new RangeParamDesc((int)min, (int)max); + res.Description = "Max number of frames you can hold at a given time. Increasing this number will reduce frame drops but increase latency, and vice versa"; + res.ReadableWhen = ParamDesc.ConnectionStates.Connected; + res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + return res; + } + } + + public PowerLineMode PowerFrequencyMode + { + get + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.POWER_LINE_FREQUENCY)) + throw new Exception("Option 'PowerFrequencyMode' is not supported by the color sensor of this camera."); + + return (PowerLineMode)RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.POWER_LINE_FREQUENCY); + } + + set + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.POWER_LINE_FREQUENCY)) + throw new Exception("Option 'PowerFrequencyMode' is not supported by the color sensor of this camera."); + + RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.POWER_LINE_FREQUENCY, (float)value); + } + } + + ParamDesc PowerFrequencyModeDesc + { + get + { + ParamDesc res = new ParamDesc() + { + Description = "Power Line Frequency control for anti-flickering Off/50Hz/60Hz/Auto", + ReadableWhen = ParamDesc.ConnectionStates.Connected, + WritableWhen = ParamDesc.ConnectionStates.Connected, + }; + + return res; + } + } + + public float ASICTemp + { + get + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.ASIC_TEMPERATURE)) + throw new Exception("Option 'ASICTemp' is not supported by the stereo sensor of this camera."); + + return (int)RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.ASIC_TEMPERATURE); + } + } + + ParamDesc ASICTempDesc + { + get + { + ParamDesc res = new ParamDesc(); + res.Unit = "Degree Celsius °C"; + res.Description = "Current Asic Temperature"; + res.ReadableWhen = ParamDesc.ConnectionStates.Connected; + res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + return res; + } + } + + public bool EnableErrorPolling + { + get + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.ERROR_POLLING_ENABLED)) + throw new Exception("Option 'ErrorPolling' is not supported by the stereo sensor of this camera."); + + return RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.ERROR_POLLING_ENABLED) == 1.0f ? true : false; + } + + set + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.ERROR_POLLING_ENABLED)) + throw new Exception("Option 'ErrorPolling' is not supported by the stereo sensor of this camera."); + + RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.ERROR_POLLING_ENABLED, value ? 1.0f : 0.0f); + } + } + + ParamDesc EnableErrorPollingDesc + { + get + { + ParamDesc res = new ParamDesc(); + res.Description = "Enable / disable polling of camera internal errors"; + res.ReadableWhen = ParamDesc.ConnectionStates.Connected; + res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + return res; + } + } + #endregion public void Dispose() @@ -1355,11 +1583,6 @@ unsafe private ColorCameraImage CalcColor() #endregion - public string GetFirmware() - { - return RealSense2API.GetFirmwareVersion(_pipeline); - } - public void LoadConfigPreset(AdvancedMode.Preset preset) { LoadCustomConfig(AdvancedMode.GetPreset(preset)); @@ -1392,5 +1615,10 @@ public void QueryOptionInfo(RealSense2API.Option option, string sensorName, out { RealSense2API.QueryOptionInfo(_pipeline, sensorName, option, out min, out max, out step, out def, out desc); } + + public string OptionValueInfo(RealSense2API.Option option, string sensorName, float value) + { + return RealSense2API.OptionValueInfo(_pipeline, sensorName, option, value); + } } } diff --git a/BetaCameras/RealSense2/RealSense2API.cs b/BetaCameras/RealSense2/RealSense2API.cs index 97ca4327..a6b41576 100644 --- a/BetaCameras/RealSense2/RealSense2API.cs +++ b/BetaCameras/RealSense2/RealSense2API.cs @@ -1038,6 +1038,17 @@ unsafe public static void QueryOptionInfo(RS2Pipeline pipe, string SensorName, O sensor.Delete(); } + unsafe public static string OptionValueInfo(RS2Pipeline pipe, string SensorName, Option option, float value) + { + IntPtr error = IntPtr.Zero; + char* msg = null; + + RS2Sensor sensor = GetSensor(pipe, SensorName); + msg = rs2_get_option_value_description(sensor.Handle, option, value, &error); + + return Marshal.PtrToStringAnsi((IntPtr)msg); + } + unsafe private static void HandleError(IntPtr e) From 55fcdfa9acf946f40807dad3c0ab1c54836533df Mon Sep 17 00:00:00 2001 From: Jan Lukas Gernert Date: Thu, 21 Dec 2017 17:47:05 +0100 Subject: [PATCH 10/32] more --- BetaCameras/RealSense2/RealSense2.cs | 104 ++++++++++++++++++++++++++- 1 file changed, 103 insertions(+), 1 deletion(-) diff --git a/BetaCameras/RealSense2/RealSense2.cs b/BetaCameras/RealSense2/RealSense2.cs index 2562ae56..c1d64b48 100644 --- a/BetaCameras/RealSense2/RealSense2.cs +++ b/BetaCameras/RealSense2/RealSense2.cs @@ -129,6 +129,7 @@ ParamDesc FirmwareDesc // - CONFIDENCE_THRESHOLD // - TOTAL_FRAME_DROPS // - AUTO_EXPOSURE_MODE + // - MOTION_MODULE_TEMPERATURE public bool BacklightCompensation { @@ -1025,7 +1026,7 @@ public float ASICTemp if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.ASIC_TEMPERATURE)) throw new Exception("Option 'ASICTemp' is not supported by the stereo sensor of this camera."); - return (int)RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.ASIC_TEMPERATURE); + return RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.ASIC_TEMPERATURE); } } @@ -1073,6 +1074,102 @@ ParamDesc EnableErrorPollingDesc } } + public float ProjectorTemp + { + get + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.PROJECTOR_TEMPERATURE)) + throw new Exception("Option 'ProjectorTemp' is not supported by the stereo sensor of this camera."); + + return RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.PROJECTOR_TEMPERATURE); + } + } + + ParamDesc ProjectorTempDesc + { + get + { + ParamDesc res = new ParamDesc(); + res.Unit = "Degree Celsius °C"; + res.Description = "Current Projector Temperature"; + res.ReadableWhen = ParamDesc.ConnectionStates.Connected; + res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + return res; + } + } + + public bool OutputTrigger + { + get + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.OUTPUT_TRIGGER_ENABLED)) + throw new Exception("Option 'OutputTrigger' is not supported by the stereo sensor of this camera."); + + return RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.OUTPUT_TRIGGER_ENABLED) == 1.0f ? true : false; + } + + set + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.OUTPUT_TRIGGER_ENABLED)) + throw new Exception("Option 'OutputTrigger' is not supported by the stereo sensor of this camera."); + + RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.OUTPUT_TRIGGER_ENABLED, value ? 1.0f : 0.0f); + } + } + + ParamDesc OutputTriggerDesc + { + get + { + ParamDesc res = new ParamDesc(); + res.Description = "Enable / disable trigger to be outputed from the camera to any external device on every depth frame"; + res.ReadableWhen = ParamDesc.ConnectionStates.Connected; + res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + return res; + } + } + + public float DepthUnits + { + get + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.DEPTH_UNITS)) + throw new Exception("Option 'DepthUnits' is not supported by the stereo sensor of this camera."); + + return RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.DEPTH_UNITS); + } + + set + { + if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.DEPTH_UNITS)) + throw new Exception("Option 'DepthUnits' is not supported by the stereo sensor of this camera."); + + RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.DEPTH_UNITS, value); + } + } + + RangeParamDesc DepthUnitsDesc + { + get + { + RealSense2API.QueryOptionInfo( + _pipeline, + RealSense2API.SensorName.STEREO, + RealSense2API.Option.DEPTH_UNITS, + out float min, + out float max, + out float step, + out float def, + out string desc); + + RangeParamDesc res = new RangeParamDesc(min, max); + res.Description = "Number of meters represented by a single depth unit"; + res.ReadableWhen = ParamDesc.ConnectionStates.Connected; + res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + return res; + } + } + #endregion public void Dispose() @@ -1601,6 +1698,11 @@ public bool IsOptionSupported(RealSense2API.Option option, string sensorName) return RealSense2API.IsOptionSupported(_pipeline, sensorName, option); } + public bool IsOptionRealOnly(RealSense2API.Option option, string sensorName) + { + return RealSense2API.IsOptionRealOnly(_pipeline, sensorName, option); + } + public float GetOption(RealSense2API.Option option, string sensorName) { return RealSense2API.GetOption(_pipeline, sensorName, option); From 19124f6c3163626ab5521970fcc4b5107631955e Mon Sep 17 00:00:00 2001 From: Jan Lukas Gernert Date: Thu, 21 Dec 2017 17:59:05 +0100 Subject: [PATCH 11/32] checked all options for compatibility with D435 --- BetaCameras/RealSense2/RealSense2.cs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/BetaCameras/RealSense2/RealSense2.cs b/BetaCameras/RealSense2/RealSense2.cs index c1d64b48..79b31c4b 100644 --- a/BetaCameras/RealSense2/RealSense2.cs +++ b/BetaCameras/RealSense2/RealSense2.cs @@ -121,7 +121,7 @@ ParamDesc FirmwareDesc #region RealSense Options - // Not implemented options: + // Not implemented options (mostly because not supported by D435): // - VISUAL_PRESET (functionallity provided by profiles built into metricam) // - ACCURACY // - MOTION_RANGE @@ -130,6 +130,15 @@ ParamDesc FirmwareDesc // - TOTAL_FRAME_DROPS // - AUTO_EXPOSURE_MODE // - MOTION_MODULE_TEMPERATURE + // - ENABLE_MOTION_CORRECTION + // - COLOR_SCHEME + // - HISTOGRAM_EQUALIZATION_ENABLED + // - MIN_DISTANCE + // - MAX_DISTANCE + // - TEXTURE_SOURCE + // - FILTER_MAGNITUDE + // - FILTER_SMOOTH_ALPHA + // - FILTER_SMOOTH_DELTA public bool BacklightCompensation { From 6842ed8acc7cdf95740182739e98a0872ea31de2 Mon Sep 17 00:00:00 2001 From: Jan Lukas Gernert Date: Fri, 22 Dec 2017 15:28:11 +0100 Subject: [PATCH 12/32] add summary comments to enable tool tips --- BetaCameras/RealSense2/RealSense2.cs | 82 ++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/BetaCameras/RealSense2/RealSense2.cs b/BetaCameras/RealSense2/RealSense2.cs index 79b31c4b..31f4a415 100644 --- a/BetaCameras/RealSense2/RealSense2.cs +++ b/BetaCameras/RealSense2/RealSense2.cs @@ -140,6 +140,9 @@ ParamDesc FirmwareDesc // - FILTER_SMOOTH_ALPHA // - FILTER_SMOOTH_DELTA + /// + /// Enable / disable color backlight compensation + /// public bool BacklightCompensation { get @@ -177,6 +180,9 @@ ParamDesc BacklightCompensationDesc } } + /// + /// Color image brightness + /// public int Brightness { get @@ -218,6 +224,9 @@ RangeParamDesc BrightnessDesc } } + /// + /// Color image contrast + /// public int Contrast { get @@ -259,6 +268,9 @@ RangeParamDesc ContrastDesc } } + /// + /// Controls exposure time of color camera. Setting any value will disable auto exposure + /// public int ExposureColor { get @@ -300,6 +312,9 @@ RangeParamDesc ExposureColorDesc } } + /// + /// Enable / disable color image auto-exposure + /// public bool AutoExposureColor { get @@ -331,6 +346,9 @@ ParamDesc AutoExposureColorDesc } } + /// + /// Limit exposure time when auto-exposure is ON to preserve constant fps rate + /// public bool AutoExposurePriorityColor { get @@ -362,6 +380,9 @@ ParamDesc AutoExposurePriorityColorDesc } } + /// + /// Controls exposure time of depth camera. Setting any value will disable auto exposure + /// public int ExposureDepth { get @@ -424,6 +445,9 @@ RangeParamDesc ExposureDepthDesc } } + /// + /// Enable / disable depth image auto-exposure + /// public bool AutoExposureDepth { get @@ -455,6 +479,9 @@ ParamDesc AutoExposureDepthDesc } } + /// + /// Color image gain + /// public int GainColor { get @@ -496,6 +523,9 @@ RangeParamDesc GainColorDesc } } + /// + /// Depth image gain + /// public int GainDepth { get @@ -537,6 +567,9 @@ RangeParamDesc GainDepthDesc } } + /// + /// Color image Gamma + /// public int Gamma { get @@ -578,6 +611,9 @@ RangeParamDesc GammaDesc } } + /// + /// Color image Hue + /// public int Hue { get @@ -619,6 +655,9 @@ RangeParamDesc HueDesc } } + /// + /// Color image Saturation + /// public int Saturation { get @@ -660,6 +699,9 @@ RangeParamDesc SaturationDesc } } + /// + /// Color image Sharpness + /// public int Sharpness { get @@ -701,6 +743,9 @@ RangeParamDesc SharpnessDesc } } + /// + /// Controls white balance of color image.Setting any value will disable auto white balance + /// public int WhiteBalance { get @@ -763,6 +808,9 @@ RangeParamDesc WhiteBalanceDesc } } + /// + /// Enable / disable auto-white-balance + /// public bool AutoWhiteBalance { get @@ -794,6 +842,9 @@ ParamDesc AutoWhiteBalanceDesc } } + /// + /// Manual laser power in mw. applicable only when laser power mode is set to Manual + /// public int LaserPower { get @@ -856,6 +907,9 @@ RangeParamDesc LaserPowerDesc } } + /// + /// Power of the DS5 projector + /// public EmitterMode LaserMode { get @@ -890,6 +944,9 @@ ParamDesc EmmiterModeDesc } } + /// + /// Max number of frames you can hold at a given time. Increasing this number will reduce frame drops but increase latency, and vice versa + /// public int FrameQueueSizeColor { get @@ -942,6 +999,9 @@ RangeParamDesc FrameQueueSizeColorDesc } } + /// + /// Max number of frames you can hold at a given time. Increasing this number will reduce frame drops but increase latency, and vice versa + /// public int FrameQueueSizeDepth { get @@ -994,6 +1054,9 @@ RangeParamDesc FrameQueueSizeDepthDesc } } + /// + /// Power Line Frequency control for anti-flickering Off/50Hz/60Hz/Auto + /// public PowerLineMode PowerFrequencyMode { get @@ -1028,6 +1091,9 @@ ParamDesc PowerFrequencyModeDesc } } + /// + /// Current Asic Temperature + /// public float ASICTemp { get @@ -1052,6 +1118,10 @@ ParamDesc ASICTempDesc } } + + /// + /// Enable / disable polling of camera internal errors + /// public bool EnableErrorPolling { get @@ -1083,6 +1153,10 @@ ParamDesc EnableErrorPollingDesc } } + + /// + /// Current Projector Temperature in °C + /// public float ProjectorTemp { get @@ -1107,6 +1181,10 @@ ParamDesc ProjectorTempDesc } } + + /// + /// Enable / disable trigger to be outputed from the camera to any external device on every depth frame + /// public bool OutputTrigger { get @@ -1138,6 +1216,10 @@ ParamDesc OutputTriggerDesc } } + + /// + /// Number of meters represented by a single depth unit + /// public float DepthUnits { get From 35cca8b83dc34c3a53d1cb251c83ce66b978f6d1 Mon Sep 17 00:00:00 2001 From: Jan Lukas Gernert Date: Fri, 22 Dec 2017 15:32:02 +0100 Subject: [PATCH 13/32] remove direct access to realsense option APIs and enums --- BetaCameras/RealSense2/RealSense2.cs | 30 ------------------------- BetaCameras/RealSense2/RealSense2API.cs | 2 +- 2 files changed, 1 insertion(+), 31 deletions(-) diff --git a/BetaCameras/RealSense2/RealSense2.cs b/BetaCameras/RealSense2/RealSense2.cs index 31f4a415..09ff4ed7 100644 --- a/BetaCameras/RealSense2/RealSense2.cs +++ b/BetaCameras/RealSense2/RealSense2.cs @@ -1783,35 +1783,5 @@ public void LoadCustomConfig(string json) //RealSense2API.DeleteDevice(dev); _depthScale = RealSense2API.GetDepthScale(_pipeline); } - - public bool IsOptionSupported(RealSense2API.Option option, string sensorName) - { - return RealSense2API.IsOptionSupported(_pipeline, sensorName, option); - } - - public bool IsOptionRealOnly(RealSense2API.Option option, string sensorName) - { - return RealSense2API.IsOptionRealOnly(_pipeline, sensorName, option); - } - - public float GetOption(RealSense2API.Option option, string sensorName) - { - return RealSense2API.GetOption(_pipeline, sensorName, option); - } - - public void SetOption(RealSense2API.Option option, string sensorName, float value) - { - RealSense2API.SetOption(_pipeline, sensorName, option, value); - } - - public void QueryOptionInfo(RealSense2API.Option option, string sensorName, out float min, out float max, out float step, out float def, out string desc) - { - RealSense2API.QueryOptionInfo(_pipeline, sensorName, option, out min, out max, out step, out def, out desc); - } - - public string OptionValueInfo(RealSense2API.Option option, string sensorName, float value) - { - return RealSense2API.OptionValueInfo(_pipeline, sensorName, option, value); - } } } diff --git a/BetaCameras/RealSense2/RealSense2API.cs b/BetaCameras/RealSense2/RealSense2API.cs index a6b41576..502b991c 100644 --- a/BetaCameras/RealSense2/RealSense2API.cs +++ b/BetaCameras/RealSense2/RealSense2API.cs @@ -4,7 +4,7 @@ namespace MetriCam2.Cameras { - public class RealSense2API + internal class RealSense2API { // HINT: update API version to currently used librealsense2 private const int API_MAJOR_VERSION = 2; From 796a701910fc11495c28feb5e5dec08b35a523a9 Mon Sep 17 00:00:00 2001 From: Jan Lukas Gernert Date: Fri, 22 Dec 2017 15:39:46 +0100 Subject: [PATCH 14/32] adjust PowerLineMode enum names --- BetaCameras/RealSense2/RealSense2.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/BetaCameras/RealSense2/RealSense2.cs b/BetaCameras/RealSense2/RealSense2.cs index 09ff4ed7..ec5ea52e 100644 --- a/BetaCameras/RealSense2/RealSense2.cs +++ b/BetaCameras/RealSense2/RealSense2.cs @@ -34,8 +34,8 @@ public enum EmitterMode public enum PowerLineMode { OFF = 0, - FIFTY_HZ = 1, - SIXTY_HZ = 2, + FREQ_50HZ = 1, + FREQ_60HZ = 2, AUTO = 3 } From 97d56da4ebfdfd9f91ce4ad1bb7e8095949363a6 Mon Sep 17 00:00:00 2001 From: Jan Lukas Gernert Date: Mon, 8 Jan 2018 16:29:50 +0100 Subject: [PATCH 15/32] check if value is valid in setter of ranged parameter --- BetaCameras/RealSense2/RealSense2.cs | 87 +++++++++++++++++++++------- 1 file changed, 67 insertions(+), 20 deletions(-) diff --git a/BetaCameras/RealSense2/RealSense2.cs b/BetaCameras/RealSense2/RealSense2.cs index ec5ea52e..20eb0862 100644 --- a/BetaCameras/RealSense2/RealSense2.cs +++ b/BetaCameras/RealSense2/RealSense2.cs @@ -198,6 +198,9 @@ public int Brightness if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.BRIGHTNESS)) throw new Exception("Option 'Brightness' is not supported by the color sensor of this camera."); + if(!BrightnessDesc.IsValid(value)) + throw new Exception(string.Format("Value {0} for 'Brightness' is outside of the range between {1} and {2}", value, BrightnessDesc.Min, BrightnessDesc.Min)); + RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.BRIGHTNESS, (float)value); } } @@ -242,6 +245,9 @@ public int Contrast if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.CONTRAST)) throw new Exception("Option 'Contrast' is not supported by the color sensor of this camera."); + if (!ContrastDesc.IsValid(value)) + throw new Exception(string.Format("Value {0} for 'Contrast' is outside of the range between {1} and {2}", value, ContrastDesc.Min, ContrastDesc.Min)); + RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.CONTRAST, (float)value); } } @@ -286,6 +292,9 @@ public int ExposureColor if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.EXPOSURE)) throw new Exception("Option 'Exposure' is not supported by the color sensor of this camera."); + if (!ExposureColorDesc.IsValid(value)) + throw new Exception(string.Format("Value {0} for 'Exposure' is outside of the range between {1} and {2}", value, ExposureColorDesc.Min, ExposureColorDesc.Min)); + RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.EXPOSURE, (float)value); } } @@ -419,6 +428,17 @@ public int ExposureDepth adjusted_value += step; } + if (!ExposureDepthDesc.IsValid(value)) + throw new Exception( + string.Format( + "Value {0} (adjusted to {1} to match stepsize) for 'Exposure' is outside of the range between {2} and {3}", + value, + adjusted_value, + ExposureDepthDesc.Min, + ExposureDepthDesc.Min + ) + ); + RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.EXPOSURE, adjusted_value); } } @@ -497,6 +517,9 @@ public int GainColor if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.GAIN)) throw new Exception("Option 'Gain' is not supported by the color sensor of this camera."); + if (!GainColorDesc.IsValid(value)) + throw new Exception(string.Format("Value {0} for 'Gain' is outside of the range between {1} and {2}", value, GainColorDesc.Min, GainColorDesc.Min)); + RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.GAIN, (float)value); } } @@ -541,6 +564,9 @@ public int GainDepth if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.GAIN)) throw new Exception("Option 'Gain' is not supported by the stereo sensor of this camera."); + if (!GainDepthDesc.IsValid(value)) + throw new Exception(string.Format("Value {0} for 'Gain' is outside of the range between {1} and {2}", value, GainDepthDesc.Min, GainDepthDesc.Min)); + RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.GAIN, (float)value); } } @@ -585,6 +611,9 @@ public int Gamma if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.GAMMA)) throw new Exception("Option 'Gamma' is not supported by the color sensor of this camera."); + if (!GammaDesc.IsValid(value)) + throw new Exception(string.Format("Value {0} for 'Gamma' is outside of the range between {1} and {2}", value, GammaDesc.Min, GammaDesc.Min)); + RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.GAMMA, (float)value); } } @@ -629,6 +658,9 @@ public int Hue if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.HUE)) throw new Exception("Option 'Hue' is not supported by the color sensor of this camera."); + if (!HueDesc.IsValid(value)) + throw new Exception(string.Format("Value {0} for 'Hue' is outside of the range between {1} and {2}", value, HueDesc.Min, HueDesc.Min)); + RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.HUE, (float)value); } } @@ -673,6 +705,9 @@ public int Saturation if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.SATURATION)) throw new Exception("Option 'Saturation' is not supported by the color sensor of this camera."); + if (!SaturationDesc.IsValid(value)) + throw new Exception(string.Format("Value {0} for 'Saturation' is outside of the range between {1} and {2}", value, SaturationDesc.Min, SaturationDesc.Min)); + RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.SATURATION, (float)value); } } @@ -717,6 +752,9 @@ public int Sharpness if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.SHARPNESS)) throw new Exception("Option 'Sharpness' is not supported by the color sensor of this camera."); + if (!SharpnessDesc.IsValid(value)) + throw new Exception(string.Format("Value {0} for 'Sharpness' is outside of the range between {1} and {2}", value, SharpnessDesc.Min, SharpnessDesc.Min)); + RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.SHARPNESS, (float)value); } } @@ -782,6 +820,17 @@ public int WhiteBalance adjusted_value += step; } + if (!WhiteBalanceDesc.IsValid(value)) + throw new Exception( + string.Format( + "Value {0} (adjusted to {1} to match stepsize) for 'WhiteBalance' is outside of the range between {2} and {3}", + value, + adjusted_value, + WhiteBalanceDesc.Min, + WhiteBalanceDesc.Min + ) + ); + RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.WHITE_BALANCE, adjusted_value); } } @@ -881,6 +930,17 @@ public int LaserPower adjusted_value += step; } + if (!LaserPowerDesc.IsValid(value)) + throw new Exception( + string.Format( + "Value {0} (adjusted to {1} to match stepsize) for 'LaserPower' is outside of the range between {2} and {3}", + value, + adjusted_value, + LaserPowerDesc.Min, + LaserPowerDesc.Min + ) + ); + RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.LASER_POWER, adjusted_value); } } @@ -962,16 +1022,8 @@ public int FrameQueueSizeColor if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.FRAMES_QUEUE_SIZE)) throw new Exception("Option 'FrameQueueSize' is not supported by the color sensor of this camera."); - RealSense2API.QueryOptionInfo( - _pipeline, - RealSense2API.SensorName.COLOR, - RealSense2API.Option.FRAMES_QUEUE_SIZE, - out float min, - out float max, - out float step, - out float def, - out string desc); - + if (!FrameQueueSizeColorDesc.IsValid(value)) + throw new Exception(string.Format("Value {0} for 'FrameQueueSize' is outside of the range between {1} and {2}", value, FrameQueueSizeColorDesc.Min, FrameQueueSizeColorDesc.Min)); RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.FRAMES_QUEUE_SIZE, (float)value); } @@ -1017,16 +1069,8 @@ public int FrameQueueSizeDepth if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.FRAMES_QUEUE_SIZE)) throw new Exception("Option 'FrameQueueSize' is not supported by the stereo sensor of this camera."); - RealSense2API.QueryOptionInfo( - _pipeline, - RealSense2API.SensorName.STEREO, - RealSense2API.Option.FRAMES_QUEUE_SIZE, - out float min, - out float max, - out float step, - out float def, - out string desc); - + if (!FrameQueueSizeDepthDesc.IsValid(value)) + throw new Exception(string.Format("Value {0} for 'FrameQueueSize' is outside of the range between {1} and {2}", value, FrameQueueSizeDepthDesc.Min, FrameQueueSizeDepthDesc.Min)); RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.FRAMES_QUEUE_SIZE, (float)value); } @@ -1235,6 +1279,9 @@ public float DepthUnits if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.DEPTH_UNITS)) throw new Exception("Option 'DepthUnits' is not supported by the stereo sensor of this camera."); + if (!DepthUnitsDesc.IsValid(value)) + throw new Exception(string.Format("Value {0} for 'DepthUnits' is outside of the range between {1} and {2}", value, DepthUnitsDesc.Min, DepthUnitsDesc.Min)); + RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.DEPTH_UNITS, value); } } From dc515f7d78d37bdf257a034b1484df872ed73145 Mon Sep 17 00:00:00 2001 From: Jan Lukas Gernert Date: Mon, 8 Jan 2018 17:29:43 +0100 Subject: [PATCH 16/32] refactor checks for every property --- BetaCameras/RealSense2/RealSense2.cs | 293 ++++++++------------------- 1 file changed, 82 insertions(+), 211 deletions(-) diff --git a/BetaCameras/RealSense2/RealSense2.cs b/BetaCameras/RealSense2/RealSense2.cs index 20eb0862..23b119a0 100644 --- a/BetaCameras/RealSense2/RealSense2.cs +++ b/BetaCameras/RealSense2/RealSense2.cs @@ -147,9 +147,7 @@ public bool BacklightCompensation { get { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.BACKLIGHT_COMPENSATION)) - throw new Exception("Option 'BacklightCompensation' is not supported by the color sensor of this camera."); - + CheckOptionSupported(RealSense2API.Option.BACKLIGHT_COMPENSATION, BacklightCompensationDesc.Name, RealSense2API.SensorName.COLOR); float res = RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.BACKLIGHT_COMPENSATION); if (res == 1.0f) @@ -160,9 +158,7 @@ public bool BacklightCompensation set { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.BACKLIGHT_COMPENSATION)) - throw new Exception("Option 'BacklightCompensation' is not supported by the color sensor of this camera."); - + CheckOptionSupported(RealSense2API.Option.BACKLIGHT_COMPENSATION, BacklightCompensationDesc.Name, RealSense2API.SensorName.COLOR); RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.BACKLIGHT_COMPENSATION, value ? 1.0f : 0.0f); } } @@ -187,19 +183,14 @@ public int Brightness { get { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.BRIGHTNESS)) - throw new Exception("Option 'Brightness' is not supported by the color sensor of this camera."); - + CheckOptionSupported(RealSense2API.Option.BRIGHTNESS, BrightnessDesc.Name, RealSense2API.SensorName.COLOR); return (int)RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.BRIGHTNESS); } set { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.BRIGHTNESS)) - throw new Exception("Option 'Brightness' is not supported by the color sensor of this camera."); - - if(!BrightnessDesc.IsValid(value)) - throw new Exception(string.Format("Value {0} for 'Brightness' is outside of the range between {1} and {2}", value, BrightnessDesc.Min, BrightnessDesc.Min)); + CheckOptionSupported(RealSense2API.Option.BRIGHTNESS, BrightnessDesc.Name, RealSense2API.SensorName.COLOR); + CheckRangeValid(BrightnessDesc, value, 0); RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.BRIGHTNESS, (float)value); } @@ -234,19 +225,14 @@ public int Contrast { get { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.CONTRAST)) - throw new Exception("Option 'Contrast' is not supported by the color sensor of this camera."); - + CheckOptionSupported(RealSense2API.Option.CONTRAST, ContrastDesc.Name, RealSense2API.SensorName.COLOR); return (int)RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.CONTRAST); } set { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.CONTRAST)) - throw new Exception("Option 'Contrast' is not supported by the color sensor of this camera."); - - if (!ContrastDesc.IsValid(value)) - throw new Exception(string.Format("Value {0} for 'Contrast' is outside of the range between {1} and {2}", value, ContrastDesc.Min, ContrastDesc.Min)); + CheckOptionSupported(RealSense2API.Option.CONTRAST, ContrastDesc.Name, RealSense2API.SensorName.COLOR); + CheckRangeValid(ContrastDesc, value, 0); RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.CONTRAST, (float)value); } @@ -281,19 +267,14 @@ public int ExposureColor { get { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.EXPOSURE)) - throw new Exception("Option 'Exposure' is not supported by the color sensor of this camera."); - + CheckOptionSupported(RealSense2API.Option.EXPOSURE, ExposureColorDesc.Name, RealSense2API.SensorName.COLOR); return (int)RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.EXPOSURE); } set { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.EXPOSURE)) - throw new Exception("Option 'Exposure' is not supported by the color sensor of this camera."); - - if (!ExposureColorDesc.IsValid(value)) - throw new Exception(string.Format("Value {0} for 'Exposure' is outside of the range between {1} and {2}", value, ExposureColorDesc.Min, ExposureColorDesc.Min)); + CheckOptionSupported(RealSense2API.Option.EXPOSURE, ExposureColorDesc.Name, RealSense2API.SensorName.COLOR); + CheckRangeValid(ExposureColorDesc, value, 0); RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.EXPOSURE, (float)value); } @@ -328,17 +309,13 @@ public bool AutoExposureColor { get { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.ENABLE_AUTO_EXPOSURE)) - throw new Exception("Option 'AutoExposure' is not supported by the color sensor of this camera."); - + CheckOptionSupported(RealSense2API.Option.ENABLE_AUTO_EXPOSURE, AutoExposureColorDesc.Name, RealSense2API.SensorName.COLOR); return RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.ENABLE_AUTO_EXPOSURE) == 1.0f ? true : false; } set { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.ENABLE_AUTO_EXPOSURE)) - throw new Exception("Option 'AutoExposure' is not supported by the color sensor of this camera."); - + CheckOptionSupported(RealSense2API.Option.ENABLE_AUTO_EXPOSURE, AutoExposureColorDesc.Name, RealSense2API.SensorName.COLOR); RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.ENABLE_AUTO_EXPOSURE, value ? 1.0f : 0.0f); } } @@ -362,17 +339,13 @@ public bool AutoExposurePriorityColor { get { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.AUTO_EXPOSURE_PRIORITY)) - throw new Exception("Option 'AutoExposurePriority' is not supported by the color sensor of this camera."); - + CheckOptionSupported(RealSense2API.Option.AUTO_EXPOSURE_PRIORITY, AutoExposurePriorityColorDesc.Name, RealSense2API.SensorName.COLOR); return RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.AUTO_EXPOSURE_PRIORITY) == 1.0f ? true : false; } set { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.AUTO_EXPOSURE_PRIORITY)) - throw new Exception("Option 'AutoExposurePriority' is not supported by the color sensor of this camera."); - + CheckOptionSupported(RealSense2API.Option.AUTO_EXPOSURE_PRIORITY, AutoExposurePriorityColorDesc.Name, RealSense2API.SensorName.COLOR); RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.AUTO_EXPOSURE_PRIORITY, value ? 1.0f : 0.0f); } } @@ -396,16 +369,13 @@ public int ExposureDepth { get { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.EXPOSURE)) - throw new Exception("Option 'Exposure' is not supported by the stereo sensor of this camera."); - + CheckOptionSupported(RealSense2API.Option.EXPOSURE, ExposureDepthDesc.Name, RealSense2API.SensorName.STEREO); return (int)RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.EXPOSURE); } set { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.EXPOSURE)) - throw new Exception("Option 'Exposure' is not supported by the stereo sensor of this camera."); + CheckOptionSupported(RealSense2API.Option.EXPOSURE, ExposureDepthDesc.Name, RealSense2API.SensorName.STEREO); RealSense2API.QueryOptionInfo( _pipeline, @@ -428,17 +398,8 @@ public int ExposureDepth adjusted_value += step; } - if (!ExposureDepthDesc.IsValid(value)) - throw new Exception( - string.Format( - "Value {0} (adjusted to {1} to match stepsize) for 'Exposure' is outside of the range between {2} and {3}", - value, - adjusted_value, - ExposureDepthDesc.Min, - ExposureDepthDesc.Min - ) - ); + CheckRangeValid(ExposureDepthDesc, value, (int)adjusted_value, true); RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.EXPOSURE, adjusted_value); } } @@ -472,17 +433,13 @@ public bool AutoExposureDepth { get { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.ENABLE_AUTO_EXPOSURE)) - throw new Exception("Option 'AutoExposure' is not supported by the stereo sensor of this camera."); - + CheckOptionSupported(RealSense2API.Option.ENABLE_AUTO_EXPOSURE, AutoExposureDepthDesc.Name, RealSense2API.SensorName.STEREO); return RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.ENABLE_AUTO_EXPOSURE) == 1.0f ? true : false; } set { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.ENABLE_AUTO_EXPOSURE)) - throw new Exception("Option 'AutoExposure' is not supported by the stereo sensor of this camera."); - + CheckOptionSupported(RealSense2API.Option.ENABLE_AUTO_EXPOSURE, AutoExposureDepthDesc.Name, RealSense2API.SensorName.STEREO); RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.ENABLE_AUTO_EXPOSURE, value ? 1.0f : 0.0f); } } @@ -506,20 +463,14 @@ public int GainColor { get { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.GAIN)) - throw new Exception("Option 'Gain' is not supported by the color sensor of this camera."); - + CheckOptionSupported(RealSense2API.Option.GAIN, GainColorDesc.Name, RealSense2API.SensorName.COLOR); return (int)RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.GAIN); } set { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.GAIN)) - throw new Exception("Option 'Gain' is not supported by the color sensor of this camera."); - - if (!GainColorDesc.IsValid(value)) - throw new Exception(string.Format("Value {0} for 'Gain' is outside of the range between {1} and {2}", value, GainColorDesc.Min, GainColorDesc.Min)); - + CheckOptionSupported(RealSense2API.Option.GAIN, GainColorDesc.Name, RealSense2API.SensorName.COLOR); + CheckRangeValid(GainColorDesc, value, 0); RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.GAIN, (float)value); } } @@ -553,20 +504,14 @@ public int GainDepth { get { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.GAIN)) - throw new Exception("Option 'Gain' is not supported by the stereo sensor of this camera."); - + CheckOptionSupported(RealSense2API.Option.GAIN, GainDepthDesc.Name, RealSense2API.SensorName.STEREO); return (int)RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.GAIN); } set { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.GAIN)) - throw new Exception("Option 'Gain' is not supported by the stereo sensor of this camera."); - - if (!GainDepthDesc.IsValid(value)) - throw new Exception(string.Format("Value {0} for 'Gain' is outside of the range between {1} and {2}", value, GainDepthDesc.Min, GainDepthDesc.Min)); - + CheckOptionSupported(RealSense2API.Option.GAIN, GainDepthDesc.Name, RealSense2API.SensorName.STEREO); + CheckRangeValid(GainDepthDesc, value, 0); RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.GAIN, (float)value); } } @@ -600,20 +545,14 @@ public int Gamma { get { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.GAMMA)) - throw new Exception("Option 'Gamma' is not supported by the color sensor of this camera."); - + CheckOptionSupported(RealSense2API.Option.GAMMA, GammaDesc.Name, RealSense2API.SensorName.COLOR); return (int)RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.GAMMA); } set { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.GAMMA)) - throw new Exception("Option 'Gamma' is not supported by the color sensor of this camera."); - - if (!GammaDesc.IsValid(value)) - throw new Exception(string.Format("Value {0} for 'Gamma' is outside of the range between {1} and {2}", value, GammaDesc.Min, GammaDesc.Min)); - + CheckOptionSupported(RealSense2API.Option.GAMMA, GammaDesc.Name, RealSense2API.SensorName.COLOR); + CheckRangeValid(GammaDesc, value, 0); RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.GAMMA, (float)value); } } @@ -647,20 +586,14 @@ public int Hue { get { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.HUE)) - throw new Exception("Option 'Hue' is not supported by the color sensor of this camera."); - + CheckOptionSupported(RealSense2API.Option.HUE, HueDesc.Name, RealSense2API.SensorName.COLOR); return (int)RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.HUE); } set { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.HUE)) - throw new Exception("Option 'Hue' is not supported by the color sensor of this camera."); - - if (!HueDesc.IsValid(value)) - throw new Exception(string.Format("Value {0} for 'Hue' is outside of the range between {1} and {2}", value, HueDesc.Min, HueDesc.Min)); - + CheckOptionSupported(RealSense2API.Option.HUE, HueDesc.Name, RealSense2API.SensorName.COLOR); + CheckRangeValid(HueDesc, value, 0); RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.HUE, (float)value); } } @@ -694,20 +627,14 @@ public int Saturation { get { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.SATURATION)) - throw new Exception("Option 'Saturation' is not supported by the color sensor of this camera."); - + CheckOptionSupported(RealSense2API.Option.SATURATION, SaturationDesc.Name, RealSense2API.SensorName.COLOR); return (int)RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.SATURATION); } set { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.SATURATION)) - throw new Exception("Option 'Saturation' is not supported by the color sensor of this camera."); - - if (!SaturationDesc.IsValid(value)) - throw new Exception(string.Format("Value {0} for 'Saturation' is outside of the range between {1} and {2}", value, SaturationDesc.Min, SaturationDesc.Min)); - + CheckOptionSupported(RealSense2API.Option.SATURATION, SaturationDesc.Name, RealSense2API.SensorName.COLOR); + CheckRangeValid(SaturationDesc, value, 0); RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.SATURATION, (float)value); } } @@ -741,20 +668,14 @@ public int Sharpness { get { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.SHARPNESS)) - throw new Exception("Option 'Sharpness' is not supported by the color sensor of this camera."); - + CheckOptionSupported(RealSense2API.Option.SHARPNESS, SharpnessDesc.Name, RealSense2API.SensorName.COLOR); return (int)RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.SHARPNESS); } set { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.SHARPNESS)) - throw new Exception("Option 'Sharpness' is not supported by the color sensor of this camera."); - - if (!SharpnessDesc.IsValid(value)) - throw new Exception(string.Format("Value {0} for 'Sharpness' is outside of the range between {1} and {2}", value, SharpnessDesc.Min, SharpnessDesc.Min)); - + CheckOptionSupported(RealSense2API.Option.SHARPNESS, SharpnessDesc.Name, RealSense2API.SensorName.COLOR); + CheckRangeValid(SharpnessDesc, value, 0); RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.SHARPNESS, (float)value); } } @@ -788,16 +709,13 @@ public int WhiteBalance { get { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.WHITE_BALANCE)) - throw new Exception("Option 'WhiteBalance' is not supported by the color sensor of this camera."); - + CheckOptionSupported(RealSense2API.Option.WHITE_BALANCE, WhiteBalanceDesc.Name, RealSense2API.SensorName.COLOR); return (int)RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.WHITE_BALANCE); } set { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.WHITE_BALANCE)) - throw new Exception("Option 'WhiteBalance' is not supported by the color sensor of this camera."); + CheckOptionSupported(RealSense2API.Option.WHITE_BALANCE, WhiteBalanceDesc.Name, RealSense2API.SensorName.COLOR); RealSense2API.QueryOptionInfo( _pipeline, @@ -820,16 +738,7 @@ public int WhiteBalance adjusted_value += step; } - if (!WhiteBalanceDesc.IsValid(value)) - throw new Exception( - string.Format( - "Value {0} (adjusted to {1} to match stepsize) for 'WhiteBalance' is outside of the range between {2} and {3}", - value, - adjusted_value, - WhiteBalanceDesc.Min, - WhiteBalanceDesc.Min - ) - ); + CheckRangeValid(WhiteBalanceDesc, value, (int)adjusted_value, true); RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.WHITE_BALANCE, adjusted_value); } @@ -864,17 +773,13 @@ public bool AutoWhiteBalance { get { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.ENABLE_AUTO_WHITE_BALANCE)) - throw new Exception("Option 'AutoWhiteBalance' is not supported by the color sensor of this camera."); - + CheckOptionSupported(RealSense2API.Option.ENABLE_AUTO_WHITE_BALANCE, AutoWhiteBalanceDesc.Name, RealSense2API.SensorName.COLOR); return RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.ENABLE_AUTO_WHITE_BALANCE) == 1.0f ? true : false; } set { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.ENABLE_AUTO_WHITE_BALANCE)) - throw new Exception("Option 'AutoWhiteBalance' is not supported by the color sensor of this camera."); - + CheckOptionSupported(RealSense2API.Option.ENABLE_AUTO_WHITE_BALANCE, AutoWhiteBalanceDesc.Name, RealSense2API.SensorName.COLOR); RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.ENABLE_AUTO_WHITE_BALANCE, value ? 1.0f : 0.0f); } } @@ -898,16 +803,13 @@ public int LaserPower { get { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.LASER_POWER)) - throw new Exception("Option 'LaserPower' is not supported by the stereo sensor of this camera."); - + CheckOptionSupported(RealSense2API.Option.LASER_POWER, LaserPowerDesc.Name, RealSense2API.SensorName.STEREO); return (int)RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.LASER_POWER); } set { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.LASER_POWER)) - throw new Exception("Option 'LaserPower' is not supported by the stereo sensor of this camera."); + CheckOptionSupported(RealSense2API.Option.LASER_POWER, LaserPowerDesc.Name, RealSense2API.SensorName.STEREO); RealSense2API.QueryOptionInfo( _pipeline, @@ -930,17 +832,7 @@ public int LaserPower adjusted_value += step; } - if (!LaserPowerDesc.IsValid(value)) - throw new Exception( - string.Format( - "Value {0} (adjusted to {1} to match stepsize) for 'LaserPower' is outside of the range between {2} and {3}", - value, - adjusted_value, - LaserPowerDesc.Min, - LaserPowerDesc.Min - ) - ); - + CheckRangeValid(LaserPowerDesc, value, (int)adjusted_value, true); RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.LASER_POWER, adjusted_value); } } @@ -974,17 +866,13 @@ public EmitterMode LaserMode { get { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.EMITTER_ENABLED)) - throw new Exception("Option 'LaserMode' is not supported by the stereo sensor of this camera."); - + CheckOptionSupported(RealSense2API.Option.EMITTER_ENABLED, EmmiterModeDesc.Name, RealSense2API.SensorName.STEREO); return (EmitterMode)RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.EMITTER_ENABLED); } set { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.EMITTER_ENABLED)) - throw new Exception("Option 'LaserMode' is not supported by the stereo sensor of this camera."); - + CheckOptionSupported(RealSense2API.Option.EMITTER_ENABLED, EmmiterModeDesc.Name, RealSense2API.SensorName.STEREO); RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.LASER_POWER, (float)value); } } @@ -1011,20 +899,14 @@ public int FrameQueueSizeColor { get { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.FRAMES_QUEUE_SIZE)) - throw new Exception("Option 'FrameQueueSize' is not supported by the color sensor of this camera."); - + CheckOptionSupported(RealSense2API.Option.FRAMES_QUEUE_SIZE, FrameQueueSizeColorDesc.Name, RealSense2API.SensorName.COLOR); return (int)RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.FRAMES_QUEUE_SIZE); } set { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.FRAMES_QUEUE_SIZE)) - throw new Exception("Option 'FrameQueueSize' is not supported by the color sensor of this camera."); - - if (!FrameQueueSizeColorDesc.IsValid(value)) - throw new Exception(string.Format("Value {0} for 'FrameQueueSize' is outside of the range between {1} and {2}", value, FrameQueueSizeColorDesc.Min, FrameQueueSizeColorDesc.Min)); - + CheckOptionSupported(RealSense2API.Option.FRAMES_QUEUE_SIZE, FrameQueueSizeColorDesc.Name, RealSense2API.SensorName.COLOR); + CheckRangeValid(FrameQueueSizeColorDesc, value, 0); RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.FRAMES_QUEUE_SIZE, (float)value); } } @@ -1058,20 +940,14 @@ public int FrameQueueSizeDepth { get { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.FRAMES_QUEUE_SIZE)) - throw new Exception("Option 'FrameQueueSize' is not supported by the stereo sensor of this camera."); - + CheckOptionSupported(RealSense2API.Option.FRAMES_QUEUE_SIZE, FrameQueueSizeDepthDesc.Name, RealSense2API.SensorName.STEREO); return (int)RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.FRAMES_QUEUE_SIZE); } set { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.FRAMES_QUEUE_SIZE)) - throw new Exception("Option 'FrameQueueSize' is not supported by the stereo sensor of this camera."); - - if (!FrameQueueSizeDepthDesc.IsValid(value)) - throw new Exception(string.Format("Value {0} for 'FrameQueueSize' is outside of the range between {1} and {2}", value, FrameQueueSizeDepthDesc.Min, FrameQueueSizeDepthDesc.Min)); - + CheckOptionSupported(RealSense2API.Option.FRAMES_QUEUE_SIZE, FrameQueueSizeDepthDesc.Name, RealSense2API.SensorName.STEREO); + CheckRangeValid(FrameQueueSizeDepthDesc, value, 0); RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.FRAMES_QUEUE_SIZE, (float)value); } } @@ -1105,17 +981,13 @@ public PowerLineMode PowerFrequencyMode { get { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.POWER_LINE_FREQUENCY)) - throw new Exception("Option 'PowerFrequencyMode' is not supported by the color sensor of this camera."); - + CheckOptionSupported(RealSense2API.Option.POWER_LINE_FREQUENCY, PowerFrequencyModeDesc.Name, RealSense2API.SensorName.COLOR); return (PowerLineMode)RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.POWER_LINE_FREQUENCY); } set { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.POWER_LINE_FREQUENCY)) - throw new Exception("Option 'PowerFrequencyMode' is not supported by the color sensor of this camera."); - + CheckOptionSupported(RealSense2API.Option.POWER_LINE_FREQUENCY, PowerFrequencyModeDesc.Name, RealSense2API.SensorName.COLOR); RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.POWER_LINE_FREQUENCY, (float)value); } } @@ -1142,9 +1014,7 @@ public float ASICTemp { get { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.ASIC_TEMPERATURE)) - throw new Exception("Option 'ASICTemp' is not supported by the stereo sensor of this camera."); - + CheckOptionSupported(RealSense2API.Option.ASIC_TEMPERATURE, ASICTempDesc.Name, RealSense2API.SensorName.STEREO); return RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.ASIC_TEMPERATURE); } } @@ -1170,17 +1040,13 @@ public bool EnableErrorPolling { get { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.ERROR_POLLING_ENABLED)) - throw new Exception("Option 'ErrorPolling' is not supported by the stereo sensor of this camera."); - + CheckOptionSupported(RealSense2API.Option.ERROR_POLLING_ENABLED, EnableErrorPollingDesc.Name, RealSense2API.SensorName.STEREO); return RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.ERROR_POLLING_ENABLED) == 1.0f ? true : false; } set { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.ERROR_POLLING_ENABLED)) - throw new Exception("Option 'ErrorPolling' is not supported by the stereo sensor of this camera."); - + CheckOptionSupported(RealSense2API.Option.ERROR_POLLING_ENABLED, EnableErrorPollingDesc.Name, RealSense2API.SensorName.STEREO); RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.ERROR_POLLING_ENABLED, value ? 1.0f : 0.0f); } } @@ -1205,9 +1071,7 @@ public float ProjectorTemp { get { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.PROJECTOR_TEMPERATURE)) - throw new Exception("Option 'ProjectorTemp' is not supported by the stereo sensor of this camera."); - + CheckOptionSupported(RealSense2API.Option.PROJECTOR_TEMPERATURE, ProjectorTempDesc.Name, RealSense2API.SensorName.STEREO); return RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.PROJECTOR_TEMPERATURE); } } @@ -1233,17 +1097,13 @@ public bool OutputTrigger { get { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.OUTPUT_TRIGGER_ENABLED)) - throw new Exception("Option 'OutputTrigger' is not supported by the stereo sensor of this camera."); - + CheckOptionSupported(RealSense2API.Option.OUTPUT_TRIGGER_ENABLED, OutputTriggerDesc.Name, RealSense2API.SensorName.STEREO); return RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.OUTPUT_TRIGGER_ENABLED) == 1.0f ? true : false; } set { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.OUTPUT_TRIGGER_ENABLED)) - throw new Exception("Option 'OutputTrigger' is not supported by the stereo sensor of this camera."); - + CheckOptionSupported(RealSense2API.Option.OUTPUT_TRIGGER_ENABLED, OutputTriggerDesc.Name, RealSense2API.SensorName.STEREO); RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.OUTPUT_TRIGGER_ENABLED, value ? 1.0f : 0.0f); } } @@ -1268,20 +1128,14 @@ public float DepthUnits { get { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.DEPTH_UNITS)) - throw new Exception("Option 'DepthUnits' is not supported by the stereo sensor of this camera."); - + CheckOptionSupported(RealSense2API.Option.DEPTH_UNITS, DepthUnitsDesc.Name, RealSense2API.SensorName.STEREO); return RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.DEPTH_UNITS); } set { - if (!RealSense2API.IsOptionSupported(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.DEPTH_UNITS)) - throw new Exception("Option 'DepthUnits' is not supported by the stereo sensor of this camera."); - - if (!DepthUnitsDesc.IsValid(value)) - throw new Exception(string.Format("Value {0} for 'DepthUnits' is outside of the range between {1} and {2}", value, DepthUnitsDesc.Min, DepthUnitsDesc.Min)); - + CheckOptionSupported(RealSense2API.Option.DEPTH_UNITS, DepthUnitsDesc.Name, RealSense2API.SensorName.STEREO); + CheckRangeValid(DepthUnitsDesc, value, 0f); RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.DEPTH_UNITS, value); } } @@ -1830,5 +1684,22 @@ public void LoadCustomConfig(string json) //RealSense2API.DeleteDevice(dev); _depthScale = RealSense2API.GetDepthScale(_pipeline); } + + private void CheckOptionSupported(RealSense2API.Option option, string optionName, string sensorName) + { + if (!RealSense2API.IsOptionSupported(_pipeline, sensorName, option)) + { + throw new NotSupportedException(string.Format("Option '{0}' is not supported by the {1} sensor of this camera.", optionName, sensorName)); + } + } + + private void CheckRangeValid(RangeParamDesc desc, T value, T adjustedValue, bool adjusted = false) + { + if (!desc.IsValid(value)) + if (adjusted) + throw new Exception(string.Format("Value {0} for '{1}' is outside of the range between {2} and {3}", value, desc.Name, desc.Min, desc.Max)); + else + throw new Exception(string.Format("Value {0} (adjusted to {1} to match stepsize) for 'LaserPower' is outside of the range between {2} and {3}", value, adjustedValue, desc.Min, desc.Max)); + } } } From 416131baa4a3db20f8bbdda562cc7d8e364fb49e Mon Sep 17 00:00:00 2001 From: Jan Lukas Gernert Date: Tue, 9 Jan 2018 15:54:24 +0100 Subject: [PATCH 17/32] Only query parameter info when camera is actually connected to avoid exception --- BetaCameras/RealSense2/RealSense2.cs | 277 +++++++++++++++++++++------ 1 file changed, 214 insertions(+), 63 deletions(-) diff --git a/BetaCameras/RealSense2/RealSense2.cs b/BetaCameras/RealSense2/RealSense2.cs index 23b119a0..d4dd931e 100644 --- a/BetaCameras/RealSense2/RealSense2.cs +++ b/BetaCameras/RealSense2/RealSense2.cs @@ -171,7 +171,7 @@ ParamDesc BacklightCompensationDesc res.Unit = "Boolean"; res.Description = "Enable / disable color backlight compensation"; res.ReadableWhen = ParamDesc.ConnectionStates.Connected; - res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + res.WritableWhen = ParamDesc.ConnectionStates.Connected; return res; } } @@ -200,20 +200,30 @@ RangeParamDesc BrightnessDesc { get { - RealSense2API.QueryOptionInfo( - _pipeline, - RealSense2API.SensorName.COLOR, - RealSense2API.Option.BRIGHTNESS, - out float min, - out float max, - out float step, - out float def, + RangeParamDesc res; + + if (this.IsConnected) + { + RealSense2API.QueryOptionInfo( + _pipeline, + RealSense2API.SensorName.COLOR, + RealSense2API.Option.BRIGHTNESS, + out float min, + out float max, + out float step, + out float def, out string desc); - RangeParamDesc res = new RangeParamDesc((int)min, (int)max); + res = new RangeParamDesc((int)min, (int)max); + } + else + { + res = new RangeParamDesc(0, 0); + } + res.Description = "Color image brightness"; res.ReadableWhen = ParamDesc.ConnectionStates.Connected; - res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + res.WritableWhen = ParamDesc.ConnectionStates.Connected; return res; } } @@ -242,7 +252,11 @@ RangeParamDesc ContrastDesc { get { - RealSense2API.QueryOptionInfo( + RangeParamDesc res; + + if(this.IsConnected) + { + RealSense2API.QueryOptionInfo( _pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.CONTRAST, @@ -252,10 +266,16 @@ RangeParamDesc ContrastDesc out float def, out string desc); - RangeParamDesc res = new RangeParamDesc((int)min, (int)max); + res = new RangeParamDesc((int)min, (int)max); + } + else + { + res = new RangeParamDesc(0, 0); + } + res.Description = "Color image contrast"; res.ReadableWhen = ParamDesc.ConnectionStates.Connected; - res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + res.WritableWhen = ParamDesc.ConnectionStates.Connected; return res; } } @@ -284,7 +304,11 @@ RangeParamDesc ExposureColorDesc { get { - RealSense2API.QueryOptionInfo( + RangeParamDesc res; + + if(this.IsConnected) + { + RealSense2API.QueryOptionInfo( _pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.EXPOSURE, @@ -294,10 +318,16 @@ RangeParamDesc ExposureColorDesc out float def, out string desc); - RangeParamDesc res = new RangeParamDesc((int)min, (int)max); + res = new RangeParamDesc((int)min, (int)max); + } + else + { + res = new RangeParamDesc(0, 0); + } + res.Description = "Controls exposure time of color camera. Setting any value will disable auto exposure"; res.ReadableWhen = ParamDesc.ConnectionStates.Connected; - res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + res.WritableWhen = ParamDesc.ConnectionStates.Connected; return res; } } @@ -327,7 +357,7 @@ ParamDesc AutoExposureColorDesc ParamDesc res = new ParamDesc(); res.Description = "Enable / disable color image auto-exposure"; res.ReadableWhen = ParamDesc.ConnectionStates.Connected; - res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + res.WritableWhen = ParamDesc.ConnectionStates.Connected; return res; } } @@ -357,7 +387,7 @@ ParamDesc AutoExposurePriorityColorDesc ParamDesc res = new ParamDesc(); res.Description = "Limit exposure time when auto-exposure is ON to preserve constant fps rate"; res.ReadableWhen = ParamDesc.ConnectionStates.Connected; - res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + res.WritableWhen = ParamDesc.ConnectionStates.Connected; return res; } } @@ -408,7 +438,11 @@ RangeParamDesc ExposureDepthDesc { get { - RealSense2API.QueryOptionInfo( + RangeParamDesc res; + + if(this.IsConnected) + { + RealSense2API.QueryOptionInfo( _pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.EXPOSURE, @@ -418,10 +452,16 @@ RangeParamDesc ExposureDepthDesc out float def, out string desc); - RangeParamDesc res = new RangeParamDesc((int)min, (int)max); + res = new RangeParamDesc((int)min, (int)max); + } + else + { + res = new RangeParamDesc(0, 0); + } + res.Description = "Controls exposure time of depth camera. Setting any value will disable auto exposure"; res.ReadableWhen = ParamDesc.ConnectionStates.Connected; - res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + res.WritableWhen = ParamDesc.ConnectionStates.Connected; return res; } } @@ -451,7 +491,7 @@ ParamDesc AutoExposureDepthDesc ParamDesc res = new ParamDesc(); res.Description = "Enable / disable depth image auto-exposure"; res.ReadableWhen = ParamDesc.ConnectionStates.Connected; - res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + res.WritableWhen = ParamDesc.ConnectionStates.Connected; return res; } } @@ -479,7 +519,11 @@ RangeParamDesc GainColorDesc { get { - RealSense2API.QueryOptionInfo( + RangeParamDesc res; + + if(this.IsConnected) + { + RealSense2API.QueryOptionInfo( _pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.GAIN, @@ -489,10 +533,16 @@ RangeParamDesc GainColorDesc out float def, out string desc); - RangeParamDesc res = new RangeParamDesc((int)min, (int)max); + res = new RangeParamDesc((int)min, (int)max); + } + else + { + res = new RangeParamDesc(0, 0); + } + res.Description = "Color image gain"; res.ReadableWhen = ParamDesc.ConnectionStates.Connected; - res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + res.WritableWhen = ParamDesc.ConnectionStates.Connected; return res; } } @@ -520,7 +570,11 @@ RangeParamDesc GainDepthDesc { get { - RealSense2API.QueryOptionInfo( + RangeParamDesc res; + + if(this.IsConnected) + { + RealSense2API.QueryOptionInfo( _pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.GAIN, @@ -530,10 +584,16 @@ RangeParamDesc GainDepthDesc out float def, out string desc); - RangeParamDesc res = new RangeParamDesc((int)min, (int)max); + res = new RangeParamDesc((int)min, (int)max); + } + else + { + res = new RangeParamDesc(0, 0); + } + res.Description = "Depth image gain"; res.ReadableWhen = ParamDesc.ConnectionStates.Connected; - res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + res.WritableWhen = ParamDesc.ConnectionStates.Connected; return res; } } @@ -561,7 +621,11 @@ RangeParamDesc GammaDesc { get { - RealSense2API.QueryOptionInfo( + RangeParamDesc res; + + if(this.IsConnected) + { + RealSense2API.QueryOptionInfo( _pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.GAMMA, @@ -571,10 +635,16 @@ RangeParamDesc GammaDesc out float def, out string desc); - RangeParamDesc res = new RangeParamDesc((int)min, (int)max); + res = new RangeParamDesc((int)min, (int)max); + } + else + { + res = new RangeParamDesc(0, 0); + } + res.Description = "Color image Gamma"; res.ReadableWhen = ParamDesc.ConnectionStates.Connected; - res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + res.WritableWhen = ParamDesc.ConnectionStates.Connected; return res; } } @@ -602,7 +672,11 @@ RangeParamDesc HueDesc { get { - RealSense2API.QueryOptionInfo( + RangeParamDesc res; + + if(this.IsConnected) + { + RealSense2API.QueryOptionInfo( _pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.HUE, @@ -612,10 +686,16 @@ RangeParamDesc HueDesc out float def, out string desc); - RangeParamDesc res = new RangeParamDesc((int)min, (int)max); + res = new RangeParamDesc((int)min, (int)max); + } + else + { + res = new RangeParamDesc(0, 0); + } + res.Description = "Color image Hue"; res.ReadableWhen = ParamDesc.ConnectionStates.Connected; - res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + res.WritableWhen = ParamDesc.ConnectionStates.Connected; return res; } } @@ -643,7 +723,11 @@ RangeParamDesc SaturationDesc { get { - RealSense2API.QueryOptionInfo( + RangeParamDesc res; + + if(this.IsConnected) + { + RealSense2API.QueryOptionInfo( _pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.SATURATION, @@ -653,10 +737,16 @@ RangeParamDesc SaturationDesc out float def, out string desc); - RangeParamDesc res = new RangeParamDesc((int)min, (int)max); + res = new RangeParamDesc((int)min, (int)max); + } + else + { + res = new RangeParamDesc(0, 0); + } + res.Description = "Color image Saturation"; res.ReadableWhen = ParamDesc.ConnectionStates.Connected; - res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + res.WritableWhen = ParamDesc.ConnectionStates.Connected; return res; } } @@ -684,7 +774,11 @@ RangeParamDesc SharpnessDesc { get { - RealSense2API.QueryOptionInfo( + RangeParamDesc res; + + if(this.IsConnected) + { + RealSense2API.QueryOptionInfo( _pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.SHARPNESS, @@ -694,10 +788,16 @@ RangeParamDesc SharpnessDesc out float def, out string desc); - RangeParamDesc res = new RangeParamDesc((int)min, (int)max); + res = new RangeParamDesc((int)min, (int)max); + } + else + { + res = new RangeParamDesc(0, 0); + } + res.Description = "Color image Sharpness"; res.ReadableWhen = ParamDesc.ConnectionStates.Connected; - res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + res.WritableWhen = ParamDesc.ConnectionStates.Connected; return res; } } @@ -748,7 +848,11 @@ RangeParamDesc WhiteBalanceDesc { get { - RealSense2API.QueryOptionInfo( + RangeParamDesc res; + + if(this.IsConnected) + { + RealSense2API.QueryOptionInfo( _pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.WHITE_BALANCE, @@ -758,10 +862,16 @@ RangeParamDesc WhiteBalanceDesc out float def, out string desc); - RangeParamDesc res = new RangeParamDesc((int)min, (int)max); + res = new RangeParamDesc((int)min, (int)max); + } + else + { + res = new RangeParamDesc(0, 0); + } + res.Description = "Controls white balance of color image.Setting any value will disable auto white balance"; res.ReadableWhen = ParamDesc.ConnectionStates.Connected; - res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + res.WritableWhen = ParamDesc.ConnectionStates.Connected; return res; } } @@ -791,7 +901,7 @@ ParamDesc AutoWhiteBalanceDesc ParamDesc res = new ParamDesc(); res.Description = "Enable / disable auto-white-balance"; res.ReadableWhen = ParamDesc.ConnectionStates.Connected; - res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + res.WritableWhen = ParamDesc.ConnectionStates.Connected; return res; } } @@ -841,7 +951,11 @@ RangeParamDesc LaserPowerDesc { get { - RealSense2API.QueryOptionInfo( + RangeParamDesc res; + + if(this.IsConnected) + { + RealSense2API.QueryOptionInfo( _pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.LASER_POWER, @@ -851,10 +965,16 @@ RangeParamDesc LaserPowerDesc out float def, out string desc); - RangeParamDesc res = new RangeParamDesc((int)min, (int)max); + res = new RangeParamDesc((int)min, (int)max); + } + else + { + res = new RangeParamDesc(0, 0); + } + res.Description = "Manual laser power in mw. applicable only when laser power mode is set to Manual"; res.ReadableWhen = ParamDesc.ConnectionStates.Connected; - res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + res.WritableWhen = ParamDesc.ConnectionStates.Connected; return res; } } @@ -915,7 +1035,11 @@ RangeParamDesc FrameQueueSizeColorDesc { get { - RealSense2API.QueryOptionInfo( + RangeParamDesc res; + + if(this.IsConnected) + { + RealSense2API.QueryOptionInfo( _pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.FRAMES_QUEUE_SIZE, @@ -925,10 +1049,16 @@ RangeParamDesc FrameQueueSizeColorDesc out float def, out string desc); - RangeParamDesc res = new RangeParamDesc((int)min, (int)max); + res = new RangeParamDesc((int)min, (int)max); + } + else + { + res = new RangeParamDesc(0, 0); + } + res.Description = "Max number of frames you can hold at a given time. Increasing this number will reduce frame drops but increase latency, and vice versa"; res.ReadableWhen = ParamDesc.ConnectionStates.Connected; - res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + res.WritableWhen = ParamDesc.ConnectionStates.Connected; return res; } } @@ -956,7 +1086,11 @@ RangeParamDesc FrameQueueSizeDepthDesc { get { - RealSense2API.QueryOptionInfo( + RangeParamDesc res; + + if (this.IsConnected) + { + RealSense2API.QueryOptionInfo( _pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.FRAMES_QUEUE_SIZE, @@ -966,10 +1100,16 @@ RangeParamDesc FrameQueueSizeDepthDesc out float def, out string desc); - RangeParamDesc res = new RangeParamDesc((int)min, (int)max); + res = new RangeParamDesc((int)min, (int)max); + } + else + { + res = new RangeParamDesc(0, 0); + } + res.Description = "Max number of frames you can hold at a given time. Increasing this number will reduce frame drops but increase latency, and vice versa"; res.ReadableWhen = ParamDesc.ConnectionStates.Connected; - res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + res.WritableWhen = ParamDesc.ConnectionStates.Connected; return res; } } @@ -1027,7 +1167,7 @@ ParamDesc ASICTempDesc res.Unit = "Degree Celsius °C"; res.Description = "Current Asic Temperature"; res.ReadableWhen = ParamDesc.ConnectionStates.Connected; - res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + res.WritableWhen = ParamDesc.ConnectionStates.Connected; return res; } } @@ -1058,7 +1198,7 @@ ParamDesc EnableErrorPollingDesc ParamDesc res = new ParamDesc(); res.Description = "Enable / disable polling of camera internal errors"; res.ReadableWhen = ParamDesc.ConnectionStates.Connected; - res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + res.WritableWhen = ParamDesc.ConnectionStates.Connected; return res; } } @@ -1084,7 +1224,7 @@ ParamDesc ProjectorTempDesc res.Unit = "Degree Celsius °C"; res.Description = "Current Projector Temperature"; res.ReadableWhen = ParamDesc.ConnectionStates.Connected; - res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + res.WritableWhen = ParamDesc.ConnectionStates.Connected; return res; } } @@ -1115,7 +1255,7 @@ ParamDesc OutputTriggerDesc ParamDesc res = new ParamDesc(); res.Description = "Enable / disable trigger to be outputed from the camera to any external device on every depth frame"; res.ReadableWhen = ParamDesc.ConnectionStates.Connected; - res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + res.WritableWhen = ParamDesc.ConnectionStates.Connected; return res; } } @@ -1144,7 +1284,11 @@ RangeParamDesc DepthUnitsDesc { get { - RealSense2API.QueryOptionInfo( + RangeParamDesc res; + + if(this.IsConnected) + { + RealSense2API.QueryOptionInfo( _pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.DEPTH_UNITS, @@ -1154,10 +1298,16 @@ RangeParamDesc DepthUnitsDesc out float def, out string desc); - RangeParamDesc res = new RangeParamDesc(min, max); + res = new RangeParamDesc(min, max); + } + else + { + res = new RangeParamDesc(0, 0); + } + res.Description = "Number of meters represented by a single depth unit"; res.ReadableWhen = ParamDesc.ConnectionStates.Connected; - res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + res.WritableWhen = ParamDesc.ConnectionStates.Connected; return res; } } @@ -1687,10 +1837,11 @@ public void LoadCustomConfig(string json) private void CheckOptionSupported(RealSense2API.Option option, string optionName, string sensorName) { + if (!this.IsConnected) + throw new Exception(string.Format("The property '{0}' can only be read or written when the camera is connected!", optionName)); + if (!RealSense2API.IsOptionSupported(_pipeline, sensorName, option)) - { throw new NotSupportedException(string.Format("Option '{0}' is not supported by the {1} sensor of this camera.", optionName, sensorName)); - } } private void CheckRangeValid(RangeParamDesc desc, T value, T adjustedValue, bool adjusted = false) From 038da039690797940734c5d7b3fe449ce97bed2e Mon Sep 17 00:00:00 2001 From: Jan Lukas Gernert Date: Tue, 9 Jan 2018 16:37:19 +0100 Subject: [PATCH 18/32] Fix value adjustment for settings with steps and ensure value never is out of bounds --- BetaCameras/RealSense2/RealSense2.cs | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/BetaCameras/RealSense2/RealSense2.cs b/BetaCameras/RealSense2/RealSense2.cs index d4dd931e..e787f270 100644 --- a/BetaCameras/RealSense2/RealSense2.cs +++ b/BetaCameras/RealSense2/RealSense2.cs @@ -420,13 +420,16 @@ public int ExposureDepth // step size for depth exposure is 20 float adjusted_value = (float)value; - int rounding = value - (int)min % (int)step; + int rounding = (value - (int)min) % (int)step; adjusted_value -= (float)rounding; if (rounding > step / 2) - { adjusted_value += step; - } + + if (adjusted_value > max) + adjusted_value -= step; + if (adjusted_value < min) + adjusted_value += step; CheckRangeValid(ExposureDepthDesc, value, (int)adjusted_value, true); @@ -830,13 +833,16 @@ public int WhiteBalance // step size for depth white balance is 10 float adjusted_value = (float)value; - int rounding = value - (int)min % (int)step; + int rounding = (value - (int)min) % (int)step; adjusted_value -= (float)rounding; if (rounding > step / 2) - { adjusted_value += step; - } + + if (adjusted_value > max) + adjusted_value -= step; + if (adjusted_value < min) + adjusted_value += step; CheckRangeValid(WhiteBalanceDesc, value, (int)adjusted_value, true); @@ -934,13 +940,16 @@ public int LaserPower // step size for depth laser power is 30 float adjusted_value = (float)value; - int rounding = value - (int)min % (int)step; + int rounding = (value - (int)min) % (int)step; adjusted_value -= (float)rounding; if (rounding > step / 2) - { adjusted_value += step; - } + + if (adjusted_value > max) + adjusted_value -= step; + if (adjusted_value < min) + adjusted_value += step; CheckRangeValid(LaserPowerDesc, value, (int)adjusted_value, true); RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.LASER_POWER, adjusted_value); From 184888cf95ebac0fc061bc6eb766c9f721463884 Mon Sep 17 00:00:00 2001 From: Jan Lukas Gernert Date: Tue, 9 Jan 2018 17:29:11 +0100 Subject: [PATCH 19/32] change power frequency mode to listparamdesc and increase frame timout to 5000ms according to examples (500ms sometimes caused problems) --- BetaCameras/RealSense2/RealSense2.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/BetaCameras/RealSense2/RealSense2.cs b/BetaCameras/RealSense2/RealSense2.cs index e787f270..1d59aebe 100644 --- a/BetaCameras/RealSense2/RealSense2.cs +++ b/BetaCameras/RealSense2/RealSense2.cs @@ -1141,11 +1141,11 @@ public PowerLineMode PowerFrequencyMode } } - ParamDesc PowerFrequencyModeDesc + ListParamDesc PowerFrequencyModeDesc { get { - ParamDesc res = new ParamDesc() + ListParamDesc res = new ListParamDesc(typeof(PowerLineMode)) { Description = "Power Line Frequency control for anti-flickering Off/50Hz/60Hz/Auto", ReadableWhen = ParamDesc.ConnectionStates.Connected, @@ -1428,7 +1428,7 @@ protected override void UpdateImpl() while (true) { - RealSense2API.RS2Frame data = RealSense2API.PipelineWaitForFrames(_pipeline, 500); + RealSense2API.RS2Frame data = RealSense2API.PipelineWaitForFrames(_pipeline, 5000); if(!data.IsValid() || data.Handle == IntPtr.Zero) { From f4e53a02b030e881d12033b74ef3bb895fc5641c Mon Sep 17 00:00:00 2001 From: Jan Lukas Gernert Date: Tue, 9 Jan 2018 19:57:01 +0100 Subject: [PATCH 20/32] gather available resolutions and fps once connected --- BetaCameras/RealSense2/RealSense2.cs | 121 ++++++++++++---- BetaCameras/RealSense2/RealSense2API.cs | 180 ++++++++++++++++++++++-- 2 files changed, 261 insertions(+), 40 deletions(-) diff --git a/BetaCameras/RealSense2/RealSense2.cs b/BetaCameras/RealSense2/RealSense2.cs index 1d59aebe..c2825e05 100644 --- a/BetaCameras/RealSense2/RealSense2.cs +++ b/BetaCameras/RealSense2/RealSense2.cs @@ -4,6 +4,7 @@ using Metrilus.Util; using System; using System.Drawing; +using System.Collections.Generic; #if NETSTANDARD2_0 #else using System.Drawing.Imaging; @@ -39,62 +40,133 @@ public enum PowerLineMode AUTO = 3 } - public Point2i ColorResolution { get; set; } = new Point2i(640, 480); + private Point2i _colorResolution = new Point2i(640, 480); + public Point2i ColorResolution + { + get + { + return _colorResolution; + } + + set + { + _colorResolution = value; + } + } - ParamDesc ColorResolutionDesc + ListParamDesc ColorResolutionDesc { get { - ParamDesc res = new ParamDesc(); + List resolutions = new List(); + resolutions.Add(new Point2i(640, 480)); + + if (this.IsConnected) + { + resolutions = RealSense2API.GetSupportedResolutions(_pipeline, RealSense2API.SensorName.COLOR); + } + + List allowedValues = new List(); + foreach (Point2i resolution in resolutions) + { + allowedValues.Add(string.Format("{0}x{1}", resolution.X, resolution.Y)); + } + + ListParamDesc res = new ListParamDesc(allowedValues); res.Unit = "Pixel"; res.Description = "Resolution of the color sensor."; res.ReadableWhen = ParamDesc.ConnectionStates.Connected | ParamDesc.ConnectionStates.Disconnected; - res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + res.WritableWhen = ParamDesc.ConnectionStates.Connected | ParamDesc.ConnectionStates.Disconnected; return res; } } - public uint ColorFPS { get; set; } = 30; + private int _colorFPS = 30; + public int ColorFPS + { + get { return _colorFPS; } + set + { + _colorFPS = value; + } + } - ParamDesc ColorFPSDesc + ListParamDesc ColorFPSDesc { get { - ParamDesc res = new ParamDesc(); + List framerates = new List(); + framerates.Add(30); + + if (this.IsConnected) + { + framerates = RealSense2API.GetSupportedFrameRates(_pipeline, RealSense2API.SensorName.COLOR); + framerates.Sort(); + } + + ListParamDesc res = new ListParamDesc(framerates); res.Unit = "Frames per Second"; res.Description = "FPS of the color sensor."; res.ReadableWhen = ParamDesc.ConnectionStates.Connected | ParamDesc.ConnectionStates.Disconnected; - res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + res.WritableWhen = ParamDesc.ConnectionStates.Connected | ParamDesc.ConnectionStates.Disconnected; return res; } } public Point2i DepthResolution { get; set; } = new Point2i(640, 480); - ParamDesc DepthResolutionDesc + ListParamDesc DepthResolutionDesc { get { - ParamDesc res = new ParamDesc(); + List resolutions = new List(); + resolutions.Add(new Point2i(640, 480)); + + if(this.IsConnected) + { + resolutions = RealSense2API.GetSupportedResolutions(_pipeline, RealSense2API.SensorName.STEREO); + } + + List allowedValues = new List(); + foreach(Point2i resolution in resolutions) + { + allowedValues.Add(string.Format("{0}x{1}", resolution.X, resolution.Y)); + } + + ListParamDesc res = new ListParamDesc(allowedValues); res.Unit = "Pixel"; res.Description = "Resolution of the depth sensor."; res.ReadableWhen = ParamDesc.ConnectionStates.Connected | ParamDesc.ConnectionStates.Disconnected; - res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + res.WritableWhen = ParamDesc.ConnectionStates.Connected | ParamDesc.ConnectionStates.Disconnected; return res; } } - public uint DepthFPS { get; set; } = 30; + private int _depthFPS = 30; + public int DepthFPS + { + get { return _depthFPS; } + set { _depthFPS = value; } + } - ParamDesc DepthFPSDesc + ListParamDesc DepthFPSDesc { get { - ParamDesc res = new ParamDesc(); + List framerates = new List(); + framerates.Add(30); + + if (this.IsConnected) + { + framerates = RealSense2API.GetSupportedFrameRates(_pipeline, RealSense2API.SensorName.STEREO); + framerates.Sort(); + } + + ListParamDesc res = new ListParamDesc(framerates); res.Unit = "Frames per Second"; res.Description = "FPS of the depth sensor."; res.ReadableWhen = ParamDesc.ConnectionStates.Connected | ParamDesc.ConnectionStates.Disconnected; - res.WritableWhen = ParamDesc.ConnectionStates.Disconnected; + res.WritableWhen = ParamDesc.ConnectionStates.Connected | ParamDesc.ConnectionStates.Disconnected; return res; } } @@ -1337,9 +1409,9 @@ protected virtual void Dispose(bool disposing) if (IsConnected) DisconnectImpl(); - RealSense2API.DeleteConfig(_config); - RealSense2API.DeletePipeline(_pipeline); - RealSense2API.DeleteContext(_context); + _config.Delete(); + _pipeline.Delete(); + _context.Delete(); _disposed = true; } @@ -1750,8 +1822,8 @@ unsafe private FloatCameraImage CalcZImage() return null; } - int height = DepthResolution.Y; - int width = DepthResolution.X; + int height = _currentDepthFrame.Height; + int width = _currentDepthFrame.Width; FloatCameraImage depthData = new FloatCameraImage(width, height); short* source = (short*)RealSense2API.GetFrameData(_currentDepthFrame); @@ -1776,8 +1848,8 @@ unsafe private FloatCameraImage CalcIRImage(RealSense2API.RS2Frame frame) return null; } - int height = DepthResolution.Y; - int width = DepthResolution.X; + int height = frame.Height; + int width = frame.Width; FloatCameraImage IRData = new FloatCameraImage(width, height); byte* source = (byte*)RealSense2API.GetFrameData(frame); @@ -1802,8 +1874,8 @@ unsafe private ColorCameraImage CalcColor() return null; } - int height = ColorResolution.Y; - int width = ColorResolution.X; + int height = _currentColorFrame.Height; + int width = _currentColorFrame.Width; Bitmap bitmap = new Bitmap(width, height, PixelFormat.Format24bppRgb); Rectangle imageRect = new Rectangle(0, 0, width, height); @@ -1840,7 +1912,6 @@ public void LoadCustomConfig(string json) { RealSense2API.RS2Device dev = RealSense2API.GetActiveDevice(_pipeline); RealSense2API.LoadAdvancedConfig(json, dev); - //RealSense2API.DeleteDevice(dev); _depthScale = RealSense2API.GetDepthScale(_pipeline); } diff --git a/BetaCameras/RealSense2/RealSense2API.cs b/BetaCameras/RealSense2/RealSense2API.cs index 502b991c..d03c37fa 100644 --- a/BetaCameras/RealSense2/RealSense2API.cs +++ b/BetaCameras/RealSense2/RealSense2API.cs @@ -1,6 +1,8 @@ using System; using System.Runtime.InteropServices; using System.Text; +using System.Collections.Generic; +using Metrilus.Util; namespace MetriCam2.Cameras { @@ -580,6 +582,30 @@ public unsafe struct Extrinsics [DllImport("realsense2", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl, SetLastError = false)] private unsafe extern static char* rs2_get_option_value_description(IntPtr sensor, Option option, float value, IntPtr* error); + + [DllImport("realsense2", CallingConvention = CallingConvention.Cdecl, SetLastError = false)] + private unsafe extern static IntPtr rs2_get_stream_profiles(IntPtr sensor, IntPtr* error); + + [DllImport("realsense2", CallingConvention = CallingConvention.Cdecl, SetLastError = false)] + private unsafe extern static int rs2_get_stream_profiles_count(IntPtr stream_profile_list, IntPtr* error); + + [DllImport("realsense2", CallingConvention = CallingConvention.Cdecl, SetLastError = false)] + private unsafe extern static void rs2_delete_stream_profiles_list(IntPtr stream_profile_list); + + [DllImport("realsense2", CallingConvention = CallingConvention.Cdecl, SetLastError = false)] + private unsafe extern static IntPtr rs2_get_stream_profile(IntPtr stream_profile_list, int index, IntPtr* error); + + [DllImport("realsense2", CallingConvention = CallingConvention.Cdecl, SetLastError = false)] + private unsafe extern static void rs2_delete_stream_profile(IntPtr stream_profile); + + [DllImport("realsense2", CallingConvention = CallingConvention.Cdecl, SetLastError = false)] + private unsafe extern static void rs2_get_video_stream_resolution(IntPtr stream_profile, ref int width, ref int height, IntPtr* error); + + [DllImport("realsense2", CallingConvention = CallingConvention.Cdecl, SetLastError = false)] + private unsafe extern static int rs2_get_frame_width(IntPtr frame, IntPtr* error); + + [DllImport("realsense2", CallingConvention = CallingConvention.Cdecl, SetLastError = false)] + private unsafe extern static int rs2_get_frame_height(IntPtr frame, IntPtr* error); #endregion public struct RS2Context @@ -590,6 +616,11 @@ public RS2Context(IntPtr p) { Handle = p; } + + public void Delete() + { + rs2_delete_context(Handle); + } } public struct RS2Pipeline @@ -600,6 +631,11 @@ public RS2Pipeline(IntPtr p) { Handle = p; } + + public void Delete() + { + rs2_delete_pipeline(Handle); + } } public struct RS2Device @@ -637,12 +673,39 @@ public RS2Config(IntPtr p) { Handle = p; } + + public void Delete() + { + rs2_delete_config(Handle); + } } public struct RS2Frame { public IntPtr Handle { get; private set; } + unsafe public int Width + { + get + { + IntPtr error = IntPtr.Zero; + int width = rs2_get_frame_width(Handle, &error); + HandleError(error); + return width; + } + } + + unsafe public int Height + { + get + { + IntPtr error = IntPtr.Zero; + int height = rs2_get_frame_height(Handle, &error); + HandleError(error); + return height; + } + } + public RS2Frame(IntPtr p) { Handle = p; @@ -651,6 +714,21 @@ public RS2Frame(IntPtr p) public bool IsValid() => (null != Handle); } + public struct RS2StreamProfileList + { + public IntPtr Handle { get; private set; } + + public RS2StreamProfileList(IntPtr p) + { + Handle = p; + } + + public void Delete() + { + rs2_delete_stream_profiles_list(Handle); + } + } + public struct RS2StreamProfile { public IntPtr Handle { get; private set; } @@ -659,6 +737,11 @@ public RS2StreamProfile(IntPtr p) { Handle = p; } + + public void Delete() + { + rs2_delete_stream_profile(Handle); + } } unsafe public static RS2Context CreateContext() @@ -695,21 +778,6 @@ unsafe public static void DisableAllStreams(RS2Config conf) HandleError(error); } - unsafe public static void DeleteConfig(RS2Config conf) - { - rs2_delete_config(conf.Handle); - } - - unsafe public static void DeletePipeline(RS2Pipeline pipe) - { - rs2_delete_pipeline(pipe.Handle); - } - - unsafe public static void DeleteContext(RS2Context ctx) - { - rs2_delete_context(ctx.Handle); - } - unsafe public static void EnableDevice(RS2Config conf, string serial_number) { IntPtr error = IntPtr.Zero; @@ -783,6 +851,15 @@ unsafe public static RS2StreamProfile GetStreamProfile(RS2Frame frame) return new RS2StreamProfile(profilePtr); } + unsafe public static RS2StreamProfile GetStreamProfile(RS2StreamProfileList list, int index) + { + IntPtr error = IntPtr.Zero; + IntPtr profilePtr = rs2_get_stream_profile(list.Handle, index, &error); + HandleError(error); + + return new RS2StreamProfile(profilePtr); + } + unsafe public static void GetStreamProfileData(RS2StreamProfile profile, out Stream stream, out Format format, out int index, out int uid, out int framerate) { IntPtr error = IntPtr.Zero; @@ -796,6 +873,18 @@ unsafe public static void GetStreamProfileData(RS2StreamProfile profile, out Str HandleError(error); } + unsafe public static Point2i GetStreamProfileResolution(RS2StreamProfile profile) + { + IntPtr error = IntPtr.Zero; + + int width = 0; + int height = 0; + rs2_get_video_stream_resolution(profile.Handle, ref width, ref height, &error); + HandleError(error); + + return new Point2i(width, height); + } + unsafe public static void ConfigEnableStream(RS2Config conf, Stream stream, int index, int width, int height, Format format, int framerate) { IntPtr error = IntPtr.Zero; @@ -1049,6 +1138,67 @@ unsafe public static string OptionValueInfo(RS2Pipeline pipe, string SensorName, return Marshal.PtrToStringAnsi((IntPtr)msg); } + unsafe public static RS2StreamProfileList GetStreamProfileList(RS2Sensor sensor) + { + IntPtr error = IntPtr.Zero; + IntPtr list = rs2_get_stream_profiles(sensor.Handle, &error); + HandleError(error); + + return new RS2StreamProfileList(list); + } + + unsafe public static int GetStreamProfileListCount(RS2StreamProfileList list) + { + IntPtr error = IntPtr.Zero; + int count = rs2_get_stream_profiles_count(list.Handle, &error); + HandleError(error); + + return count; + } + + unsafe public static List GetSupportedResolutions(RS2Pipeline pipe, string sensorName) + { + IntPtr error = IntPtr.Zero; + List res = new List(); + RS2Sensor sensor = GetSensor(pipe, sensorName); + RS2StreamProfileList list = GetStreamProfileList(sensor); + int count = GetStreamProfileListCount(list); + + for(int i = 0; i < count; i++) + { + RS2StreamProfile profile = GetStreamProfile(list, i); + Point2i resolution = GetStreamProfileResolution(profile); + if(!res.Contains(resolution)) + res.Add(resolution); + } + + list.Delete(); + sensor.Delete(); + + return res; + } + + unsafe public static List GetSupportedFrameRates(RS2Pipeline pipe, string sensorName) + { + IntPtr error = IntPtr.Zero; + List res = new List(); + RS2Sensor sensor = GetSensor(pipe, sensorName); + RS2StreamProfileList list = GetStreamProfileList(sensor); + int count = GetStreamProfileListCount(list); + + for (int i = 0; i < count; i++) + { + RS2StreamProfile profile = GetStreamProfile(list, i); + GetStreamProfileData(profile, out Stream stream, out Format format, out int index, out int uid, out int framerate); + if (!res.Contains(framerate)) + res.Add(framerate); + } + + list.Delete(); + sensor.Delete(); + + return res; + } unsafe private static void HandleError(IntPtr e) From 9aabec514cb506c9b29cc6baee5a683e058e4a4d Mon Sep 17 00:00:00 2001 From: Jan Lukas Gernert Date: Wed, 10 Jan 2018 12:44:10 +0100 Subject: [PATCH 21/32] implement live adjustments of resolution and fps --- BetaCameras/RealSense2/RealSense2.cs | 172 ++++++++++++++++++++++----- 1 file changed, 144 insertions(+), 28 deletions(-) diff --git a/BetaCameras/RealSense2/RealSense2.cs b/BetaCameras/RealSense2/RealSense2.cs index c2825e05..a1eb3e9e 100644 --- a/BetaCameras/RealSense2/RealSense2.cs +++ b/BetaCameras/RealSense2/RealSense2.cs @@ -24,6 +24,7 @@ public class RealSense2 : Camera, IDisposable private RealSense2API.RS2Frame _currentRightFrame = new RealSense2API.RS2Frame(); private float _depthScale = 0.0f; private bool _disposed = false; + private bool _updatingPipeline = false; public enum EmitterMode { @@ -43,14 +44,19 @@ public enum PowerLineMode private Point2i _colorResolution = new Point2i(640, 480); public Point2i ColorResolution { - get - { - return _colorResolution; - } - + get { return _colorResolution; } set { - _colorResolution = value; + if(value != _colorResolution) + { + _updatingPipeline = true; + StopPipeline(); + DeactivateChannelImpl(ChannelNames.Color); + _colorResolution = value; + ActivateChannelImpl(ChannelNames.Color); + StartPipeline(); + _updatingPipeline = false; + } } } @@ -87,7 +93,16 @@ public int ColorFPS get { return _colorFPS; } set { - _colorFPS = value; + if (value != _colorFPS) + { + _updatingPipeline = true; + StopPipeline(); + DeactivateChannelImpl(ChannelNames.Color); + _colorFPS = value; + ActivateChannelImpl(ChannelNames.Color); + StartPipeline(); + _updatingPipeline = false; + } } } @@ -113,7 +128,42 @@ ListParamDesc ColorFPSDesc } } - public Point2i DepthResolution { get; set; } = new Point2i(640, 480); + private Point2i _depthResolution = new Point2i(640, 480); + public Point2i DepthResolution + { + get { return _depthResolution; } + set + { + if (value != _depthResolution) + { + _updatingPipeline = true; + StopPipeline(); + + if(IsChannelActive(ChannelNames.ZImage)) + DeactivateChannelImpl(ChannelNames.ZImage); + + if (IsChannelActive(ChannelNames.Left)) + DeactivateChannelImpl(ChannelNames.Left); + + if (IsChannelActive(ChannelNames.Right)) + DeactivateChannelImpl(ChannelNames.Right); + + _depthResolution = value; + + if (IsChannelActive(ChannelNames.ZImage)) + ActivateChannelImpl(ChannelNames.ZImage); + + if (IsChannelActive(ChannelNames.Left)) + ActivateChannelImpl(ChannelNames.Left); + + if (IsChannelActive(ChannelNames.Right)) + ActivateChannelImpl(ChannelNames.Right); + + StartPipeline(); + _updatingPipeline = false; + } + } + } ListParamDesc DepthResolutionDesc { @@ -146,7 +196,37 @@ ListParamDesc DepthResolutionDesc public int DepthFPS { get { return _depthFPS; } - set { _depthFPS = value; } + set + { + if (value != _depthFPS) + { + _updatingPipeline = true; + StopPipeline(); + + if (IsChannelActive(ChannelNames.ZImage)) + DeactivateChannelImpl(ChannelNames.ZImage); + + if (IsChannelActive(ChannelNames.Left)) + DeactivateChannelImpl(ChannelNames.Left); + + if (IsChannelActive(ChannelNames.Right)) + DeactivateChannelImpl(ChannelNames.Right); + + _depthFPS = value; + + if (IsChannelActive(ChannelNames.ZImage)) + ActivateChannelImpl(ChannelNames.ZImage); + + if (IsChannelActive(ChannelNames.Left)) + ActivateChannelImpl(ChannelNames.Left); + + if (IsChannelActive(ChannelNames.Right)) + ActivateChannelImpl(ChannelNames.Right); + + StartPipeline(); + _updatingPipeline = false; + } + } } ListParamDesc DepthFPSDesc @@ -1459,7 +1539,7 @@ protected override void ConnectImpl() ActivateChannel(ChannelNames.ZImage); } - RealSense2API.PipelineStart(_pipeline, _config); + StartPipeline(); RealSense2API.RS2Device dev = RealSense2API.GetActiveDevice(_pipeline); @@ -1471,19 +1551,46 @@ protected override void ConnectImpl() _depthScale = RealSense2API.GetDepthScale(_pipeline); } - public void RestartPipeline() + private void StopPipeline() + { + if (RealSense2API.PipelineRunning) + RealSense2API.PipelineStop(_pipeline); + else + { + string msg = "RealSense2: Can't stop the pipeline since it is not running"; + log.Error(msg); + throw new Exception(msg); + } + + } + + private void StartPipeline() { - RealSense2API.PipelineStop(_pipeline); + if (RealSense2API.PipelineRunning) + RealSense2API.PipelineStop(_pipeline); + RealSense2API.PipelineStart(_pipeline, _config); } protected override void DisconnectImpl() { - RealSense2API.PipelineStop(_pipeline); + StopPipeline(); } protected override void UpdateImpl() { + while(_updatingPipeline) + { + // wait for pipeline to restart with new settings + } + + if (!RealSense2API.PipelineRunning) + { + string msg = "RealSense2: Can't update camera since pipeline is not running"; + log.Error(msg); + throw new Exception(msg); + } + RealSense2API.ReleaseFrame(_currentColorFrame); RealSense2API.ReleaseFrame(_currentDepthFrame); RealSense2API.ReleaseFrame(_currentLeftFrame); @@ -1603,12 +1710,6 @@ protected override CameraImage CalcChannelImpl(string channelName) protected override void ActivateChannelImpl(String channelName) { - if(IsChannelActive(channelName)) - { - log.Debug(string.Format("Channel {0} is already active", channelName)); - return; - } - RealSense2API.Stream stream = RealSense2API.Stream.ANY; RealSense2API.Format format = RealSense2API.Format.ANY; int res_x = 640; @@ -1623,7 +1724,7 @@ protected override void ActivateChannelImpl(String channelName) res_x = ColorResolution.X; res_y = ColorResolution.Y; - fps = (int)ColorFPS; + fps = ColorFPS; index = -1; } else if (channelName == ChannelNames.ZImage) @@ -1640,7 +1741,7 @@ protected override void ActivateChannelImpl(String channelName) res_x = DepthResolution.X; res_y = DepthResolution.Y; - fps = (int)DepthFPS; + fps = DepthFPS; index = -1; } else if (channelName == ChannelNames.Left) @@ -1674,7 +1775,7 @@ protected override void ActivateChannelImpl(String channelName) res_x = DepthResolution.X; res_y = DepthResolution.Y; - fps = (int)DepthFPS; + fps = DepthFPS; index = 2; } else @@ -1686,13 +1787,21 @@ protected override void ActivateChannelImpl(String channelName) bool running = RealSense2API.PipelineRunning; - if(running) - RealSense2API.PipelineStop(_pipeline); + if (running) + { + _updatingPipeline = true; + StopPipeline(); + } + RealSense2API.ConfigEnableStream(_config, stream, index, res_x, res_y, format, fps); + if(running) - RealSense2API.PipelineStart(_pipeline, _config); + { + StartPipeline(); + _updatingPipeline = false; + } } protected override void DeactivateChannelImpl(String channelName) @@ -1722,14 +1831,21 @@ protected override void DeactivateChannelImpl(String channelName) _currentRightFrame = new RealSense2API.RS2Frame(); bool running = RealSense2API.PipelineRunning; + - if(running) - RealSense2API.PipelineStop(_pipeline); + if (running) + { + _updatingPipeline = true; + StopPipeline(); + } RealSense2API.ConfigDisableStream(_config, stream); if (running) - RealSense2API.PipelineStart(_pipeline, _config); + { + StartPipeline(); + _updatingPipeline = false; + } } unsafe public override IProjectiveTransformation GetIntrinsics(string channelName) From fc6c9d5e69b3581b6f99316b3f0adc82da9703b1 Mon Sep 17 00:00:00 2001 From: Jan Lukas Gernert Date: Wed, 10 Jan 2018 15:34:13 +0100 Subject: [PATCH 22/32] Check configuration before starting the pipeline --- BetaCameras/RealSense2/RealSense2.cs | 7 +++++++ BetaCameras/RealSense2/RealSense2API.cs | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/BetaCameras/RealSense2/RealSense2.cs b/BetaCameras/RealSense2/RealSense2.cs index a1eb3e9e..6bd2b7ca 100644 --- a/BetaCameras/RealSense2/RealSense2.cs +++ b/BetaCameras/RealSense2/RealSense2.cs @@ -1566,6 +1566,13 @@ private void StopPipeline() private void StartPipeline() { + if(!RealSense2API.CheckConfig(_pipeline, _config)) + { + string msg = "RealSense2: No camera that supports the current configuration detected"; + log.Error(msg); + throw new Exception(msg); + } + if (RealSense2API.PipelineRunning) RealSense2API.PipelineStop(_pipeline); diff --git a/BetaCameras/RealSense2/RealSense2API.cs b/BetaCameras/RealSense2/RealSense2API.cs index d03c37fa..24af05e7 100644 --- a/BetaCameras/RealSense2/RealSense2API.cs +++ b/BetaCameras/RealSense2/RealSense2API.cs @@ -606,6 +606,9 @@ public unsafe struct Extrinsics [DllImport("realsense2", CallingConvention = CallingConvention.Cdecl, SetLastError = false)] private unsafe extern static int rs2_get_frame_height(IntPtr frame, IntPtr* error); + + [DllImport("realsense2", CallingConvention = CallingConvention.Cdecl, SetLastError = false)] + private unsafe extern static int rs2_config_can_resolve(IntPtr config, IntPtr pipe, IntPtr* error); #endregion public struct RS2Context @@ -771,6 +774,15 @@ unsafe public static RS2Config CreateConfig() return new RS2Config(conf); } + unsafe public static bool CheckConfig(RS2Pipeline pipe, RS2Config conf) + { + IntPtr error = IntPtr.Zero; + int res = rs2_config_can_resolve(conf.Handle, pipe.Handle, &error); + HandleError(error); + + return res == 1; + } + unsafe public static void DisableAllStreams(RS2Config conf) { IntPtr error = IntPtr.Zero; From f75fcf150294955ac6370d7b325f6ecba48661e4 Mon Sep 17 00:00:00 2001 From: Jan Lukas Gernert Date: Wed, 10 Jan 2018 16:10:50 +0100 Subject: [PATCH 23/32] Fix MetriCam2 choking on Point2i properties --- MetriCam2/Camera.cs | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/MetriCam2/Camera.cs b/MetriCam2/Camera.cs index 572fa930..ae4277e3 100644 --- a/MetriCam2/Camera.cs +++ b/MetriCam2/Camera.cs @@ -704,10 +704,24 @@ public bool IsValid(object value) } else { - T castedItem = (T)Convert.ChangeType(item, this.Type, CultureInfo.InvariantCulture); - if (castedItem.Equals(castedValue)) + + + if(this.Type == typeof(Point2i)) { - return true; + string[] stringValue = item.Split('x'); + Point2i point = new Point2i(int.Parse(stringValue[0]), int.Parse(stringValue[1])); + if(point.Equals(castedValue)) + { + return true; + } + } + else + { + T castedItem = (T)Convert.ChangeType(item, this.Type, CultureInfo.InvariantCulture); + if (castedItem.Equals(castedValue)) + { + return true; + } } } } @@ -2157,6 +2171,10 @@ private static string GetAsGoodString(object value, Type valueType) { valueAsString = ((double)value).ToString("R", CultureInfo.InvariantCulture); } + else if (valueType == typeof(Point2i)) + { + valueAsString = string.Format("{0}x{1}", ((Point2i)value).X, ((Point2i)value).Y); + } else { valueAsString = value.ToString(); From 3564460ff87bb02113b6b032a2787f470ed215a2 Mon Sep 17 00:00:00 2001 From: Jan Lukas Gernert Date: Fri, 12 Jan 2018 14:51:48 +0100 Subject: [PATCH 24/32] rethrow exceptions from HandleErrors --- BetaCameras/RealSense2/RealSense2.cs | 169 +++--- BetaCameras/RealSense2/RealSense2API.cs | 706 ++++++++++++++++-------- 2 files changed, 573 insertions(+), 302 deletions(-) diff --git a/BetaCameras/RealSense2/RealSense2.cs b/BetaCameras/RealSense2/RealSense2.cs index 6bd2b7ca..5e103eac 100644 --- a/BetaCameras/RealSense2/RealSense2.cs +++ b/BetaCameras/RealSense2/RealSense2.cs @@ -1591,104 +1591,111 @@ protected override void UpdateImpl() // wait for pipeline to restart with new settings } - if (!RealSense2API.PipelineRunning) + try { - string msg = "RealSense2: Can't update camera since pipeline is not running"; - log.Error(msg); - throw new Exception(msg); - } - - RealSense2API.ReleaseFrame(_currentColorFrame); - RealSense2API.ReleaseFrame(_currentDepthFrame); - RealSense2API.ReleaseFrame(_currentLeftFrame); - RealSense2API.ReleaseFrame(_currentRightFrame); - - bool getColor = IsChannelActive(ChannelNames.Color); - bool getDepth = IsChannelActive(ChannelNames.ZImage); - bool getLeft = IsChannelActive(ChannelNames.Left); - bool getRight = IsChannelActive(ChannelNames.Right); - bool haveColor = false; - bool haveDepth = false; - bool haveLeft = false; - bool haveRight = false; - - while (true) - { - RealSense2API.RS2Frame data = RealSense2API.PipelineWaitForFrames(_pipeline, 5000); - - if(!data.IsValid() || data.Handle == IntPtr.Zero) + if (!RealSense2API.PipelineRunning) { - RealSense2API.ReleaseFrame(data); - continue; + string msg = "RealSense2: Can't update camera since pipeline is not running"; + log.Error(msg); + throw new Exception(msg); } - int frameCount = RealSense2API.FrameEmbeddedCount(data); - log.Debug(string.Format("RealSense2: Got {0} Frames", frameCount)); - - - // extract all frames - for (int j = 0; j < frameCount; j++) + RealSense2API.ReleaseFrame(_currentColorFrame); + RealSense2API.ReleaseFrame(_currentDepthFrame); + RealSense2API.ReleaseFrame(_currentLeftFrame); + RealSense2API.ReleaseFrame(_currentRightFrame); + + bool getColor = IsChannelActive(ChannelNames.Color); + bool getDepth = IsChannelActive(ChannelNames.ZImage); + bool getLeft = IsChannelActive(ChannelNames.Left); + bool getRight = IsChannelActive(ChannelNames.Right); + bool haveColor = false; + bool haveDepth = false; + bool haveLeft = false; + bool haveRight = false; + + while (true) { - RealSense2API.RS2Frame frame = RealSense2API.FrameExtract(data, j); - RealSense2API.FrameAddRef(frame); + RealSense2API.RS2Frame data = RealSense2API.PipelineWaitForFrames(_pipeline, 5000); - // what kind of frame did we get? - RealSense2API.RS2StreamProfile profile = RealSense2API.GetStreamProfile(frame); - RealSense2API.GetStreamProfileData(profile, out RealSense2API.Stream stream, out RealSense2API.Format format, out int index, out int uid, out int framerate); + if (!data.IsValid() || data.Handle == IntPtr.Zero) + { + RealSense2API.ReleaseFrame(data); + continue; + } - log.Debug(string.Format("RealSense2: Analyzing frame {0}", j + 1)); - log.Debug(string.Format("RealSense2: stream {0}", stream.ToString())); - log.Debug(string.Format("RealSense2: format {0}", format.ToString())); + int frameCount = RealSense2API.FrameEmbeddedCount(data); + log.Debug(string.Format("RealSense2: Got {0} Frames", frameCount)); - switch (stream) + // extract all frames + for (int j = 0; j < frameCount; j++) { - case RealSense2API.Stream.COLOR: - if (getColor) - { - RealSense2API.ReleaseFrame(_currentColorFrame); - _currentColorFrame = frame; - haveColor = true; - } - break; - case RealSense2API.Stream.DEPTH: - if (getDepth) - { - RealSense2API.ReleaseFrame(_currentDepthFrame); - _currentDepthFrame = frame; - haveDepth = true; - } - break; - case RealSense2API.Stream.INFRARED: - if(index == 1) - { - if (getLeft) + RealSense2API.RS2Frame frame = RealSense2API.FrameExtract(data, j); + RealSense2API.FrameAddRef(frame); + + // what kind of frame did we get? + RealSense2API.RS2StreamProfile profile = RealSense2API.GetStreamProfile(frame); + RealSense2API.GetStreamProfileData(profile, out RealSense2API.Stream stream, out RealSense2API.Format format, out int index, out int uid, out int framerate); + + log.Debug(string.Format("RealSense2: Analyzing frame {0}", j + 1)); + log.Debug(string.Format("RealSense2: stream {0}", stream.ToString())); + log.Debug(string.Format("RealSense2: format {0}", format.ToString())); + + + switch (stream) + { + case RealSense2API.Stream.COLOR: + if (getColor) + { + RealSense2API.ReleaseFrame(_currentColorFrame); + _currentColorFrame = frame; + haveColor = true; + } + break; + case RealSense2API.Stream.DEPTH: + if (getDepth) + { + RealSense2API.ReleaseFrame(_currentDepthFrame); + _currentDepthFrame = frame; + haveDepth = true; + } + break; + case RealSense2API.Stream.INFRARED: + if (index == 1) { - RealSense2API.ReleaseFrame(_currentLeftFrame); - _currentLeftFrame = frame; - haveLeft = true; + if (getLeft) + { + RealSense2API.ReleaseFrame(_currentLeftFrame); + _currentLeftFrame = frame; + haveLeft = true; + } } - } - else if(index == 2) - { - if (getRight) + else if (index == 2) { - RealSense2API.ReleaseFrame(_currentRightFrame); - _currentRightFrame = frame; - haveRight = true; + if (getRight) + { + RealSense2API.ReleaseFrame(_currentRightFrame); + _currentRightFrame = frame; + haveRight = true; + } } - } - break; + break; + } } - } - RealSense2API.ReleaseFrame(data); + RealSense2API.ReleaseFrame(data); - if (((getColor && haveColor) || !getColor) - && ((getDepth && haveDepth) || !getDepth) - && ((getLeft && haveLeft) || !getLeft) - && ((getRight && haveRight) || !getRight)) - break; + if (((getColor && haveColor) || !getColor) + && ((getDepth && haveDepth) || !getDepth) + && ((getLeft && haveLeft) || !getLeft) + && ((getRight && haveRight) || !getRight)) + break; + } + } + catch (Exception) + { + throw; } } diff --git a/BetaCameras/RealSense2/RealSense2API.cs b/BetaCameras/RealSense2/RealSense2API.cs index 24af05e7..3d7d595c 100644 --- a/BetaCameras/RealSense2/RealSense2API.cs +++ b/BetaCameras/RealSense2/RealSense2API.cs @@ -691,10 +691,17 @@ unsafe public int Width { get { - IntPtr error = IntPtr.Zero; - int width = rs2_get_frame_width(Handle, &error); - HandleError(error); - return width; + try + { + IntPtr error = IntPtr.Zero; + int width = rs2_get_frame_width(Handle, &error); + HandleError(error); + return width; + } + catch (Exception) + { + throw; + } } } @@ -702,10 +709,17 @@ unsafe public int Height { get { - IntPtr error = IntPtr.Zero; - int height = rs2_get_frame_height(Handle, &error); - HandleError(error); - return height; + try + { + IntPtr error = IntPtr.Zero; + int height = rs2_get_frame_height(Handle, &error); + HandleError(error); + return height; + } + catch (Exception) + { + throw; + } } } @@ -749,70 +763,126 @@ public void Delete() unsafe public static RS2Context CreateContext() { - IntPtr error = IntPtr.Zero; - IntPtr ctx = rs2_create_context(ApiVersion, &error); - HandleError(error); + try + { + IntPtr error = IntPtr.Zero; + IntPtr ctx = rs2_create_context(ApiVersion, &error); + HandleError(error); - return new RS2Context(ctx); + return new RS2Context(ctx); + } + catch (Exception) + { + throw; + } } unsafe public static RS2Pipeline CreatePipeline(RS2Context ctx) { - IntPtr error = IntPtr.Zero; - IntPtr pipe = rs2_create_pipeline(ctx.Handle, &error); - HandleError(error); + try + { + IntPtr error = IntPtr.Zero; + IntPtr pipe = rs2_create_pipeline(ctx.Handle, &error); + HandleError(error); - return new RS2Pipeline(pipe); + return new RS2Pipeline(pipe); + } + catch (Exception) + { + throw; + } } unsafe public static RS2Config CreateConfig() { - IntPtr error = IntPtr.Zero; - IntPtr conf = rs2_create_config(&error); - HandleError(error); + try + { + IntPtr error = IntPtr.Zero; + IntPtr conf = rs2_create_config(&error); + HandleError(error); - return new RS2Config(conf); + return new RS2Config(conf); + } + catch (Exception) + { + throw; + } } unsafe public static bool CheckConfig(RS2Pipeline pipe, RS2Config conf) { - IntPtr error = IntPtr.Zero; - int res = rs2_config_can_resolve(conf.Handle, pipe.Handle, &error); - HandleError(error); + try + { + IntPtr error = IntPtr.Zero; + int res = rs2_config_can_resolve(conf.Handle, pipe.Handle, &error); + HandleError(error); - return res == 1; + return res == 1; + } + catch (Exception) + { + throw; + } } unsafe public static void DisableAllStreams(RS2Config conf) { - IntPtr error = IntPtr.Zero; - rs2_config_disable_all_streams(conf.Handle, &error); - HandleError(error); + try + { + IntPtr error = IntPtr.Zero; + rs2_config_disable_all_streams(conf.Handle, &error); + HandleError(error); + } + catch (Exception) + { + throw; + } } unsafe public static void EnableDevice(RS2Config conf, string serial_number) { - IntPtr error = IntPtr.Zero; - rs2_config_enable_device(conf.Handle, serial_number, &error); - HandleError(error); + try + { + IntPtr error = IntPtr.Zero; + rs2_config_enable_device(conf.Handle, serial_number, &error); + HandleError(error); + } + catch (Exception) + { + throw; + } } unsafe public static void PipelineStart(RS2Pipeline pipe, RS2Config conf) { - IntPtr error = IntPtr.Zero; - rs2_pipeline_start_with_config(pipe.Handle, conf.Handle, &error); - HandleError(error); + try + { + IntPtr error = IntPtr.Zero; + rs2_pipeline_start_with_config(pipe.Handle, conf.Handle, &error); + HandleError(error); - PipelineRunning = true; + PipelineRunning = true; + } + catch (Exception) + { + throw; + } } unsafe public static void PipelineStop(RS2Pipeline pipe) { - IntPtr error = IntPtr.Zero; - rs2_pipeline_stop(pipe.Handle, &error); - HandleError(error); + try + { + IntPtr error = IntPtr.Zero; + rs2_pipeline_stop(pipe.Handle, &error); + HandleError(error); - PipelineRunning = false; + PipelineRunning = false; + } + catch (Exception) + { + throw; + } } unsafe public static void ReleaseFrame(RS2Frame frame) @@ -822,114 +892,197 @@ unsafe public static void ReleaseFrame(RS2Frame frame) unsafe public static RS2Frame PipelineWaitForFrames(RS2Pipeline pipe, uint timeout) { - IntPtr error = IntPtr.Zero; - IntPtr frameset = rs2_pipeline_wait_for_frames(pipe.Handle, timeout, &error); - HandleError(error); - - return new RS2Frame(frameset); + try + { + IntPtr error = IntPtr.Zero; + IntPtr frameset = rs2_pipeline_wait_for_frames(pipe.Handle, timeout, &error); + HandleError(error); + return new RS2Frame(frameset); + } + catch(Exception) + { + throw; + } } unsafe public static void FrameAddRef(RS2Frame frame) { - IntPtr error = IntPtr.Zero; - rs2_frame_add_ref(frame.Handle, &error); - HandleError(error); + try + { + IntPtr error = IntPtr.Zero; + rs2_frame_add_ref(frame.Handle, &error); + HandleError(error); + } + catch (Exception) + { + throw; + } } unsafe public static int FrameEmbeddedCount(RS2Frame frame) { - IntPtr error = IntPtr.Zero; - int count = rs2_embedded_frames_count(frame.Handle, &error); - HandleError(error); + try + { + IntPtr error = IntPtr.Zero; + int count = rs2_embedded_frames_count(frame.Handle, &error); + HandleError(error); - return count; + return count; + } + catch (Exception) + { + throw; + } } unsafe public static RS2Frame FrameExtract(RS2Frame frame, int index) { - IntPtr error = IntPtr.Zero; - IntPtr extractedFramePtr = rs2_extract_frame(frame.Handle, index, &error); - HandleError(error); + try + { + IntPtr error = IntPtr.Zero; + IntPtr extractedFramePtr = rs2_extract_frame(frame.Handle, index, &error); + HandleError(error); - return new RS2Frame(extractedFramePtr); + return new RS2Frame(extractedFramePtr); + } + catch (Exception) + { + throw; + } } unsafe public static RS2StreamProfile GetStreamProfile(RS2Frame frame) { - IntPtr error = IntPtr.Zero; - IntPtr profilePtr = rs2_get_frame_stream_profile(frame.Handle, &error); - HandleError(error); + try + { + IntPtr error = IntPtr.Zero; + IntPtr profilePtr = rs2_get_frame_stream_profile(frame.Handle, &error); + HandleError(error); - return new RS2StreamProfile(profilePtr); + return new RS2StreamProfile(profilePtr); + } + catch (Exception) + { + throw; + } } unsafe public static RS2StreamProfile GetStreamProfile(RS2StreamProfileList list, int index) { - IntPtr error = IntPtr.Zero; - IntPtr profilePtr = rs2_get_stream_profile(list.Handle, index, &error); - HandleError(error); + try + { + IntPtr error = IntPtr.Zero; + IntPtr profilePtr = rs2_get_stream_profile(list.Handle, index, &error); + HandleError(error); - return new RS2StreamProfile(profilePtr); + return new RS2StreamProfile(profilePtr); + } + catch (Exception) + { + throw; + } } unsafe public static void GetStreamProfileData(RS2StreamProfile profile, out Stream stream, out Format format, out int index, out int uid, out int framerate) { - IntPtr error = IntPtr.Zero; - stream = Stream.ANY; - format = Format.ANY; - index = 0; - uid = 0; - framerate = 0; - - rs2_get_stream_profile_data(profile.Handle, ref stream, ref format, ref index, ref uid, ref framerate, &error); - HandleError(error); + try + { + IntPtr error = IntPtr.Zero; + stream = Stream.ANY; + format = Format.ANY; + index = 0; + uid = 0; + framerate = 0; + + rs2_get_stream_profile_data(profile.Handle, ref stream, ref format, ref index, ref uid, ref framerate, &error); + HandleError(error); + } + catch (Exception) + { + throw; + } } unsafe public static Point2i GetStreamProfileResolution(RS2StreamProfile profile) { - IntPtr error = IntPtr.Zero; + try + { + IntPtr error = IntPtr.Zero; - int width = 0; - int height = 0; - rs2_get_video_stream_resolution(profile.Handle, ref width, ref height, &error); - HandleError(error); + int width = 0; + int height = 0; + rs2_get_video_stream_resolution(profile.Handle, ref width, ref height, &error); + HandleError(error); - return new Point2i(width, height); + return new Point2i(width, height); + } + catch (Exception) + { + throw; + } } unsafe public static void ConfigEnableStream(RS2Config conf, Stream stream, int index, int width, int height, Format format, int framerate) { - IntPtr error = IntPtr.Zero; - rs2_config_enable_stream(conf.Handle, stream, index, width, height, format, framerate, &error); - HandleError(error); + try + { + IntPtr error = IntPtr.Zero; + rs2_config_enable_stream(conf.Handle, stream, index, width, height, format, framerate, &error); + HandleError(error); + } + catch (Exception) + { + throw; + } } unsafe public static void ConfigDisableStream(RS2Config conf, Stream stream) { - IntPtr error = IntPtr.Zero; - rs2_config_disable_stream(conf.Handle, stream, &error); - HandleError(error); + try + { + IntPtr error = IntPtr.Zero; + rs2_config_disable_stream(conf.Handle, stream, &error); + HandleError(error); + } + catch (Exception) + { + throw; + } } unsafe public static IntPtr GetFrameData(RS2Frame frame) { - IntPtr error = IntPtr.Zero; - IntPtr data = rs2_get_frame_data(frame.Handle, &error); - HandleError(error); + try + { + IntPtr error = IntPtr.Zero; + IntPtr data = rs2_get_frame_data(frame.Handle, &error); + HandleError(error); - return data; + return data; + } + catch (Exception) + { + throw; + } } unsafe public static RS2Device GetActiveDevice(RS2Pipeline pipe) { - IntPtr error = IntPtr.Zero; - IntPtr profile = rs2_pipeline_get_active_profile(pipe.Handle, &error); - HandleError(error); - IntPtr device = rs2_pipeline_profile_get_device(profile, &error); - HandleError(error); - - rs2_delete_pipeline_profile(profile); - return new RS2Device(device); + try + { + IntPtr error = IntPtr.Zero; + IntPtr profile = rs2_pipeline_get_active_profile(pipe.Handle, &error); + HandleError(error); + IntPtr device = rs2_pipeline_profile_get_device(profile, &error); + HandleError(error); + + rs2_delete_pipeline_profile(profile); + return new RS2Device(device); + } + catch (Exception) + { + throw; + } } unsafe public static void DeleteDevice(RS2Device device) @@ -939,238 +1092,350 @@ unsafe public static void DeleteDevice(RS2Device device) unsafe public static bool AdvancedModeEnabled(RS2Device device) { - IntPtr error = IntPtr.Zero; - int enabled = 0; - rs2_is_enabled(device.Handle, ref enabled, &error); - HandleError(error); + try + { + IntPtr error = IntPtr.Zero; + int enabled = 0; + rs2_is_enabled(device.Handle, ref enabled, &error); + HandleError(error); - return enabled != 0; + return enabled != 0; + } + catch (Exception) + { + throw; + } } unsafe public static void EnabledAdvancedMode(RS2Device device, bool enable) { - IntPtr error = IntPtr.Zero; - rs2_toggle_advanced_mode(device.Handle, enable ? 1 : 0, &error); - HandleError(error); + try + { + IntPtr error = IntPtr.Zero; + rs2_toggle_advanced_mode(device.Handle, enable ? 1 : 0, &error); + HandleError(error); + } + catch (Exception) + { + throw; + } } unsafe public static void LoadAdvancedConfig(string config, RS2Device device) { - IntPtr error = IntPtr.Zero; - rs2_load_json(device.Handle, config, (uint)config.ToCharArray().Length, &error); - HandleError(error); + try + { + IntPtr error = IntPtr.Zero; + rs2_load_json(device.Handle, config, (uint)config.ToCharArray().Length, &error); + HandleError(error); + } + catch (Exception) + { + throw; + } } unsafe public static Intrinsics GetIntrinsics(RS2StreamProfile profile) { - IntPtr error = IntPtr.Zero; - Intrinsics intrinsics = new Intrinsics(); - rs2_get_video_stream_intrinsics(profile.Handle, &intrinsics, &error); - HandleError(error); + try + { + IntPtr error = IntPtr.Zero; + Intrinsics intrinsics = new Intrinsics(); + rs2_get_video_stream_intrinsics(profile.Handle, &intrinsics, &error); + HandleError(error); - return intrinsics; + return intrinsics; + } + catch (Exception) + { + throw; + } } unsafe public static Extrinsics GetExtrinsics(RS2StreamProfile from, RS2StreamProfile to) { - IntPtr error = IntPtr.Zero; - Extrinsics extrinsics = new Extrinsics(); - rs2_get_extrinsics(from.Handle, to.Handle, &extrinsics, &error); - HandleError(error); + try + { + IntPtr error = IntPtr.Zero; + Extrinsics extrinsics = new Extrinsics(); + rs2_get_extrinsics(from.Handle, to.Handle, &extrinsics, &error); + HandleError(error); - return extrinsics; + return extrinsics; + } + catch (Exception) + { + throw; + } } unsafe private static RS2Sensor GetSensor(RS2Pipeline pipe, string sensorName) { - IntPtr error = IntPtr.Zero; - IntPtr sensor = IntPtr.Zero; - - RS2Device dev = GetActiveDevice(pipe); - IntPtr list = rs2_query_sensors(dev.Handle, &error); - HandleError(error); - int sensorCount = rs2_get_sensors_count(list, &error); - HandleError(error); - - for (int i = 0; i < sensorCount; i++) + try { - sensor = rs2_create_sensor(list, i, &error); - HandleError(error); + IntPtr error = IntPtr.Zero; + IntPtr sensor = IntPtr.Zero; - char* info = rs2_get_sensor_info(sensor, CameraInfo.NAME, &error); - string infoString = Marshal.PtrToStringAnsi((IntPtr)info); + RS2Device dev = GetActiveDevice(pipe); + IntPtr list = rs2_query_sensors(dev.Handle, &error); + HandleError(error); + int sensorCount = rs2_get_sensors_count(list, &error); HandleError(error); - if (infoString == sensorName) + for (int i = 0; i < sensorCount; i++) { - break; + sensor = rs2_create_sensor(list, i, &error); + HandleError(error); + + char* info = rs2_get_sensor_info(sensor, CameraInfo.NAME, &error); + string infoString = Marshal.PtrToStringAnsi((IntPtr)info); + HandleError(error); + + if (infoString == sensorName) + { + break; + } + + rs2_delete_sensor(sensor); } - rs2_delete_sensor(sensor); - } + rs2_delete_sensor_list(list); + DeleteDevice(dev); - rs2_delete_sensor_list(list); - DeleteDevice(dev); + RS2Sensor sensor_obj = new RS2Sensor(sensor); + if (!sensor_obj.IsValid()) + { + throw new Exception(string.Format("No sensor with the name {0} detected", sensorName)); + } - RS2Sensor sensor_obj = new RS2Sensor(sensor); - if (!sensor_obj.IsValid()) + return sensor_obj; + } + catch (Exception) { - throw new Exception(string.Format("No sensor with the name {0} detected", sensorName)); + throw; } - - return sensor_obj; } unsafe public static float GetDepthScale(RS2Pipeline pipe) { - IntPtr error = IntPtr.Zero; - float scale = 0.0f; + try + { + IntPtr error = IntPtr.Zero; + float scale = 0.0f; - RS2Sensor sensor = GetSensor(pipe, SensorName.STEREO); - scale = rs2_get_depth_scale(sensor.Handle, &error); - HandleError(error); + RS2Sensor sensor = GetSensor(pipe, SensorName.STEREO); + scale = rs2_get_depth_scale(sensor.Handle, &error); + HandleError(error); - sensor.Delete(); - return scale; + sensor.Delete(); + return scale; + } + catch (Exception) + { + throw; + } } unsafe public static string GetFirmwareVersion(RS2Pipeline pipe) { - IntPtr error = IntPtr.Zero; + try + { + IntPtr error = IntPtr.Zero; - RS2Device dev = GetActiveDevice(pipe); - IntPtr list = rs2_query_sensors(dev.Handle, &error); - HandleError(error); - int sensorCount = rs2_get_sensors_count(list, &error); - HandleError(error); + RS2Device dev = GetActiveDevice(pipe); + IntPtr list = rs2_query_sensors(dev.Handle, &error); + HandleError(error); + int sensorCount = rs2_get_sensors_count(list, &error); + HandleError(error); - if (sensorCount == 0) - throw new InvalidOperationException("No sensor detected to get firmware version from"); + if (sensorCount == 0) + throw new InvalidOperationException("No sensor detected to get firmware version from"); - IntPtr sensor = rs2_create_sensor(list, 0, &error); - HandleError(error); + IntPtr sensor = rs2_create_sensor(list, 0, &error); + HandleError(error); - char* info = rs2_get_sensor_info(sensor, CameraInfo.FIRMWARE_VERSION, &error); - string infoString = Marshal.PtrToStringAnsi((IntPtr)info); - HandleError(error); + char* info = rs2_get_sensor_info(sensor, CameraInfo.FIRMWARE_VERSION, &error); + string infoString = Marshal.PtrToStringAnsi((IntPtr)info); + HandleError(error); - rs2_delete_sensor_list(list); - DeleteDevice(dev); + rs2_delete_sensor_list(list); + DeleteDevice(dev); - return infoString; + return infoString; + } + catch (Exception) + { + throw; + } } unsafe public static bool IsOptionSupported(RS2Pipeline pipe, string SensorName, Option option) { - IntPtr error = IntPtr.Zero; - int res = 0; + try + { + IntPtr error = IntPtr.Zero; + int res = 0; - RS2Sensor sensor = GetSensor(pipe, SensorName); - res = rs2_supports_option(sensor.Handle, option, &error); - HandleError(error); + RS2Sensor sensor = GetSensor(pipe, SensorName); + res = rs2_supports_option(sensor.Handle, option, &error); + HandleError(error); - sensor.Delete(); + sensor.Delete(); - if (res == 1) - return true; + if (res == 1) + return true; - return false; + return false; + } + catch (Exception) + { + throw; + } } unsafe public static bool IsOptionRealOnly(RS2Pipeline pipe, string SensorName, Option option) { - IntPtr error = IntPtr.Zero; - int res = 0; + try + { + IntPtr error = IntPtr.Zero; + int res = 0; - RS2Sensor sensor = GetSensor(pipe, SensorName); - res = rs2_is_option_read_only(sensor.Handle, option, &error); - HandleError(error); + RS2Sensor sensor = GetSensor(pipe, SensorName); + res = rs2_is_option_read_only(sensor.Handle, option, &error); + HandleError(error); - sensor.Delete(); + sensor.Delete(); - if (res == 1) - return true; + if (res == 1) + return true; - return false; + return false; + } + catch (Exception) + { + throw; + } } unsafe public static float GetOption(RS2Pipeline pipe, string SensorName, Option option) { - IntPtr error = IntPtr.Zero; - float res = 0.0f; + try + { + IntPtr error = IntPtr.Zero; + float res = 0.0f; - RS2Sensor sensor = GetSensor(pipe, SensorName); - res = rs2_get_option(sensor.Handle, option, &error); - HandleError(error); + RS2Sensor sensor = GetSensor(pipe, SensorName); + res = rs2_get_option(sensor.Handle, option, &error); + HandleError(error); - sensor.Delete(); - return res; + sensor.Delete(); + return res; + } + catch (Exception) + { + throw; + } } unsafe public static void SetOption(RS2Pipeline pipe, string SensorName, Option option, float value) { - IntPtr error = IntPtr.Zero; + try + { + IntPtr error = IntPtr.Zero; - RS2Sensor sensor = GetSensor(pipe, SensorName); - rs2_set_option(sensor.Handle, option, value, &error); - HandleError(error); + RS2Sensor sensor = GetSensor(pipe, SensorName); + rs2_set_option(sensor.Handle, option, value, &error); + HandleError(error); - sensor.Delete(); + sensor.Delete(); + } + catch (Exception) + { + throw; + } } unsafe public static void QueryOptionInfo(RS2Pipeline pipe, string SensorName, Option option, out float min, out float max, out float step, out float def, out string desc) { - IntPtr error = IntPtr.Zero; - char* msg = null; + try + { + IntPtr error = IntPtr.Zero; + char* msg = null; - min = 0; - max = 0; - step = 0; - def = 0; + min = 0; + max = 0; + step = 0; + def = 0; - RS2Sensor sensor = GetSensor(pipe, SensorName); - rs2_get_option_range(sensor.Handle, option, ref min, ref max, ref step, ref def, &error); + RS2Sensor sensor = GetSensor(pipe, SensorName); + rs2_get_option_range(sensor.Handle, option, ref min, ref max, ref step, ref def, &error); - msg = rs2_get_option_description(sensor.Handle, option, &error); - HandleError(error); - desc = Marshal.PtrToStringAnsi((IntPtr)msg); + msg = rs2_get_option_description(sensor.Handle, option, &error); + HandleError(error); + desc = Marshal.PtrToStringAnsi((IntPtr)msg); - sensor.Delete(); + sensor.Delete(); + } + catch (Exception) + { + throw; + } } unsafe public static string OptionValueInfo(RS2Pipeline pipe, string SensorName, Option option, float value) { - IntPtr error = IntPtr.Zero; - char* msg = null; + try + { + IntPtr error = IntPtr.Zero; + char* msg = null; - RS2Sensor sensor = GetSensor(pipe, SensorName); - msg = rs2_get_option_value_description(sensor.Handle, option, value, &error); + RS2Sensor sensor = GetSensor(pipe, SensorName); + msg = rs2_get_option_value_description(sensor.Handle, option, value, &error); + HandleError(error); - return Marshal.PtrToStringAnsi((IntPtr)msg); + return Marshal.PtrToStringAnsi((IntPtr)msg); + } + catch (Exception) + { + throw; + } } unsafe public static RS2StreamProfileList GetStreamProfileList(RS2Sensor sensor) { - IntPtr error = IntPtr.Zero; - IntPtr list = rs2_get_stream_profiles(sensor.Handle, &error); - HandleError(error); + try + { + IntPtr error = IntPtr.Zero; + IntPtr list = rs2_get_stream_profiles(sensor.Handle, &error); + HandleError(error); - return new RS2StreamProfileList(list); + return new RS2StreamProfileList(list); + } + catch (Exception) + { + throw; + } } unsafe public static int GetStreamProfileListCount(RS2StreamProfileList list) { - IntPtr error = IntPtr.Zero; - int count = rs2_get_stream_profiles_count(list.Handle, &error); - HandleError(error); + try + { + IntPtr error = IntPtr.Zero; + int count = rs2_get_stream_profiles_count(list.Handle, &error); + HandleError(error); - return count; + return count; + } + catch (Exception) + { + throw; + } } unsafe public static List GetSupportedResolutions(RS2Pipeline pipe, string sensorName) { - IntPtr error = IntPtr.Zero; List res = new List(); RS2Sensor sensor = GetSensor(pipe, sensorName); RS2StreamProfileList list = GetStreamProfileList(sensor); @@ -1192,7 +1457,6 @@ unsafe public static List GetSupportedResolutions(RS2Pipeline pipe, str unsafe public static List GetSupportedFrameRates(RS2Pipeline pipe, string sensorName) { - IntPtr error = IntPtr.Zero; List res = new List(); RS2Sensor sensor = GetSensor(pipe, sensorName); RS2StreamProfileList list = GetStreamProfileList(sensor); From f258558bea48bc25cf01c8aa85e0fca480d85279 Mon Sep 17 00:00:00 2001 From: Jan Lukas Gernert Date: Mon, 15 Jan 2018 13:20:43 +0100 Subject: [PATCH 25/32] Handle Point2i property and description in CameraSettingsControl --- MetriCam2.Controls/CameraSettingsControl.cs | 68 ++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/MetriCam2.Controls/CameraSettingsControl.cs b/MetriCam2.Controls/CameraSettingsControl.cs index 53b3ce8c..29e6f038 100644 --- a/MetriCam2.Controls/CameraSettingsControl.cs +++ b/MetriCam2.Controls/CameraSettingsControl.cs @@ -11,6 +11,7 @@ using System.Text.RegularExpressions; using System.Threading; using System.Windows.Forms; +using Metrilus.Util; namespace MetriCam2.Controls { @@ -104,6 +105,9 @@ public Camera Camera // use cam.Channels; // Update child controls. InitConfigurationParameters(cam); + + cam.OnConnected += (camera) => { InitConfigurationParameters(camera); }; + cam.OnDisconnected += (camera) => { InitConfigurationParameters(camera); }; } } @@ -235,6 +239,14 @@ private void InitConfigurationParameters(Camera cam) { ContainsOneOrMoreWritableParameters = true; } + scrollbarValue.ValueChanged += (sender, e) => + { + string parameterValue = scrollbarValue.Value.ToString(CultureInfo.InvariantCulture); + string parameterName = scrollbarValue.Name.Replace(VALUE_SUFFIX, string.Empty); + Dictionary keyValues = new Dictionary(); + keyValues.Add(parameterName, parameterValue); + Camera.SetParameters(keyValues); + }; } else if (paramDesc is MetriCam2.Camera.RangeParamDesc) { @@ -246,6 +258,15 @@ private void InitConfigurationParameters(Camera cam) { ContainsOneOrMoreWritableParameters = true; } + + upDownValue.ValueChanged += (sender, e) => + { + string parameterValue = upDownValue.Value.ToString(CultureInfo.InvariantCulture); + string parameterName = upDownValue.Name.Replace(VALUE_SUFFIX, string.Empty); + Dictionary keyValues = new Dictionary(); + keyValues.Add(parameterName, parameterValue); + Camera.SetParameters(keyValues); + }; } else { @@ -274,6 +295,31 @@ private void InitConfigurationParameters(Camera cam) ContainsOneOrMoreWritableParameters = true; } + if(paramDesc is MetriCam2.Camera.ListParamDesc + || paramDesc is MetriCam2.Camera.ListParamDesc) + { + comboBoxValue.SelectedValueChanged += (sender, e) => + { + object parameterValue; + + if (paramDesc is MetriCam2.Camera.ListParamDesc) + { + string selectedText = comboBoxValue.SelectedItem as string; + string[] stringValue = selectedText.Split('x'); + parameterValue = new Point2i(int.Parse(stringValue[0]), int.Parse(stringValue[1])); + } + else + { + parameterValue = int.Parse(comboBoxValue.SelectedItem as string); + } + + string parameterName = comboBoxValue.Name.Replace(VALUE_SUFFIX, string.Empty); + Dictionary keyValues = new Dictionary(); + keyValues.Add(parameterName, parameterValue); + Camera.SetParameters(keyValues); + }; + } + continue; } @@ -304,6 +350,15 @@ private void InitConfigurationParameters(Camera cam) ContainsOneOrMoreWritableParameters = true; } + checkBoxValue.CheckStateChanged += (sender, e) => + { + string parameterValue = checkBoxValue.Checked.ToString(CultureInfo.InvariantCulture); + string parameterName = checkBoxValue.Name.Replace(VALUE_SUFFIX, string.Empty); + Dictionary keyValues = new Dictionary(); + keyValues.Add(parameterName, parameterValue); + Camera.SetParameters(keyValues); + }; + continue; } @@ -469,7 +524,18 @@ private ComboBox CreateComboBox(Camera.IListParamDesc listParamDesc, int current } else { - object tmpVal = Convert.ChangeType(item, paramDesc.Type, CultureInfo.InvariantCulture); + object tmpVal; + + if (paramDesc is MetriCam2.Camera.ListParamDesc) + { + string[] stringValue = item.Split('x'); + tmpVal = new Point2i(int.Parse(stringValue[0]), int.Parse(stringValue[1])); + } + else + { + tmpVal = Convert.ChangeType(item, paramDesc.Type, CultureInfo.InvariantCulture); + } + if (null != tmpVal && paramDesc.Value.Equals(tmpVal)) { comboBoxValue.SelectedIndex = i; From f7878e8b56f048e84dc24d10bba689f486ca3f73 Mon Sep 17 00:00:00 2001 From: Jan Lukas Gernert Date: Mon, 15 Jan 2018 14:24:15 +0100 Subject: [PATCH 26/32] RealSense2: remove exception when certain channels are active at the same time. This seems to be a limitation of old libRealSense2 versions that no longer are valid --- BetaCameras/RealSense2/RealSense2.cs | 21 --------------------- 1 file changed, 21 deletions(-) diff --git a/BetaCameras/RealSense2/RealSense2.cs b/BetaCameras/RealSense2/RealSense2.cs index 5e103eac..69bfe0ce 100644 --- a/BetaCameras/RealSense2/RealSense2.cs +++ b/BetaCameras/RealSense2/RealSense2.cs @@ -1743,13 +1743,6 @@ protected override void ActivateChannelImpl(String channelName) } else if (channelName == ChannelNames.ZImage) { - if (IsChannelActive(ChannelNames.Left) || IsChannelActive(ChannelNames.Right)) - { - string msg = string.Format("RealSense2: can't have {0}/{1} and {2} active at the same time", ChannelNames.Left, ChannelNames.Right, ChannelNames.ZImage); - log.Error(msg); - throw new Exception(msg); - } - stream = RealSense2API.Stream.DEPTH; format = RealSense2API.Format.Z16; @@ -1760,13 +1753,6 @@ protected override void ActivateChannelImpl(String channelName) } else if (channelName == ChannelNames.Left) { - if(IsChannelActive(ChannelNames.ZImage)) - { - string msg = string.Format("RealSense2: can't have {0} and {1} active at the same time", ChannelNames.Left, ChannelNames.ZImage); - log.Error(msg); - throw new Exception(msg); - } - stream = RealSense2API.Stream.INFRARED; format = RealSense2API.Format.Y8; @@ -1777,13 +1763,6 @@ protected override void ActivateChannelImpl(String channelName) } else if (channelName == ChannelNames.Right) { - if (IsChannelActive(ChannelNames.ZImage)) - { - string msg = string.Format("RealSense2: can't have {0} and {1} active at the same time", ChannelNames.Right, ChannelNames.ZImage); - log.Error(msg); - throw new Exception(msg); - } - stream = RealSense2API.Stream.INFRARED; format = RealSense2API.Format.Y8; From d28440a6cc652c8a896b1464286a3ca9c0074189 Mon Sep 17 00:00:00 2001 From: Jan Lukas Gernert Date: Mon, 15 Jan 2018 15:53:40 +0100 Subject: [PATCH 27/32] Only use captured frames as a fallback to get intrinsics, try to get them directly from the sensor first --- BetaCameras/RealSense2/RealSense2.cs | 47 ++++++++++++++++++++++--- BetaCameras/RealSense2/RealSense2API.cs | 4 ++- 2 files changed, 46 insertions(+), 5 deletions(-) diff --git a/BetaCameras/RealSense2/RealSense2.cs b/BetaCameras/RealSense2/RealSense2.cs index 69bfe0ce..16e1d86c 100644 --- a/BetaCameras/RealSense2/RealSense2.cs +++ b/BetaCameras/RealSense2/RealSense2.cs @@ -1843,7 +1843,46 @@ protected override void DeactivateChannelImpl(String channelName) unsafe public override IProjectiveTransformation GetIntrinsics(string channelName) { - RealSense2API.RS2StreamProfile profile = GetProfileForChannelName(channelName); + string sensorName; + Point2i refResolution; + + switch (channelName) + { + case ChannelNames.Color: + sensorName = RealSense2API.SensorName.COLOR; + refResolution = ColorResolution; + break; + case ChannelNames.ZImage: + case ChannelNames.Left: + case ChannelNames.Right: + default: + sensorName = RealSense2API.SensorName.STEREO; + refResolution = DepthResolution; + break; + } + + RealSense2API.RS2StreamProfile profile = new RealSense2API.RS2StreamProfile(IntPtr.Zero); + RealSense2API.RS2Sensor sensor = RealSense2API.GetSensor(_pipeline, sensorName); + RealSense2API.RS2StreamProfileList list = RealSense2API.GetStreamProfileList(sensor); + int count = RealSense2API.GetStreamProfileListCount(list); + + for (int i = 0; i < count; i++) + { + RealSense2API.RS2StreamProfile p = RealSense2API.GetStreamProfile(list, i); + Point2i resolution = RealSense2API.GetStreamProfileResolution(p); + if (resolution == refResolution) + { + profile = p; + break; + } + } + + if(!profile.IsValid()) + { + // try to get profile from captured frame + profile = GetProfileFromCapturedFrames(channelName); + } + RealSense2API.Intrinsics intrinsics = RealSense2API.GetIntrinsics(profile); if(intrinsics.model != RealSense2API.DistortionModel.BROWN_CONRADY) @@ -1867,7 +1906,7 @@ unsafe public override IProjectiveTransformation GetIntrinsics(string channelNam intrinsics.coeffs[4]); } - private RealSense2API.RS2StreamProfile GetProfileForChannelName(string channelName) + private RealSense2API.RS2StreamProfile GetProfileFromCapturedFrames(string channelName) { RealSense2API.RS2Frame frame; @@ -1907,8 +1946,8 @@ private RealSense2API.RS2StreamProfile GetProfileForChannelName(string channelNa unsafe public override RigidBodyTransformation GetExtrinsics(string channelFromName, string channelToName) { - RealSense2API.RS2StreamProfile from = GetProfileForChannelName(channelFromName); - RealSense2API.RS2StreamProfile to = GetProfileForChannelName(channelToName); + RealSense2API.RS2StreamProfile from = GetProfileFromCapturedFrames(channelFromName); + RealSense2API.RS2StreamProfile to = GetProfileFromCapturedFrames(channelToName); RealSense2API.Extrinsics extrinsics = RealSense2API.GetExtrinsics(from, to); diff --git a/BetaCameras/RealSense2/RealSense2API.cs b/BetaCameras/RealSense2/RealSense2API.cs index 3d7d595c..fafbc87d 100644 --- a/BetaCameras/RealSense2/RealSense2API.cs +++ b/BetaCameras/RealSense2/RealSense2API.cs @@ -759,6 +759,8 @@ public void Delete() { rs2_delete_stream_profile(Handle); } + + public bool IsValid() => (null != Handle); } unsafe public static RS2Context CreateContext() @@ -1169,7 +1171,7 @@ unsafe public static Extrinsics GetExtrinsics(RS2StreamProfile from, RS2StreamPr } } - unsafe private static RS2Sensor GetSensor(RS2Pipeline pipe, string sensorName) + unsafe public static RS2Sensor GetSensor(RS2Pipeline pipe, string sensorName) { try { From 2f15f9b624dab425c431bc079bd0c30d6604a969 Mon Sep 17 00:00:00 2001 From: Jan Lukas Gernert Date: Mon, 15 Jan 2018 16:16:11 +0100 Subject: [PATCH 28/32] Try to get extrinsics from sensor first before checking recorded frames as well --- BetaCameras/RealSense2/RealSense2.cs | 71 ++++++++++++++++------------ 1 file changed, 41 insertions(+), 30 deletions(-) diff --git a/BetaCameras/RealSense2/RealSense2.cs b/BetaCameras/RealSense2/RealSense2.cs index 16e1d86c..994879cd 100644 --- a/BetaCameras/RealSense2/RealSense2.cs +++ b/BetaCameras/RealSense2/RealSense2.cs @@ -1842,6 +1842,38 @@ protected override void DeactivateChannelImpl(String channelName) } unsafe public override IProjectiveTransformation GetIntrinsics(string channelName) + { + RealSense2API.RS2StreamProfile profile = GetProfileFromSensor(channelName); + if (!profile.IsValid()) + { + // try to get profile from captured frame + profile = GetProfileFromCapturedFrames(channelName); + } + + RealSense2API.Intrinsics intrinsics = RealSense2API.GetIntrinsics(profile); + + if(intrinsics.model != RealSense2API.DistortionModel.BROWN_CONRADY) + { + string msg = string.Format("RealSense2: intrinsics distrotion model {0} does not match Metrilus.Util", intrinsics.model.ToString()); + log.Error(msg); + throw new Exception(msg); + } + + return new ProjectiveTransformationZhang( + intrinsics.width, + intrinsics.height, + intrinsics.fx, + intrinsics.fy, + intrinsics.ppx, + intrinsics.ppy, + intrinsics.coeffs[0], + intrinsics.coeffs[1], + intrinsics.coeffs[2], + intrinsics.coeffs[3], + intrinsics.coeffs[4]); + } + + private RealSense2API.RS2StreamProfile GetProfileFromSensor(string channelName) { string sensorName; Point2i refResolution; @@ -1860,7 +1892,7 @@ unsafe public override IProjectiveTransformation GetIntrinsics(string channelNam refResolution = DepthResolution; break; } - + RealSense2API.RS2StreamProfile profile = new RealSense2API.RS2StreamProfile(IntPtr.Zero); RealSense2API.RS2Sensor sensor = RealSense2API.GetSensor(_pipeline, sensorName); RealSense2API.RS2StreamProfileList list = RealSense2API.GetStreamProfileList(sensor); @@ -1877,33 +1909,7 @@ unsafe public override IProjectiveTransformation GetIntrinsics(string channelNam } } - if(!profile.IsValid()) - { - // try to get profile from captured frame - profile = GetProfileFromCapturedFrames(channelName); - } - - RealSense2API.Intrinsics intrinsics = RealSense2API.GetIntrinsics(profile); - - if(intrinsics.model != RealSense2API.DistortionModel.BROWN_CONRADY) - { - string msg = string.Format("RealSense2: intrinsics distrotion model {0} does not match Metrilus.Util", intrinsics.model.ToString()); - log.Error(msg); - throw new Exception(msg); - } - - return new ProjectiveTransformationZhang( - intrinsics.width, - intrinsics.height, - intrinsics.fx, - intrinsics.fy, - intrinsics.ppx, - intrinsics.ppy, - intrinsics.coeffs[0], - intrinsics.coeffs[1], - intrinsics.coeffs[2], - intrinsics.coeffs[3], - intrinsics.coeffs[4]); + return profile; } private RealSense2API.RS2StreamProfile GetProfileFromCapturedFrames(string channelName) @@ -1946,8 +1952,13 @@ private RealSense2API.RS2StreamProfile GetProfileFromCapturedFrames(string chann unsafe public override RigidBodyTransformation GetExtrinsics(string channelFromName, string channelToName) { - RealSense2API.RS2StreamProfile from = GetProfileFromCapturedFrames(channelFromName); - RealSense2API.RS2StreamProfile to = GetProfileFromCapturedFrames(channelToName); + RealSense2API.RS2StreamProfile from = GetProfileFromSensor(channelFromName); + RealSense2API.RS2StreamProfile to = GetProfileFromSensor(channelToName); + if (!from.IsValid()) + from = GetProfileFromCapturedFrames(channelFromName); + if (!to.IsValid()) + to = GetProfileFromCapturedFrames(channelToName); + RealSense2API.Extrinsics extrinsics = RealSense2API.GetExtrinsics(from, to); From 300738334bac78ccbba3586e3edf625035151bb1 Mon Sep 17 00:00:00 2001 From: Jan Lukas Gernert Date: Tue, 16 Jan 2018 18:24:11 +0100 Subject: [PATCH 29/32] address almost all remarks of the review --- BetaCameras/RealSense2/RealSense2.cs | 633 +++++++------------- BetaCameras/RealSense2/RealSense2.csproj | 9 +- BetaCameras/RealSense2/RealSense2API.cs | 28 +- MetriCam2.Controls/CameraSettingsControl.cs | 45 +- 4 files changed, 269 insertions(+), 446 deletions(-) diff --git a/BetaCameras/RealSense2/RealSense2.cs b/BetaCameras/RealSense2/RealSense2.cs index 994879cd..c23a994d 100644 --- a/BetaCameras/RealSense2/RealSense2.cs +++ b/BetaCameras/RealSense2/RealSense2.cs @@ -5,6 +5,7 @@ using System; using System.Drawing; using System.Collections.Generic; +using System.Threading; #if NETSTANDARD2_0 #else using System.Drawing.Imaging; @@ -47,16 +48,16 @@ public Point2i ColorResolution get { return _colorResolution; } set { - if(value != _colorResolution) - { - _updatingPipeline = true; - StopPipeline(); - DeactivateChannelImpl(ChannelNames.Color); - _colorResolution = value; - ActivateChannelImpl(ChannelNames.Color); - StartPipeline(); - _updatingPipeline = false; - } + if (value == _colorResolution) + return; + + _updatingPipeline = true; + StopPipeline(); + DeactivateChannelImpl(ChannelNames.Color); + _colorResolution = value; + ActivateChannelImpl(ChannelNames.Color); + StartPipeline(); + _updatingPipeline = false; } } @@ -79,7 +80,7 @@ ListParamDesc ColorResolutionDesc } ListParamDesc res = new ListParamDesc(allowedValues); - res.Unit = "Pixel"; + res.Unit = "px"; res.Description = "Resolution of the color sensor."; res.ReadableWhen = ParamDesc.ConnectionStates.Connected | ParamDesc.ConnectionStates.Disconnected; res.WritableWhen = ParamDesc.ConnectionStates.Connected | ParamDesc.ConnectionStates.Disconnected; @@ -93,16 +94,16 @@ public int ColorFPS get { return _colorFPS; } set { - if (value != _colorFPS) - { - _updatingPipeline = true; - StopPipeline(); - DeactivateChannelImpl(ChannelNames.Color); - _colorFPS = value; - ActivateChannelImpl(ChannelNames.Color); - StartPipeline(); - _updatingPipeline = false; - } + if (value == _colorFPS) + return; + + _updatingPipeline = true; + StopPipeline(); + DeactivateChannelImpl(ChannelNames.Color); + _colorFPS = value; + ActivateChannelImpl(ChannelNames.Color); + StartPipeline(); + _updatingPipeline = false; } } @@ -120,8 +121,8 @@ ListParamDesc ColorFPSDesc } ListParamDesc res = new ListParamDesc(framerates); - res.Unit = "Frames per Second"; - res.Description = "FPS of the color sensor."; + res.Unit = "fps"; + res.Description = "Frames per Second of the color sensor."; res.ReadableWhen = ParamDesc.ConnectionStates.Connected | ParamDesc.ConnectionStates.Disconnected; res.WritableWhen = ParamDesc.ConnectionStates.Connected | ParamDesc.ConnectionStates.Disconnected; return res; @@ -134,34 +135,34 @@ public Point2i DepthResolution get { return _depthResolution; } set { - if (value != _depthResolution) - { - _updatingPipeline = true; - StopPipeline(); + if (value == _depthResolution) + return; - if(IsChannelActive(ChannelNames.ZImage)) - DeactivateChannelImpl(ChannelNames.ZImage); + _updatingPipeline = true; + StopPipeline(); - if (IsChannelActive(ChannelNames.Left)) - DeactivateChannelImpl(ChannelNames.Left); + if (IsChannelActive(ChannelNames.ZImage)) + DeactivateChannelImpl(ChannelNames.ZImage); - if (IsChannelActive(ChannelNames.Right)) - DeactivateChannelImpl(ChannelNames.Right); + if (IsChannelActive(ChannelNames.Left)) + DeactivateChannelImpl(ChannelNames.Left); - _depthResolution = value; + if (IsChannelActive(ChannelNames.Right)) + DeactivateChannelImpl(ChannelNames.Right); - if (IsChannelActive(ChannelNames.ZImage)) - ActivateChannelImpl(ChannelNames.ZImage); + _depthResolution = value; - if (IsChannelActive(ChannelNames.Left)) - ActivateChannelImpl(ChannelNames.Left); + if (IsChannelActive(ChannelNames.ZImage)) + ActivateChannelImpl(ChannelNames.ZImage); - if (IsChannelActive(ChannelNames.Right)) - ActivateChannelImpl(ChannelNames.Right); - - StartPipeline(); - _updatingPipeline = false; - } + if (IsChannelActive(ChannelNames.Left)) + ActivateChannelImpl(ChannelNames.Left); + + if (IsChannelActive(ChannelNames.Right)) + ActivateChannelImpl(ChannelNames.Right); + + StartPipeline(); + _updatingPipeline = false; } } @@ -184,7 +185,7 @@ ListParamDesc DepthResolutionDesc } ListParamDesc res = new ListParamDesc(allowedValues); - res.Unit = "Pixel"; + res.Unit = "px"; res.Description = "Resolution of the depth sensor."; res.ReadableWhen = ParamDesc.ConnectionStates.Connected | ParamDesc.ConnectionStates.Disconnected; res.WritableWhen = ParamDesc.ConnectionStates.Connected | ParamDesc.ConnectionStates.Disconnected; @@ -198,34 +199,34 @@ public int DepthFPS get { return _depthFPS; } set { - if (value != _depthFPS) - { - _updatingPipeline = true; - StopPipeline(); + if (value == _depthFPS) + return; + + _updatingPipeline = true; + StopPipeline(); - if (IsChannelActive(ChannelNames.ZImage)) - DeactivateChannelImpl(ChannelNames.ZImage); + if (IsChannelActive(ChannelNames.ZImage)) + DeactivateChannelImpl(ChannelNames.ZImage); - if (IsChannelActive(ChannelNames.Left)) - DeactivateChannelImpl(ChannelNames.Left); + if (IsChannelActive(ChannelNames.Left)) + DeactivateChannelImpl(ChannelNames.Left); - if (IsChannelActive(ChannelNames.Right)) - DeactivateChannelImpl(ChannelNames.Right); + if (IsChannelActive(ChannelNames.Right)) + DeactivateChannelImpl(ChannelNames.Right); - _depthFPS = value; + _depthFPS = value; - if (IsChannelActive(ChannelNames.ZImage)) - ActivateChannelImpl(ChannelNames.ZImage); + if (IsChannelActive(ChannelNames.ZImage)) + ActivateChannelImpl(ChannelNames.ZImage); - if (IsChannelActive(ChannelNames.Left)) - ActivateChannelImpl(ChannelNames.Left); + if (IsChannelActive(ChannelNames.Left)) + ActivateChannelImpl(ChannelNames.Left); - if (IsChannelActive(ChannelNames.Right)) - ActivateChannelImpl(ChannelNames.Right); + if (IsChannelActive(ChannelNames.Right)) + ActivateChannelImpl(ChannelNames.Right); - StartPipeline(); - _updatingPipeline = false; - } + StartPipeline(); + _updatingPipeline = false; } } @@ -243,8 +244,8 @@ ListParamDesc DepthFPSDesc } ListParamDesc res = new ListParamDesc(framerates); - res.Unit = "Frames per Second"; - res.Description = "FPS of the depth sensor."; + res.Unit = "fps"; + res.Description = "Frames per Second of the depth sensor."; res.ReadableWhen = ParamDesc.ConnectionStates.Connected | ParamDesc.ConnectionStates.Disconnected; res.WritableWhen = ParamDesc.ConnectionStates.Connected | ParamDesc.ConnectionStates.Disconnected; return res; @@ -300,12 +301,7 @@ public bool BacklightCompensation get { CheckOptionSupported(RealSense2API.Option.BACKLIGHT_COMPENSATION, BacklightCompensationDesc.Name, RealSense2API.SensorName.COLOR); - float res = RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.BACKLIGHT_COMPENSATION); - - if (res == 1.0f) - return true; - - return false; + return RealSense2API.GetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.BACKLIGHT_COMPENSATION) == 1.0f ? true : false; } set @@ -320,7 +316,6 @@ ParamDesc BacklightCompensationDesc get { ParamDesc res = new ParamDesc(); - res.Unit = "Boolean"; res.Description = "Enable / disable color backlight compensation"; res.ReadableWhen = ParamDesc.ConnectionStates.Connected; res.WritableWhen = ParamDesc.ConnectionStates.Connected; @@ -356,17 +351,8 @@ RangeParamDesc BrightnessDesc if (this.IsConnected) { - RealSense2API.QueryOptionInfo( - _pipeline, - RealSense2API.SensorName.COLOR, - RealSense2API.Option.BRIGHTNESS, - out float min, - out float max, - out float step, - out float def, - out string desc); - - res = new RangeParamDesc((int)min, (int)max); + var option = QueryOption(RealSense2API.Option.BRIGHTNESS, RealSense2API.SensorName.COLOR); + res = new RangeParamDesc((int)option.min, (int)option.max); } else { @@ -408,17 +394,8 @@ RangeParamDesc ContrastDesc if(this.IsConnected) { - RealSense2API.QueryOptionInfo( - _pipeline, - RealSense2API.SensorName.COLOR, - RealSense2API.Option.CONTRAST, - out float min, - out float max, - out float step, - out float def, - out string desc); - - res = new RangeParamDesc((int)min, (int)max); + var option = QueryOption(RealSense2API.Option.CONTRAST, RealSense2API.SensorName.COLOR); + res = new RangeParamDesc((int)option.min, (int)option.max); } else { @@ -460,17 +437,8 @@ RangeParamDesc ExposureColorDesc if(this.IsConnected) { - RealSense2API.QueryOptionInfo( - _pipeline, - RealSense2API.SensorName.COLOR, - RealSense2API.Option.EXPOSURE, - out float min, - out float max, - out float step, - out float def, - out string desc); - - res = new RangeParamDesc((int)min, (int)max); + var option = QueryOption(RealSense2API.Option.EXPOSURE, RealSense2API.SensorName.COLOR); + res = new RangeParamDesc((int)option.min, (int)option.max); } else { @@ -558,32 +526,10 @@ public int ExposureDepth set { CheckOptionSupported(RealSense2API.Option.EXPOSURE, ExposureDepthDesc.Name, RealSense2API.SensorName.STEREO); - - RealSense2API.QueryOptionInfo( - _pipeline, - RealSense2API.SensorName.STEREO, - RealSense2API.Option.EXPOSURE, - out float min, - out float max, - out float step, - out float def, - out string desc); - + var option = QueryOption(RealSense2API.Option.EXPOSURE, RealSense2API.SensorName.STEREO); // step size for depth exposure is 20 - float adjusted_value = (float)value; - int rounding = (value - (int)min) % (int)step; - adjusted_value -= (float)rounding; - - if (rounding > step / 2) - adjusted_value += step; - - if (adjusted_value > max) - adjusted_value -= step; - if (adjusted_value < min) - adjusted_value += step; - - + float adjusted_value = AdjustValue(option.min, option.max, value, option.step); CheckRangeValid(ExposureDepthDesc, value, (int)adjusted_value, true); RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.EXPOSURE, adjusted_value); } @@ -597,17 +543,8 @@ RangeParamDesc ExposureDepthDesc if(this.IsConnected) { - RealSense2API.QueryOptionInfo( - _pipeline, - RealSense2API.SensorName.STEREO, - RealSense2API.Option.EXPOSURE, - out float min, - out float max, - out float step, - out float def, - out string desc); - - res = new RangeParamDesc((int)min, (int)max); + var option = QueryOption(RealSense2API.Option.EXPOSURE, RealSense2API.SensorName.STEREO); + res = new RangeParamDesc((int)option.min, (int)option.max); } else { @@ -678,17 +615,8 @@ RangeParamDesc GainColorDesc if(this.IsConnected) { - RealSense2API.QueryOptionInfo( - _pipeline, - RealSense2API.SensorName.COLOR, - RealSense2API.Option.GAIN, - out float min, - out float max, - out float step, - out float def, - out string desc); - - res = new RangeParamDesc((int)min, (int)max); + var option = QueryOption(RealSense2API.Option.GAIN, RealSense2API.SensorName.COLOR); + res = new RangeParamDesc((int)option.min, (int)option.max); } else { @@ -729,17 +657,8 @@ RangeParamDesc GainDepthDesc if(this.IsConnected) { - RealSense2API.QueryOptionInfo( - _pipeline, - RealSense2API.SensorName.STEREO, - RealSense2API.Option.GAIN, - out float min, - out float max, - out float step, - out float def, - out string desc); - - res = new RangeParamDesc((int)min, (int)max); + var option = QueryOption(RealSense2API.Option.GAIN, RealSense2API.SensorName.STEREO); + res = new RangeParamDesc((int)option.min, (int)option.max); } else { @@ -780,17 +699,8 @@ RangeParamDesc GammaDesc if(this.IsConnected) { - RealSense2API.QueryOptionInfo( - _pipeline, - RealSense2API.SensorName.COLOR, - RealSense2API.Option.GAMMA, - out float min, - out float max, - out float step, - out float def, - out string desc); - - res = new RangeParamDesc((int)min, (int)max); + var option = QueryOption(RealSense2API.Option.GAMMA, RealSense2API.SensorName.COLOR); + res = new RangeParamDesc((int)option.min, (int)option.max); } else { @@ -831,17 +741,8 @@ RangeParamDesc HueDesc if(this.IsConnected) { - RealSense2API.QueryOptionInfo( - _pipeline, - RealSense2API.SensorName.COLOR, - RealSense2API.Option.HUE, - out float min, - out float max, - out float step, - out float def, - out string desc); - - res = new RangeParamDesc((int)min, (int)max); + var option = QueryOption(RealSense2API.Option.HUE, RealSense2API.SensorName.COLOR); + res = new RangeParamDesc((int)option.min, (int)option.max); } else { @@ -882,17 +783,8 @@ RangeParamDesc SaturationDesc if(this.IsConnected) { - RealSense2API.QueryOptionInfo( - _pipeline, - RealSense2API.SensorName.COLOR, - RealSense2API.Option.SATURATION, - out float min, - out float max, - out float step, - out float def, - out string desc); - - res = new RangeParamDesc((int)min, (int)max); + var option = QueryOption(RealSense2API.Option.SATURATION, RealSense2API.SensorName.COLOR); + res = new RangeParamDesc((int)option.min, (int)option.max); } else { @@ -933,17 +825,8 @@ RangeParamDesc SharpnessDesc if(this.IsConnected) { - RealSense2API.QueryOptionInfo( - _pipeline, - RealSense2API.SensorName.COLOR, - RealSense2API.Option.SHARPNESS, - out float min, - out float max, - out float step, - out float def, - out string desc); - - res = new RangeParamDesc((int)min, (int)max); + var option = QueryOption(RealSense2API.Option.SHARPNESS, RealSense2API.SensorName.COLOR); + res = new RangeParamDesc((int)option.min, (int)option.max); } else { @@ -971,31 +854,11 @@ public int WhiteBalance set { CheckOptionSupported(RealSense2API.Option.WHITE_BALANCE, WhiteBalanceDesc.Name, RealSense2API.SensorName.COLOR); - - RealSense2API.QueryOptionInfo( - _pipeline, - RealSense2API.SensorName.COLOR, - RealSense2API.Option.WHITE_BALANCE, - out float min, - out float max, - out float step, - out float def, - out string desc); + var option = QueryOption(RealSense2API.Option.WHITE_BALANCE, RealSense2API.SensorName.COLOR); // step size for depth white balance is 10 - float adjusted_value = (float)value; - int rounding = (value - (int)min) % (int)step; - adjusted_value -= (float)rounding; - - if (rounding > step / 2) - adjusted_value += step; - - if (adjusted_value > max) - adjusted_value -= step; - if (adjusted_value < min) - adjusted_value += step; - + float adjusted_value = AdjustValue(option.min, option.max, value, option.step); CheckRangeValid(WhiteBalanceDesc, value, (int)adjusted_value, true); RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.COLOR, RealSense2API.Option.WHITE_BALANCE, adjusted_value); @@ -1010,17 +873,8 @@ RangeParamDesc WhiteBalanceDesc if(this.IsConnected) { - RealSense2API.QueryOptionInfo( - _pipeline, - RealSense2API.SensorName.COLOR, - RealSense2API.Option.WHITE_BALANCE, - out float min, - out float max, - out float step, - out float def, - out string desc); - - res = new RangeParamDesc((int)min, (int)max); + var option = QueryOption(RealSense2API.Option.WHITE_BALANCE, RealSense2API.SensorName.COLOR); + res = new RangeParamDesc((int)option.min, (int)option.max); } else { @@ -1078,31 +932,10 @@ public int LaserPower set { CheckOptionSupported(RealSense2API.Option.LASER_POWER, LaserPowerDesc.Name, RealSense2API.SensorName.STEREO); - - RealSense2API.QueryOptionInfo( - _pipeline, - RealSense2API.SensorName.STEREO, - RealSense2API.Option.LASER_POWER, - out float min, - out float max, - out float step, - out float def, - out string desc); - + var option = QueryOption(RealSense2API.Option.LASER_POWER, RealSense2API.SensorName.STEREO); // step size for depth laser power is 30 - float adjusted_value = (float)value; - int rounding = (value - (int)min) % (int)step; - adjusted_value -= (float)rounding; - - if (rounding > step / 2) - adjusted_value += step; - - if (adjusted_value > max) - adjusted_value -= step; - if (adjusted_value < min) - adjusted_value += step; - + float adjusted_value = AdjustValue(option.min, option.max, value, option.step); CheckRangeValid(LaserPowerDesc, value, (int)adjusted_value, true); RealSense2API.SetOption(_pipeline, RealSense2API.SensorName.STEREO, RealSense2API.Option.LASER_POWER, adjusted_value); } @@ -1116,17 +949,8 @@ RangeParamDesc LaserPowerDesc if(this.IsConnected) { - RealSense2API.QueryOptionInfo( - _pipeline, - RealSense2API.SensorName.STEREO, - RealSense2API.Option.LASER_POWER, - out float min, - out float max, - out float step, - out float def, - out string desc); - - res = new RangeParamDesc((int)min, (int)max); + var option = QueryOption(RealSense2API.Option.LASER_POWER, RealSense2API.SensorName.STEREO); + res = new RangeParamDesc((int)option.min, (int)option.max); } else { @@ -1200,17 +1024,8 @@ RangeParamDesc FrameQueueSizeColorDesc if(this.IsConnected) { - RealSense2API.QueryOptionInfo( - _pipeline, - RealSense2API.SensorName.COLOR, - RealSense2API.Option.FRAMES_QUEUE_SIZE, - out float min, - out float max, - out float step, - out float def, - out string desc); - - res = new RangeParamDesc((int)min, (int)max); + var option = QueryOption(RealSense2API.Option.FRAMES_QUEUE_SIZE, RealSense2API.SensorName.COLOR); + res = new RangeParamDesc((int)option.min, (int)option.max); } else { @@ -1251,17 +1066,8 @@ RangeParamDesc FrameQueueSizeDepthDesc if (this.IsConnected) { - RealSense2API.QueryOptionInfo( - _pipeline, - RealSense2API.SensorName.STEREO, - RealSense2API.Option.FRAMES_QUEUE_SIZE, - out float min, - out float max, - out float step, - out float def, - out string desc); - - res = new RangeParamDesc((int)min, (int)max); + var option = QueryOption(RealSense2API.Option.FRAMES_QUEUE_SIZE, RealSense2API.SensorName.STEREO); + res = new RangeParamDesc((int)option.min, (int)option.max); } else { @@ -1325,8 +1131,8 @@ ParamDesc ASICTempDesc get { ParamDesc res = new ParamDesc(); - res.Unit = "Degree Celsius °C"; - res.Description = "Current Asic Temperature"; + res.Unit = "°C"; + res.Description = "Asic Temperature"; res.ReadableWhen = ParamDesc.ConnectionStates.Connected; res.WritableWhen = ParamDesc.ConnectionStates.Connected; return res; @@ -1382,8 +1188,8 @@ ParamDesc ProjectorTempDesc get { ParamDesc res = new ParamDesc(); - res.Unit = "Degree Celsius °C"; - res.Description = "Current Projector Temperature"; + res.Unit = "°C"; + res.Description = "Projector Temperature"; res.ReadableWhen = ParamDesc.ConnectionStates.Connected; res.WritableWhen = ParamDesc.ConnectionStates.Connected; return res; @@ -1449,17 +1255,8 @@ RangeParamDesc DepthUnitsDesc if(this.IsConnected) { - RealSense2API.QueryOptionInfo( - _pipeline, - RealSense2API.SensorName.STEREO, - RealSense2API.Option.DEPTH_UNITS, - out float min, - out float max, - out float step, - out float def, - out string desc); - - res = new RangeParamDesc(min, max); + var option = QueryOption(RealSense2API.Option.DEPTH_UNITS, RealSense2API.SensorName.STEREO); + res = new RangeParamDesc(option.min, option.max); } else { @@ -1589,113 +1386,107 @@ protected override void UpdateImpl() while(_updatingPipeline) { // wait for pipeline to restart with new settings + Thread.Sleep(50); } - try + if (!RealSense2API.PipelineRunning) { - if (!RealSense2API.PipelineRunning) - { - string msg = "RealSense2: Can't update camera since pipeline is not running"; - log.Error(msg); - throw new Exception(msg); - } + string msg = "RealSense2: Can't update camera since pipeline is not running"; + log.Error(msg); + throw new Exception(msg); + } - RealSense2API.ReleaseFrame(_currentColorFrame); - RealSense2API.ReleaseFrame(_currentDepthFrame); - RealSense2API.ReleaseFrame(_currentLeftFrame); - RealSense2API.ReleaseFrame(_currentRightFrame); - - bool getColor = IsChannelActive(ChannelNames.Color); - bool getDepth = IsChannelActive(ChannelNames.ZImage); - bool getLeft = IsChannelActive(ChannelNames.Left); - bool getRight = IsChannelActive(ChannelNames.Right); - bool haveColor = false; - bool haveDepth = false; - bool haveLeft = false; - bool haveRight = false; - - while (true) - { - RealSense2API.RS2Frame data = RealSense2API.PipelineWaitForFrames(_pipeline, 5000); + RealSense2API.ReleaseFrame(_currentColorFrame); + RealSense2API.ReleaseFrame(_currentDepthFrame); + RealSense2API.ReleaseFrame(_currentLeftFrame); + RealSense2API.ReleaseFrame(_currentRightFrame); - if (!data.IsValid() || data.Handle == IntPtr.Zero) - { - RealSense2API.ReleaseFrame(data); - continue; - } + bool getColor = IsChannelActive(ChannelNames.Color); + bool getDepth = IsChannelActive(ChannelNames.ZImage); + bool getLeft = IsChannelActive(ChannelNames.Left); + bool getRight = IsChannelActive(ChannelNames.Right); + bool haveColor = false; + bool haveDepth = false; + bool haveLeft = false; + bool haveRight = false; - int frameCount = RealSense2API.FrameEmbeddedCount(data); - log.Debug(string.Format("RealSense2: Got {0} Frames", frameCount)); + while (true) + { + RealSense2API.RS2Frame data = RealSense2API.PipelineWaitForFrames(_pipeline, 5000); + if (!data.IsValid() || data.Handle == IntPtr.Zero) + { + RealSense2API.ReleaseFrame(data); + continue; + } - // extract all frames - for (int j = 0; j < frameCount; j++) - { - RealSense2API.RS2Frame frame = RealSense2API.FrameExtract(data, j); - RealSense2API.FrameAddRef(frame); + int frameCount = RealSense2API.FrameEmbeddedCount(data); + log.Debug(string.Format("RealSense2: Got {0} Frames", frameCount)); - // what kind of frame did we get? - RealSense2API.RS2StreamProfile profile = RealSense2API.GetStreamProfile(frame); - RealSense2API.GetStreamProfileData(profile, out RealSense2API.Stream stream, out RealSense2API.Format format, out int index, out int uid, out int framerate); - log.Debug(string.Format("RealSense2: Analyzing frame {0}", j + 1)); - log.Debug(string.Format("RealSense2: stream {0}", stream.ToString())); - log.Debug(string.Format("RealSense2: format {0}", format.ToString())); + // extract all frames + for (int j = 0; j < frameCount; j++) + { + RealSense2API.RS2Frame frame = RealSense2API.FrameExtract(data, j); + RealSense2API.FrameAddRef(frame); + // what kind of frame did we get? + RealSense2API.RS2StreamProfile profile = RealSense2API.GetStreamProfile(frame); + RealSense2API.GetStreamProfileData(profile, out RealSense2API.Stream stream, out RealSense2API.Format format, out int index, out int uid, out int framerate); - switch (stream) - { - case RealSense2API.Stream.COLOR: - if (getColor) - { - RealSense2API.ReleaseFrame(_currentColorFrame); - _currentColorFrame = frame; - haveColor = true; - } - break; - case RealSense2API.Stream.DEPTH: - if (getDepth) - { - RealSense2API.ReleaseFrame(_currentDepthFrame); - _currentDepthFrame = frame; - haveDepth = true; - } - break; - case RealSense2API.Stream.INFRARED: - if (index == 1) + log.Debug(string.Format("RealSense2: Analyzing frame {0}", j + 1)); + log.Debug(string.Format("RealSense2: stream {0}", stream.ToString())); + log.Debug(string.Format("RealSense2: format {0}", format.ToString())); + + + switch (stream) + { + case RealSense2API.Stream.COLOR: + if (getColor) + { + RealSense2API.ReleaseFrame(_currentColorFrame); + _currentColorFrame = frame; + haveColor = true; + } + break; + case RealSense2API.Stream.DEPTH: + if (getDepth) + { + RealSense2API.ReleaseFrame(_currentDepthFrame); + _currentDepthFrame = frame; + haveDepth = true; + } + break; + case RealSense2API.Stream.INFRARED: + if (index == 1) + { + if (getLeft) { - if (getLeft) - { - RealSense2API.ReleaseFrame(_currentLeftFrame); - _currentLeftFrame = frame; - haveLeft = true; - } + RealSense2API.ReleaseFrame(_currentLeftFrame); + _currentLeftFrame = frame; + haveLeft = true; } - else if (index == 2) + } + else if (index == 2) + { + if (getRight) { - if (getRight) - { - RealSense2API.ReleaseFrame(_currentRightFrame); - _currentRightFrame = frame; - haveRight = true; - } + RealSense2API.ReleaseFrame(_currentRightFrame); + _currentRightFrame = frame; + haveRight = true; } - break; - } + } + break; } + } - RealSense2API.ReleaseFrame(data); + RealSense2API.ReleaseFrame(data); - if (((getColor && haveColor) || !getColor) - && ((getDepth && haveDepth) || !getDepth) - && ((getLeft && haveLeft) || !getLeft) - && ((getRight && haveRight) || !getRight)) - break; - } - } - catch (Exception) - { - throw; + if (((getColor && haveColor) || !getColor) + && ((getDepth && haveDepth) || !getDepth) + && ((getLeft && haveLeft) || !getLeft) + && ((getRight && haveRight) || !getRight)) + break; } } @@ -1893,9 +1684,8 @@ private RealSense2API.RS2StreamProfile GetProfileFromSensor(string channelName) break; } - RealSense2API.RS2StreamProfile profile = new RealSense2API.RS2StreamProfile(IntPtr.Zero); RealSense2API.RS2Sensor sensor = RealSense2API.GetSensor(_pipeline, sensorName); - RealSense2API.RS2StreamProfileList list = RealSense2API.GetStreamProfileList(sensor); + RealSense2API.RS2StreamProfilesList list = RealSense2API.GetStreamProfileList(sensor); int count = RealSense2API.GetStreamProfileListCount(list); for (int i = 0; i < count; i++) @@ -1904,12 +1694,11 @@ private RealSense2API.RS2StreamProfile GetProfileFromSensor(string channelName) Point2i resolution = RealSense2API.GetStreamProfileResolution(p); if (resolution == refResolution) { - profile = p; - break; + return p; } } - return profile; + return new RealSense2API.RS2StreamProfile(IntPtr.Zero); } private RealSense2API.RS2StreamProfile GetProfileFromCapturedFrames(string channelName) @@ -2089,7 +1878,39 @@ private void CheckRangeValid(RangeParamDesc desc, T value, T adjustedValue if (adjusted) throw new Exception(string.Format("Value {0} for '{1}' is outside of the range between {2} and {3}", value, desc.Name, desc.Min, desc.Max)); else - throw new Exception(string.Format("Value {0} (adjusted to {1} to match stepsize) for 'LaserPower' is outside of the range between {2} and {3}", value, adjustedValue, desc.Min, desc.Max)); + throw new Exception(string.Format("Value {0} (adjusted to {1} to match stepsize) for '{2}' is outside of the range between {3} and {4}", value, adjustedValue, desc.Name, desc.Min, desc.Max)); + } + + private (float min, float max, float step, float def) QueryOption(RealSense2API.Option option, string sensorName) + { + RealSense2API.QueryOptionInfo( + _pipeline, + sensorName, + option, + out float min, + out float max, + out float step, + out float def, + out string desc); + + return (min, max, step, def); + } + + private float AdjustValue(float min, float max, float value, float step) + { + float adjusted_value = value; + float rounding = (value - min) % step; + adjusted_value -= rounding; + + if (rounding > step / 2) + adjusted_value += step; + + if (adjusted_value > max) + adjusted_value -= step; + if (adjusted_value < min) + adjusted_value += step; + + return adjusted_value; } } } diff --git a/BetaCameras/RealSense2/RealSense2.csproj b/BetaCameras/RealSense2/RealSense2.csproj index 7480a8aa..3b4a5246 100644 --- a/BetaCameras/RealSense2/RealSense2.csproj +++ b/BetaCameras/RealSense2/RealSense2.csproj @@ -1,5 +1,5 @@  - + @@ -10,8 +10,9 @@ Properties MetriCam2.Cameras MetriCam2.Cameras.RealSense2 - v4.0 + v4.7 512 + true @@ -23,6 +24,7 @@ prompt MinimumRecommendedRules.ruleset true + false ..\..\bin\x86\Release\ @@ -34,6 +36,7 @@ prompt MinimumRecommendedRules.ruleset true + false true @@ -45,6 +48,7 @@ prompt MinimumRecommendedRules.ruleset true + false ..\..\bin\x64\Release\ @@ -56,6 +60,7 @@ prompt MinimumRecommendedRules.ruleset true + false diff --git a/BetaCameras/RealSense2/RealSense2API.cs b/BetaCameras/RealSense2/RealSense2API.cs index fafbc87d..ce4fac62 100644 --- a/BetaCameras/RealSense2/RealSense2API.cs +++ b/BetaCameras/RealSense2/RealSense2API.cs @@ -731,11 +731,11 @@ public RS2Frame(IntPtr p) public bool IsValid() => (null != Handle); } - public struct RS2StreamProfileList + public struct RS2StreamProfilesList { public IntPtr Handle { get; private set; } - public RS2StreamProfileList(IntPtr p) + public RS2StreamProfilesList(IntPtr p) { Handle = p; } @@ -819,7 +819,7 @@ unsafe public static bool CheckConfig(RS2Pipeline pipe, RS2Config conf) int res = rs2_config_can_resolve(conf.Handle, pipe.Handle, &error); HandleError(error); - return res == 1; + return (1 == res); } catch (Exception) { @@ -969,7 +969,7 @@ unsafe public static RS2StreamProfile GetStreamProfile(RS2Frame frame) } } - unsafe public static RS2StreamProfile GetStreamProfile(RS2StreamProfileList list, int index) + unsafe public static RS2StreamProfile GetStreamProfile(RS2StreamProfilesList list, int index) { try { @@ -1285,10 +1285,7 @@ unsafe public static bool IsOptionSupported(RS2Pipeline pipe, string SensorName, sensor.Delete(); - if (res == 1) - return true; - - return false; + return (1 == res); } catch (Exception) { @@ -1309,10 +1306,7 @@ unsafe public static bool IsOptionRealOnly(RS2Pipeline pipe, string SensorName, sensor.Delete(); - if (res == 1) - return true; - - return false; + return (1 == res); } catch (Exception) { @@ -1404,7 +1398,7 @@ unsafe public static string OptionValueInfo(RS2Pipeline pipe, string SensorName, } } - unsafe public static RS2StreamProfileList GetStreamProfileList(RS2Sensor sensor) + unsafe public static RS2StreamProfilesList GetStreamProfileList(RS2Sensor sensor) { try { @@ -1412,7 +1406,7 @@ unsafe public static RS2StreamProfileList GetStreamProfileList(RS2Sensor sensor) IntPtr list = rs2_get_stream_profiles(sensor.Handle, &error); HandleError(error); - return new RS2StreamProfileList(list); + return new RS2StreamProfilesList(list); } catch (Exception) { @@ -1420,7 +1414,7 @@ unsafe public static RS2StreamProfileList GetStreamProfileList(RS2Sensor sensor) } } - unsafe public static int GetStreamProfileListCount(RS2StreamProfileList list) + unsafe public static int GetStreamProfileListCount(RS2StreamProfilesList list) { try { @@ -1440,7 +1434,7 @@ unsafe public static List GetSupportedResolutions(RS2Pipeline pipe, str { List res = new List(); RS2Sensor sensor = GetSensor(pipe, sensorName); - RS2StreamProfileList list = GetStreamProfileList(sensor); + RS2StreamProfilesList list = GetStreamProfileList(sensor); int count = GetStreamProfileListCount(list); for(int i = 0; i < count; i++) @@ -1461,7 +1455,7 @@ unsafe public static List GetSupportedFrameRates(RS2Pipeline pipe, string s { List res = new List(); RS2Sensor sensor = GetSensor(pipe, sensorName); - RS2StreamProfileList list = GetStreamProfileList(sensor); + RS2StreamProfilesList list = GetStreamProfileList(sensor); int count = GetStreamProfileListCount(list); for (int i = 0; i < count; i++) diff --git a/MetriCam2.Controls/CameraSettingsControl.cs b/MetriCam2.Controls/CameraSettingsControl.cs index 29e6f038..6ec3af59 100644 --- a/MetriCam2.Controls/CameraSettingsControl.cs +++ b/MetriCam2.Controls/CameraSettingsControl.cs @@ -92,13 +92,19 @@ public Camera Camera get { return cam; } set { - cam = value; - if (null == value) { return; } + if (null != cam) + { + cam.OnConnected -= InitConfigurationParameters; + cam.OnDisconnected -= InitConfigurationParameters; + } + + cam = value; + // Update icon // use cam.CameraIcon // Update list of channels @@ -106,8 +112,8 @@ public Camera Camera // Update child controls. InitConfigurationParameters(cam); - cam.OnConnected += (camera) => { InitConfigurationParameters(camera); }; - cam.OnDisconnected += (camera) => { InitConfigurationParameters(camera); }; + cam.OnConnected += InitConfigurationParameters; + cam.OnDisconnected += InitConfigurationParameters; } } @@ -242,10 +248,8 @@ private void InitConfigurationParameters(Camera cam) scrollbarValue.ValueChanged += (sender, e) => { string parameterValue = scrollbarValue.Value.ToString(CultureInfo.InvariantCulture); - string parameterName = scrollbarValue.Name.Replace(VALUE_SUFFIX, string.Empty); - Dictionary keyValues = new Dictionary(); - keyValues.Add(parameterName, parameterValue); - Camera.SetParameters(keyValues); + string parameterName = paramDesc.Name; + Camera.SetParameter(paramDesc.Name, parameterValue); }; } else if (paramDesc is MetriCam2.Camera.RangeParamDesc) @@ -262,10 +266,8 @@ private void InitConfigurationParameters(Camera cam) upDownValue.ValueChanged += (sender, e) => { string parameterValue = upDownValue.Value.ToString(CultureInfo.InvariantCulture); - string parameterName = upDownValue.Name.Replace(VALUE_SUFFIX, string.Empty); - Dictionary keyValues = new Dictionary(); - keyValues.Add(parameterName, parameterValue); - Camera.SetParameters(keyValues); + string parameterName = paramDesc.Name; + Camera.SetParameter(paramDesc.Name, parameterValue); }; } else @@ -304,19 +306,15 @@ private void InitConfigurationParameters(Camera cam) if (paramDesc is MetriCam2.Camera.ListParamDesc) { - string selectedText = comboBoxValue.SelectedItem as string; - string[] stringValue = selectedText.Split('x'); - parameterValue = new Point2i(int.Parse(stringValue[0]), int.Parse(stringValue[1])); + parameterValue = StringToPoint2i(comboBoxValue.SelectedItem as string); } else { parameterValue = int.Parse(comboBoxValue.SelectedItem as string); } - string parameterName = comboBoxValue.Name.Replace(VALUE_SUFFIX, string.Empty); - Dictionary keyValues = new Dictionary(); - keyValues.Add(parameterName, parameterValue); - Camera.SetParameters(keyValues); + string parameterName = paramDesc.Name; + Camera.SetParameter(paramDesc.Name, parameterValue); }; } @@ -528,8 +526,7 @@ private ComboBox CreateComboBox(Camera.IListParamDesc listParamDesc, int current if (paramDesc is MetriCam2.Camera.ListParamDesc) { - string[] stringValue = item.Split('x'); - tmpVal = new Point2i(int.Parse(stringValue[0]), int.Parse(stringValue[1])); + tmpVal = StringToPoint2i(item); } else { @@ -647,5 +644,11 @@ private TextBox CreateTextBox(Camera.ParamDesc paramDesc, int currentRow) return textBoxValue; } #endregion + + private Point2i StringToPoint2i(string s) + { + string[] stringValue = s.Split('x'); + return new Point2i(int.Parse(stringValue[0]), int.Parse(stringValue[1])); + } } } \ No newline at end of file From 362b71e5f4ce5eb09aba0ea445ed5c7013de0026 Mon Sep 17 00:00:00 2001 From: Jan Lukas Gernert Date: Wed, 17 Jan 2018 12:28:39 +0100 Subject: [PATCH 30/32] Rename StringToPoin2i and throw if pipeline needs to be restarted while already busy --- BetaCameras/RealSense2/RealSense2.cs | 14 ++++++++++++++ MetriCam2.Controls/CameraSettingsControl.cs | 6 +++--- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/BetaCameras/RealSense2/RealSense2.cs b/BetaCameras/RealSense2/RealSense2.cs index c23a994d..987193ea 100644 --- a/BetaCameras/RealSense2/RealSense2.cs +++ b/BetaCameras/RealSense2/RealSense2.cs @@ -51,6 +51,8 @@ public Point2i ColorResolution if (value == _colorResolution) return; + ThrowIfBusy("color resolution"); + _updatingPipeline = true; StopPipeline(); DeactivateChannelImpl(ChannelNames.Color); @@ -97,6 +99,8 @@ public int ColorFPS if (value == _colorFPS) return; + ThrowIfBusy("color fps"); + _updatingPipeline = true; StopPipeline(); DeactivateChannelImpl(ChannelNames.Color); @@ -138,6 +142,8 @@ public Point2i DepthResolution if (value == _depthResolution) return; + ThrowIfBusy("depth resolution"); + _updatingPipeline = true; StopPipeline(); @@ -202,6 +208,8 @@ public int DepthFPS if (value == _depthFPS) return; + ThrowIfBusy("depth fps"); + _updatingPipeline = true; StopPipeline(); @@ -1881,6 +1889,12 @@ private void CheckRangeValid(RangeParamDesc desc, T value, T adjustedValue throw new Exception(string.Format("Value {0} (adjusted to {1} to match stepsize) for '{2}' is outside of the range between {3} and {4}", value, adjustedValue, desc.Name, desc.Min, desc.Max)); } + private void ThrowIfBusy(string propertyName) + { + if (_updatingPipeline) + throw new InvalidOperationException(string.Format("Can't set {0}. The pipeline is still in the process of updating a parameter.", propertyName)); + } + private (float min, float max, float step, float def) QueryOption(RealSense2API.Option option, string sensorName) { RealSense2API.QueryOptionInfo( diff --git a/MetriCam2.Controls/CameraSettingsControl.cs b/MetriCam2.Controls/CameraSettingsControl.cs index 6ec3af59..5d10568c 100644 --- a/MetriCam2.Controls/CameraSettingsControl.cs +++ b/MetriCam2.Controls/CameraSettingsControl.cs @@ -306,7 +306,7 @@ private void InitConfigurationParameters(Camera cam) if (paramDesc is MetriCam2.Camera.ListParamDesc) { - parameterValue = StringToPoint2i(comboBoxValue.SelectedItem as string); + parameterValue = ResolutionToPoint2i(comboBoxValue.SelectedItem as string); } else { @@ -526,7 +526,7 @@ private ComboBox CreateComboBox(Camera.IListParamDesc listParamDesc, int current if (paramDesc is MetriCam2.Camera.ListParamDesc) { - tmpVal = StringToPoint2i(item); + tmpVal = ResolutionToPoint2i(item); } else { @@ -645,7 +645,7 @@ private TextBox CreateTextBox(Camera.ParamDesc paramDesc, int currentRow) } #endregion - private Point2i StringToPoint2i(string s) + public static Point2i ResolutionToPoint2i(string s) { string[] stringValue = s.Split('x'); return new Point2i(int.Parse(stringValue[0]), int.Parse(stringValue[1])); From 8089ff79c3e856514e6c4822bcf9bb0fcaee3b86 Mon Sep 17 00:00:00 2001 From: Jan Lukas Gernert Date: Wed, 17 Jan 2018 13:13:26 +0100 Subject: [PATCH 31/32] More specific exceptions --- BetaCameras/RealSense2/RealSense2.cs | 14 +++++++------- BetaCameras/RealSense2/RealSense2API.cs | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/BetaCameras/RealSense2/RealSense2.cs b/BetaCameras/RealSense2/RealSense2.cs index 987193ea..546e7678 100644 --- a/BetaCameras/RealSense2/RealSense2.cs +++ b/BetaCameras/RealSense2/RealSense2.cs @@ -1364,7 +1364,7 @@ private void StopPipeline() { string msg = "RealSense2: Can't stop the pipeline since it is not running"; log.Error(msg); - throw new Exception(msg); + throw new InvalidOperationException(msg); } } @@ -1375,7 +1375,7 @@ private void StartPipeline() { string msg = "RealSense2: No camera that supports the current configuration detected"; log.Error(msg); - throw new Exception(msg); + throw new InvalidOperationException(msg); } if (RealSense2API.PipelineRunning) @@ -1401,7 +1401,7 @@ protected override void UpdateImpl() { string msg = "RealSense2: Can't update camera since pipeline is not running"; log.Error(msg); - throw new Exception(msg); + throw new InvalidOperationException(msg); } RealSense2API.ReleaseFrame(_currentColorFrame); @@ -1734,7 +1734,7 @@ private RealSense2API.RS2StreamProfile GetProfileFromCapturedFrames(string chann default: string msg = string.Format("RealSense2: stream profile for channel {0} not available", channelName); log.Error(msg); - throw new Exception(msg); + throw new ArgumentException(msg, channelName); } if (!frame.IsValid()) @@ -1874,7 +1874,7 @@ public void LoadCustomConfig(string json) private void CheckOptionSupported(RealSense2API.Option option, string optionName, string sensorName) { if (!this.IsConnected) - throw new Exception(string.Format("The property '{0}' can only be read or written when the camera is connected!", optionName)); + throw new InvalidOperationException(string.Format("The property '{0}' can only be read or written when the camera is connected!", optionName)); if (!RealSense2API.IsOptionSupported(_pipeline, sensorName, option)) throw new NotSupportedException(string.Format("Option '{0}' is not supported by the {1} sensor of this camera.", optionName, sensorName)); @@ -1884,9 +1884,9 @@ private void CheckRangeValid(RangeParamDesc desc, T value, T adjustedValue { if (!desc.IsValid(value)) if (adjusted) - throw new Exception(string.Format("Value {0} for '{1}' is outside of the range between {2} and {3}", value, desc.Name, desc.Min, desc.Max)); + throw new ArgumentOutOfRangeException(string.Format("Value {0} for '{1}' is outside of the range between {2} and {3}", value, desc.Name, desc.Min, desc.Max)); else - throw new Exception(string.Format("Value {0} (adjusted to {1} to match stepsize) for '{2}' is outside of the range between {3} and {4}", value, adjustedValue, desc.Name, desc.Min, desc.Max)); + throw new ArgumentOutOfRangeException(string.Format("Value {0} (adjusted to {1} to match stepsize) for '{2}' is outside of the range between {3} and {4}", value, adjustedValue, desc.Name, desc.Min, desc.Max)); } private void ThrowIfBusy(string propertyName) diff --git a/BetaCameras/RealSense2/RealSense2API.cs b/BetaCameras/RealSense2/RealSense2API.cs index ce4fac62..7983071a 100644 --- a/BetaCameras/RealSense2/RealSense2API.cs +++ b/BetaCameras/RealSense2/RealSense2API.cs @@ -1491,7 +1491,7 @@ unsafe private static void HandleError(IntPtr e) rs2_free_error(e); - throw new Exception($"Failed function: {functionName}\nErrormessage: {errorMsg}\nArguments: {arguments}"); + throw new Exception($"Failed function: {functionName} Errormessage: {errorMsg} Arguments: {arguments}"); } } } From 1224d3ceb05c5c5c6dd9965bc2fb4339b640dbf7 Mon Sep 17 00:00:00 2001 From: Jan Lukas Gernert Date: Wed, 17 Jan 2018 15:10:46 +0100 Subject: [PATCH 32/32] get name of argument for argumentexception instead of value --- BetaCameras/RealSense2/RealSense2.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BetaCameras/RealSense2/RealSense2.cs b/BetaCameras/RealSense2/RealSense2.cs index 546e7678..53f8a661 100644 --- a/BetaCameras/RealSense2/RealSense2.cs +++ b/BetaCameras/RealSense2/RealSense2.cs @@ -1734,7 +1734,7 @@ private RealSense2API.RS2StreamProfile GetProfileFromCapturedFrames(string chann default: string msg = string.Format("RealSense2: stream profile for channel {0} not available", channelName); log.Error(msg); - throw new ArgumentException(msg, channelName); + throw new ArgumentException(msg, nameof(channelName)); } if (!frame.IsValid())