Example: Modeling the Ball and Beam Experiment in Simulink

Problem Setup and System Equations
Building the Simulink Model
Open-Loop Response
Extracting the Model into MATLAB
Constructing a Lead Compensator Controller
Closed-Loop Response

Problem Setup

A ball is placed on a beam, see figure below, where it is allowed to roll with 1 degree of freedom along the length of the beam. A lever arm is attached to the beam at one end and a servo gear at the other. As the servo gear turns by an angle theta, the lever changes the angle of the beam by alpha. When the angle is changed from the horizontal position, gravity causes the ball to roll along the beam. A controller will be designed for this system so that the ball's position can be manipulated.

For this problem, we will assume that the ball rolls without slipping and friction between the beam and ball is negligible. The constants and variab for this example are defined as follows:

M mass of the ball 0.11 kg
R radius of the ball 0.015 m
d lever arm offset 0.03 m
g gravitational acceleration 9.8 m/s^2
L length of the beam 1.0 m
J ball's moment of inertia 9.99e-6 kgm^2
r ball position coordinate
alpha beam angle coordinate
theta servo gear angle

The design criteria for this problem are:

The second derivative of the input angle alpha actually affects the second derivative of r. However, we will ignore this contribution. The Lagrangian equation of motion for the ball is then given by the following:

The beam angle (alpha) can be expressed in terms of the angle of the gear (theta).

Building the Model in Simulink

In this example, rather than express all the forces and geometric constraints (which is difficult to model in Simulink for dynamic systems with constraints) we will model the nonlinear Lagrangian equation of motion directly. This equation gives d/dt(r) as a function of the state and input variab, r, d/dt(r), alpha, and d/dt(alpha). We will make use of the Nonlinear Function Block to express this function. First, we must express the derivatives of the output, r.

Now, we will insert the function which takes the vector [r d/dt(r) alpha d/dt(alpha)] and returns d/dt(r).

Now, we will begin to construct the function input vector u by feeding back the state signals from the integrators and forming a vector from them with a Mux block.
Now we will construct the signals alpha and d/dt(alpha) from the input theta.
Save your model as "ball.mdl". You can download ours here. Open Loop Response To generate the open-loop response, it is first necessary to contain this model in a subsystem block.
Before obtaining a step response, we must set the physical parameters Enter the following commands at the MATLAB prompt. We are now ready to run the simulation. If you like, you can download our version of the open-loop system here. Start the simulation by selecting Start from the Simulation menu (or hit Ctrl-t). When the simulation is finished, open the Scope by double clicking on it and hit the Scope's autoscale button. You will see the following response.
From this plot it is clear that the system is unstable in open-loop causing the ball to roll right off the end of the beam. Therefore, some method of controlling the ball's position in this system is required. Later in this tutorial, we will implement a lead compensator.

Extracting the Model into MATLAB

The Simulink model can be extracted into an equivalent state-space or transfer function model in MATLAB. This is done through the use of In and Out Connection blocks and the MATLAB function linmod.

To extract a model, it is necessary to start with a model file with inputs and outputs defined as In and Out blocks. Earlier in this tutorial this was done, and the file was saved as ball.mdl. In this model, one input, theta (the input crank angle) and one output, r (ball position), were defined.

At the MATLAB prompt, enter the following commands

You will see the following output providing the open-loop model of the system. We can verify this model by obtaining an open-loop step response. Enter the following command at the MATLAB prompt: You will see the following open-loop response:

Building a Lead Compensator Controller

In the CTMS Example: Solution to the Ball & Beam Problem Using Root Locus Method a lead compensator was designed with a zero at -0.01 and a pole at -5, with a gain of 37.1. We will now construct this controller in Simulink.

You can download our version of the closed-loop model here.

Closed-Loop Response

Start the simulation in Simulink. Open the scope window and hit the Autoscale button. You should see the following response.


Simulink Examples
Cruise Control | Motor Speed | Motor Position | Bus Suspension | Inverted Pendulum | Pitch Controller | Ball and Beam

Ball and Beam Examples
Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink

Tutorials
MATLAB Basics | MATLAB Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics | Simulink Modeling | Examples