Skip to content

Commit fb8e261

Browse files
committed
add holistic examples
1 parent 1c71492 commit fb8e261

File tree

2 files changed

+248
-0
lines changed

2 files changed

+248
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#!/usr/bin/env python
2+
"""
3+
@author Jesse Haviland
4+
"""
5+
6+
import swift
7+
import roboticstoolbox as rtb
8+
import spatialgeometry as sg
9+
import spatialmath as sm
10+
import qpsolvers as qp
11+
import numpy as np
12+
import math
13+
14+
15+
def step_robot(r, Tep):
16+
17+
wTe = r.fkine(r.q, fast=True)
18+
19+
eTep = np.linalg.inv(wTe) @ Tep
20+
21+
# Spatial error
22+
et = np.sum(np.abs(eTep[:3, -1]))
23+
24+
# Gain term (lambda) for control minimisation
25+
Y = 0.01
26+
27+
# Quadratic component of objective function
28+
Q = np.eye(r.n + 6)
29+
30+
# Joint velocity component of Q
31+
Q[: r.n, : r.n] *= Y
32+
Q[:2, :2] *= 1.0 / et
33+
34+
# Slack component of Q
35+
Q[r.n :, r.n :] = (1.0 / et) * np.eye(6)
36+
37+
v, _ = rtb.p_servo(wTe, Tep, 1.5)
38+
39+
v[3:] *= 1.3
40+
41+
# The equality contraints
42+
Aeq = np.c_[r.jacobe(r.q, fast=True), np.eye(6)]
43+
beq = v.reshape((6,))
44+
45+
# The inequality constraints for joint limit avoidance
46+
Ain = np.zeros((r.n + 6, r.n + 6))
47+
bin = np.zeros(r.n + 6)
48+
49+
# The minimum angle (in radians) in which the joint is allowed to approach
50+
# to its limit
51+
ps = 0.1
52+
53+
# The influence angle (in radians) in which the velocity damper
54+
# becomes active
55+
pi = 0.9
56+
57+
# Form the joint limit velocity damper
58+
Ain[: r.n, : r.n], bin[: r.n] = r.joint_velocity_damper(ps, pi, r.n)
59+
60+
# Linear component of objective function: the manipulability Jacobian
61+
c = np.concatenate(
62+
(np.zeros(2), -r.jacobm(start=r.links[4]).reshape((r.n - 2,)), np.zeros(6))
63+
)
64+
65+
# Get base to face end-effector
66+
= 0.5
67+
bTe = r.fkine(r.q, include_base=False, fast=True)
68+
θε = math.atan2(bTe[1, -1], bTe[0, -1])
69+
ε = * θε
70+
c[0] = -ε
71+
72+
# The lower and upper bounds on the joint velocity and slack variable
73+
lb = -np.r_[r.qdlim[: r.n], 10 * np.ones(6)]
74+
ub = np.r_[r.qdlim[: r.n], 10 * np.ones(6)]
75+
76+
# Solve for the joint velocities dq
77+
qd = qp.solve_qp(Q, c, Ain, bin, Aeq, beq, lb=lb, ub=ub)
78+
qd = qd[: r.n]
79+
80+
if et > 0.5:
81+
qd *= 0.7 / et
82+
else:
83+
qd *= 1.4
84+
85+
if et < 0.02:
86+
return True, qd
87+
else:
88+
return False, qd
89+
90+
91+
env = swift.Swift()
92+
env.launch(realtime=True)
93+
94+
ax_goal = sg.Axes(0.1)
95+
env.add(ax_goal)
96+
97+
frankie = rtb.models.Frankie()
98+
frankie.q = frankie.qr
99+
env.add(frankie)
100+
101+
arrived = False
102+
dt = 0.025
103+
104+
# Behind
105+
env.set_camera_pose([-2, 3, 0.7], [-2, 0.0, 0.5])
106+
wTep = frankie.fkine(frankie.q) * sm.SE3.Rz(np.pi)
107+
wTep.A[:3, :3] = np.diag([-1, 1, -1])
108+
wTep.A[0, -1] -= 4.0
109+
wTep.A[2, -1] -= 0.25
110+
ax_goal.base = wTep
111+
env.step()
112+
113+
114+
while not arrived:
115+
116+
arrived, frankie.qd = step_robot(frankie, wTep.A)
117+
env.step(dt)
118+
119+
# Reset bases
120+
base_new = frankie.fkine(frankie._q, end=frankie.links[2], fast=True)
121+
frankie._base.A[:] = base_new
122+
frankie.q[:2] = 0
123+
124+
env.hold()
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
#!/usr/bin/env python
2+
"""
3+
@author Jesse Haviland
4+
"""
5+
6+
import swift
7+
import roboticstoolbox as rtb
8+
import spatialgeometry as sg
9+
import spatialmath as sm
10+
import qpsolvers as qp
11+
import numpy as np
12+
import math
13+
14+
15+
def step_robot(r, Tep):
16+
17+
wTe = r.fkine(r.q, fast=True)
18+
19+
eTep = np.linalg.inv(wTe) @ Tep
20+
21+
# Spatial error
22+
et = np.sum(np.abs(eTep[:3, -1]))
23+
24+
# Gain term (lambda) for control minimisation
25+
Y = 0.01
26+
27+
# Quadratic component of objective function
28+
Q = np.eye(r.n + 6)
29+
30+
# Joint velocity component of Q
31+
Q[: r.n, : r.n] *= Y
32+
Q[:3, :3] *= 1.0 / et
33+
34+
# Slack component of Q
35+
Q[r.n :, r.n :] = (1.0 / et) * np.eye(6)
36+
37+
v, _ = rtb.p_servo(wTe, Tep, 1.5)
38+
39+
v[3:] *= 1.3
40+
41+
# The equality contraints
42+
Aeq = np.c_[r.jacobe(r.q, fast=True), np.eye(6)]
43+
beq = v.reshape((6,))
44+
45+
# The inequality constraints for joint limit avoidance
46+
Ain = np.zeros((r.n + 6, r.n + 6))
47+
bin = np.zeros(r.n + 6)
48+
49+
# The minimum angle (in radians) in which the joint is allowed to approach
50+
# to its limit
51+
ps = 0.1
52+
53+
# The influence angle (in radians) in which the velocity damper
54+
# becomes active
55+
pi = 0.9
56+
57+
# Form the joint limit velocity damper
58+
Ain[: r.n, : r.n], bin[: r.n] = r.joint_velocity_damper(ps, pi, r.n)
59+
60+
# Linear component of objective function: the manipulability Jacobian
61+
c = np.concatenate(
62+
(np.zeros(3), -r.jacobm(start=r.links[5]).reshape((r.n - 3,)), np.zeros(6))
63+
)
64+
65+
# Get base to face end-effector
66+
= 0.5
67+
bTe = r.fkine(r.q, include_base=False, fast=True)
68+
θε = math.atan2(bTe[1, -1], bTe[0, -1])
69+
ε = * θε
70+
c[0] = -ε
71+
72+
# The lower and upper bounds on the joint velocity and slack variable
73+
lb = -np.r_[r.qdlim[: r.n], 10 * np.ones(6)]
74+
ub = np.r_[r.qdlim[: r.n], 10 * np.ones(6)]
75+
76+
# Solve for the joint velocities dq
77+
qd = qp.solve_qp(Q, c, Ain, bin, Aeq, beq, lb=lb, ub=ub)
78+
qd = qd[: r.n]
79+
80+
if et > 0.5:
81+
qd *= 0.7 / et
82+
else:
83+
qd *= 1.4
84+
85+
if et < 0.02:
86+
return True, qd
87+
else:
88+
return False, qd
89+
90+
91+
env = swift.Swift()
92+
env.launch(realtime=True)
93+
94+
ax_goal = sg.Axes(0.1)
95+
env.add(ax_goal)
96+
97+
frankie = rtb.models.FrankieOmni()
98+
frankie.q = frankie.qr
99+
env.add(frankie)
100+
101+
arrived = False
102+
dt = 0.025
103+
104+
# Behind
105+
env.set_camera_pose([-2, 3, 0.7], [-2, 0.0, 0.5])
106+
wTep = frankie.fkine(frankie.q) * sm.SE3.Rz(np.pi)
107+
wTep.A[:3, :3] = np.diag([-1, 1, -1])
108+
wTep.A[0, -1] -= 4.0
109+
wTep.A[2, -1] -= 0.25
110+
ax_goal.base = wTep
111+
env.step()
112+
113+
114+
while not arrived:
115+
116+
arrived, frankie.qd = step_robot(frankie, wTep.A)
117+
env.step(dt)
118+
119+
# Reset bases
120+
base_new = frankie.fkine(frankie._q, end=frankie.links[3], fast=True)
121+
frankie._base.A[:] = base_new
122+
frankie.q[:3] = 0
123+
124+
env.hold()

0 commit comments

Comments
 (0)