13
13
import jsrm
14
14
from jsrm .parameters .hsa_params import PARAMS_FPU_CONTROL
15
15
from jsrm .systems import planar_hsa
16
+ from jsrm .utils .numerical_jacobian import approx_derivative
16
17
17
18
num_segments = 1
18
19
num_rods_per_segment = 2
25
26
)
26
27
27
28
28
- def factory_fn (params : Dict [str , Array ], nlq_tol : float = 1e-5 ) :
29
+ def factory_fn (params : Dict [str , Array ]) -> Tuple [ Callable , Callable ] :
29
30
"""
30
31
Factory function for the planar HSA.
31
32
Args:
32
33
params: dictionary with robot parameters
33
- nlq_tol: tolerance for the nonlinear least squares solver
34
-
35
34
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
37
37
"""
38
38
(
39
39
forward_kinematics_virtual_backbone_fn ,
@@ -107,16 +107,20 @@ def phi2chi_static_model_fn(phi: Array, q0: Array = jnp.zeros((3, ))) -> Tuple[A
107
107
aux ["chi" ] = chi
108
108
return chi , aux
109
109
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 ]]:
111
111
"""
112
112
Compute the Jacobian between the actuation space and the task-space.
113
113
Arguments:
114
114
phi: motor angles
115
115
"""
116
116
# 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
+ )
120
124
121
125
# evaluate the closed-form, analytical jacobian of the forward kinematics
122
126
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]]:
126
130
127
131
return J_phi2chi , aux
128
132
129
- return phi2chi_static_model_fn
133
+ return phi2chi_static_model_fn , jac_phi2chi_static_model_fn
130
134
131
135
132
136
if __name__ == "__main__" :
@@ -135,13 +139,8 @@ def jac_phi2chi_static_model_fn(phi: Array) -> Tuple[Array, Dict[str, Array]]:
135
139
params = PARAMS_FPU_CONTROL
136
140
phi_max = params ["phi_max" ].flatten ()
137
141
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
-
143
142
# 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 )
145
144
146
145
# define initial configuration
147
146
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]]:
162
161
maxval = phi_max
163
162
)
164
163
165
- print ("i" , i , "phi" , phi )
164
+ print ("i" , i )
166
165
167
166
chi , aux = phi2chi_static_model_fn (phi , q0 = q0 )
168
167
print ("phi" , phi , "q" , aux ["q" ], "chi" , chi )
169
168
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 )
0 commit comments