Class: ROS::Subscriber
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
-
#callback ⇒ Proc
readonly
Callback of this subscription.
-
#tcp_no_delay ⇒ Boolean
readonly
Use tcp no delay option or not.
Attributes inherited from Topic
#caller_id, #topic_name, #topic_type
Instance Method Summary collapse
-
#add_connection(uri) ⇒ TCPROS::Client
request topic to master and start connection with publisher.
- #get_connected_uri ⇒ Object
-
#get_connection_data ⇒ Array
data of connection.
-
#get_connection_info ⇒ Array
connection information fro slave API.
- #has_connection_with?(uri) ⇒ Boolean
-
#initialize(caller_id, topic_name, topic_type, callback = nil, tcp_no_delay = nil) ⇒ Subscriber
constructor
A new instance of Subscriber.
-
#process_queue ⇒ Object
execute callback for all queued messages.
-
#shutdown ⇒ Object
user interface of shutdown this subscriber.
Methods inherited from Topic
Constructor Details
#initialize(caller_id, topic_name, topic_type, callback = nil, tcp_no_delay = nil) ⇒ Subscriber
Returns a new instance of Subscriber.
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
#callback ⇒ Proc (readonly)
Returns callback of this subscription.
42 43 44 |
# File 'lib/ros/subscriber.rb', line 42 def callback @callback end |
#tcp_no_delay ⇒ Boolean (readonly)
Returns 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.
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_uri ⇒ Object
108 109 110 |
# File 'lib/ros/subscriber.rb', line 108 def get_connected_uri @connections.map {|x| x.target_uri} end |
#get_connection_data ⇒ Array
data of connection. for slave API
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_info ⇒ Array
connection information fro slave API
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
104 105 106 |
# File 'lib/ros/subscriber.rb', line 104 def has_connection_with?(uri) get_connected_uri.include?(uri) end |
#process_queue ⇒ Object
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 |
#shutdown ⇒ Object
user interface of shutdown this subscriber
115 116 117 |
# File 'lib/ros/subscriber.rb', line 115 def shutdown @manager.shutdown_subscriber(self) end |