4
4
import numpy as onp
5
5
import sympy as sp
6
6
from pathlib import Path
7
- from typing import Callable , Dict , List , Tuple , Union
7
+ from typing import Callable , Dict , List , Tuple , Union , Optional
8
8
9
9
from .utils import (
10
10
concatenate_params_syms ,
17
17
def factory (
18
18
filepath : Union [str , Path ],
19
19
strain_selector : Array = None ,
20
- xi_eq : Array = None ,
20
+ xi_eq : Optional [Array ] = None ,
21
+ stiffness_fn : Optional [Callable ] = None ,
21
22
global_eps : float = 1e-6 ,
22
23
) -> Tuple [
23
24
Array ,
@@ -35,6 +36,8 @@ def factory(
35
36
strain_selector: array of shape (n_xi, ) with boolean values indicating which components of the
36
37
strain are active / non-zero
37
38
xi_eq: array of shape (3 * num_segments) with the rest strains of the rod
39
+ stiffness_fn: function to compute the stiffness matrix of the system. Should have the signature
40
+ stiffness_fn(params: Dict[str, Array], formulate_in_strain_space: bool) -> Array
38
41
global_eps: small number to avoid singularities (e.g., division by zero)
39
42
Returns:
40
43
B_xi: strain basis matrix of shape (3 * num_segments, n_q)
@@ -199,32 +202,33 @@ def classify_segment(params: Dict[str, Array], s: Array) -> Tuple[Array, Array]:
199
202
200
203
return segment_idx , s_segment
201
204
202
- def stiffness_fn (
203
- params : Dict [str , Array ], formulate_in_strain_space : bool = False
204
- ) -> Array :
205
- """
206
- Compute the stiffness matrix of the system.
207
- Args:
208
- params: Dictionary of robot parameters
209
-
210
- Returns:
211
- K: elastic matrix of shape (n_q, n_q) if formulate_in_strain_space is False or (n_xi, n_xi) otherwise
212
- """
213
- # cross-sectional area and second moment of area
214
- A = jnp .pi * params ["r" ] ** 2
215
- Ib = A ** 2 / (4 * jnp .pi )
216
-
217
- # elastic and shear modulus
218
- E , G = params ["E" ], params ["G" ]
219
- # stiffness matrix of shape (num_segments, 3, 3)
220
- S = compute_stiffness_matrix_for_all_segments_fn (A , Ib , E , G )
221
- # we define the elastic matrix of shape (n_xi, n_xi) as K(xi) = K @ xi where K is equal to
222
- K = blk_diag (S )
223
-
224
- if not formulate_in_strain_space :
225
- K = B_xi .T @ K @ B_xi
226
-
227
- return K
205
+ if stiffness_fn is None :
206
+ def stiffness_fn (
207
+ params : Dict [str , Array ], formulate_in_strain_space : bool = False
208
+ ) -> Array :
209
+ """
210
+ Compute the stiffness matrix of the system.
211
+ Args:
212
+ params: Dictionary of robot parameters
213
+ formulate_in_strain_space: whether to formulate the elastic matrix in the strain space
214
+ Returns:
215
+ K: elastic matrix of shape (n_q, n_q) if formulate_in_strain_space is False or (n_xi, n_xi) otherwise
216
+ """
217
+ # cross-sectional area and second moment of area
218
+ A = jnp .pi * params ["r" ] ** 2
219
+ Ib = A ** 2 / (4 * jnp .pi )
220
+
221
+ # elastic and shear modulus
222
+ E , G = params ["E" ], params ["G" ]
223
+ # stiffness matrix of shape (num_segments, 3, 3)
224
+ S = compute_stiffness_matrix_for_all_segments_fn (A , Ib , E , G )
225
+ # we define the elastic matrix of shape (n_xi, n_xi) as K(xi) = K @ xi where K is equal to
226
+ K = blk_diag (S )
227
+
228
+ if not formulate_in_strain_space :
229
+ K = B_xi .T @ K @ B_xi
230
+
231
+ return K
228
232
229
233
@jit
230
234
def forward_kinematics_fn (
0 commit comments