Observe the system to get a basic understanding of its main features: the d.c. electrical motor is on the right and drives a large wheel. Circular rails are fixed around the wheel and a ball rolls freely on top of them with an unstable equilibrium at the top position. The ball movement is limited to a small range.

For simulation and controller design, the system is represented as a state-space model. It can be split into two subsystems: the electrical drive with the d.c. motor, and the mechanical part with the wheel, rails and ball. These subsystems have a mutual influence, not an input-output connection where causality is clearly defined.

The numerical values given for the parameters correspond to the virtual experiment.

### Electrical drive and wheel

- System input
- u (motor voltage)
- Parameters
- Rm = 1 Ω (motor resistance)
- K = 0.1 N·m/A (motor torque/current constant)
- N = 7 (reduction factor from motor to wheel)
- Jw = 0.02 kg·m
^{2} (moment of inertia of the load)
- R = 0.201 m (rails radius)
- Auxiliary values
- Mw (moment on the wheel produced by the motor)
- theta (angle of the wheel)
- thetaDer (derivative of theta)
- thetaDDer (second derivative of theta)
- F (tangential force applied by ball to wheel at contact point)
- Equations
- Mw = K N (u − K N thetaDer) / Rm
- Jw thetaDDer = Mw - R F

### Wheel and ball

- Parameters
- m = 0.1 kg (ball mass)
- r = 27·10
^{−3} m (rail contact radius of the ball)
- Jb = 36·10
^{−6} kg·m^{2} (moment of inertia of the ball)
- g = 9.81 m/s
^{2} (gravity acceleration constant)
- Auxiliary values
- thetaDDer (second derivative of theta)
- phi (ball angle with respect to ground)
- phiDer (derivative of phi)
- phiDDer (second derivative of phi)
- psi (ball position along rail as an angle from wheel center, 0 at top)
- psiDer (derivative of psi)
- psiDDer (second derivative of psi)
- Equations
- m (R + r) psiDDer = F + m g sin psi
- Jb phiDDer = −F r
- psi = theta + phi r / (R + r)

Eliminating phi and substituting F by its value in the first equation yields

- Equation
- m (R + r) psiDDer = Jb (R + r) (thetaDDer − psiDDer) / r
^{2} + m g sin psi
- F = Jb (R + r) (thetaDDer − psiDDer) / r
^{2}

F doesn't appear anymore in the first equation which describes the effect of the wheel on the ball; but we still need it to take into account the effect of the ball on the wheel in the first subsystem.

### State-space model

Combining these equations gives the following state-space model for the complete system. Because of the intricate influence between the wheel and the ball, to get explicit first-order differential equations, a 2x2 linear system must be solved; hence the auxilairy values a, b, c and d, coming from a 2x2 matrix, and what can be recognized as an inverse.

- States
- theta (angle of the wheel, 0 when rails are horizontal)
- thetaDer (derivative of theta)
- psi (ball position along rail as an angle from wheel center, 0 at bottom)
- psiDer (derivative of p)
- Auxiliary values (constant)
- a = Jw + R Jb (R + r) / r
^{2}
- b = −R Jb (R + r) / r
^{2}
- c = Jb (R + r) / r
^{2}
- d = m (R + r) + Jb (R + r) / r
^{2}
- Equations (non-linear model)
- theta' = thetaDer
- thetaDer' = ((u − K N thetaDer) d K N / Rm − b m g sin psi) / (a d − b c)
- psi' = psiDer
- psiDer' = ((K N thetaDer − u) c K N / Rm + a m g sin psi) / (a d − b c)

These equations aren't linear with respect to states and input. It isn't a problem for numerical simulation (at least not a difficult one: we're using a Runge-Kutta 4th order integration algorithm with a fixed maximum time step), but many controller design methods, such as those in frequency domain, require linearity.
Therefore we'll approximate sin psi ≈ psi, assuming psi is small.

This leads to the following linear model, with the same states.

- Equations (linear model)
- theta' = thetaDer
- thetaDer' = ((u − K N thetaDer) d K N / Rm − b m g psi) / (a d − b c)
- psi' = psiDer
- psiDer' = ((K N thetaDer − u) c K N / Rm + a m g psi) / (a d − b c)

### Decoupled model

In the state-space models above, the rotation of the wheel is perturbed by the ball. If we assume the ball effect is negligible, we start from the following equations:

- Equations (decoupled system)
- Jw thetaDDer = Mw
- Mw = K N (u − K N thetaDer) / Rm
- m (R + r) psiDDer = Jb (R + r) (thetaDDer − psiDDer) / r
^{2} + m g sin psi

Approximating sin psi ≈ psi, we eventually obtain the following decoupled linear state-space model:

- Equations (decoupled linear model)
- theta' = thetaDer
- thetaDer' = K N u / (Rm Jw) − K
^{2} N^{2} thetaDer / (Rm Jw)
- psi' = psiDer
- psiDer' = (Jb K N u / (Jw Rm) − Jb K
^{2} N^{2} thetaDer / (Jw Rm) + m g r^{2} psi / (R + r)) / (m r^{2} + Jb)

We have here two subsystems connected in series: the first two equations for the d.c. motor and wheel with input u and output theta, and the last two equations for the ball with input theta and output psi. The controller must be robust enough to support all these approximations, in addition to other model structural assumptions, parameter uncertainties and external perturbations.

Two remarks on the ball subsystem:

- Its input is the second derivative of theta (angular acceleration). The wheel angle theta isn't required to simulate or control the ball position. This is also true for the non-linear model.
- The second derivative of psi depends on psi itself, not on its first derivative. This results in symmetric eigenvalues: the ball subsystem is unstable for any (positive) values of the parameters, which corresponds to our expectations. A controller can remedy that.

### Controller structure

The system will be controlled with cascade controllers. The inner loop controls the wheel velocity thetaDer with a P controller whose control signal is the motor voltage u. Since the ball depends only on the wheel acceleration, a velocity steady-state error isn't harmful; no need for an integral term.
We still allow for one, though.

**Question:** why? If you have difficulty in finding an answer, continue reading and experimenting first.

The outer loop controls the ball position; its control signal is the wheel velocity, the reference value passed to the inner controller. Since the ball position is actually controlled by the wheel acceleration, an integral term is required in the controller to reject perturbations, and no derivator term should be needed. An additional state is hence required.

The controller equations are given below.

- Controller parameters
- kp1 (proportional gain for the inner loop)
- ki1 (integral gain for the inner loop)
- kp2 (proportional gain for the outer loop)
- ki2 (integral gain for the outer loop)
- Controller state
- psiInt (integrated psi)
- Auxiliary value
- thetaDerRef (reference value of the wheel velocity)
- Equations (cascade P/PD controllers)
- psiInt' = psi
- thetaDerRef = −kp2 psi − ki2 psiInt
- u = kp1 (thetaDerRef − thetaDer)

The transfer function of the two subsystems and the controller parameters can be computed with Sysquake.