## HYSDEL Examples

### Logic expressions

The passengers of a train can pull the handle of the emergency stop,
generating the binary signal `alarm = 1`. Usually, this causes an
emergency stop of the train, by setting the emergency braking signal
`brake = 1`. However, the brake should not be activated if fire is
detected while the train is in a tunnel. We define the indicator
variables for fire as `fire = 1` and tunnel transition as
`tunnel = 1`. Then we can describe this system as `brake =
alarm & (~tunnel | ~fire)`.

SYSTEM train { INTERFACE { INPUT { BOOL alarm, tunnel, fire; } OUTPUT { BOOL brake; } } IMPLEMENTATION { AUX { BOOL decision; } LOGIC { decision = alarm & (~tunnel | ~fire); } OUTPUT { brake = decision; } } }

### Electric motor with a nonlinear amplifier

The command signal `u` to an electric motor is passed through a
nonlinear amplifier, which has two modes of operation: high gain and
low gain. In *low gain* the amplifier completely rejects the
noise `d`, while in *high gain* it only attenuates
it. The Boolean input `low = 1` selects the low gain mode. The
actual signal `uact` applied to the motor is `uact = u` if
`low = 1`, otherwise `uact = 2.3u + 0.4d`.

SYSTEM motor { INTERFACE { INPUT { REAL u, d; BOOL low; } OUTPUT { REAL uact; } } IMPLEMENTATION { AUX { REAL unl; } DA { unl = { IF low THEN u ELSE 2.3*u + 0.4*d}; } OUTPUT { uact = unl; } } }

### Discrete-Time Dynamics

We want to model the dynamic behavior of a capacitor, whose
discret-time model is given by `u(k+T) = u(k) + T/C*i(k)`.
The corresponding HYSDEL model is as follows:

SYSTEM capacitor { INTERFACE { /* The interface section defines state and input variables, along with symbolic parameters. */ STATE { REAL u; } INPUT { REAL i; } PARAMETER { REAL C; REAL T; } } IMPLEMENTATION { CONTINUOUS { /* The continuous section describes the discrete-time state-update equation. */ u = u - T/C*i; } } }

### Binary sensors

In a liquid tank with inflow `Qin` and outflow `Qout`,
the binary sensor `overflow` should be set to
`overflow=1` if and only if the liquid level `h` is
above a certain threshold. Moreover, the liquid level evolves
according to discre-time mass-balance equation `h(k+1) = h(k) + Qin
- Qout`.

SYSTEM tank { INTERFACE { STATE { REAL h; } INPUT { REAL Qin, Qout; } OUTPUT { BOOL overflow; } PARAMETER { REAL threshold = 1.5; } } IMPLEMENTATION { AUX { BOOL threshold_exceeded; } AD { threshold_exceeded = (h >= threshold); } CONTINUOUS { h = h + Qin - Qout; } OUTPUT { overflow = threshold_exceeded; } } }

### Finite State Machines

A hydroelectric power plant operates according to the FSM diagram
above with boolean inputs `uclose`, `uopen`,
`ustop`, and boolean states `closing`, `stop`,
`opening`.

SYSTEM fsm { IMPLEMENTATION { STATE { BOOL closing, stop, opening; } INPUT { BOOL uclose, uopen, ustop; } } IMPLEMENTATION { AUTOMATA { /* Model of the FSM */ closing = (uclose & closing) | (uclose & stop); stop = ustop | (uopen & closing) | (uclose & opening); opening = (uopen & stop) | (uopen & opening); } MUST { /* The MUST section describes constraints */ ~(uclose & uopen); ~(uclose & ustop); ~(uopen & ustop); } } }

### Gas actuated hopping machine

The gas actuated hopping machine is driven by a penumatic actuator,
with the location of the piston relative to the mass under no external
loading defined as `yp`

. After contact occurs with the
ground with `y<=yp`

, the upward force on the mass from
actuator can be approximated by a linear spring with ```
F =
k(yp-y)
```

; where `k`

is the spring constant. The
position yp can be viewed as the unstretched spring length and it can
be easily changed by pumping air into or out of either side of the
cylinder. The equations of motion for the mass are

The system is subject to constraints on maximal and minimal piston
position `yp`

, as well as on the control input
`u`

.
Behavior of the system shows clear hybrid characteristics, since the
force `F`

takes non-zero values if the machine hits the
ground, otherwise the external force due to the piston has no effect
on the machine movement.
A simple Euler approximation can be used to discretize the equations of
motions. Then the following HYSDEL model describes behavior of the
hopping machine under different operating conditions:

SYSTEM hopper { INTERFACE { STATE { REAL y [0, 30]; /* mass height */ REAL speed [-30, 30]; /* mass speed */ REAL yp [0, 5]; /* piston position */ } INPUT { REAL u [-20, 20]; /* air inflow */ } OUTPUT { REAL height; /* output = mass height */ } PARAMETER { REAL dt = 0.1; /* sampling time */ REAL k = 500; /* spring constant */ REAL m = 1; /* mass */ REAL g = 386 * dt; /* acceleration due to gravity */ } } IMPLEMENTATION { AUX { /* auxiliary variables */ BOOL groundhit; REAL aux_speed; } AD { /* the 'groundhit' variable will be true iff we hit the ground */ groundhit = y - yp <= 0; } DA { aux_speed = { IF groundhit THEN /* when the machine hits the ground, additional upwards force is generated due to the piston */ dt*(k*yp - g) ELSE /* while in the air, only gravity affects the motion */ -dt*g + speed }; } CONTINUOUS { /* state update equations */ y = dt*speed + y; speed = aux_speed; yp = dt*u + yp; } OUTPUT { height = y; } } }

### Two tanks connected via binary valves

The two-tanks systems consists of two tanks which are connected by
two valves. Liquid inflow `Q1`

to the first tank is governed by
a pump and there are two interconnections between the tanks:

- Through Valve 1, which is always fully open.
The liquid flows through this valve if
`h1 > hv`

. - Through Valve 2, which can be either fully open (
`V2=1`

), or fully closed (`V2=0`

).

In addition, there is a permanent leak from tank 2. The system has two manipulated variables:

- Liquid inflow
`Q1`

which can change continuously. - Position of Valve 2 which can be either fully open (
`V2=1`

), or fully closed (`V2=0`

). No other positions are allowed.

The system is subject to constraints on liquid inflow and heights of liquid levels in both tanks. The two-tanks system shows clear characteristics of hybrid systems. First, the dynamics changes depending on current operating conditions (e.g. there is a direct flow from tank 1 to tank 2 through Valve 1 if level in the first tank is above certain level), and secondly, some system inputs are in fact on-off switches, such as Valve 2.

SYSTEM twotanks { INTERFACE { STATE { REAL h1 [0, 0.62]; /* level in 1st tank */ REAL h2 [0, 0.3]; /* level in 2nd tank */ } INPUT { REAL q1 [0, 1]; /* liquid inflow */ BOOL V2; /* position of Valve 2 */ } OUTPUT { REAL y; /* output = h2 */ } PARAMETER { REAL dT = 10; /* sampling time */ REAL hv = 0.3; /* height of Valve 1 */ REAL s = 10e-6; /* scross-section of valves */ REAL hmax = 0.62; /* maximum height of liquid */ REAL g = 9.81; REAL F = 0.0143/dT; /* area of each tank */ REAL q_in = 0.1e-3; /* maximum liquid inflow */ REAL k1 = s*sqrt(2*g/(hmax-hv)); REAL k2 = s*sqrt(2*g/hmax); } } IMPLEMENTATION { AUX { /* auxiliary variables */ REAL z1_1, z2_1, z3_1, z4_1; REAL z1_2, z2_2, z3_2, z4_2; BOOL above; } AD { above = h1 >= hv; /* d1 will be true iff h1 >= hv */ } DA { z1_1 = { /* h1>=hv and V2 open */ IF (above & V2) THEN (1-k1/F)*h1 - k2/F*h2 + hv/F*k1 ELSE 0 }; z1_2 = { /* h1>=hv and V2 open */ IF (above & V2) THEN (k1+k2)/F*h1 + (1-k2/F)*h2 - hv/F*k1 ELSE 0 }; z2_1 = { /* h1 <=hv and V2 open */ IF (~above & V2) THEN (1-k2/F)*h1 ELSE 0 }; z2_2 = { /* h1 < hv and V2 open */ IF (~above & V2) THEN k2/F*h1 + (1-k2/F)*h2 ELSE 0 }; z3_1 = { /* h1>=hv and V2 closed */ IF (above & ~V2) THEN (1-k1/F)*h1 + k1/F*hv ELSE 0 }; z3_2 = { /* h1>=hv and V2 closed */ IF (above & ~V2) THEN k1/F*h1 + (1-k2/F)*h2 - k1/F*hv ELSE 0 }; z4_1 = { /* h1<=hv and V2 closed */ IF (~above & ~V2) THEN h1 ELSE 0 }; z4_2 = { /* h1<=hv and V2 closed */ IF (~above & ~V2) THEN 1-k2/F ELSE 0 }; } CONTINUOUS { /* state update equations */ /* h1(k+1) = ... */ h1 = z1_1 + z2_1 + z3_1 + z4_1 + q_in/F*Q1; /* h2(k+1) = ... */ h2 = z1_2 + z2_2 + z3_2 + z4_2; } OUTPUT { /* output is just h2 */ y = h2; } } }