跳转到内容

传感器相关接口

传感器相关的接口由如下类实现。您可以在 c++\include\drivers\sensors 目录下找到对应的头文件以获取完整 API。

头文件类名说明
sensor_factory.hSensorFactory传感器工厂类,用于创建不同类型的传感器实例。
sensor_interface.hSensorData传感器数据基类。所有传感器数据类型都应该继承此类,以提供时间戳信息。
SensorInterface传感器接口基类,定义了传感器控制的通用接口。所有特定类型的传感器驱动都应该继承此类。
StreamableSensor传感器数据流类型,继承自 SensorInterface
camera_data.hRGBData彩色图像数据类型,继承自 SensorData
RGBDData深度图像数据类型,继承自 RGBData
depth_camera_interface.hDepthCamera深度相机接口类,继承自 SensorInterface

创建传感器实例有两种方式,具体如下所示。

  1. 根据传感器参数,创建传感器实例。

    • 方法原型:

      static std::shared_ptr<SensorInterface> create_sensor(
      const std::unordered_map<std::string, std::string>& params)
    • 参数说明:

      参数名称类型说明

      params

      输入参数

      传感器相关参数,例如:

      {
      "sensor_name":"rs1", #传感器名称
      "sensor_type":"realsense", #传感器厂商
      "serial_number":"215322070063", #序列号
      "color_width":"640", #彩色图宽
      "color_height":"480", #彩色图高
      "depth_width":"640", #深度图宽
      "depth_height":"480", #深度图高
      "align_to_color":"true", #是否将深度图对齐到彩色图
      "frame_rate":"30" #帧率
      }
    • 返回值:

      若创建成功,则返回 SensorInterface 类型的共享智能指针;否则返回 nullptr

  2. 根据配置文件,创建传感器实例。

    • 方法原型:

      static std::shared_ptr<SensorInterface> create_sensor_from_config(
      const std::string& config_path)
    • 参数说明:

      参数名称类型说明

      config_path

      输入参数

      配置文件路径。配置文件为 JSON 文件,例如:

      {
      "sensor_name":"rs1", #传感器名称
      "sensor_type":"realsense", #传感器厂商
      "serial_number":"215322070063", #序列号
      "color_width":"640", #彩色图宽
      "color_height":"480", #彩色图高
      "depth_width":"640", #深度图宽
      "depth_height":"480", #深度图高
      "align_to_color":"true", #是否将深度图对齐到彩色图
      "frame_rate":"30" #帧率
      }
    • 返回值:

      若创建成功,则返回 SensorInterface 类型的共享智能指针;否则返回 nullptr

  • 方法原型:

    AbcErrorCode connect()
  • 返回值:

    AbcErrorCode::SUCCESS 表示成功;其他错误码,请参见 AbcErrorCode

  • 方法原型:

    AbcErrorCode disconnect()
  • 返回值:

    AbcErrorCode::SUCCESS 表示成功;其他错误码,请参见 AbcErrorCode

此方法用于注册一个回调函数,当传感器采集到新数据时,系统会自动调用该回调函数进行数据处理。

  • 方法原型:

    void set_data_callback(SensorCallback cb)
  • 参数说明:

    参数名称类型说明
    cb输入参数当传感器数据更新时,系统会自动调用此回调函数。SensorCallback 为函数指针类型。其数据类型,请参见 SensorCallback
  • 返回值:

    无。

  • 方法原型:

    std::string get_name() const
  • 返回值:

    传感器名称。

  • 方法原型:

    SensorType get_type() const
  • 返回值:

    传感器类型,具体参见 SensorType

  • 方法原型:

    SensorStatus get_status() const
  • 返回值:

    传感器状态,具体参见 SensorStatus

  • 方法原型:

    void set_status(SensorStatus status)
  • 参数说明:

    参数名称类型说明
    status输入参数传感器状态,具体参见 SensorStatus
  • 返回值:

    无。

  • 方法原型:

    std::chrono::system_clock::time_point get_timestamp()
  • 返回值:

    时间戳。

  • 方法原型:

    void set_timestamp(const std::chrono::system_clock::time_point& timestamp)
  • 参数说明:

    参数名称类型说明
    timestamp输入参数时间戳
  • 返回值:

    无。

  • 方法原型:

    AbcErrorCode start_stream()
  • 返回值:

    AbcErrorCode::SUCCESS 表示成功;其他错误码,请参见 AbcErrorCode

  • 方法原型:

    AbcErrorCode stop_stream()
  • 返回值:

    AbcErrorCode::SUCCESS 表示成功;其他错误码,请参见 AbcErrorCode

  • 方法原型:

    int get_width() const
  • 返回值:

    彩色图像宽度。

  • 方法原型:

    int get_height() const
  • 返回值:

    彩色图像高度。

  • 方法原型:

    int get_channels() const
  • 返回值:

    彩色图像通道数。

  • 方法原型:

    std::vector<uint8_t>& get_rgb_data()
  • 返回值:

    彩色图像数据。

  • 方法原型:

    void set_rgb_data(const std::vector<uint8_t>& data)
  • 参数说明:

    参数名称类型说明
    data输入参数彩色图像数据。
  • 返回值:

    无。

  • 方法原型:

    const AbcIntrinsic& get_color_intrinsic() const
  • 返回值:

    彩色相机内参,具体参见 AbcIntrinsic

  • 方法原型:

    void set_color_intrinsic(const AbcIntrinsic& intrinsic)
  • 参数说明:

    参数名称类型说明
    intrinsic输入参数彩色相机内参,具体参见 AbcIntrinsic
  • 返回值:

    无。

  • 方法原型:

    const std::vector<float>& get_depth_data() const
    std::vector<float>& get_depth_data()
  • 返回值:

    深度图像数据,单位为米。

  • 方法原型:

    void set_depth_data(const std::vector<float>& data)
  • 参数说明:

    参数名称类型说明
    data输入参数深度图像数据,单位为米。
  • 返回值:

    无。

  • 方法原型:

    int get_depth_width() const
  • 返回值:

    深度图像宽度。

  • 方法原型:

    int get_depth_height() const
  • 返回值:

    深度图像高度。

  • 方法原型:

    const AbcIntrinsic& get_depth_intrinsic() const
  • 返回值:

    深度相机内参,具体参见 AbcIntrinsic

  • 方法原型:

    void set_depth_intrinsic(const AbcIntrinsic& intrinsic)
  • 参数说明:

    参数名称类型说明
    intrinsic输入参数深度相机内参,具体参见 AbcIntrinsic
  • 返回值:

    无。

创建深度相机实例有两种方式,具体如下所示。

  1. 根据深度相机参数,创建深度相机实例。

    • 方法原型:

      static std::shared_ptr<DepthCamera> create_depth_camera(
      const DepthSensorParam& param)
    • 参数说明:

      参数名称类型说明
      param输入参数深度相机相关参数。其内部数据结构,请参见 DepthSensorParam
    • 返回值:

      若创建成功,则返回 DepthCamera 类型的共享智能指针;否则返回 nullptr

  2. 根据配置文件,创建深度相机实例。

    • 方法原型:

      static std::shared_ptr<DepthCamera> create_depth_camera_from_config(
      const std::string& config_path)
    • 参数说明:

      参数名称类型说明

      config_path

      输入参数

      配置文件路径。配置文件为 JSON 文件,包含以下键值对。

      {
      "sensor_name":"rs1", #传感器名称
      "sensor_type":"realsense", #传感器厂商
      "serial_number":"215322070063", #序列号
      "color_width":"640", #彩色图宽
      "color_height":"480", #彩色图高
      "depth_width":"640", #深度图宽
      "depth_height":"480", #深度图高
      "align_to_color":"true", #是否将深度图对齐到彩色图
      "frame_rate":"30" #帧率
      }
    • 返回值:

      若创建成功,则返回 DepthCamera 类型的共享智能指针;否则返回 nullptr

  • 方法原型:

    std::shared_ptr<const RGBDData> get_data()
  • 返回值:

    RGBDData 类型的共享智能指针。

  • 方法原型:

    AbcErrorCode start_stream()
  • 返回值:

    AbcErrorCode::SUCCESS 表示成功;其他错误码,请参见 AbcErrorCode

  • 方法原型:

    AbcErrorCode stop_stream()
  • 返回值:

    AbcErrorCode::SUCCESS 表示成功;其他错误码,请参见 AbcErrorCode

  • 方法原型:

    AbcErrorCode set_color_resolution(int width, int height)
  • 参数说明:

    参数名称类型说明
    width输入参数宽度
    height输入参数高度
  • 返回值:

    AbcErrorCode::SUCCESS 表示成功;其他错误码,请参见 AbcErrorCode

  • 方法原型:

    AbcErrorCode set_depth_resolution(int width, int height)
  • 参数说明:

    参数名称类型说明
    width输入参数宽度
    height输入参数高度
  • 返回值:

    AbcErrorCode::SUCCESS 表示成功;其他错误码,请参见 AbcErrorCode

  • 方法原型:

    AbcErrorCode set_framerate(int fps)
  • 参数说明:

    参数名称类型说明
    fps输入参数帧率
  • 返回值:

    AbcErrorCode::SUCCESS 表示成功;其他错误码,请参见 AbcErrorCode

  • 方法原型:

    AbcErrorCode set_align_to_color(bool align)
  • 参数说明:

    参数名称类型说明
    align输入参数是否将深度图像对齐到彩色图像
  • 返回值:

    AbcErrorCode::SUCCESS 表示成功;其他错误码,请参见 AbcErrorCode

  • 方法原型:

    bool is_aligned_to_color()
  • 返回值:

    true 表示深度图像已对齐到彩色图像,false 表示深度图像未对齐到彩色图像。