rurouniwallace
rurouniwallace

Reputation: 2087

s-function direct feedthrough

Note: This is my first time using a MATLAB S-function, and I've only used Simulink a handful of times.

When I try to use my input variables in the S-function, inp, they return NaN on flag 3. The input to the system is three simple unity step functions, connected to the system via bus creator. When trying to simulate the system, I get an error message--I only found that they are NaN by doing a test print on inp. Any ideas?


Details:
I am simulating a Segway as a class project. I'm simulating the actuator (a DC motor) and the plant (the Segway itself) as separate S-function blocks in Simulink. The motor takes three inputs: a voltage, the velocity of the Segway, and the angular velocity Segway with respect to its pitch axis (due to back emf). The output is the armature current.

This is a simple, linear, non-differential system. For that reason, there are no states. The only computation that is done is a feed-through from the inputs to the output. The code is as follows:

function [sys,X0]=nl_pend(time,state,inp,FLAG,ICs);
% actuator parameters:
% R_m: motor armature resistance
% K_b: motor back emf constant
% K_t: motor torque constant
% r: radius of the wheel
global R_m K_b K_t r;

if FLAG == 0,   % initialize system parameters and states
% Call file that initializes parameters
    segway_dcmotor_pars;
% This system has:
%  0 states (theta,thetatheta_dot),
%  3 input  (v,x_dot,theta_dot),
%  1 outputs (i)
nx = 0; % # of states
nu = 3; % # of inputs
ny = 1; % # of outputs
sys = [nx; 0; ny; nu; 0; 0];
% Initial Conditions  (will be passed as and argument)
X0 = ICs;
elseif FLAG == 1, % states derivatives
    %blank
elseif FLAG == 3,  % system outputs
    %inp(1) = motor voltage
    %inp(2) = segway velocity
    %inp(3) = segway pitch angular velocity
    display( inp );
    y(1) = (1/R_m)*inp(1) - (K_b/(r*R_m))*inp(2) + (K_b/R_m)*inp(3);
    sys = y;
else,
    sys = [];
end

Upvotes: 0

Views: 1301

Answers (1)

Raphgut
Raphgut

Reputation: 11

As you mentioned, it is direct feedthrough, so you have to set

sys(6)=1 in flag==0... 

Upvotes: 1

Related Questions