Class: ROS::Rate

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

Overview

sleep in a Hz timing.

Examples:

r = ROS::Rate.new(10)
r.sleep

Instance Method Summary (collapse)

Constructor Details

- (Rate) initialize(hz)

Returns a new instance of Rate

Parameters:

  • hz (Float)

    Hz



22
23
24
25
# File 'lib/ros/rate.rb', line 22

def initialize(hz)
  @sleep_duration = ::ROS::Duration.new(1.0 / hz)
  @last_time = ::ROS::Time.now
end

Instance Method Details

- (Object) sleep

sleep for preset rate [Hz]



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/ros/rate.rb', line 30

def sleep
  current_time = ::ROS::Time.now
  if @last_time > current_time
    @last_time = current_time
  end
  elapsed = current_time - @last_time
  time_to_sleep = @sleep_duration - elapsed
  if time_to_sleep.to_sec > 0.0
    time_to_sleep.sleep
  end
  @last_time = @last_time + @sleep_duration

  # detect time jumping forwards, as well as loops that are
  # inherently too slow
  if (current_time - @last_time).to_sec > @sleep_duration.to_sec * 2
    @last_time = current_time
  end
  nil
end