To enhance your animation by adding a visualization of the repulsive force region around the obstacle, you can draw a circle centered at the obstacle's position with a radius equal to rho0. This circle will help visualize the boundary within which the repulsive force is active.
Below is the modified MATLAB code with comments highlighting the changes:
clc
clear all
% Constants
epsilon = 0.05;
zeta2 = 0.05;
alpha = 0.01;
neta2 = 2;
rho0 = 0.5;
% Constraints
m2 = 0;
l1 = 0;
l2 = 1.5;
% Initial conditions
q1s = 0;
q2s = 0.9;
% Final conditions
q1f = 120*(pi/180);
q2f = 1.3;
x0 = 0;
y0 = 0;
% Obstacle position
b = [0.75; -0.0];
% Initialize variables
q1(1) = q1s;
q2(1) = q2s;
o2f = [q2f*sin(q1f); -q2f*cos(q1f)];
i = 1;
% Precompute the circle for repulsive force region
theta = linspace(0, 2*pi, 100);
circle_x = b(1) + rho0 * cos(theta);
circle_y = b(2) + rho0 * sin(theta);
figure(1); % Create a new figure for animation
hold on; % Hold on to plot multiple elements
% Plot the obstacle
plot(b(1), b(2), 'kp', 'MarkerSize', 20, 'MarkerFaceColor', 'k');
% Plot the repulsive force region
hCircle = plot(circle_x, circle_y, 'r--', 'LineWidth', 1.5);
legend('Robot Link', 'Obstacle', 'Repulsive Force Region');
grid on
axis equal
axis([-1.5 1.5 -1.5 1.5])
while norm([q1(i) ; q2(i)] - [q1f ; q2f]) > epsilon
th1 = q1(i);
d2 = q2(i);
o2 = [d2*sin(th1) ; -d2*cos(th1)];
% Force due to attractive potential
fatt_2 = -zeta2*(o2 - o2f);
% Obstacle avoidance (repulsive force)
distance_to_obstacle = norm(o2 - b);
if distance_to_obstacle <= rho0
frep2 = neta2*((1/distance_to_obstacle) - (1/rho0)) * ((1/distance_to_obstacle)^2) * ((o2 - b)/distance_to_obstacle);
else
frep2 = [0; 0];
end
% Net force
Jo2 = [d2*cos(th1), sin(th1) ; d2*sin(th1), -cos(th1)];
tau = transpose(Jo2)*(fatt_2 + frep2);
% Update angles and distance
tau_norm = norm(tau);
if tau_norm ~= 0
q1(i+1) = q1(i) + alpha*(tau(1)/tau_norm);
q2(i+1) = q2(i) + alpha*(tau(2)/tau_norm);
else
q1(i+1) = q1(i);
q2(i+1) = q2(i);
end
% Enforce constraints on q2
if q2(i+1) > l2
q2(i+1) = l2;
elseif q2(i+1) <= 0
q2(i+1) = 0.01;
end
% Calculate positions
x = d2*sin(th1);
y = -d2*cos(th1);
xe = (d2-l2)*sin(th1);
ye = -(d2-l2)*cos(th1);
ax(i) = x;
ay(i) = y;
% Update the plot
% Clear the previous robot links but keep the obstacle and repulsive region
clf;
hold on;
% Replot the obstacle and repulsive region
plot(b(1), b(2), 'kp', 'MarkerSize', 20, 'MarkerFaceColor', 'k');
plot(circle_x, circle_y, 'r--', 'LineWidth', 1.5);
legend('Robot Link', 'Obstacle', 'Repulsive Force Region');
% Plot the robot links
plot([x0 x], [y0 y], 'b-', 'LineWidth', 2); % First link
plot([x0 xe], [y0 ye], 'g-', 'LineWidth', 2); % Second link
plot(ax, ay, 'bo', 'MarkerSize', 5, 'MarkerFaceColor', 'b'); % Trace of the end effector
grid on
axis equal
axis([-1.5 1.5 -1.5 1.5])
% Store data for plotting
frep_2_plot(i) = norm(frep2);
fatt_2_plot(i) = norm(fatt_2);
tau1_plot(i) = tau(1);
tau2_plot(i) = tau(2);
i_plot(i) = i;
pause(0.01)
i = i+1;
end
% Plotting results
figure(2)
plot(i_plot, frep_2_plot, 'r', 'LineWidth', 2)
title("Repulsive Force")
xlabel("Iteration")
ylabel("Force Magnitude")
grid on
figure(3)
plot(i_plot, fatt_2_plot, 'b', 'LineWidth', 2)
title("Attractive Force")
xlabel("Iteration")
ylabel("Force Magnitude")
grid on
figure(4)
plot(i_plot, tau1_plot, 'g', 'LineWidth', 2)
title("Tau 1")
xlabel("Iteration")
ylabel("Torque")
grid on
figure(5)
plot(i_plot, tau2_plot, 'm', 'LineWidth', 2)
title("Tau 2")
xlabel("Iteration")
ylabel("Torque")
grid on
Explanation of the Modifications:
-
Precompute the Repulsive Force Region Circle:
- To efficiently plot the repulsive force region, compute the circle coordinates outside the loop.
theta = linspace(0, 2*pi, 100);
circle_x = b(1) + rho0 * cos(theta);
circle_y = b(2) + rho0 * sin(theta);
-
Initial Plot Setup:
- Initialize the figure and plot the obstacle and the repulsive force region before entering the loop.
figure(1); % Create a new figure for animation
hold on; % Hold on to plot multiple elements
% Plot the obstacle
plot(b(1), b(2), 'kp', 'MarkerSize', 20, 'MarkerFaceColor', 'k');
% Plot the repulsive force region
hCircle = plot(circle_x, circle_y, 'r--', 'LineWidth', 1.5);
legend('Robot Link', 'Obstacle', 'Repulsive Force Region');
grid on
axis equal
axis([-1.5 1.5 -1.5 1.5])
-
Updating the Plot Inside the Loop:
- Clear the previous robot links while retaining the obstacle and repulsive force region.
- Re-plot the robot's current links and the trace of the end effector.
clf;
hold on;
% Replot the obstacle and repulsive region
plot(b(1), b(2), 'kp', 'MarkerSize', 20, 'MarkerFaceColor', 'k');
plot(circle_x, circle_y, 'r--', 'LineWidth', 1.5);
legend('Robot Link', 'Obstacle', 'Repulsive Force Region');
% Plot the robot links
plot([x0 x], [y0 y], 'b-', 'LineWidth', 2); % First link
plot([x0 xe], [y0 ye], 'g-', 'LineWidth', 2); % Second link
plot(ax, ay, 'bo', 'MarkerSize', 5, 'MarkerFaceColor', 'b'); % Trace of the end effector
grid on
axis equal
axis([-1.5 1.5 -1.5 1.5])
-
Enhancements and Optimizations:
-
Plotting Enhancements:
- Added labels and grid lines to the plots for better readability.
Additional Notes:
By implementing these modifications, your animation will now display the repulsive force region around the obstacle, providing a clearer understanding of how the robot navigates to avoid it.