P Set10
P Set10
UZB 438E
PROBLEM SET 10
By
Enes Sagnak 110200063
Murad Abu-Khalaf
1 Question 1 2
1.0.1 Introduction ..................................................................................................... 2
1.0.2 Modifications ................................................................................................... 2
1.0.3 Outputs........................................................................................................... 4
1.0.4 Discussion ...................................................................................................... 11
2 REFERENCES 15
1
1. QUESTION 1
1.0.1. Introduction
This report presents the development and enhancement of a trajectory optimization framework for a two-
link planar elbow arm, incorporating obstacle-awareness capabilities. The main task involves modifying
the existing trajectory planning constraints to include a specific condition: the arm must not leverage an
equilibrium point where the second linkâĂŹs mass is at the coordinates (0, 2), between the 4th and 13th
seconds of the trajectory. This additional constraint, representing a circular obstacle with a radius of 0.1
centered at (0, 2), has been incorporated into the function planning nonlcon within the provided code. The
updated code and simulation results demonstrate the arms ability to adapt to the new constraint while
achieving the desired trajectory.
1.0.2. Modifications
The following changes have been made to the code based on the Lecture 10 implementation for the
obstacle-aware Two-Link Planar Elbow Arm:
Added New Constraint (c3): In the planningnonlcon function, I introduced an additional constraint c3
to prevent the arm from leveraging the equilibrium point at the coordinates (0, 2). This is achieved by
defining a circular constraint of radius 0.1 around the point (0, 2), ensuring that the arm avoids this region.
Enhanced Animation (draw function): To improve the visual representation of the arm’s movement, a
draw function was added. This function enables the visualization of the robot’s trajectory, obstacle
2
Enes SAGNAK UZB438E Homework3
avoidance, and the interaction between the links of the arm during the optimization process.
Increased the Grid Resolution (N): The number of grid points, N, was increased to improve the
accuracy of the trajectory optimization. This higher resolution allows for more precise control over the
motion planning, especially around critical areas such as the equilibrium point.
Constraining the Maximum Speed Values: The bounds on the joint velocities and torques were
adjusted to limit the maximum values that the arm’s joints can reach. Specifically, the joint velocities were
constrained to mx spd 1 and mx spd 2 for the first and second joints, respectively. The upper and lower
bounds on the state were also set to ensure that the arm operates within a reasonable range of motion.
This prevents excessive movement and improves stability.
9 % Enhanced Animation
10 function draw (t , X )
11 % This function visualizes the arms motion in a more intuitive way .
12 [ x1 , y1 , x2 , y2 ] = g e t F o r w a r d K i n e ma t i c s ( X (1 ,:) , X (2 ,:) ) ;
13 % Plot the arm and obstacles
14 plot ([0 x1 ] , [0 y1 ] , ’r ’ , ’ LineWidth ’ , 2) ; % First link
15 plot ([ x1 x2 ] , [ y1 y2 ] , ’b ’ , ’ LineWidth ’ , 2) ; % Second link
16 % Add obstacle visualizations
17 fill ( Ox , Oy , ’k ’) ; % Obstacle
18 plot ( x1 , y1 , ’ ro ’) ; % Joint 1
19 plot ( x2 , y2 , ’ bo ’) ; % Joint 2
20 axis equal ;
21 drawnow ;
22 end
23
3
Enes SAGNAK UZB438E Homework3
26
1.0.3. Outputs
The following code was run with new obstacle sizes of different radii and the result was monitored.
4
Enes SAGNAK UZB438E Homework3
31 %%
32 bounds . x . lower = [ -2* pi ; -2* pi ; - mx_spd_1 ; - mx_spd_1 ];
33 bounds . x . upper = [2* pi ;2* pi ; mx_spd_2 ; mx_spd_2 ];
34 bounds . u . lower = - tau_max ;
35 bounds . u . upper = tau_max ;
36 %% Initial guess at trajectory %
37 t_ = [0 , tf ];
38 x_ = [ bounds . x0 . lower , bounds . xf . lower ];
39 u_ = [0 0;0 0];
40 guess . T = t_ ([1 , end ]) ;
41 guess . t = linspace ( guess . T (1) , guess . T (2) , N ) ;
42 guess . x = interp1 ( t_ ’ , x_ ’ , guess . t ’) ’;
43 guess . u = interp1 ( t_ ’ , u_ ’ , guess . t ’) ’;
44 zGuess = [ reshape ([ guess . x ; guess . u ] , numel ( guess . x ) + numel ( guess . u ) ,
1) ];
45 %% Create Decision Variables vector
46 x_Lower = [ bounds . x0 . lower , bounds . x . lower * ones (1 ,N -2) ,
bounds . xf . lower ];
47 u_Lower = bounds . u . lower * ones (1 , N ) ;
48 z_Lower = [ reshape ([ x_Lower ; u_Lower ] , numel ( x_Lower ) + numel ( u_Lower ) ,
1) ];
49 % Note that for time , the upper and lower limits are the same ,
essentially fixed grid points .
50 x_Upper = [ bounds . x0 . upper , bounds . x . upper * ones (1 ,N -2) ,
bounds . xf . upper ];
51 u_Upper = bounds . u . upper * ones (1 , N ) ;
52 z_Upper = [ reshape ([ x_Upper ; u_Upper ] , numel ( x_Upper ) + numel ( u_Upper ) ,
1) ];
53 %% fmincon problem structure formulation
54 problem . objective = @ ( z ) ( planning_Jcost (z , func . Lcost ,
func . terminalCost , tf , N ) ) ;
55 problem . x0 = zGuess ;
56 problem . Aineq = [];
57 problem . bineq = [];
58 problem . Aeq = [];
59 problem . beq = [];
60 problem . lb = z_Lower ;
5
Enes SAGNAK UZB438E Homework3
61 problem . ub = z_Upper ;
62 problem . nonlcon = @ ( z ) ( planning_nonlcon (z , func . dynamics , tf , N ) ) ;
63 problem . solver = ’ fmincon ’;
64 problem . options = optimoptions ( " fmincon " ,...
65 " Algorithm " ," interior - point " ,...
66 " E n a b l e F e a s i b i l i t y M o d e " , true ,...
67 " S u bp r ob l e mA l go r i th m " ," cg " ,...
68 ’ Display ’ , ’ iter ’ ,...
69 ’ TolFun ’ ,1e -6 ,...
70 ’ MaxIter ’ ,400 ,...
71 ’ M a x F u n c t i o n E v a l u a t i o n s ’ ,5 e5 ) ;
72 [ zStar , objVal , exitFlag , output ] = fmincon ( problem ) ;
73 %% Process the output of fmincon
74 tStar = linspace (0 , tf , N ) ;
75 xu = reshape ( zStar (1:(6* N ) ) , [6 , N ]) ;
76 xStar = xu (1:4 ,:) ;
77 uStar = xu (5:6 ,:) ;
78 % Use piecewise linear interpolation for the control
79 u = @ ( t ) ( interp1 ( tSta r ’ , uStar ’ ,t ’) ’ ) ;
80 % Use piecewise quadratic interpolation for the state :
81 f = func . dynamics ( tStar , xStar , uStar ) ;
82 x = @ ( t ) ( q u a d r a t i c I n t e r p o l a t i o n (t , xStar ,f , tf , N ) ) ;
83 figure (1) ; hold on ;
84 X = x (0:0.1: tf ) ;
85 plot (0:0.1: tf , X (1:2 ,:) , ’ LineWidth ’ ,2) ; xlabel ( ’ Time
( s ) ’ , ’ FontSize ’ ,20) ; ylabel ( ’ rad ’ , ’ FontSize ’ ,20) ;
86 legend ({ ’$ \ theta_1 ( t ) $ ’ , ’$ \ theta_2 ( t ) $ ’} , ’ FontSize ’ ,20 , ’ Interpreter ’ , ’ latex ’)
87 title ( ’ Planned Trajectory for Arm Position Angles $ \ theta_1 ( t ) $ \&
$ \ theta_2 ( t ) $ ’ , ’ FontSize ’ ,20 , ’ Interpreter ’ , ’ latex ’) ;
88 subtitle ( ’ Trajectory Optimization , assumes $ \ tau_d =
0 $ ’ , ’ FontSize ’ ,15 , ’ Interpreter ’ , ’ latex ’) ;
89 figure (2) ; hold on ;
90 plot (0:0.1: tf , u (0:0.1: tf ) , ’ LineWidth ’ ,2) ; xlabel ( ’ Time
( s ) ’ , ’ FontSize ’ ,20) ; ylabel ( ’N . m ’ , ’ FontSize ’ ,20)
91 legend ({ ’$ \ tau_1 ( t ) $ ’ , ’$ \ tau_2 ( t ) $ ’} , ’ FontSize ’ ,20 , ’ Interpreter ’ , ’ latex ’)
92 title ( ’ Planned Torque $ \ tau ( t ) $ ’ , ’ FontSize ’ ,20 , ’ Interpreter ’ , ’ latex ’) ;
93 subtitle ( ’ Trajectory Optimization , assumes $ \ tau_d =
0 $ ’ , ’ FontSize ’ ,15 , ’ Interpreter ’ , ’ latex ’) ;
94 figure (3) ; hold on ;
6
Enes SAGNAK UZB438E Homework3
7
Enes SAGNAK UZB438E Homework3
8
Enes SAGNAK UZB438E Homework3
180 function xt = q u a d r a t i c I n t e r p o l a t i o n (t ,X ,F , tf , N )
181 % Generate trajectoy via Quadratic Polynomial Interpolation
182 % p ( t ) = a t ^2 + b t + c
183 % p ( t [ k ]) = c = x [ k ]
184 % pdot ( t [ k ]) = b = f ( xk )
185 % pdot ( t [ k +1]) = 2 a T + b = f ( x [ k +1])
186 Ts = ( tf -0) /( N -1) ;
187 if max ( t ) > tf
188 error ( ’t is larger than allowed . ’) ;
189 end
190 k = floor ( t / Ts ) ;
191 tk = Ts * k ;
192 xk = X (: , min (k ,N -1) +1) ; % for k = 0 , pick 1 and 2 waypoints .
193 fk = F (: , min (k ,N -1) +1) ;
194 fk1 = F (: , min (k ,N -2) +2) ;
195 c = xk ;
196 b = fk ;
197 a = ( fk1 - fk ) /(2* Ts ) ;
198 xt = a .*( t - tk ) .^2 + b .*( t - tk ) + c ;
199 end
200 %%
201 function xdot = robotDynamics (t ,x , u ) % # ok < INUSD >
202 % Robot dynamics
203 nU = size (u ,2) ;
204 xdot = zeros ( size ( x ) ) ;
205 for i =1: nU
206 % Write state x in terms of thetas for convenience
207 theta1 = x (1 , i ) ;
208 theta2 = x (2 , i ) ;
209 theta1dot = x (3 , i ) ;
9
Enes SAGNAK UZB438E Homework3
210 theta2dot = x (4 , i ) ;
211 % Parameters of the Arm
212 m1 = 1; m2 = 1; % mass
213 a1 = 1; a2 = 1; % arm length
214 g = 9.81; % gravity constant
215 % Inertia matrix
216 M11 = a1 ^2* m1 + a1 ^2* m2 + a2 ^2* m2 + 2* a1 * a2 * m2 * cos ( theta2 ) ;
217 M12 = m2 * a2 ^2 + a1 * m2 * cos ( theta2 ) * a2 ;
218 M21 = M12 ;
219 M22 = a2 ^2* m2 ;
220 M = [ M11 M12 ;
221 M21 M22 ];
222 % Coriolis / Centrifugal
223 V = [ - a1 * a2 * m2 * sin ( theta2 ) *(2* theta1dot + theta2dot ) * theta2dot ;
224 a1 * a2 * m2 * sin ( theta2 ) * theta1dot ^2];
225 % Gravity
226 G = [ g *( a1 * m1 * cos ( theta1 ) + a1 * m2 * cos ( theta1 ) + a2 * m2 * cos ( theta1 +
theta2 ) ) ;
227 a2 * g * m2 * cos ( theta1 + theta2 ) ];
228 % Disturbance Unknown to the Controller
229 tau_d = [0;0];
230 % Control input
231 tau = u (: , i ) ;
232 % Position / Velocity State Representation
233 xdot (: , i ) = [ theta1dot ;
234 theta2dot ;
235 inv ( M ) *( -V -G - tau_d + tau ) ]; % # ok < MINV >
236 end
237 end
238 %%
239 function [ x1 , y1 , x2 , y2 ] = g e t F o r w a r d K i n e m a ti c s ( theta1 , theta2 )
240 % Forward Kinematics
241 a1 = 1; a2 = 1;
242 x1 = a1 * cos ( theta1 ) ;
243 y1 = a1 * sin ( theta1 ) ;
244 x2 = x1 + a2 * cos ( theta1 + theta2 ) ;
245 y2 = y1 + a2 * sin ( theta1 + theta2 ) ;
246 end
10
Enes SAGNAK UZB438E Homework3
1.0.4. Discussion
In the modified Two-Link Planar Elbow Arm simulation, the goal was to prevent the arm from utilizing the
equilibrium point at coordinates (0, 2) by adding a circular constraint around it. As the radius of the
11
Enes SAGNAK UZB438E Homework3
constraint increased, the arm’s feasible movement area was further restricted, reducing the number of
feasible trajectories.
12
Enes SAGNAK UZB438E Homework3
However, as the constraint radius grew, the arm’s ability to avoid the equilibrium point decreased. The
arm was increasingly confined to a smaller region, which led to higher oscillations as it struggled to
navigate around the constraint. This resulted in the arm passing through or staying near the equilibrium
13
Enes SAGNAK UZB438E Homework3
This highlights the trade-off between restricting the arm’s movement near critical points to prevent
staying at equilibrium and maintaining a stable trajectory. While the constraint minimized the risk of
staying at equilibrium, its increasing size ultimately limited the arm’s movement options, leading to higher
oscillations and instability.
14
2. REFERENCES
M. Spong, S. Hutchinson, and M. Vidyasagar, Robot Modeling and Control, 2nd ed. Wiley, 2020, isbn:
9781119523994.
Murad-Abu-Khalaf,Lecture Note 10
15