def test_simulator_dynamics_symbolic(): with pks.Chain() as chain: f1 = pks.Fixture() j1 = pks.Joint() m1 = pks.Mass() pks.Link(f1, j1, l=0.0) pks.Link(j1, m1, l=1.0) with pks.Simulator(chain, f1) as sim: dynamics = sim.dynamics(g=(0.0, 0.0, 9.81)) print(dynamics) theta = sim.symbols.var_theta[0] assert sim.symbols.var_ddtheta[0] in dynamics ddtheta = dynamics[sim.symbols.var_ddtheta[0]] assert sp.simplify(9.81 * sp.cos(theta) - ddtheta) == 0
def test_simulator_forward_kinematics_symbolic(): l1, l2 = sp.symbols("l1 l2") with pks.Chain() as chain: f1 = pks.Fixture() j1 = pks.Joint() m1 = pks.Mass() m2 = pks.Mass() pks.Link(f1, j1, l=0.0) pks.Link(j1, m1, l=l1) pks.Link(m1, m2, l=l2) with pks.Simulator(chain, f1) as sim: trafos = sim.kinematics() theta = sim.symbols.theta[0] x, z = sim.symbols.x, sim.symbols.z assert sp.simplify(x + l1 * sp.cos(theta) - trafos[m1][0, 3]) == 0 assert sp.simplify(z - l1 * sp.sin(theta) - trafos[m1][2, 3]) == 0 assert sp.simplify(x + (l1 + l2) * sp.cos(theta) - trafos[m2][0, 3]) == 0 assert sp.simplify(z - (l1 + l2) * sp.sin(theta) - trafos[m2][2, 3]) == 0
#!/usr/bin/env python3 import sympy as sp import pykinsim as pks theta, l1, l2 = sp.symbols("theta l1 l2") with pks.Chain() as chain: f1 = pks.Fixture() j1 = pks.Joint(axis="y", theta=theta) m1 = pks.Mass() m2 = pks.Mass() pks.Link(f1, j1, l=0.0) pks.Link(j1, m1, l=l1) pks.Link(m1, m2, l=l2) with pks.Simulator(chain, f1) as sim: state = sim.symbolic_state() trafos = sim.kinematics(state) print("m1.x(t) =", trafos[m1][0, 3]) print("m1.z(t) =", trafos[m1][2, 3]) print("m2.x(t) =", trafos[m2][0, 3]) print("m2.z(t) =", trafos[m2][2, 3])
#!/usr/bin/env python3 import pykinsim as pks with pks.Chain() as chain: f1 = pks.Fixture(axes="yz") j1 = pks.Joint() m1 = pks.Mass() f2 = pks.Fixture(axes="xy") pks.Link(f1, j1, l=0.0) # Place the joint directly at the fixture pks.Link(j1, m1) pks.Link(m1, f2) with pks.Simulator(chain, root=f1) as sim: pks.visualization.animate(sim)
#!/usr/bin/env python3 import math import pykinsim as pks with pks.Chain() as chain: m1 = pks.Mass(m=2.0) f1 = pks.Fixture(axes="yz") # Allow motion in the x-direction j1 = pks.Joint() m2 = pks.Mass() j2 = pks.Joint() m3 = pks.Mass() pks.Link(j1, f1, l=0.0) pks.Link(j1, m1, l=0.5, ry=math.pi) pks.Link(j1, m2, l=0.33) pks.Link(m2, j2, l=0.33) pks.Link(j2, m3, l=0.33) with pks.Simulator(chain, root=f1) as sim: pks.visualization.animate(sim)
#!/usr/bin/env python3 import pykinsim as pks # Construct the kinematic chain with pks.Chain() as chain: f1 = pks.Fixture() # A fixture is an object that does not move j1 = pks.Joint(axis="y") # A rotational joint along the y-axis m1 = pks.Mass(m=1.0) # A mass of 1kg pks.Link(f1, j1, l=0.0) # Place the joint directly at the fixture pks.Link(j1, m1, l=1.0) # A link with length 1m # Build the underlying simulator and run the simulation with pks.Simulator(chain, root=f1) as sim: state = sim.initial_state() for i in range(3): # Advance the simulation for T=0.25s state = sim.run(0.25, state) # Draw the pendulum to a figure using matplotlib vis = sim.visualize(state, "matplotlib") vis.fig.savefig("pendulum_{}.svg".format(i), bbox_inches="tight", transparent=True)
#!/usr/bin/env python3 import math import pykinsim as pks with pks.Chain() as chain: f1 = pks.Fixture(axes="xyz") j1 = pks.Joint(axis="x") j2 = pks.Joint(axis="y", theta=math.pi) # Fold the arm in on itself as an initial state m1 = pks.Mass(m=1.0) pks.Link(f1, j1, l=0.0, ry=-math.pi/2) pks.Link(j1, j2) pks.Link(j2, m1) with pks.Simulator(chain, root=f1) as sim: pks.visualization.animate(sim)