def test_clock(): from pyctrl import Controller from pyctrl.block.clock import Clock, TimerClock controller = Controller() period = 0.01 controller.add_signal('clock') clock = Clock() controller.add_source('clock', clock, ['clock']) K = 10 k = 0 while k < K: (t, ) = controller.read_source('clock') k += 1 assert clock.get('count') == 10 controller.set_source('clock', reset=True) (t, ) = controller.read_source('clock') assert t < 0.01 controller.remove_source('clock') clock = TimerClock(period=period) controller.add_source('clock', clock, ['clock']) K = 10 k = 0 while k < K: (t, ) = controller.read_source('clock') k += 1 assert t > 0.9 * K * period controller.set_source('clock', reset=True) (t, ) = controller.read_source('clock') assert t < 0.9 * 2 * period clock.set_enabled(False)
def test_run(): from pyctrl import Controller from pyctrl.block.clock import Clock, TimerClock from pyctrl.block import Map controller = Controller() clock = Clock() controller.add_source('clock', clock, ['clock']) # start/stop with condition controller.add_filter('condition', Map(function=lambda x: x < 1), ['clock'], ['is_running']) controller.set_source('clock', reset=True) controller.start() is_running = controller.get_signal('is_running') while is_running: is_running = controller.get_signal('is_running') controller.stop() tk = controller.get_signal('clock') assert tk > 1 and tk < 1.01 # run with condition controller.set_source('clock', reset=True) controller.run() controller.stop() tk = controller.get_signal('clock') assert tk > 1 and tk < 1.01 controller.remove_source('clock')