Class: ROS::Publisher

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

Overview

Publisher interface of ROS. A publisher contains multi connection with subscribers. TCPROS protocol implementation is in ROS::TCPROS::Server. Here is sample code. You can shutdown publisher by using this instance (ROS::Publisher#shutdown)

node = ROS::Node.new('/rosruby/sample_publisher')
publisher = node.advertise('/chatter', Std_msgs::String)
sleep(1)
msg = Std_msgs::String.new
i = 0
while node.ok?
  msg.data = "Hello, rosruby!: #{i}"
  publisher.publish(msg)
  i += 1
end

Instance Attribute Summary

Attributes inherited from Topic

#caller_id, #topic_name, #topic_type

Instance Method Summary (collapse)

Methods inherited from Topic

#close, #set_manager

Constructor Details

- (Publisher) initialize(caller_id, topic_name, topic_type, is_latched, host)

Returns a new instance of Publisher

Parameters:

  • caller_id (String)

    caller_id of this node

  • topic_name (String)

    name of topic to publish (String)

  • topic_type (Class)

    class of topic

  • is_latched (Boolean)

    latched topic?

  • host (String)

    host name of this node



34
35
36
37
38
39
# File 'lib/ros/publisher.rb', line 34

def initialize(caller_id, topic_name, topic_type, is_latched, host)
  super(caller_id, topic_name, topic_type)
  @host = host
  @is_latched = is_latched
  @seq = 0
end

Instance Method Details

- (TCPROS::Server) add_connection(caller_id)

add tcpros connection as server

Parameters:

  • caller_id (String)

    caller_id of subscriber

Returns:



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ros/publisher.rb', line 61

def add_connection(caller_id) #:nodoc:
  connection = TCPROS::Server.new(@caller_id, @topic_name, @topic_type,
                                  :host=>@host,
                                  :latched=>@is_latched,
                                  :last_published_msg=>@last_published_msg)
  connection.start
  connection.id = "#{@topic_name}_out_#{@connection_id_number}"
  @connection_id_number += 1
  @connections.push(connection)
  return connection
end

- (Array) get_connection_data

Returns connection data for slave api

Returns:

  • (Array)

    connection data for slave api



80
81
82
83
84
# File 'lib/ros/publisher.rb', line 80

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

- (array) get_connection_info

Returns connection info for slave api

Returns:

  • (array)

    connection info for slave api



87
88
89
90
91
92
93
# File 'lib/ros/publisher.rb', line 87

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

- (Integer) get_number_of_subscribers

get number of subscribers to this publisher

Returns:

  • (Integer)

    number of subscribers



75
76
77
# File 'lib/ros/publisher.rb', line 75

def get_number_of_subscribers
  @connections.length
end

- (Publisher) publish(message)

publish msg object

Parameters:

  • message (Class)

    instance of topic_type class

Returns:



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/ros/publisher.rb', line 45

def publish(message)
  @seq += 1
  @last_published_msg = message
  if message.has_header?
    message.header.seq = @seq
  end
  @connections.each do |connection|
    connection.msg_queue.push(message)
  end
  self
end

- (nil) shutdown

user interface of shutdown this publisher

Returns:

  • (nil)

    nil



98
99
100
# File 'lib/ros/publisher.rb', line 98

def shutdown
  @manager.shutdown_publisher(self)
end