Skip to content

Commit a17903d

Browse files
authored
Merge pull request #33 from JuliaRobotics/feat/3Q20/aniTtitle
add dt to animation title
2 parents c390b86 + 59f84b7 commit a17903d

File tree

4 files changed

+71
-54
lines changed

4 files changed

+71
-54
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name = "FunctionalStateMachine"
22
uuid = "3e9e306e-7e3c-11e9-12d2-8f8f67a2f951"
33
keywords = ["state machine"]
44
desc = "Functional state machine with stepping and visualization tools."
5-
version = "0.2.5"
5+
version = "0.2.6"
66

77
[deps]
88
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"

src/FunctionalStateMachine.jl

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export
99
StateMachine,
1010
emptyState,
1111
exitStateMachine,
12+
getIterCount,
1213
sandboxStateMachineStep,
1314
getStateLabel,
1415
histStateMachineTransitions

src/StateMachine.jl

+18-5
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,22 @@ usrdata = nothing
4040
while st(usrdata); end
4141
```
4242
"""
43-
function (st::StateMachine{T})(userdata::T=nothing;
44-
breakafter::Function=exitStateMachine,
45-
verbose::Bool=false,
46-
iterlimit::Int=-1,
47-
recordhistory::Bool=false ) where {T}
43+
function (st::StateMachine{T})( userdata::T=nothing;
44+
breakafter::Function=exitStateMachine,
45+
verbose::Bool=false,
46+
iterlimit::Int=-1,
47+
recordhistory::Bool=false,
48+
housekeeping_cb::Function=(st)->() ) where {T}
4849
#
4950
st.iter += 1
51+
# verbose print to help debugging
5052
!verbose ? nothing : println("FSM $(st.name), iter=$(st.iter) -- $(st.next)")
53+
# early exit plumbing
5154
retval = st.next != breakafter && (iterlimit == -1 || st.iter < iterlimit)
55+
# record steps for later
5256
recordhistory ? push!(st.history, (Dates.now(), st.iter, deepcopy(st.next), deepcopy(userdata))) : nothing
57+
# user has some special situation going on.
58+
housekeeping_cb(st)
5359
st.next = st.next(userdata)
5460
return retval
5561
end
@@ -73,6 +79,13 @@ function exitStateMachine(dummy)
7379
return emptyState
7480
end
7581

82+
"""
83+
$SIGNATURES
84+
85+
How many iterations has this `::StateMachine` stepped through.
86+
"""
87+
getIterCount(st::StateMachine) = st.iter
88+
7689
"""
7790
$SIGNATURES
7891

src/StateMachineAnimation.jl

+51-48
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,17 @@ function histGraphStateMachineTransitions(stateVisits, allStates::Vector{Symbol}
8484
end
8585

8686

87-
function renderStateMachineFrame(vg,
88-
frame::Int;
89-
title::String="",
90-
viewerapp::String="eog",
91-
fext::String="png",
92-
engine::String="dot",
93-
show::Bool=true,
94-
folder::String="fsm_animation",
95-
folderpath = "/tmp/$folder/",
96-
timest::String="",
97-
rmfirst::Bool=false )
87+
function renderStateMachineFrame( vg,
88+
frame::Int;
89+
title::String="",
90+
viewerapp::String="eog",
91+
fext::String="png",
92+
engine::String="dot",
93+
show::Bool=true,
94+
folder::String="fsm_animation",
95+
folderpath = "/tmp/$folder/",
96+
timest::String="",
97+
rmfirst::Bool=false )
9898
#
9999
if rmfirst
100100
@warn "removing contents of $(folderpath)"
@@ -113,7 +113,7 @@ function renderStateMachineFrame(vg,
113113
fid = open("$folderpath/dotscript.sh","w")
114114
str = "head -n `wc -l $dotfile | awk '{print \$1-1}'` $dotfile > $folderpath/tmpdot.dot"
115115
println(fid, str)
116-
println(fid, "echo \"graph [label=\\\"$title, #$step, $(timest)\\\", labelloc=t];\" >> $folderpath/tmpdot.dot")
116+
println(fid, "echo \"graph [label=\\\"$title, $(timest)\\\", labelloc=t];\" >> $folderpath/tmpdot.dot")
117117
println(fid, "echo \"}\" >> $folderpath/tmpdot.dot")
118118
close(fid)
119119
run(`chmod u+x $folderpath/dotscript.sh`)
@@ -128,10 +128,10 @@ function renderStateMachineFrame(vg,
128128
return filepath
129129
end
130130

131-
function setVisGraphOnState!(vg, vertid;
132-
xlabel::String="",
133-
appendxlabel::String="",
134-
vertColor::AbstractString="red" )
131+
function setVisGraphOnState!( vg, vertid;
132+
xlabel::String="",
133+
appendxlabel::String="",
134+
vertColor::AbstractString="red" )
135135
#
136136
vg.vertices[vertid].attributes["fillcolor"] = vertColor
137137
vg.vertices[vertid].attributes["style"] = "filled"
@@ -154,18 +154,18 @@ function clearVisGraphAttributes!(vg)
154154
nothing
155155
end
156156

157-
function drawStateTransitionStep(hist,
158-
step::Int,
159-
vg,
160-
lookup::Dict{Symbol,Int};
161-
title::String="",
162-
viewerapp::String="eog",
163-
fext::String="png",
164-
engine::String="dot",
165-
show::Bool=true,
166-
folder::String="",
167-
frame::Int=step,
168-
vertColor::AbstractString="red" )
157+
function drawStateTransitionStep( hist,
158+
step::Int,
159+
vg,
160+
lookup::Dict{Symbol,Int};
161+
title::String="",
162+
viewerapp::String="eog",
163+
fext::String="png",
164+
engine::String="dot",
165+
show::Bool=true,
166+
folder::String="",
167+
frame::Int=step,
168+
vertColor::AbstractString="red" )
169169
#
170170

171171
lbl = getStateLabel(hist[step][3])
@@ -179,26 +179,26 @@ function drawStateTransitionStep(hist,
179179
# delete!(vert.attributes, "style")
180180

181181
# identify and set the node
182-
xlabel = length(title) > 0 ? (xlabelbefore != nothing ? xlabelbefore*"," : "")*title : ""
182+
xlabel = length(title) > 0 ? (xlabelbefore !== nothing ? xlabelbefore*"," : "")*title : ""
183183
setVisGraphOnState!(vg, vertid, xlabel=xlabel, vertColor=vertColor )
184184

185185
# render state machine frame
186-
filepath = renderStateMachineFrame(vg,
187-
frame,
188-
title=title,
189-
viewerapp=viewerapp,
190-
fext=fext,
191-
engine=engine,
192-
show=show,
193-
folder=folder,
194-
timest=string(split(string(hist[step][1]),'T')[end]),
195-
rmfirst=false)
186+
filepath = renderStateMachineFrame( vg,
187+
frame,
188+
title=title,
189+
viewerapp=viewerapp,
190+
fext=fext,
191+
engine=engine,
192+
show=show,
193+
folder=folder,
194+
timest=string(split(string(hist[step][1]),'T')[end]),
195+
rmfirst=false)
196196
#
197197

198198
# clean up the vg structure
199-
fillcolorbefore == nothing ? delete!(vert.attributes, "fillcolor") : (vert.attributes["fillcolor"]=fillcolorbefore)
200-
stylebefore == nothing ? delete!(vert.attributes, "style") : (vert.attributes["style"]=stylebefore)
201-
xlabelbefore == nothing ? delete!(vert.attributes, "xlabel") : (vert.attributes["xlabel"]=xlabelbefore)
199+
fillcolorbefore === nothing ? delete!(vert.attributes, "fillcolor") : (vert.attributes["fillcolor"]=fillcolorbefore)
200+
stylebefore === nothing ? delete!(vert.attributes, "style") : (vert.attributes["style"]=stylebefore)
201+
xlabelbefore === nothing ? delete!(vert.attributes, "xlabel") : (vert.attributes["xlabel"]=xlabelbefore)
202202

203203
return filepath
204204
end
@@ -398,8 +398,8 @@ end
398398
# @async run(`totem /tmp/caesar/csmCompound/out.ogv`)
399399
# draw_more_cb(::Tuple, ::Int, ::String)
400400
function animateStateMachineHistoryIntervalCompound(hists::Dict{Symbol, Vector{Tuple{DateTime, Int, <: Function, T}}};
401-
interval::Int=2, # frames
402-
# frames::Int=100,
401+
easyNames::Dict{Symbol,N}=Dict{Symbol,Nothing}(),
402+
interval::Int=2,
403403
folderpath="/tmp/animatestate",
404404
title::String="",
405405
show::Bool=false,
@@ -408,7 +408,7 @@ function animateStateMachineHistoryIntervalCompound(hists::Dict{Symbol, Vector{T
408408
draw_more_cb::Function=(x...)->(),
409409
fsmColors::Dict{Symbol,String}=Dict{Symbol,String}(),
410410
defaultColor::AbstractString="red",
411-
autocolor_cb::Function=(histstep,csym,aniT)->(haskey(fsmColors, csym) ? fsmColors[csym] : defaultColor) ) where T
411+
autocolor_cb::Function=(histstep,csym,aniT)->(haskey(fsmColors, csym) ? fsmColors[csym] : defaultColor) ) where {T, N}
412412
#
413413
# Dict{Symbol, Vector{Symbol}}
414414
stateVisits = Dict{Symbol, Vector{Symbol}}()
@@ -433,6 +433,7 @@ function animateStateMachineHistoryIntervalCompound(hists::Dict{Symbol, Vector{T
433433
prevList = Dict{Symbol, Vector{Int}}()
434434
latestList = Dict{Symbol, Int}(whId => fsmStep)
435435

436+
prevT = aniT
436437
frameCount = 0
437438
# loop across time
438439
@showprogress "exporting state machine images, $title " for stepCount in 1:totSteps
@@ -451,18 +452,20 @@ function animateStateMachineHistoryIntervalCompound(hists::Dict{Symbol, Vector{T
451452
lbl = getStateLabel(hists[csym][lstep][3])
452453
vertid = lookup[lbl]
453454
vertColor=autocolor_cb(hists[csym][lstep], csym, aniT)
454-
# vertColor = haskey(fsmColors,csym) ? fsmColors[csym] : defaultColor
455-
setVisGraphOnState!(vg, vertid, appendxlabel=string(csym)*",", vertColor=vertColor )
455+
easyn = haskey(easyNames, csym) ? easyNames[csym] : csym
456+
setVisGraphOnState!(vg, vertid, appendxlabel="($easyn.$lstep),", vertColor=vertColor )
456457
end
457458

458459
# and draw as many frames for that setup
459460
for itr in 1:interval
460461
# increment frame counter
461462
frameCount += 1
463+
deltaT = (aniT - prevT).value
464+
prevT = aniT
462465
# finally render one frame
463466
renderStateMachineFrame(vg,
464467
frameCount,
465-
title=title,
468+
title=title*" || dt=$deltaT ms ||",
466469
show=false,
467470
folderpath=folderpath,
468471
timest=string(split(string(aniT),' ')[1]),

0 commit comments

Comments
 (0)