Skip to content

Commit 05904bd

Browse files
committed
Fix sign of gravitational potential energy
1 parent f61fec1 commit 05904bd

File tree

3 files changed

+24
-25
lines changed

3 files changed

+24
-25
lines changed

src/jsrm/symbolic_derivation/planar_hsa.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def symbolically_derive_planar_hsa_model(
219219
# inertia matrix
220220
B = sp.zeros(num_dof, num_dof)
221221
# potential energy
222-
U = sp.Matrix([[0]])
222+
U_g = sp.Matrix([[0]])
223223
# stiffness matrix
224224
Shat = sp.zeros(3 * num_segments, 3 * num_segments)
225225
# elastic vector
@@ -299,11 +299,11 @@ def symbolically_derive_planar_hsa_model(
299299
B = B + Br_ij
300300

301301
# derivative of the potential energy with respect to the point coordinate s
302-
dUr_ds = sp.simplify(rhor[i, j] * Ar[i, j] * g.T @ pr)
302+
dU_g_r_ds = sp.simplify(-rhor[i, j] * Ar[i, j] * g.T @ pr)
303303
# potential energy of the current segment
304-
U_ij = sp.simplify(sp.integrate(dUr_ds, (s, 0, l[i])))
304+
U_g_ij = sp.simplify(sp.integrate(dU_g_r_ds, (s, 0, l[i])))
305305
# add potential energy of segment to previous segments
306-
U = U + U_ij
306+
U_g = U_g + U_g_ij
307307

308308
# strains in physical HSA rod
309309
pxir = _sym_beta_fn(vxi, roff[i, j])
@@ -466,8 +466,8 @@ def symbolically_derive_planar_hsa_model(
466466
Bp = sp.simplify(Bp)
467467
B = B + Bp
468468
# potential energy of the platform
469-
Up = sp.simplify(mp * g.T @ pCoGp)
470-
U = U + Up
469+
U_g_p = sp.simplify(-mp * g.T @ pCoGp)
470+
U_g = U_g + U_g_p
471471

472472
# update the orientation for the next segment
473473
th_prev = th.subs(s, l[i])
@@ -507,8 +507,8 @@ def symbolically_derive_planar_hsa_model(
507507
Bpl = sp.simplify(Bpl)
508508
B = B + Bpl
509509
# add the gravitational potential energy of the payload
510-
Upl = sp.simplify(mpl * g.T @ pCoGpl)
511-
U = U + Upl
510+
Upl = sp.simplify(- mpl * g.T @ pCoGpl)
511+
U_g = U_g + Upl
512512

513513
# simplify mass matrix
514514
if simplify:
@@ -519,7 +519,7 @@ def symbolically_derive_planar_hsa_model(
519519
print("C =\n", C)
520520

521521
# compute the gravity force vector
522-
G = -U.jacobian(xi).transpose()
522+
G = U_g.jacobian(xi).transpose()
523523
if simplify:
524524
G = sp.simplify(G)
525525
print("G =\n", G)

src/jsrm/symbolic_derivation/planar_pcs.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ def symbolically_derive_planar_pcs_model(
6464
# inertia matrix
6565
B = sp.zeros(num_dof, num_dof)
6666
# potential energy
67-
U = sp.Matrix([[0]])
67+
U_g = sp.Matrix([[0]])
6868

6969
# symbol for the point coordinate
7070
s = sp.symbols("s", real=True, nonnegative=True)
@@ -134,15 +134,15 @@ def symbolically_derive_planar_pcs_model(
134134
B = B + B_i
135135

136136
# derivative of the potential energy with respect to the point coordinate s
137-
dU_ds = rho[i] * A[i] * g.T @ p
137+
dU_g_ds = -rho[i] * A[i] * g.T @ p
138138
if simplify_expressions:
139-
dU_ds = sp.simplify(dU_ds)
140-
# potential energy of the current segment
141-
U_i = sp.integrate(dU_ds, (s, 0, l[i]))
139+
dU_g_ds = sp.simplify(dU_g_ds)
140+
# gravitational potential energy of the current segment
141+
U_gi = sp.integrate(dU_g_ds, (s, 0, l[i]))
142142
if simplify_expressions:
143-
U_i = sp.simplify(U_i)
143+
U_gi = sp.simplify(U_gi)
144144
# add potential energy of segment to previous segments
145-
U = U + U_i
145+
U_g = U_g + U_gi
146146

147147
# update the orientation for the next segment
148148
th_prev = th.subs(s, l[i])
@@ -155,11 +155,11 @@ def symbolically_derive_planar_pcs_model(
155155
B = sp.simplify(B)
156156
print("B =\n", B)
157157

158-
C = compute_coriolis_matrix(B, xi, xi_d)
158+
C = compute_coriolis_matrix(B, xi, xi_d, simplify=simplify_expressions)
159159
print("C =\n", C)
160160

161161
# compute the gravity force vector
162-
G = -U.jacobian(xi).transpose()
162+
G = U_g.jacobian(xi).transpose()
163163
if simplify_expressions:
164164
G = sp.simplify(G)
165165
print("G =\n", G)
@@ -190,7 +190,7 @@ def symbolically_derive_planar_pcs_model(
190190
"B": B, # mass matrix
191191
"C": C, # coriolis matrix
192192
"G": G, # gravity vector
193-
"U": U, # gravitational potential energy
193+
"U_g": U_g, # gravitational potential energy
194194
},
195195
}
196196

src/jsrm/systems/planar_pcs.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -132,8 +132,8 @@ def select_params_for_lambdify_fn(params: Dict[str, Array]) -> List[Array]:
132132
G_lambda = sp.lambdify(
133133
params_syms_cat + sym_exps["state_syms"]["xi"], sym_exps["exps"]["G"], "jax"
134134
)
135-
U_lambda = sp.lambdify(
136-
params_syms_cat + sym_exps["state_syms"]["xi"], sym_exps["exps"]["U"], "jax"
135+
U_g_lambda = sp.lambdify(
136+
params_syms_cat + sym_exps["state_syms"]["xi"], sym_exps["exps"]["U_g"], "jax"
137137
)
138138

139139
compute_stiffness_matrix_for_all_segments_fn = vmap(
@@ -299,10 +299,9 @@ def dynamical_matrices_fn(
299299
Ib = A**2 / (4 * jnp.pi)
300300

301301
# elastic and shear modulus
302-
E, G = params["E"], params["G"]
303-
302+
elastic_modulus, shear_modulus = params["E"], params["G"]
304303
# stiffness matrix of shape (num_segments, 3, 3)
305-
S = compute_stiffness_matrix_for_all_segments_fn(A, Ib, E, G)
304+
S = compute_stiffness_matrix_for_all_segments_fn(A, Ib, elastic_modulus, shear_modulus)
306305

307306
# we define the elastic matrix of shape (n_xi, n_xi) as K(xi) = K @ xi where K is equal to
308307
K = blk_diag(S)
@@ -375,7 +374,7 @@ def potential_energy_fn(params: Dict[str, Array], q: Array, eps: float = 1e4 * g
375374

376375
# gravitational potential energy
377376
params_for_lambdify = select_params_for_lambdify_fn(params)
378-
U_G = U_lambda(*params_for_lambdify, *xi_epsed)
377+
U_G = U_g_lambda(*params_for_lambdify, *xi_epsed)
379378

380379
# total potential energy
381380
U = (U_G + U_K).squeeze()

0 commit comments

Comments
 (0)