From Symbolic System to Simulink Block

1 次查看(过去 30 天)
N/A
N/A 2022-5-22
回答: Sayan 2023-12-5
Hello everyone,
I am trying without succes to convert a set of Equations of Motion (EoMs) to a Simulink block.
The EoMs have been derived symbolically using Matlab symbolic Toolbox.
In order to make my problem clear I add below the code of a simplified problem, consisting in the EoMs of a 3 Degrees of Freedom (DoFs) system (a body in the 2D cartesian space) with only 1 DoF controlled by a torque.
Once the EoMs have been computed, I would like to generate a block in Simulink that implments them.
I tried using the function matlabFunctionBlock but it does not work. The reason (I believe) is that the EoMs are written as:
% EoM_i = diff(var(t), t, t)) == in(t)
I would like to know if there is a workaround to use the Equations in the way I defined them or an alternative way to solve the issue.
Thank you very much!
clear all
close all
clc
%% State Variables
% Position [m] and Orientation [rad] of the base
syms x(t) y(t) phi(t) dx(t) dy(t) dphi(t) ddx(t) ddy(t) ddphi(t)
% Base Torque [Nm]
syms TAU(t)
% Base Forces [N] there are no external forces acting in the syst.
% Mass [kg]
syms m0 positive
% Geometry arms
syms l1 l2 positive
% Geometry of Robot
syms a b positive
% Inertia Moments in Principal Axes Frame {base-frame}
syms I0 positive
% Lagrangian variables
syms L K V
%% Position and Velocity of the Masses in inertial frame {N}
% Position of Mass 0 (base)
x0 = x; y0 = y;
xx0 = [x0, y0].';
% Velocities [m/s]
vv0 = diff(xx0,t);
%% Moments of Inertia in {base-frame} [kg*m^2]
% Total Inertia
Itot = I0;
%% Lagrangian
% Kinetic Energy
K = 0.5*(m0*(vv0.'*vv0) + Itot*dphi^2);
% Substitution of variables
K = subs(K, diff(x, t), dx);
K = subs(K, diff(y, t), dy);
K = subs(K, diff(phi, t), dphi);
% Potential Energy
V = 0;
% Lagrangian
L = K - V;
%% Partial Derivatives of the Lagrangian
% Partial wrt state
pL_x = simplify(diff(L, x));
pL_y = simplify(diff(L, y));
pL_phi = simplify(diff(L, phi));
% Partial wrt state (time) derivatives
pL_dx = simplify(diff(L, dx));
pL_dy = simplify(diff(L, dy));
pL_dphi = simplify(diff(L, dphi));
%% Generalized Equations of Motion:
% Position of CoM
EoM1 = diff(pL_dx, t) - pL_x == 0; % null ext. force
EoM2 = diff(pL_dy, t) - pL_y == 0; % null ext. force
% Orientation of principal frame
EoM3 = diff(pL_dphi, t) - pL_phi == TAU; % actuated base
%% Substitution of Variables in EoMs:
% EoM1
EoM1 = subs(EoM1, dx, diff(x, t));
EoM1 = subs(EoM1, ddx, diff(x, t, t));
EoM1 = subs(EoM1, dy, diff(y, t));
EoM1 = subs(EoM1, ddy, diff(y, t, t));
EoM1 = subs(EoM1, dphi, diff(phi, t));
EoM1 = subs(EoM1, ddphi, diff(phi, t, t));
% EoM2
EoM2 = subs(EoM2, dx, diff(x, t));
EoM2 = subs(EoM2, ddx, diff(x, t, t));
EoM2 = subs(EoM2, dy, diff(y, t));
EoM2 = subs(EoM2, ddy, diff(y, t, t));
EoM2 = subs(EoM2, dphi, diff(phi, t));
EoM2 = subs(EoM2, ddphi, diff(phi, t, t));
% EoM3
EoM3 = subs(EoM3, dx, diff(x, t));
EoM3 = subs(EoM3, ddx, diff(x, t, t));
EoM3 = subs(EoM3, dy, diff(y, t));
EoM3 = subs(EoM3, ddy, diff(y, t, t));
EoM3 = subs(EoM3, dphi, diff(phi, t));
EoM3 = subs(EoM3, ddphi, diff(phi, t, t));
%% Substitution of geometric and mass values
% Lentghs [m]
a = 2;
b = 1;
l1 = 0.5;
l2 = 0.25;
% Masses[Kg]:
m0 = 100;
m1 = 2;
m2 = 1;
% Inertia of cuboid [Wikipedia: https://en.wikipedia.org/wiki/List_of_moments_of_inertia#Moments_of_inertia]
I0 = (1/12)*m0*4*(a^2*b^2); % 4* due to a,b being HALF of width and depth
% Substitution in EoMs
EoM1 = subs(EoM1);
EoM2 = subs(EoM2);
EoM3 = subs(EoM3);
% System of Non Linear EoMs:
EoMs = [EoM1, EoM2, EoM3].';
%% Simulink Block: Initialization
new_system('my_simple_sys')
%% Simulink Block
matlabFunctionBlock('my_simple_sys/vdp', EoMs, 'vars', {'t', 'x', 'y', 'phi', 'TAU'})
Warning: Function 'x' not verified to be a valid MATLAB function.
Warning: Function 'y' not verified to be a valid MATLAB function.
Warning: Function 'phi' not verified to be a valid MATLAB function.
Warning: Function 'TAU' not verified to be a valid MATLAB function.

回答(1 个)

Sayan
Sayan 2023-12-5
Hi N/A,
I understand from your issue that you need to create a Simulink block that simulates the behavior of your defined Equations of Motion (EoMs). It cannot be done directly from the code that you have written. However,you can do it by implementing the same equations directly in Simulink. You can find the following steps to achieve the same:
  • Create a Subsystem by using the Subsystem block (available at Simulink / Ports & Subsystems).
  • Use the same number of Inport blocks (available at Simulink / Ports & Subsystems) and Outport blocks (available at Simulink / Ports & Subsystems) in the subsystem as the number of inputs and outputs for the block to implement the equations.
  • Implement the equations in the Subsystem using various blocks of Simulink.
  • Create a "Mask" for the subsystem and define the parameters and their properties.
  • Create a custom library and put your block in the library for future use.
You can find further information on "Create Custom Library" , "Create Block Masks" in the following documentations.
Hope this helps in resolving the issue.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by