Why can't I pass a measured disturbance to my state function for my nlmpc?
5 views (last 30 days)
Show older comments
I'm trying to implement a semi-active suspension in simulink that has a preview model of the road. I am passing the roads vertical velocity as a measured disturbance to the nlmpc block in simulink, I am passing four states of my quarter-car suspension model ([x_s - x_u; x_dots; x_u - x_r; x_dotu]) as measured outputs, as well as a refering input of [0; 0; 0; 0]. And the output of the nlmpc block is meant to be the current supplied to the damper. The damper I am simulating is affected by the displacement (like a spring) the rate of change of its ends, and the current being applied to it. These three inputs go into the state function to determine the damping force applied by the damper, which then is an input to the state space model shown below
A = [0 1 0 -1; -k_s/m_s -c/m_s 0 c/m_s; 0 0 0 1; k_s/m_u c/m_u -k_t/m_u -c/m_u];
B = [0 0 ; 1/m_s 0; 0 -1; -1/m_u 0];
C= [1 0 0 0; 0 1 0 0; 0 0 1 0; 0 0 0 1];
D = [0 0; 0 0; 0 0; 0 0]
In the form of x_dot = Ax + B(:,1) * damper_force + B(:,2) * road surface velocity.
So it is key that I pass the road surface velocity as a measured disturbance, and to my state function since I don't believe that my state function would be accurate if I am not inputting the road surface velocity to it (I could be mistaken, perhaps there is a better way to handle this) but this is the main part of my problem.
My code to try and setup the nlmpc object is below
% Setting up Non-Linear MPC Block
nlobj = nlmpc(4, 4, 'MV', 1, 'MD', 2);
nlobj.Ts = 0.01;
nlobj.PredictionHorizon = 15;
nlobj.ControlHorizon = 3;
nlobj.Model.StateFcn = @(x, u, md) quarterCarStateFcn(x, u, md);
nlobj.Model.OutputFcn = @(x,u,md) x;
nlobj.OV(1).Min = -0.04; % x_s - x_u
nlobj.OV(1).Max = 0.04; % x_s - x_u
nlobj.OV(3).Max = 0.01962; % x_u - x_r
nlobj.MV.Min = 0;
nlobj.MV.Max = 2; % Amps
nlobj.Weights.OutputVariables = [0.5 1 0 0];
nlobj.Weights.ManipulatedVariablesRate = 0.1; % Smooth control effort
nlobj.Model.IsContinuousTime = true;
x0 = [0; 0; 0; 0]; % Initial state
u0 = 0; % Initial manipulated variable (current to damper)
md0 = 0; % Initial measured disturbance (road velocity)
validateFcns(nlobj, x0, u0, md0)
And my state function is in a separate .m file shown below
function xdot = quarterCarStateFcn(x, u, md)
% x = [x1; x2; x3; x4] = [xs - xu; xdot_s; xu - xr; xdot_u]
% u = current to damper (I)
% md = road velocity (x_dot_r)
% Extract states
x1 = x(1); % xs - xu
x2 = x(2); % x_dot_s
x3 = x(3); % xu - xr
x4 = x(4); % x_dot_u
xdot_r = md;
% Damper model
b1 = 117.2610; c1 = 40.4957; a2 = 776.5809; k = 0.3506;
b2 = 484.3815; c2 = 167.2799; f0 = 7.1671;
a1 = b1*u + c1;
a3 = b2*u + c2;
damperForce = a1 * tanh(a2 * (x2 - x4 + k * x1)) + a3 * (x2 - x4 + k * x1) + f0;
% Parameters of the quarter car
ms = 320; % Sprung mass
mu = 40; % Unsprung mass
ks = 22000; % Suspension spring
kt = 180000; % Tire spring
c = 1000; % Parallel Spring
% Dynamics
x1dot = x2 - x4; % Derivative of x1 = xs - xu
x2dot = ( -ks*x1 - c*x2 + c*x4 + damperForce ) / ms; % Sprung mass acceleration
x3dot = x4 - xdot_r; % Derivative of x3 = xu - xr
x4dot = ( ks*x1 + c*x2 - kt*x3 - c*x4 - damperForce ) / mu; % Unsprung mass acc
xdot = [x1dot; x2dot; x3dot; x4dot];
end
When I run the code to set up the nlmpc object, I get the following error on the validateFcns line
Expecting 2 input arguments but "Model.StateFcn" appears to take 3 inputs.
I apologize since I know I wasn't very concise in my question, but because I don't have a lot of experience in simulink I suspect that my entire approach may be misguided.
I will attach a screenshot of my simulink model for reference, as well as my MATLAB files.
1 Comment
Bay Jay
on 24 Apr 2025
I hope this helps:
You can consider adding parameter to the nmpc object and pass the md as parameter . Also check how validate is configured when parameter is added.
Check these two as they show paramters
Answers (0)
See Also
Categories
Find more on Statics and Dynamics in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!