Esempio n. 1
0
        /// <summary>
        ///   Create a ROS Topic Publisher
        /// </summary>
        /// <typeparam name="TMessage"> Topic Message Type </typeparam>
        /// <param name="topicName"> Topic Name </param>
        /// <param name="latching"> true: send the latest published message when subscribed topic </param>
        /// <returns> Publisher </returns>
        public Task <Publisher <TMessage> > PublisherAsync <TMessage>(string topicName, bool latching = false)
            where TMessage : IMessage, new()
        {
            if (_disposed)
            {
                throw new ObjectDisposedException("Node");
            }

            if (_topicContainer.HasPublisher(topicName))
            {
                throw new InvalidOperationException(topicName + " is already created.");
            }

            _logger.InfoFormat("Create Publisher: {0}", topicName);

            var publisher = new Publisher <TMessage>(topicName, NodeId, latching);

            _topicContainer.AddPublisher(publisher);
            publisher.Disposing += DisposePublisherAsync;

            var tcpRosListener = new TcpRosListener(0);

            _slaveServer.AddListener(topicName, tcpRosListener);

            var acceptDisposable = tcpRosListener.AcceptAsync()
                                   .Do(_ => _logger.Debug("Accepted for Publisher"))
                                   .Subscribe(socket => publisher.AddTopic(socket),
                                              ex => _logger.Error("Accept Error", ex));

            _publisherDisposables.Add(topicName, acceptDisposable);

            var tcs = new TaskCompletionSource <Publisher <TMessage> >();

            _logger.Debug("RegisterPublisher");
            _masterClient
            .RegisterPublisherAsync(NodeId, topicName, publisher.MessageType, _slaveServer.SlaveUri)
            .ContinueWith(task =>
            {
                _logger.Debug("Registered Publisher");

                if (task.Status == TaskStatus.RanToCompletion)
                {
                    publisher.UpdateSubscribers(task.Result);
                    tcs.SetResult(publisher);
                }
                else if (task.Status == TaskStatus.Faulted)
                {
                    _logger.Error("RegisterPublisher: Failure", task.Exception.InnerException);
                    tcs.SetException(task.Exception.InnerException);
                }
            });

            return(tcs.Task);
        }
        public void Initialize()
        {
            Ros.MasterUri = new Uri("http://localhost:11311/");
            Ros.HostName  = "localhost";

            var topicContainer = new TopicContainer();

            topicContainer.AddPublisher(new Publisher <std_msgs.String>("/test_topic", "test"));

            var tcpListener = new TcpRosListener(0);

            _slaveServer = new SlaveServer("test", 0, topicContainer);
            _slaveServer.AddListener("/test_topic", tcpListener);
        }
Esempio n. 3
0
        public void Initialize()
        {
            Ros.MasterUri     = new Uri("http://localhost:11311/");
            Ros.HostName      = "localhost";
            Ros.TopicTimeout  = 3000;
            Ros.XmlRpcTimeout = 3000;

            _container = new TopicContainer();
            var listener = new TcpRosListener(0);

            _slaveServer = new SlaveServer("test", 0, _container);
            _slaveServer.AddListener("test", listener);

            _slaveClient = new SlaveClient(_slaveServer.SlaveUri);
        }