Class: ROS::Time

Inherits:
TimeValue show all
Defined in:
lib/ros/time.rb

Overview

ROS Time object. This is used as msg object for time

Constant Summary

SIM_TIME_PARAMETER =

parameter name for switching sim/wall time.

'/use_sim_time'
@@use_sim_time =

use simulated time?

false
@@current_sim_time =

current simulated time

nil
@@clock_subscriber =

subscriber for /clock

nil

Instance Attribute Summary

Attributes inherited from TimeValue

#nsecs, #secs

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from TimeValue

#<=>, #canonicalize, #to_nsec, #to_sec

Constructor Details

- (Time) initialize(time) - (Time) initialize

Returns a new instance of Time

Overloads:

  • - (Time) initialize(time)

    Parameters:

    • initialize (::Time)

      with this time



137
138
139
140
141
142
143
144
145
146
# File 'lib/ros/time.rb', line 137

def initialize(time=nil)
  if time
    @secs = time.to_i
    @nsecs = ((time.to_f - @secs) * 1e9.to_i).to_i
  else
    @secs = 0
    @nsecs = 0
  end
  canonicalize
end

Class Method Details

+ (ROS::Time) get_simtime

get the simulated time

Returns:



121
122
123
# File 'lib/ros/time.rb', line 121

def self.get_simtime
  @@current_sim_time
end

+ (ROS::Time) get_walltime

get the system real time

Returns:



114
115
116
# File 'lib/ros/time.rb', line 114

def self.get_walltime
  self.new(::Time::now)
end

+ (Object) initialize_with_sim_or_wall(node)

initialize Time

Parameters:

  • node (Node)

    for subscribe /clock.



88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/ros/time.rb', line 88

def self.initialize_with_sim_or_wall(node)
  @@use_sim_time = node.get_param(SIM_TIME_PARAMETER, false)
  if @@use_sim_time and not @@clock_subscriber
    puts 'initializing simulated clock (/clock)'
    @@clock_subscriber = node.subscribe('/clock',
                                        Rosgraph_msgs::Clock) do |msg|
      @@current_sim_time = msg.clock
    end
    @@sim_thread = Thread.new do
      while node.ok?
        @@clock_subscriber.process_queue
      end
    end
  elsif not @@use_sim_time and @@clock_subscriber
    begin
      @@clock_subscriber.shutdown
    rescue => e
      # even if node is already shutdown, do nothing.
    end
    @@current_sim_time = nil
  end
end

+ (Object) now

initialize with current time



126
127
128
129
130
131
132
# File 'lib/ros/time.rb', line 126

def self.now
  if @@current_sim_time
    @@current_sim_time
  else
    self.new(::Time::now)
  end
end

Instance Method Details

- (Time) +(duration)

add time value

Parameters:

  • duration (Duration)

    duration for adding

Returns:

  • (Time)

    new time



151
152
153
154
155
156
# File 'lib/ros/time.rb', line 151

def +(duration)
  tm = ::ROS::Time.new
  tm.secs = @secs + duration.secs
  tm.nsecs = @nsecs + duration.nsecs
  tm.canonicalize
end

- (Duration) -(other)

subtract time value.

Parameters:

Returns:



161
162
163
164
165
166
# File 'lib/ros/time.rb', line 161

def -(other)
  d = ::ROS::Duration.new
  d.secs = @secs - other.secs
  d.nsecs = @nsecs - other.nsecs
  d.canonicalize
end

- (Time) to_time

returns ruby Time object.

Returns:

  • (Time)

    ruby time object



170
171
172
# File 'lib/ros/time.rb', line 170

def to_time
  ::Time.at(@secs, @nsecs)
end