Skip to content

Commit

Permalink
fixed timespan filtering of messages
Browse files Browse the repository at this point in the history
  • Loading branch information
damiendr committed Sep 11, 2018
1 parent 22d1b4c commit 208a171
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ You can also read only some of the messages, which is faster than reading the wh
0.317334 seconds (743.59 k allocations: 231.204 MiB)
# combine a specific topic and time span:
>>> @time bag["/davis/left/image_raw", ROSTime("2017-09-05T20:59:40"):ROSTime("2017-09-05T20:59:50")]
0.100355 seconds (323.90 k allocations: 51.066 MiB)
0.116424 seconds (323.86 k allocations: 50.807 MiB)
```
## Message Types
Expand Down
1 change: 1 addition & 0 deletions src/bag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ select(filter::ConnectionFilter, _) = true

select(filter::ROSTimespan, infos::ChunkInfo) =
infos.start_time <= filter.end_time && infos.end_time >= filter.start_time
select(filter::ROSTimespan, msg::MessageData) = msg.time in filter
select(filter::ROSTimespan, _) = true

make_filter(bag, ts::ROSTimespan) = ts
Expand Down
22 changes: 13 additions & 9 deletions src/ros/builtin_msgs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@ struct ROSTime <: Readable
nsecs::UInt32
end
ROSTime(d::DateTime) = convert(ROSTime, d)
ROSTime(s::String) = begin
ROSTime(s::String) = convert(ROSTime, s)

Base.convert(::Type{UInt64}, t::ROSTime) = UInt64(t.secs) << 32 + t.nsecs
Base.convert(::Type{DateTime}, t::ROSTime) = unix2datetime(t.secs) + Nanosecond(t.nsecs)
# Note: converting ROSTime to DateTime will truncate to millisecond precision

function Base.convert(::Type{ROSTime}, d::DateTime)
ms = round(Int64, datetime2unix(d) * 1000)
ROSTime(ms ÷ 1000, (ms % 1000) * 1000000)
end

function Base.convert(::Type{ROSTime}, s::String)
# We could just call ROSTime(DateTime(s)), but
# let's be extra nice and parse nanoseconds.
point = findlast(".", s)
Expand All @@ -20,13 +31,6 @@ ROSTime(s::String) = begin
end
ROSTime(trunc(UInt32, secs), trunc(UInt32, nsecs))
end
Base.convert(::Type{UInt64}, t::ROSTime) = UInt64(t.secs) << 32 + t.nsecs
Base.convert(::Type{DateTime}, t::ROSTime) = unix2datetime(t.secs) + Nanosecond(t.nsecs)
# Note: converting ROSTime to DateTime will truncate to millisecond precision
Base.convert(::Type{ROSTime}, d::DateTime) = begin
ms = round(Int64, datetime2unix(d) * 1000)
ROSTime(ms ÷ 1000, (ms % 1000) * 1000000)
end

Base.isequal(t1::ROSTime, t2::ROSTime) = convert(UInt64, t1) == convert(UInt64, t2)
Base.isless(t1::ROSTime, t2::ROSTime) = convert(UInt64, t1) < convert(UInt64, t2)
Expand Down Expand Up @@ -58,7 +62,7 @@ struct ROSTimespan
end_time::ROSTime
end
(::Colon)(t1::ROSTime, t2::ROSTime) = ROSTimespan(t1, t2)
Base.in(t, span::ROSTimespan) = t.start_time <= ROSTime(t) <= t.end_time
Base.in(t, span::ROSTimespan) = span.start_time <= convert(ROSTime, t) <= span.end_time


struct Header <: Readable
Expand Down
3 changes: 3 additions & 0 deletions test/testbags.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ bag = load("data/convergent_gen1.bag", TestTypes)
msgs = bag["convergent"]
@assert msgs isa Vector{MessageData{TestTypes.test_rosbag.Convergent}}

# TODO:
# - test message filters
# - test for message content

0 comments on commit 208a171

Please sign in to comment.