@@ -8,6 +8,15 @@ export Time, Duration, Rate, to_sec, to_nsec, get_rostime, rossleep
8
8
# Time type definitions
9
9
@compat abstract type TVal end
10
10
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
+ """
11
20
immutable Time <: TVal
12
21
secs:: Int32
13
22
nsecs:: Int32
19
28
Time () = Time (0 ,0 )
20
29
Time (t:: Real ) = Time (t,0 )
21
30
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
+ """
22
40
immutable Duration <: TVal
23
41
secs:: Int32
24
42
nsecs:: Int32
@@ -45,7 +63,6 @@ function _canonical_time(secs, nsecs)
45
63
(secs32 + addsecs, crnsecs)
46
64
end
47
65
48
- # Temporal arithmetic
49
66
+ (t1:: Time , t2:: Duration ) = Time ( t1. secs+ t2. secs, t1. nsecs+ t2. nsecs)
50
67
+ (t1:: Duration , t2:: Time ) = Time ( t1. secs+ t2. secs, t1. nsecs+ t2. nsecs)
51
68
+ (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)
62
79
convert (:: Type{PyObject} , t:: Duration ) = __rospy__[:Duration ](t. secs,t. nsecs)
63
80
64
81
# 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
+ """
65
87
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
+ """
66
94
to_nsec {T<:TVal} (t:: T ) = 1_000_000_000 * t. secs + t. nsecs
67
95
convert {T<:TVal} (:: Type{Float64} , t:: T ) = to_sec (t)
68
96
69
97
# Comparisons
70
98
== {T<: TVal }(t1:: T , t2:: T ) = (t1. secs == t2. secs) && (t1. nsecs == t2. nsecs)
71
99
isless {T<:TVal} (t1:: T , t2:: T ) = to_nsec (t1) < to_nsec (t2)
72
100
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
+ """
73
120
function get_rostime ()
74
121
t = try
75
122
__rospy__[:get_rostime ]()
@@ -78,8 +125,19 @@ function get_rostime()
78
125
end
79
126
convert (Time, t)
80
127
end
128
+
129
+ """
130
+ RobotOS.now()
131
+
132
+ Return the current ROS time as a `Time` object.
133
+ """
81
134
now () = get_rostime ()
82
135
136
+ """
137
+ rossleep(t)
138
+
139
+ Call the ROS sleep function with a number of seconds, a `Duration` or a `Rate` object.
140
+ """
83
141
function rossleep (td:: Duration )
84
142
# Busy sleep loop needed to allow both julia and python async activity
85
143
tnsecs = to_nsec (td)
@@ -91,15 +149,6 @@ function rossleep(td::Duration)
91
149
end
92
150
rossleep (t:: Real ) = rossleep (Duration (t))
93
151
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
-
103
152
function rossleep (r:: Rate )
104
153
ctime = get_rostime ()
105
154
if r. last_time > ctime
@@ -113,4 +162,12 @@ function rossleep(r::Rate)
113
162
r. last_time = ctime
114
163
end
115
164
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)
116
173
sleep (t:: Rate ) = rossleep (t)
0 commit comments