# Differentially flat systems¶

The control.flatsys package contains a set of classes and functions that can be used to compute trajectories for differentially flat systems.

A differentially flat system is defined by creating an object using the FlatSystem class, which has member functions for mapping the system state and input into and out of flat coordinates. The point_to_point() function can be used to create a trajectory between two endpoints, written in terms of a set of basis functions defined using the BasisFamily class. The resulting trajectory is return as a SystemTrajectory object and can be evaluated using the eval() member function.

## Overview of differential flatness¶

A nonlinear differential equation of the form

is differentially flat if there exists a function such that

and we can write the solutions of the nonlinear system as functions of and a finite number of derivatives

(1)

For a differentially flat system, all of the feasible trajectories for the system can be written as functions of a flat output and its derivatives. The number of flat outputs is always equal to the number of system inputs.

Differentially flat systems are useful in situations where explicit trajectory generation is required. Since the behavior of a flat system is determined by the flat outputs, we can plan trajectories in output space, and then map these to appropriate inputs. Suppose we wish to generate a feasible trajectory for the the nonlinear system

If the system is differentially flat then

and we see that the initial and final condition in the full state space depends on just the output and its derivatives at the initial and final times. Thus any trajectory for that satisfies these boundary conditions will be a feasible trajectory for the system, using equation (1) to determine the full state space and input trajectories.

In particular, given initial and final conditions on and its derivatives that satisfy the initial and final conditions any curve satisfying those conditions will correspond to a feasible trajectory of the system. We can parameterize the flat output trajectory using a set of smooth basis functions :

We seek a set of coefficients , such that satisfies the boundary conditions for and . The derivatives of the flat output can be computed in terms of the derivatives of the basis functions:

We can thus write the conditions on the flat outputs and their derivatives as

This equation is a linear equation of the form

where is called the flat flag for the system. Assuming that has a sufficient number of columns and that it is full column rank, we can solve for a (possibly non-unique) that solves the trajectory generation problem.

## Module usage¶

To create a trajectory for a differentially flat system, a FlatSystem object must be created. This is done by specifying the forward and reverse mappings between the system state/input and the differentially flat outputs and their derivatives (“flat flag”).

The forward() method computes the flat flag given a state and input:

zflag = sys.forward(x, u)

The reverse() method computes the state and input given the flat flag:

x, u = sys.reverse(zflag)

The flag is implemented as a list of flat outputs and their derivatives up to order :

zflag[i][j] =

The number of flat outputs must match the number of system inputs.

For a linear system, a flat system representation can be generated using the LinearFlatSystem class:

flatsys = control.flatsys.LinearFlatSystem(linsys)

For more general systems, the FlatSystem object must be created manually

flatsys = control.flatsys.FlatSystem(nstate, ninputs, forward, reverse)

In addition to the flat system descriptionn, a set of basis functions must be chosen. The FlatBasis class is used to represent the basis functions. A polynomial basis function of the form 1, , , … can be computed using the PolyBasis class, which is initialized by passing the desired order of the polynomial basis set:

polybasis = control.flatsys.PolyBasis(N)

Once the system and basis function have been defined, the point_to_point() function can be used to compute a trajectory between initial and final states and inputs:

traj = control.flatsys.point_to_point(x0, u0, xf, uf, Tf, basis=polybasis)

The returned object has class SystemTrajectory and can be used to compute the state and input trajectory between the initial and final condition:

xd, ud = traj.eval(T)

where T is a list of times on which the trajectory should be evaluated (e.g., T = numpy.linspace(0, Tf, M).

## Example¶

To illustrate how we can use a two degree-of-freedom design to improve the performance of the system, consider the problem of steering a car to change lanes on a road. We use the non-normalized form of the dynamics, which are derived Feedback Systems by Astrom and Murray, Example 3.11.

import control.flatsys as fs

# Function to take states, inputs and return the flat flag
def vehicle_flat_forward(x, u, params={}):
# Get the parameter values
b = params.get('wheelbase', 3.)

# Create a list of arrays to store the flat output and its derivatives
zflag = [np.zeros(3), np.zeros(3)]

# Flat output is the x, y position of the rear wheels
zflag[0][0] = x[0]
zflag[1][0] = x[1]

# First derivatives of the flat output
zflag[0][1] = u[0] * np.cos(x[2])  # dx/dt
zflag[1][1] = u[0] * np.sin(x[2])  # dy/dt

# First derivative of the angle
thdot = (u[0]/b) * np.tan(u[1])

# Second derivatives of the flat output (setting vdot = 0)
zflag[0][2] = -u[0] * thdot * np.sin(x[2])
zflag[1][2] =  u[0] * thdot * np.cos(x[2])

return zflag

# Function to take the flat flag and return states, inputs
def vehicle_flat_reverse(zflag, params={}):
# Get the parameter values
b = params.get('wheelbase', 3.)

# Create a vector to store the state and inputs
x = np.zeros(3)
u = np.zeros(2)

# Given the flat variables, solve for the state
x[0] = zflag[0][0]  # x position
x[1] = zflag[1][0]  # y position
x[2] = np.arctan2(zflag[1][1], zflag[0][1])  # tan(theta) = ydot/xdot

# And next solve for the inputs
u[0] = zflag[0][1] * np.cos(x[2]) + zflag[1][1] * np.sin(x[2])
u[1] = np.arctan2(
(zflag[1][2] * np.cos(x[2]) - zflag[0][2] * np.sin(x[2])), u[0]/b)

return x, u

vehicle_flat = fs.FlatSystem(
3, 2, forward=vehicle_flat_forward, reverse=vehicle_flat_reverse)


To find a trajectory from an initial state to a final state in time we solve a point-to-point trajectory generation problem. We also set the initial and final inputs, which sets the vehicle velocity and steering wheel angle at the endpoints.

# Define the endpoints of the trajectory
x0 = [0., -2., 0.]; u0 = [10., 0.]
xf = [100., 2., 0.]; uf = [10., 0.]
Tf = 10

# Define a set of basis functions to use for the trajectories
poly = fs.PolyFamily(6)

# Find a trajectory between the initial condition and the final condition
traj = fs.point_to_point(vehicle_flat, x0, u0, xf, uf, Tf, basis=poly)

# Create the trajectory
t = np.linspace(0, Tf, 100)
x, u = traj.eval(t)


## Module classes and functions¶

### Flat systems classes¶

 Base class for implementing basis functions for flat systems. FlatSystem(forward, reverse[, updfcn, …]) Base class for representing a differentially flat system. LinearFlatSystem(linsys[, inputs, outputs, …]) Polynomial basis functions. SystemTrajectory(sys, basis[, coeffs, flaglen]) Class representing a system trajectory.

### Flat systems functions¶

 point_to_point(sys, x0, u0, xf, uf, Tf[, …]) Compute trajectory between an initial and final conditions.