rs-distance.c
Go to the documentation of this file.
1 // License: Apache 2.0. See LICENSE file in root directory.
2 // Copyright(c) 2017 Intel Corporation. All Rights Reserved.
3 
4 /* Include the librealsense C header files */
5 #include <librealsense2/rs.h>
9 #include "example.h"
10 
11 #include <stdlib.h>
12 #include <stdint.h>
13 #include <stdio.h>
14 
15 
17 // These parameters are reconfigurable //
19 #define STREAM RS2_STREAM_DEPTH // rs2_stream is a types of data provided by RealSense device //
20 #define FORMAT RS2_FORMAT_Z16 // rs2_format identifies how binary data is encoded within a frame //
21 #define WIDTH 640 // Defines the number of columns for each frame or zero for auto resolve//
22 #define HEIGHT 0 // Defines the number of lines for each frame or zero for auto resolve //
23 #define FPS 30 // Defines the rate of frames per second //
24 #define STREAM_INDEX 0 // Defines the stream index, used for multiple streams of the same type //
25 
27 
28 int main()
29 {
30  rs2_error* e = 0;
31 
32  // Create a context object. This object owns the handles to all connected realsense devices.
33  // The returned object should be released with rs2_delete_context(...)
35  check_error(e);
36 
37  /* Get a list of all the connected devices. */
38  // The returned object should be released with rs2_delete_device_list(...)
39  rs2_device_list* device_list = rs2_query_devices(ctx, &e);
40  check_error(e);
41 
42  int dev_count = rs2_get_device_count(device_list, &e);
43  check_error(e);
44  printf("There are %d connected RealSense devices.\n", dev_count);
45  if (0 == dev_count)
46  return EXIT_FAILURE;
47 
48  // Get the first connected device
49  // The returned object should be released with rs2_delete_device(...)
50  rs2_device* dev = rs2_create_device(device_list, 0, &e);
51  check_error(e);
52 
53  print_device_info(dev);
54 
55  // Create a pipeline to configure, start and stop camera streaming
56  // The returned object should be released with rs2_delete_pipeline(...)
58  check_error(e);
59 
60  // Create a config instance, used to specify hardware configuration
61  // The retunred object should be released with rs2_delete_config(...)
63  check_error(e);
64 
65  // Request a specific configuration
67  check_error(e);
68 
69  // Start the pipeline streaming
70  // The retunred object should be released with rs2_delete_pipeline_profile(...)
72  if (e)
73  {
74  printf("The connected device doesn't support depth streaming!\n");
75  exit(EXIT_FAILURE);
76  }
77 
78  while (1)
79  {
80  // This call waits until a new composite_frame is available
81  // composite_frame holds a set of frames. It is used to prevent frame drops
82  // The returned object should be released with rs2_release_frame(...)
84  check_error(e);
85 
86  // Returns the number of frames embedded within the composite frame
87  int num_of_frames = rs2_embedded_frames_count(frames, &e);
88  check_error(e);
89 
90  int i;
91  for (i = 0; i < num_of_frames; ++i)
92  {
93  // The retunred object should be released with rs2_release_frame(...)
94  rs2_frame* frame = rs2_extract_frame(frames, i, &e);
95  check_error(e);
96 
97  // Check if the given frame can be extended to depth frame interface
98  // Accept only depth frames and skip other frames
100  continue;
101 
102  // Get the depth frame's dimensions
103  int width = rs2_get_frame_width(frame, &e);
104  check_error(e);
105  int height = rs2_get_frame_height(frame, &e);
106  check_error(e);
107 
108  // Query the distance from the camera to the object in the center of the image
109  float dist_to_center = rs2_depth_frame_get_distance(frame, width / 2, height / 2, &e);
110  check_error(e);
111 
112  // Print the distance
113  printf("The camera is facing an object %.3f meters away.\n", dist_to_center);
114 
115  rs2_release_frame(frame);
116  }
117 
118  rs2_release_frame(frames);
119  }
120 
121  // Stop the pipeline streaming
122  rs2_pipeline_stop(pipeline, &e);
123  check_error(e);
124 
125  // Release resources
126  rs2_delete_pipeline_profile(pipeline_profile);
127  rs2_delete_config(config);
128  rs2_delete_pipeline(pipeline);
129  rs2_delete_device(dev);
130  rs2_delete_device_list(device_list);
131  rs2_delete_context(ctx);
132 
133  return EXIT_SUCCESS;
134 }
void rs2_config_enable_stream(rs2_config *config, rs2_stream stream, int index, int width, int height, rs2_format format, int framerate, rs2_error **error)
Definition: rs.cpp:1928
void print_device_info(rs2_device *dev)
Definition: example.h:19
#define FPS
Definition: rs-distance.c:23
Exposes RealSense processing-block functionality for C compilers.
#define RS2_API_VERSION
Definition: rs.h:41
rs2_device_list * rs2_query_devices(const rs2_context *context, rs2_error **error)
Definition: rs.cpp:208
int rs2_is_frame_extendable_to(const rs2_frame *frame, rs2_extension extension_type, rs2_error **error)
Definition: rs.cpp:1461
#define HEIGHT
Definition: rs-distance.c:22
void rs2_delete_device(rs2_device *device)
Definition: rs.cpp:301
rs2_context * rs2_create_context(int api_version, rs2_error **error)
Creates RealSense context that is required for the rest of the API.
Definition: rs.cpp:163
rs2_config * rs2_create_config(rs2_error **error)
Definition: rs.cpp:1914
void rs2_delete_context(rs2_context *context)
Frees the relevant context object.
Definition: rs.cpp:171
rs2_pipeline * rs2_create_pipeline(rs2_context *ctx, rs2_error **error)
Definition: rs.cpp:1756
rs2_device * rs2_create_device(const rs2_device_list *info_list, int index, rs2_error **error)
Definition: rs.cpp:289
void check_error(rs2_error *e)
Definition: example.h:9
e
Definition: rmse.py:177
Exposes librealsense functionality for C compilers.
int rs2_get_frame_height(const rs2_frame *frame, rs2_error **error)
Definition: rs.cpp:955
rs2_frame * rs2_pipeline_wait_for_frames(rs2_pipeline *pipe, unsigned int timeout_ms, rs2_error **error)
Definition: rs.cpp:1774
Exposes RealSense frame functionality for C compilers.
#define FORMAT
Definition: rs-distance.c:20
int rs2_embedded_frames_count(rs2_frame *composite, rs2_error **error)
Definition: rs.cpp:2144
int rs2_get_frame_width(const rs2_frame *frame, rs2_error **error)
Definition: rs.cpp:947
float rs2_depth_frame_get_distance(const rs2_frame *frame_ref, int x, int y, rs2_error **error)
Definition: rs.cpp:2346
GLint GLsizei GLsizei height
Definition: api.h:28
#define RS2_DEFAULT_TIMEOUT
Definition: rs_config.h:13
Exposes sensor options functionality for C compilers.
static const textual_icon exit
Definition: model-views.h:254
rs2_pipeline_profile * rs2_pipeline_start_with_config(rs2_pipeline *pipe, rs2_config *config, rs2_error **error)
Definition: rs.cpp:1834
#define WIDTH
Definition: rs-distance.c:21
void rs2_pipeline_stop(rs2_pipeline *pipe, rs2_error **error)
Definition: rs.cpp:1766
void rs2_delete_config(rs2_config *config)
Definition: rs.cpp:1920
int i
void rs2_release_frame(rs2_frame *frame)
Definition: rs.cpp:993
rs2_frame * rs2_extract_frame(rs2_frame *composite, int index, rs2_error **error)
Definition: rs.cpp:2114
#define STREAM
Definition: rs-distance.c:19
void rs2_delete_device_list(rs2_device_list *info_list)
Definition: rs.cpp:275
void rs2_delete_pipeline_profile(rs2_pipeline_profile *profile)
Definition: rs.cpp:1905
int rs2_get_device_count(const rs2_device_list *info_list, rs2_error **error)
Definition: rs.cpp:259
int main()
Definition: rs-distance.c:28
void rs2_delete_pipeline(rs2_pipeline *pipe)
Definition: rs.cpp:1819
#define STREAM_INDEX
Definition: rs-distance.c:24
struct rs2_frame rs2_frame
Definition: rs_types.h:261
GLint GLsizei width


librealsense2
Author(s): Sergey Dorodnicov , Doron Hirshberg , Mark Horn , Reagan Lopez , Itay Carpis
autogenerated on Mon May 3 2021 02:47:40