Homework 4 Solution
Homework 4 Solution
1. Potential Field. Use MATLAB to make a 3-D plot of the potential fields described below.
You will need to use plot commands and maybe the mesh function. The work area is a
square from (0,0) to (12,12) in the (x,y) plane. The goal is at (10,10). There are obstacles at
(4,6) and (6,4). Use a repulsive potential of K i / ri for each obstacle, with ri the vector to the
i-th obstacle. For the target use an attractive potential of KT rT , with rT the vector to the
target. Adjust the gains to get a decent plot. Plot the sum of the three force fields in 3-D.
2. Potential Field Navigation. For the same scenario as in Problem 1, a mobile robot starts at
(0,0). The front wheel steered mobile robot has dynamics
x V cos sin
y V cos cos
V
sin
L
with (x,y) the position, the heading angle, V the wheel speed, L the wheel base, and the
steering angle. Set L= 2.
a. Compute forces due to each obstacle and goal. Compute total force on the vehicle at
point (x,y).
b. Design a feedback control system for force-field control. Sketch your control system.
c. Use MATLAB to simulate the nonlinear dynamics assuming a constant velocity V and a
steerable front wheel. The wheel should be steered so that the vehicle always goes
downhill in the force field plot. Plot the resulting trajectory in the (x,y) plane.
3. Swarm/Platoon/Formation. Do what you want to for this problem. The intent is to focus
on some sort of swarm or platoon or formation behavior, not the full dynamics. Therefore,
take 5 vehicles each with the simple point mass (Newton’s law) dynamics
x Fx / m
y Fy / m
with (x,y) the position of the vehicle and Fx , Fy the forces in the x and y direction respectively.
The forces might be the sums of attractive forces to goals, repulsive forces from obstacles, and
repulsive forces between the agents.
Make some sort of interesting plots or movies showing the leader going to a desired goal
or moving along a prescribed trajectory and the followers staying close to him, or in a prescribed
formation. Obstacle avoidance by a platoon or swarm is interesting.
5
Sukru Akif ERTURK, 1000810591, HW4 02/24/2015
Question 1:
Potential Field:
In this case, there are 2 obstacles and one target considered. Target position is chosen as
(10,10). One of obstacles is placed at (4,6). Another obstacle is placed at (6,4). Total potential is
obtained as
𝐾1 𝐾2
𝑉𝑡𝑜𝑡𝑎𝑙 = 𝐾𝑇 𝑟𝑇 + +
𝑟1 𝑟2
𝐾𝑇 = 1; 𝐾1 = 3; 𝐾2 = 5;
1
Sukru Akif ERTURK, 1000810591, HW4 02/24/2015
60
40
V
20
0
12
10
8
6 12
10
4 8
6
2 4
y 0 2
0 x
10
6
y
0
0 2 4 6 8 10 12
x
2
Sukru Akif ERTURK, 1000810591, HW4 02/24/2015
%% Position of Obstacle 1
x_1 = 4;
y_1 = 6;
%% Position of Obstacle 2
x_2 = 6;
y_2 = 4;
%% Position of Goal
x_g = 10;
y_g = 10;
%% Calculate Potential Fields
x = 0:0.1:12;
y = 0:0.1:12;
n = length(x);
for i = 1:n
for j = 1:n
end
end
%% Plot Figures
figure;
mesh(x,y,V)
xlabel('\bfx')
ylabel('\bfy')
zlabel('\bfV')
title('\bfPotential Field on 3D Plot')
figure;
contour(x,y,V)
xlabel('\bfx')
ylabel('\bfy')
title('\bfPotential Field on Contour Plot')
3
Sukru Akif ERTURK, 1000810591, HW4 02/24/2015
Question 2:
Potential Field Navigation:
𝑥̇ = 𝑉𝑐𝑜𝑠(𝜙) cos(𝜃)
𝑦̇ = 𝑉𝑐𝑜𝑠(𝜙)sin𝜃)
𝑉
𝜃̇ = 𝑠𝑖𝑛(𝜙)
𝐿
To compute the forces,
𝜕𝑉
𝐹𝑥 = −
𝜕𝑥
𝜕𝑉
𝐹𝑦 = −
𝜕𝑦
a.) Computed forces for each obstacles and goal are depicted below.
600
400
200
x
0
F
-200
-400
-600
15 10 15
5 5 10
0
y x
4
Sukru Akif ERTURK, 1000810591, HW4 02/24/2015
1.5
0.5
y
F
-0.5
-1
15
10 10 12
5 6 8
2 4
0 0
y x
b.) Design feedback control system for force field control can be chosen as
5
Sukru Akif ERTURK, 1000810591, HW4 02/24/2015
𝜕𝑉 𝐾𝑇 (𝑥𝑇 − 𝑥)
𝐹𝑥 = − =
𝜕𝑥 √(𝑥 − 𝑥𝑇 )2 + (𝑦 − 𝑦𝑇 )2
𝜕𝑉 𝐾𝑇 (𝑦𝑇 − 𝑦)
𝐹𝑦 = − =
𝜕𝑦 √(𝑥 − 𝑥𝑇 )2 + (𝑦 − 𝑦𝑇 )2
𝜕𝑉 −𝐾𝑖 (𝑥𝑖 − 𝑥)
𝐹𝑥𝑖 = − =
𝜕𝑥𝑖 ((𝑥 − 𝑥𝑖 )2 + (𝑦 − 𝑦𝑖 )2 )1.5
𝜕𝑉 −𝐾𝑖 (𝑦𝑖 − 𝑦)
𝐹𝑦 = − =
𝜕𝑦𝑖 ((𝑥 − 𝑥𝑖 )2 + (𝑦 − 𝑦𝑖 )2 )1.5
6
y
0
0 2 4 6 8 10 12
x
6
Sukru Akif ERTURK, 1000810591, HW4 02/24/2015
10
6
y
0
0 2 4 6 8 10 12
x
60
40
V
20
0
12
10
8
12
6 10
8
4
6
2 4
y 2
0 0 x
7
Sukru Akif ERTURK, 1000810591, HW4 02/24/2015
%% Position of Obstacle 1
x_1 = 4;
y_1 = 6;
%% Position of Obstacle 2
x_2 = 6;
y_2 = 4;
%% Position of Goal
x_g = 10;
y_g = 10;
%% Calculate Potential Fields
x = 0:0.1:12;
y = 0:0.1:12;
n = length(x);
for i = 1:n
for j = 1:n
end
end
%% Plot Forces
figure;
mesh(x,y,F_x)
xlabel('\bfx')
ylabel('\bfy')
zlabel('\bfF_x')
title('\bfForce X-component (F_x)')
figure;
mesh(x,y,F_y)
xlabel('\bfx')
ylabel('\bfy')
zlabel('\bfF_y')
title('\bfForce Y-component (F_y)')
x_robot = States(:,1);
y_robot = States(:,2);
8
Sukru Akif ERTURK, 1000810591, HW4 02/24/2015
figure;
contour(x,y,V)
hold on
plot(x_robot,y_robot,'k','LineWidth',2)
hold on
plot(x_g,y_g,'r-s','LineWidth',2)
xlabel('\bfx')
ylabel('\bfy')
title('\bfSingle Mobile Robot Trajectory on Contour Plot')
figure;
mesh(x,y,V)
hold on
plot3(x_robot,y_robot,VV,'k','Linewidth',10)
xlabel('\bfx')
ylabel('\bfy')
zlabel('\bfV')
title('\bfSingle Mobile Robot Trajectory on 3D Plot')
function dS = singleRobotQ2(t,state)
global K_1 K_2 K_G
%% States
x = state(1);
y = state(2);
Theta = state(3);
%% Position of Obstacle 1
x_1 = 4;
y_1 = 6;
%% Position of Obstacle 2
x_2 = 6;
y_2 = 4;
%% Position of Goal
x_g = 10;
y_g = 10;
%% Parameters
V = 5;
K_p = 3;
L = 2;
%% Calculating Forces for a Mobile Robot
F_1x = ((-K_1)*((x_1)-x)) / ((x-x_1)^2 + (y-y_1)^2)^(1.5);
F_2x = ((-K_2)*((x_2)-x)) / ((x-x_2)^2 + (y-y_2)^2)^(1.5);
F_Gx = ((K_G)*((x_g)-x)) / sqrt((x-x_g)^2 + (y-y_g)^2);
9
Sukru Akif ERTURK, 1000810591, HW4 02/24/2015
Alpha = atan2(F_y,F_x);
Phi = K_p*(Alpha-Theta);
dS(1) = V*cos(Phi)*cos(Theta);
dS(2) = V*cos(Phi)*sin(Theta);
dS(3) = (V/L)*sin(Phi);
dS = [dS(1);dS(2);dS(3)];
end
Simulation is stopped when the robot reaches the target. The function is,
function [Val,Ister,Dir] = StopSimulation(t,st)
x = st(1);
y = st(2);
10
Sukru Akif ERTURK, 1000810591, HW4 02/24/2015
Question 3:
Swarm/Platoon/Formation:
A mobile robot dynamics as a simple point mass dynamics for this problem are
𝑥̇1 = 𝑥̇ 𝑖
𝑥̇2 = 𝑦̇ 𝑖
𝑥̇3 = 𝐹𝑥𝑖
𝑥̇4 = 𝐹𝑦𝑖
𝜕𝑉 𝐾𝑇 (𝑥𝑇 − 𝑥)
𝐹𝑥 = − =
𝜕𝑥 √(𝑥 − 𝑥𝑇 )2 + (𝑦 − 𝑦𝑇 )2
𝜕𝑉 𝐾𝑇 (𝑦𝑇 − 𝑦)
𝐹𝑦 = − =
𝜕𝑦 √(𝑥 − 𝑥𝑇 )2 + (𝑦 − 𝑦𝑇 )2
𝜕𝑉 −𝐾𝑖 (𝑥𝑖 − 𝑥)
𝐹𝑥𝑖 = − =
𝜕𝑥𝑖 ((𝑥 − 𝑥𝑖 )2 + (𝑦 − 𝑦𝑖 )2 )1.5
𝜕𝑉 −𝐾𝑖 (𝑦𝑖 − 𝑦)
𝐹𝑦 = − =
𝜕𝑦𝑖 ((𝑥 − 𝑥𝑖 )2 + (𝑦 − 𝑦𝑖 )2 )1.5
11
Sukru Akif ERTURK, 1000810591, HW4 02/24/2015
10
6
y
0
0 2 4 6 8 10 12
x
𝐾𝑖
𝑉𝑖𝑗 =
𝑟𝑖𝑗2
where
2 2
𝑟𝑖𝑗 = √(𝑥𝑖 − 𝑥𝑗 ) + (𝑦𝑖 − 𝑦𝑗 )
1
𝑉𝑖𝐿 = (𝑟 − 𝑟𝐷 )2
2 𝑖𝐿
where
12
Sukru Akif ERTURK, 1000810591, HW4 02/24/2015
where
2 2
𝑟𝑖𝑗 = √(𝑥𝑖 − 𝑥𝑗 ) + (𝑦𝑖 − 𝑦𝑗 )
where
13
Sukru Akif ERTURK, 1000810591, HW4 02/24/2015
Simulation results are depicted below. Gain between followers is chosen as 𝐾𝐹 = 0.1 and
desired separation is chosen as 𝑟𝐷 = 0.5. Simulation is stopped when leader reaches the target
point.
10
6
y
Leader
2 Follow er 1
Follow er 2
Follow er 3
0 Follow er 4
Target
Obstacles
-2
-2 0 2 4 6 8 10 12
x
14
Sukru Akif ERTURK, 1000810591, HW4 02/24/2015
x_robot = States(:,1);
y_robot = States(:,2);
%% Plot Single Robot Trajectory
figure;
plot(x_robot,y_robot,'k.')
hold on
plot(x_1,y_1,'k-s','LineWidth',2)
hold on
plot(x_2,y_2,'k-s','LineWidth',2)
hold on
plot(x_g,y_g,'r-s','LineWidth',2)
grid on
xlim([0 12])
ylim([0 12])
xlabel('\bfx')
ylabel('\bfy')
title('\bfSingle Mobile Robot Trajectory (Point-Mass Dynamics)')
%% Swarm/Platoon/Formation
Initial_L = [0; 0; 0; 0];
Initial_F1 = [0; 0.2; 0; 0];
Initial_F2 = [0.2; 0; 0; 0];
Initial_F3 = [0.2; 0.1; 0; 0];
Initial_F4 = [0.1; 0.2; 0; 0];
All_initials = [Initial_L;Initial_F1;Initial_F2;Initial_F3;Initial_F4];
x_L = StatesPR(:,1);
y_L = StatesPR(:,2);
x_F1 = StatesPR(:,5);
y_F1 = StatesPR(:,6);
x_F2 = StatesPR(:,9);
y_F2 = StatesPR(:,10);
x_F3 = StatesPR(:,13);
y_F3 = StatesPR(:,14);
x_F4 = StatesPR(:,17);
y_F4 = StatesPR(:,18);
%% Plot Swarm Trajectories
figure;
plot(x_L,y_L,'k.')
hold on
plot(x_F1,y_F1,'b.')
hold on
plot(x_F2,y_F2,'r.')
hold on
plot(x_F3,y_F3,'g.')
hold on
plot(x_F4,y_F4,'c.')
hold on
plot(x_g,y_g,'r-s','LineWidth',2)
hold on
plot(x_1,y_1,'k-s','LineWidth',2)
hold on
plot(x_2,y_2,'k-s','LineWidth',2)
grid on
xlim([-2 12])
ylim([-2 12])
xlabel('\bfx')
ylabel('\bfy')
title('\bfPlatoon of Mobile Robots Trajectories')
hh = legend('Leader','Follower 1','Follower 2','Follower 3','Follower
4','Target','Obstacles','Location','SouthEast');
set(hh,'FontSize',8);
function dS = singleRobotQ3(t,state)
global K_1 K_2 K_G
%% States
x = state(1);
15
Sukru Akif ERTURK, 1000810591, HW4 02/24/2015
y = state(2);
xdot = state(3);
ydot = state(4);
%% Gains
K_p = 3;
%% Position of Obstacle 1
x_1 = 4;
y_1 = 6;
%% Position of Obstacle 2
x_2 = 6;
y_2 = 4;
%% Position of Goal
x_g = 10;
y_g = 10;
%% Calculating Forces for a Mobile Robot
F_1x = ((-K_1)*((x_1)-x)) / ((x-x_1)^2 + (y-y_1)^2)^(1.5);
F_2x = ((-K_2)*((x_2)-x)) / ((x-x_2)^2 + (y-y_2)^2)^(1.5);
F_Gx = ((K_G)*((x_g)-x)) / sqrt((x-x_g)^2 + (y-y_g)^2);
dS(1) = xdot;
dS(2) = ydot;
dS(3) = F_x;
dS(4) = F_y;
dS = [dS(1);dS(2);dS(3);dS(4)];
end
function dS = PlatoonRobots(t,state)
global K_1 K_2 K_G K_F rD K_p K_L
%% Leader States
xL = state(1);
yL = state(2);
xLdot = state(3);
yLdot = state(4);
%% Follower 1 States
xF1 = state(5);
yF1 = state(6);
xF1dot = state(7);
yF1dot = state(8);
%% Follower 2 States
xF2 = state(9);
yF2 = state(10);
xF2dot = state(11);
yF2dot = state(12);
%% Follower 3 States
xF3 = state(13);
yF3 = state(14);
xF3dot = state(15);
yF3dot = state(16);
%% Follower 4 States
xF4 = state(17);
yF4 = state(18);
xF4dot = state(19);
yF4dot = state(20);
%% Position of Obstacle 1
x_1 = 4;
y_1 = 6;
%% Position of Obstacle 2
x_2 = 6;
16
Sukru Akif ERTURK, 1000810591, HW4 02/24/2015
y_2 = 4;
%% Position of Goal
x_g = 10;
y_g = 10;
%% Calculating Forces for Leader:
F_1xL = ((-K_1)*((x_1)-xL)) / ((xL-x_1)^2 + (yL-y_1)^2)^(1.5);
F_2xL = ((-K_2)*((x_2)-xL)) / ((xL-x_2)^2 + (yL-y_2)^2)^(1.5);
F_GxL = ((K_G)*((x_g)-xL)) / sqrt((xL-x_g)^2 + (yL-y_g)^2);
17
Sukru Akif ERTURK, 1000810591, HW4 02/24/2015
%% Swarm Dynamics:
dS(1) = xLdot;
dS(2) = yLdot;
dS(3) = F_xL;
dS(4) = F_yL;
18
Sukru Akif ERTURK, 1000810591, HW4 02/24/2015
dS(5) = xF1dot;
dS(6) = yF1dot;
dS(7) = F_xF1;
dS(8) = F_yF1;
dS(9) = xF2dot;
dS(10) = yF2dot;
dS(11) = F_xF2;
dS(12) = F_yF2;
dS(13) = xF3dot;
dS(14) = yF3dot;
dS(15) = F_xF3;
dS(16) = F_yF3;
dS(17) = xF4dot;
dS(18) = yF4dot;
dS(19) = F_xF4;
dS(20) = F_yF4;
dS =
[dS(1);dS(2);dS(3);dS(4);dS(5);dS(6);dS(7);dS(8);dS(9);dS(10);dS(11);dS(12);dS(13);dS(14);dS(15);
dS(16);dS(17);dS(18);dS(19);dS(20);];
end
19
Sukru Akif ERTURK, 1000810591, HW4 02/24/2015
Platoon Movie:
A movie of platoon of mobile robots is prepared for this problem. Here is a screen shot of
this movie when the leader reached the target.
10
4
y
2
Leader
Follow er 1
0 Follow er 2
Follow er 3
Follow er 4
-2
Target
Obstacles
-4
-4 -2 0 2 4 6 8 10 12
x
PlatoonMovie.avi
20
Sukru Akif ERTURK, 1000810591, HW4 02/24/2015
time = length(tPR);
for j = 1:time
plot(x_L(j),y_L(j),'*','MarkerSize',14,'MarkerEdgeColor','black','LineWidth',3)
hold on
plot(x_F1(j),y_F1(j),'bo','LineWidth',3)
hold on
plot(x_F2(j),y_F2(j),'ro','LineWidth',3)
hold on
plot(x_F3(j),y_F3(j),'go','LineWidth',3)
hold on
plot(x_F4(j),y_F4(j),'co','LineWidth',3)
hold on
plot(x_g,y_g,'r-s','LineWidth',2)
hold on
plot(x_1,y_1,'k-s','LineWidth',2)
hold on
plot(x_2,y_2,'k-s','LineWidth',2)
grid on
hold off
xlim([-4 12])
ylim([-4 12])
xlabel('x')
ylabel('y')
title('\bfPlatoon of Mobile Robots Trajectories')
hh = legend('Leader','Follower 1','Follower 2','Follower 3','Follower
4','Target','Obstacles','Location','SouthEast');
set(hh,'FontSize',8);
M(j) = getframe(gcf);
writeVideo(writerObj, M(j));
end
close(writerObj);
21