Skip to content

Commit 1fb147d

Browse files
committed
Add stiffness function to planar pcs system
1 parent 303c370 commit 1fb147d

File tree

2 files changed

+34
-21
lines changed

2 files changed

+34
-21
lines changed

examples/simulate_planar_pcs.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ def draw_robot(
151151
plt.grid(True)
152152
plt.xlabel("End-effector x [m]")
153153
plt.ylabel("End-effector y [m]")
154+
plt.tight_layout()
154155
plt.show()
155156
# plot end-effector position vs time
156157
plt.figure()
@@ -161,6 +162,7 @@ def draw_robot(
161162
plt.legend()
162163
plt.grid(True)
163164
plt.box(True)
165+
plt.tight_layout()
164166
plt.show()
165167

166168
# plot the energy along the trajectory
@@ -176,6 +178,7 @@ def draw_robot(
176178
plt.legend()
177179
plt.grid(True)
178180
plt.box(True)
181+
plt.tight_layout()
179182
plt.show()
180183

181184
# create video

src/jsrm/systems/planar_pcs.py

Lines changed: 31 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,31 @@ def classify_segment(params: Dict[str, Array], s: Array) -> Tuple[Array, Array]
199199

200200
return segment_idx, s_segment
201201

202+
def stiffness_fn(params: Dict[str, Array], formulate_in_strain_space: bool = False) -> Array:
203+
"""
204+
Compute the stiffness matrix of the system.
205+
Args:
206+
params: Dictionary of robot parameters
207+
208+
Returns:
209+
K: elastic matrix of shape (n_q, n_q) if formulate_in_strain_space is False or (n_xi, n_xi) otherwise
210+
"""
211+
# cross-sectional area and second moment of area
212+
A = jnp.pi * params["r"] ** 2
213+
Ib = A ** 2 / (4 * jnp.pi)
214+
215+
# elastic and shear modulus
216+
E, G = params["E"], params["G"]
217+
# stiffness matrix of shape (num_segments, 3, 3)
218+
S = compute_stiffness_matrix_for_all_segments_fn(A, Ib, E, G)
219+
# we define the elastic matrix of shape (n_xi, n_xi) as K(xi) = K @ xi where K is equal to
220+
K = blk_diag(S)
221+
222+
if not formulate_in_strain_space:
223+
K = B_xi.T @ K @ B_xi
224+
225+
return K
226+
202227
@jit
203228
def forward_kinematics_fn(
204229
params: Dict[str, Array], q: Array, s: Array, eps: float = global_eps
@@ -295,17 +320,8 @@ def dynamical_matrices_fn(
295320
# add a small number to the bending strain to avoid singularities
296321
xi_epsed = apply_eps_to_bend_strains(xi, eps)
297322

298-
# cross-sectional area and second moment of area
299-
A = jnp.pi * params["r"] ** 2
300-
Ib = A**2 / (4 * jnp.pi)
301-
302-
# elastic and shear modulus
303-
elastic_modulus, shear_modulus = params["E"], params["G"]
304-
# stiffness matrix of shape (num_segments, 3, 3)
305-
S = compute_stiffness_matrix_for_all_segments_fn(A, Ib, elastic_modulus, shear_modulus)
306-
307-
# we define the elastic matrix of shape (n_xi, n_xi) as K(xi) = K @ xi where K is equal to
308-
K = blk_diag(S)
323+
# compute the stiffness matrix
324+
K = stiffness_fn(params, formulate_in_strain_space=True)
309325

310326
# dissipative matrix from the parameters
311327
D = params.get("D", jnp.zeros((n_xi, n_xi)))
@@ -360,16 +376,8 @@ def potential_energy_fn(params: Dict[str, Array], q: Array, eps: float = 1e4 * g
360376
# add a small number to the bending strain to avoid singularities
361377
xi_epsed = apply_eps_to_bend_strains(xi, eps)
362378

363-
# cross-sectional area and second moment of area
364-
A = jnp.pi * params["r"] ** 2
365-
Ib = A**2 / (4 * jnp.pi)
366-
367-
# elastic and shear modulus
368-
E, G = params["E"], params["G"]
369-
# stiffness matrix of shape (num_segments, 3, 3)
370-
S = compute_stiffness_matrix_for_all_segments_fn(A, Ib, E, G)
371-
# we define the elastic matrix of shape (n_xi, n_xi) as K(xi) = K @ xi where K is equal to
372-
K = blk_diag(S)
379+
# compute the stiffness matrix
380+
K = stiffness_fn(params, formulate_in_strain_space=True)
373381
# elastic energy
374382
U_K = (xi - xi_eq).T @ K @ (xi - xi_eq) # evaluate K(xi) = K @ xi
375383

@@ -476,6 +484,8 @@ def operational_space_dynamical_matrices_fn(
476484

477485
auxiliary_fns = {
478486
"apply_eps_to_bend_strains": apply_eps_to_bend_strains,
487+
"classify_segment": classify_segment,
488+
"stiffness_fn": stiffness_fn,
479489
"jacobian_fn": jacobian_fn,
480490
"kinetic_energy_fn": kinetic_energy_fn,
481491
"potential_energy_fn": potential_energy_fn,

0 commit comments

Comments
 (0)