MATLAB/Simulink interface¶
The MATLAB and Simulink interfaces make it possible to use and control a precompiled solver from MATLAB/Simulink.
Dependencies¶
To make use of this functionality, the following are required:
- MATLAB (tested using version 7.8.0 (R2009a))
- a C compiler (tested using GCC version 4.4 (gcc4.4))
Creating the interfaces¶
For basic usage, the procedure consist on writting the system module, generating and compiling the code, and finally using the interface.
Create the system module¶
The sytem module can also be created in MATLAB. Simply define the required
matrices (e.g. Ac
, Bc
, Q
, u_lb
, e_lb
) and save them in a
mat file. In this example, we will assume the sys_motor.mat
system
module was created, based on the MPC setup described on A basic MPC problem.
Note: to read mat files, SciPy is required.
Generate the code¶
At first you have to generate the code that represents the MATLAB interface.
Simply add the option matlab=True
to the code generation function. The following Python commands will generate the
MATLAB/Simulink interfaces for the sys_motor.mat
example:
import muaompc
mpc = muaompc.ltidt.setup_mpc_problem('sys_motor.mat')
mpc.generate_c_files(matlab=True)
The generated cmpc
directory now contains two new directories called
matlab
and simulink
besides the usual C files. It contains all the code of the MATLAB
and Simulink interfaces. A detailed description follows.
Using the MATLAB interface¶
Compiling the interface¶
Once the code the interfaces have been created,
the next step is to compile the MATLAB interface. Start MATLAB, set the
generated cmpc/matlab
directory as the working directory and call
make
, which will execute the make.m
script. For example,
assuming you are in the directory where you generated the data,
inside MATLAB type:
>> cd cmpc/matlab
>> make
After this the compiled code is put inside the @mpc_ctl
directory.
Adding the interface to MATLAB’s path¶
The last step is to add the matlab
directory to the PATH environment in
MATLAB.
Using the interface¶
Now you can use the interface which is encapsulated in a class called
mpc_ctl
, which represents the MPC controller. Simply declare an
instance of that class, which we usually call ctl
(controller).
For example:
ctl = mpc_ctl; % create an instance of the class
ctl.conf.in_iter = 10; % configure the algorithm
x = [0.1; -0.5]; % current state
ctl.form_qp(x) % form the QP for the current state
qpx = ctl.qpx; % qpx contains the QP in standard form
u = quadprog(qpx.HoL, qpx.gxoL, [], [], [], [], qpx.u_lb, qpx.u_ub);
ctl.solve_problem(x); % solve the MPC problem for current state
ctl.u_opt % display the computed input sequence
norm(u - ctl.u_opt) % ctl.u_opt is the MPC approximation of u
Example¶
The complete example can be found under the examples/ltidt/matlab_interface
folder.
Using the Simulink interface¶
It is possible to use generated code in Simulink to build models for simulation and real time execution on targets which are supported with MATLAB Coder, including compilation for embedded targets.
The Simulink feature that allows to do it is called system functions (S-Functions). S-functions provide a way to execute C code as a Simulink block.
Compiling the interface¶
As explained above, to generate preconfigured Simulink model
you need to pass matlab=True
parameter
to the generator call. The model is placed in cmpc/simulink
folder.
To run the model follow the next steps:
- In MATLAB change the current folder to
cmpc/simulink
. - Open
cmpc/simulink/simulinkmpc.mdl
in Simulink. - Double click the
S-Function Builder
block to open the block properties window. - Click
Build
to build the S-Function.
Now the model is ready to be extended and used in your design.
Example¶
The example system simulink_example.mdl
is
located in the folder examples/ltidt/matlab_interface
.
It shows a sample
use of a MPC controller. To run the example follow the next steps:
- Set the current MATLAB folder to
examples/ltidt/matlab_interface
. - Type
open_simulink_example
in the MATLAB console. - Press the
Build
button in the block properties window which will appear on the screen. - Run the simulation.
The example includes the functionality of solving the control problem and prediction of the next state of the plant. The model can operate using closed-loop and open-loop mode. Also the model shows how to check the execution speed on the device.