CasADi is a powerful open-source tool for nonlinear optimization. It can be used with MATLAB/Octave, Python, or C++, with the bulk of the available resources referencing the former two options. This post series is intended to show a possible method of developing a simulation for an example system controlled by Nonlinear Model Predictive Control (NMPC) using CasADi and Python.
In this post, we will look at determining the system equations of a robot with two degrees of freedom in stace-space form. These state-space equations can be used for simulation and for developing a nonlinear model predictive controller. For some transformation tasks, Python with SymPy will be utilized and an alternative using the MATLAB Symbolic Math Toolbox will be shown.
During the following, we are considering a robot with two links, two masses at the end of the links, and motors applying torque as control input at the joints. A diagram of the robot is shown below.
This post shows one way to linearize a nonlinear state equation at a steady state setpoint in MATLAB. It is assumed that a function ode.m exists in which the state equation is implemented:
function dx = ode(t, x, u) % example ODE dx1 = tan(x(4)) * x(2) + 2*u(2) - 1; dx2 = x(1) - sin(x(2)); dx3 = 13 * x(4) + u(1) + 1; dx4 = 2*x(1); dx = [dx1; dx2; dx3; dx4]; end
This is a MATLAB implementation of the Wagner–Fischer algorithm.
- dSPACE ControlDesk can export measurements to mdf4 files or mat files
- mat file exports can be converted to timeseries with this MATLAB script
- The timeseries can then be imported into Simulink with the ‘From Workspace’ blog
- Multiple signals can be imported at once
>> import_dspace_mat_to_simulink_ts --Imported dSPACE mat file C:\Users\Nicolai\Nextcloud\foo.mat into workspace variable dsp_ts --Time: 0.000000 s to 42.399572 s, 423980 steps --2 variables are in dsp_ts: 'In1' 'In1_1'
Function 1 (e.g. input signal/Eingangssignal):
Function 2 (e.g. impulse response/Stoßantwort):