Class: Rclrb::Subscription

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

Overview

Represent a ROS subscription to a topic

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(subscription_handle, node_handle, msg_type, topic, callback, qos_profile, callback_group, event_callbacks, raw) ⇒ Subscription

Construct a new subscription, this should not be called directly, instead use Node.create_subscription.



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/rclrb/subscription.rb', line 7

def initialize(subscription_handle, node_handle, msg_type, topic, callback, qos_profile, callback_group, event_callbacks, raw)
  @subscription_handle = subscription_handle
  @node_handle = node_handle
  @msg_type = msg_type
  @topic = topic
  @callback = callback
  @qos_profile = qos_profile
  @raw = raw
  callback_group.add self
  
  subscription_ops = CApi.rcl_subscription_get_default_options()
  subscription_ops[:qos] = QoSProfile.get_profile(qos_profile).ros_profile
  CApi.handle_result CApi.rcl_subscription_init(@subscription_handle, node_handle, msg_type.type_support(), @topic, subscription_ops)
  Rclrb.rcl_signal_guard_condition.trigger
end

Instance Attribute Details

#msg_typeObject (readonly)

Returns the value of attribute msg_type.



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

def msg_type
  @msg_type
end

#subscription_handleObject (readonly)

Returns the value of attribute subscription_handle.



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

def subscription_handle
  @subscription_handle
end

Instance Method Details

#raw?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/rclrb/subscription.rb', line 25

def raw?
  return @raw
end

#spin(wait_set = nil) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/rclrb/subscription.rb', line 29

def spin(wait_set = nil)
  wait_set.add self if wait_set
  message_info = CApi::RmwMessageInfoT.new
  data = @msg_type::FFIType.new
  status = CApi.rcl_take @subscription_handle, data, message_info, nil

  if status == CApi::RCL_RET_OK
    msg = @msg_type.parse_ros_message data
    @msg_type.destroy_ros_message data
    @callback.call(msg)
  elsif status == CApi::RCL_RET_SUBSCRIPTION_TAKE_FAILED
    # no message received
  else
    CApi.handle_result status
  end
end