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

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

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

#callbackProc (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

#tcp_no_delayBoolean (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

#add_connection(uri) ⇒ TCPROS::Client

request topic to master and start connection with publisher.

Parameters:

  • uri (String)

    uri to connect

Returns:



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/ros/subscriber.rb', line 63

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 fail"
    return false
  end
end

#get_connected_uriObject



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

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

#get_connection_dataArray

data of connection. for slave API

Returns:

  • (Array)

    connection data



87
88
89
90
91
# File 'lib/ros/subscriber.rb', line 87

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

#get_connection_infoArray

connection information fro slave API

Returns:

  • (Array)

    connection info



96
97
98
99
100
101
102
# File 'lib/ros/subscriber.rb', line 96

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

#has_connection_with?(uri) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/ros/subscriber.rb', line 104

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

#process_queueObject

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



48
49
50
51
52
53
54
55
56
57
# File 'lib/ros/subscriber.rb', line 48

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

#shutdownObject

user interface of shutdown this subscriber



115
116
117
# File 'lib/ros/subscriber.rb', line 115

def shutdown
  @manager.shutdown_subscriber(self)
end