-
Notifications
You must be signed in to change notification settings - Fork 0
/
CCA_Straight.m
119 lines (97 loc) · 3.21 KB
/
CCA_Straight.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
clc, clear, close all
% Parameters Initialization
% Angle Converting Parameters
r2d = 180 / pi; % Radian to Degree [-]
d2r = 1 / r2d; % Degree to Radian [-]
% Time Step Size
dt = 0.1; % Time Step Size [s]
% Time
t(1) = 0; % Simulation Time [s]
% Waypoint
Wi = [ 0, 0 ]'; % Initial Waypoint Position [m]
Wf = [ 500, 500 ]'; % Final Waypoint Position [m]
% Position and Velocity of UAV
x(1) = 100; % Initial UAV X Position [m]
y(1) = 0; % Initial UAV Y Position [m]
psi(1) = 0 * d2r; % Initial UAV Heading Angle [rad]
p(:,1) = [ x(1), y(1) ]'; % UAV Position Initialization [m]
va = 20; % UAV Velocity [m/s]
% Maximum Lateral Acceleration of UAV
Rmin = 50 ; % UAV Minimum Turn Radius [m]
umax = va^2 / Rmin ; % UAV Maximum Lateral Acceleration [m]
% Design Parameters
kappa = 0.75 ; % Gain
delta = 50 ; % Carrot Distance
%% Path Following Algorithm
i = 0; % Time Index
while x(i+1) < Wf(1)
i = i + 1 ;
%--------------------CCA Path Following Algorithm---------------------
% Step 1
% Distance between initial waypoint and current UAV position, Ru
Ru = norm(Wi - p(:,i));
% Orientation of vector from initial waypoint to final waypoint, theta
theta = atan2(Wf(2) - Wi(2), Wf(1) - Wi(1));
% Step 2
% Orientation of vector from initial waypoint to current UAV position, thetau
theta_u = atan2(p(2,i) - Wi(2), p(1,i) - Wi(1));
% Difference between theta and theatu, DEL_theta
DEL_theta = theta - theta_u;
% Step 3
% Distance between initial waypoint and q, R
R = sqrt( Ru^2 - (Ru*sin(DEL_theta))^2 );
% Step 4
% Carrot position, s = ( xt, yt )
xt = Wi(1) + (R + delta) * cos(theta);
yt = Wi(2) + (R + delta) * sin(theta);
% Step 5
% Desired heading angle, psid
psi_d = atan2(yt - p(2,i), xt - p(1,i));
% Wrapping up psid
psi_d = rem(psi_d,2*pi);
if psi_d < -pi
psi_d = psi_d + 2*pi;
elseif psi_d > pi
psi_d = psi_d-2*pi;
end
% Step6
% Guidance command, u
u(i) = kappa*(psi_d - psi(i))*va;
% Limit u
if u(i) > umax
u(i) = umax;
elseif u(i) < -umax
u(i) = - umax;
end
%--------------------------------------------------------------------%
%.. UAV Dynamics
% Dynamic Model of UAV
dx = va * cos(psi(i));
dy = va * sin(psi(i));
dpsi = u(i) / va ;
% UAV State Update
x(i+1) = x(i) + dx * dt ;
y(i+1) = y(i) + dy * dt ;
psi(i+1) = psi(i) + dpsi * dt ;
% UAV Position Vector Update
p(:,i+1) = [ x(i+1), y(i+1) ]' ;
%.. Time Update
t(i+1) = t(i) + dt ;
end
%% Result Plot
% Trajectory Plot
figure(1) ;
plot( [ Wi(1), Wf(1) ], [ Wi(2), Wf(2) ], 'r--', 'LineWidth', 2 ) ;
hold on ;
plot( x, y, 'b-', 'LineWidth', 1.2 ) ;
hold on ;
xlabel('X (m)') ;
ylabel('Y (m)') ;
legend('Desired Path', 'UAV Trajectory', 'Location', 'southeast' ) ;
% axis([ 0 500 0 500 ]) ;
title(['\delta = ' num2str(delta) ' \kappa = ' num2str(kappa)])
% Guidance Command
figure(2) ;
plot( t(1:end-1), u, 'LineWidth', 2 ) ;
xlabel('Time (s)') ;
ylabel('u (m/s^2)') ;