Skip to content

Commit b84ae66

Browse files
committed
Add docstrings to time types/functions
1 parent 0b344d1 commit b84ae66

File tree

1 file changed

+67
-10
lines changed

1 file changed

+67
-10
lines changed

src/time.jl

Lines changed: 67 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
"""
1120
immutable Time <: TVal
1221
secs::Int32
1322
nsecs::Int32
@@ -19,6 +28,15 @@ end
1928
Time() = Time(0,0)
2029
Time(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+
"""
2240
immutable Duration <: TVal
2341
secs::Int32
2442
nsecs::Int32
@@ -45,7 +63,6 @@ function _canonical_time(secs, nsecs)
4563
(secs32 + addsecs, crnsecs)
4664
end
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)
6279
convert(::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+
"""
6587
to_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+
"""
6694
to_nsec{T<:TVal}(t::T) = 1_000_000_000*t.secs + t.nsecs
6795
convert{T<:TVal}(::Type{Float64}, t::T) = to_sec(t)
6896

6997
#Comparisons
7098
=={T<:TVal}(t1::T, t2::T) = (t1.secs == t2.secs) && (t1.nsecs == t2.nsecs)
7199
isless{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 and keep 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+
"""
73120
function get_rostime()
74121
t = try
75122
__rospy__[:get_rostime]()
@@ -78,8 +125,19 @@ function get_rostime()
78125
end
79126
convert(Time, t)
80127
end
128+
129+
"""
130+
RobotOS.now()
131+
132+
Return the current ROS time as a `Time` object.
133+
"""
81134
now() = get_rostime()
82135

136+
"""
137+
rossleep(t)
138+
139+
Call the ROS sleep function with a number of seconds, a `Duration` or a `Rate` object.
140+
"""
83141
function rossleep(td::Duration)
84142
#Busy sleep loop needed to allow both julia and python async activity
85143
tnsecs = to_nsec(td)
@@ -91,15 +149,6 @@ function rossleep(td::Duration)
91149
end
92150
rossleep(t::Real) = rossleep(Duration(t))
93151

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-
103152
function rossleep(r::Rate)
104153
ctime = get_rostime()
105154
if r.last_time > ctime
@@ -113,4 +162,12 @@ function rossleep(r::Rate)
113162
r.last_time = ctime
114163
end
115164
end
165+
166+
"""
167+
sleep(t::Duration), sleep(t::Rate)
168+
169+
Call the ROS sleep function with a `Duration` or `Rate` object. Use `rossleep` to specify sleep time
170+
directly.
171+
"""
172+
sleep(t::Duration) = rossleep(t)
116173
sleep(t::Rate) = rossleep(t)

0 commit comments

Comments
 (0)