P Set9
P Set9
UZB 438E
PROBLEM SET 9
By
Enes Sagnak 110200063
Murad Abu-Khalaf
1 Question 1 2
1.1 Plots ......................................................................................................................... 11
1.2 Summary................................................................................................................... 11
2 Question 2 14
2.1 Summary................................................................................................................... 26
3 REFERENCES 27
1
1. QUESTION 1
The code is based on the source code from Lecture 10 slides and originally demonstrated the use of
Probabilistic Roadmaps (PRM) for motion planning and control of a two-link elbow arm planar
manipulator. The environment was initially defined with a static obstacle and fixed starting and goal
positions. The code is modified for the environment to improve collision detection and tailor the planner to
suit their specific problem. This included adjustments to visibility conditions and the configuration of the
adjacency matrix to enhance collision avoidance.
Inverse kinematics adjustments were also made to improve the precision of the solutions. The step
size in inverse kinematics was made dynamic, adapting to the manipulatorâĂŹs configuration. This
change aimed to refine the path planning process and produce more accurate joint angle solutions.
The trajectory generation was modified to include path smoothing techniques using cubic polynomial
interpolation. These techniques help in creating smoother, more stable paths, which is crucial for
real-time applications where sudden changes in joint angles can lead to instability.
Additionally, for the kinematic planning, the proportional gain (Kp) was set to 50, and the velocity gain
(Kv) was tuned to 5, based on several tested values. The best results were achieved at these specific
settings, balancing path smoothness and stability.
2
Enes SAGNAK UZB438E Homework3
2 % Planning and Control for a Two Link Elbow Arm Planar Manipulator .
3 clc ; clear ; close all ;
4 rng ( ’ default ’) ; % Set the seed for the random number generator
5
14 % Obtain a collision - free configuration for the End - Effector ’ s START position
15 for i = 1:5
16 StartConfig = g e t I n v e r s e K i n e m a t i c s ( startPosition , 2 * ( rand (2 , 1) - 0.5) * pi ) ;
17 [ jointX1 , jointY1 , endEffectorX , endEffectorY ] =
g e t F o r w a r d K i n e m a t i c s ( StartConfig (1) , StartConfig (2) ) ;
18 isCollision = d et ec t Co ll i si on ( envMap , jointX1 , endEffectorX , jointY1 , endEffectorY ,
10) ;
19 if ~ isCollision && sqrt (( endEffectorX - startPosition (1) ) ^2 + ( endEffectorY -
startPosition (2) ) ^2) < 0.1
20 break ; % Exit the loop if there is no collision and the end - effector is close to
the start position
21 end
22 end
23
41 if isfinite ( s h o r t e s t D i s t a n c e )
42 roadmap = addedge ( roadmap , roadmap . numnodes , nearestNodeIndex , d ) ; % Add the new
3
Enes SAGNAK UZB438E Homework3
45 % Obtain a collision - free configuration for the End - Effector ’ s GOAL position
46 for i = 1:5
47 GoalConfig = g e t I n v e r s e K i n e m a t i c s ( goalPosition , 2 * ( rand (2 , 1) - 0.5) * pi ) ;
48 [ jointX1 , jointY1 , endEffectorX , endEffectorY ] = g e t F o r w a r d K i n e m a t i c s ( GoalConfig (1) ,
GoalConfig (2) ) ;
49 isCollision = d et ec t Co ll i si on ( envMap , jointX1 , endEffectorX , jointY1 , endEffectorY ,
10) ;
50 if ~ isCollision && sqrt (( endEffectorX - goalPosition (1) ) ^2 + ( endEffectorY -
goalPosition (2) ) ^2) < 0.1
51 break ; % Exit the loop if there is no collision and the end - effector is close to
the goal position
52 end
53 end
54
61 % Find the nearest node in the PRM for the goal configuration
62 s h o r t e s t D i s t a n c e = inf ; n e a r e s t N o d e I n d e x = 0;
63 for i = 1: n N o d e s B e f o r e A d d i n g
64 % Input must be row vectors
65 d = v i s i b l e C o n f i g u r a t i o n ( envMap , GoalConfig ’ , roadmap . Nodes . s ampled Config s (i ,:) , 5) ;
66 if d < s h o r t e s t D i s t a n c e
67 n e a r e s t N o d e I n d e x = i ; % Update the index of the nearest node
68 s h o r t e s t D i s t a n c e = d ; % Update the shortest distance
69 end
70 end
71
72 if isfinite ( s h o r t e s t D i s t a n c e )
73 roadmap = addedge ( roadmap , roadmap . numnodes , nearestNodeIndex , d ) ; % Add the goal
node to the roadmap
74 end
75
4
Enes SAGNAK UZB438E Homework3
92
93 for i = 1: n
94 [~ , u (i ,:) , Qd (i ,:) ,~ ,~] = T w o L i n k P l a n a r E l b o w A r m ( t ( i ) ,x (i ,:) ) ;
95 end
96 figure (1) ; hold on ;
97 plot (t , x (: ,1:2) , ’ LineWidth ’ ,2) ;
98 xlabel ( ’ Time ( s ) ’ , ’ FontSize ’ ,20) ;
99 ylabel ( ’ rad ’ , ’ FontSize ’ ,20) ;
100 legend ({ ’$ \ theta_1 ( t ) $ ’ , ’$ \ theta_2 ( t ) $ ’} , ’ FontSize ’ ,20 , ’ Interpreter ’ , ’ latex ’)
101 title ( ’ Arm Position Angles $ \ theta_1 ( t ) $ \&
$ \ theta_2 ( t ) $ ’ , ’ FontSize ’ ,20 , ’ Interpreter ’ , ’ latex ’) ;
102 subtitle ( ’ PD Computed - Torque , $ \ tau_d = 0 $ ’ , ’ FontSize ’ ,15 , ’ Interpreter ’ , ’ latex ’) ;
103 figure (2) ; hold on ;
104 plot (t , Qd (: ,1:2) , ’ LineWidth ’ ,2) ; xlabel ( ’ Time ( s ) ’ , ’ FontSize ’ ,20) ;
ylabel ( ’ rad ’ , ’ FontSize ’ ,20) ;
105 legend ({ ’$ \ theta_ {1 d }( t ) $ ’ , ’$ \ theta_ {2 d }( t ) $ ’} , ’ FontSize ’ ,20 , ’ Interpreter ’ , ’ latex ’)
106 title ( ’ Desired Arm Position Angles $ \ theta_ {1 d }( t ) $ \&
$ \ theta_ {2 d }( t ) $ ’ , ’ FontSize ’ ,20 , ’ Interpreter ’ , ’ latex ’) ;
107 subtitle ( ’ PD Computed - Torque , $ \ tau_d = 0 $ ’ , ’ FontSize ’ ,15 , ’ Interpreter ’ , ’ latex ’) ;
108 figure (3) ; hold on ;
109 err = Qd - x (: ,1:2) ;
110 plot (t , err , ’ LineWidth ’ ,2) ; xlabel ( ’ Time ( s ) ’ , ’ FontSize ’ ,20) ; ylabel ( ’ rad ’ , ’ FontSize ’ ,20) ;
111 legend ({ ’ $e_1 ( t ) $ ’ , ’ $e_2 ( t ) $ ’} , ’ FontSize ’ ,20 , ’ Interpreter ’ , ’ latex ’)
112 title ( ’ $e_1 ( t ) = \ theta_ {1 d }( t ) - \ theta_1 ( t ) $ and $e_2 ( t ) = \ theta_ {2 d }( t )
-\ theta_2 ( t ) $ ’ , ’ FontSize ’ ,20 , ’ Interpreter ’ , ’ latex ’) ;
113 subtitle ( ’ PD Computed - Torque , $ \ tau_d = 0 $ ’ , ’ FontSize ’ ,15 , ’ Interpreter ’ , ’ latex ’) ;
114 figure (4) ; hold on ;
115 plot (t , u (: ,1:2) , ’ LineWidth ’ ,2) ; xlabel ( ’ Time ( s ) ’ , ’ FontSize ’ ,20) ;
ylabel ( ’N . m ’ , ’ FontSize ’ ,20)
116 legend ({ ’$ \ tau_1 ( t ) $ ’ , ’$ \ tau_2 ( t ) $ ’} , ’ FontSize ’ ,20 , ’ Interpreter ’ , ’ latex ’)
117 title ( ’ Torque Input Vector $ \ tau ( t ) $ ’ , ’ FontSize ’ ,20 , ’ Interpreter ’ , ’ latex ’) ;
118 subtitle ( ’ PD Computed - Torque , $ \ tau_d = 0 $ ’ , ’ FontSize ’ ,15 , ’ Interpreter ’ , ’ latex ’) ;
119 figure (5) ; hold on ;
120 title ( ’ Two Link Planar Elbow Arm and an Obstacle ’ , ’ FontSize ’ ,20 , ’ Interpreter ’ , ’ latex ’) ;
121 axis ([ -2 2 -2 2]) ; daspect ([1 ,1 ,1]) ;
122 [ x1 , y1 , x2 , y2 ] = g e t F o r w a r d K i n e m a t i c s ( x (: ,1) ,x (: ,2) ) ;
123 % Draw Obstacle
124 Ox = [0.4 0.6 0.6 0.8 0.8 0.6 0.6 0.4 0.4];
125 Oy = [2.0 2.0 1.6 1.6 0.8 0.8 1.2 1.2 2.0];
126 fill ( Ox , Oy , ’k ’) ;
127 Ox = [ -0.4 -0.6 -0.6 -0.8 -0.8 -0.6 -0.6 -0.4 -0.4];
5
Enes SAGNAK UZB438E Homework3
128 Oy = [ -2.0 -2.0 -1.6 -1.6 -0.8 -0.8 -1.2 -1.2 -2.0];
129 fill ( Ox , Oy , ’k ’) ;
130 Ox = [ 0.6 0.8 0.8 0.6];
131 Oy = [ -0.8 -0.8 -1.6 -1.6];
132 fill ( Ox , Oy , ’k ’) ;
133 Ox = [ -0.6 -0.8 -0.8 -0.6];
134 Oy = [ 0.8 0.8 1.6 1.6];
135 fill ( Ox , Oy , ’k ’) ;
136 % Animate the links
137 p1 = plot (0 ,0 , ’ -o ’ , ’ M ar ke r Fa ce C ol or ’ , ’ red ’) ;
138 p2 = plot (0 ,0 , ’ -o ’ , ’ M ar ke r Fa ce C ol or ’ , ’ red ’) ;
139 videoHandle = VideoWriter ( ’ T w o L i n k A n i m a t i o n V i d e o P R M . mp4 ’ , ’ MPEG -4 ’) ;
140 videoHandle . FrameRate = 60;
141 open ( videoHandle ) ;
142 for k = 2: length ( x1 )
143 p1 . XData = [0 x1 ( k ) ];
144 p1 . YData = [0 y1 ( k ) ];
145 p2 . XData = [ x1 ( k ) x2 ( k ) ];
146 p2 . YData = [ y1 ( k ) y2 ( k ) ];
147 drawnow
148 aFrame = getframe ( gcf ) ;
149 writeVideo ( videoHandle , aFrame ) ;
150 end
151 close ( videoHandle ) ;
152
6
Enes SAGNAK UZB438E Homework3
177 0 0 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 0
178 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
179 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0];
180 [ nr , nc ] = size ( p ) ; % # ok < ASGLU >
181 resolution = 5; % 5 cells / meter
182 envMap = b i n a r y O c c u p a n c y M a p (p , resolution ) ;
183 end
184 %%
185 function roadmap = createPRM ( EnvMap )
186 % Plans a sequence of collision - free waypoints for the end effector
187 nsamples = 600;
188 sam pledCo nfigs = NaN ( nsamples ,2) ;
189 s a m p l e d P o s i t i o n s = NaN ( nsamples ,2) ;
190 for i =1: nsamples
191 [ theta1 , theta2 ] = deal (2*( rand -0.5) * pi /2 , rand *2* pi ) ;
192 [ jointX1 , jointY1 , endEffectorX , endEffectorY ] = g e t F o r w a r d K i n e m a t i c s ( theta1 , theta2 ) ;
193 isCollision = d et ec t Co ll i si on ( EnvMap , jointX1 , endEffectorX , jointY1 , endEffectorY ,10) ;
194 if ~ isCollision
195 sam pledCo nfigs (i ,:) = [ theta1 , theta2 ];
196 s a m p l e d P o s i t i o n s (i ,:) = [ endEffectorX , endEffectorY ];
197 end
198 end
199 % Remove rows corresponding to samples for which collision is detected .
200 [i ,~] = find ( isnan ( s a m p l e d P o s i t i o n s ) ) ;
201 if ~ isempty ( i )
202 i = unique ( i ) ;
203 s a m p l e d P o s i t i o n s (i ,:) = [];
204 sam pledCo nfigs (i ,:) = [];
205 end
206 % Create Adjacency Matrix for the Graph
207 n e i g h b o r T h r e s h o l d = 0.5;
208 nQ = size ( sampledConfigs ,1) ;
209 A = zeros ( nQ ) ;
210 for i =1:( nQ -1)
211 for j =( i +1) : nQ
212 d = v i s i b l e C o n f i g u r a t i o n ( EnvMap , sample dConfi gs (i ,:) , sample dConfi gs (j ,:) ,5) ;
213 if d < n e i g h b o r T h r e s h o l d
214 A (i , j ) = d ; % Nodes " visible " to each other .
215 A (j , i ) = A (i , j ) ;
216 end
217 end
218 end
219 NodeTable =
table ( sampledConfigs , sampledPositions , ’ VariableNames ’ ,{ ’ sample dConfi gs ’ , ’ s a m p l e d P o s i t i o n s ’ }) ;
220 roadmap = graph (A , NodeTable ) ;
221 end
222 %%
223 function [ xdot , tau , qd , qd_dot , qd_ddot ]= T w o L i n k P l a n a r E l b o w A r m (t , x )
224 % Dynamics
7
Enes SAGNAK UZB438E Homework3
8
Enes SAGNAK UZB438E Homework3
273 else
274 % Hold the last waypoint if buffer is empty
275 q0 = roadmap . Nodes . sa mpledC onfigs ( shortestPath ( end -1) ,:) ’;
276 qT = roadmap . Nodes . sa mpledC onfigs ( shortestPath ( end ) ,:) ’;
277 delta = atan2 ( sin ( qT - q0 ) , cos ( qT - q0 ) ) ;
278 qT = q0 + delta ; % To ensure the sign direction is per checked visibility
279 q0 = qT ;
280 end
281 [ qd , qd_dot , qd_ddot ] = t r a j e c t o r y G e n e r a t o r (t , q0 , qT , T ) ;
282 end
283 %%
284 function [ qd , qd_dot , qd_ddot ] = t r a j e c t o r y G e n e r a t o r (t , q0 , qT , T )
285 % Generate trajectoy via Cubic Polynomial Interpolation
286 k = floor ( t / T ) ;
287 tk = T * k ;
288 v0 = 0.1*[1;1];
289 vT = 0.1*[1;1];
290 a = q0 ;
291 b = v0 ;
292 c = (3*( qT - q0 ) - T *( 2* v0 + vT ) ) / T ^2;
293 d = (2*( q0 - qT ) + T *( v0 + vT ) ) / T ^3;
294 qd = a + b *( t - tk ) + c *( t - tk ) ^2 + d *( t - tk ) ^3;
295 qd_dot = b + 2* c *( t - tk ) + 3* d *( t - tk ) ^2;
296 qd_ddot = 2* c + 6* d *( t - tk ) ;
297 end
298 %%
299 function q = g e t I n v e r s e K i n e m a t i c s ( yd , q )
300 % Inverse Kinematics via Jacobian Transpose Method for a 2 - Link Planar Manipulator
301 a1 = 1; a2 = 1;
302 alpha = 0.50;
303 for i =1:30
304 theta1 = q (1) ;
305 theta2 = q (2) ;
306 % Position only forward kinematics
307 fq = [ a2 * cos ( theta1 + theta2 ) + a1 * cos ( theta1 ) ;
308 a2 * sin ( theta1 + theta2 ) + a1 * sin ( theta1 ) ];
309 g = yd - fq ;
310 % The linear velocity Jacobian Jv , where Jw is ignored
311 Jv = [ - a2 * sin ( theta1 + theta2 ) - a1 * sin ( theta1 ) , - a2 * sin ( theta1 + theta2 ) ;
312 a2 * cos ( theta1 + theta2 ) + a1 * cos ( theta1 ) , a2 * cos ( theta1 + theta2 ) ];
313 q = q + alpha * Jv ’* g ;
314 end
315 end
316 %%
317 function [ jointX1 , jointY1 , endEffectorX , endEffectorY ] =
g e t F o r w a r d K i n e m a t i c s ( theta1 , theta2 )
318 % Forward Kinematics
319 a1 = 1; a2 = 1;
320 jointX1 = a1 * cos ( theta1 ) ;
9
Enes SAGNAK UZB438E Homework3
10
Enes SAGNAK UZB438E Homework3
1.1. PLOTS
Figure 1.2
1.2. SUMMARY
The code implements a PRM-based motion planning and control framework for a two-link elbow arm
planar manipulator. The process begins by creating a binary occupancy map of the environment, which
represents obstacles in the workspace. A PRM is then generated by sampling feasible configurations and
positions of the manipulator’s end-effector that avoid collisions with obstacles. These samples are
connected to form a graph where nodes represent valid configurations and edges represent feasible
paths between these configurations.
For planning, start and goal configurations are selected, and collision-free configurations are generated
for these positions. The nearest neighbors in the roadmap are identified and connected to form a path
11
Enes SAGNAK UZB438E Homework3
Figure 1.3
Figure 1.4
Figure 1.5
from the start to the goal. The shortest path is computed using graph search algorithms, and the control
input is designed to track this path. Using a PD computed-torque controller, desired angles and torque
inputs are computed, and an ODE solver is used to simulate the manipulator’s motion along the planned
trajectory. The results are visualized through desired angles, torque inputs, and error analysis to assess
the control performance.
12
Enes SAGNAK UZB438E Homework3
Figure 1.6
Figure 1.7
13
2. QUESTION 2
The original code from Lecture 10, which was based on a Rapidly-exploring Random Tree (RRT)
algorithm for motion planning of a two-link elbow arm planar manipulator, was used as a foundation. The
RRT algorithm was employed to generate feasible paths for the manipulator within a workspace, taking
into account obstacles and constraints.
The first modification involved integrating a new path smoothing technique using cubic splines to smooth
the paths generated by the RRT algorithm. This adjustment aimed to reduce unnecessary abrupt
changes in the manipulator’s path, enhancing the efficiency and smoothness of its execution.
Additionally, a more accurate collision detection method was incorporated to enhance the robustness
of the RRT algorithm in avoiding obstacles. This involved refining how collision checks were performed
during the path generation phase, ensuring that the generated paths were both feasible and safe for the
manipulator to follow.
Finally, a feedback loop mechanism was added, which adjusts the path in real-time based on the
manipulator’s current state and desired trajectory. This feedback loop helps in correcting any deviations
from the planned path, thereby improving the overall performance of the motion planning system.
The modifications were based on an analysis of the Lecture 10 source code and the solution of the
spring semester.
1
5 clc ;
6 clear ;
7 close all ;
8
14
Enes SAGNAK UZB438E Homework3
10 rng ( ’ default ’) ;
11
55 % Get the discrete sequence of feasible confi gurati ons from the RRT
56 ns = 1; % ID of START node
57 nt = G . numnodes ; % ID of GOAL node
58 aPath = G . shortestpath ( ns , nt ) ;
15
Enes SAGNAK UZB438E Homework3
59
16
Enes SAGNAK UZB438E Homework3
103
137
138
17
Enes SAGNAK UZB438E Homework3
152 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
153 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
154 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
155 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
156 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
157 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
158 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
159 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
160 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0
161 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0
162 0 0 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 0
163 0 0 0 0 0 0 1 1 0 0 0 0 0 1 0 0 0 0 0 0
164 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
165 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0];
166 [ nr , nc ] = size ( p ) ; % # ok < ASGLU >
167 resolution = 5; % 5 cells / meter
168 occupancyMap = b i n a r y O c c u p a n c y M a p (p , resolution ) ;
169 end
170 %%
171 function G = createRRT ( EnvMap , qs , qf )
172 % Plans a sequence of collision - free waypoints for the end effector
173 nsamples = 1000;
174 t r e e C o n f i g u r a t i o n s = NaN ( nsamples ,2) ;
175 treePositions = NaN ( nsamples ,2) ;
176 A = zeros ( nsamples +1) ;
177 % Add root node to tree
178 [ x1 , y1 , x2 , y2 ] = c o m p u t e F o r w a r d K i n e m a t i c s ( qs (1) , qs (2) ) ;
179 fCollision = i s Co ll i si o nF re e ( EnvMap , x1 , x2 , y1 , y2 , 30) ;
180 if ~ fCollision
181
18
Enes SAGNAK UZB438E Homework3
201 continue ;
202 end
203 % Get nearest configuration on the tree , to qsample
204 qi = t r e e C o n f i g u r a t i o n s (1: numConfigurations ,:) ’;
205 delta = atan2 ( sin ( qsample - qi ) , cos ( qsample - qi ) ) ;
206 d = sqrt ( delta (1 ,:) .^2+ delta (2 ,:) .^2) ;
207 [ dmin , idx_qnear ] = min ( d ) ;
208 qnear = t r e e C o n f i g u r a t i o n s ( idx_qnear ,:) ’;
209 % Get qnew by driving from qnear towards qsample
210 T = 0.25;
211 qm = zeros (2 ,4) ;
212 for m = 1:4
213 [~ , x ]= ode45 ( @ (t , x ) M o t i o n P r e d i c t i o n M o d e l (t ,x , con trolTo rques (: , m ) ) , [0 T ] , [ qnear (1)
qnear (2) 0 0]) ;
214 qm (: , m ) = x ( end ,1:2) ’;
215 end
216 delta = atan2 ( sin ( qsample - qm ) , cos ( qsample - qm ) ) ;
217 d = sqrt ( delta (1 ,:) .^2+ delta (2 ,:) .^2) ;
218 [ dmin , idx_qnew ] = min ( d ) ;
219 qnew = qm (: , idx_qnew ) ;
220 % Test if qnew is in collision ; if not add qnew to TREE
221 % ( NOTE : Change to test if the entire T seconds path between qnew and qnear is colliding )
222 [ x1 , y1 , x2 , y2 ] = c o m p u t e F o r w a r d K i n e m a t i c s ( qnew (1) , qnew (2) ) ;
223 fCollision = i s Co ll i si o nF re e ( EnvMap , x1 , x2 , y1 , y2 , 30) ;
224 if ~ fCollision
225 d = g e t C o n f i g u r a t i o n D i s t a n c e ( EnvMap , qnear , qnew , 5) ;
226 if isfinite ( d )
227 n u m C o n f i g u r a t i o n s = n u m C o n f i g u r a t i o n s + 1;
228 A ( idx_qnear , n u m C o n f i g u r a t i o n s ) = d ; % Nodes " visible " to each other .
229 t r e e C o n f i g u r a t i o n s ( numConfigurations ,:) = [ qnew (1) , qnew (2) ];
230 treePositions ( numConfigurations ,:) = [ x2 , y2 ];
231
232 end
233 end
234 % Test if qnew is within the vicinity of the GOAL .
235 delta = atan2 ( sin ( qf - qnew ) , cos ( qf - qnew ) ) ;
236 d = sqrt ( delta (1 ,:) .^2+ delta (2 ,:) .^2) ;
237 if d < 0.3
238 disp ( ’ Goal vicinity reached ! ’)
239 break ;
240 end
241 end
242 % Remove rows corresponding to samples for which collision is detected .
243 [i ,~] = find ( isnan ( treePositions ) ) ;
244 if ~ isempty ( i )
245 i = unique ( i ) ;
246 treePositions (i ,:) = [];
247 t r e e C o n f i g u r a t i o n s (i ,:) = [];
248 end
19
Enes SAGNAK UZB438E Homework3
279 % Gravity
280 G = [ g *( a1 * m1 * cos ( theta1 ) + a1 * m2 * cos ( theta1 ) + a2 * m2 * cos ( theta1 + theta2 ) ) ;
281 a2 * g * m2 * cos ( theta1 + theta2 ) ];
282 % Disturbance Unknown to the Controller
283 tau_d = [0;0];
284 % Control input
285 Kp = 200; Kv = 100;
286 [ qd , qd_dot , qd_ddot ] = g e t P l a n n e d T r a j e c t o r y (t ,[ theta1 ; theta2 ]) ;
287 e = qd - [ theta1 ; theta2 ];
288 edot = qd_dot - [ theta1dot ; theta2dot ];
289 con trolTo rques = M *( qd_ddot + Kv * edot + Kp * e ) + V + G ;
290 % Position / Velocity State Repr esenta tion
291 xdot = [ theta1dot ;
292 theta2dot ;
293 inv ( M ) *( -V -G - tau_d + cont rolTor ques ) ]; % # ok < MINV >
294 end
295 %%
296 function [ qd , qd_dot , qd_ddot ] = g e t P l a n n e d T r a j e c t o r y (t , qcurrent )
20
Enes SAGNAK UZB438E Homework3
21
Enes SAGNAK UZB438E Homework3
370 end
371 %%
372 function q = c o m p u t e I n v e r s e K i n e m a t i c s ( yd , q )
373 % Inverse Kinematics via Jacobian Transpose Method for a 2 - Link Planar Manipulator
374 a1 = 1; a2 = 1;
375 alpha = 0.50;
376 for i =1:30
377 theta1 = q (1) ;
378 theta2 = q (2) ;
379 % Position only forward kinematics
380 fq = [ a2 * cos ( theta1 + theta2 ) + a1 * cos ( theta1 ) ;
381 a2 * sin ( theta1 + theta2 ) + a1 * sin ( theta1 ) ];
382 g = yd - fq ;
383 % The linear velocity Jacobian Jv , where Jw is ignored
384 Jv = [ - a2 * sin ( theta1 + theta2 ) - a1 * sin ( theta1 ) , - a2 * sin ( theta1 + theta2 ) ;
385 a2 * cos ( theta1 + theta2 ) + a1 * cos ( theta1 ) , a2 * cos ( theta1 + theta2 ) ];
386 q = q + alpha * Jv ’* g ;
387 end
388 end
389 %%
390 function [ x1 , y1 , x2 , y2 ] = c o m p u t e F o r w a r d K i n e m a t i c s ( theta1 , theta2 )
391 % Forward Kinematics
392 a1 = 1; a2 = 1;
393 x1 = a1 * cos ( theta1 ) ;
394 y1 = a1 * sin ( theta1 ) ;
22
Enes SAGNAK UZB438E Homework3
23
Enes SAGNAK UZB438E Homework3
Figure 2.2
Figure 2.3
24
Enes SAGNAK UZB438E Homework3
Figure 2.4
Figure 2.5
Figure 2.6
25
Enes SAGNAK UZB438E Homework3
Figure 2.7
2.1. SUMMARY
The Rapidly-exploring Random Tree (RRT) algorithm is a probabilistic motion planning method commonly
used to navigate robotic systems through environments with obstacles. This algorithm aims to construct
a tree incrementally, starting from a random initial configuration and extending towards a desired goal
configuration in the workspace.
Algorithm Overview:
1. Random Sampling: At each iteration, a random point within the workspace is generated. This
point represents a potential new configuration for the robot.
2. Nearest Neighbor Search: The algorithm searches for the nearest existing node in the growing
tree to this random sample. The nearest node is used as a basis to guide the extension of the tree.
3. Tree Extension: The tree is extended by adding a new node that is a step towards the sampled
point. This step is controlled by a step size parameter, which determines how far the new node is
from the nearest existing node. If the extension leads to a collision-free path (checked via collision
detection mechanisms), the new node is successfully integrated into the tree.
4. Collision Checking: At each step, collision checks are performed to verify that the new path
segment from the nearest node to the sampled point does not intersect with any obstacles. This
ensures that the generated paths are feasible and safe for the robotic manipulator.
5. Iteration and Goal Reaching: The process continues iteratively, with new nodes being added
towards the random samples, growing the tree until it spans the workspace or reaches the target
goal position. If the target is reached before the tree fully spans the workspace, the path to the goal
is extracted from the tree structure.
26
3. REFERENCES
M. Spong, S. Hutchinson, and M. Vidyasagar, Robot Modeling and Control, 2nd ed. Wiley, 2020, isbn:
9781119523994.
Murad-Abu-Khalaf,Lecture Note 10
27