Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1"""Blocks for discrete-time simulation""" 

2from modypy.model import Block, SignalState, Port, EventPort 

3 

4 

5class ZeroOrderHold(Block): 

6 """A zero-order-hold block which samples an input signal when the connected 

7 event occurs. 

8 

9 The block provides an event port ``event_input`` that should be connected 

10 to the event source that shall trigger the sampling. 

11 """ 

12 

13 def __init__(self, owner, shape=1, initial_condition=None): 

14 """ 

15 Constructor for ``ZeroOrderHold`` 

16 

17 Args: 

18 owner: The owner of the block (system or block) 

19 shape: The shape of the input and output signal 

20 initial_condition: The initial state of the sampling output 

21 (before the first tick of the block) 

22 """ 

23 Block.__init__(self, owner) 

24 

25 self.event_input = EventPort(self) 

26 self.event_input.register_listener(self.update_state) 

27 self.input = Port(shape=shape) 

28 self.output = SignalState(self, 

29 shape=shape, 

30 initial_condition=initial_condition, 

31 derivative_function=None) 

32 

33 def update_state(self, data): 

34 """Update the state on a clock event 

35 

36 Args: 

37 data: The time, states and signals of the system 

38 """ 

39 self.output.set_value(data, self.input(data)) 

40 

41 

42def zero_order_hold(system, input_port, event_port, initial_condition=None): 

43 """Create a ``ZeroOrderHold`` instance that samples the given input port. 

44 This is a convenience function that returns the single output port of the 

45 zero-order-hold block. 

46 

47 Args: 

48 system: The system the ``ZeroOrderHold`` block shall be added to. 

49 input_port: The input port to sample. 

50 event_port: The event port to use as a sampling signal 

51 initial_condition: The initial condition of the ``ZeroOrderHold`` block. 

52 (Default value = None) 

53 

54 Returns: 

55 The output signal of the zero-order hold 

56 """ 

57 

58 hold = ZeroOrderHold(system, 

59 shape=input_port.shape, 

60 initial_condition=initial_condition) 

61 hold.input.connect(input_port) 

62 hold.event_input.connect(event_port) 

63 return hold.output