Qube-Servo 3 Pendulum Model Predictive Control

Designs and implements a model predictive control (MPC) to balance the pendulum on the Quanser Qube-Servo 3 system.

You are now following this Submission

The Quanser Qube-Servo 3, shown below, can be configured as a DC motor or an inverted pendulum plant for conducting teaching and research in control systems. The Qube-Servo 3 has an encoder to measure the position of the rotary arm on the base and another encoder to measure the position of the pendulum.
State-feedback control techniques are commonly used to balance an inverted pendulum, e.g., using Linear Quadratic Regular (LQR). State-feedback control usually follows a model-based design approach and performs well, but it doesn’t consider unmodeled uncertainties and disturbances, such as amplifier dynamics or friction of the encoder cable. If the model does not accurately represent the system, then the controller may not perform adequately or as expected.
Model Predictive Control (MPC) can be used to design the pendulum balance control and help compensate for unmodelled elements. Here, we show how to use the MathWorks® Model Predictive Control toolbox to design to do this.
Before You Run this Example
The Qube-Servo 3 model predictive control can be simulated in MATLAB Simulink using a nonlinear model of the system. The controller can also be run with the Virtual Qube-Servo 3 and the Qube-Servo 3 hardware. The following toolboxes and additional software are required depending on what you want to do:
  1. MathWorks Control System Toolbox to define state-space model in MATLAB and so on.
  2. MathWorks Model Predictive Control Toolbox. Required to design the MPC pendulum balance control.
  3. [optional] Quanser Virtual Qube-Servo 3 experiment. Only needed to run the controller with the Virtual Twin. If you don't have Quanser Interactive Labs (QLabs) software, then you can sign up for a free trial here.
  4. [optional] Quanser QUARC Real-Time Control Software is required to run the controllers with the Qube-Servo 3 hardware. If you don’t have the QUARC software, you can contact the Quanser team for a free trial.
How Does it Work?
The model predictive control (MPC) is designed using the Model Predictive Control Toolbox™ and the design process is detailed in this Quanser blog. The Qube-Servo 3 is a single-input multiple-output system. The input is the DC motor voltage, and the outputs are the rotary arm and pendulum angular positions.
The MPC designed is implemented using the MPC Controller block from the Model Predictive Control Toolbox™, as shown in the qs3_bal_mpc_sim.slx Simulink model. This model simulates the MPC control using a nonlinear model of the Qube-Servo 3 system.
How to Run the Example
Go through each section of the setup_qube3_inv_pen_mpc.mlxLive Script. The script designs the model predictive control (MPC) and shows how to
  1. Simulate the MPC using the nonlinear model
  2. Run the MPC using the QLabs Virtual Qube-Servo 3, and
  3. Run the MPC with the Qube-Servo 3 hardware.
Running the Simulation
This Simulink model that was used in the training can be used to simulate the pendulum balance RL control using the nonlinear rotary pendulum model.
  1. Open the qs3_bal_mpc_sim Simulink model.
  2. Run the Simulink model.
  3. Try changing in the initial angle of the inverted pendulum somewhere between +/- 10 deg. For example, set the ic_alpha0 to 9*pi/180 to have the initial angle start at 9 deg away from the inverted position.
The response of the rotary arm and pendulum when the pendulum starts at 9 deg from the upright balance position is shown in the Rotary Arm (deg) and Inverted Pendulum (deg) scopes. The voltage applied to the motor is shown in the Vm (V) scope.
Running MPC using the Virtual Qube-Servo 3
Run the model predictive control with the QLabs Virtual Qube-Servo 3 experiment through Simulink. If you don't have the Quanser Interactive Labs (QLabs) software and want to try it out, you can request for a free trial here.
  1. Open the Quanser Interactive Labs (QLabs) software
  2. Click on the Pendulum Workspace in the Qube 3 - Pendulum workspace to load the Virtual Qube-Servo 3, as shown above. If you have any issues running the software, please go to the QLabs support page.
  3. Open the qs3_bal_mpc_virtual Simulink model that interacts with the Virtual Qube-Servo 3 Pendulum.
  4. Run the Simulink model.
  5. Click on the Lift pendulum button in the top-right corner to bring the pendulum up to the inverted position. The balance control will engage once the pendulum reaches +/- 10 deg of the vertical.
The scopes below show the response when the pendulum is brought up from the downward position, and the balance control is engaged on the virtual Qube-Servo 3.
Running MPC using the Qube-Servo 3 Hardware
The Quanser QUARC Real-Time Control Software is needed to interface to the Qube-Servo 3 hardware through Simulink. QUARC creates an executable for 64-bit Windows using code generation from Simulink Coder and MATLAB Coder. The QUARC executable is then run through the Simulink interface in External mode.
Follow these instructions to use Simulink and the QUARC Real-Time Control Software to run the MPC balance control using the Qube-Servo 3 hardware.
  1. Connect the Qube-Servo 3 to the PC/laptop USB port.
  2. Make sure the Qube-Servo 3 is powered and Power LED is lit.
  3. Open the qs3_bal_mpc.slx Simulink model that interacts with the Qube-Servo 3.
  4. Click on the Monitor and Tune button in the HARDWARE or QUARC tab of the Simulink toolbar to generate and run the QUARC real-time executable.
  5. Manually bring up the pendulum to the vertical position. Immediately release the pendulum once the controller engages (i.e., when it's within +/- 10 deg of vertical).
  6. Click on the Stop button to stop running the QUARC controller.
The sample response when MPC is run by QUARC are shown in the scopes below. The response on the physical hardware is very similar to the virtual twin.

Cite As

Quanser (2026). Qube-Servo 3 Pendulum Model Predictive Control (https://ch.mathworks.com/matlabcentral/fileexchange/184078-qube-servo-3-pendulum-model-predictive-control), MATLAB Central File Exchange. Retrieved .

General Information

MATLAB Release Compatibility

  • Compatible with R2025b and later releases

Platform Compatibility

  • Windows
  • macOS
  • Linux
Version Published Release Notes Action
1.0.0