|
13 | 13 |
|
14 | 14 | visualize = True
|
15 | 15 | use_mesh = True
|
| 16 | +parallel = False |
16 | 17 |
|
17 | 18 |
|
18 | 19 | def chomp():
|
@@ -58,6 +59,7 @@ def chomp():
|
58 | 59 | lmbda = 1000
|
59 | 60 | eta = 1000
|
60 | 61 | iters = 4
|
| 62 | + num_pts = 5 |
61 | 63 |
|
62 | 64 |
|
63 | 65 | # Make cost field, starting & end points
|
@@ -92,41 +94,51 @@ def chomp():
|
92 | 94 | for i in range(nq): # timestep
|
93 | 95 | robot.q = xi[cdim * i: cdim * (i+1)]
|
94 | 96 | # 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 | + |
95 | 105 | for link in robot.links: # bodypart
|
96 | 106 | k = link.jindex
|
97 | 107 | if k is None:
|
98 | 108 | 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) |
129 | 109 |
|
| 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 | + |
130 | 142 | # dxi = Ainv.dot(lmbda * nabla_smooth)
|
131 | 143 | dxi = Ainv.dot(nabla_obs + lmbda * nabla_smooth)
|
132 | 144 | xi -= dxi / eta
|
|
0 commit comments