Home » Robotics

# Category Archives: Robotics

## Distance Transform Path Planning Algorithm

clc;close all;clear all;

Input Map

```RGB = imread('Mapn.png');
figure('Position',[600 0 600 1000],'color','k');
image(RGB);
hold on;
axis off
axis image
hold off;
% Convert to binary image matrix and inverse matrix values
I = rgb2gray(RGB);
binaryImage = im2bw(RGB, 0.3);
binaryImage = 1-binaryImage;
% Inital pose
xs = 540; ys = 750;
% Goal Pose
xg = 150; yg = 150;```

Distance to Goal

```sz  = size(binaryImage);
for i = 1:sz(1)
for j = 1:sz(2)
rg(i,j) = sqrt((i-yg)^2 +(j - xg)^2);
end
end
% Pose Matrix
pose = zeros(sz(1),sz(2));
pose(ys,xs) = 1;
pose(yg,xg) = 1;```
```Di = 15; % Distance of influence of the obstacles
% Compute for every pixel the distance to the nearest obstacle(non zero
% elements which after inversion corespondent to the obstacles)
[D,IDX] = bwdist(binaryImage);
for i = 1:sz(1)
for j = 1:sz(2)
rd = D(i,j);
if (rd <= Di)
rg(i,j)  = 1e6;
end
end
end```

Distance Transform Path Planning Algorithm

```figure('Position',[600 0 600 1000],'color','k');
hold on;
image(RGB);
colormap gray
contour(rg,15);
spy(pose,'*r');
axis off
axis image
title('Distance Transform Path Planning Algorithm','color','w');
x = xs; y = ys;
last = rg(y-1,x-1);
while (x ~= xg)||(y ~= yg)
dis =[ rg(y-1,x-1), rg(y-1,x),rg(y-1,x+1);
rg(y,x-1), rg(y,x) ,rg(y,x+1);
rg(y+1,x-1), rg(y+1,x),rg(y+1,x+1)];
m = min(dis(:));
[r,c] = find(dis == m);
rg(y,x) = 1E6;
y = y-2+r;
x = x-2+c;
pose(y,x) = 1;
end
spy(pose,'.r');
set(gcf, 'InvertHardCopy', 'off');
hold off;```

## Voronoi Road Map, Path Planing

clc;close all;clear all;

Input Map

```RGB = imread('Map.png');
figure('Position',[100 0 600 700],'color','k');
imagesc(RGB);
axis image
title('Input Map','color','w');
axis off
set(gcf, 'InvertHardCopy', 'off');
hold off;```

Convert to binary image matrix and inverse matrix values

```I = rgb2gray(RGB);
binaryImage = im2bw(RGB, 0.3);
sz  = size(binaryImage);```

Inital pose

```xs = 225; ys = 355;
% Goal Pose
xg = 50; yg = 50;
% Pose Matrix
pose = zeros(sz(1),sz(2));
pose(ys,xs) = 1;
pose(yg,xg) = 1;```

```figure('Position',[100 0 600 700],'color','k');
hold on;
set(gcf, 'InvertHardCopy', 'off');
sk = 1;
for i = 1:sz(1)
for j = 1:sz(2)
if(binaryImage(i,j) == 1 )
xv(sk) = j;
yv(sk) = i;
sk = sk+1;
end
end
end
[vrx,vry]  = voronoi(xv,yv);
set(gca,'YDir','Reverse');
plot(xv,yv,'y.',vrx,vry,'b-');
axis tight
%axis([1 sz(2) 1 sz(1) ]) ;
axis image
'* - Represents Starting and Goal Position '},'color','w');
axis off
spy(pose,'*r');```

Robot Path

```x = xs; y = ys;
indk = 1;
while (abs(x - xg) > 5)||(abs(y - yg)> 5)
path(:,indk) = [x y];
dist = sqrt((vrx - x).^2+(vry - y).^2);
goal = sqrt((xg - x).^2+(xg - y).^2);
% Find closest vertices of the Voronoi edges
[mn ind] = min(dist(:));
xt = vrx(ind);
yt = vry(ind);
goalj = sqrt((xg - xt).^2+(xg - yt).^2);
if (goalj < goal )
x = xt;
y = yt;
vrx(ind) = 1E6;
vry(ind) = 1E6;
indk = indk +1;
else
vrx(ind) = 1E6;
vry(ind) = 1E6;
end
end
path(:,indk) = [xg yg];
plot(path(1,:),path(2,:),'-r');
title({'Voronoi Road Map and Robot Path';
'* - Represents Starting and Goal Position '},'color','w');```

## Potential Field Path Planning

clc;close all;clear all;

Input Map

```RGB = imread('Mapn.png');
figure('Position',[600 0 600 1000],'color','k');
image(RGB);
hold on;
axis off
axis image```

Convert to binary image matrix and inverse matrix values

```I = rgb2gray(RGB);
binaryImage = im2bw(RGB, 0.3);
binaryImage = 1-binaryImage;
% Inital pose
xs = 540; ys = 750;
% Goal Pose
xg = 150; yg = 150;```

Attractive potential

```sz  = size(binaryImage);
for i = 1:sz(1)
for j = 1:sz(2)
rg = sqrt((i-yg)^2 +(j - xg)^2);
U_att(i,j) = 0.5*rg^2;
end
end
contour(U_att,40);

set(gcf, 'InvertHardCopy', 'off');
% Pose Matrix
pose = zeros(sz(1),sz(2));
pose(ys,xs) = 1;
pose(yg,xg) = 1;
spy(pose,'*r');
title({'Attractive Potential Field';...
'Circles Center Corespondents to the Goal Position '},'color','w');
hold off;```

Repulsive potential

```Di = 35; % Distance of influence of the object
% Compute for every pixel the distance to the nearest obstacle(non zero
% elements which after inversion corespondent to the obstacles)
[D,IDX] = bwdist(binaryImage);
for i = 1:sz(1)
for j = 1:sz(2)
rd = D(i,j);
if (rd <= Di)
U_rep(i,j)  = 0.5*(rd - Di)^2;
else
U_rep(i,j)  = 0;
end
end
end
figure('Position',[600 0 600 1000],'color','k');
hold on;
colormap jet;
contourf(U_rep,5);
spy(pose,'*r');
axis off
axis image
set(gcf, 'InvertHardCopy', 'off');
title('Repulsive Potential Field','color','w');
hold off;```

Summary Potential Field

```k_s  = 2; % Scaling factor
U_sum = U_rep/max(U_rep(:))+k_s*U_att/max(U_att(:));
figure('Position',[600 0 600 1000],'color','k');
hold on;
contourf(U_sum,15);
spy(pose,'*r');
axis off
axis image
title('Summary Potential Field','color','w');
set(gcf, 'InvertHardCopy', 'off');
hold off;```

Potential Field Path Planning

```figure('Position',[600 0 600 1000],'color','k');
hold on;
contourf(U_sum,15);
spy(pose,'*r');
axis off
axis image
title('Potential Field Path Planning,*Note the Potential Trap','color','w');
x = xs; y = ys;
last = U_sum(y-1,x-1);
while (x ~= xg)||(y ~= yg)
dis =[ U_sum(y-1,x-1), U_sum(y-1,x),U_sum(y-1,x+1);
U_sum(y,x-1), U_sum(y,x) ,U_sum(y,x+1);
U_sum(y+1,x-1), U_sum(y+1,x),U_sum(y+1,x+1)];
m = min(dis(:));
[r,c] = find(dis == m);
U_sum(y,x) = inf;
y = y-2+r;
x = x-2+c;
pose(y,x) = 1;
end
spy(pose,'.r');
set(gcf, 'InvertHardCopy', 'off');
hold off;```

## Computing a Skeleton of a Map for Mobile Robots,Path Planning

clc;close all;clear all;

Input Map

```RGB = imread('Map1.png');
figure('Position',[0 0 800 800]);
imshow(RGB);```

Convert to binary image matrix and inverse matrix values

```I = rgb2gray(RGB);
binaryImage = im2bw(RGB, 0.3);
binaryImage = 1-binaryImage;
figure('Position',[0 0 800 800]);
imshow(binaryImage);```
```Warning: Image is too big to fit on screen; displaying at
50%```

Compute for every pixel the distance to the nearest obstacle(non zero elements which after inversion corespondent to the obstacles)

```[D,IDX] = bwdist(binaryImage);
figure('Position',[0 0 800 800],'color','k')
colormap bone
image(D);
axis off          % Remove axis ticks and numbers
axis image
set(gcf, 'InvertHardCopy', 'off');```

Taking the Laplacian of the distance transform. Play with the treshold value 0.15 to remove or add more futures in skeleton

```lap = del2(D);
sz  = size(lap);
for i = 1:sz(1)
for j = 1:sz(2)
if( abs(lap(i,j)) < 0.15)
lap(i,j) = 0;
end
end
end
lap = flipdim(lap ,1);           %# vertical flip
figure('Position',[0 0 800 800],'color','k');
colormap winter;
contour(lap,'DisplayName','lap');
axis off
axis image
set(gcf, 'InvertHardCopy', 'off');```

## Dynamics simulator program for the robot

clear all; clc; close all;

```% Characteristics of the Robot
m1 = 10;  % kg
m2 = 5;   % kg
d1 = 200; % mm
l2 = 400; % mm
w0 = [-100;-200;200;0;0;-1];
w1 = [200;150;200;0;0;-1];
T  = 15;   % s
dt = 0.01; % s
t = 0:dt:T;```

Function sddot(t, T) is the speed profile to be used for that motion in tool-configuration space is a linear acceleration profile and indirectly given by

```a0 = 6/T^2;
sddot = a0 - 2*a0*t/T;```

Function sdot(t, T) delivering the value of the integral of speed profile

```sz = size(t);
sdot(1) = 0;
for i =1:sz(2)-1;
sdot(i+1) = sdot(i) + dt*sddot(i);
end```

Function s(t, T)

```s(1) = 0;
for i =1:sz(2)-1;
s(i+1) = s(i) + dt*sdot(i);
end```

Function w(t, T, w0, w1) delivering the tool-configuration position at time t of the straight line between w0 and w1 Function wdot(t, T, w0, w1) delivering the tool-configuration velocity at time t of the straight line between w0 and w1,computed using function sdot(t, T) Function wddot(t, T, w0, w1) delivering the tool-configuration acceleration at time t of the straight line between w0 and w1, computed using function sddot(t, T)

```for i = 1:sz(2)
w_des(:,i)      = (1 - s(i))*w0 + s(i)*w1;
wdot_des(:,i)   = sdot(i)*(w1-w0);
wddot_des(:,i)  = sddot(i)*(w1-w0);
end```

Function qw(w), yielding the vector of joint variables given a tool-configuration position (i.e. solution of inverse kinematics)

```for i = 1:sz(2)
q_des(1,i) = atan2(-w_des(1,i),w_des(2,i));
%q_des(2,i) = w_des(2,i)/cos(q_des(1,i)); % vers1
q_des(2,i) = sqrt(w_des(1,i)^2+w_des(2,i)^2);
end```

Function qdot(w, wdot), computing the vector of joint velocities given a tool-configuration position and velocity Function qddot(w, wdot, wddot), computing the vector of joint accelerations given a tool-configuration position, velocity and acceleration

```for i = 1:sz(2)
qdot(1,i) = (w_des(1,i)*wdot_des(2,i)-w_des(2,i)*wdot_des(1,i))/...
(w_des(1,i)^2+w_des(2,i)^2);
qdot(2,i) = (w_des(1,i)*wdot_des(1,i)+w_des(2,i)*wdot_des(2,i))/...
sqrt(w_des(1,i)^2+w_des(2,i)^2);
w1 = w_des(1,i);
w2 = w_des(2,i);
wdt1 = wdot_des(1,i); wdt2 = wdot_des(2,i);
wddt1 = wddot_des(1,i);wddt2 = wddot_des(2,i);
qddot(1,i) = ((w1^2+w2^2)*(w1*wddt2-w2*wddt1)-2*(w1*wdt2-w2*wdt1)*...
(w1*wdt1+w2*wdt2))/((w1^2+w2^2)^2);
qddot(2,i) = ((w1^2+w2^2)*(w1*wddt1+w2*wddt2)+(w1*wdt2-w2*wdt1)^2)/...
((w1^2+w2^2)^1.5);
end```

Function torques(q, qdot, qddot), computing the vector of torques given the vectors of joint-space positions q, velocities qdot and accelerations qddot, equations of motion, direct dynamics

```for i = 1:sz(2)
% torques(q, qdot, qddot)
trq1(i) = m2*(l2^2/3 - l2*q_des(2,i)+q_des(2,i)^2)*qddot(1,i)  +...
m2*(2*q_des(2,i)-l2)*qdot(1,i)* qdot(2,i);
trq2(i) = m2*qddot(2,i)+m2*(l2/2 - q_des(2,i))*qdot(1,i)^2;
%
end
q_cur    = [q_des(1,1); q_des(2,1)];
qdot_cur = [0;0];
for i = 1:sz(2)
tau   = [trq1(i) trq2(i)];
ct = t(i); % Current time
[t0, y0] = ode45(@(ct,y) dyns(ct,y,tau), [ct,ct+dt],...
[q_cur(1); qdot_cur(1); q_cur(2); qdot_cur(2)]);
rs = size(y0);
q_cur = [y0(rs(1),1);y0(rs(1),3)];
q_curv(i,:) = q_cur;
qdot_cur = [y0(rs(1),2);y0(rs(1),4)];
% Function wq(q), yielding the tool-configuration position for  vector
% of  joint  variables q,   direct kinematics
% Given  q = [q1 ,q2], return w
w_cur = [-sin(q_cur(1))*q_cur(2); cos(q_cur(1))*q_cur(2); d1];
% fprintf('[ %6.4f %6.4f  %6.4f ] \n',w_cur);
w_curv(i,:) = w_cur;
end
% Plots
figure('Position',[200 0 600 600])
subplot(2,1,1);
hold on;
plot(t,q_curv(:,1)*180/pi ,'r');
xlabel('Time[s]');
ylabel('q1(t)[deg]');
grid on;
subplot(2,1,2);
plot(t,q_curv(:,2),'b');
xlabel('Time[s]');
ylabel('q2(t)[mm]');
grid on;
figure('Position',[200 0 600 600])
subplot(2,1,1);
plot(t,w_curv(:,1),'r');
xlabel('Time[s]');
ylabel('w1(t)[mm]');
grid on;
subplot(2,1,2);
plot(t,w_curv(:,2),'b');
xlabel('Time[s]');
ylabel('w2(t)[mm]');
grid on;```