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

#initialize(hz) ⇒ Rate

Returns a new instance of Rate.

Parameters:

  • hz (Float)

    Hz



20
21
22
23
# File 'lib/ros/rate.rb', line 20

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

Instance Method Details

#sleepObject

sleep for preset rate [Hz]



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/ros/rate.rb', line 28

def sleep
  current_time = ::Time.now
  elapsed = current_time - @last_time
  Kernel.sleep(@sleep_duration - elapsed)
  @last_time = @last_time + @sleep_duration

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