Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions src/machinevisiontoolbox/blocks/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,26 +245,29 @@ def __init__(
self.init = init
self.retain = retain

super().__init__(nin=1, **blockargs)
# watch is passed to the base class, which adds the input signals to the
# watchlist when the simulation starts
super().__init__(nin=1, watch=watch, **blockargs)

self.grid = grid

self.watch = watch

# TODO, wire width
# inherit names from wires, block needs to be able to introspect

def start(self, simstate):
# the base class creates the figure and axes, and registers the input
# signals for watching if watch=True
super().start(simstate)

# init the arrays that hold the data
self.u_data = []
self.v_data = []
self.t_data = []

# create the figures
self.fig = self.create_figure(simstate)
self.ax = self.fig.add_subplot(111)
if not self._enabled:
# graphics are turned off for this run
return

self.camera._init_imageplane(ax=self.ax)

self.ax.set_title(self.name_tex)
Expand All @@ -280,16 +283,11 @@ def start(self, simstate):
if self.init is not None:
self.init(self.camera)

if self.watch:
for wire in self.inports:
plug = wire.start # start plug for input wire

# append to the watchlist, bdsim.run() will do the rest
simstate.watchlist.append(plug)
simstate.watchnamelist.append(str(plug))

def step(self, t, inports):
# inputs are set
def record(self, t, inports):
# called for every accepted sample, whereas step() may be called less often
# just to refresh the display
if not self._enabled:
return
self.t_data.append(t)
u, v = inports[0]

Expand All @@ -300,6 +298,10 @@ def step(self, t, inports):
self.u_data = u
self.v_data = v

def step(self, t, inports):
if not self._enabled:
return

self.line.set_data(self.u_data, self.v_data)

super().step(t, inports)
30 changes: 27 additions & 3 deletions tests/test_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,12 @@ class TestImagePlaneInASimulation:

P = np.array([1.0, 2.0, 5.0])

def run_diagram(self, **imageplane_args):
def run_diagram(self, graphics=True, **imageplane_args):
cam = CentralCamera.Default()
# sysargs=False: bdsim must not read pytest's command line as its own options
sim = bdsim.BDSim(
animation=False,
graphics=True,
graphics=graphics,
hold=False,
progress=False,
verbose=False,
Expand All @@ -189,7 +189,7 @@ def run_diagram(self, **imageplane_args):
bd.connect(pose, camera[1])
bd.connect(camera, imageplane)
bd.compile()
sim.run(bd, T=0.5, dt=0.1)
self.out = sim.run(bd, T=0.5, dt=0.1)
return cam, imageplane

def test_point_is_drawn_where_the_camera_projects_it(self, agg_backend):
Expand Down Expand Up @@ -223,6 +223,30 @@ def test_init_function_is_called_once_with_the_camera(self, agg_backend):
cam, _ = self.run_diagram(init=calls.append)
assert calls == [cam]

def test_one_figure_is_opened(self, agg_backend):
# the block used to make a second figure of its own, leaving the one
# that the base class makes empty
self.run_diagram()
assert len(plt.get_fignums()) == 1

def test_watch_records_the_input_signal(self, agg_backend):
# watch=True used to fail with AttributeError: 'StartPlug' object has no
# attribute 'start'
self.run_diagram(watch=True)
assert len(self.out.ynames) == 1
assert self.out.y.shape[0] > 1

def test_without_watch_nothing_is_recorded(self, agg_backend):
self.run_diagram()
assert len(getattr(self.out, "ynames", [])) == 0

def test_graphics_off_draws_nothing_and_still_watches(self, agg_backend):
# graphics=False used to open a figure and fail on the missing axes
_, imageplane = self.run_diagram(graphics=False, watch=True)
assert len(plt.get_fignums()) == 0
assert len(self.out.ynames) == 1
assert imageplane.ax is None


class TestEstPoseFrame:
"""EstPose_p passes its ``frame`` argument on to ``CentralCamera.estpose``."""
Expand Down
Loading