Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event handling is not triggered on systems that require discrete or unrestricted update handling at time zero #9702

Closed
edrumwri opened this issue Oct 18, 2018 · 2 comments

Comments

@edrumwri
Copy link
Collaborator

(and documentation no longer indicates that we require CalcNextUpdateTime() to return a time strictly in the future). Proposed fix is to do any such updates in Simulator::Initialize().

@EricCousineau-TRI
Copy link
Contributor

EricCousineau-TRI commented Oct 24, 2018

I believe this may be a small reproduction case (relevant to what @RussTedrake mentioned), writing using Drake @ cf807a1:

from pydrake.all import DiagramBuilder, AbstractValue, LeafSystem, ZeroOrderHold, Simulator

class Source(LeafSystem):
    def __init__(self):
        LeafSystem.__init__(self)
        self.output = self._DeclareAbstractOutputPort(
            "output", alloc=lambda: AbstractValue.Make(""), calc=self._Output)

    def _Output(self, context, raw):
        raw.set_value("u[t={}]".format(context.get_time()))

class Stuff(LeafSystem):
    def __init__(self, name, dt):
        LeafSystem.__init__(self)
        self.input = self._DeclareAbstractInputPort("input")
        self._DeclarePeriodicPublish(dt)
        self.name = name
    
    def _DoPublish(self, context, events):
        t = context.get_time()
        u = self.EvalAbstractInput(context, self.input.get_index()).get_value()
        print("{}: t={:.3g}, u=({})".format(self.name, t, u))

builder = DiagramBuilder()
source = builder.AddSystem(Source())

dt = 1
stuff = builder.AddSystem(Stuff("direct", dt))
builder.Connect(source.output, stuff.input)

stuff_zoh = builder.AddSystem(Stuff("zoh   ", dt))
zoh = builder.AddSystem(ZeroOrderHold(dt, AbstractValue.Make("")))
builder.Connect(source.output, zoh.get_input_port(0))
builder.Connect(zoh.get_output_port(0), stuff_zoh.input)

diag = builder.Build()

sim = Simulator(diag)
sim.set_publish_every_time_step(False)
sim.Initialize()
sim.StepTo(3)

What it prints out:

direct: t=0, u=(u[t=0.0])
zoh   : t=0, u=()
direct: t=1, u=(u[t=1.0])
zoh   : t=1, u=()
direct: t=2, u=(u[t=2.0])
zoh   : t=2, u=(u[t=1.0])
direct: t=3, u=(u[t=3.0])
zoh   : t=3, u=(u[t=2.0])

What it should print out:

direct: t=0, u=(u[t=0.0])
zoh   : t=0, u=()
direct: t=1, u=(u[t=1.0])
zoh   : t=1, u=(u[t=0.0])  <-- Missing from above
direct: t=2, u=(u[t=2.0])
zoh   : t=2, u=(u[t=1.0])
direct: t=3, u=(u[t=3.0])
zoh   : t=3, u=(u[t=2.0])

Ran into this when talking with @thduynguyen, relevant to @SeanCurtis-TRI's PR on an image writer.

@sherm1
Copy link
Member

sherm1 commented Nov 30, 2018

#9707 duplicates this and has more information so I'll close this one.

@sherm1 sherm1 closed this as completed Nov 30, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants