Skip to content

Commit 0af32ee

Browse files
committed
For loop version of chomp
1 parent 4b87dab commit 0af32ee

File tree

1 file changed

+42
-30
lines changed

1 file changed

+42
-30
lines changed

roboticstoolbox/examples/chomp.py

Lines changed: 42 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
visualize = True
1515
use_mesh = True
16+
parallel = False
1617

1718

1819
def chomp():
@@ -58,6 +59,7 @@ def chomp():
5859
lmbda = 1000
5960
eta = 1000
6061
iters = 4
62+
num_pts = 5
6163

6264

6365
# Make cost field, starting & end points
@@ -92,41 +94,51 @@ def chomp():
9294
for i in range(nq): # timestep
9395
robot.q = xi[cdim * i: cdim * (i+1)]
9496
# print(t, i, xi[cdim * i: cdim * (i+1)])
97+
qt = xi[cdim * i: cdim * (i+1)]
98+
if i == 0:
99+
qd = 0.5 * (xi[cdim * (i+1): cdim * (i+2)] - qs)
100+
elif i == nq - 1:
101+
qd = 0.5 * (qe - xi[cdim * (i-1): cdim * (i)])
102+
else:
103+
qd = 0.5 * (xi[cdim * (i+1): cdim * (i+2)] - xi[cdim * (i-1): cdim * (i)])
104+
95105
for link in robot.links: # bodypart
96106
k = link.jindex
97107
if k is None:
98108
continue
99-
100-
qt = xi[cdim * i: cdim * (i+1)]
101-
JJ = robot.jacob0(qt, end=link) # (k, 6)
102-
if i == 0:
103-
qd = 0.5 * (xi[cdim * (i+1): cdim * (i+2)] - qs)
104-
elif i == nq - 1:
105-
qd = 0.5 * (qe - xi[cdim * (i-1): cdim * (i)])
106-
else:
107-
qd = 0.5 * (xi[cdim * (i+1): cdim * (i+2)] - xi[cdim * (i-1): cdim * (i)])
108-
109-
qq = xi[cdim * i: cdim * i + k + 1]
110-
xx = robot.fkine(qt, end=link) # x_current, (4, 4)
111-
## TODO: get mesh data, vertices
112-
xd = JJ.dot(qd[:k+1]) # x_delta
113-
vel = np.linalg.norm(xd)
114-
115-
xdn = xd / (vel + 1e-3) # speed normalized
116-
xdd = JJ.dot(xidd[cdim * i: cdim * i + k + 1]) # second derivative of x
117-
prj = np.eye(6) - xdn[:, None].dot(xdn[:, None].T) # curvature vector (6, 6)
118-
kappa = (prj.dot(xdd) / (vel ** 2 + 1e-3)) # (6,)
119-
120-
xyz = xx.data[0][:3, -1]
121-
cost = np.sum(xyz)
122-
total_cost += cost
123-
# delta := negative gradient of obstacle cost in work space, (6, cdim)
124-
delta = -1 * np.concatenate([[1, 1, 0], np.zeros(3)])
125-
# for link in robot.links:
126-
# cost = get_link_cost(robot, meshes, link)
127-
128-
nabla_obs[cdim * i: cdim * i + k + 1] += JJ.T.dot(vel).dot(prj.dot(delta) - cost * kappa)
129109

110+
link_base = robot.fkine(robot.q, end=link) # x_current, (4, 4)
111+
delta_nabla_obs = np.zeros(k + 1)
112+
if not parallel:
113+
mesh = meshes[link.name]
114+
for j in range(num_pts):
115+
# For each point: compute Jacobian, compute cost, compute cost gradient
116+
117+
pt_rel = mesh.vertices[j]
118+
pt_tool = link_base @ SE3(pt_rel)
119+
pt_pos = pt_tool.t
120+
121+
# JJ = robot.jacob0(qt, end=link, tool=pt_tool) # (k, 6)
122+
JJ = robot.jacob0(qt, end=link, tool=SE3(pt_rel)) # (k, 6)
123+
## TODO: get mesh data, vertices
124+
xd = JJ.dot(qd[:k+1]) # x_delta
125+
vel = np.linalg.norm(xd)
126+
127+
xdn = xd / (vel + 1e-3) # speed normalized
128+
xdd = JJ.dot(xidd[cdim * i: cdim * i + k + 1]) # second derivative of xi
129+
prj = np.eye(6) - xdn[:, None].dot(xdn[:, None].T) # curvature vector (6, 6)
130+
kappa = (prj.dot(xdd) / (vel ** 2 + 1e-3)) # (6,)
131+
132+
cost = np.sum(pt_pos)
133+
total_cost += cost / num_pts
134+
# delta := negative gradient of obstacle cost in work space, (6, cdim)
135+
delta = -1 * np.concatenate([[1, 1, 0], np.zeros(3)])
136+
# for link in robot.links:
137+
# cost = get_link_cost(robot, meshes, link)
138+
delta_nabla_obs += JJ.T.dot(vel).dot(prj.dot(delta) - cost * kappa)
139+
140+
nabla_obs[cdim * i: cdim * i + k + 1] += (delta_nabla_obs / num_pts)
141+
130142
# dxi = Ainv.dot(lmbda * nabla_smooth)
131143
dxi = Ainv.dot(nabla_obs + lmbda * nabla_smooth)
132144
xi -= dxi / eta

0 commit comments

Comments
 (0)