-
Notifications
You must be signed in to change notification settings - Fork 4
/
leader_follower_triangular_formation.m
161 lines (119 loc) · 4.26 KB
/
leader_follower_triangular_formation.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
%% Leader-follower with triangular formation
%Master ATSI : Multi Agent System Course
%Group 3: Nadine KABBARA
%Leonardo FELIPE TOSO
%Lara JABER
%Soha KANSO
%Fadhlallah BOUDEHANE
%robots form a triangular formation and follow a leader in its trajectory
%while keeping the formation
%% Set up the Robotarium object
N = 6;
%initial_positions = generate_initial_conditions(N, 'Width', 1, 'Height', 1, 'Spacing', 0.3);
initial_positions = [ -0.75, 0, -1, 0, -1,0; ...
0.6, 0.8, 0, 0, -0.5, -0.5 ; ...
0, 0, 0, 0, 0, 0];
r = Robotarium('NumberOfRobots', N, 'ShowFigure', true, 'InitialConditions', initial_positions);
%% Experiment Constants
%Run the simulation for a specific number of iterations
iterations = 6000;
% Sampling time (not to be modified)
Ts = 0.033;
%% Create the desired Laplacian
%Graph laplacian
%
L= [0 0 0 0 0 0 ; ...
-1 3 0 -1 0 -1; ...
-1 0 3 -1 0 -1 ; ...
0 -1 -1 3 -1 0 ; ...
-1 0 0 -1 3 -1 ; ...
0 -1 -1 0 -1 3];
A= [0 0 0 0 0 0 ; ...
-1 0 0 -1 0 -1; ...
-1 0 0 -1 0 -1 ; ...
0 -1 -1 0 -1 0 ; ...
-1 0 0 -1 0 -1 ; ...
0 -1 -1 0 -1 0];
% The desired inter-agent distance for the formation
d = 0.4;
% Pre-compute diagonal values for the rectangular formation
ddiag = d*sqrt(3);
weights = [ 0 0 0 0 0 0; ...
d 0 0 d 0 ddiag; ...
d 0 0 ddiag 0 d; ...
0 d ddiag 0 d 0; ...
ddiag 0 0 d 0 d; ...
0 ddiag d 0 d 0];
%Initialize velocity vector
dxi = zeros(2, N);
%State for leader
state = 1;
% These are gains for our formation control algorithm
formation_control_gain = 0.3;
%% Grab tools we need to convert from single-integrator to unicycle dynamics
%
[~, uni_to_si_states] = create_si_to_uni_mapping();
% si_to_uni_dyn = create_si_to_uni_dynamics_with_backwards_motion();
si_to_uni_dyn = create_si_to_uni_dynamics('LinearVelocityGain', 0.5, 'AngularVelocityLimit', pi/2);
% Single-integrator position controller
uni_barrier_cert= create_uni_barrier_certificate_with_boundary();
waypoints =0.9* [-1 0.8;-1 -0.8; 1 -0.8; 1 0.8]';
close_enough = 0.05;
v=0.8;
leader_controller = create_si_position_controller('XVelocityGain', v, 'YVelocityGain', v, 'VelocityMagnitudeLimit', 0.1);
for t = 1:iterations
% Retrieve the most recent poses from the Robotarium. The time delay is
% approximately 0.033 seconds
x = r.get_poses();
% Convert to SI states
xsi = uni_to_si_states(x);
%% Algorithm
if t<300
state=1;
formation_control_gain = 2;
end
if t>=300 && t<1000
state=1;
formation_control_gain = 5;
end
if t>=1000
formation_control_gain = 10;
end
for i = 2:N
%Zero velocity and get the topological neighbors of agent i
dxi(:, i) = [0 ; 0];
neighbors = topological_neighbors(L, i);
for j = neighbors
dxi(:, i) = dxi(:, i) + ...
formation_control_gain*(norm(xsi(:, i) - xsi(:, j))^2 - weights(i, j)^2) ...
*(xsi(:, j) - xsi(:, i));
end
end
%% Make the leader travel between waypoints
waypoint = waypoints(:, state);
dxi(:, 1) = leader_controller(xsi(:, 1), waypoint);
if(norm(xsi(:, 1) - waypoint) < close_enough)
state = state+1;
if(state==5)
state=1;
end
end
%% Avoid actuator errors
% To avoid errors, we need to threshold dxi
norms = arrayfun(@(x) norm(dxi(:, x)), 1:N);
threshold = 3/4*r.max_linear_velocity;
to_thresh = norms > threshold;
dxi(:, to_thresh) = threshold*dxi(:, to_thresh)./norms(to_thresh);
%% Use barrier certificate and convert to unicycle dynamics
dxu = si_to_uni_dyn(dxi, x);
dxu = uni_barrier_cert(dxu, x);
%% Send velocities to agents
%Set velocities
r.set_velocities(1:N, dxu);
%Iterate experiment
r.step();
end
% We can call this function to debug our experiment! Fix all the errors
% before submitting to maximize the chance that your experiment runs
% successfully.
r.debug();