# Controller Design Steps

Below are steps to design a controller for a system. An example is also provided.

## Steps

**1. Modelling**
Create a model. Express the real system on paper. The model should:

- Capture the characteristics of the control target.
- Be easy to analyze and design a controller for. Do not directly apply dynamics equations if it complicates analysis.

**2. Design & Analysis**
Study the characteristics of the control target using the model. Then, design a control method.

- Applying different inputs and observe how the system behaves.
- Find input,
*u(t)*, that result in the desired behavior.
- Choose parameters that satisfy criteria specified in the design specification.

**3. Implementation**
Write code according to the previously determined control method.

## Example

A car moves along a line. Design a controller that moves the car to *y = 0*

**1. Modelling**
From Newton's second law:

**2. Design & Analysis**
Control the force by using the car's position and velocity as feedback (as an example):

Substitute into the model and solve for the position, *y(t)* because we want *y = 0*:

Where *C*_{1} and *C*_{2} depends on *y(0)* and *ẏ(0)* so we leave them.

*λ*_{1} and *λ*_{2} are:

The objective is *y = 0* so we want,

So we want *λ*_{1}, *λ*_{2} < 0

So choose *k*_{1}, *k*_{2} > 0

**3. Implementation**
C code:

loop()
{
y = get_position();
vy = get_velocity();
u = -k1 * y - k2 * vy;
set_force(u);
}

Simulation:

from scipy.integrate import solve_ivp
import matplotlib.pyplot as plt
# Constants
m = 1 # Mass (kg)
k1 = 10
k2 = 3
# dx/dt = f(t, x)
#
# t : Current time (seconds), scalar
# x : Current state, [y, vy]
# return: First derivative of state, [vy, ay]
def xdot(t, x):
# Controller (input, u)
u = -k1 * x[0] - k2 * x[1]
# Dynamics (dx/dt = xdot = [vz, az])
return [x[1], u/m]
x0 = [5, 0] # Initial state, [y0, vy0]
t_span = [0, 5] # Simulation time (seconds), [from, to]
# Solve for the states, x(t) = [y(t), vy(t)]
sol = solve_ivp(xdot, t_span, x0)
# Plot z vs t
plt.plot(sol.t, sol.y[0], 'k-o')
plt.show()