-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfin3.m
90 lines (69 loc) · 2.46 KB
/
fin3.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
%% environment
numRobots = 100;
env = MultiRobotEnv(numRobots);
env.robotRadius = 0.5;
env.showTrajectory = false;
%% detectors for all robots
detectors = cell(1,numRobots);
for rIdx = 1:numRobots
detector = RobotDetector(env,rIdx);
detector.maxDetections = numRobots;
detector.maxRange = 1; %SENSOR RANGE 0.75 IS OK
detector.fieldOfView = pi/3;
detectors{rIdx} = detector;
end
env.plotSensorLines = false;
%% Initialization
sampleTime = 0.1; % Sample time [s]
tVec = 0:sampleTime:15; % Time array
% Initialize poses randomly
poses = [((10*(scale-1))+7)*(rand(2,numRobots))+2*ones(2,numRobots);
pi*rand(1,numRobots)];
%% Simulation loop
frame = 1;
vel = zeros(3,numRobots);
for idx = 0:numel(tVec)
% Update the environment
env(1:numRobots, poses);
xlim([1 10*scale]); % Without this, axis resizing can slow things down
ylim([1 10*scale]);
M(frame) = getframe(gcf);
frame = frame + 1;
% Read the sensor and execute the controller for each robot
for rIdx = 1:numRobots
detections = step(detectors{rIdx});
vel(:,rIdx) = swarmTeamController(poses,rIdx,detections,potentialimage,scale);
end
% Discrete integration of pose
poses = poses + vel*sampleTime;
end
%% Robot Controller Logic
function vel = swarmTeamController(poses,rIdx,detections,potentialimage,scale)
% Unpack the robot's pose
pose = poses(:,rIdx);
w = 2; % INCREASE STALL ROTATION 1 IS OK
[px,py] = gradient(potentialimage);
n = ceil(pose(1)); m = ceil(pose(2));
f_grad = [px(m,n) py(m,n)];
vx = (1/(4*scale))*f_grad(1);
vy = (1/(4*scale))*f_grad(2);
vel = [vx;vy;w];
if ~isempty(detections)
% Take the average range and angle
range = min(detections(:,1));
angle = mean(detections(:,2));
% Move linearly to maintain a range to the nearest robot
if range < 1
v = -10; %AVOID COLLISIONS -10 IS OK
elseif range >= 1
v = 10;
end
% Turn to maintain a heading to the nearest robot
if angle > pi/2
w = 2;
elseif angle < -pi/12
w = -2;
end
vel = bodyToWorld([v;0;w],pose);
end
end