Skip to content

Commit 375d5f7

Browse files
committed
Allow for custom stiffness functions to be used in planar pcs system
1 parent 9f5b7c8 commit 375d5f7

File tree

1 file changed

+32
-28
lines changed

1 file changed

+32
-28
lines changed

src/jsrm/systems/planar_pcs.py

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import numpy as onp
55
import sympy as sp
66
from pathlib import Path
7-
from typing import Callable, Dict, List, Tuple, Union
7+
from typing import Callable, Dict, List, Tuple, Union, Optional
88

99
from .utils import (
1010
concatenate_params_syms,
@@ -17,7 +17,8 @@
1717
def factory(
1818
filepath: Union[str, Path],
1919
strain_selector: Array = None,
20-
xi_eq: Array = None,
20+
xi_eq: Optional[Array] = None,
21+
stiffness_fn: Optional[Callable] = None,
2122
global_eps: float = 1e-6,
2223
) -> Tuple[
2324
Array,
@@ -35,6 +36,8 @@ def factory(
3536
strain_selector: array of shape (n_xi, ) with boolean values indicating which components of the
3637
strain are active / non-zero
3738
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
3841
global_eps: small number to avoid singularities (e.g., division by zero)
3942
Returns:
4043
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]:
199202

200203
return segment_idx, s_segment
201204

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
228232

229233
@jit
230234
def forward_kinematics_fn(

0 commit comments

Comments
 (0)