def test_Gain(): # Gain blk = system.Gain() assert blk.gain == 1 blk = system.Gain(gain=-1) assert blk.gain == -1 blk = system.Gain(gain=3) assert blk.gain == 3 blk = system.Gain(gain=-1.2) assert blk.gain == -1.2 with pytest.raises(block.BlockException): blk = system.Gain(gain='asd') blk = system.Gain(gain=-5.2) blk.write(np.array([2])) (yk, ) = blk.read() assert yk == -10.4 blk = system.Gain(gain=3) blk.write(2, 4) yk = blk.read() assert yk == (6, 12) blk.write(np.array([2, 4])) (yk, ) = blk.read() assert np.all(yk == [6, 12]) blk.write(2, np.array([4, 2])) yk = blk.read() assert yk[0] == 6 and np.all(yk[1] == np.array([12, 6])) blk.set(gain=8) assert blk.gain == 8 blk = system.Gain(gain=(-1, 2), demux=True) blk.write(1) yk = blk.read() assert yk == (-1, 2) blk = system.Gain(gain=np.array([-1, 2]), demux=True) blk.write(1) yk = blk.read() assert yk == (-1, 2) with pytest.raises(block.BlockException): blk = system.Gain(gain=np.array([[-1, 2], [3, 1]]), mux=True, demux=True)
def _test_timer_sub_container(controller): import pyctrl import numpy import pyctrl.block as block import pyctrl.block.system as system import pyctrl.block.random as blkrnd import pyctrl.block.container as container print('> * * * TEST TIMER SUB CONTAINER {} * * *'.format( controller.__class__)) controller.reset() controller.add_signals('s1', 's2', 's3') # add subcontainer controller.add_timer('container1', container.Container(), ['s1'], ['s2', 's3'], period=1, repeat=False) controller.add_signals('timer/container1/s1', 'timer/container1/s2', 'timer/container1/s3') controller.add_source('timer/container1/input1', container.Input(), ['s1']) controller.add_filter('timer/container1/gain1', system.Gain(gain=3), ['s1'], ['s2']) controller.add_filter('timer/container1/gain2', system.Gain(gain=5), ['s1'], ['s3']) controller.add_sink('timer/container1/output1', container.Output(), ['s2']) controller.add_sink('timer/container1/output2', container.Output(), ['s3']) assert controller.get_signal('s2') == 0 assert controller.get_signal('s3') == 0 with controller: controller.set_signal('s1', 1) time.sleep(.1) assert controller.get_signal('s2') == 0 assert controller.get_signal('s3') == 0 with controller: controller.set_signal('s1', 1) time.sleep(1.5) assert controller.get_signal('s2') == 3 assert controller.get_signal('s3') == 5
def test_Gain(self): # Gain blk = system.Gain() self.assertEqual(blk.gain, 1) blk = system.Gain(gain=-1) self.assertEqual(blk.gain, -1) blk = system.Gain(gain=3) self.assertEqual(blk.gain, 3) blk = system.Gain(gain=-1.2) self.assertEqual(blk.gain, -1.2) with self.assertRaises(block.BlockException): blk = system.Gain(gain='asd') blk = system.Gain(gain=-5.2) blk.write(np.array([2])) (yk, ) = blk.read() self.assertEqual(yk[0], -10.4) blk = system.Gain(gain=3) blk.write(2, 4) yk = blk.read() self.assertEqual(yk, (6, 12)) blk.write(np.array([2, 4])) (yk, ) = blk.read() assert np.all(yk == np.array([6, 12])) blk.write(2, np.array([4, 2])) yk = blk.read() assert yk[0] == 6 and np.all(yk[1] == np.array([12, 6])) blk.set(gain=8) self.assertEqual(blk.gain, 8) blk = system.Gain(gain=(-1, 2), demux=True) blk.write(1) yk = blk.read() self.assertEqual(yk, (-1, 2)) blk = system.Gain(gain=np.array([-1, 2]), demux=True) blk.write(1) yk = blk.read() self.assertEqual(yk, (-1, 2)) with self.assertRaises(block.BlockException): blk = system.Gain(gain=np.array([[-1, 2], [3, 1]]), mux=True, demux=True)
def run_test_sub_container_timer(self, controller): import pyctrl.block as block import pyctrl.block.system as system import pyctrl.block.container as container print('> * * * TEST SUB CONTAINER TIMER {} * * *'.format( controller.__class__)) controller.reset() controller.add_signals('s1', 's2', 's3') # add subcontainer controller.add_filter('container1', container.Container(), ['s1'], ['s2', 's3']) controller.add_signals('container1/s1', 'container1/s2', 'container1/s3') controller.add_source('container1/input1', container.Input(), ['s1']) controller.add_filter('container1/gain1', system.Gain(gain=3), ['s1'], ['s2']) controller.add_timer('container1/constant1', block.Constant(value=5), None, ['s3'], period=1, repeat=False) controller.add_sink('container1/output1', container.Output(), ['s2']) controller.add_sink('container1/output2', container.Output(), ['s3']) with controller: controller.set_signal('s1', 1) time.sleep(.1) self.assertTrue(controller.get_signal('s2') == 3) self.assertTrue(controller.get_signal('s3') == 0) with controller: controller.set_signal('s1', 1) time.sleep(1.5) self.assertTrue(controller.get_signal('s2') == 3) self.assertTrue(controller.get_signal('s3') == 5)
def test_container(self): import pyctrl import numpy import pyctrl.block as block import pyctrl.block.system as system import pyctrl.block.logic as logic # import pyctrl.block.random as blkrnd from pyctrl import BlockType from pyctrl.block.container import Container container = Container() # initial signals _signals = container.list_signals() _sinks = container.list_sinks() _sources = container.list_sources() _filters = container.list_filters() self.assertTrue(not container.is_enabled()) container.add_signal('_test_') self.assertTrue('_test_' in container.list_signals()) #with self.assertRaises(block.container.ContainerWarning): container.add_signal('_test_') self.assertEqual(container.get_signal('_test_'), 0) container.set_signal('_test_', 1.2) self.assertEqual(container.get_signal('_test_'), 1.2) container.remove_signal('_test_') self.assertTrue('_test_' not in container.list_signals()) with self.assertRaises(pyctrl.block.container.ContainerException): container.set_signal('_test_', 1.2) container.add_signals('_test1_', '_test2_') self.assertTrue('_test1_' in container.list_signals()) self.assertTrue('_test2_' in container.list_signals()) container.remove_signal('_test1_') container.remove_signal('_test2_') self.assertTrue('_test1_' not in container.list_signals()) self.assertTrue('_test2_' not in container.list_signals()) # test info print(container.info()) self.assertTrue(isinstance(container.info(), str)) self.assertTrue(isinstance(container.info('summary'), str)) self.assertTrue(isinstance(container.info('sources', 'sinks'), str)) # test sink container.add_signal('clock') container.add_sink('_logger_', block.Logger(), ['_test_']) self.assertTrue('_logger_' in container.list_sinks()) self.assertTrue('_test_' in container.list_signals()) self.assertEqual( container.get_sink('_logger_'), { 'current': 0, 'auto_reset': False, 'page': 0, 'enabled': True, 'labels': ['_test_'], 'index': None }) self.assertEqual( container.get_sink('_logger_', 'current', 'auto_reset'), { 'current': 0, 'auto_reset': False }) self.assertEqual(container.get_sink('_logger_', 'current'), 0) container.set_sink('_logger_', current=1) self.assertEqual(container.get_sink('_logger_', 'current'), 1) # try to remove signal _test_ container.remove_signal('_test_') self.assertTrue('_test_' in container.list_signals()) container.add_sink('_logger_', block.Logger(), ['clock']) self.assertTrue('_logger_' in container.list_sinks()) # TODO: test for changed signals container.set_sink('_logger_', reset=True) log = container.get_sink('_logger_', 'log') self.assertTrue(isinstance(log['clock'], numpy.ndarray)) self.assertShape(log['clock'].shape, (0, 1)) with self.assertRaises(block.BlockException): container.set_sink('_logger_', _reset=True) container.set_enabled(True) container.run() container.run() container.set_enabled(False) log = container.get_sink('_logger_', 'log') self.assertTrue(isinstance(log['clock'], numpy.ndarray)) self.assertShape(log['clock'].shape, (2, 1)) container.set_sink('_logger_', reset=True) log = container.get_sink('_logger_', 'log') self.assertTrue(isinstance(log['clock'], numpy.ndarray)) self.assertShape(log['clock'].shape, (0, 1)) container.remove_sink('_logger_') self.assertTrue('_logger_' not in container.list_sinks()) container.add_signal('_test_') container.add_sink('_logger_', block.Logger(), ['clock', '_test_']) self.assertTrue('_logger_' in container.list_sinks()) container.set_enabled(True) container.run() container.run() container.set_enabled(False) log = container.get_sink('_logger_', 'log') self.assertTrue(isinstance(log['clock'], numpy.ndarray)) self.assertTrue(isinstance(log['_test_'], numpy.ndarray)) self.assertShape(log['clock'].shape, (2, 1)) self.assertShape(log['_test_'].shape, (2, 1)) container.set_sink('_logger_', reset=True) log = container.get_sink('_logger_', 'log') self.assertTrue(isinstance(log['clock'], numpy.ndarray)) self.assertTrue(isinstance(log['_test_'], numpy.ndarray)) self.assertShape(log['clock'].shape, (0, 1)) self.assertShape(log['_test_'].shape, (0, 1)) container.remove_sink('_logger_') self.assertTrue('_logger_' not in container.list_sinks()) # test source #container.add_source('_rand_', blkrnd.Uniform(), ['clock']) container.add_source('_rand_', block.Constant(), ['clock']) self.assertTrue('_rand_' in container.list_sources()) #container.add_source('_rand_', blkrnd.Uniform(), ['_test_']) container.add_source('_rand_', block.Constant(value=2), ['_test_']) self.assertTrue('_rand_' in container.list_sources()) #self.assertTrue( container.get_source('_rand_') == {'demux': False, 'mux': False, 'low': 0, 'high': 1, 'enabled': True, 'seed': None, 'm': 1} ) self.assertTrue( container.get_source('_rand_') == { 'demux': False, 'mux': False, 'value': 2, 'enabled': True }) self.assertTrue( container.get_source('_rand_', 'value', 'demux') == { 'value': 2, 'demux': False }) self.assertTrue(container.get_source('_rand_', 'value') == 2) container.set_source('_rand_', value=1) self.assertTrue(container.get_source('_rand_', 'value') == 1) # TODO: test for changed signals container.set_source('_rand_', reset=True) a = container.read_source('_rand_') self.assertTrue(isinstance(a, tuple)) self.assertTrue(0 <= a[0] <= 1) with self.assertRaises(block.BlockException): container.set_source('_rand_', _reset=True) container.remove_source('_rand_') self.assertTrue('_rand_' not in container.list_sources()) # test filter container.add_signal('_output_') #container.add_source('_rand_', blkrnd.Uniform(), ['_test_']) container.add_source('_rand_', block.Constant(), ['_test_']) self.assertTrue('_rand_' in container.list_sources()) container.add_filter('_gain_', block.ShortCircuit(), ['_test_'], ['_output_']) self.assertTrue('_gain_' in container.list_filters()) # TODO: test for changed block container.add_filter('_gain_', system.Gain(gain=2), ['_test_'], ['_output_']) self.assertTrue('_gain_' in container.list_filters()) self.assertTrue( container.get_filter('_gain_') == { 'demux': False, 'enabled': True, 'gain': 2, 'mux': False }) self.assertTrue( container.get_filter('_gain_', 'demux', 'gain') == { 'demux': False, 'gain': 2 }) self.assertTrue(container.get_filter('_gain_', 'gain') == 2) container.add_sink('_logger_', block.Logger(), ['_test_', '_output_']) self.assertTrue('_logger_' in container.list_sinks()) container.set_enabled(True) container.run() container.run() container.set_enabled(False) log = container.get_sink('_logger_', 'log') self.assertTrue(isinstance(log['_test_'], numpy.ndarray)) self.assertTrue(isinstance(log['_output_'], numpy.ndarray)) self.assertShape(log['_test_'].shape, (2, 1)) self.assertShape(log['_output_'].shape, (2, 1)) self.assertTrue(numpy.array_equal(log['_output_'], log['_test_'] * 2)) # test reset signals = container.list_signals() sinks = container.list_sinks() sources = container.list_sources() filters = container.list_filters() print(signals, sources, filters, sinks) print(container.info('all')) container.reset() container = Container() signals = container.list_signals() sinks = container.list_sinks() sources = container.list_sources() filters = container.list_filters() print(signals, sources, filters, sinks) self.assertTrue(signals == _signals) self.assertTrue(sources == _sources) self.assertTrue(filters == _filters) self.assertTrue(sinks == _sinks) print(container.info('all')) container.add_signal('timer') container.add_timer('timer', block.Constant(value=1), None, ['timer'], 1, False) print(container.info('all')) self.assertTrue(container.get_signal('timer') == 0) self.assertTrue( container.get_timer('timer') == { 'enabled': True, 'demux': False, 'mux': False, 'value': 1 }) self.assertTrue( container.get_timer('timer', 'enabled', 'demux') == { 'enabled': True, 'demux': False }) self.assertTrue(container.get_timer('timer', 'enabled') == True) container.set_enabled(True) container.run() time.sleep(2) container.run() container.set_enabled(False) self.assertTrue(container.get_signal('timer') == 1) container.set_signal('timer', 0) self.assertTrue(container.get_signal('timer') == 0) container.set_enabled(True) container.run() time.sleep(.5) container.run() container.set_enabled(False) self.assertTrue(container.get_signal('timer') == 0) container.set_signal('timer', 0) self.assertTrue(container.get_signal('timer') == 0) container.add_timer('stop', block.Constant(value=0), None, ['is_running'], 2, False) #print('##########') container.set_enabled(True) container.run() #print('##########') time.sleep(3) container.run() container.set_enabled(False) self.assertTrue(container.get_signal('timer') == 1) # test set container = Container() container.add_signals('s1', 's2') container.add_source('const', block.Constant(value=1), ['s1']) container.add_sink( 'set1', logic.SetSource(label='const', on_rise={'value': 0.6}, on_fall={'value': 0.4}), ['s2']) container.set_enabled(True) container.run() container.set_enabled(False) self.assertTrue(container.get_signal('s2') == 0) self.assertTrue(container.get_source('const', 'value') == 1) container.set_enabled(True) container.run() container.set_signal('s2', 1) container.run() container.set_enabled(False) self.assertTrue(container.get_signal('s2') == 1) self.assertTrue(container.get_source('const', 'value') == 0.6) container.set_enabled(True) container.run() container.set_signal('s2', 0) container.run() container.set_enabled(False) self.assertTrue(container.get_signal('s2') == 0) self.assertTrue(container.get_source('const', 'value') == 0.4)
def test_container(): import pyctrl import numpy import pyctrl.block as block import pyctrl.block.system as system import pyctrl.block.logic as logic import pyctrl.block.random as blkrnd from pyctrl import BlockType from pyctrl.block.container import Container container = Container() # initial signals _signals = container.list_signals() _sinks = container.list_sinks() _sources = container.list_sources() _filters = container.list_filters() assert not container.is_enabled() container.add_signal('_test_') assert '_test_' in container.list_signals() #with pytest.raises(pyctrl.block.container.ContainerException): container.add_signal('_test_') assert container.get_signal('_test_') == 0 container.set_signal('_test_', 1.2) assert container.get_signal('_test_') == 1.2 container.remove_signal('_test_') assert '_test_' not in container.list_signals() with pytest.raises(pyctrl.block.container.ContainerException): container.set_signal('_test_', 1.2) container.add_signals('_test1_', '_test2_') assert '_test1_' in container.list_signals() assert '_test2_' in container.list_signals() container.remove_signal('_test1_') container.remove_signal('_test2_') assert '_test1_' not in container.list_signals() assert '_test2_' not in container.list_signals() # test info assert isinstance(container.info(), str) assert isinstance(container.info('summary'), str) assert isinstance(container.info('source', 'sink'), str) # test sink container.add_signal('clock') container.add_sink('_logger_', block.Logger(), ['_test_']) assert '_logger_' in container.list_sinks() assert '_test_' in container.list_signals() assert container.get_sink('_logger_') == { 'current': 0, 'auto_reset': False, 'page': 0, 'enabled': True, 'labels': ['_test_'], 'index': None } assert container.get_sink('_logger_', 'current', 'auto_reset') == { 'current': 0, 'auto_reset': False } assert container.get_sink('_logger_', 'current') == 0 container.set_sink('_logger_', current=1) assert container.get_sink('_logger_', 'current') == 1 # try to remove signal _test_ container.remove_signal('_test_') assert '_test_' in container.list_signals() container.add_sink('_logger_', block.Logger(), ['clock']) assert '_logger_' in container.list_sinks() # TODO: test for changed signals container.set_sink('_logger_', reset=True) log = container.get_sink('_logger_', 'log') assert isinstance(log['clock'], numpy.ndarray) assert log['clock'].shape == (0, 1) with pytest.raises(block.BlockException): container.set_sink('_logger_', _reset=True) container.set_enabled(True) container.run() container.run() container.set_enabled(False) log = container.get_sink('_logger_', 'log') #print(log) assert isinstance(log['clock'], numpy.ndarray) assert log['clock'].shape == (2, 1) container.set_sink('_logger_', reset=True) log = container.get_sink('_logger_', 'log') assert isinstance(log['clock'], numpy.ndarray) assert log['clock'].shape == (0, 1) container.remove_sink('_logger_') assert '_logger_' not in container.list_sinks() container.add_signal('_test_') container.add_sink('_logger_', block.Logger(), ['clock', '_test_']) assert '_logger_' in container.list_sinks() container.set_enabled(True) container.run() container.run() container.set_enabled(False) log = container.get_sink('_logger_', 'log') assert isinstance(log['clock'], numpy.ndarray) assert isinstance(log['_test_'], numpy.ndarray) assert log['clock'].shape == (2, 1) assert log['_test_'].shape == (2, 1) container.set_sink('_logger_', reset=True) log = container.get_sink('_logger_', 'log') assert isinstance(log['clock'], numpy.ndarray) assert isinstance(log['_test_'], numpy.ndarray) assert log['clock'].shape == (0, 1) assert log['_test_'].shape == (0, 1) container.remove_sink('_logger_') assert '_logger_' not in container.list_sinks() # test source container.add_source('_rand_', blkrnd.Uniform(), ['clock']) assert '_rand_' in container.list_sources() container.add_source('_rand_', blkrnd.Uniform(), ['_test_']) assert '_rand_' in container.list_sources() assert container.get_source('_rand_') == { 'demux': False, 'mux': False, 'low': 0, 'high': 1, 'enabled': True, 'seed': None, 'm': 1 } assert container.get_source('_rand_', 'low', 'high') == { 'low': 0, 'high': 1 } assert container.get_source('_rand_', 'low') == 0 container.set_source('_rand_', low=1) assert container.get_source('_rand_', 'low') == 1 # TODO: test for changed signals container.set_source('_rand_', reset=True) a = container.read_source('_rand_') assert isinstance(a[0], float) assert 0 <= a[0] <= 1 with pytest.raises(block.BlockException): container.set_source('_rand_', _reset=True) container.remove_source('_rand_') assert '_rand_' not in container.list_sources() # test filter container.add_signal('_output_') container.add_source('_rand_', blkrnd.Uniform(), ['_test_']) assert '_rand_' in container.list_sources() container.add_filter('_gain_', block.ShortCircuit(), ['_test_'], ['_output_']) assert '_gain_' in container.list_filters() # TODO: test for changed block container.add_filter('_gain_', system.Gain(gain=2), ['_test_'], ['_output_']) assert '_gain_' in container.list_filters() assert container.get_filter('_gain_') == { 'demux': False, 'enabled': True, 'gain': 2, 'mux': False } assert container.get_filter('_gain_', 'demux', 'gain') == { 'demux': False, 'gain': 2 } assert container.get_filter('_gain_', 'gain') == 2 container.add_sink('_logger_', block.Logger(), ['_test_', '_output_']) assert '_logger_' in container.list_sinks() container.set_enabled(True) container.run() container.run() container.set_enabled(False) log = container.get_sink('_logger_', 'log') assert isinstance(log['_test_'], numpy.ndarray) assert isinstance(log['_output_'], numpy.ndarray) assert log['_test_'].shape == (2, 1) assert log['_output_'].shape == (2, 1) assert numpy.all(numpy.fabs(log['_output_'] - 2 * log['_test_']) < 1e-6) # test reset signals = container.list_signals() sinks = container.list_sinks() sources = container.list_sources() filters = container.list_filters() print(signals, sources, filters, sinks) print(container.info('all')) container.reset() container = Container() signals = container.list_signals() sinks = container.list_sinks() sources = container.list_sources() filters = container.list_filters() print(signals, sources, filters, sinks) assert signals == _signals assert sources == _sources assert filters == _filters assert sinks == _sinks print(container.info('all')) container.add_signal('timer') container.add_timer('timer', block.Constant(value=1), None, ['timer'], 1, False) print(container.info('all')) assert container.get_signal('timer') == 0 assert container.get_timer('timer') == { 'enabled': True, 'demux': False, 'mux': False, 'value': 1 } assert container.get_timer('timer', 'enabled', 'demux') == { 'enabled': True, 'demux': False } assert container.get_timer('timer', 'enabled') == True container.set_enabled(True) container.run() time.sleep(2) container.run() container.set_enabled(False) assert container.get_signal('timer') == 1 container.set_signal('timer', 0) assert container.get_signal('timer') == 0 container.set_enabled(True) container.run() time.sleep(.5) container.run() container.set_enabled(False) assert container.get_signal('timer') == 0 container.set_signal('timer', 0) assert container.get_signal('timer') == 0 container.add_timer('stop', block.Constant(value=0), None, ['is_running'], 2, False) #print('##########') container.set_enabled(True) container.run() #print('##########') time.sleep(3) container.run() container.set_enabled(False) assert container.get_signal('timer') == 1 # test set container = Container() print('* * * TEST SET * * *') print(container.info('all')) container.add_signals('s1', 's2') container.add_source('const', block.Constant(value=1), ['s1']) container.add_sink( 'set1', logic.SetSource(label='const', on_rise={'value': 0.6}, on_fall={'value': 0.4}), ['s2']) container.set_enabled(True) container.run() container.set_enabled(False) print(container.get_source('const')) assert container.get_signal('s2') == 0 assert container.get_source('const', 'value') == 1 container.set_enabled(True) container.run() container.set_signal('s2', 1) container.run() container.set_enabled(False) assert container.get_signal('s2') == 1 assert container.get_source('const', 'value') == 0.6 container.set_enabled(True) container.run() container.set_signal('s2', 0) container.run() container.set_enabled(False) assert container.get_signal('s2') == 0 assert container.get_source('const', 'value') == 0.4
def test_Feedback(self): # Feedback blk1 = system.Gain(gain=2) blk = system.Feedback(block=blk1) assert blk.block is blk1 assert blk.gamma == 1.0 blk = system.Feedback(block=blk1) assert blk.block is blk1 assert blk.gamma == 1.0 blk = system.Feedback(block=blk1, gamma=4) assert blk.block is blk1 assert blk.gamma == 4 blk.write(2, 3) (yk, ) = blk.read() self.assertEqual(yk, 2 * (3 * 4 - 2)) gn = system.Gain(gain=150) blk.set(block=gn) self.assertTrue(blk.block is gn) blk.set(gamma=10) self.assertEqual(blk.gamma, 10) # Feedback with transfer-function # # G(z) = -.5/(z - .5) # TODO: CHECK DIFFERENT SIZES NUM/DEN blk1 = system.System(model=tf.zDTTF([-.5, 0], [-.5, 1])) blktf = system.Feedback(block=blk1) self.assertTrue(blktf.block is blk1) # A = .5, B = 1, C = -.5, D = 0 # # u = C x + D (- y + r) # x = A x + B (- y + r) A = np.array([[.5]]) B = np.array([[-1, 1]]) C = np.array([[-.5]]) D = np.array([[0, 0]]) blkss = system.System(model=ss.DTSS(A, B, C, D)) blktf.write(1, 3) yk1 = list(blktf.read()) blkss.write([1, 3]) yk2, = blkss.read() self.assertTrue(np.all(np.array(yk1) == yk2)) blktf.write(-1, 3) yk1 = list(blktf.read()) blkss.write([-1, 3]) yk2, = blkss.read() self.assertTrue(np.all(np.array(yk1) == yk2)) blktf.write(-1, 3) yk1 = list(blktf.read()) blkss.write([-1, 3]) yk2, = blkss.read() self.assertTrue(np.all(np.array(yk1) == yk2)) # Reset feedback self.assertTrue( np.array_equal(blktf.block.model.state, np.array((6.5, )))) blktf.reset() self.assertTrue( np.array_equal(blktf.block.model.state, np.array((0, ))))
def _test_sub_container(controller): import pyctrl import numpy import pyctrl.block as block import pyctrl.block.system as system import pyctrl.block.random as blkrnd import pyctrl.block.container as container print('> * * * TEST SUB CONTAINER {} * * *'.format(controller.__class__)) controller.reset() # add subcontainer controller.add_signals('s1', 's2', 's3') controller.add_filter('container1', container.Container(), ['s1'], ['s2', 's3']) controller.add_signals('container1/s1', 'container1/s2', 'container1/s3') controller.add_source('container1/input1', container.Input(), ['s1']) controller.add_filter('container1/gain1', system.Gain(gain=3), ['s1'], ['s2']) controller.add_sink('container1/output1', container.Output(), ['s2']) controller.set_signal('s2', 0) with controller: controller.set_signal('s1', 1) time.sleep(.2) assert controller.get_signal('s2') == 3 # add subsubcontainer controller.add_filter('container1/container2', container.Container(), ['s1'], ['s3']) controller.add_signals('container1/container2/s1', 'container1/container2/s2') controller.add_sink('container1/output2', container.Output(), ['s3']) controller.add_source('container1/container2/input1', container.Input(), ['s1']) controller.add_filter('container1/container2/gain1', system.Gain(gain=5), ['s1'], ['s2']) controller.add_sink('container1/container2/output1', container.Output(), ['s2']) controller.set_signal('s2', 0) controller.set_signal('s3', 0) with controller: controller.set_signal('s1', 1) time.sleep(.2) assert controller.get_signal('s2') == 3 assert controller.get_signal('s3') == 5
def _test_basic(controller): import pyctrl import numpy import pyctrl.block as block import pyctrl.block.system as system import pyctrl.block.random as blkrnd print('\n> * * * TEST BASIC {} * * *'.format(controller.__class__)) # reset controller controller.reset() # initial signals _signals = controller.list_signals() _sinks = controller.list_sinks() _sources = controller.list_sources() _filters = controller.list_filters() # period #assert controller.get_period() == 0.01 # default #controller.set_period(0.1) #assert controller.get_period() == 0.1 # test signals #assert 'clock' in controller.list_signals() # clock is default controller.add_signal('_test_') assert '_test_' in controller.list_signals() #with pytest.raises(pyctrl.ControllerException): controller.add_signal('_test_') assert controller.get_signal('_test_') == 0 controller.set_signal('_test_', 1.2) assert controller.get_signal('_test_') == 1.2 controller.remove_signal('_test_') assert '_test_' not in controller.list_signals() with pytest.raises(Exception): controller.set_signal('_test_', 1.2) controller.add_signals('_test1_', '_test2_') assert '_test1_' in controller.list_signals() assert '_test2_' in controller.list_signals() controller.remove_signal('_test1_') controller.remove_signal('_test2_') assert '_test1_' not in controller.list_signals() assert '_test2_' not in controller.list_signals() # test info assert isinstance(controller.info(), str) assert isinstance(controller.info('summary'), str) assert isinstance(controller.info('sources', 'sinks'), str) # test sink controller.add_signal('clock') logger = block.Logger() controller.add_sink('_logger_', logger, ['_test_']) assert '_logger_' in controller.list_sinks() assert '_test_' in controller.list_signals() assert controller.get_sink('_logger_') == { 'current': 0, 'auto_reset': False, 'page': 0, 'enabled': True, 'labels': ['_test_'], 'index': None } assert controller.get_sink('_logger_', 'current', 'auto_reset') == { 'current': 0, 'auto_reset': False } assert controller.get_sink('_logger_', 'current') == 0 controller.set_sink('_logger_', current=1) assert controller.get_sink('_logger_', 'current') == 1 # try to remove signal _test_ controller.remove_signal('_test_') assert '_test_' in controller.list_signals() controller.add_sink('_logger_', block.Logger(), ['clock']) assert '_logger_' in controller.list_sinks() # TODO: test for changed signals controller.set_sink('_logger_', reset=True) log = controller.get_sink('_logger_', 'log') assert not hasattr(logger, 'log') assert isinstance(log['clock'], numpy.ndarray) assert log['clock'].shape == (0, 1) with pytest.raises(block.BlockException): controller.set_sink('_logger_', _reset=True) with controller: time.sleep(.2) log = controller.get_sink('_logger_', 'log') assert isinstance(log['clock'], numpy.ndarray) assert log['clock'].shape[1] == 1 assert log['clock'].shape[0] > 1 controller.set_sink('_logger_', reset=True) log = controller.get_sink('_logger_', 'log') assert isinstance(log['clock'], numpy.ndarray) assert log['clock'].shape == (0, 1) controller.remove_sink('_logger_') assert '_logger_' not in controller.list_sinks() controller.add_signal('_test_') controller.add_sink('_logger_', block.Logger(), ['clock', '_test_']) assert '_logger_' in controller.list_sinks() with controller: time.sleep(.2) log = controller.get_sink('_logger_', 'log') assert isinstance(log['clock'], numpy.ndarray) assert isinstance(log['_test_'], numpy.ndarray) assert log['clock'].shape[1] == 1 assert log['clock'].shape[0] > 1 assert log['_test_'].shape[1] == 1 assert log['_test_'].shape[0] > 1 controller.set_sink('_logger_', reset=True) log = controller.get_sink('_logger_', 'log') assert isinstance(log['clock'], numpy.ndarray) assert isinstance(log['_test_'], numpy.ndarray) assert log['clock'].shape == (0, 1) assert log['_test_'].shape == (0, 1) controller.remove_sink('_logger_') assert '_logger_' not in controller.list_sinks() # test source controller.add_source('_rand_', blkrnd.Uniform(), ['clock']) assert '_rand_' in controller.list_sources() controller.add_source('_rand_', blkrnd.Uniform(), ['_test_']) assert '_rand_' in controller.list_sources() assert controller.get_source('_rand_') == { 'demux': False, 'mux': False, 'low': 0, 'high': 1, 'enabled': True, 'seed': None, 'm': 1 } assert controller.get_source('_rand_', 'low', 'high') == { 'low': 0, 'high': 1 } assert controller.get_source('_rand_', 'low') == 0 controller.set_source('_rand_', low=1) assert controller.get_source('_rand_', 'low') == 1 # TODO: test for changed signals controller.set_source('_rand_', reset=True) a = controller.read_source('_rand_') assert isinstance(a[0], float) assert 0 <= a[0] <= 1 with pytest.raises(block.BlockException): controller.set_source('_rand_', _reset=True) controller.remove_source('_rand_') assert '_rand_' not in controller.list_sources() # test filter controller.add_signal('_output_') controller.add_source('_rand_', blkrnd.Uniform(), ['_test_']) assert '_rand_' in controller.list_sources() controller.add_filter('_gain_', block.ShortCircuit(), ['_test_'], ['_output_']) assert '_gain_' in controller.list_filters() # TODO: test for changed block controller.add_filter('_gain_', system.Gain(gain=2), ['_test_'], ['_output_']) assert '_gain_' in controller.list_filters() assert controller.get_filter('_gain_') == { 'demux': False, 'enabled': True, 'gain': 2, 'mux': False } assert controller.get_filter('_gain_', 'demux', 'gain') == { 'demux': False, 'gain': 2 } assert controller.get_filter('_gain_', 'gain') == 2 with controller: time.sleep(.2) controller.add_sink('_logger_', block.Logger(), ['_test_', '_output_']) assert '_logger_' in controller.list_sinks() with controller: time.sleep(.2) log = controller.get_sink('_logger_', 'log') assert isinstance(log['_test_'], numpy.ndarray) assert isinstance(log['_output_'], numpy.ndarray) assert log['_test_'].shape[1] == 1 assert log['_test_'].shape[0] > 1 assert log['_test_'].shape[1] == 1 assert log['_test_'].shape[0] > 1 assert numpy.all(numpy.fabs(log['_output_'] - 2 * log['_test_']) < 1e-6) # test reset signals = controller.list_signals() sinks = controller.list_sinks() sources = controller.list_sources() filters = controller.list_filters() controller.reset() signals = controller.list_signals() sinks = controller.list_sinks() sources = controller.list_sources() filters = controller.list_filters() assert signals == _signals assert sources == _sources assert filters == _filters assert sinks == _sinks # test is_running assert controller.get_signal('is_running') == False controller.start() assert controller.get_signal('is_running') == True controller.stop() assert controller.get_signal('is_running') == False
def run_test_basic(self, controller): import numpy import pyctrl.block as block import pyctrl.block.system as system print('\n> * * * TEST BASIC {} * * *'.format(controller.__class__)) # reset controller controller.reset() # initial signals _signals = controller.list_signals() _sinks = controller.list_sinks() _sources = controller.list_sources() _filters = controller.list_filters() # period # self.assertTrue( controller.get_period() == 0.01 # default # controller.set_period(0.1) # self.assertTrue( controller.get_period() == 0.1 # test signals # self.assertTrue( 'clock' in controller.list_signals() # clock is default controller.add_signal('_test_') self.assertTrue('_test_' in controller.list_signals()) # with self.assertRaises(pyctrl.ControllerException): controller.add_signal('_test_') self.assertTrue(controller.get_signal('_test_') == 0) controller.set_signal('_test_', 1.2) self.assertTrue(controller.get_signal('_test_') == 1.2) controller.remove_signal('_test_') self.assertTrue('_test_' not in controller.list_signals()) with self.assertRaises(Exception): controller.set_signal('_test_', 1.2) controller.add_signals('_test1_', '_test2_') self.assertTrue('_test1_' in controller.list_signals()) self.assertTrue('_test2_' in controller.list_signals()) controller.remove_signal('_test1_') controller.remove_signal('_test2_') self.assertTrue('_test1_' not in controller.list_signals()) self.assertTrue('_test2_' not in controller.list_signals()) # test info self.assertTrue(isinstance(controller.info(), str)) self.assertTrue(isinstance(controller.info('summary'), str)) self.assertTrue(isinstance(controller.info('sources', 'sinks'), str)) # test sink controller.add_signal('clock') logger = block.Logger() controller.add_sink('_logger_', logger, ['_test_']) self.assertTrue('_logger_' in controller.list_sinks()) self.assertTrue('_test_' in controller.list_signals()) self.assertTrue( controller.get_sink('_logger_') == { 'current': 0, 'auto_reset': False, 'page': 0, 'enabled': True, 'labels': ['_test_'], 'index': None }) self.assertTrue( controller.get_sink('_logger_', 'current', 'auto_reset') == { 'current': 0, 'auto_reset': False }) self.assertTrue(controller.get_sink('_logger_', 'current') == 0) controller.set_sink('_logger_', current=1) self.assertTrue(controller.get_sink('_logger_', 'current') == 1) # try to remove signal _test_ controller.remove_signal('_test_') self.assertTrue('_test_' in controller.list_signals()) controller.add_sink('_logger_', block.Logger(), ['clock']) self.assertTrue('_logger_' in controller.list_sinks()) # TODO: test for changed signals controller.set_sink('_logger_', reset=True) log = controller.get_sink('_logger_', 'log') self.assertTrue(not hasattr(logger, 'log')) self.assertTrue(isinstance(log['clock'], numpy.ndarray)) self.assertShape(log['clock'].shape, (0, 1)) with self.assertRaises(block.BlockException): controller.set_sink('_logger_', _reset=True) with controller: time.sleep(.2) log = controller.get_sink('_logger_', 'log') self.assertTrue(isinstance(log['clock'], numpy.ndarray)) self.assertShape(log['clock'].shape, 1, 1) self.assertShapeGreater(log['clock'].shape, 1, 0) controller.set_sink('_logger_', reset=True) log = controller.get_sink('_logger_', 'log') self.assertTrue(isinstance(log['clock'], numpy.ndarray)) self.assertShape(log['clock'].shape, (0, 1)) controller.remove_sink('_logger_') self.assertTrue('_logger_' not in controller.list_sinks()) controller.add_signal('_test_') controller.add_sink('_logger_', block.Logger(), ['clock', '_test_']) self.assertTrue('_logger_' in controller.list_sinks()) with controller: time.sleep(.2) log = controller.get_sink('_logger_', 'log') self.assertTrue(isinstance(log['clock'], numpy.ndarray)) self.assertTrue(isinstance(log['_test_'], numpy.ndarray)) self.assertShape(log['clock'].shape, 1, 1) self.assertShapeGreater(log['clock'].shape, 1, 0) self.assertShape(log['_test_'].shape, 1, 1) self.assertShapeGreater(log['_test_'].shape, 1, 0) controller.set_sink('_logger_', reset=True) log = controller.get_sink('_logger_', 'log') self.assertTrue(isinstance(log['clock'], numpy.ndarray)) self.assertTrue(isinstance(log['_test_'], numpy.ndarray)) self.assertShape(log['clock'].shape, (0, 1)) self.assertShape(log['_test_'].shape, (0, 1)) controller.remove_sink('_logger_') self.assertTrue('_logger_' not in controller.list_sinks()) # test source # controller.add_source('_rand_', blkrnd.Uniform(), ['clock']) controller.add_source('_rand_', block.Constant(), ['clock']) self.assertTrue('_rand_' in controller.list_sources()) # controller.add_source('_rand_', blkrnd.Uniform(), ['_test_']) controller.add_source('_rand_', block.Constant(value=2), ['_test_']) self.assertTrue('_rand_' in controller.list_sources()) self.assertTrue( controller.get_source('_rand_') == { 'demux': False, 'mux': False, 'value': 2, 'enabled': True }) self.assertTrue( controller.get_source('_rand_', 'value', 'demux') == { 'value': 2, 'demux': False }) self.assertTrue(controller.get_source('_rand_', 'value') == 2) controller.set_source('_rand_', value=1) self.assertTrue(controller.get_source('_rand_', 'value') == 1) # TODO: test for changed signals controller.set_source('_rand_', reset=True) a, = controller.read_source('_rand_') self.assertTrue(a == 1) with self.assertRaises(block.BlockException): controller.set_source('_rand_', _reset=True) controller.remove_source('_rand_') self.assertTrue('_rand_' not in controller.list_sources()) # test filter controller.add_signal('_output_') controller.add_source('_rand_', block.Constant(), ['_test_']) self.assertTrue('_rand_' in controller.list_sources()) controller.add_filter('_gain_', block.ShortCircuit(), ['_test_'], ['_output_']) self.assertTrue('_gain_' in controller.list_filters()) # TODO: test for changed block controller.add_filter('_gain_', system.Gain(gain=2), ['_test_'], ['_output_']) self.assertTrue('_gain_' in controller.list_filters()) self.assertTrue( controller.get_filter('_gain_') == { 'demux': False, 'enabled': True, 'gain': 2, 'mux': False }) self.assertTrue( controller.get_filter('_gain_', 'demux', 'gain') == { 'demux': False, 'gain': 2 }) self.assertTrue(controller.get_filter('_gain_', 'gain') == 2) with controller: time.sleep(.2) controller.add_sink('_printer_', block.Printer(), ['_test_', '_output_']) self.assertTrue('_printer_' in controller.list_sinks()) controller.add_sink('_logger_', block.Logger(), ['_test_', '_output_']) self.assertTrue('_logger_' in controller.list_sinks()) with controller: time.sleep(.2) log = controller.get_sink('_logger_', 'log') self.assertTrue(isinstance(log['_test_'], numpy.ndarray)) self.assertTrue(isinstance(log['_output_'], numpy.ndarray)) self.assertShape(log['_test_'].shape, 1, 1) self.assertShapeGreater(log['_test_'].shape, 1, 0) self.assertShape(log['_test_'].shape, 1, 1) self.assertShapeGreater(log['_test_'].shape, 1, 0) self.assertTrue(numpy.array_equal(log['_output_'], log['_test_'] * 2)) # test reset signals = controller.list_signals() sinks = controller.list_sinks() sources = controller.list_sources() filters = controller.list_filters() controller.reset() signals = controller.list_signals() sinks = controller.list_sinks() sources = controller.list_sources() filters = controller.list_filters() self.assertTrue(signals == _signals) self.assertTrue(sources == _sources) self.assertTrue(filters == _filters) self.assertTrue(sinks == _sinks) # test is_running self.assertTrue(controller.get_signal('is_running') == False) controller.start() time.sleep(1) self.assertTrue(controller.get_signal('is_running') == True) controller.stop() time.sleep(1) self.assertTrue(controller.get_signal('is_running') == False)
time.sleep(1) controller.set_signal('motor1', 100) time.sleep(1) controller.set_signal('motor1', -100) time.sleep(1) controller.set_signal('motor1', 0) time.sleep(1) print('\n> CLOSED LOOP ON POSITION') pmax = 2 Kp = 10 / k controller.add_signal('reference1') controller.add_filter('controller1', system.Feedback(block=system.Gain(gain=Kp)), ['encoder1', 'reference1'], ['motor1']) controller.set_sink('printer', inputs=['clock', 'encoder1', 'reference1', 'motor1']) print(controller.info('all')) with controller: time.sleep(1) controller.set_signal('reference1', pmax) print('\n> REFERENCE = {}'.format(pmax)) time.sleep(3) controller.set_signal('reference1', pmax / 2) print('\n> REFERENCE = {}'.format(pmax / 2)) time.sleep(3) controller.set_signal('reference1', -pmax / 2) print('\n> REFERENCE = {}'.format(-pmax / 2))