Class: Rclrb::WaitSet

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

Overview

This class is use to Wait for events.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeWaitSet

Returns a new instance of WaitSet.



5
6
7
8
9
10
# File 'lib/rclrb/wait_set.rb', line 5

def initialize
  @subscriptions = []
  @services = []
  @clients = []
  @timers = []
end

Class Method Details

.wait_for(what, timeout = -1,, &block) ⇒ Object

Wait for an event on ẃhat, every timeout evaluate block and if the value returned is true, then exit the function otherwise continue waiting.



14
15
16
17
18
19
20
# File 'lib/rclrb/wait_set.rb', line 14

def WaitSet.wait_for what, timeout = -1, &block
  ws = WaitSet.new
  ws.add what
  until block.call
    ws.wait timeout
  end
end

Instance Method Details

#add(what) ⇒ Object

Add an item (Subscription, Service…) to the wait set



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/rclrb/wait_set.rb', line 23

def add(what)
  if what.kind_of? Subscription
    @subscriptions.append what
  elsif what.kind_of? Service
    @services.append what
  elsif what.kind_of? Client
    @clients.append what
  elsif what.kind_of? Timer
    @timers.append what
  end
end

#wait(timeout = -1)) ⇒ Object

Wait until timeout or an event occured in one of the item added with add. If timeout is equalt to -1, this function will block until an event occured or indefinitely.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rclrb/wait_set.rb', line 37

def wait(timeout = -1)
  ws = CApi.rcl_get_zero_initialized_wait_set
  CApi.handle_result CApi.rcl_wait_set_init(ws, @subscriptions.size, 1, @timers.size, @clients.size, @services.size, 0, Rclrb.rcl_context, Rclrb.rcl_allocator)
  begin
    @subscriptions.each() { |x| CApi.handle_result CApi.rcl_wait_set_add_subscription(ws, x.subscription_handle, nil)}
    CApi.handle_result CApi.rcl_wait_set_add_guard_condition(ws, Rclrb.rcl_signal_guard_condition.guard_condition_handle, nil)
    @services.each() { |x| CApi.handle_result CApi.rcl_wait_set_add_service(ws, x.service_handle, nil)}
    @clients.each() { |x| CApi.handle_result CApi.rcl_wait_set_add_client(ws, x.client_handle, nil)}
    @timers.each() { |x| CApi.handle_result CApi.rcl_wait_set_add_timer(ws, x.timer_handle, nil)}
    # Wait has to run in a seperate thread to allow for signals to work
    status = nil
    t = Thread.new do
      status = CApi.rcl_wait(ws, timeout)
    end
    t.join
    if status != CApi::RCL_RET_OK and status != CApi::RCL_RET_TIMEOUT
      CApi.handle_result status
    end
  rescue RclError
    CApi.handle_result CApi.rcl_wait_set_fini(ws)
    raise
  end
  CApi.handle_result CApi.rcl_wait_set_fini(ws)
end