Class: ROS::Subscriber

Inherits:
Topic
  • Object
show all
Defined in:
lib/ros/subscriber.rb

Overview

subscriber of ROS topic. This is created by ROS::Node#subscribe. Please use proc block for callback method. It uses ROS::TCPROS::Client for message transfer. Subscription can be shutdown by this ROS::Subscriber#shutdown

node = ROS::Node.new('/rosruby/sample_subscriber')
sub = node.subscribe('/chatter', Std_msgs::String) do |msg|
  puts "message come! = \'#{msg.data}\'"
end
while node.ok?
  node.spin_once
  sleep(1)
end

Instance Attribute Summary (collapse)

Attributes inherited from Topic

#caller_id, #topic_name, #topic_type

Instance Method Summary (collapse)

Methods inherited from Topic

#close, #set_manager

Constructor Details

- (Subscriber) initialize(caller_id, topic_name, topic_type, callback = nil, tcp_no_delay = nil)

Returns a new instance of Subscriber

Parameters:

  • caller_id (String)

    caller id of this node

  • topic_name (String)

    name of this topic (String)

  • topic_type (Class)

    class of msg

  • callback (Proc) (defaults to: nil)

    callback for this topic

  • tcp_no_delay (Boolean) (defaults to: nil)

    use tcp no delay option or not



32
33
34
35
36
# File 'lib/ros/subscriber.rb', line 32

def initialize(caller_id, topic_name, topic_type, callback=nil, tcp_no_delay=nil)
  super(caller_id, topic_name, topic_type)
  @callback = callback
  @tcp_no_delay = tcp_no_delay
end

Instance Attribute Details

- (Proc) callback (readonly)

Returns callback of this subscription

Returns:

  • (Proc)

    callback of this subscription



42
43
44
# File 'lib/ros/subscriber.rb', line 42

def callback
  @callback
end

- (Boolean) tcp_no_delay (readonly)

Returns use tcp no delay option or not

Returns:

  • (Boolean)

    use tcp no delay option or not



39
40
41
# File 'lib/ros/subscriber.rb', line 39

def tcp_no_delay
  @tcp_no_delay
end

Instance Method Details

- (TCPROS::Client) add_connection(uri)

request topic to master and start connection with publisher.

Parameters:

  • uri (String)

    uri to connect

Returns:



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ros/subscriber.rb', line 75

def add_connection(uri) #:nodoc:
  publisher = SlaveProxy.new(@caller_id, uri)
  begin
    protocol, host, port = publisher.request_topic(@topic_name, [["TCPROS"]])
    if protocol == "TCPROS"
      connection = TCPROS::Client.new(host, port, @caller_id, @topic_name, @topic_type, uri, @tcp_no_delay)
      connection.start
    else
      puts "not support protocol: #{protocol}"
      raise "not support protocol: #{protocol}"
    end
    connection.id = "#{@topic_name}_in_#{@connection_id_number}"
    @connection_id_number += 1
    @connections.push(connection)
    return connection
  rescue
#	puts "request to #{uri} fail"
    return false
  end
end

- (Object) drop_connection(uri)

remove connection

Parameters:

  • uri (String)

    uri to connect



99
100
101
# File 'lib/ros/subscriber.rb', line 99

def drop_connection(uri) #:nodoc:
  @connections.delete_if {|c| c.target_uri == uri}
end

- (Array) get_connected_uri

Get the uri list of connected publishers.

Returns:

  • (Array)

    URI list.



133
134
135
# File 'lib/ros/subscriber.rb', line 133

def get_connected_uri
  @connections.map {|x| x.target_uri}
end

- (Array) get_connection_data

data of connection. for slave API

Returns:

  • (Array)

    connection data



106
107
108
109
110
# File 'lib/ros/subscriber.rb', line 106

def get_connection_data
  @connections.map do |connection|
    [connection.id, connection.byte_received, 1]
  end
end

- (Array) get_connection_info

connection information fro slave API

Returns:

  • (Array)

    connection info



115
116
117
118
119
120
121
# File 'lib/ros/subscriber.rb', line 115

def get_connection_info
  info = []
  @connections.each do |connection|
    info.push([connection.id, connection.target_uri, 'i', connection.protocol, @topic_name])
  end
  info
end

- (Integer) get_number_of_publishers

get number of publishers to this subscriber

Returns:

  • (Integer)

    number of publishers



46
47
48
# File 'lib/ros/subscriber.rb', line 46

def get_number_of_publishers
  @connections.length
end

- (Bool) has_connection_with?(uri)

Check if it has connection to the uri

Returns:

  • (Bool)

    true: it has connection. false: not connected yet.



126
127
128
# File 'lib/ros/subscriber.rb', line 126

def has_connection_with?(uri)
  get_connected_uri.include?(uri)
end

- (Bool) process_queue Also known as: spin_once

execute callback for all queued messages. This is called by Node#spin_once. It checks all queues of connections and callback for all messages.

Returns:

  • (Bool)

    some message has come or not.



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ros/subscriber.rb', line 55

def process_queue #:nodoc:
  messages_come = false
  @connections.each do |connection|
    while not connection.msg_queue.empty?
      msg = connection.msg_queue.pop
      messages_come = true
      if @callback
        @callback.call(msg)
      end
    end
  end
  messages_come
end

- (Object) shutdown

user interface of shutdown this subscriber



140
141
142
# File 'lib/ros/subscriber.rb', line 140

def shutdown
  @manager.shutdown_subscriber(self)
end