Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

need help in simulink embedded matlab function

8 views
Skip to first unread message

Constantine Ong

unread,
Mar 30, 2011, 1:26:05 PM3/30/11
to
Dear all,
My problem is to navigate a ship from a starting point to its target. I am using matlab simulink where a PID controller and a plant model is used. The equations of the ship model in discrete form are as follow:-

instantaneous x:
x(t+1)= x(t) + delta*v*sin(theta(t));
instantaneous y:
y(t+1)= y(t) + delta*v*cos(theta(t));
instantaneous heading angle:
theta(t+1)= theta(t) + delta*rate(t); // where rate(t) is actually dtheta/dt
actual turning rate of the ship:
rate(t+1)= rate(t) + delta*(r(t)-rate(t))/T; // r(t)-rate(t)=error(t)
assume delta = 2, T=5, v is the velocity 30m/s.
r(t) is the desired turning rate of the ship, |r| < 15 degreees/sec

target position:
xtarget= 800;
ytarget= 1250;
theta1(t)= atan((xg-x(t))/(yg-y(t)));
error(t)=theta1-theta(t)

the error is feedback to the pid controller where the transfer function is the first order Nomoto's Model, 1/s(sT+1), and the value of Kp=0.406, KI=0.094, Kd=0.
The output of the PID controller is r(t).

I am using a matlab embedded function block in order to get the output x,y,psi,rate based on the input r. My problem is I don't know how to write all these equation in time increment form so that after each loop the number of "t" will increase 1. My initial condition is x=0, y=800 and my target is x=1650, y=1250, theta=0, rate=0.

function [x,y,pos,theta,rate] = ship(r)
% This block supports an embeddable subset of the MATLAB language.
% See the help menu for details.

v = 30; %m/s
delta = 2;%s
T=5; %s
t=1;
theta=nan(100,1);
rate=nan(100,1);
x=nan(100,1);
y=nan(100,1);
x(t,:)=0;
y(t,:)=800;
theta(t,:)=0;
rate(t,:)=0;
pos = nan(100,2);
pos(1,:) = [x(t),y(t)];

while t>0;
x(t+1,:)= x(t,:) + delta*v*sin(theta(t));
y(t+1,:)= y(t,:) + delta*v*cos(theta(t));
theta(t+1,:)= theta(t,:)+ delta*rate(t);
rate(t+1,:)= rate(t,:) + delta*(r(t)-rate(t))/T;
t=t+1;
end

I really need somebody help me in matlab coding. Or is there any other alternatives to navigate the ship or to implement the equation of the motion?? Thank you for your help.

Phil Goddard

unread,
Mar 30, 2011, 4:32:02 PM3/30/11
to
You should read some of the early sections of the Simulink User's Guide to obtain an understanding of how Simulink works, and look at some of the (discrete time) example models.

Fundamentally the Simulink solvers/integrators are the mechanism for stepping your dynamic equations through time -- you do not do that yourself in a loop as you seems to be trying to do.
And for the equations you give, using an Embedded MATLAB Function block is not the right approach.

You should be using some (discrete) unit delay blocks to define your states and using various blocks from the Math library to implement your equations.
Start by at least looking at the doc for the unit delay block and understanding how to us it to implement discrete equations like those you show.

Phil.

Constantine Ong

unread,
Apr 1, 2011, 10:16:04 PM4/1/11
to
"Phil Goddard" <philN...@goddardconsulting.ca> wrote in message <in0402$hmb$1...@fred.mathworks.com>...

thanks Phil. Appreciate your advice. ^^

0 new messages