Skip to content

Commit 8065b56

Browse files
committed
Add argument to symbolically_derive_planar_pcs_model to decide if sympy simplify should be called or not
1 parent 9104399 commit 8065b56

File tree

3 files changed

+22
-10
lines changed

3 files changed

+22
-10
lines changed

examples/derive_planar_pcs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@
1212
/ f"planar_pcs_ns-{NUM_SEGMENTS}.dill"
1313
)
1414
symbolically_derive_planar_pcs_model(
15-
num_segments=NUM_SEGMENTS, filepath=sym_exp_filepath
15+
num_segments=NUM_SEGMENTS, filepath=sym_exp_filepath, simplify_expressions=True
1616
)

src/jsrm/symbolic_derivation/planar_pcs.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
import dill
22
from pathlib import Path
33
import sympy as sp
4-
from typing import Callable, Dict, Tuple, Union
4+
from typing import Callable, Dict, Optional, Tuple, Union
55

66
from .symbolic_utils import compute_coriolis_matrix
77

88

99
def symbolically_derive_planar_pcs_model(
10-
num_segments: int, filepath: Union[str, Path] = None
10+
num_segments: int, filepath: Optional[Union[str, Path]] = None, simplify_expressions: bool = True
1111
) -> Dict:
1212
"""
1313
Symbolically derive the kinematics and dynamics of a planar continuum soft robot modelled with
1414
Piecewise Constant Strain.
1515
Args:
1616
num_segments: number of constant strain segments
1717
filepath: path to save the derived model
18+
simplify_expressions: if true, simplify the expressions (might take some time). Default is True.
1819
Returns:
1920
sym_exps: dictionary with entries
2021
params_syms: dictionary of robot parameters
@@ -118,16 +119,24 @@ def symbolically_derive_planar_pcs_model(
118119
J_sms.append(J)
119120

120121
# derivative of mass matrix with respect to the point coordinate s
121-
dB_ds = sp.simplify(rho[i] * A[i] * Jp.T @ Jp + rho[i] * I[i] * Jo.T @ Jo)
122+
dB_ds = rho[i] * A[i] * Jp.T @ Jp + rho[i] * I[i] * Jo.T @ Jo
123+
if simplify_expressions:
124+
dB_ds = sp.simplify(dB_ds)
122125
# mass matrix of the current segment
123-
B_i = sp.simplify(sp.integrate(dB_ds, (s, 0, l[i])))
126+
B_i = sp.integrate(dB_ds, (s, 0, l[i]))
127+
if simplify_expressions:
128+
B_i = sp.simplify(B_i)
124129
# add mass matrix of segment to previous segments
125130
B = B + B_i
126131

127132
# derivative of the potential energy with respect to the point coordinate s
128-
dU_ds = sp.simplify(rho[i] * A[i] * g.T @ p)
133+
dU_ds = rho[i] * A[i] * g.T @ p
134+
if simplify_expressions:
135+
dU_ds = sp.simplify(dU_ds)
129136
# potential energy of the current segment
130-
U_i = sp.simplify(sp.integrate(dU_ds, (s, 0, l[i])))
137+
U_i = sp.integrate(dU_ds, (s, 0, l[i]))
138+
if simplify_expressions:
139+
U_i = sp.simplify(U_i)
131140
# add potential energy of segment to previous segments
132141
U = U + U_i
133142

@@ -137,15 +146,18 @@ def symbolically_derive_planar_pcs_model(
137146
# update the position for the next segment
138147
p_prev = p.subs(s, l[i])
139148

140-
# simplify mass matrix
141-
B = sp.simplify(B)
149+
if simplify_expressions:
150+
# simplify mass matrix
151+
B = sp.simplify(B)
142152
print("B =\n", B)
143153

144154
C = compute_coriolis_matrix(B, xi, xi_d)
145155
print("C =\n", C)
146156

147157
# compute the gravity force vector
148-
G = sp.simplify(-U.jacobian(xi).transpose())
158+
G = -U.jacobian(xi).transpose()
159+
if simplify_expressions:
160+
G = sp.simplify(G)
149161
print("G =\n", G)
150162

151163
# dictionary with expressions
397 KB
Binary file not shown.

0 commit comments

Comments
 (0)