Class: ROS::Rate
- Inherits:
-
Object
- Object
- ROS::Rate
- Defined in:
- lib/ros/rate.rb
Overview
sleep in a Hz timing.
Instance Method Summary collapse
-
#initialize(hz) ⇒ Rate
constructor
A new instance of Rate.
-
#sleep ⇒ Object
sleep for preset rate [Hz].
Constructor Details
#initialize(hz) ⇒ Rate
Returns a new instance of Rate.
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
#sleep ⇒ Object
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 |