Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
need help in looping, motion control

Subject: need help in looping, motion control

From: Constantine Ong

Date: 7 Apr, 2011 16:42:05

Message: 1 of 3

Dear all,

I have writen a code for simple ship control system where a ship can move from an initial point to the target point. When there is only 1 target, my code run smoothly. However, I have to make it in two target, where the ship reach target 1 then continue to move to next target. My code is as below:-

clc
clear

%Parameter Constant
T=5;
v=10;
delta=0.5;

%Position of Target
xg=1650;
yg=1250;

%Gain of PID controller
ki=0.094;
kp=0.406;

for t=1:2000
    if t==1
        x(t)=0;
        y(t)=500;
        theta(t)=0;
        turning(t)=0;
        si=atan((xg-x(t))/(yg-y(t)));
        err(t)=si-theta(t);
        u(t)=ki*err(t)*delta/2;
        r=kp*err(t)+u(t);
    
    else if t>1
            x(t)=x(t-1)+delta*v*sin(theta(t-1));
            y(t)=y(t-1)+delta*v*cos(theta(t-1));
            theta(t)=theta(t-1)+delta*turning(t-1);
            turning(t)=turning(t-1)+(delta*(r-turning(t-1)))/T;
            si=atan((xg-x(t))/(yg-y(t)));
            err(t)=si-theta(t);
            u(t)=ki*(err(t)+err(t-1))*delta/2;
            r=kp*err(t)+u(t);
        end
    end
    
    figure(1)
    plot (0,500,'x',1650,1250,'x',x(t),y(t),'-')
    axis([0 2000 0 1500])
    hold on

    if x(t)>1650
       disp('Simulation End')
       break
    end
end

This code run smoothly. If the target yg less than initial y , then the equation
            x(t)=x(t-1)+delta*v*sin(theta(t-1));
            y(t)=y(t-1)+delta*v*cos(theta(t-1));
becomes
            x(t)=x(t-1)-delta*v*sin(theta(t-1));
            y(t)=y(t-1)-delta*v*cos(theta(t-1));
position xg (x of target) is always greater than x.

I wish to set my first target at xg1=800, yg1=1200. My second target at xg2=1650,yg2=1250, it is possible to modified my code??
I have try to add while loop but it juz didn't go right. Can someone help me in this problem??? thank you very much for your help.

Subject: need help in looping, motion control

From: Florin Neacsu

Date: 7 Apr, 2011 17:23:04

Message: 2 of 3

"Constantine Ong" wrote in message <inkpgt$ppk$1@fred.mathworks.com>...
> Dear all,
>
> I have writen a code for simple ship control system where a ship can move from an initial point to the target point. When there is only 1 target, my code run smoothly. However, I have to make it in two target, where the ship reach target 1 then continue to move to next target. My code is as below:-
>
> clc
> clear
>
> %Parameter Constant
> T=5;
> v=10;
> delta=0.5;
>
> %Position of Target
> xg=1650;
> yg=1250;
>
> %Gain of PID controller
> ki=0.094;
> kp=0.406;
>
> for t=1:2000
> if t==1
> x(t)=0;
> y(t)=500;
> theta(t)=0;
> turning(t)=0;
> si=atan((xg-x(t))/(yg-y(t)));
> err(t)=si-theta(t);
> u(t)=ki*err(t)*delta/2;
> r=kp*err(t)+u(t);
>
> else if t>1
> x(t)=x(t-1)+delta*v*sin(theta(t-1));
> y(t)=y(t-1)+delta*v*cos(theta(t-1));
> theta(t)=theta(t-1)+delta*turning(t-1);
> turning(t)=turning(t-1)+(delta*(r-turning(t-1)))/T;
> si=atan((xg-x(t))/(yg-y(t)));
> err(t)=si-theta(t);
> u(t)=ki*(err(t)+err(t-1))*delta/2;
> r=kp*err(t)+u(t);
> end
> end
>
> figure(1)
> plot (0,500,'x',1650,1250,'x',x(t),y(t),'-')
> axis([0 2000 0 1500])
> hold on
>
> if x(t)>1650
> disp('Simulation End')
> break
> end
> end
>
> This code run smoothly. If the target yg less than initial y , then the equation
> x(t)=x(t-1)+delta*v*sin(theta(t-1));
> y(t)=y(t-1)+delta*v*cos(theta(t-1));
> becomes
> x(t)=x(t-1)-delta*v*sin(theta(t-1));
> y(t)=y(t-1)-delta*v*cos(theta(t-1));
> position xg (x of target) is always greater than x.
>
> I wish to set my first target at xg1=800, yg1=1200. My second target at xg2=1650,yg2=1250, it is possible to modified my code??
> I have try to add while loop but it juz didn't go right. Can someone help me in this problem??? thank you very much for your help.


Hi,

You should have a look at documents dealing with pre-allocation in matlab. You were increasing the size of 4 or 5 vectors inside a loop (it's not a good habit). Also you should check it this is what you really want : if your target has an x coord < x coord of initial point the code won't work. The same for y coord. And even if they both are superior, there is a 'strange' (maybe it's what you want) behavior for
target=[0,500;800,1200;1650,1250;1800,1350].

Here is the code with the slight modifications. Hope it helps. Regards, Florin






clc
clear

%Parameter Constant
T=5;
v=10;
delta=0.5;



%Gain of PID controller
ki=0.094;
kp=0.406;

%Position of Target
target=[0,500;800,1200;1650,1250;1800,1500];

finalStep=2000;

for ii=2:size(target,1)
    startx=target(ii-1,1);
    starty=target(ii-1,2);
    xg=target(ii,1);
    yg=target(ii,2);
    
    
    x=zeros(1,finalStep);
    y=zeros(1,finalStep);
    theta=zeros(1,finalStep);
    turning=zeros(1,finalStep);
    err=zeros(1,finalStep);
    u=zeros(1,finalStep);

    
    for t=1:finalStep
        if t==1
            x(t)=startx;
            y(t)=starty;
            theta(t)=0;
            turning(t)=0;
            si=atan((xg-x(t))/(yg-y(t)));
            err(t)=si-theta(t);
            u(t)=ki*err(t)*delta/2;
            r=kp*err(t)+u(t);

        else if t>1
                x(t)=x(t-1)+delta*v*sin(theta(t-1));
                y(t)=y(t-1)+delta*v*cos(theta(t-1));
                theta(t)=theta(t-1)+delta*turning(t-1);
                turning(t)=turning(t-1)+(delta*(r-turning(t-1)))/T;
                si=atan((xg-x(t))/(yg-y(t)));
                err(t)=si-theta(t);
                u(t)=ki*(err(t)+err(t-1))*delta/2;
                r=kp*err(t)+u(t);
            end
        end

        figure(1)
        plot (startx,starty,'x',xg,yg,'x',x(t),y(t),'-')
        axis([0 2000 0 1500])
        hold on

        if x(t)>xg
           disp('Simulation End')
           break
        end
    end
end

Subject: need help in looping, motion control

From: Constantine Ong

Date: 8 Apr, 2011 01:06:05

Message: 3 of 3

Dear Florin, thanks for your advice and helping me to modify my code. Actually I am quite new to matlab so I will try to improve my skill in programming.....
From your code, if I change my first target to x=800, y=1500, then the following target is x=1650,y=1250, the code juz didn't run well. In my project, I actually have to show the ship can turn in another angle, which will approximately resulted in a sine wave form. So in my original code, if i change my starting point to (800,1500) and final target to (1650, 1250) and change the
                x(t)=x(t-1)+delta*v*sin(theta(t-1));
                y(t)=y(t-1)+delta*v*cos(theta(t-1));
to
                x(t)=x(t-1)-delta*v*sin(theta(t-1));
                y(t)=y(t-1)-delta*v*cos(theta(t-1));
then the ship is guided in another direction. How to show the ship is being guided to the say, north-east direction, then in the next guide show that it is being guided to the south-east direction?

Thank you.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us