test_jointsmethod.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. from sympy.core.function import expand
  2. from sympy.core.symbol import symbols
  3. from sympy.functions.elementary.trigonometric import (cos, sin)
  4. from sympy.matrices.dense import Matrix
  5. from sympy.simplify.trigsimp import trigsimp
  6. from sympy.physics.mechanics import (PinJoint, JointsMethod, Body, KanesMethod,
  7. PrismaticJoint, LagrangesMethod, inertia)
  8. from sympy.physics.vector import dynamicsymbols, ReferenceFrame
  9. from sympy.testing.pytest import raises
  10. from sympy.core.backend import zeros
  11. from sympy.utilities.lambdify import lambdify
  12. from sympy.solvers.solvers import solve
  13. t = dynamicsymbols._t # type: ignore
  14. def test_jointsmethod():
  15. P = Body('P')
  16. C = Body('C')
  17. Pin = PinJoint('P1', P, C)
  18. C_ixx, g = symbols('C_ixx g')
  19. q, u = dynamicsymbols('q_P1, u_P1')
  20. P.apply_force(g*P.y)
  21. method = JointsMethod(P, Pin)
  22. assert method.frame == P.frame
  23. assert method.bodies == [C, P]
  24. assert method.loads == [(P.masscenter, g*P.frame.y)]
  25. assert method.q == Matrix([q])
  26. assert method.u == Matrix([u])
  27. assert method.kdes == Matrix([u - q.diff()])
  28. soln = method.form_eoms()
  29. assert soln == Matrix([[-C_ixx*u.diff()]])
  30. assert method.forcing_full == Matrix([[u], [0]])
  31. assert method.mass_matrix_full == Matrix([[1, 0], [0, C_ixx]])
  32. assert isinstance(method.method, KanesMethod)
  33. def test_jointmethod_duplicate_coordinates_speeds():
  34. P = Body('P')
  35. C = Body('C')
  36. T = Body('T')
  37. q, u = dynamicsymbols('q u')
  38. P1 = PinJoint('P1', P, C, q)
  39. P2 = PrismaticJoint('P2', C, T, q)
  40. raises(ValueError, lambda: JointsMethod(P, P1, P2))
  41. P1 = PinJoint('P1', P, C, speeds=u)
  42. P2 = PrismaticJoint('P2', C, T, speeds=u)
  43. raises(ValueError, lambda: JointsMethod(P, P1, P2))
  44. P1 = PinJoint('P1', P, C, q, u)
  45. P2 = PrismaticJoint('P2', C, T, q, u)
  46. raises(ValueError, lambda: JointsMethod(P, P1, P2))
  47. def test_complete_simple_double_pendulum():
  48. q1, q2 = dynamicsymbols('q1 q2')
  49. u1, u2 = dynamicsymbols('u1 u2')
  50. m, l, g = symbols('m l g')
  51. C = Body('C') # ceiling
  52. PartP = Body('P', mass=m)
  53. PartR = Body('R', mass=m)
  54. J1 = PinJoint('J1', C, PartP, speeds=u1, coordinates=q1,
  55. child_point=-l*PartP.x, joint_axis=C.z)
  56. J2 = PinJoint('J2', PartP, PartR, speeds=u2, coordinates=q2,
  57. child_point=-l*PartR.x, joint_axis=PartP.z)
  58. PartP.apply_force(m*g*C.x)
  59. PartR.apply_force(m*g*C.x)
  60. method = JointsMethod(C, J1, J2)
  61. method.form_eoms()
  62. assert expand(method.mass_matrix_full) == Matrix([[1, 0, 0, 0],
  63. [0, 1, 0, 0],
  64. [0, 0, 2*l**2*m*cos(q2) + 3*l**2*m, l**2*m*cos(q2) + l**2*m],
  65. [0, 0, l**2*m*cos(q2) + l**2*m, l**2*m]])
  66. assert trigsimp(method.forcing_full) == trigsimp(Matrix([[u1], [u2], [-g*l*m*(sin(q1 + q2) + sin(q1)) -
  67. g*l*m*sin(q1) + l**2*m*(2*u1 + u2)*u2*sin(q2)],
  68. [-g*l*m*sin(q1 + q2) - l**2*m*u1**2*sin(q2)]]))
  69. def test_two_dof_joints():
  70. q1, q2, u1, u2 = dynamicsymbols('q1 q2 u1 u2')
  71. m, c1, c2, k1, k2 = symbols('m c1 c2 k1 k2')
  72. W = Body('W')
  73. B1 = Body('B1', mass=m)
  74. B2 = Body('B2', mass=m)
  75. J1 = PrismaticJoint('J1', W, B1, coordinates=q1, speeds=u1)
  76. J2 = PrismaticJoint('J2', B1, B2, coordinates=q2, speeds=u2)
  77. W.apply_force(k1*q1*W.x, reaction_body=B1)
  78. W.apply_force(c1*u1*W.x, reaction_body=B1)
  79. B1.apply_force(k2*q2*W.x, reaction_body=B2)
  80. B1.apply_force(c2*u2*W.x, reaction_body=B2)
  81. method = JointsMethod(W, J1, J2)
  82. method.form_eoms()
  83. MM = method.mass_matrix
  84. forcing = method.forcing
  85. rhs = MM.LUsolve(forcing)
  86. assert expand(rhs[0]) == expand((-k1 * q1 - c1 * u1 + k2 * q2 + c2 * u2)/m)
  87. assert expand(rhs[1]) == expand((k1 * q1 + c1 * u1 - 2 * k2 * q2 - 2 *
  88. c2 * u2) / m)
  89. def test_simple_pedulum():
  90. l, m, g = symbols('l m g')
  91. C = Body('C')
  92. b = Body('b', mass=m)
  93. q = dynamicsymbols('q')
  94. P = PinJoint('P', C, b, speeds=q.diff(t), coordinates=q,
  95. child_point=-l * b.x, joint_axis=C.z)
  96. b.potential_energy = - m * g * l * cos(q)
  97. method = JointsMethod(C, P)
  98. method.form_eoms(LagrangesMethod)
  99. rhs = method.rhs()
  100. assert rhs[1] == -g*sin(q)/l
  101. def test_chaos_pendulum():
  102. #https://www.pydy.org/examples/chaos_pendulum.html
  103. mA, mB, lA, lB, IAxx, IBxx, IByy, IBzz, g = symbols('mA, mB, lA, lB, IAxx, IBxx, IByy, IBzz, g')
  104. theta, phi, omega, alpha = dynamicsymbols('theta phi omega alpha')
  105. A = ReferenceFrame('A')
  106. B = ReferenceFrame('B')
  107. rod = Body('rod', mass=mA, frame=A, central_inertia=inertia(A, IAxx, IAxx, 0))
  108. plate = Body('plate', mass=mB, frame=B, central_inertia=inertia(B, IBxx, IByy, IBzz))
  109. C = Body('C')
  110. J1 = PinJoint('J1', C, rod, coordinates=theta, speeds=omega,
  111. child_point=-lA * rod.z, joint_axis=C.y)
  112. J2 = PinJoint('J2', rod, plate, coordinates=phi, speeds=alpha,
  113. parent_point=(lB - lA) * rod.z, joint_axis=rod.z)
  114. rod.apply_force(mA*g*C.z)
  115. plate.apply_force(mB*g*C.z)
  116. method = JointsMethod(C, J1, J2)
  117. method.form_eoms()
  118. MM = method.mass_matrix
  119. forcing = method.forcing
  120. rhs = MM.LUsolve(forcing)
  121. xd = (-2 * IBxx * alpha * omega * sin(phi) * cos(phi) + 2 * IByy * alpha * omega * sin(phi) *
  122. cos(phi) - g * lA * mA * sin(theta) - g * lB * mB * sin(theta)) / (IAxx + IBxx *
  123. sin(phi)**2 + IByy * cos(phi)**2 + lA**2 * mA + lB**2 * mB)
  124. assert (rhs[0] - xd).simplify() == 0
  125. xd = (IBxx - IByy) * omega**2 * sin(phi) * cos(phi) / IBzz
  126. assert (rhs[1] - xd).simplify() == 0
  127. def test_four_bar_linkage_with_manual_constraints():
  128. q1, q2, q3, u1, u2, u3 = dynamicsymbols('q1:4, u1:4')
  129. l1, l2, l3, l4, rho = symbols('l1:5, rho')
  130. N = ReferenceFrame('N')
  131. inertias = [inertia(N, 0, 0, rho * l ** 3 / 12) for l in (l1, l2, l3, l4)]
  132. link1 = Body('Link1', frame=N, mass=rho * l1, central_inertia=inertias[0])
  133. link2 = Body('Link2', mass=rho * l2, central_inertia=inertias[1])
  134. link3 = Body('Link3', mass=rho * l3, central_inertia=inertias[2])
  135. link4 = Body('Link4', mass=rho * l4, central_inertia=inertias[3])
  136. joint1 = PinJoint(
  137. 'J1', link1, link2, coordinates=q1, speeds=u1, joint_axis=link1.z,
  138. parent_point=l1 / 2 * link1.x, child_point=-l2 / 2 * link2.x)
  139. joint2 = PinJoint(
  140. 'J2', link2, link3, coordinates=q2, speeds=u2, joint_axis=link2.z,
  141. parent_point=l2 / 2 * link2.x, child_point=-l3 / 2 * link3.x)
  142. joint3 = PinJoint(
  143. 'J3', link3, link4, coordinates=q3, speeds=u3, joint_axis=link3.z,
  144. parent_point=l3 / 2 * link3.x, child_point=-l4 / 2 * link4.x)
  145. loop = link4.masscenter.pos_from(link1.masscenter) \
  146. + l1 / 2 * link1.x + l4 / 2 * link4.x
  147. fh = Matrix([loop.dot(link1.x), loop.dot(link1.y)])
  148. method = JointsMethod(link1, joint1, joint2, joint3)
  149. t = dynamicsymbols._t
  150. qdots = solve(method.kdes, [q1.diff(t), q2.diff(t), q3.diff(t)])
  151. fhd = fh.diff(t).subs(qdots)
  152. kane = KanesMethod(method.frame, q_ind=[q1], u_ind=[u1],
  153. q_dependent=[q2, q3], u_dependent=[u2, u3],
  154. kd_eqs=method.kdes, configuration_constraints=fh,
  155. velocity_constraints=fhd, forcelist=method.loads,
  156. bodies=method.bodies)
  157. fr, frs = kane.kanes_equations()
  158. assert fr == zeros(1)
  159. # Numerically check the mass- and forcing-matrix
  160. p = Matrix([l1, l2, l3, l4, rho])
  161. q = Matrix([q1, q2, q3])
  162. u = Matrix([u1, u2, u3])
  163. eval_m = lambdify((q, p), kane.mass_matrix)
  164. eval_f = lambdify((q, u, p), kane.forcing)
  165. eval_fhd = lambdify((q, u, p), fhd)
  166. p_vals = [0.13, 0.24, 0.21, 0.34, 997]
  167. q_vals = [2.1, 0.6655470375077588, 2.527408138024188] # Satisfies fh
  168. u_vals = [0.2, -0.17963733938852067, 0.1309060540601612] # Satisfies fhd
  169. mass_check = Matrix([[3.452709815256506e+01, 7.003948798374735e+00,
  170. -4.939690970641498e+00],
  171. [-2.203792703880936e-14, 2.071702479957077e-01,
  172. 2.842917573033711e-01],
  173. [-1.300000000000123e-01, -8.836934896046506e-03,
  174. 1.864891330060847e-01]])
  175. forcing_check = Matrix([[-0.031211821321648],
  176. [-0.00066022608181],
  177. [0.001813559741243]])
  178. eps = 1e-10
  179. assert all(abs(x) < eps for x in eval_fhd(q_vals, u_vals, p_vals))
  180. assert all(abs(x) < eps for x in
  181. (Matrix(eval_m(q_vals, p_vals)) - mass_check))
  182. assert all(abs(x) < eps for x in
  183. (Matrix(eval_f(q_vals, u_vals, p_vals)) - forcing_check))