Skip to content

Commit 22ce55f

Browse files
committed
Fix bugs in calling approx_derivative
1 parent 0c23d08 commit 22ce55f

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
lines changed

examples/demo_planar_hsa_motor2ee_jacobian.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@
2626
)
2727

2828

29-
def factory_fn(params: Dict[str, Array]) -> Tuple[Callable, Callable]:
29+
def factory_fn(params: Dict[str, Array], verbose: bool = False) -> Tuple[Callable, Callable]:
3030
"""
3131
Factory function for the planar HSA.
3232
Args:
3333
params: dictionary with robot parameters
34+
verbose: flag to print additional information
3435
Returns:
3536
phi2chi_static_model_fn: function that maps motor angles to the end-effector pose
3637
jac_phi2chi_static_model_fn: function that computes the Jacobian between the actuation space and the task-space
@@ -45,6 +46,7 @@ def factory_fn(params: Dict[str, Array]) -> Tuple[Callable, Callable]:
4546
) = planar_hsa.factory(sym_exp_filepath, strain_selector)
4647
dynamical_matrices_fn = partial(dynamical_matrices_fn, params)
4748
forward_kinematics_end_effector_fn = jit(partial(forward_kinematics_end_effector_fn, params))
49+
jacobian_end_effector_fn = jit(partial(jacobian_end_effector_fn, params))
4850

4951
def residual_fn(q: Array, phi: Array) -> Array:
5052
q_d = jnp.zeros_like(q)
@@ -77,9 +79,10 @@ def phi2q_static_model_fn(phi: Array, q0: Array = jnp.zeros((3, ))) -> Tuple[Arr
7779
fun=lambda q: residual_fn(q, phi).item(),
7880
x0=q0,
7981
jac=lambda q: jac_residual_fn(q, phi),
80-
# options={"disp": True},
82+
options={"disp": True} if verbose else None,
8183
)
82-
print("Optimization converged after", sol.nit, "iterations with residual", sol.fun)
84+
if verbose:
85+
print("Optimization converged after", sol.nit, "iterations with residual", sol.fun)
8386

8487
# configuration that minimizes the residual
8588
q = jnp.array(sol.x)
@@ -117,13 +120,13 @@ def jac_phi2chi_static_model_fn(phi: Array, q0: Array = jnp.zeros((3, ))) -> Tup
117120
q, aux = phi2q_static_model_fn(phi, q0=q0)
118121
# approximate the Jacobian between the actuation and the task-space using finite differences
119122
J_phi2q = approx_derivative(
120-
fun=lambda _phi: phi2q_static_model_fn(phi, q0=q0)[0],
123+
fun=lambda _phi: phi2q_static_model_fn(_phi, q0=q0)[0],
121124
x0=phi,
122125
f0=q,
123126
)
124127

125128
# evaluate the closed-form, analytical jacobian of the forward kinematics
126-
J_q2chi = jacobian_end_effector_fn(params, q)
129+
J_q2chi = jacobian_end_effector_fn(q)
127130

128131
# evaluate the Jacobian between the actuation and the task-space
129132
J_phi2chi = J_q2chi @ J_phi2q

0 commit comments

Comments
 (0)