realsense_rust/kind/
option.rs

1//! Enumeration of RS2 sensor options.
2//!
3//! Not all options apply to every sensor. In order to retrieve the correct options,
4//! one must iterate over the `sensor` object for option compatibility.
5//!
6//! Notice that this option refers to the `sensor`, not the device. However, the device
7//! used also matters; sensors that are alike across devices are not guaranteed to share
8//! the same sensor options. Again, it is up to the user to query whether an option
9//! is supported by the sensor before attempting to set it. Failure to do so may cause
10//! an error in operation.
11
12use super::Rs2Exception;
13use num_derive::{FromPrimitive, ToPrimitive};
14use realsense_sys as sys;
15use std::ffi::CStr;
16use thiserror::Error;
17
18/// Occur when an option cannot be set.
19#[derive(Error, Debug)]
20pub enum OptionSetError {
21    /// The requested option is not supported by this sensor.
22    #[error("Option not supported on this sensor.")]
23    OptionNotSupported,
24    /// The requested option is read-only and cannot be set.
25    #[error("Option is read only.")]
26    OptionIsReadOnly,
27    /// The requested option could not be set. Reason is reported by the sensor.
28    #[error("Could not set option. Type: {0}; Reason: {1}")]
29    CouldNotSetOption(Rs2Exception, String),
30}
31
32/// The enumeration of options available in the RealSense SDK.
33///
34/// The majority of the options presented have a specific range of valid values. Run
35/// `sensor.get_option_range(Rs2Option::_)` to retrieve possible values of an Option type for your sensor.
36/// Setting a bad value will lead to a no-op at best, and a malfunction at worst.
37///
38/// # Deprecated Options
39///
40/// `AmbientLight`
41///
42/// - Equivalent to `RS2_OPTION_AMBIENT_LIGHT`
43/// - Replacement: [Rs2Option::DigitalGain].
44/// - Old Description: "Change the depth ambient light see rs2_ambient_light for values".
45///
46/// `ZeroOrderEnabled`
47///
48/// - Equivalent to `RS2_OPTION_ZERO_ORDER_ENABLED`
49/// - Replacement: N/A.
50/// - Old Description: "Toggle Zero-Order mode."
51///
52/// `ZeroOrderPointX`
53///
54/// - Equivalent to `RS2_OPTION_ZERO_ORDER_POINT_X`
55/// - Replacement: N/A.
56/// - Old Description: "Get the Zero order point x."
57///
58/// `ZeroOrderPointY`
59///
60/// - Equivalent to `RS2_OPTION_ZERO_ORDER_POINT_Y`
61/// - Replacement: N/A.
62/// - Old Description: "Get the Zero order point y."
63///
64/// `Trigger camera accuracy health`
65///
66/// - Deprecated as of 2.46 (not officially released, so technically 2.47)
67/// - Old Description: "Enable Depth & color frame sync with periodic calibration for proper
68/// alignment"
69///
70/// `Reset camera accuracy health`
71///
72/// - Deprecated as of 2.46 (not officially released, so technically 2.47)
73/// - Old Description: "Reset Camera Accuracy metric (if affected by TriggerCameraAccuracyHealth
74/// option)."
75#[repr(i32)]
76#[derive(FromPrimitive, ToPrimitive, Debug, Clone, Copy, PartialEq, Eq, Hash)]
77pub enum Rs2Option {
78    /// Enable/disable color backlight compensation.
79    BacklightCompensation = sys::rs2_option_RS2_OPTION_BACKLIGHT_COMPENSATION as i32,
80    /// Set color image brightness.
81    Brightness = sys::rs2_option_RS2_OPTION_BRIGHTNESS as i32,
82    /// Set color image contrast.
83    Contrast = sys::rs2_option_RS2_OPTION_CONTRAST as i32,
84    /// Set exposure time of color camera. Setting any value will disable auto exposure.
85    Exposure = sys::rs2_option_RS2_OPTION_EXPOSURE as i32,
86    /// Set color image gain.
87    Gain = sys::rs2_option_RS2_OPTION_GAIN as i32,
88    /// Set color image gamma setting.
89    Gamma = sys::rs2_option_RS2_OPTION_GAMMA as i32,
90    /// Set color image hue.
91    Hue = sys::rs2_option_RS2_OPTION_HUE as i32,
92    /// Set color image saturation.
93    Saturation = sys::rs2_option_RS2_OPTION_SATURATION as i32,
94    /// Set color image sharpness.
95    Sharpness = sys::rs2_option_RS2_OPTION_SHARPNESS as i32,
96    /// Set white balance of color image. Setting any value will disable auto white balance.
97    WhiteBalance = sys::rs2_option_RS2_OPTION_WHITE_BALANCE as i32,
98    /// Enable/disable color image auto-exposure.
99    EnableAutoExposure = sys::rs2_option_RS2_OPTION_ENABLE_AUTO_EXPOSURE as i32,
100    /// Enable/disable color image auto-white-balance
101    EnableAutoWhiteBalance = sys::rs2_option_RS2_OPTION_ENABLE_AUTO_WHITE_BALANCE as i32,
102    /// Set the visual preset on the sensor. `sensor.get_option_range()` provides
103    /// access to several recommend sets of option presets for a depth camera. The preset
104    /// selection varies between devices and sensors.
105    VisualPreset = sys::rs2_option_RS2_OPTION_VISUAL_PRESET as i32,
106    /// Set the power of the laser emitter, with 0 meaning projector off.
107    LaserPower = sys::rs2_option_RS2_OPTION_LASER_POWER as i32,
108    /// Set the number of patterns projected per frame. The higher the accuracy value,
109    /// the more patterns projected. Increasing the number of patterns helps to achieve
110    /// better accuracy. Note that this control affects Depth FPS.
111    Accuracy = sys::rs2_option_RS2_OPTION_ACCURACY as i32,
112    /// Set the motion vs. range trade-off. Lower values allow for better motion sensitivity.
113    /// Higher values allow for better depth range.
114    MotionRange = sys::rs2_option_RS2_OPTION_MOTION_RANGE as i32,
115    /// Set the filter to apply to each depth frame. Each one of the filter is optimized per the
116    /// application requirements.
117    FilterOption = sys::rs2_option_RS2_OPTION_FILTER_OPTION as i32,
118    /// Set the confidence level threshold used by the Depth algorithm pipe.
119    /// This determines whether a pixel will get a valid range or will be marked as invalid.
120    ConfidenceThreshold = sys::rs2_option_RS2_OPTION_CONFIDENCE_THRESHOLD as i32,
121    /// Enable/disable emitters. Emitter selection:
122    ///
123    /// - `0`: disable all emitters
124    /// - `1`: enable laser
125    /// - `2`: enable auto laser
126    /// - `3`: enable LED
127    EmitterEnabled = sys::rs2_option_RS2_OPTION_EMITTER_ENABLED as i32,
128    /// Set the number of frames the user is allowed to keep per stream.
129    /// Trying to hold on to more frames will cause frame drops.
130    FramesQueueSize = sys::rs2_option_RS2_OPTION_FRAMES_QUEUE_SIZE as i32,
131    /// Get the total number of detected frame drops from all streams.
132    TotalFrameDrops = sys::rs2_option_RS2_OPTION_TOTAL_FRAME_DROPS as i32,
133    /// Set the auto-exposure mode:
134    ///
135    /// - Static
136    /// - Anti-Flicker
137    /// - Hybrid
138    AutoExposureMode = sys::rs2_option_RS2_OPTION_AUTO_EXPOSURE_MODE as i32,
139    /// Set the power line frequency control for anti-flickering:
140    ///
141    /// - Off
142    /// - 50Hz
143    /// - 60Hz
144    /// - Auto
145    PowerLineFrequency = sys::rs2_option_RS2_OPTION_POWER_LINE_FREQUENCY as i32,
146    /// Get the current Temperature of the ASIC.
147    AsicTemperature = sys::rs2_option_RS2_OPTION_ASIC_TEMPERATURE as i32,
148    /// Enable/disable error handling.
149    ErrorPollingEnabled = sys::rs2_option_RS2_OPTION_ERROR_POLLING_ENABLED as i32,
150    /// Get the Current Temperature of the projector.
151    ProjectorTemperature = sys::rs2_option_RS2_OPTION_PROJECTOR_TEMPERATURE as i32,
152    /// Enable/disable trigger to be outputed from the camera to any external device on
153    /// every depth frame.
154    OutputTriggerEnabled = sys::rs2_option_RS2_OPTION_OUTPUT_TRIGGER_ENABLED as i32,
155    /// Get the current Motion-Module Temperature.
156    MotionModuleTemperature = sys::rs2_option_RS2_OPTION_MOTION_MODULE_TEMPERATURE as i32,
157    /// Set the number of meters represented by a single depth unit.
158    DepthUnits = sys::rs2_option_RS2_OPTION_DEPTH_UNITS as i32,
159    /// Enable/Disable automatic correction of the motion data.
160    EnableMotionCorrection = sys::rs2_option_RS2_OPTION_ENABLE_MOTION_CORRECTION as i32,
161    /// Allows sensor to dynamically ajust the frame rate depending on lighting conditions.
162    AutoExposurePriority = sys::rs2_option_RS2_OPTION_AUTO_EXPOSURE_PRIORITY as i32,
163    /// Set the color scheme for data visualization.
164    ColorScheme = sys::rs2_option_RS2_OPTION_COLOR_SCHEME as i32,
165    /// Enable/disable histogram equalization post-processing on the depth data.
166    HistogramEqualizationEnabled = sys::rs2_option_RS2_OPTION_HISTOGRAM_EQUALIZATION_ENABLED as i32,
167    /// Set the Minimal distance to the target.
168    MinDistance = sys::rs2_option_RS2_OPTION_MIN_DISTANCE as i32,
169    /// Set the Maximum distance to the target.
170    MaxDistance = sys::rs2_option_RS2_OPTION_MAX_DISTANCE as i32,
171    /// Get the texture mapping stream unique ID.
172    TextureSource = sys::rs2_option_RS2_OPTION_TEXTURE_SOURCE as i32,
173    /// Set the 2D-filter effect. The specific interpretation is given within the context of the filter.
174    FilterMagnitude = sys::rs2_option_RS2_OPTION_FILTER_MAGNITUDE as i32,
175    /// Set the 2D-filter parameter that controls the weight/radius for smoothing.
176    FilterSmoothAlpha = sys::rs2_option_RS2_OPTION_FILTER_SMOOTH_ALPHA as i32,
177    /// Set the 2D-filter range/validity threshold.
178    FilterSmoothDelta = sys::rs2_option_RS2_OPTION_FILTER_SMOOTH_DELTA as i32,
179    /// Enhance depth data post-processing with holes filling where appropriate.
180    HolesFill = sys::rs2_option_RS2_OPTION_HOLES_FILL as i32,
181    /// Get the distance in mm between the first and the second imagers in stereo-based depth cameras.
182    StereoBaseline = sys::rs2_option_RS2_OPTION_STEREO_BASELINE as i32,
183    /// Allows dynamically ajust the converge step value of the target exposure in
184    /// the Auto-Exposure algorithm.
185    AutoExposureConvergeStep = sys::rs2_option_RS2_OPTION_AUTO_EXPOSURE_CONVERGE_STEP as i32,
186    /// Impose Inter-camera HW synchronization mode. Applicable for D400/L500/Rolling Shutter SKUs.
187    InterCamSyncMode = sys::rs2_option_RS2_OPTION_INTER_CAM_SYNC_MODE as i32,
188    /// Select a stream to process.
189    StreamFilter = sys::rs2_option_RS2_OPTION_STREAM_FILTER as i32,
190    /// Select a stream format to process.
191    StreamFormatFilter = sys::rs2_option_RS2_OPTION_STREAM_FORMAT_FILTER as i32,
192    /// Select a stream index to process.
193    StreamIndexFilter = sys::rs2_option_RS2_OPTION_STREAM_INDEX_FILTER as i32,
194    /// When supported, this option make the camera to switch the emitter state every frame.
195    /// 0 for disabled, 1 for enabled.
196    EmitterOnOff = sys::rs2_option_RS2_OPTION_EMITTER_ON_OFF as i32,
197    /// Get the LDD temperature.
198    LldTemperature = sys::rs2_option_RS2_OPTION_LLD_TEMPERATURE as i32,
199    /// Get the MC temperature.
200    McTemperature = sys::rs2_option_RS2_OPTION_MC_TEMPERATURE as i32,
201    /// Get the MA temperature.
202    MaTemperature = sys::rs2_option_RS2_OPTION_MA_TEMPERATURE as i32,
203    /// Hardware stream configuration.
204    HardwarePreset = sys::rs2_option_RS2_OPTION_HARDWARE_PRESET as i32,
205    /// Enable/disable global time.
206    GlobalTimeEnabled = sys::rs2_option_RS2_OPTION_GLOBAL_TIME_ENABLED as i32,
207    /// Get the APD temperature.
208    ApdTemperature = sys::rs2_option_RS2_OPTION_APD_TEMPERATURE as i32,
209    /// Enable/disable an internal map.
210    EnableMapping = sys::rs2_option_RS2_OPTION_ENABLE_MAPPING as i32,
211    /// Enable/disable appearance-based relocalization.
212    EnableRelocalization = sys::rs2_option_RS2_OPTION_ENABLE_RELOCALIZATION as i32,
213    /// Enable/disable position jumping.
214    EnablePoseJumping = sys::rs2_option_RS2_OPTION_ENABLE_POSE_JUMPING as i32,
215    /// Enable/disable dynamic calibration.
216    EnableDynamicCalibration = sys::rs2_option_RS2_OPTION_ENABLE_DYNAMIC_CALIBRATION as i32,
217    /// Get the offset from sensor to depth origin in millimeters.
218    DepthOffset = sys::rs2_option_RS2_OPTION_DEPTH_OFFSET as i32,
219    /// Set the power of the LED (light emitting diode), with 0 meaning off
220    LedPower = sys::rs2_option_RS2_OPTION_LED_POWER as i32,
221    /// Preserve the previous map when starting.
222    EnableMapPreservation = sys::rs2_option_RS2_OPTION_ENABLE_MAP_PRESERVATION as i32,
223    /// Enable/disable sensor shutdown when a free-fall is detected (on by default).
224    FreefallDetectionEnabled = sys::rs2_option_RS2_OPTION_FREEFALL_DETECTION_ENABLED as i32,
225    /// Changes the exposure time of Avalanche Photo Diode in the receiver.
226    AvalanchePhotoDiode = sys::rs2_option_RS2_OPTION_AVALANCHE_PHOTO_DIODE as i32,
227    /// Changes the amount of sharpening in the post-processed image.
228    PostProcessingSharpening = sys::rs2_option_RS2_OPTION_POST_PROCESSING_SHARPENING as i32,
229    /// Changes the amount of sharpening in the pre-processed image.
230    PreProcessingSharpening = sys::rs2_option_RS2_OPTION_PRE_PROCESSING_SHARPENING as i32,
231    /// Control edges and background noise.
232    NoiseFiltering = sys::rs2_option_RS2_OPTION_NOISE_FILTERING as i32,
233    /// Enable/disable pixel invalidation.
234    InvalidationBypass = sys::rs2_option_RS2_OPTION_INVALIDATION_BYPASS as i32,
235    /// Change the depth digital gain see rs2_digital_gain for values.
236    DigitalGain = sys::rs2_option_RS2_OPTION_DIGITAL_GAIN as i32,
237    /// The resolution mode: see rs2_sensor_mode for values.
238    SensoeMode = sys::rs2_option_RS2_OPTION_SENSOR_MODE as i32,
239    /// Enable/disable Laser On constantly (GS SKU Only).
240    EmitterAlwaysOn = sys::rs2_option_RS2_OPTION_EMITTER_ALWAYS_ON as i32,
241    /// Depth Thermal Compensation for selected D400 SKUs.
242    ThermalCompensation = sys::rs2_option_RS2_OPTION_THERMAL_COMPENSATION as i32,
243    /// Set host performance mode to optimize device settings so host can keep up with workload.
244    /// Take USB transaction granularity as an example. Setting option to low performance host leads
245    /// to larger USB transaction sizes and a reduced number of transactions. This improves performance
246    /// and stability if the host machine is relatively weak compared to the workload.
247    HostPerformance = sys::rs2_option_RS2_OPTION_HOST_PERFORMANCE as i32,
248    /// Enable/disable HDR.
249    HdrEnabled = sys::rs2_option_RS2_OPTION_HDR_ENABLED as i32,
250    /// Get HDR Sequence name.
251    SequenceName = sys::rs2_option_RS2_OPTION_SEQUENCE_NAME as i32,
252    /// Get HDR Sequence size.
253    SequenceSize = sys::rs2_option_RS2_OPTION_SEQUENCE_SIZE as i32,
254    /// Get HDR Sequence ID - 0 is not HDR; sequence ID for HDR configuration starts from 1.
255    SequenceId = sys::rs2_option_RS2_OPTION_SEQUENCE_ID as i32,
256    /// Get Humidity temperature [in Celsius].
257    HumidityTemperature = sys::rs2_option_RS2_OPTION_HUMIDITY_TEMPERATURE as i32,
258    /// Enable/disable the maximum usable depth sensor range given the amount of ambient light in the scene.
259    EnableMaxUsableRange = sys::rs2_option_RS2_OPTION_ENABLE_MAX_USABLE_RANGE as i32,
260    /// Enable/disable the alternate IR, When enabling alternate IR, the IR image is holding the amplitude of the depth correlation.
261    AlternateIr = sys::rs2_option_RS2_OPTION_ALTERNATE_IR as i32,
262    /// Get an estimation of the noise on the IR image.
263    NoiseEstimation = sys::rs2_option_RS2_OPTION_NOISE_ESTIMATION as i32,
264    /// Enable/disable data collection for calculating IR pixel reflectivity.
265    EnableIrReflectivity = sys::rs2_option_RS2_OPTION_ENABLE_IR_REFLECTIVITY as i32,
266    /// Auto exposure limit in microseconds.
267    ///
268    /// Default is 0 which means full exposure range. If the requested exposure limit is greater
269    /// than frame time, it will be set to frame time at runtime. Setting will not take effect
270    /// until next streaming session.
271    AutoExposureLimit = sys::rs2_option_RS2_OPTION_AUTO_EXPOSURE_LIMIT as i32,
272    /// Auto gain limits ranging from 16 to 248.
273    ///
274    /// Default is 0 which means full gain. If the requested gain limit is less than 16, it will be
275    /// set to 16. If the requested gain limit is greater than 248, it will be set to 248. Setting
276    /// will not take effect until next streaming session.
277    AutoGainLimit = sys::rs2_option_RS2_OPTION_AUTO_GAIN_LIMIT as i32,
278    /// Enable receiver sensitivity according to ambient light, bounded by the Receiver GAin
279    /// control.
280    AutoReceiverSensitivity = sys::rs2_option_RS2_OPTION_AUTO_RX_SENSITIVITY as i32,
281    /// Changes the transmistter frequency frequencies increasing effective range over sharpness.
282    TransmitterFrequency = sys::rs2_option_RS2_OPTION_TRANSMITTER_FREQUENCY as i32,
283    /// Enables vertical binning which increases the maximal sensed distance.
284    VerticalBinning = sys::rs2_option_RS2_OPTION_VERTICAL_BINNING as i32,
285    /// Control receiver sensitivity to incoming light, both projected and ambient (same as APD on L515).
286    ReceiverSensitivity = sys::rs2_option_RS2_OPTION_RECEIVER_SENSITIVITY as i32,
287    /// Enable / disable color image auto-exposure
288    AutoExposureLimitToggle = sys::rs2_option_RS2_OPTION_AUTO_EXPOSURE_LIMIT_TOGGLE as i32,
289    /// Enable / disable color image auto-gain
290    AutoGainLimitToggle = sys::rs2_option_RS2_OPTION_AUTO_GAIN_LIMIT_TOGGLE as i32,
291    /// Select emitter (laser projector) frequency, see rs2_emitter_frequency for values
292    EmitterFrequency = sys::rs2_option_RS2_OPTION_EMITTER_FREQUENCY as i32,
293    /// Select depth sensor auto exposure mode see rs2_depth_auto_exposure_mode for values
294    DepthAutoExposureMode = sys::rs2_option_RS2_OPTION_DEPTH_AUTO_EXPOSURE_MODE as i32,
295    // Not included since this just tells us the total number of options.
296    //
297    // Count = sys::rs2_option_RS2_OPTION_COUNT as i32,
298}
299
300impl Rs2Option {
301    /// Get the option as a CStr.
302    pub fn to_cstr(self) -> &'static CStr {
303        unsafe {
304            let ptr = sys::rs2_option_to_string(self as sys::rs2_option);
305            CStr::from_ptr(ptr)
306        }
307    }
308
309    /// Get the option as a str.
310    pub fn to_str(self) -> &'static str {
311        self.to_cstr().to_str().unwrap()
312    }
313}
314
315impl ToString for Rs2Option {
316    fn to_string(&self) -> String {
317        self.to_str().to_owned()
318    }
319}
320
321/// The range of available values of a supported option.
322pub struct Rs2OptionRange {
323    /// The minimum value which will be accepted for this option
324    pub min: f32,
325    /// The maximum value which will be accepted for this option
326    pub max: f32,
327    /// The granularity of options which accept discrete values, or zero if the option accepts
328    /// continuous values
329    pub step: f32,
330    /// The default value of the option
331    pub default: f32,
332}
333
334#[cfg(test)]
335mod tests {
336    use super::*;
337    use num_traits::FromPrimitive;
338
339    #[test]
340    fn all_variants_exist() {
341        let deprecated_options = [
342            sys::rs2_option_RS2_OPTION_ZERO_ORDER_POINT_X as i32,
343            sys::rs2_option_RS2_OPTION_ZERO_ORDER_POINT_Y as i32,
344            sys::rs2_option_RS2_OPTION_ZERO_ORDER_ENABLED as i32,
345            sys::rs2_option_RS2_OPTION_AMBIENT_LIGHT as i32,
346            sys::rs2_option_RS2_OPTION_TRIGGER_CAMERA_ACCURACY_HEALTH as i32,
347            sys::rs2_option_RS2_OPTION_RESET_CAMERA_ACCURACY_HEALTH as i32,
348        ];
349
350        for i in 0..sys::rs2_option_RS2_OPTION_COUNT as i32 {
351            if deprecated_options.iter().any(|x| x == &i) {
352                continue;
353            }
354
355            assert!(
356                Rs2Option::from_i32(i).is_some(),
357                "Rs2Option variant for ordinal {} does not exist.",
358                i,
359            );
360        }
361    }
362}