传感器相关接口
传感器相关的接口由如下类实现。您可以在 c++\include\drivers\sensors 目录下找到对应的头文件以获取完整 API。
| 头文件 | 类名 | 说明 |
sensor_factory.h | SensorFactory | 传感器工厂类,用于创建不同类型的传感器实例。 |
sensor_interface.h | SensorData | 传感器数据基类。所有传感器数据类型都应该继承此类,以提供时间戳信息。 |
SensorInterface | 传感器接口基类,定义了传感器控制的通用接口。所有特定类型的传感器驱动都应该继承此类。 | |
StreamableSensor | 传感器数据流类型,继承自 SensorInterface。 | |
camera_data.h | RGBData | 彩色图像数据类型,继承自 SensorData。 |
RGBDData | 深度图像数据类型,继承自 RGBData。 | |
depth_camera_interface.h | DepthCamera | 深度相机接口类,继承自 SensorInterface。 |
创建传感器实例有两种方式,具体如下所示。
-
根据传感器参数,创建传感器实例。
-
方法原型:
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。
-
-
根据配置文件,创建传感器实例。
-
方法原型:
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。
注册传感器数据处理回调函数
Section titled “注册传感器数据处理回调函数”此方法用于注册一个回调函数,当传感器采集到新数据时,系统会自动调用该回调函数进行数据处理。
-
方法原型:
void set_data_callback(SensorCallback cb) -
参数说明:
参数名称 类型 说明 cb输入参数 当传感器数据更新时,系统会自动调用此回调函数。 SensorCallback为函数指针类型。其数据类型,请参见 SensorCallback。 -
返回值:
无。
获取传感器名称
Section titled “获取传感器名称”-
方法原型:
std::string get_name() const -
返回值:
传感器名称。
获取传感器类型
Section titled “获取传感器类型”-
方法原型:
SensorType get_type() const -
返回值:
传感器类型,具体参见 SensorType。
获取传感器状态
Section titled “获取传感器状态”-
方法原型:
SensorStatus get_status() const -
返回值:
传感器状态,具体参见 SensorStatus。
设置传感器状态
Section titled “设置传感器状态”-
方法原型:
void set_status(SensorStatus status) -
参数说明:
参数名称 类型 说明 status输入参数 传感器状态,具体参见 SensorStatus。 -
返回值:
无。
获取数据时间戳
Section titled “获取数据时间戳”-
方法原型:
std::chrono::system_clock::time_point get_timestamp() -
返回值:
时间戳。
设置数据时间戳
Section titled “设置数据时间戳”-
方法原型:
void set_timestamp(const std::chrono::system_clock::time_point& timestamp) -
参数说明:
参数名称 类型 说明 timestamp输入参数 时间戳 -
返回值:
无。
传感器数据流
Section titled “传感器数据流”-
方法原型:
AbcErrorCode start_stream() -
返回值:
AbcErrorCode::SUCCESS表示成功;其他错误码,请参见 AbcErrorCode。
-
方法原型:
AbcErrorCode stop_stream() -
返回值:
AbcErrorCode::SUCCESS表示成功;其他错误码,请参见 AbcErrorCode。
获取彩色图像宽度
Section titled “获取彩色图像宽度”-
方法原型:
int get_width() const -
返回值:
彩色图像宽度。
获取彩色图像高度
Section titled “获取彩色图像高度”-
方法原型:
int get_height() const -
返回值:
彩色图像高度。
获取彩色图像通道数
Section titled “获取彩色图像通道数”-
方法原型:
int get_channels() const -
返回值:
彩色图像通道数。
获取彩色图像数据
Section titled “获取彩色图像数据”-
方法原型:
std::vector<uint8_t>& get_rgb_data() -
返回值:
彩色图像数据。
设置彩色图像数据
Section titled “设置彩色图像数据”-
方法原型:
void set_rgb_data(const std::vector<uint8_t>& data) -
参数说明:
参数名称 类型 说明 data输入参数 彩色图像数据。 -
返回值:
无。
获取彩色相机内参
Section titled “获取彩色相机内参”-
方法原型:
const AbcIntrinsic& get_color_intrinsic() const -
返回值:
彩色相机内参,具体参见 AbcIntrinsic。
设置彩色相机内参
Section titled “设置彩色相机内参”-
方法原型:
void set_color_intrinsic(const AbcIntrinsic& intrinsic) -
参数说明:
参数名称 类型 说明 intrinsic输入参数 彩色相机内参,具体参见 AbcIntrinsic。 -
返回值:
无。
获取深度图像数据
Section titled “获取深度图像数据”-
方法原型:
const std::vector<float>& get_depth_data() conststd::vector<float>& get_depth_data() -
返回值:
深度图像数据,单位为米。
设置深度图像数据
Section titled “设置深度图像数据”-
方法原型:
void set_depth_data(const std::vector<float>& data) -
参数说明:
参数名称 类型 说明 data输入参数 深度图像数据,单位为米。 -
返回值:
无。
获取深度图像宽度
Section titled “获取深度图像宽度”-
方法原型:
int get_depth_width() const -
返回值:
深度图像宽度。
获取深度图像高度
Section titled “获取深度图像高度”-
方法原型:
int get_depth_height() const -
返回值:
深度图像高度。
获取深度相机内参
Section titled “获取深度相机内参”-
方法原型:
const AbcIntrinsic& get_depth_intrinsic() const -
返回值:
深度相机内参,具体参见 AbcIntrinsic。
设置深度相机内参
Section titled “设置深度相机内参”-
方法原型:
void set_depth_intrinsic(const AbcIntrinsic& intrinsic) -
参数说明:
参数名称 类型 说明 intrinsic输入参数 深度相机内参,具体参见 AbcIntrinsic。 -
返回值:
无。
创建深度相机
Section titled “创建深度相机”创建深度相机实例有两种方式,具体如下所示。
-
根据深度相机参数,创建深度相机实例。
-
方法原型:
static std::shared_ptr<DepthCamera> create_depth_camera(const DepthSensorParam& param) -
参数说明:
参数名称 类型 说明 param输入参数 深度相机相关参数。其内部数据结构,请参见 DepthSensorParam。 -
返回值:
若创建成功,则返回
DepthCamera类型的共享智能指针;否则返回nullptr。
-
-
根据配置文件,创建深度相机实例。
-
方法原型:
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。
-
获取深度图像数据
Section titled “获取深度图像数据”-
方法原型:
std::shared_ptr<const RGBDData> get_data() -
返回值:
RGBDData类型的共享智能指针。
-
方法原型:
AbcErrorCode start_stream() -
返回值:
AbcErrorCode::SUCCESS表示成功;其他错误码,请参见 AbcErrorCode。
-
方法原型:
AbcErrorCode stop_stream() -
返回值:
AbcErrorCode::SUCCESS表示成功;其他错误码,请参见 AbcErrorCode。
设置彩色图像分辨率
Section titled “设置彩色图像分辨率”-
方法原型:
AbcErrorCode set_color_resolution(int width, int height) -
参数说明:
参数名称 类型 说明 width输入参数 宽度 height输入参数 高度 -
返回值:
AbcErrorCode::SUCCESS表示成功;其他错误码,请参见 AbcErrorCode。
设置深度图像分辨率
Section titled “设置深度图像分辨率”-
方法原型:
AbcErrorCode set_depth_resolution(int width, int height) -
参数说明:
参数名称 类型 说明 width输入参数 宽度 height输入参数 高度 -
返回值:
AbcErrorCode::SUCCESS表示成功;其他错误码,请参见 AbcErrorCode。
-
方法原型:
AbcErrorCode set_framerate(int fps) -
参数说明:
参数名称 类型 说明 fps输入参数 帧率 -
返回值:
AbcErrorCode::SUCCESS表示成功;其他错误码,请参见 AbcErrorCode。
设置深度图像对齐到彩色图像
Section titled “设置深度图像对齐到彩色图像”-
方法原型:
AbcErrorCode set_align_to_color(bool align) -
参数说明:
参数名称 类型 说明 align输入参数 是否将深度图像对齐到彩色图像 -
返回值:
AbcErrorCode::SUCCESS表示成功;其他错误码,请参见 AbcErrorCode。
获取当前对齐设置
Section titled “获取当前对齐设置”-
方法原型:
bool is_aligned_to_color() -
返回值:
true表示深度图像已对齐到彩色图像,false表示深度图像未对齐到彩色图像。