├── Potential Fields ├── Code │ ├── GoalDelta.m │ ├── ObsDelta.m │ ├── PotentialFields.m │ ├── PotentialFields_Q2.m │ ├── README.md │ └── circles.m ├── Plots │ ├── 1obstacle.png │ ├── 2obstacle.png │ ├── README.md │ └── potentialFields.png └── README.md ├── README.md ├── RRT ├── Code │ ├── CheckIntersection.m │ ├── EuclDist.m │ ├── IfOnEdge.m │ ├── NearestNode.m │ ├── NewPointonLine.m │ ├── NodeToPoint.m │ ├── PointToNode.m │ ├── README.md │ ├── RRT.m │ ├── RandomPoint.m │ └── circles.m ├── Plots │ ├── README.md │ ├── RRT_Conf1.png │ ├── RRT_Conf2.png │ └── RRT_Conf3.png └── README.md ├── RRT_Star ├── Code │ ├── CheckIntersection.m │ ├── EuclDist.m │ ├── IfOnEdge.m │ ├── MinCostNode.m │ ├── NearestNode.m │ ├── NewPointonLine.m │ ├── NodeToPoint.m │ ├── PointToNode.m │ ├── README.md │ ├── RRTStar.m │ ├── RandomPoint.m │ └── circles.m ├── Plots │ ├── README.md │ ├── RRTstar_Conf1.png │ ├── RRTstar_Conf2.png │ └── RRTstar_Conf3.png └── README.md ├── Report_PDF.pdf └── Visibility Graph ├── Code ├── EuclDist.m ├── README.md ├── circles.m └── visible_graph.m ├── Plots ├── README.md ├── VisGraph_Conf1.png ├── VisGraph_Conf2.png └── VisGraph_Conf3.png └── README.md /Potential Fields/Code/GoalDelta.m: -------------------------------------------------------------------------------- 1 | function [delXG, delYG] = GoalDelta(vx, vy, gx, gy, goalR, goalS, alpha) 2 | % This function gives delX, delY of attraction caused by the goal point 3 | 4 | dGoal = sqrt((gx-vx)^2 + (gy-vy)^2); % distance bw goal and current position 5 | thetaG = atan2((gy-vy),(gx-vx)); % angle between goal and current position 6 | % delXG = 0; delYG = 0; 7 | 8 | if dGoal= dGoal) && (dGoal >= goalR) 11 | delXG = alpha*(dGoal - goalR)*cos(thetaG); 12 | delYG = alpha*(dGoal - goalR)*sin(thetaG); 13 | else 14 | delXG = alpha*goalS*cos(thetaG); 15 | delYG = alpha*goalS*sin(thetaG); 16 | end 17 | 18 | end -------------------------------------------------------------------------------- /Potential Fields/Code/ObsDelta.m: -------------------------------------------------------------------------------- 1 | function [delXO, delYO] = ObsDelta(vx, vy, ox, oy, obsRad, obsS, beta) 2 | % This function gives delX, delY of repulsion caused by the obstacle 3 | 4 | inf = 10; 5 | dObs = sqrt((ox-vx)^2 + (oy-vy)^2); % distance bw goal and current position 6 | thetaO = atan2((oy-vy),(ox-vx)); % angle between goal and current position 7 | % delXO = 0; delYO = 0; 8 | 9 | if dObs=obsRad) 13 | delXO = -beta*(obsS + obsRad - dObs)*cos(thetaO); 14 | delYO = -beta*(obsS + obsRad - dObs)*sin(thetaO); 15 | else 16 | delXO = 0; 17 | delYO = 0; 18 | end 19 | 20 | end -------------------------------------------------------------------------------- /Potential Fields/Code/PotentialFields.m: -------------------------------------------------------------------------------- 1 | %%% Author: ADITYA JAIN %%%% 2 | %%% Date: 20th January, 2018 %%% 3 | %%% Topic: Potential Fields Robotics Assignment %%% 4 | 5 | clc 6 | close all 7 | clear 8 | %% Defining environment variables 9 | startPos = [5,5]; 10 | goalPos = [90, 95]; 11 | obs1Pos = [50, 50]; 12 | obsRad = 10; 13 | goalR = 0.2; % The radius of the goal 14 | goalS = 20; % The spread of attraction of the goal 15 | obsS = 30; % The spread of repulsion of the obstacle 16 | alpha = 0.8; % Strength of attraction 17 | beta = 0.6; % Strength of repulsion 18 | 19 | 20 | %% Doing the Potential Field Math here 21 | 22 | u = zeros(100, 100); 23 | v = zeros(100, 100); 24 | testu = zeros(100, 100); 25 | testv = zeros(100, 100); 26 | 27 | 28 | for x = 1:1:100 29 | for y = 1:1:100 30 | [uG, vG] = GoalDelta(x, y, goalPos(1), goalPos(2), goalR, goalS, alpha); 31 | [uO, vO] = ObsDelta(x, y, obs1Pos(2), obs1Pos(1), obsRad, obsS, beta); 32 | xnet = uG + uO; 33 | ynet = vG + vO; 34 | vspeed = sqrt(xnet^2 + ynet^2); 35 | theta = atan2(ynet,xnet); 36 | u(x,y) = vspeed*cos(theta); 37 | v(x,y) = vspeed*sin(theta); 38 | % hold on 39 | 40 | end 41 | end 42 | %% 43 | [X,Y] = meshgrid(1:1:100,1:1:100); 44 | figure 45 | quiver(X, Y, u, v, 3) 46 | 47 | 48 | %% Defining the grid 49 | 50 | % Plotting the obstacles 51 | circles(obs1Pos(1),obs1Pos(2),obsRad, 'facecolor','red') 52 | axis square 53 | 54 | hold on % Plotting start position 55 | circles(startPos(1),startPos(2),2, 'facecolor','green') 56 | 57 | hold on % Plotting goal position 58 | circles(goalPos(1),goalPos(2),2, 'facecolor','yellow') 59 | 60 | %% Priting of the path 61 | currentPos = startPos; 62 | x = 0; 63 | 64 | while sqrt((goalPos(1)-currentPos(1))^2 + (goalPos(2)-currentPos(2))^2) > 1 65 | tempPos = currentPos + [u(currentPos(1),currentPos(2)), v(currentPos(1),currentPos(2))] 66 | currentPos = round(tempPos) 67 | hold on 68 | plot(currentPos(1),currentPos(2),'-o', 'MarkerFaceColor', 'black') 69 | pause(0.5) 70 | end 71 | -------------------------------------------------------------------------------- /Potential Fields/Code/PotentialFields_Q2.m: -------------------------------------------------------------------------------- 1 | %%% Author: ADITYA JAIN %%%% 2 | %%% Date: 20th January, 2018 %%% 3 | %%% Topic: Potential Fields Robotics Assignment %%% 4 | 5 | clc 6 | close all 7 | clear 8 | %% Defining environment variables 9 | startPos = [5,15]; 10 | goalPos = [90, 95]; 11 | obs1Pos = [50, 50]; 12 | obs2Pos = [30, 80]; 13 | obsRad = 10; 14 | goalR = 0.2; % The radius of the goal 15 | goalS = 20; % The spread of attraction of the goal 16 | obsS = 20; % The spread of repulsion of the obstacle 17 | alpha = 0.8; % Strength of attraction 18 | beta = 0.9; % Strength of repulsion 19 | 20 | 21 | %% Doing the Potential Field Math here 22 | 23 | u = zeros(100, 100); 24 | v = zeros(100, 100); 25 | testu = zeros(100, 100); 26 | testv = zeros(100, 100); 27 | 28 | 29 | for x = 1:1:100 30 | for y = 1:1:100 31 | [uG, vG] = GoalDelta(x, y, goalPos(1), goalPos(2), goalR, goalS, alpha); 32 | [uO, vO] = ObsDelta(x, y, obs1Pos(2), obs1Pos(1), obsRad, obsS, beta); 33 | [uO2, vO2] = ObsDelta(x, y, obs2Pos(2), obs2Pos(1), obsRad, obsS, beta); 34 | xnet = uG + uO + uO2; 35 | ynet = vG + vO + vO2; 36 | vspeed = sqrt(xnet^2 + ynet^2); 37 | theta = atan2(ynet,xnet); 38 | u(x,y) = vspeed*cos(theta); 39 | v(x,y) = vspeed*sin(theta); 40 | % hold on 41 | 42 | end 43 | end 44 | %% 45 | [X,Y] = meshgrid(1:1:100,1:1:100); 46 | figure 47 | quiver(X, Y, u, v, 3) 48 | 49 | 50 | %% Defining the grid 51 | 52 | % Plotting the obstacles 53 | circles(obs1Pos(1),obs1Pos(2),obsRad, 'facecolor','red') 54 | axis square 55 | 56 | hold on 57 | circles(obs2Pos(1),obs2Pos(2),obsRad, 'facecolor','red') 58 | 59 | hold on % Plotting start position 60 | circles(startPos(1),startPos(2),2, 'facecolor','green') 61 | 62 | hold on % Plotting goal position 63 | circles(goalPos(1),goalPos(2),2, 'facecolor','yellow') 64 | 65 | %% Priting of the path 66 | currentPos = startPos; 67 | x = 0; 68 | 69 | while sqrt((goalPos(1)-currentPos(1))^2 + (goalPos(2)-currentPos(2))^2) > 1 70 | tempPos = currentPos + [u(currentPos(1),currentPos(2)), v(currentPos(1),currentPos(2))] 71 | currentPos = round(tempPos) 72 | hold on 73 | plot(currentPos(1),currentPos(2),'o', 'MarkerFaceColor', 'black') 74 | pause(0.5) 75 | end 76 | -------------------------------------------------------------------------------- /Potential Fields/Code/README.md: -------------------------------------------------------------------------------- 1 | # Running the code 2 | 3 | 1. Navigate to the codes folder 4 | 2. Run the "PotentialFields.m" file for 1 obstacle environment and "PotentialFields_Q2.m" for 2 obstacle environment. 5 | 6 | Note: other files are helper files for the above main scripts 7 | -------------------------------------------------------------------------------- /Potential Fields/Code/circles.m: -------------------------------------------------------------------------------- 1 | function [ h ] = circles(x,y,r,varargin) 2 | % h = circles(x,y,r,varargin) plots circles of radius r at points x and y. 3 | % x, y, and r can be scalars or N-D arrays. 4 | % 5 | % Chad Greene, March 2014. Updated August 2014. 6 | % University of Texas Institute for Geophysics. 7 | % 8 | %% Syntax 9 | % circles(x,y,r) 10 | % circles(...,'points',numberOfPoints) 11 | % circles(...,'rotation',degreesRotation) 12 | % circles(...,'ColorProperty',ColorValue) 13 | % circles(...,'LineProperty',LineValue) 14 | % h = circles(...) 15 | % 16 | %% Description 17 | % 18 | % circles(x,y,r) plots circle(s) of radius or radii r centered at points given by 19 | % x and y. Inputs x, y, and r may be any combination of scalar, 20 | % vector, or 2D matrix, but dimensions of all nonscalar inputs must agree. 21 | % 22 | % circles(...,'points',numberOfPoints) allows specification of how many points to use 23 | % for the outline of each circle. Default value is 1000, but this may be 24 | % increased to increase plotting resolution. Or you may specify a small 25 | % number (e.g. 4 to plot a square, 5 to plot a pentagon, etc.). 26 | % 27 | % circles(...,'rotation',degreesRotation) rotates the shape by a given 28 | % degreesRotation, which can be a scalar or a matrix. This is useless for 29 | % circles, but may be desired for polygons with a discernible number of corner points. 30 | % 31 | % circles(...,'ColorProperty',ColorValue) allows declaration of 32 | % 'facecolor' or 'facealpha' 33 | % as name-value pairs. Try declaring any fill property as name-value pairs. 34 | % 35 | % circles(...,'LineProperty',LineValue) allows declaration of 'edgecolor', 36 | % 'linewidth', etc. 37 | % 38 | % h = circles(...) returns the handle(s) h of the plotted object(s). 39 | % 40 | % 41 | %% EXAMPLES: 42 | % 43 | % Example 1: 44 | % circles(5,10,3) 45 | % 46 | % % Example 2: 47 | % x = 2:7; 48 | % y = [5,15,12,25,3,18]; 49 | % r = [3 4 5 5 7 3]; 50 | % figure 51 | % circles(x,y,r) 52 | % 53 | % % Example 3: 54 | % figure 55 | % circles(1:10,5,2) 56 | % 57 | % % Example 4: 58 | % figure 59 | % circles(5,15,1:5,'facecolor','none') 60 | % 61 | % % Example 5: 62 | % figure 63 | % circles(5,10,3,'facecolor','green') 64 | % 65 | % % Example 6: 66 | % figure 67 | % h = circles(5,10,3,'edgecolor',[.5 .2 .9]) 68 | % 69 | % % Example 7: 70 | % lat = repmat((10:-1:1)',1,10); 71 | % lon = repmat(1:10,10,1); 72 | % r = .4; 73 | % figure 74 | % h1 = circles(lon,lat,r,'linewidth',4,'edgecolor','m','facecolor',[.6 .4 .8]); 75 | % hold on; 76 | % h2 = circles(1:.5:10,((1:.5:10).^2)/10,.12,'edgecolor','k','facecolor','none'); 77 | % axis equal 78 | % 79 | % % Example 8: Circles have corners 80 | % This script approximates circles with 1000 points. If all those points 81 | % are too complex for your Pentium-II, you can reduce the number of points 82 | % used to make each circle. If 1000 points is not high enough resolution, 83 | % you can increase the number of points. Or if you'd like to draw 84 | % triangles or squares, or pentagons, you can significantly reduce the 85 | % number of points. Let's try drawing a stop sign: 86 | % 87 | % figure 88 | % h = circles(1,1,10,'points',8,'color','red'); 89 | % axis equal 90 | % % and we see that our stop sign needs to be rotated a little bit, so we'll 91 | % % delete the one we drew and try again: 92 | % delete(h) 93 | % h = circles(1,1,10,'points',8,'color','red','rot',45/2); 94 | % text(1,1,'STOP','fontname','helvetica CY',... 95 | % 'horizontalalignment','center','fontsize',140,... 96 | % 'color','w','fontweight','bold') 97 | % 98 | % figure 99 | % circles([1 3 5],2,1,'points',4,'rot',[0 45 35]) 100 | % 101 | % 102 | % TIPS: 103 | % 1. Include the name-value pair 'facecolor','none' to draw outlines 104 | % (non-filled) circles. 105 | % 106 | % 2. Follow the circles command with axis equal to fix distorted circles. 107 | % 108 | % See also: fill, patch, and scatter. 109 | 110 | %% Check inputs: 111 | 112 | assert(isnumeric(x),'Input x must be numeric.') 113 | assert(isnumeric(y),'Input y must be numeric.') 114 | assert(isnumeric(r),'Input r must be numeric.') 115 | 116 | if ~isscalar(x) && ~isscalar(y) 117 | assert(numel(x)==numel(y),'If neither x nor y is a scalar, their dimensions must match.') 118 | end 119 | if ~isscalar(x) && ~isscalar(r) 120 | assert(numel(x)==numel(r),'If neither x nor r is a scalar, their dimensions must match.') 121 | end 122 | if ~isscalar(r) && ~isscalar(y) 123 | assert(numel(r)==numel(y),'If neither y nor r is a scalar, their dimensions must match.') 124 | end 125 | 126 | %% Parse inputs: 127 | 128 | % Define number of points per circle: 129 | tmp = strcmpi(varargin,'points')|strcmpi(varargin,'NOP')|strcmpi(varargin,'corners')|... 130 | strncmpi(varargin,'vert',4); 131 | if any(tmp) 132 | NOP = varargin{find(tmp)+1}; 133 | tmp(find(tmp)+1)=1; 134 | varargin = varargin(~tmp); 135 | else 136 | NOP = 1000; % 1000 points on periphery by default 137 | end 138 | 139 | % Define rotation 140 | tmp = strncmpi(varargin,'rot',3); 141 | if any(tmp) 142 | rotation = varargin{find(tmp)+1}; 143 | assert(isnumeric(rotation)==1,'Rotation must be numeric.') 144 | rotation = rotation*pi/180; % converts to radians 145 | tmp(find(tmp)+1)=1; 146 | varargin = varargin(~tmp); 147 | else 148 | rotation = 0; % no rotation by default. 149 | end 150 | 151 | % Be forgiving if the user enters "color" instead of "facecolor" 152 | tmp = strcmpi(varargin,'color'); 153 | if any(tmp) 154 | varargin{tmp} = 'facecolor'; 155 | end 156 | 157 | %% Begin operations: 158 | 159 | % Make inputs column vectors: 160 | x = x(:); 161 | y = y(:); 162 | r = r(:); 163 | rotation = rotation(:); 164 | 165 | % Determine how many circles to plot: 166 | numcircles = max([length(x) length(y) length(r) length(rotation)]); 167 | 168 | % Create redundant arrays to make the plotting loop easy: 169 | if length(x) 7 | Green circle: start position
8 | Red circle: obstacle(s)
9 | Yellow circle: goal position
10 | ![alt text](https://github.com/adityajain07/Path-Planning-Algorithms/blob/master/Potential%20Fields/Plots/potentialFields.png) 11 | 12 | 13 | ## Path: 1-Obstacle Environment 14 | ![alt text](https://github.com/adityajain07/Path-Planning-Algorithms/blob/master/Potential%20Fields/Plots/1obstacle.png) 15 | 16 | ## Path: 2-Obstacle Environment 17 | ![alt text](https://github.com/adityajain07/Path-Planning-Algorithms/blob/master/Potential%20Fields/Plots/2obstacle.png) 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Path-Planning-Algorithms 2 | 3 | This repository contains MATLAB codes for popular path planning algorithms like potential fields, visibility graph, RRT and RRT* 4 | -------------------------------------------------------------------------------- /RRT/Code/CheckIntersection.m: -------------------------------------------------------------------------------- 1 | % This script checks if two line segments intersect 2 | % Used to check intersection between qnew and qnear with all obstacle edges 3 | % returns 1 if no intersection 4 | function t = CheckIntersection(qnear,qnewtemp,edges) 5 | 6 | ledgeSize = size(edges); 7 | noEdges = ledgeSize(1); 8 | 9 | 10 | 11 | % find equation of line between qnear and qnewtemp 12 | p1 = qnear; 13 | q1 = qnewtemp; 14 | m1 = (q1(2)-p1(2))/(q1(1)-p1(1)); 15 | c1 = p1(2) - m1*(p1(1)); 16 | %%%%%%%%%%%% 17 | 18 | t = 1; % flag to check if the edge has any intersection with any other edge; 19 | % '1' means no intersection 20 | % need to compare with the edges 21 | for k = 1:noEdges 22 | ed = edges(k,:); 23 | m2 = (ed(4) - ed(3))/(ed(2) - ed(1)); 24 | if(ed(2)==ed(1)) 25 | m2 = 1e+10; 26 | end 27 | c2 = ed(3) - m2*ed(1); 28 | 29 | if m1==m2 %% ignoring 30 | t = 1; 31 | else 32 | 33 | %%%%%% 34 | temp1 = ed(3) - m1*ed(1) - c1; 35 | temp2 = ed(4) - m1*ed(2) - c1; 36 | 37 | temp3 = p1(2) - m2*p1(1) - c2; 38 | temp4 = q1(2) - m2*q1(1) - c2; 39 | 40 | if (sign(temp1) ~= sign(temp2)) && sign(temp1)~=0 && sign(temp2)~=0 && (sign(temp3) ~= sign(temp4)) && sign(temp3)~=0 && sign(temp4)~=0 41 | t = 0; 42 | break 43 | end 44 | %%%%%% 45 | end 46 | 47 | end 48 | end -------------------------------------------------------------------------------- /RRT/Code/EuclDist.m: -------------------------------------------------------------------------------- 1 | % This scripts returns the euclidean distance between two points 2 | function dis = EuclDist(p,q) 3 | dis = sqrt(sum((p - q) .^ 2)); 4 | end -------------------------------------------------------------------------------- /RRT/Code/IfOnEdge.m: -------------------------------------------------------------------------------- 1 | % This script checks if the new node to be added is not lying on any of 2 | % the obstacle edges 3 | function res = IfOnEdge(p,edges) 4 | 5 | ledgeSize = size(edges); 6 | noEdges = ledgeSize(1); 7 | 8 | for k = 1:noEdges 9 | ed = edges(k,:); 10 | m = (ed(4) - ed(3))/(ed(2) - ed(1)); 11 | if(ed(2)==ed(1)) 12 | m = 1e+10; 13 | end 14 | c = ed(3) - m*ed(1); 15 | 16 | res = p(1,2) - m*p(1,1) - c; 17 | 18 | if (res == 0) 19 | break 20 | end 21 | 22 | end 23 | 24 | end -------------------------------------------------------------------------------- /RRT/Code/NearestNode.m: -------------------------------------------------------------------------------- 1 | % Given input a graph and a random point in the grid, this script returns 2 | % the nearest node in the graph to that point 3 | function n = NearestNode(G,q) 4 | sizeG = size(G.Nodes); 5 | dmin = 1e+10; % min distance 6 | 7 | for i=1:sizeG(1,1) % length of number of nodes in the graph 8 | temp = G.Nodes{i,1}; 9 | point = NodeToPoint(temp); 10 | % D = sqrt(sum((q - point) .^ 2)); 11 | D = EuclDist(q,point); 12 | 13 | if (D 12 | Yellow circle: goal position
13 | ![alt text](https://github.com/adityajain07/Path-Planning-Algorithms/blob/master/RRT/Plots/RRT_Conf1.png) 14 | 15 | 16 | 17 | 18 | ## Path: 2nd Environment 19 | ![alt text](https://github.com/adityajain07/Path-Planning-Algorithms/blob/master/RRT/Plots/RRT_Conf2.png) 20 | 21 | 22 | 23 | 24 | ## Path: 3rd Environment 25 | ![alt text](https://github.com/adityajain07/Path-Planning-Algorithms/blob/master/RRT/Plots/RRT_Conf3.png) 26 | -------------------------------------------------------------------------------- /RRT_Star/Code/CheckIntersection.m: -------------------------------------------------------------------------------- 1 | % This script checks if two line segments intersect 2 | % Used to check intersection between qnew and qnear with all obstacle edges 3 | % returns 1 if no intersection 4 | function t = CheckIntersection(qnear,qnewtemp,edges) 5 | 6 | ledgeSize = size(edges); 7 | noEdges = ledgeSize(1); 8 | 9 | 10 | 11 | % find equation of line between qnear and qnewtemp 12 | p1 = qnear; 13 | q1 = qnewtemp; 14 | m1 = (q1(2)-p1(2))/(q1(1)-p1(1)); 15 | c1 = p1(2) - m1*(p1(1)); 16 | %%%%%%%%%%%% 17 | 18 | t = 1; % flag to check if the edge has any intersection with any other edge; 19 | % '1' means no intersection 20 | % need to compare with the edges 21 | for k = 1:noEdges 22 | ed = edges(k,:); 23 | m2 = (ed(4) - ed(3))/(ed(2) - ed(1)); 24 | if(ed(2)==ed(1)) 25 | m2 = 1e+10; 26 | end 27 | c2 = ed(3) - m2*ed(1); 28 | 29 | if m1==m2 %% ignoring 30 | t = 1; 31 | else 32 | 33 | %%%%%% 34 | temp1 = ed(3) - m1*ed(1) - c1; 35 | temp2 = ed(4) - m1*ed(2) - c1; 36 | 37 | temp3 = p1(2) - m2*p1(1) - c2; 38 | temp4 = q1(2) - m2*q1(1) - c2; 39 | 40 | if (sign(temp1) ~= sign(temp2)) && sign(temp1)~=0 && sign(temp2)~=0 && (sign(temp3) ~= sign(temp4)) && sign(temp3)~=0 && sign(temp4)~=0 41 | t = 0; 42 | break 43 | end 44 | %%%%%% 45 | end 46 | 47 | end 48 | end -------------------------------------------------------------------------------- /RRT_Star/Code/EuclDist.m: -------------------------------------------------------------------------------- 1 | % This scripts returns the euclidean distance between two points 2 | function dis = EuclDist(p,q) 3 | dis = sqrt(sum((p - q) .^ 2)); 4 | end -------------------------------------------------------------------------------- /RRT_Star/Code/IfOnEdge.m: -------------------------------------------------------------------------------- 1 | % This script checks if the new node to be added is not lying on any of 2 | % the obstacle edges 3 | function res = IfOnEdge(p,edges) 4 | 5 | ledgeSize = size(edges); 6 | noEdges = ledgeSize(1); 7 | 8 | for k = 1:noEdges 9 | ed = edges(k,:); 10 | m = (ed(4) - ed(3))/(ed(2) - ed(1)); 11 | if(ed(2)==ed(1)) 12 | m = 1e+10; 13 | end 14 | c = ed(3) - m*ed(1); 15 | 16 | res = p(1,2) - m*p(1,1) - c; 17 | 18 | if (res == 0) 19 | break 20 | end 21 | 22 | end 23 | 24 | end -------------------------------------------------------------------------------- /RRT_Star/Code/MinCostNode.m: -------------------------------------------------------------------------------- 1 | % This script returns the node to which we have to connect for minimum cost 2 | % to the start node 3 | 4 | function minNode = MinCostNode(G,q,vic,start,qnear) 5 | sizeG = size(G.Nodes); 6 | minCost = 1e+10; % min distance 7 | minNode = qnear; % initialising 8 | for i=1:sizeG(1,1) % length of number of nodes in the graph 9 | temp = G.Nodes{i,1}; 10 | point = NodeToPoint(temp); 11 | 12 | if (EuclDist(q,point) <= vic) 13 | path = shortestpath(G, PointToNode(start), temp); 14 | pathSize = size(path); 15 | 16 | totalDis = 0; 17 | for j=1:pathSize(2)-1 18 | t1 = path{j}; 19 | t2 = path{j+1}; 20 | 21 | p1 = NodeToPoint(t1); 22 | p2 = NodeToPoint(t2); 23 | totalDis = totalDis + EuclDist(p1,p2); 24 | 25 | end 26 | cost = totalDis + EuclDist(q,point); 27 | 28 | if (cost < minCost) 29 | minCost = cost; 30 | minNode = point; 31 | end 32 | end 33 | 34 | end 35 | end -------------------------------------------------------------------------------- /RRT_Star/Code/NearestNode.m: -------------------------------------------------------------------------------- 1 | % Given input a graph and a random point in the grid, this script returns 2 | % the nearest node in the graph to that point 3 | function n = NearestNode(G,q) 4 | sizeG = size(G.Nodes); 5 | dmin = 1e+10; % min distance 6 | 7 | for i=1:sizeG(1,1) % length of number of nodes in the graph 8 | temp = G.Nodes{i,1}; 9 | point = NodeToPoint(temp); 10 | % D = sqrt(sum((q - point) .^ 2)); 11 | D = EuclDist(q,point); 12 | 13 | if (D 6 | qrand: randomly chosen point in the environment
7 | qnear: nearest node to qrand
8 | qnew: new node in the direction of qrand and step size 's' from qnear
9 | 1. After qnew has been found, it is not connected to qnear unlike in RRT. All the nodes in neighbourhood (specified radius) of qnew is checked; the one which gives the shortest path from qnew to the start node is chosen. qnew is connected to this node. 10 | 2. After the above connection and again in the same neighbourhood of qnew, the path from start to all these neighbour nodes is calculated. If this path is more than the path from start to qnew and from qnew to the node, then the parent of this neighbour node is removed and qnew is made the new parent.
11 |
12 | The algorithm has been borrowed from this paper: https://arxiv.org/pdf/1704.04585.pdf
13 | 14 | Note: Only the first rewiring has been implemented till now. 15 | 16 | ## Points to Note 17 | 1. The obstacles have been deterministically placed in the environment 18 | 2. Shortest path in the graph is calculated using BFS 19 | 3. The code also outputs total path length in unit distance and time taken to execute the code 20 | 21 | 22 | ## Path: 1st Environment 23 | Green circle: start position
24 | Yellow circle: goal position
25 | ![alt text](https://github.com/adityajain07/Path-Planning-Algorithms/blob/master/RRT_Star/Plots/RRTstar_Conf1.png) 26 | 27 | 28 | 29 | 30 | ## Path: 2nd Environment 31 | ![alt text](https://github.com/adityajain07/Path-Planning-Algorithms/blob/master/RRT_Star/Plots/RRTstar_Conf2.png) 32 | 33 | 34 | 35 | 36 | ## Path: 3rd Environment 37 | ![alt text](https://github.com/adityajain07/Path-Planning-Algorithms/blob/master/RRT_Star/Plots/RRTstar_Conf3.png) 38 | -------------------------------------------------------------------------------- /Report_PDF.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adityajain07/Path-Planning-Algorithms/48c3db87d330033e40407ad9021b10981584cef1/Report_PDF.pdf -------------------------------------------------------------------------------- /Visibility Graph/Code/EuclDist.m: -------------------------------------------------------------------------------- 1 | % This scripts returns the euclidean distance between two points 2 | function dis = EuclDist(p,q) 3 | dis = sqrt(sum((p - q) .^ 2)); 4 | end -------------------------------------------------------------------------------- /Visibility Graph/Code/README.md: -------------------------------------------------------------------------------- 1 | # Running the code 2 | 3 | 1. Navigate to the code folder 4 | 2. Run "visible_graph.m" file 5 | 6 | Note: 7 | - other files are helper files for the above main scripts 8 | - To switch between the three environment configurations, comment the others in visible_graph.m (well documented and commented code) 9 | -------------------------------------------------------------------------------- /Visibility Graph/Code/circles.m: -------------------------------------------------------------------------------- 1 | function [ h ] = circles(x,y,r,varargin) 2 | % h = circles(x,y,r,varargin) plots circles of radius r at points x and y. 3 | % x, y, and r can be scalars or N-D arrays. 4 | % 5 | % Chad Greene, March 2014. Updated August 2014. 6 | % University of Texas Institute for Geophysics. 7 | % 8 | %% Syntax 9 | % circles(x,y,r) 10 | % circles(...,'points',numberOfPoints) 11 | % circles(...,'rotation',degreesRotation) 12 | % circles(...,'ColorProperty',ColorValue) 13 | % circles(...,'LineProperty',LineValue) 14 | % h = circles(...) 15 | % 16 | %% Description 17 | % 18 | % circles(x,y,r) plots circle(s) of radius or radii r centered at points given by 19 | % x and y. Inputs x, y, and r may be any combination of scalar, 20 | % vector, or 2D matrix, but dimensions of all nonscalar inputs must agree. 21 | % 22 | % circles(...,'points',numberOfPoints) allows specification of how many points to use 23 | % for the outline of each circle. Default value is 1000, but this may be 24 | % increased to increase plotting resolution. Or you may specify a small 25 | % number (e.g. 4 to plot a square, 5 to plot a pentagon, etc.). 26 | % 27 | % circles(...,'rotation',degreesRotation) rotates the shape by a given 28 | % degreesRotation, which can be a scalar or a matrix. This is useless for 29 | % circles, but may be desired for polygons with a discernible number of corner points. 30 | % 31 | % circles(...,'ColorProperty',ColorValue) allows declaration of 32 | % 'facecolor' or 'facealpha' 33 | % as name-value pairs. Try declaring any fill property as name-value pairs. 34 | % 35 | % circles(...,'LineProperty',LineValue) allows declaration of 'edgecolor', 36 | % 'linewidth', etc. 37 | % 38 | % h = circles(...) returns the handle(s) h of the plotted object(s). 39 | % 40 | % 41 | %% EXAMPLES: 42 | % 43 | % Example 1: 44 | % circles(5,10,3) 45 | % 46 | % % Example 2: 47 | % x = 2:7; 48 | % y = [5,15,12,25,3,18]; 49 | % r = [3 4 5 5 7 3]; 50 | % figure 51 | % circles(x,y,r) 52 | % 53 | % % Example 3: 54 | % figure 55 | % circles(1:10,5,2) 56 | % 57 | % % Example 4: 58 | % figure 59 | % circles(5,15,1:5,'facecolor','none') 60 | % 61 | % % Example 5: 62 | % figure 63 | % circles(5,10,3,'facecolor','green') 64 | % 65 | % % Example 6: 66 | % figure 67 | % h = circles(5,10,3,'edgecolor',[.5 .2 .9]) 68 | % 69 | % % Example 7: 70 | % lat = repmat((10:-1:1)',1,10); 71 | % lon = repmat(1:10,10,1); 72 | % r = .4; 73 | % figure 74 | % h1 = circles(lon,lat,r,'linewidth',4,'edgecolor','m','facecolor',[.6 .4 .8]); 75 | % hold on; 76 | % h2 = circles(1:.5:10,((1:.5:10).^2)/10,.12,'edgecolor','k','facecolor','none'); 77 | % axis equal 78 | % 79 | % % Example 8: Circles have corners 80 | % This script approximates circles with 1000 points. If all those points 81 | % are too complex for your Pentium-II, you can reduce the number of points 82 | % used to make each circle. If 1000 points is not high enough resolution, 83 | % you can increase the number of points. Or if you'd like to draw 84 | % triangles or squares, or pentagons, you can significantly reduce the 85 | % number of points. Let's try drawing a stop sign: 86 | % 87 | % figure 88 | % h = circles(1,1,10,'points',8,'color','red'); 89 | % axis equal 90 | % % and we see that our stop sign needs to be rotated a little bit, so we'll 91 | % % delete the one we drew and try again: 92 | % delete(h) 93 | % h = circles(1,1,10,'points',8,'color','red','rot',45/2); 94 | % text(1,1,'STOP','fontname','helvetica CY',... 95 | % 'horizontalalignment','center','fontsize',140,... 96 | % 'color','w','fontweight','bold') 97 | % 98 | % figure 99 | % circles([1 3 5],2,1,'points',4,'rot',[0 45 35]) 100 | % 101 | % 102 | % TIPS: 103 | % 1. Include the name-value pair 'facecolor','none' to draw outlines 104 | % (non-filled) circles. 105 | % 106 | % 2. Follow the circles command with axis equal to fix distorted circles. 107 | % 108 | % See also: fill, patch, and scatter. 109 | 110 | %% Check inputs: 111 | 112 | assert(isnumeric(x),'Input x must be numeric.') 113 | assert(isnumeric(y),'Input y must be numeric.') 114 | assert(isnumeric(r),'Input r must be numeric.') 115 | 116 | if ~isscalar(x) && ~isscalar(y) 117 | assert(numel(x)==numel(y),'If neither x nor y is a scalar, their dimensions must match.') 118 | end 119 | if ~isscalar(x) && ~isscalar(r) 120 | assert(numel(x)==numel(r),'If neither x nor r is a scalar, their dimensions must match.') 121 | end 122 | if ~isscalar(r) && ~isscalar(y) 123 | assert(numel(r)==numel(y),'If neither y nor r is a scalar, their dimensions must match.') 124 | end 125 | 126 | %% Parse inputs: 127 | 128 | % Define number of points per circle: 129 | tmp = strcmpi(varargin,'points')|strcmpi(varargin,'NOP')|strcmpi(varargin,'corners')|... 130 | strncmpi(varargin,'vert',4); 131 | if any(tmp) 132 | NOP = varargin{find(tmp)+1}; 133 | tmp(find(tmp)+1)=1; 134 | varargin = varargin(~tmp); 135 | else 136 | NOP = 1000; % 1000 points on periphery by default 137 | end 138 | 139 | % Define rotation 140 | tmp = strncmpi(varargin,'rot',3); 141 | if any(tmp) 142 | rotation = varargin{find(tmp)+1}; 143 | assert(isnumeric(rotation)==1,'Rotation must be numeric.') 144 | rotation = rotation*pi/180; % converts to radians 145 | tmp(find(tmp)+1)=1; 146 | varargin = varargin(~tmp); 147 | else 148 | rotation = 0; % no rotation by default. 149 | end 150 | 151 | % Be forgiving if the user enters "color" instead of "facecolor" 152 | tmp = strcmpi(varargin,'color'); 153 | if any(tmp) 154 | varargin{tmp} = 'facecolor'; 155 | end 156 | 157 | %% Begin operations: 158 | 159 | % Make inputs column vectors: 160 | x = x(:); 161 | y = y(:); 162 | r = r(:); 163 | rotation = rotation(:); 164 | 165 | % Determine how many circles to plot: 166 | numcircles = max([length(x) length(y) length(r) length(rotation)]); 167 | 168 | % Create redundant arrays to make the plotting loop easy: 169 | if length(x)