Skip to content

Commit

Permalink
PR #13715 from Eran: adapter ROS2 node & parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
maloel authored Jan 28, 2025
2 parents 07171c5 + fdb21d5 commit 5b62c73
Show file tree
Hide file tree
Showing 91 changed files with 17,343 additions and 33 deletions.
3 changes: 3 additions & 0 deletions third-party/realdds/include/realdds/dds-device-server.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class dds_device_server
dds_guid const & guid() const;
std::shared_ptr< dds_participant > participant() const;
std::shared_ptr< dds_subscriber > subscriber() const { return _subscriber; }
std::shared_ptr< dds_publisher > publisher() const { return _publisher; }
std::string const & topic_root() const { return _topic_root; }
rsutils::string::slice debug_name() const;

Expand All @@ -85,6 +86,7 @@ class dds_device_server
bool operator!() const { return ! is_valid(); }

std::map< std::string, std::shared_ptr< dds_stream_server > > const & streams() const { return _stream_name_to_server; }
dds_options const & options() const { return _options; }

void publish_notification( topics::flexible_msg && );
void publish_metadata( rsutils::json && );
Expand All @@ -104,6 +106,7 @@ class dds_device_server
// Same as find_options, except throws if not found
std::shared_ptr< dds_option > get_option( std::string const & option_name, std::string const & stream_name ) const;


private:
struct control_sample;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ class dds_stream_sensor_bridge
start_sensor_callback;
typedef std::function< void( std::string const & sensor_name ) > stop_sensor_callback;
typedef std::function< void( std::string const & error_string ) > on_error_callback;
typedef std::function< void( std::shared_ptr< realdds::dds_stream_server > const &,
std::shared_ptr< realdds::dds_stream_profile > const & ) >
on_stream_profile_change_callback;

private:
std::map< std::string, sensor_bridge > _sensors;
Expand All @@ -64,6 +67,7 @@ class dds_stream_sensor_bridge
start_sensor_callback _on_start_sensor;
stop_sensor_callback _on_stop_sensor;
on_error_callback _on_error;
on_stream_profile_change_callback _on_stream_profile_change;

public:
dds_stream_sensor_bridge();
Expand Down Expand Up @@ -101,12 +105,16 @@ class dds_stream_sensor_bridge
// Return true if the stream for the given server is currently streaming
bool is_streaming( std::shared_ptr< dds_stream_server > const & ) const;

// Return the profile a server is set to, even if not yet streaming
std::shared_ptr< dds_stream_profile > get_profile( std::shared_ptr< dds_stream_server > const & ) const;

// Notifications
public:
void on_readers_changed( readers_changed_callback callback ) { _on_readers_changed = std::move( callback ); }
void on_start_sensor( start_sensor_callback callback ) { _on_start_sensor = std::move( callback ); }
void on_stop_sensor( stop_sensor_callback callback ) { _on_stop_sensor = std::move( callback ); }
void on_error( on_error_callback callback ) { _on_error = std::move( callback ); }
void on_stream_profile_change( on_stream_profile_change_callback callback ) { _on_stream_profile_change = std::move( callback ); }

// Impl
protected:
Expand Down
23 changes: 23 additions & 0 deletions third-party/realdds/include/realdds/topics/dds-topic-names.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,29 @@ constexpr char const * METADATA_TOPIC_NAME = "/metadata";
constexpr char const * DFU_TOPIC_NAME = "/dfu";


namespace ros2 {

constexpr char const * NAMESPACE = "/realsense"; // must begin with /

constexpr char const * ROOT = "rt/";
constexpr size_t ROOT_LEN = 3;
constexpr char const * SERVICE_REQUEST_ROOT = "rq/";
constexpr char const * SERVICE_RESPONSE_ROOT = "rr/";

constexpr char const * GET_PARAMETERS_NAME = "/get_parameters";
constexpr char const * SET_PARAMETERS_NAME = "/set_parameters";
constexpr char const * LIST_PARAMETERS_NAME = "/list_parameters";
constexpr char const * DESCRIBE_PARAMETERS_NAME = "/describe_parameters";

constexpr char const * REQUEST_SUFFIX = "Request";
constexpr char const * RESPONSE_SUFFIX = "Reply";

constexpr char const * DISCOVERY_INFO = "ros_discovery_info";
constexpr char const * PARAMETER_EVENTS_NAME = "parameter_events";

} // namespace ros2


namespace notification {
namespace key {
extern std::string const id;
Expand Down
2 changes: 1 addition & 1 deletion third-party/realdds/include/realdds/topics/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ The various topics are arranged hierarchically:

The code for each message type in this directory is generated from IDL files if the message format is defined by RealDDS.

For [ROS](#ros2), the IDLs are not available.
For [ROS](#ros2), the IDLs are available in the `/opt/ros/<distro>/share/` installation folder.

# Generation

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2025 Intel Corporation. All Rights Reserved.
#pragma once

#include <realdds/dds-defines.h>

#include <realdds/topics/ros2/rcl_interfaces/srv/DescribeParameters.h>
#include <realdds/topics/ros2/rcl_interfaces/msg/ParameterDescriptor.h>

#include <string>
#include <memory>
#include <vector>


namespace rcl_interfaces {
namespace srv {
class DescribeParameters_RequestPubSubType;
class DescribeParameters_ResponsePubSubType;
} // namespace srv
} // namespace rcl_interfaces


namespace realdds {


class dds_participant;
class dds_topic;
class dds_topic_reader;
class dds_topic_writer;


namespace topics {
namespace ros2 {


class describe_parameters_request_msg
{
rcl_interfaces::srv::DescribeParameters_Request _raw;

public:
using type = rcl_interfaces::srv::DescribeParameters_RequestPubSubType;

void clear() { _raw.names().clear(); }
std::vector< std::string > const & names() const { return _raw.names(); }
void add( std::string const & name ) { _raw.names().push_back( name ); }

bool is_valid() const { return ! names().empty(); }
void invalidate() { clear(); }


static std::shared_ptr< dds_topic > create_topic( std::shared_ptr< dds_participant > const & participant,
char const * topic_name );
static std::shared_ptr< dds_topic > create_topic( std::shared_ptr< dds_participant > const & participant,
std::string const & topic_name )
{
return create_topic( participant, topic_name.c_str() );
}

// This helper method will take the next sample from a reader.
//
// Returns true if successful. Make sure you still check is_valid() in case the sample info isn't!
// Returns false if no more data is available.
// Will throw if an unexpected error occurs.
//
//Note - copies the data.
//TODO - add an API for a function that loans the data and enables the user to free it later.
bool take_next( dds_topic_reader &, dds_sample * optional_sample = nullptr );
};


class describe_parameters_response_msg
{
rcl_interfaces::srv::DescribeParameters_Response _raw;

public:
using type = rcl_interfaces::srv::DescribeParameters_ResponsePubSubType;

using descriptor_type = rcl_interfaces::msg::ParameterDescriptor;

std::vector< descriptor_type > const & descriptors() const { return _raw.descriptors(); }
bool is_valid() const { return ! descriptors().empty(); }
void invalidate() { _raw.descriptors().clear(); }

void add( descriptor_type const & descriptor )
{
_raw.descriptors().push_back( descriptor );
}


static std::shared_ptr< dds_topic > create_topic( std::shared_ptr< dds_participant > const & participant,
char const * topic_name );
static std::shared_ptr< dds_topic > create_topic( std::shared_ptr< dds_participant > const & participant,
std::string const & topic_name )
{
return create_topic( participant, topic_name.c_str() );
}

// Sends out the response to the given writer
// The request sample is needed for ROS2 request-response mechanism to connect the two
// Returns a unique (to the writer) identifier for the sample that was sent, or 0 if unsuccessful
dds_sequence_number respond_to( dds_sample const & request_sample, dds_topic_writer & ) const;

// This helper method will take the next sample from a reader.
//
// Returns true if successful. Make sure you still check is_valid() in case the sample info isn't!
// Returns false if no more data is available.
// Will throw if an unexpected error occurs.
//
//Note - copies the data.
//TODO - add an API for a function that loans the data and enables the user to free it later.
bool take_next( dds_topic_reader &, dds_sample * optional_sample = nullptr );
};


} // namespace ros2
} // namespace topics
} // namespace realdds
112 changes: 112 additions & 0 deletions third-party/realdds/include/realdds/topics/ros2/get-parameters-msg.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// License: Apache 2.0. See LICENSE file in root directory.
// Copyright(c) 2025 Intel Corporation. All Rights Reserved.
#pragma once

#include <realdds/dds-defines.h>

#include <realdds/topics/ros2/rcl_interfaces/srv/GetParameters.h>
#include <realdds/topics/ros2/rcl_interfaces/msg/ParameterValue.h>

#include <string>
#include <memory>
#include <vector>


namespace rcl_interfaces {
namespace srv {
class GetParameters_RequestPubSubType;
class GetParameters_ResponsePubSubType;
} // namespace srv
} // namespace rcl_interfaces


namespace realdds {


class dds_participant;
class dds_topic;
class dds_topic_reader;
class dds_topic_writer;


namespace topics {
namespace ros2 {


class get_parameters_request_msg
{
rcl_interfaces::srv::GetParameters_Request _raw;

public:
using type = rcl_interfaces::srv::GetParameters_RequestPubSubType;

void clear() { _raw.names().clear(); }
std::vector< std::string > const & names() const { return _raw.names(); }
void add( std::string const & name ) { _raw.names().push_back( name ); }

bool is_valid() const { return ! names().empty(); }
void invalidate() { clear(); }


static std::shared_ptr< dds_topic > create_topic( std::shared_ptr< dds_participant > const & participant,
char const * topic_name );
static std::shared_ptr< dds_topic > create_topic( std::shared_ptr< dds_participant > const & participant,
std::string const & topic_name )
{
return create_topic( participant, topic_name.c_str() );
}

// This helper method will take the next sample from a reader.
//
// Returns true if successful. Make sure you still check is_valid() in case the sample info isn't!
// Returns false if no more data is available.
// Will throw if an unexpected error occurs.
//
//Note - copies the data.
//TODO - add an API for a function that loans the data and enables the user to free it later.
bool take_next( dds_topic_reader &, dds_sample * optional_sample = nullptr );
};


class get_parameters_response_msg
{
rcl_interfaces::srv::GetParameters_Response _raw;

public:
using type = rcl_interfaces::srv::GetParameters_ResponsePubSubType;

using value_type = rcl_interfaces::msg::ParameterValue;

std::vector< value_type > const & values() const { return _raw.values(); }
void add( value_type const & value ) { _raw.values().push_back( value ); }
bool is_valid() const { return ! values().empty(); }
void invalidate() { _raw.values().clear(); }

// Sends out the response to the given writer
// The request sample is needed for ROS2 request-response mechanism to connect the two
// Returns a unique (to the writer) identifier for the sample that was sent, or 0 if unsuccessful
dds_sequence_number respond_to( dds_sample const & request_sample, dds_topic_writer & ) const;

static std::shared_ptr< dds_topic > create_topic( std::shared_ptr< dds_participant > const & participant,
char const * topic_name );
static std::shared_ptr< dds_topic > create_topic( std::shared_ptr< dds_participant > const & participant,
std::string const & topic_name )
{
return create_topic( participant, topic_name.c_str() );
}

// This helper method will take the next sample from a reader.
//
// Returns true if successful. Make sure you still check is_valid() in case the sample info isn't!
// Returns false if no more data is available.
// Will throw if an unexpected error occurs.
//
//Note - copies the data.
//TODO - add an API for a function that loans the data and enables the user to free it later.
bool take_next( dds_topic_reader &, dds_sample * optional_sample = nullptr );
};


} // namespace ros2
} // namespace topics
} // namespace realdds
Loading

0 comments on commit 5b62c73

Please sign in to comment.