Generating Repeated Events Using Sample

1 SamplingClock

The function sample(first,interval) returns true and can be used to trigger events at time instants first + i*interval (i=0,1,...), where first is the time of the first event and interval is the time interval between the periodic events. It is typically used in models of periodically sampled systems.



sample acts as a kind of clock that returns true at periodically occurring time instants. We can use this behavior in combination with a when-equation to periodically trigger events, e.g. as below:



1.1 Simulation of SamplingClock





2 Sampling Values at Clock Ticks Using Clocked sample()

The clocked version of sample( ) is quite different from the unclocked version generating events, which was described in the previous section. The clocked version of the operator samples a continuous-time expression at the ticks of a clock and returns a clocked expression. Below a very simple example model using sample is given where the variable x is sampled at 5 Hz.








3 Accessing Clocked Values Between Clocked Events Using hold()

Unclocked discrete-time variables are piece-wise constant, may change at events, and have defined values also between events. This is however not the case for clocked variables which are only defined and may only change at clock ticks for their associated clock. The hold() operator makes clocked values available also in unclocked equations and expressions. The operator effectively converts a clocked discrete-time expression into an unclocked discrete-time expression which is piece-wise constant and accessible between clock tick events.


In the SquareWaveClocked model we instead use Modelica synchronous clocks where the clock variable clk is declared using the builtin Clock type, the clock interval is specified using the Clock constructor, and we have introduced a synchronous clocked discrete-time variable xclocked which is only defined at clock ticks.



This model contains clocked synchronous constructs that are not yet fully supported by OpenModelica as of October 2015, but may start working within the near future. Hence Simulation of this model does not work





4 Preventing Events Using smooth () for Continuous Expressions

The function smooth(p,expr) evaluates and returns the value of the expression expr in a way that can avoid event generation. This is similar to noEvent(), but with the difference that smooth() does not guarantee preventing events- it lets the Modelica environment decide what is appropriate. By placing a call to smooth() around expr, we state that expr is continuous and its derivates up to order p are continuous. This information can be used by the Modelica environment to avoid event generation and thus increase performance.