IntelRealSense / librealsense

Intel® RealSense™ SDK
https://www.intelrealsense.com/
Apache License 2.0
7.49k stars 4.8k forks source link

Toggling the IR emitter while video is playing... #3151

Closed drhalftone closed 5 years ago

drhalftone commented 5 years ago

Are there any examples of how to toggle the IR emitter while the camera is running? I don't want to toggle on every frame. I just want to do what RealSense viewer does which is to toggle the IR emitter at any time. The only way I have been able to do it, is completely shutdown the video, toggle the emitter, and then reconnect and launch the camera streams again.

Is there any example code?

MartyG-RealSense commented 5 years ago

The link below has C++ code for controlling the emitter on-off state, under the 'Librealsense2' heading.

https://github.com/IntelRealSense/librealsense/wiki/API-How-To#controlling-the-laser

There is also a recent example from Dorodnic the RealSense SDK Manager for controlling auto-exposure on / off that you could probably adapt to use the emitter control instruction.

https://github.com/IntelRealSense/librealsense/issues/3141#issuecomment-457361895

drhalftone commented 5 years ago

What about C code instead of C++?

On Jan 26, 2019, at 3:06 AM, MartyG-RealSense notifications@github.com wrote:

The link below has C++ code for controlling the emitter on-off state, under the 'Librealsense2' heading.

https://github.com/IntelRealSense/librealsense/wiki/API-How-To#controlling-the-laser https://github.com/IntelRealSense/librealsense/wiki/API-How-To#controlling-the-laser There is also a recent example from Dorodnic the RealSense SDK Manager for controlling auto-exposure on / off that you could probably adapt to use the emitter control instruction.

3141 (comment) https://github.com/IntelRealSense/librealsense/issues/3141#issuecomment-457361895

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/IntelRealSense/librealsense/issues/3151#issuecomment-457812114, or mute the thread https://github.com/notifications/unsubscribe-auth/AKL1oq3CB1RX6gf60ahu8It2zF3CXZr7ks5vHAyhgaJpZM4aT8Qv.

MartyG-RealSense commented 5 years ago

The only reference I could find was a C user who said that they set the emitter manually using set_option.

https://github.com/IntelRealSense/librealsense/issues/1217

I do not know the C language well enough to explain how it works though. One of the Intel guys such as Dorodnic may be able to help with that.

drhalftone commented 5 years ago

Yes, that post describes how I set the emitter state before I start streaming video. So the problem now is how do I change the state after I've started the pipeline, and as you suggest, I'll put this on the back burner until the Intel team can get back to me.

dorodnic commented 5 years ago

It would be roughly the same, just like with python, Matlab and other wrappers. We do provide couple of simple C examples. C bindings exist mostly for integration with other languages, so I'd strongly advice against this approach.

drhalftone commented 5 years ago

So let me make sure I understand this. You don’t want us to use C libraries directly? Also, do you have an example in python, Matlab, or anything other than C++ that demonstrates toggling the emitter while streaming video? The only example I have found sets the emitter state before starting the video stream.

On Jan 27, 2019, at 4:04 AM, Sergey Dorodnicov notifications@github.com wrote:

It would be roughly the same, just like with python, Matlab and other wrappers. We do provide couple of simple C examples https://github.com/IntelRealSense/librealsense/tree/master/examples/C. C bindings exist mostly for integration with other languages, so I'd strongly advice against this approach.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/IntelRealSense/librealsense/issues/3151#issuecomment-457900957, or mute the thread https://github.com/notifications/unsubscribe-auth/AKL1oqPuFYNDcBDmCjvIFPKo1GvvvgURks5vHWuGgaJpZM4aT8Qv.

drhalftone commented 5 years ago

The following solution assumes you have a pointer to an rs2_profile generated by rs2_pipeline_start_with_config() and stored in profile and that you've already started collecting live video from the sensor. So while the sensor is running, you can interject the following code to toggle the IR emitter: `

    // CREATE A VARIABLE TO KEEP TRACK OF ERRORS
    rs2_error *err = nullptr;
    rs2_device *device = rs2_pipeline_profile_get_device(profile, &err);
    if (err) {
        errorString = QString("Unable to retreive device from profile: %1").arg(QString(rs2_get_error_message(err)));
        emit emitError(errorString);
        return;
    }

    // GET SENSOR LIST
    rs2_sensor_list *sensorList = rs2_query_sensors(device, &err);
    if (err) {
        errorString = QString("Unable to retreive valid context: %1").arg(QString(rs2_get_error_message(err)));
        emit emitError(errorString);
        return;
    }

    // MAKE SURE WE HAVE AT LEAST ONE VALID SENSOR
    int numSensors = rs2_get_sensors_count(sensorList, &err);
    if (err) {
        errorString = QString("Unable to retreive valid context: %1").arg(QString(rs2_get_error_message(err)));
        emit emitError(errorString);
        return;
    } else if (numSensors == 0) {
        errorString = QString("Unable to retreive valid context: %1").arg(QString(rs2_get_error_message(err)));
        emit emitError(errorString);
        return;
    }

    // GET A HANDLE TO THE FIRST SENSOR IN THE LIST
    rs2_sensor *sensor = rs2_create_sensor(sensorList, 0, &err);
    if (err) {
        errorString = QString("Unable to create sensor: %1").arg(QString(rs2_get_error_message(err)));
        emit emitError(errorString);
        return;
    } else if (sensor == nullptr) {
        errorString = QString("Unable to retreive valid sensor.");
        emit emitError(errorString);
        return;
    }

    // ENABLE/DISABLE THE INFRARED EMITTER
    rs2_set_option((rs2_options *)sensor, RS2_OPTION_EMITTER_ENABLED, (float)state, &err);
    if (err) {
        errorString = QString("Unable to disable emitter: %1").arg(QString(rs2_get_error_message(err)));
        emit emitError(errorString);
        return;
    }

    // DELETE THE SENSOR NOW THAT WE ARE DONE WITH IT
    rs2_delete_sensor(sensor);
    rs2_delete_sensor_list(sensorList);
    rs2_delete_device(device);

`

So after the above block of code is run, you can continue to collect sensor video.