@@ -8,6 +8,15 @@ export Time, Duration, Rate, to_sec, to_nsec, get_rostime, rossleep
88# Time type definitions
99@compat abstract type TVal end
1010
11+ """
12+ Time(secs, nsecs), Time(), Time(t::Real)
13+
14+ Object representing an absolute time from a fixed past reference point at nanosecond precision.
15+
16+ Basic arithmetic can be performed on combinations of `Time` and `Duration` objects that make sense.
17+ For example, if `t::Time` and `d::Duration`, `t+d` will be a `Time`, `d+d` a Duration`, `t-d` a
18+ `Time`, `d-d` a `Duration`, and `t-t` a `Duration`.
19+ """
1120immutable Time <: TVal
1221 secs:: Int32
1322 nsecs:: Int32
1928Time () = Time (0 ,0 )
2029Time (t:: Real ) = Time (t,0 )
2130
31+ """
32+ Duration(secs, nsecs), Duration(), Duration(t::Real)
33+
34+ Object representing a relative period of time at nanosecond precision.
35+
36+ Basic arithmetic can be performed on combinations of `Time` and `Duration` objects that make sense.
37+ For example, if `t::Time` and `d::Duration`, `t+d` will be a `Time`, `d+d` a `Duration`, `t-d` a
38+ `Time`, `d-d` a `Duration`, and `t-t` a `Duration`.
39+ """
2240immutable Duration <: TVal
2341 secs:: Int32
2442 nsecs:: Int32
@@ -45,7 +63,6 @@ function _canonical_time(secs, nsecs)
4563 (secs32 + addsecs, crnsecs)
4664end
4765
48- # Temporal arithmetic
4966+ (t1:: Time , t2:: Duration ) = Time ( t1. secs+ t2. secs, t1. nsecs+ t2. nsecs)
5067+ (t1:: Duration , t2:: Time ) = Time ( t1. secs+ t2. secs, t1. nsecs+ t2. nsecs)
5168+ (t1:: Duration , t2:: Duration ) = Duration (t1. secs+ t2. secs, t1. nsecs+ t2. nsecs)
@@ -62,14 +79,44 @@ convert(::Type{PyObject}, t::Time) = __rospy__[:Time]( t.secs,t.nsecs)
6279convert (:: Type{PyObject} , t:: Duration ) = __rospy__[:Duration ](t. secs,t. nsecs)
6380
6481# Real number conversions
82+ """
83+ to_sec(t)
84+
85+ Return the value of a ROS time object in absolute seconds (with nanosecond precision)
86+ """
6587to_sec {T<:TVal} (t:: T ) = t. secs + 1e-9 * t. nsecs
88+
89+ """
90+ to_nsec(t)
91+
92+ Return the value of a ROS time object in nanoseconds as an integer.
93+ """
6694to_nsec {T<:TVal} (t:: T ) = 1_000_000_000 * t. secs + t. nsecs
6795convert {T<:TVal} (:: Type{Float64} , t:: T ) = to_sec (t)
6896
6997# Comparisons
70- == {T<: TVal }(t1:: T , t2:: T ) = (t1. secs == t2. secs) && (t1. nsecs == t2. nsecs)
98+ == {T<: TVal }(t1:: T , t2:: T ) = (t1. secs == t2. secs) && (t1. nsecs == t2. nsecs)
7199isless {T<:TVal} (t1:: T , t2:: T ) = to_nsec (t1) < to_nsec (t2)
72100
101+ """
102+ Rate(hz::Real), Rate(d::Duration)
103+
104+ Used to allow a loop to run at a fixed rate. Construct with a frequency or `Duration` and use with
105+ `rossleep` or `sleep`. The rate object will record execution time of other work in the loop and
106+ modify the sleep time to compensate, keeping the loop rate as consistent as possible.
107+ """
108+ type Rate
109+ duration:: Duration
110+ last_time:: Time
111+ end
112+ Rate (d:: Duration ) = Rate (d, get_rostime ())
113+ Rate (hz:: Real ) = Rate (Duration (1.0 / hz), get_rostime ())
114+
115+ """
116+ get_rostime()
117+
118+ Return the current ROS time as a `Time` object.
119+ """
73120function get_rostime ()
74121 t = try
75122 __rospy__[:get_rostime ]()
@@ -78,8 +125,20 @@ function get_rostime()
78125 end
79126 convert (Time, t)
80127end
128+
129+ """
130+ RobotOS.now()
131+
132+ Return the current ROS time as a `Time` object.
133+ """
81134now () = get_rostime ()
82135
136+ """
137+ rossleep(t)
138+
139+ Sleep and process callbacks for a number of seconds implied by the type and value of `t`, which may
140+ be a real-value, a `Duration` object, or a `Rate` object.
141+ """
83142function rossleep (td:: Duration )
84143 # Busy sleep loop needed to allow both julia and python async activity
85144 tnsecs = to_nsec (td)
@@ -91,15 +150,6 @@ function rossleep(td::Duration)
91150end
92151rossleep (t:: Real ) = rossleep (Duration (t))
93152
94- sleep (t:: Duration ) = rossleep (t)
95-
96- type Rate
97- duration:: Duration
98- last_time:: Time
99- end
100- Rate (d:: Duration ) = Rate (d, get_rostime ())
101- Rate (hz:: Real ) = Rate (Duration (1.0 / hz), get_rostime ())
102-
103153function rossleep (r:: Rate )
104154 ctime = get_rostime ()
105155 if r. last_time > ctime
@@ -113,4 +163,11 @@ function rossleep(r::Rate)
113163 r. last_time = ctime
114164 end
115165end
166+
167+ """
168+ sleep(t::Duration), sleep(t::Rate)
169+
170+ Call `rossleep` with a `Duration` or `Rate` object. Use `rossleep` to specify sleep time directly.
171+ """
172+ sleep (t:: Duration ) = rossleep (t)
116173sleep (t:: Rate ) = rossleep (t)
0 commit comments