1
1
import dill
2
2
from pathlib import Path
3
3
import sympy as sp
4
- from typing import Callable , Dict , Tuple , Union
4
+ from typing import Callable , Dict , Optional , Tuple , Union
5
5
6
6
from .symbolic_utils import compute_coriolis_matrix
7
7
8
8
9
9
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
11
11
) -> Dict :
12
12
"""
13
13
Symbolically derive the kinematics and dynamics of a planar continuum soft robot modelled with
14
14
Piecewise Constant Strain.
15
15
Args:
16
16
num_segments: number of constant strain segments
17
17
filepath: path to save the derived model
18
+ simplify_expressions: if true, simplify the expressions (might take some time). Default is True.
18
19
Returns:
19
20
sym_exps: dictionary with entries
20
21
params_syms: dictionary of robot parameters
@@ -118,16 +119,24 @@ def symbolically_derive_planar_pcs_model(
118
119
J_sms .append (J )
119
120
120
121
# 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 )
122
125
# 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 )
124
129
# add mass matrix of segment to previous segments
125
130
B = B + B_i
126
131
127
132
# 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 )
129
136
# 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 )
131
140
# add potential energy of segment to previous segments
132
141
U = U + U_i
133
142
@@ -137,15 +146,18 @@ def symbolically_derive_planar_pcs_model(
137
146
# update the position for the next segment
138
147
p_prev = p .subs (s , l [i ])
139
148
140
- # simplify mass matrix
141
- B = sp .simplify (B )
149
+ if simplify_expressions :
150
+ # simplify mass matrix
151
+ B = sp .simplify (B )
142
152
print ("B =\n " , B )
143
153
144
154
C = compute_coriolis_matrix (B , xi , xi_d )
145
155
print ("C =\n " , C )
146
156
147
157
# 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 )
149
161
print ("G =\n " , G )
150
162
151
163
# dictionary with expressions
0 commit comments