Skip to content

Commit 6552bf3

Browse files
committed
Start implementing numerical Jacobian method
1 parent 14bd079 commit 6552bf3

File tree

4 files changed

+460
-19
lines changed

4 files changed

+460
-19
lines changed

examples/demo_planar_hsa_motor2ee_jacobian.py

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import jsrm
1414
from jsrm.parameters.hsa_params import PARAMS_FPU_CONTROL
1515
from jsrm.systems import planar_hsa
16+
from jsrm.utils.numerical_jacobian import approx_derivative
1617

1718
num_segments = 1
1819
num_rods_per_segment = 2
@@ -25,15 +26,14 @@
2526
)
2627

2728

28-
def factory_fn(params: Dict[str, Array], nlq_tol: float = 1e-5):
29+
def factory_fn(params: Dict[str, Array]) -> Tuple[Callable, Callable]:
2930
"""
3031
Factory function for the planar HSA.
3132
Args:
3233
params: dictionary with robot parameters
33-
nlq_tol: tolerance for the nonlinear least squares solver
34-
3534
Returns:
36-
35+
phi2chi_static_model_fn: function that maps motor angles to the end-effector pose
36+
jac_phi2chi_static_model_fn: function that computes the Jacobian between the actuation space and the task-space
3737
"""
3838
(
3939
forward_kinematics_virtual_backbone_fn,
@@ -107,16 +107,20 @@ def phi2chi_static_model_fn(phi: Array, q0: Array = jnp.zeros((3, ))) -> Tuple[A
107107
aux["chi"] = chi
108108
return chi, aux
109109

110-
def jac_phi2chi_static_model_fn(phi: Array) -> Tuple[Array, Dict[str, Array]]:
110+
def jac_phi2chi_static_model_fn(phi: Array, q0: Array = jnp.zeros((3, ))) -> Tuple[Array, Dict[str, Array]]:
111111
"""
112112
Compute the Jacobian between the actuation space and the task-space.
113113
Arguments:
114114
phi: motor angles
115115
"""
116116
# evaluate the static model to convert motor angles into a configuration
117-
q = phi2q_static_model_jaxopt_fn(phi)
118-
# take the Jacobian between actuation and configuration space
119-
J_phi2q, aux = jacfwd(phi2q_static_model_jaxopt_fn, has_aux=True)(phi)
117+
q, aux = phi2q_static_model_fn(phi, q0=q0)
118+
# approximate the Jacobian between the actuation and the task-space using finite differences
119+
J_phi2q = approx_derivative(
120+
fun=lambda _phi: phi2q_static_model_fn(phi, q0=q0)[0],
121+
x0=phi,
122+
f0=q,
123+
)
120124

121125
# evaluate the closed-form, analytical jacobian of the forward kinematics
122126
J_q2chi = jacobian_end_effector_fn(params, q)
@@ -126,7 +130,7 @@ def jac_phi2chi_static_model_fn(phi: Array) -> Tuple[Array, Dict[str, Array]]:
126130

127131
return J_phi2chi, aux
128132

129-
return phi2chi_static_model_fn
133+
return phi2chi_static_model_fn, jac_phi2chi_static_model_fn
130134

131135

132136
if __name__ == "__main__":
@@ -135,13 +139,8 @@ def jac_phi2chi_static_model_fn(phi: Array) -> Tuple[Array, Dict[str, Array]]:
135139
params = PARAMS_FPU_CONTROL
136140
phi_max = params["phi_max"].flatten()
137141

138-
# increase damping for simulation stability
139-
params["zetab"] = 5 * params["zetab"]
140-
params["zetash"] = 5 * params["zetash"]
141-
params["zetaa"] = 5 * params["zetaa"]
142-
143142
# call the factory function
144-
phi2chi_static_model_fn = factory_fn(params)
143+
phi2chi_static_model_fn, jac_phi2chi_static_model_fn = factory_fn(params)
145144

146145
# define initial configuration
147146
q0 = jnp.array([0.0, 0.0, 0.0])
@@ -162,11 +161,10 @@ def jac_phi2chi_static_model_fn(phi: Array) -> Tuple[Array, Dict[str, Array]]:
162161
maxval=phi_max
163162
)
164163

165-
print("i", i, "phi", phi)
164+
print("i", i)
166165

167166
chi, aux = phi2chi_static_model_fn(phi, q0=q0)
168167
print("phi", phi, "q", aux["q"], "chi", chi)
169168

170-
# J_phi2chi_autodiff, aux = J_phi2chi_autodiff_fn(phi)
171-
# J_phi2chi, aux = J_phi2chi_fn(phi)
172-
# print("J_phi2chi:\n", J_phi2chi, "\nJ_phi2chi_autodiff:\n", J_phi2chi_autodiff)
169+
J_phi2chi, aux = jac_phi2chi_static_model_fn(phi, q0=q0)
170+
print("J_phi2chi:\n", J_phi2chi)

src/jsrm/utils/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)