I am trying to create a simple pose data app using Intel realsense with T265. Since C examples database is very limited I am really struggling to start with this API.
Anyway here's my code:
/* Include the librealsense C header files */
#include <librealsense2/rs.h>
#include <librealsense2/h/rs_pipeline.h>
#include <librealsense2/h/rs_option.h>
#include <librealsense2/h/rs_frame.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// These parameters are reconfigurable //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#define STREAM RS2_STREAM_ANY
#define FORMAT RS2_FORMAT_ANY
#define WIDTH 0
#define HEIGHT 0
#define FPS 0
#define STREAM_INDEX -1
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void check_error(rs2_error* e)
{
if (e)
{
printf("rs_error was raised when calling %s(%s):\n", rs2_get_failed_function(e), rs2_get_failed_args(e));
printf(" %s\n", rs2_get_error_message(e));
exit(EXIT_FAILURE);
}
}
void print_device_info(rs2_device* dev)
{
rs2_error* e = 0;
printf("\nUsing device 0, an %s\n", rs2_get_device_info(dev, RS2_CAMERA_INFO_NAME, &e));
check_error(e);
printf(" Serial number: %s\n", rs2_get_device_info(dev, RS2_CAMERA_INFO_SERIAL_NUMBER, &e));
check_error(e);
printf(" Firmware version: %s\n\n", rs2_get_device_info(dev, RS2_CAMERA_INFO_FIRMWARE_VERSION, &e));
check_error(e);
}
int main()
{
rs2_error* e = 0;
// Create a context object. This object owns the handles to all connected realsense devices.
// The returned object should be released with rs2_delete_context(...)
rs2_context* ctx = rs2_create_context(RS2_API_VERSION, &e);
check_error(e);
/* Get a list of all the connected devices. */
// The returned object should be released with rs2_delete_device_list(...)
rs2_device_list* device_list = rs2_query_devices(ctx, &e);
check_error(e);
int dev_count = rs2_get_device_count(device_list, &e);
check_error(e);
printf("There are %d connected RealSense devices.\n", dev_count);
if (0 == dev_count)
return EXIT_FAILURE;
// Get the first connected device
// The returned object should be released with rs2_delete_device(...)
rs2_device* dev = rs2_create_device(device_list, 0, &e);
check_error(e);
print_device_info(dev);
// Create a pipeline to configure, start and stop camera streaming
// The returned object should be released with rs2_delete_pipeline(...)
rs2_pipeline* pipeline = rs2_create_pipeline(ctx, &e);
check_error(e);
// Create a config instance, used to specify hardware configuration
// The returned object should be released with rs2_delete_config(...)
rs2_config* config = rs2_create_config(&e);
check_error(e);
// Request a specific configuration
rs2_config_enable_stream(config, STREAM, STREAM_INDEX, WIDTH, HEIGHT, FORMAT, FPS, &e);
check_error(e);
// Start the pipeline streaming
// The retunred object should be released with rs2_delete_pipeline_profile(...)
rs2_pipeline_profile* pipeline_profile = rs2_pipeline_start_with_config(pipeline, config, &e);
check_error(e);
while (1)
{
// This call waits until a new composite_frame is available
// composite_frame holds a set of frames. It is used to prevent frame drops
// The returned object should be released with rs2_release_frame(...)
rs2_frame* frames = rs2_pipeline_wait_for_frames(pipeline, RS2_DEFAULT_TIMEOUT, &e);
check_error(e);
// Returns the number of frames embedded within the composite frame
int num_of_frames = rs2_embedded_frames_count(frames, &e);
check_error(e);
int i;
for (i = 0; i < num_of_frames; ++i)
{
// The retunred object should be released with rs2_release_frame(...)
rs2_frame* frame = rs2_extract_frame(frames, i, &e);
check_error(e);
// Check if the given frame can be extended to depth frame interface
// Accept only depth frames and skip other frames
if (0 == rs2_is_frame_extendable_to(frame, RS2_EXTENSION_POSE, &e))
continue;
rs2_pose pose_data;
rs2_pose_frame_get_pose_data(frame, &pose_data, &e);
// Print the distance
printf("data %f, %f, %f\n", pose_data.translation.x, pose_data.translation.y, pose_data.translation.z);
rs2_release_frame(frame);
}
rs2_release_frame(frames);
}
// Stop the pipeline streaming
rs2_pipeline_stop(pipeline, &e);
check_error(e);
// Release resources
rs2_delete_pipeline_profile(pipeline_profile);
rs2_delete_config(config);
rs2_delete_pipeline(pipeline);
rs2_delete_device(dev);
rs2_delete_device_list(device_list);
rs2_delete_context(ctx);
return EXIT_SUCCESS;
}
And here's what I get from the console:
There are 2 connected RealSense devices.
Using device 0, an Intel RealSense T265 Serial number: my cams serialnumber Firmware version: 0.2.0.951
rs_error was raised when calling rs2_pipeline_start_with_config(pipe:00000000021 90DB0, config:0000000002190E30): No device connected
I always fail in rs2_pipeline_start_with_config function. I tried different config parameters but result is the same. Let me also mention that I can run C++ pose sample which was delivered with the SDK with no problems. But I want to make it run using C.
Also, I don't know why it recognizes two same devices with the same serial number while only one T265 is connected to my PC.