## 121  Two-Link Robotic Arm

ITERATIVE DYNAMIC PROGRAMMING, REIN LUUS

12.4.2 Example 2: Two-link robotic arm

CHAPMAN & HALL/CRC Monographs and Surveys in Pure and Applied Mathematics

### 121.1  Problem Formulation

Find u over t in [0; t_F ] to minimize

 J = tF

subject to:

 dx1 dt
=
sin(x3)*(
 9 4
*cos(x3)*x12+2*x22) +
 4 3
*(u1u2) −
 3 2
*cos(x3)*u2
 31 36
+
 9 4
*sin(x3)2

 dx2 dt
= −
sin(x3)*(
 7 2
*x12+
 9 4
*cos(x3)*x22) −
 7 3
*u2 +
 3 2
*cos(x3)*(u1u2
 31 36
+
 9 4
*sin(x3)2

 dx3 dt
= x2x1
 dx4 dt
= x1

The initial condition are:

 x(0)  = [0  0  0.5  0]
 x(tF) = [0  0  0.5  0.522]
 −1 <= u(1:2) <= 1

Reference: [25]

### 121.2  Problem setup

```toms t t_f
p = tomPhase('p', t, 0, t_f, 30);
setPhase(p);

tomStates x1 x2 x3 x4
tomControls u1 u2

% Initial guess
x0 = {t_f == 3
icollocate({x1 == 0; x2 == 0
x3 == 0.5; x4 == 0.522})
collocate({u1 == 1-2*t/t_f
u2 == 1-2*t/t_f})};

% Box constraints
cbox = {2.6 <= t_f <= 100
-1  <= collocate(u1) <= 1
-1  <= collocate(u2) <= 1};

% Boundary constraints
cbnd = {initial({x1 == 0; x2 == 0
x3 == 0.5; x4 == 0})
final({x1 == 0; x2 == 0
x3 == 0.5; x4 == 0.522})};

% ODEs and path constraints
ceq = collocate({
dot(x1) == ( sin(x3).*(9/4*cos(x3).*x1.^2+2*x2.^2) ...
+4/3*(u1-u2) - 3/2*cos(x3).*u2 )./ (31/36 + 9/4*sin(x3).^2)
dot(x2) == -( sin(x3).*(7/2*x1.^2 + 9/4*cos(x3).*x2.^2) ...
- 7/3*u2 + 3/2*cos(x3).*(u1-u2) )./ (31/36 + 9/4*sin(x3).^2)
dot(x3) == x2-x1
dot(x4) == x1});

% Objective
objective = t_f;
```

### 121.3  Solve the problem

```options = struct;
options.name = 'Two Link Robotic Arm';
solution = ezsolve(objective, {cbox, cbnd, ceq}, x0, options);
t  = subs(collocate(t),solution);
x1 = subs(collocate(x1),solution);
x2 = subs(collocate(x2),solution);
x3 = subs(collocate(x3),solution);
x4 = subs(collocate(x4),solution);
u1 = subs(collocate(u1),solution);
u2 = subs(collocate(u2),solution);
```
```Problem type appears to be: lpcon
Starting numeric solver
===== * * * =================================================================== * * *
TOMLAB - Tomlab Optimization Inc. Development license  999001. Valid to 2011-02-05
=====================================================================================
Problem: ---  1: Two Link Robotic Arm           f_k       2.983364855223869000
sum(|constr|)      0.000000154455635731
f(x_k) + sum(|constr|)      2.983365009679504800
f(x_0)      3.000000000000000000

Solver: snopt.  EXIT=0.  INFORM=1.
SNOPT 7.2-5 NLP code
Optimality conditions satisfied

FuncEv    1 ConstrEv   20 ConJacEv   20 Iter   16 MinorIter  278
CPU time: 0.203125 sec. Elapsed time: 0.219000 sec.
```

### 121.4  Plot result

```subplot(2,1,1)
plot(t,x1,'*-',t,x2,'*-',t,x3,'*-',t,x4,'*-');
legend('x1','x2','x3','x4');
title('Two Link Robotic Arm state variables');

subplot(2,1,2)
plot(t,u1,'+-',t,u2,'+-');
legend('u1','u2');
title('Two Link Robotic Arm control');
```