EE 222 Course Project
EE 222 Course Project
Motivation: In this class, you have learned a few techniques for nonlinear control. This
project is an opportunity to put them to work on hardware and design your own controller!
The ball-and-beam is a classic example of a control system. In this project, you’ll design
a controller for this system that tracks certain trajectories and satisfies other performance
criteria (like minimizing control cost and safety violations).
Modeling: The ball and beam setup consists of a track on which the metal ball is free to
roll. One side of the beam is attached to a lever arm that can be coupled to the load gear
of the rotary servo unit. By controlling the position of the servo, the beam angle can be
adjusted to balance the ball to a desired position. A schematic of the setup is below:
𝑧 𝐹" + 𝐹#
𝐿 sin 𝛼
𝑟!
≈ 𝑟! sin 𝜃
𝑚𝑔 𝜃
The state of the ball and beam system are given by x = [z, ż, θ, θ̇]⊤ , where z is the deviation
of the ball from the center of the beam, and θ is the angle of the arm of the servo motor,
made with the horizontal axis.
The dynamics of this system can be derived from the kinematics of the links, the equation
of motion of the ball, and the motor dynamics of the servo motor. The force applied to
the ball consists of the gravity force, centrifugal force Fc = m L2 − z α̇2 , and the rolling
resistance Fr = Jr2b z̈, where m is the ball mass, L is the beam length, rb is the ball radius,
b
and Jb = 25 mrb2 the moment of inertia of the ball. From this, the equation of motion of the
ball is given as
L Jb
mz̈ = mg sin α − m − z α̇2 − 2 z̈. (1)
2 rb
Here, we can assume that the tilt angle of the beam α is small enough so that α ≈ sin α.
From the kinematics relation L sin α ≈ rg sin θ, where rg is the servo arm length, we can
replace sin α and α̇ with
rg rg
sin α = sin θ, α̇ = cos θ θ̇. (2)
L L
Next, the motor dynamics is given as
τ θ̈ = −θ̇ + K · V (3)
where V is the voltage applied to the motor, τ is the delay constant, and K is the motor
constant.
ẋ1 = x2
5g rg 5 L rg 2 2
ẋ2 = sin x3 − − x1 x4 cos2 x3
7 L 7 2 L
ẋ3 = x4
x4 K
ẋ4 = − + u (4)
τ τ
where u = V is defined as the control input to the system. We give a table of the relevant
model parameters below:
rg 0.0254 m
L 0.4255 m
g 9.81 m/s2
K 1.5 rad/sV
τ 0.025 s
Finally, note that you do not have measurement of the full state. You only have measurement
of the ball position z from the ball position sensor that outputs a voltage signal proportional
to z, and the servo angle θ from the encoder attached to the motor. If you wish to use the
full state information, consider designing your own state estimator or observers.
2
For more details of the system, please refer to [1]. Note that the verified dynamics might
definitely have some modeling errors due to the approximations, forces that are not consid-
ered in the equation of motion, and many other factors. You will be able to see the effect of
these modeling errors, or model-plant mismatch, when your controller gives different results
in the simulation and in the actual experiment.
Problem Definition: You goal is to design a controller that computes the input voltage to
the servo motor V that can drive the ball position to a desired trajectory, given as a function
in time. The main objective of this controller is to reduce the tracking error and the energy
spent by the motor as small as possible. While doing so, you also want to make sure that
the controller is operated in a safe region; the main safety constraint considered here is that
the ball does not escape the physical limit of the beam. We next describe the metrics that
capture the tracking error, energy consumption, and safety constraint violation.
where zr (t) is the desired ball position trajectory and ∆t is the sampling time of the
trajectory data.
3. Safety constraint: The physical limit of the position of the ball on the beam is given
as −0.2m ≤ z ≤ 0.2m. At each side of the beam, a stopper makes sure that the ball
does not escape the beam. Ideally, we want the ball to avoid collision with the stoppers
because the impact will make the system unstable and unpredictable (as your system
equations do not model this effect). Also, the servo motor has a physical limit on its
3
angle range. Therefore, we consider the system to be unsafe when the state satisfies
one of the following conditions:
The exception to this condition is allowed for the first one second of the trajectory, to
allow the ball to start from the boundary at its initial state. We consider the following
indicator
1 ∃t, z(t) unsafe,
Jsaf ety = , (10)
0 else
which is 1 if the trajectory is unsafe and 0 if the trajectory is safe.
For each experiment run, given the recorded data of {z(tk )}N N
k=0 and {u(tk )}k=0 , Jtracking ,
Jenergy and Jsaf ety can be evaluated. Your goal is to minimize the score of your controller,
computed as:
Jscore = wt Jtracking + we Jenergy + ws Jsaf ety , (11)
where wt , we , ws are the weight of each cost terms. When you report your data to the
leaderboard, the leader board will save the best (minimum) score among your reported runs,
and use it as your final score. (Instructions on how to report your data will be announced
next week with the experiment guideline.) Students who achieve better (lower) scores will
receive more credits for the course project. You will have to write a short report on what
controllers you tried when the competition ends.
Code Instructions:
3. Run setup.m or add the repository and the subfolders to the matlab path.
4. You only need to change one file for developing your controller: studentController-
Interface.m.
Development guideline:
4
function stepImpl is used to compute the control at each instant in time. For each
timestep, this function takes the ball position (p ball) and the servo angle (theta)
measurement, and decides the voltage applied to the motor (V servo). Feel free to do
whatever you want inside the class, but make sure you do not modify the signature
of the two functions setupImpl and stepImpl. Call the function get ref traj.m
to get the reference trajectory zr (t) at a specific time instance. We gave you an
implementation of a very simple proportional feedback controller for your reference.
For more details, please read the instructions in the code.
2. Test the performance of your controller by running run matlab ball and beam.m.
Play around with different reference trajectory profile under various values of amplitude
and period by modifying get ref traj.m, and debug and improve the controller.
3. Finally, before the experiment, make sure that your controller can run in the Simulink.
This is very important to make sure that your controller can interface with the real
hardware for experiments. Run run simulink ball and beam.m to test this. If
there are issues in your implementation, building the code will fail and it will output
errors in the Simulink Diagnostic Viewer.
4. Once your controller works well in the simulation, be ready to test it on the real
hardware! (Instructions for the experiment and the lab sessions will be an-
nounced next week.)
5. Debug your controller on the real hardware. When the reference trajectory for the
competition is released, you will be able to report the score of the controller to the
leader board.
Note: The simulation result from run simulink ball and beam.m and from run matlab
ball and beam.m may be different from each other, due to the difference in the used mod-
els. The dynamics used in latter is implemented in ball and beam dynamics.m, which is
the implementation of (4).
References
[1] Quanser. Ball and beam - quanser. https://www.quanser.com/products/
ball-and-beam/. Accessed: 2022-04-12.