RealSense系列文章 - 2.基础连接

RealSense系列文章 - 基础连接

本系列文章针对 D415/D435,不涉及早期产品
仅限于Windows平台下开发

1. 项目配置

使用VS2015创建一个空的控制台项目,然后编写如下代码:

#include "windows.h"
#include "stdlib.h"
#include "stdio.h"
#include 
#include "librealsense2/rs.hpp"

#pragma comment(lib, "realsense2.lib")

void main() {
    rs2::context ctx;
    auto dev_list = ctx.query_devices(); // snapshot of connected devices
    // 查询,包含普通摄像头在内
    int nNums = dev_list.size();
    printf("Dev num:%d\n", nNums);
    if (nNums<1)
    {
        system("pause");
        return;
    }
    for (auto dev : dev_list)
    {
        printf("======================================\n");
        printf("Device Name: %s\n", dev.get_info(RS2_CAMERA_INFO_NAME));
        printf("Serial Num: %s\n", dev.get_info(RS2_CAMERA_INFO_SERIAL_NUMBER));
        printf("Product ID: %s\n", dev.get_info(RS2_CAMERA_INFO_PRODUCT_ID));
        printf("Physcal Port: %s\n", dev.get_info(RS2_CAMERA_INFO_PHYSICAL_PORT));
        //printf("FireWall Ver: %s\n", dev.get_info(RS2_CAMERA_INFO_FIRMWARE_VERSION));
    }
    system("pause");
}
  • Intel RealSense SDK 2.0\include加入 include 目录。
  • Intel RealSense SDK 2.0\lib\x86加入lib目录。
  • Intel RealSense SDK 2.0\bin\x86\realsense2.dll复制到exe所在目录。

编译,运行,OK!

2. 数据获取

如果想获取数据,则需要使用pipeline,如下,获取深度数据Demo:

#include "windows.h"
#include "stdlib.h"
#include "stdio.h"
#include 
#include "librealsense2/rs.hpp"

#pragma comment(lib, "realsense2.lib")

void main() {
    rs2::config cfg;
    // 可开启2个红外、深度、彩色、鱼眼、陀螺仪、加速度传感器。
    cfg.enable_stream(RS2_STREAM_DEPTH, -1); // -1表示使用默认
    rs2::pipeline p; // 创建一个采集使用的pipeline
    try
    {
        p.start(cfg); // 使用默认的配置,在后台启动采集线程
    }
    catch (std::exception& e)
    {
        // 设备没有连接时,会有异常
        printf("Error: %s\n", e.what());
        system("pause");
        return;
    }

    while (true)
    {
        rs2::frameset frames = p.wait_for_frames(); // 阻塞等待获取帧数据
        rs2::depth_frame depth = frames.get_depth_frame(); // 从帧中获取深度数据 

        // 获取深度图像大小
        float width = depth.get_width();
        float height = depth.get_height();

        // 计算图像中心位置距离设备的距离
        float dist_to_center = depth.get_distance(width / 2, height / 2);

        std::cout << width << " x " << height << " , Image Center Point Distance: " << dist_to_center << "\r";
    }
    system("pause");
}

尴尬的是,如果开的东西多了,CPU 100%。。。

控制激光传感器:

rs2::pipeline pipe; 
rs2::pipeline_profile selection = pipe.start();
rs2::device selected_device = selection.get_device();
auto depth_sensor = selected_device.first();
auto scale =  depth_sensor.get_depth_scale();

if (depth_sensor.supports(RS2_OPTION_EMITTER_ENABLED))
{
    depth_sensor.set_option(RS2_OPTION_EMITTER_ENABLED, 1.f); // Enable emitter
    depth_sensor.set_option(RS2_OPTION_EMITTER_ENABLED, 0.f); // Disable emitter
}
if (depth_sensor.supports(RS2_OPTION_LASER_POWER))
{
    // Query min and max values:
    auto range = depth_sensor.get_option_range(RS2_OPTION_LASER_POWER);
    depth_sensor.set_option(RS2_OPTION_LASER_POWER, range.max); // Set max power
    depth_sensor.set_option(RS2_OPTION_LASER_POWER, 0.f); // Disable laser
}

3. 其他的?

I feel so sad!

look: https://github.com/IntelRealSense/librealsense/issues/1050

你可能感兴趣的:(C/C++)