Skip to content

Commit 7594782

Browse files
authored
Better nanosecond timestamp support (#1200)
1 parent 2ad66fc commit 7594782

File tree

9 files changed

+34
-34
lines changed

9 files changed

+34
-34
lines changed

src/Common.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,3 +179,15 @@ function solveGraph! end
179179
Standard parametric graph solution (Experimental).
180180
"""
181181
function solveGraphParametric! end
182+
183+
# delta timestamps
184+
calcDeltatime(from::Nanosecond, to::Nanosecond) = Dates.value(to - from) / 10^9
185+
function calcDeltatime(from::TimeDateZone, to::TimeDateZone)
186+
return Dates.value(convert(Nanosecond, to - from)) / 10^9
187+
end
188+
calcDeltatime(from_node, to_node) = calcDeltatime(from_node.timestamp, to_node.timestamp)
189+
190+
function tdz_now(zone = tz"UTC") #TODO or default to slower localzone()?
191+
t = time()
192+
return TimeDateZone(TimeDate(1970) + Nanosecond(t * 10^9), zone)
193+
end

src/DataBlobs/entities/BlobEntry.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ StructUtils.@kwarg struct Blobentry
4444
""" Storage for a couple of bytes directly in the graph. Use with caution and keep it small and simple."""
4545
metadata::JSONText = JSONText("{}")
4646
""" When the Blob itself was first created. Serialized as an ISO 8601 string."""
47-
timestamp::TimeDateZone = TimeDateZone(now(localzone()))
47+
timestamp::TimeDateZone = tdz_now()
4848
""" Type version of this Blobentry."""
4949
version::VersionNumber = DFG.version(Blobentry)
5050
end

src/entities/DFGFactor.jl

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ StructUtils.@kwarg struct FactorDFG{T <: AbstractObservation, N} <: AbstractGrap
6868
variableorder::NTuple{N, Symbol} & (choosetype = x->NTuple{length(x), Symbol},) # NOTE v0.29 renamed from _variableOrderSymbols
6969
"""Variable timestamp.
7070
Accessors: [`getTimestamp`](@ref)"""
71-
timestamp::TimeDateZone = TimeDateZone(now(localzone())) # NOTE v0.29 changed from ZonedDateTime
71+
timestamp::TimeDateZone = tdz_now() # NOTE v0.29 changed from ZonedDateTime
7272
# TODO
7373
# """(Optional) Steady (monotonic) time in nanoseconds `Nanosecond` (`Int64``)"""
7474
# nstime::Nanosecond #NOTE v0.29 REMOVED as not used, add when needed, or now as steadytime.
@@ -103,7 +103,7 @@ function FactorDFG(
103103
variableorder::Union{<:Tuple, Vector{Symbol}},
104104
observation::AbstractObservation;
105105
label::Symbol = assembleFactorName(variableorder),
106-
timestamp::Union{TimeDateZone, ZonedDateTime} = TimeDateZone(now(localzone())),
106+
timestamp::Union{TimeDateZone, ZonedDateTime} = tdz_now(),
107107
tags::Union{Set{Symbol}, Vector{Symbol}} = Set{Symbol}([:FACTOR]),
108108
bloblets::Bloblets = Bloblets(),
109109
multihypo::Vector{Float64} = Float64[],
@@ -160,9 +160,7 @@ function FactorDFG(
160160
state::Recipestate = Recipestate(),
161161
cache = nothing;
162162
tags::Set{Symbol} = Set{Symbol}([:FACTOR]),
163-
timestamp::Union{DateTime, ZonedDateTime, TimeDateZone} = TimeDateZone(
164-
now(localzone()),
165-
),
163+
timestamp::Union{DateTime, ZonedDateTime, TimeDateZone} = tdz_now(),
166164
solvable::Int = 1,
167165
bloblets::Bloblets = Bloblets(),
168166
blobentries::Blobentries = Blobentries(),
@@ -237,7 +235,7 @@ end
237235
function FactorSummary(
238236
label::Symbol,
239237
variableorder::Union{Vector{Symbol}, Tuple};
240-
timestamp::TimeDateZone = TimeDateZone(now(localzone())),
238+
timestamp::TimeDateZone = tdz_now(),
241239
tags::Set{Symbol} = Set{Symbol}(),
242240
)
243241
return FactorSummary(label, tags, Tuple(variableorder), timestamp)

src/entities/DFGVariable.jl

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,6 @@ end
128128
##------------------------------------------------------------------------------
129129
# The Variable information packed in a way that accomdates multi-lang using json.
130130

131-
# Notes:
132-
# - timestamp is a `ZonedDateTime` in UTC.
133-
# - nstime can be used as mission time, with the convention that the timestamp millis coincide with the mission start nstime
134-
# - e.g. timestamp is `2020-01-01 06:30:01.250 UTC` and first nstime is `250_000_000`.
135-
136131
"""
137132
$(TYPEDEF)
138133
Complete variable structure for a DistributedFactorGraph variable.
@@ -147,9 +142,9 @@ $(TYPEDFIELDS)
147142
label::Symbol
148143
"""Variable timestamp.
149144
Accessors: [`getTimestamp`](@ref)"""
150-
timestamp::TimeDateZone = TimeDateZone(now(localzone())) #NOTE changed to TimeDateZone in v0.29
151-
"""Nanoseconds since a user-understood epoch (i.e unix epoch, robot boot time, etc.)"""
152-
steadytime::Union{Nothing, Nanosecond} = nothing #NOTE changed to TimeDateZone in v0.29
145+
timestamp::TimeDateZone = tdz_now() #NOTE changed to TimeDateZone in v0.29
146+
# """Nanoseconds since a user-understood epoch (i.e unix epoch, robot boot time, etc.)"""
147+
# steadytime::Union{Nothing, Nanosecond} = nothing #NOTE changed to TimeDateZone in v0.29
153148
#nstime::String = "0" #NOTE different uses, as 0-999_999 nanosecond part of timestamp now in timestamp, as steady timestamp now in steadytime
154149
"""Variable tags, e.g [:POSE, :VARIABLE, and :LANDMARK].
155150
Accessors: [`getTags`](@ref), [`mergeTags!`](@ref), and [`deleteTags!`](@ref)"""
@@ -179,7 +174,7 @@ function StructUtils.fielddefaults(
179174
::Type{VariableDFG{T, P, N}},
180175
) where {T, P, N}
181176
return (
182-
timestamp = TimeDateZone(now(localzone())),
177+
timestamp = tdz_now(),
183178
tags = Set{Symbol}(),
184179
# states = OrderedDict{Symbol, State{T, P, N}}(),
185180
bloblets = Bloblets(),
@@ -218,9 +213,9 @@ function VariableDFG(
218213
label::Symbol,
219214
statetype::Union{T, Type{T}};
220215
tags::Union{Set{Symbol}, Vector{Symbol}} = Set{Symbol}(),
221-
timestamp::Union{TimeDateZone, ZonedDateTime} = TimeDateZone(now(localzone())),
216+
timestamp::Union{TimeDateZone, ZonedDateTime} = tdz_now(),
222217
solvable::Union{Int, Base.RefValue{Int}} = Ref{Int}(1),
223-
steadytime::Union{Nothing, Nanosecond} = nothing,
218+
# steadytime::Union{Nothing, Nanosecond} = nothing,
224219
nanosecondtime = nothing,
225220
smalldata = nothing,
226221
kwargs...,
@@ -230,13 +225,10 @@ function VariableDFG(
230225
timestamp = TimeDateZone(timestamp)
231226
end
232227
if !isnothing(nanosecondtime)
233-
@assert isnothing(steadytime),
234-
"nanosecondtime is replaced by steadytime. Cannot specify both steadytime and nanosecondtime"
235228
Base.depwarn(
236-
"nanosecondtime kwarg is deprecated, use steadytime instead",
229+
"nanosecondtime kwarg is deprecated, use `timestamp` or `bloblets` instead",
237230
:VariableDFG,
238231
)
239-
steadytime = Nanosecond(nanosecondtime)
240232
end
241233
if !isnothing(smalldata)
242234
Base.depwarn("smalldata kwarg is deprecated, use bloblets instead", :VariableDFG)
@@ -249,7 +241,7 @@ function VariableDFG(
249241

250242
N = getDimension(T)
251243
P = getPointType(T)
252-
return VariableDFG{T, P, N}(; label, steadytime, solvable, tags, timestamp, kwargs...)
244+
return VariableDFG{T, P, N}(; label, solvable, tags, timestamp, kwargs...)
253245
end
254246

255247
function VariableDFG(label::Symbol, state::State; kwargs...)

src/services/CustomPrinting.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ function printVariable(
3636
nothing
3737
end
3838
println(ioc, " timestamp: ", vert.timestamp)
39-
isnothing(vert.steadytime) || println(ioc, " steadytime: ", vert.steadytime)
4039
print(ioc, " label: ")
4140
printstyled(ioc, vert.label; bold = true)
4241
println(ioc)

src/services/find.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@
66
"""
77
$SIGNATURES
88
9-
Find and return the closest timestamp from two sets of Tuples. Also return the minimum delta-time (`::Millisecond`) and how many elements match from the two sets are separated by the minimum delta-time.
9+
Find and return the closest timestamp from two sets of Tuples. Also return the minimum delta-time (`::Nanosecond`) and how many elements match from the two sets are separated by the minimum delta-time.
1010
"""
1111
function findClosestTimestamp(
1212
setA::Vector{Tuple{TimeDateZone, T}},
1313
setB::Vector{Tuple{TimeDateZone, S}},
1414
) where {S, T}
1515
#
1616
# build matrix of delta times, ranges on rows x vars on columns
17-
DT = Array{Millisecond, 2}(undef, length(setA), length(setB))
17+
DT = Array{Nanosecond, 2}(undef, length(setA), length(setB))
1818
for i = 1:length(setA), j = 1:length(setB)
1919
DT[i, j] = setB[j][1] - setA[i][1]
2020
end
@@ -38,7 +38,7 @@ end
3838
Find and return nearest variable labels per delta time. Function will filter on `regexFilter`, `tags`, and `solvable`.
3939
4040
Notes
41-
- Returns `Vector{Tuple{Vector{Symbol}, Millisecond}}`
41+
- Returns `Vector{Tuple{Vector{Symbol}, Nanosecond}}`
4242
4343
DevNotes:
4444
- TODO `number` should allow returning more than one for k-nearest matches.
@@ -68,7 +68,7 @@ function findVariableNearTimestamp(
6868
mask = BitArray{1}(undef, length(syms))
6969
fill!(mask, true)
7070

71-
RET = Vector{Tuple{Vector{Symbol}, Millisecond}}()
71+
RET = Vector{Tuple{Vector{Symbol}, Nanosecond}}()
7272
SYMS = Symbol[]
7373
CORRS = 1
7474
NUMBER = number

test/GraphsDFGSummaryTypes.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ if false
5757
tags = [:FACTOR],
5858
variableorder = [:a],
5959
timestamp = DFG.Dates.now(DFG.tz"Z"),
60-
nstime = 0,
6160
fnctype = "PriorPose2",
6261
solvable = 1,
6362
data = "",
@@ -68,7 +67,6 @@ if false
6867
tags = [:FACTOR],
6968
variableorder = [:a, :b],
7069
timestamp = DFG.Dates.now(DFG.tz"Z"),
71-
nstime = 0,
7270
fnctype = "Pose2Pose2",
7371
solvable = 1,
7472
data = "",
@@ -79,7 +77,6 @@ if false
7977
tags = [:FACTOR],
8078
variableorder = [:b, :c],
8179
timestamp = DFG.Dates.now(DFG.tz"Z"),
82-
nstime = 0,
8380
fnctype = "Pose2Pose2",
8481
solvable = 1,
8582
data = "",

test/interfaceTests.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,7 @@ end
6464
@test printVariable(var1) === nothing
6565
@test printFactor(fac1) === nothing
6666

67-
@test printVariable(iobuf, var1; skipfields = [:timestamp, :solver, :ppe, :nstime]) ===
68-
nothing
67+
@test printVariable(iobuf, var1; skipfields = [:timestamp]) === nothing
6968

7069
varstr = String(take!(iobuf))
7170
@test occursin(r"VariableDFG", varstr)
@@ -79,7 +78,7 @@ end
7978
@test occursin(r"bandwidths", varstr)
8079
# == "VariableCompute{TestVariableType1}\nlabel: a\ntags: Set([:VARIABLE, :POSE])\nsize marginal samples: (1, 1)\nkde bandwidths: [0.0]\nNo PPEs\n"
8180

82-
@test printFactor(iobuf, fac1; skipfields = [:timestamp, :solver, :nstime]) === nothing
81+
@test printFactor(iobuf, fac1; skipfields = [:timestamp]) === nothing
8382
@test occursin(r"FactorDFG.*\nlabel:\n:abf1", String(take!(iobuf)))
8483

8584
@test printFactor(iobuf, fac1; short = true) === nothing

test/testBlocks.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,8 @@ function DFGVariableSCA()
319319
@test getDimension(testvar) == 1
320320
@test getManifold(testvar) == TranslationGroup(1)
321321

322+
@test DFG.calcDeltatime(v1, v2) isa Real
323+
322324
# #TODO sort out
323325
# getState
324326
# getSolvedCount
@@ -389,6 +391,7 @@ function DFGFactorSCA()
389391
# create f0 here for a later timestamp
390392
f0 = FactorCompute(:af1, [:a], obs_prior; tags = Set([:PRIOR]))
391393

394+
@test DFG.calcDeltatime(f1, f2) isa Real
392395
#fill in undefined fields
393396
# f2.solverData.certainhypo = Int[]
394397
# f2.solverData.multihypo = Float64[]

0 commit comments

Comments
 (0)