ros2-dotnet / ros2_dotnet

.NET bindings for ROS2
Apache License 2.0
136 stars 54 forks source link

Add Quality of Service (QoS) to publishers and subscribers #107

Closed hoffmann-stefan closed 1 year ago

hoffmann-stefan commented 1 year ago

extracted from https://github.com/ros2-dotnet/ros2_dotnet/pull/94

Adds QoS basic settings to publisher and subscribers.

The default profiles (see https://github.com/ros2/rmw/blob/rolling/rmw/include/rmw/qos_profiles.h) are for another PR.

Example

// taken and stripped down from https://github.com/schiller-de/tf2_dotnet/blob/main/tf2_dotnet/TransformListener.cs

var tf2ListenerQosProfile = QosProfile.KeepLast(100);

// The with methods return a immutable copy with the changed value.
var tf2StaticListenerQosProfile = QosProfile.KeepLast(100)
    .WithTransientLocal();

var tfSubscription = node.CreateSubscription<TFMessage>("/tf", (TFMessage message) =>
{
    // ...
}, tf2ListenerQosProfile);

var tfStaticSubscription = node.CreateSubscription<TFMessage>("/tf_static", (TFMessage message) =>
{
    // ...
}, tf2StaticListenerQosProfile);

Added/changed public API

namespace ROS2
{
    public enum QosHistoryPolicy
    {
        SystemDefault = 0,
        KeepLast = 1,
        KeepAll = 2,
    }

    public enum QosReliabilityPolicy
    {
        SystemDefault = 0,
        Reliable = 1,
        BestEffort = 2,
    }

    public enum QosDurabilityPolicy
    {
        SystemDefault = 0,
        TransientLocal = 1,
        Volatile = 2,
    }

    public enum QosLivelinessPolicy
    {
        SystemDefault = 0,
        Automatic = 1,
        ManualByTopic = 3,
    }

    public sealed class QosProfile
    {
        // no default constructor
        private QosProfile() {}

        public static TimeSpan InfiniteDuration { get; }
        public static QosProfile DefaultProfile { get; }
        public QosHistoryPolicy History { get; }
        public int Depth { get; }
        public QosReliabilityPolicy Reliability { get; }
        public QosDurabilityPolicy Durability { get; }
        public TimeSpan Deadline { get; }
        public TimeSpan Lifespan { get; }
        public QosLivelinessPolicy Liveliness { get; }
        public TimeSpan LivelinessLeaseDuration { get; }
        public bool AvoidRosNamespaceConventions { get; }

        // factory methods
        public static QosProfile KeepLast(int depth);
        public static QosProfile KeepAll();

        // "immutable setters"
        public QosProfile WithKeepLast(int depth);
        public QosProfile WithKeepAll();

        public QosProfile WithReliability(QosReliabilityPolicy reliability);
        public QosProfile WithReliable();
        public QosProfile WithBestEffort();

        public QosProfile WithDurability(QosDurabilityPolicy durability);
    public QosProfile WithTransientLocal();
        public QosProfile WithVolatile();

        public QosProfile WithDeadline(TimeSpan deadline);
        public QosProfile WithLifespan(TimeSpan lifespan);

        public QosProfile WithLiveliness(QosLivelinessPolicy liveliness);
        public QosProfile WithLivelinessLeaseDuration(TimeSpan livelinessLeaseDuration);

        public QosProfile WithAvoidRosNamespaceConventions(bool avoidRosNamespaceConventions);
    }
}
 namespace ROS2
 {
     public sealed partial class Node
     {
-        public Publisher<T> CreatePublisher<T>(string topic) where T : IRosMessage
+        public Publisher<T> CreatePublisher<T>(string topic, QosProfile qosProfile = null) where T : IRosMessage

-        public Subscription<T> CreateSubscription<T>(string topic, Action<T> callback) where T : IRosMessage, new()
+        public Subscription<T> CreateSubscription<T>(string topic, Action<T> callback, QosProfile qosProfile = null) where T : IRosMessage, new()
     }
 }