Skip to content

Commit c2e25ed

Browse files
committed
Reimplement rospy sleep functions with julia busy-wait loop
1 parent c8e9a60 commit c2e25ed

File tree

2 files changed

+41
-32
lines changed

2 files changed

+41
-32
lines changed

src/time.jl

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
#All time related types and functions
22

3-
import Base: convert, isless, sleep, +, -, ==
3+
import Base: convert, isless, sleep, +, -, *, ==
44
export Time, Duration, Rate, to_sec, to_nsec, get_rostime, rossleep
55

66
#Time type definitions
77
abstract TVal
88

9-
type Time <: TVal
9+
immutable Time <: TVal
1010
secs::Int32
1111
nsecs::Int32
1212
function Time(s::Real,n::Real)
@@ -17,7 +17,7 @@ end
1717
Time() = Time(0,0)
1818
Time(t::Real) = Time(t,0)
1919

20-
type Duration <: TVal
20+
immutable Duration <: TVal
2121
secs::Int32
2222
nsecs::Int32
2323
function Duration(s::Real,n::Real)
@@ -50,6 +50,8 @@ end
5050
-(t1::Time, t2::Duration) = Time( t1.secs-t2.secs, t1.nsecs-t2.nsecs)
5151
-(t1::Duration, t2::Duration) = Duration(t1.secs-t2.secs, t1.nsecs-t2.nsecs)
5252
-(t1::Time, t2::Time) = Duration(t1.secs-t2.secs, t1.nsecs-t2.nsecs)
53+
*(td::Duration, tf::Real) = Duration(tf*td.secs , tf*td.nsecs)
54+
*(tf::Real, td::Duration) = Duration(tf*td.secs , tf*td.nsecs)
5355

5456
#PyObject conversions
5557
convert(::Type{Time}, o::PyObject) = Time( o[:secs],o[:nsecs])
@@ -66,28 +68,6 @@ convert{T<:TVal}(::Type{Float64}, t::T) = to_sec(t)
6668
=={T<:TVal}(t1::T, t2::T) = (t1.secs == t2.secs) && (t1.nsecs == t2.nsecs)
6769
isless{T<:TVal}(t1::T, t2::T) = to_nsec(t1) < to_nsec(t2)
6870

69-
#----------------------------
70-
#Extra time-related utilities
71-
#----------------------------
72-
73-
type Rate
74-
o::PyObject
75-
end
76-
Rate(hz::Real) = Rate(__rospy__[:Rate](hz))
77-
Rate(d::Duration) = Rate(1.0/to_sec(d))
78-
79-
type Timer
80-
t::PyObject
81-
end
82-
83-
type TimerEvent
84-
last_expected::Time
85-
last_real::Time
86-
current_expected::Time
87-
current_real::Time
88-
last_duration::Duration
89-
end
90-
9171
function get_rostime()
9272
t = try
9373
__rospy__[:get_rostime]()
@@ -98,8 +78,36 @@ function get_rostime()
9878
end
9979
now() = get_rostime()
10080

101-
rossleep(t::Real) = __rospy__[:sleep](t)
102-
rossleep(t::Duration) = __rospy__[:sleep](convert(PyObject, t))
103-
rossleep(r::Rate) = pycall(r.o["sleep"], PyAny)
81+
function rossleep(td::Duration)
82+
tnsecs = to_nsec(td)
83+
t0 = time_ns()
84+
while time_ns()-t0 < tnsecs
85+
yield()
86+
__rospy__[:sleep](0.001)
87+
end
88+
end
89+
rossleep(t::Real) = rossleep(Duration(t))
90+
10491
sleep(t::Duration) = rossleep(t)
92+
93+
type Rate
94+
duration::Duration
95+
last_time::Time
96+
end
97+
Rate(d::Duration) = Rate(d, get_rostime())
98+
Rate(hz::Real) = Rate(Duration(1.0/hz), get_rostime())
99+
100+
function rossleep(r::Rate)
101+
ctime = get_rostime()
102+
if r.last_time > ctime
103+
r.last_time = ctime
104+
end
105+
elapsed = ctime - r.last_time
106+
rossleep(r.duration - elapsed)
107+
r.last_time += r.duration
108+
109+
if ctime - r.last_time > r.duration*2
110+
r.last_time = ctime
111+
end
112+
end
105113
sleep(t::Rate) = rossleep(t)

test/time.jl

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,16 @@ d3 = Duration(0, 1)
2626
@test d1 + d2 + d3 == Duration(t3.secs, t3.nsecs)
2727
@test d2 - d1 - d3 == Duration(0, 500_000_000)
2828

29-
tt = Time()
30-
tt.secs = 2
29+
@test d2*2 == Duration(3,0)
30+
@test 3.0*d2 == Duration(4,500_000_000)
31+
32+
tt = Time(2,0)
3133
@test tt == Time(2.0)
3234
@test convert(Float64,tt) == 2.0
3335
@test to_sec(tt) == 2.0
3436
@test to_nsec(tt) == 2_000_000_000
3537

36-
dt = Duration()
37-
dt.secs = 3
38+
dt = Duration(3,0)
3839
@test dt == Duration(3.0)
3940
@test convert(Float64,dt) == 3.0
4041
@test to_sec(dt) == 3.0

0 commit comments

Comments
 (0)