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)
-
- (Proc) callback
readonly
Callback of this subscription.
-
- (Boolean) tcp_no_delay
readonly
Use tcp no delay option or not.
Attributes inherited from Topic
#caller_id, #topic_name, #topic_type
Instance Method Summary (collapse)
-
- (TCPROS::Client) add_connection(uri)
request topic to master and start connection with publisher.
-
- (Object) drop_connection(uri)
remove connection.
-
- (Array) get_connected_uri
Get the uri list of connected publishers.
-
- (Array) get_connection_data
data of connection.
-
- (Array) get_connection_info
connection information fro slave API.
-
- (Integer) get_number_of_publishers
get number of publishers to this subscriber.
-
- (Bool) has_connection_with?(uri)
Check if it has connection to the uri.
-
- (Subscriber) initialize(caller_id, topic_name, topic_type, callback = nil, tcp_no_delay = nil)
constructor
A new instance of Subscriber.
-
- (Bool) process_queue
(also: #spin_once)
execute callback for all queued messages.
-
- (Object) shutdown
user interface of shutdown this subscriber.
Methods inherited from Topic
Constructor Details
- (Subscriber) initialize(caller_id, topic_name, topic_type, callback = nil, tcp_no_delay = nil)
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
- (Proc) callback (readonly)
Returns 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
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.
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
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.
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
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
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
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
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.
55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/ros/subscriber.rb', line 55 def process_queue #:nodoc: = false @connections.each do |connection| while not connection.msg_queue.empty? msg = connection.msg_queue.pop = true if @callback @callback.call(msg) end end end 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 |