Class: Rclrb::Publisher

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

Overview

Represent a ROS publisher

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(publisher_handle, node_handle, msg_type, topic, qos_profile, callback_group, event_callbacks) ⇒ Publisher

Construct a new publisher, this should not be called directly, instead use Node.create_publisher.



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/rclrb/publisher.rb', line 7

def initialize(publisher_handle, node_handle, msg_type, topic, qos_profile, callback_group, event_callbacks)
  @publisher_handle = publisher_handle
  @node_handle = node_handle
  @msg_type = msg_type
  @topic = topic
  @qos_profile = qos_profile

  publisher_ops = CApi.rcl_publisher_get_default_options()
  publisher_ops[:qos] = QoSProfile.get_profile(qos_profile).ros_profile
  CApi.handle_result(CApi.rcl_publisher_init(@publisher_handle, node_handle, msg_type.type_support(), @topic, publisher_ops))
end

Instance Attribute Details

#msg_typeObject (readonly)

Returns the value of attribute msg_type.



5
6
7
# File 'lib/rclrb/publisher.rb', line 5

def msg_type
  @msg_type
end

Instance Method Details

#pub(msg) ⇒ Object

Publish a message. If msg has the wrong type, an exception is triggered.



25
26
27
28
29
# File 'lib/rclrb/publisher.rb', line 25

def pub(msg)
  raise InvalidMessageTypeError.new msg.class, @msg_type unless msg.kind_of? @msg_type
  ros_msg = @msg_type.get_ros_message msg
  CApi.handle_result(CApi.rcl_publish(@publisher_handle, ros_msg, nil), lambda { @msg_type.destroy_ros_message ros_msg})
end

#subscription_countObject

Return the number of subscription to that topic.



33
34
35
36
37
# File 'lib/rclrb/publisher.rb', line 33

def subscription_count()
  count = CApi::SizeTPtr.new
  CApi.handle_result CApi.rcl_publisher_get_subscription_count(@publisher_handle, count)
  return count[:value]
end