# Tech Blog

## Labview State-Space

# Implementing state-space functionality in LabVIEW real-time

# State-space implementation in LabVIEW

## Overview

The state-space formulation offers a very flexible framework for control applications and model simulation applications. A state-space block is available as part of an extended LabVIEW licence, but does not ship with the standard real-time & control module. This post shows a straightforward implementation of a mass-spring-damper system in state-space, and compares the LabVIEW implementation to a Matlab simulation.

## Spring-mass-damper system as state-space model in Matlab

A simple mass-spring-damper system can be formulated as

*m**x*″ + *d**x*′ + *k**x* = *F*,

where a prime denotes the derivative with respect to time, and *F* is the external forcing. This system can be written directly in a state space formulation, once the states are identified. Alternatively, we use Matlab here to transform between the Laplace formulation and the state space system. The required Matlab code is as follows:

```
clear all; close all; clc
%% == Simulation settings =================================================
m = 1;
d = 1;
k = 1;
dt = 1/1000;
% =========================================================================
%% === Create TF object ===================================================
s = tf('s');
H = 1/(m*s^2 + d*s + k); % relative to position
% =========================================================================
%% === Convert to SS ======================================================
[b,a] = tfdata(H);
[A,B,C,D] = tf2ss(cell2mat(b),cell2mat(a));
Hss = ss(A,B,C,D); % continous form
Hssd = c2d(Hss,dt); % discrete form
% =========================================================================
```

The transfer function *H*(*s*) is formulated relative to the position of the system. The function `tfdata`

returns the numerator and denominator of the transfer function object. The output of `tfdata`

can then be used as an input to `tf2ss`

, which returns the state-space formulation. The function `ss`

then creates a state-space object. Finally, `c2d`

converts the continuous state-space object to a discrete formulation, with a sample time of 1ms. The 1ms time is equivalent to our chosen loop time in LabVIEW. This discrete model has the following form

```
>> Hssd
Hssd =
A =
x1 x2
x1 0.999 -0.0009995
x2 0.0009995 1
B =
u1
x1 0.0009995
x2 4.998e-07
C =
x1 x2
y1 0 1
D =
u1
y1 0
Sample time: 0.001 seconds
Discrete-time state-space model.
```

## Building a state-space block in LabVIEW

The state space formulation in LabVIEW is straightforward to implement, as shown in the figure below.

Inputs to this block are the model parameters *A*,*B* and *C* (a feedthrough term D could be added easily), the present state vector *x*(*t*), and the input *u*(*t*) (the system forcing in the present example). The block outputs are the new state vector *x*(*t+1*) and the vector *y*(*t*). Using the values for *A*, *B* and *C* from Matlab, the state-space simulation block can now be used to simulate the response of the mass-spring-damper system.

Note that the values for *A*, *B* and *C* must be entered with very high precision. In practice, we recommend to simply save the Matlab outputs into a .csv file (easily done via Excel) and load this .csv file using the LabVIEW function `Read Delimited Spreadsheet.vi`

. Also note that we are using a timed loop with a set rate of 1ms, which is consistent with our parameter to the Matlab `c2d`

function.

## Simulating a state-space system in LabVIEW & Matlab

The simulation of the state-space system in LabVIEW follows the block diagram shown above. In Matlab, the simulation is readily undertaken using the `lsim`

command as follows

```
t = 0:dt:10;
T = 2; % period of oscillation
Fa = 10; % forcing amplitude
F = Fa*sin(2*pi/T*t);
x = lsim(H,F,t);
u = lsim(H*s,F,t);
```

where *x* is the position and *u* is the velocity of the system. The velocity of the system could alternatively be obtained as `gradient(x,t)`

, but the *s* transform is a more elegant way of implementing the derivative. The comparison between the two simulations (using Matlab `lsim`

and the LabVIEW state-space implementation) is shown below.

The two results are identical as expected. This approach hence provides a means of implementing any Matlab transfer function (in Laplace) as a discrete-time state-space model in LabVIEW.

We would like to thank Giorgio Bacelli of Sandia National Laboratories and Thomas Boerner of CalWave Power Technologies for providing the inspiration for this post.